Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
method5.go298 linesDownload Raw Back to test
1// run2 3// Copyright 2013 The Go Authors. All rights reserved.4// Use of this source code is governed by a BSD-style5// license that can be found in the LICENSE file.6 7package main8 9// Concrete types implementing M method.10// Smaller than a word, word-sized, larger than a word.11// Value and pointer receivers.12 13type Tinter interface {14	M(int, byte) (byte, int)15}16 17type Tsmallv byte18 19func (v Tsmallv) M(x int, b byte) (byte, int) { return b, x+int(v) }20 21type Tsmallp byte22 23func (p *Tsmallp) M(x int, b byte) (byte, int) { return b, x+int(*p) }24 25type Twordv uintptr26 27func (v Twordv) M(x int, b byte) (byte, int) { return b, x+int(v) }28 29type Twordp uintptr30 31func (p *Twordp) M(x int, b byte) (byte, int) { return b, x+int(*p) }32 33type Tbigv [2]uintptr34 35func (v Tbigv) M(x int, b byte) (byte, int) { return b, x+int(v[0])+int(v[1]) }36 37type Tbigp [2]uintptr38 39func (p *Tbigp) M(x int, b byte) (byte, int) { return b, x+int(p[0])+int(p[1]) }40 41// Again, with an unexported method.42 43type tsmallv byte44 45func (v tsmallv) m(x int, b byte) (byte, int) { return b, x+int(v) }46 47type tsmallp byte48 49func (p *tsmallp) m(x int, b byte) (byte, int) { return b, x+int(*p) }50 51type twordv uintptr52 53func (v twordv) m(x int, b byte) (byte, int) { return b, x+int(v) }54 55type twordp uintptr56 57func (p *twordp) m(x int, b byte) (byte, int) { return b, x+int(*p) }58 59type tbigv [2]uintptr60 61func (v tbigv) m(x int, b byte) (byte, int) { return b, x+int(v[0])+int(v[1]) }62 63type tbigp [2]uintptr64 65func (p *tbigp) m(x int, b byte) (byte, int) { return b, x+int(p[0])+int(p[1]) }66 67type tinter interface {68	m(int, byte) (byte, int)69}70 71// Embedding via pointer.72 73type T1 struct {74	T275}76 77type T2 struct {78	*T379}80 81type T3 struct {82	*T483}84 85type T4 struct {86}87 88func (t4 T4) M(x int, b byte) (byte, int) { return b, x+40 }89 90var failed = false91 92func CheckI(name string, i Tinter, inc int) {93	b, x := i.M(1000, 99)94	if b != 99 || x != 1000+inc {95		failed = true96		print(name, ".M(1000, 99) = ", b, ", ", x, " want 99, ", 1000+inc, "\n")97	}98	99	CheckF("(i="+name+")", i.M, inc)100}101 102func CheckF(name string, f func(int, byte) (byte, int), inc int) {103	b, x := f(1000, 99)104	if b != 99 || x != 1000+inc {105		failed = true106		print(name, "(1000, 99) = ", b, ", ", x, " want 99, ", 1000+inc, "\n")107	}108}109 110func checkI(name string, i tinter, inc int) {111	b, x := i.m(1000, 99)112	if b != 99 || x != 1000+inc {113		failed = true114		print(name, ".m(1000, 99) = ", b, ", ", x, " want 99, ", 1000+inc, "\n")115	}116	117	checkF("(i="+name+")", i.m, inc)118}119 120func checkF(name string, f func(int, byte) (byte, int), inc int) {121	b, x := f(1000, 99)122	if b != 99 || x != 1000+inc {123		failed = true124		print(name, "(1000, 99) = ", b, ", ", x, " want 99, ", 1000+inc, "\n")125	}126}127 128func shouldPanic(f func()) {129	defer func() {130		if recover() == nil {131			panic("not panicking")132		}133	}()134	f()135}136 137func shouldNotPanic(f func()) {138	f()139}140 141func main() {142	sv := Tsmallv(1)143	CheckI("sv", sv, 1)144	CheckF("sv.M", sv.M, 1)145	CheckF("(&sv).M", (&sv).M, 1)146	psv := &sv147	CheckI("psv", psv, 1)148	CheckF("psv.M", psv.M, 1)149	CheckF("(*psv).M", (*psv).M, 1)150 151	sp := Tsmallp(2)152	CheckI("&sp", &sp, 2)153	CheckF("sp.M", sp.M, 2)154	CheckF("(&sp).M", (&sp).M, 2)155	psp := &sp156	CheckI("psp", psp, 2)157	CheckF("psp.M", psp.M, 2)158	CheckF("(*psp).M", (*psp).M, 2)159 160	wv := Twordv(3)161	CheckI("wv", wv, 3)162	CheckF("wv.M", wv.M, 3)163	CheckF("(&wv).M", (&wv).M, 3)164	pwv := &wv165	CheckI("pwv", pwv, 3)166	CheckF("pwv.M", pwv.M, 3)167	CheckF("(*pwv).M", (*pwv).M, 3)168 169	wp := Twordp(4)170	CheckI("&wp", &wp, 4)171	CheckF("wp.M", wp.M, 4)172	CheckF("(&wp).M", (&wp).M, 4)173	pwp := &wp174	CheckI("pwp", pwp, 4)175	CheckF("pwp.M", pwp.M, 4)176	CheckF("(*pwp).M", (*pwp).M, 4)177 178	bv := Tbigv([2]uintptr{5, 6})179	pbv := &bv180	CheckI("bv", bv, 11)181	CheckF("bv.M", bv.M, 11)182	CheckF("(&bv).M", (&bv).M, 11)183	CheckI("pbv", pbv, 11)184	CheckF("pbv.M", pbv.M, 11)185	CheckF("(*pbv).M", (*pbv).M, 11)186	187	bp := Tbigp([2]uintptr{7,8})188	CheckI("&bp", &bp, 15)189	CheckF("bp.M", bp.M, 15)190	CheckF("(&bp).M", (&bp).M, 15)191	pbp := &bp192	CheckI("pbp", pbp, 15)193	CheckF("pbp.M", pbp.M, 15)194	CheckF("(*pbp).M", (*pbp).M, 15)195 196	_sv := tsmallv(1)197	checkI("_sv", _sv, 1)198	checkF("_sv.m", _sv.m, 1)199	checkF("(&_sv).m", (&_sv).m, 1)200	_psv := &_sv201	checkI("_psv", _psv, 1)202	checkF("_psv.m", _psv.m, 1)203	checkF("(*_psv).m", (*_psv).m, 1)204 205	_sp := tsmallp(2)206	checkI("&_sp", &_sp, 2)207	checkF("_sp.m", _sp.m, 2)208	checkF("(&_sp).m", (&_sp).m, 2)209	_psp := &_sp210	checkI("_psp", _psp, 2)211	checkF("_psp.m", _psp.m, 2)212	checkF("(*_psp).m", (*_psp).m, 2)213 214	_wv := twordv(3)215	checkI("_wv", _wv, 3)216	checkF("_wv.m", _wv.m, 3)217	checkF("(&_wv).m", (&_wv).m, 3)218	_pwv := &_wv219	checkI("_pwv", _pwv, 3)220	checkF("_pwv.m", _pwv.m, 3)221	checkF("(*_pwv).m", (*_pwv).m, 3)222 223	_wp := twordp(4)224	checkI("&_wp", &_wp, 4)225	checkF("_wp.m", _wp.m, 4)226	checkF("(&_wp).m", (&_wp).m, 4)227	_pwp := &_wp228	checkI("_pwp", _pwp, 4)229	checkF("_pwp.m", _pwp.m, 4)230	checkF("(*_pwp).m", (*_pwp).m, 4)231 232	_bv := tbigv([2]uintptr{5, 6})233	_pbv := &_bv234	checkI("_bv", _bv, 11)235	checkF("_bv.m", _bv.m, 11)236	checkF("(&_bv).m", (&_bv).m, 11)237	checkI("_pbv", _pbv, 11)238	checkF("_pbv.m", _pbv.m, 11)239	checkF("(*_pbv).m", (*_pbv).m, 11)240	241	_bp := tbigp([2]uintptr{7,8})242	checkI("&_bp", &_bp, 15)243	checkF("_bp.m", _bp.m, 15)244	checkF("(&_bp).m", (&_bp).m, 15)245	_pbp := &_bp246	checkI("_pbp", _pbp, 15)247	checkF("_pbp.m", _pbp.m, 15)248	checkF("(*_pbp).m", (*_pbp).m, 15)249	250	t4 := T4{}251	t3 := T3{&t4}252	t2 := T2{&t3}253	t1 := T1{t2}254	CheckI("t4", t4, 40)255	CheckI("&t4", &t4, 40)256	CheckI("t3", t3, 40)257	CheckI("&t3", &t3, 40)258	CheckI("t2", t2, 40)259	CheckI("&t2", &t2, 40)260	CheckI("t1", t1, 40)261	CheckI("&t1", &t1, 40)262	263	// x.M panics if x is an interface type and is nil,264	// or if x.M expands to (*x).M where x is nil,265	// or if x.M expands to x.y.z.w.M where something266	// along the evaluation of x.y.z.w is nil.267	var f func(int, byte) (byte, int)268	shouldPanic(func() { psv = nil; f = psv.M })269	shouldPanic(func() { pwv = nil; f = pwv.M })270	shouldPanic(func() { pbv = nil; f = pbv.M })271	shouldPanic(func() { var i Tinter; f = i.M })272	shouldPanic(func() { _psv = nil; f = _psv.m })273	shouldPanic(func() { _pwv = nil; f = _pwv.m })274	shouldPanic(func() { _pbv = nil; f = _pbv.m })275	shouldPanic(func() { var _i tinter; f = _i.m })276	shouldPanic(func() { var t1 T1; f = t1.M })277	shouldPanic(func() { var t2 T2; f = t2.M })278	shouldPanic(func() { var t3 *T3; f = t3.M })279	shouldPanic(func() { var t3 T3; f = t3.M })280 281	if f != nil {282		panic("something set f")283	}284	285	// x.M does not panic if x is a nil pointer and286	// M is a method with a pointer receiver.287	shouldNotPanic(func() { psp = nil; f = psp.M })288	shouldNotPanic(func() { pwp = nil; f = pwp.M })289	shouldNotPanic(func() { pbp = nil; f = pbp.M })290	shouldNotPanic(func() { _psp = nil; f = _psp.m })291	shouldNotPanic(func() { _pwp = nil; f = _pwp.m })292	shouldNotPanic(func() { _pbp = nil; f = _pbp.m })293	shouldNotPanic(func() { var t4 T4; f = t4.M })294	if f == nil {295		panic("nothing set f")296	}297}298 
codekingpro/portable-devtools · Team Ai