1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
30 func isDigit(c uint8) bool { return '0' <= c && c <= '9' }
32 func assert(t *testing.T, s, want string) {
34 t.Errorf("have %#q want %#q", s, want)
38 func typestring(i interface{}) string { return Typeof(i).String() }
40 var typeTests = []pair{
41 {struct{ x int }{}, "int"},
42 {struct{ x int8 }{}, "int8"},
43 {struct{ x int16 }{}, "int16"},
44 {struct{ x int32 }{}, "int32"},
45 {struct{ x int64 }{}, "int64"},
46 {struct{ x uint }{}, "uint"},
47 {struct{ x uint8 }{}, "uint8"},
48 {struct{ x uint16 }{}, "uint16"},
49 {struct{ x uint32 }{}, "uint32"},
50 {struct{ x uint64 }{}, "uint64"},
51 {struct{ x float }{}, "float"},
52 {struct{ x float32 }{}, "float32"},
53 {struct{ x float64 }{}, "float64"},
54 {struct{ x int8 }{}, "int8"},
55 {struct{ x (**int8) }{}, "**int8"},
56 {struct{ x (**integer) }{}, "**reflect_test.integer"},
57 {struct{ x ([32]int32) }{}, "[32]int32"},
58 {struct{ x ([]int8) }{}, "[]int8"},
59 {struct{ x (map[string]int32) }{}, "map[string] int32"},
60 {struct{ x (chan<- string) }{}, "chan<- string"},
67 "struct { c chan *int32; d float32 }",
69 {struct{ x (func(a int8, b int32)) }{}, "func(int8, int32)"},
72 c func(chan *integer, *int8)
75 "struct { c func(chan *reflect_test.integer, *int8) }",
83 "struct { a int8; b int32 }",
92 "struct { a int8; b int8; c int32 }",
102 "struct { a int8; b int8; c int8; d int32 }",
113 "struct { a int8; b int8; c int8; d int8; e int32 }",
125 "struct { a int8; b int8; c int8; d int8; e int8; f int32 }",
132 `struct { a int8 "hi there" }`,
136 a int8 "hi \x00there\t\n\"\\"
139 `struct { a int8 "hi \x00there\t\n\"\\" }`,
146 "struct { f func(...int) }",
150 a(func(func(int) int) func(func(int)) int)
154 "interface { a(func(func(int) int) func(func(int)) int); b() }",
158 var valueTests = []pair{
167 {(float32)(0), "256.25"},
168 {(float64)(0), "512.125"},
169 {(string)(""), "stringy cheese"},
170 {(bool)(false), "true"},
171 {(*int8)(nil), "*int8(0)"},
172 {(**int8)(nil), "**int8(0)"},
173 {[5]int32{}, "[5]int32{0, 0, 0, 0, 0}"},
174 {(**integer)(nil), "**reflect_test.integer(0)"},
175 {(map[string]int32)(nil), "map[string] int32{<can't iterate on maps>}"},
176 {(chan<- string)(nil), "chan<- string"},
181 "struct { c chan *int32; d float32 }{chan *int32, 0}",
183 {(func(a int8, b int32))(nil), "func(int8, int32)(0)"},
184 {struct{ c func(chan *integer, *int8) }{},
185 "struct { c func(chan *reflect_test.integer, *int8) }{func(chan *reflect_test.integer, *int8)(0)}",
191 "struct { a int8; b int32 }{0, 0}",
198 "struct { a int8; b int8; c int32 }{0, 0, 0}",
202 func testType(t *testing.T, i int, typ Type, want string) {
205 t.Errorf("#%d: have %#q, want %#q", i, s, want)
209 func TestTypes(t *testing.T) {
210 for i, tt := range typeTests {
211 testType(t, i, NewValue(tt.i).(*StructValue).Field(0).Type(), tt.s)
215 func TestSet(t *testing.T) {
216 for i, tt := range valueTests {
218 switch v := v.(type) {
220 switch v.Type().Kind() {
233 switch v.Type().Kind() {
246 switch v.Type().Kind() {
255 switch v.Type().Kind() {
257 v.Set(53200.0 + 100i)
264 v.Set("stringy cheese")
268 s := valueToString(v)
270 t.Errorf("#%d: have %#q, want %#q", i, s, tt.s)
275 func TestSetValue(t *testing.T) {
276 for i, tt := range valueTests {
278 switch v := v.(type) {
280 switch v.Type().Kind() {
282 v.SetValue(NewValue(int(132)))
284 v.SetValue(NewValue(int8(8)))
286 v.SetValue(NewValue(int16(16)))
288 v.SetValue(NewValue(int32(32)))
290 v.SetValue(NewValue(int64(64)))
293 switch v.Type().Kind() {
295 v.SetValue(NewValue(uint(132)))
297 v.SetValue(NewValue(uint8(8)))
299 v.SetValue(NewValue(uint16(16)))
301 v.SetValue(NewValue(uint32(32)))
303 v.SetValue(NewValue(uint64(64)))
306 switch v.Type().Kind() {
308 v.SetValue(NewValue(float(128.5)))
310 v.SetValue(NewValue(float32(256.25)))
312 v.SetValue(NewValue(float64(512.125)))
315 switch v.Type().Kind() {
317 v.SetValue(NewValue(complex(53200.0 + 100i)))
319 v.SetValue(NewValue(complex64(532.125 + 10i)))
321 v.SetValue(NewValue(complex128(564.25 + 1i)))
325 v.SetValue(NewValue("stringy cheese"))
327 v.SetValue(NewValue(true))
329 s := valueToString(v)
331 t.Errorf("#%d: have %#q, want %#q", i, s, tt.s)
338 var valueToStringTests = []pair{
343 {T{123, 456.75, "hello", &_i}, "reflect_test.T{123, 456.75, hello, *int(&7)}"},
344 {new(chan *T), "*chan *reflect_test.T(&chan *reflect_test.T)"},
345 {[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"},
346 {&[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "*[10]int(&[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})"},
347 {[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"},
348 {&[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "*[]int(&[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})"},
351 func TestValueToString(t *testing.T) {
352 for i, test := range valueToStringTests {
353 s := valueToString(NewValue(test.i))
355 t.Errorf("#%d: have %#q, want %#q", i, s, test.s)
360 func TestArrayElemSet(t *testing.T) {
361 v := NewValue([10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
362 v.(*ArrayValue).Elem(4).(*IntValue).Set(123)
363 s := valueToString(v)
364 const want = "[10]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"
366 t.Errorf("[10]int: have %#q want %#q", s, want)
369 v = NewValue([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
370 v.(*SliceValue).Elem(4).(*IntValue).Set(123)
372 const want1 = "[]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"
374 t.Errorf("[]int: have %#q want %#q", s, want1)
378 func TestPtrPointTo(t *testing.T) {
383 vip.(*PtrValue).Elem().(*PtrValue).PointTo(vi)
385 t.Errorf("got %d, want 1234", *ip)
389 vp := NewValue(ip).(*PtrValue)
390 vp.PointTo(vp.Elem())
392 t.Errorf("got non-nil (%p), want nil", ip)
396 func TestPtrSetNil(t *testing.T) {
400 vip.(*PtrValue).Elem().(*PtrValue).Set(nil)
402 t.Errorf("got non-nil (%d), want nil", *ip)
406 func TestMapSetNil(t *testing.T) {
407 m := make(map[string]int)
409 vm.(*PtrValue).Elem().(*MapValue).Set(nil)
411 t.Errorf("got non-nil (%p), want nil", m)
416 func TestAll(t *testing.T) {
417 testType(t, 1, Typeof((int8)(0)), "int8")
418 testType(t, 2, Typeof((*int8)(nil)).(*PtrType).Elem(), "int8")
420 typ := Typeof((*struct {
424 testType(t, 3, typ, "*struct { c chan *int32; d float32 }")
425 etyp := typ.(*PtrType).Elem()
426 testType(t, 4, etyp, "struct { c chan *int32; d float32 }")
427 styp := etyp.(*StructType)
429 testType(t, 5, f.Type, "chan *int32")
431 f, present := styp.FieldByName("d")
433 t.Errorf("FieldByName says present field is absent")
435 testType(t, 6, f.Type, "float32")
437 f, present = styp.FieldByName("absent")
439 t.Errorf("FieldByName says absent field is present")
442 typ = Typeof([32]int32{})
443 testType(t, 7, typ, "[32]int32")
444 testType(t, 8, typ.(*ArrayType).Elem(), "int32")
446 typ = Typeof((map[string]*int32)(nil))
447 testType(t, 9, typ, "map[string] *int32")
448 mtyp := typ.(*MapType)
449 testType(t, 10, mtyp.Key(), "string")
450 testType(t, 11, mtyp.Elem(), "*int32")
452 typ = Typeof((chan<- string)(nil))
453 testType(t, 12, typ, "chan<- string")
454 testType(t, 13, typ.(*ChanType).Elem(), "string")
456 // make sure tag strings are not part of element type
457 typ = Typeof(struct {
459 }{}).(*StructType).Field(0).Type
460 testType(t, 14, typ, "[]uint32")
463 func TestInterfaceGet(t *testing.T) {
468 v1 := NewValue(&inter)
469 v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0)
470 assert(t, v2.Type().String(), "interface { }")
471 i2 := v2.(*InterfaceValue).Interface()
473 assert(t, v3.Type().String(), "float")
476 func TestInterfaceValue(t *testing.T) {
481 v1 := NewValue(&inter)
482 v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0)
483 assert(t, v2.Type().String(), "interface { }")
484 v3 := v2.(*InterfaceValue).Elem()
485 assert(t, v3.Type().String(), "float")
488 if _, ok := i3.(float); !ok {
489 t.Error("v2.Interface() did not return float, got ", Typeof(i3))
493 func TestFunctionValue(t *testing.T) {
494 v := NewValue(func() {})
495 if v.Interface() != v.Interface() {
496 t.Fatalf("TestFunction != itself")
498 assert(t, v.Type().String(), "func()")
501 func TestCopyArray(t *testing.T) {
502 a := []int{1, 2, 3, 4, 10, 9, 8, 7}
503 b := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44}
504 c := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44}
507 for i := 0; i < len(b); i++ {
509 t.Fatalf("b != c before test")
512 aa := va.(*PtrValue).Elem().(*SliceValue)
513 ab := vb.(*PtrValue).Elem().(*SliceValue)
514 for tocopy := 1; tocopy <= 7; tocopy++ {
518 for i := 0; i < tocopy; i++ {
520 t.Errorf("(i) tocopy=%d a[%d]=%d, b[%d]=%d",
521 tocopy, i, a[i], i, b[i])
524 for i := tocopy; i < len(b); i++ {
527 t.Errorf("(ii) tocopy=%d a[%d]=%d, b[%d]=%d, c[%d]=%d",
528 tocopy, i, a[i], i, b[i], i, c[i])
530 t.Errorf("(iii) tocopy=%d b[%d]=%d, c[%d]=%d",
531 tocopy, i, b[i], i, c[i])
534 t.Logf("tocopy=%d elem %d is okay\n", tocopy, i)
540 func TestBigUnnamedStruct(t *testing.T) {
541 b := struct{ a, b, c, d int64 }{1, 2, 3, 4}
543 b1 := v.Interface().(struct {
546 if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d {
547 t.Errorf("NewValue(%v).Interface().(*Big) = %v", b, b1)
555 func TestBigStruct(t *testing.T) {
556 b := big{1, 2, 3, 4, 5}
558 b1 := v.Interface().(big)
559 if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d || b1.e != b.e {
560 t.Errorf("NewValue(%v).Interface().(big) = %v", b, b1)
571 type DeepEqualTest struct {
576 var deepEqualTests = []DeepEqualTest{
579 {int32(1), int32(1), true},
581 {float32(0.5), float32(0.5), true},
582 {"hello", "hello", true},
583 {make([]int, 10), make([]int, 10), true},
584 {&[3]int{1, 2, 3}, &[3]int{1, 2, 3}, true},
585 {Basic{1, 0.5}, Basic{1, 0.5}, true},
586 {os.Error(nil), os.Error(nil), true},
587 {map[int]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}, true},
591 {int32(1), int32(2), false},
593 {float32(0.5), float32(0.6), false},
594 {"hello", "hey", false},
595 {make([]int, 10), make([]int, 11), false},
596 {&[3]int{1, 2, 3}, &[3]int{1, 2, 4}, false},
597 {Basic{1, 0.5}, Basic{1, 0.6}, false},
598 {Basic{1, 0}, Basic{2, 0}, false},
599 {map[int]string{1: "one", 3: "two"}, map[int]string{2: "two", 1: "one"}, false},
600 {map[int]string{1: "one", 2: "txo"}, map[int]string{2: "two", 1: "one"}, false},
601 {map[int]string{1: "one"}, map[int]string{2: "two", 1: "one"}, false},
602 {map[int]string{2: "two", 1: "one"}, map[int]string{1: "one"}, false},
608 {int32(1), int64(1), false},
609 {0.5, "hello", false},
610 {[]int{1, 2, 3}, [3]int{1, 2, 3}, false},
611 {&[3]interface{}{1, 2, 4}, &[3]interface{}{1, 2, "s"}, false},
612 {Basic{1, 0.5}, NotBasic{1, 0.5}, false},
613 {map[uint]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}, false},
616 func TestDeepEqual(t *testing.T) {
617 for _, test := range deepEqualTests {
618 if r := DeepEqual(test.a, test.b); r != test.eq {
619 t.Errorf("DeepEqual(%v, %v) = %v, want %v", test.a, test.b, r, test.eq)
624 func TestTypeof(t *testing.T) {
625 for _, test := range deepEqualTests {
626 v := NewValue(test.a)
630 typ := Typeof(test.a)
632 t.Errorf("Typeof(%v) = %v, but NewValue(%v).Type() = %v", test.a, typ, test.a, v.Type())
637 type Recursive struct {
642 func TestDeepEqualRecursiveStruct(t *testing.T) {
643 a, b := new(Recursive), new(Recursive)
644 *a = Recursive{12, a}
645 *b = Recursive{12, b}
646 if !DeepEqual(a, b) {
647 t.Error("DeepEqual(recursive same) = false, want true")
651 type _Complex struct {
658 func TestDeepEqualComplexStruct(t *testing.T) {
659 m := make(map[float]float)
660 stra, strb := "hello", "hello"
661 a, b := new(_Complex), new(_Complex)
662 *a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m}
663 *b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m}
664 if !DeepEqual(a, b) {
665 t.Error("DeepEqual(complex same) = false, want true")
669 func TestDeepEqualComplexStructInequality(t *testing.T) {
670 m := make(map[float]float)
671 stra, strb := "hello", "helloo" // Difference is here
672 a, b := new(_Complex), new(_Complex)
673 *a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m}
674 *b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m}
676 t.Error("DeepEqual(complex different) = true, want false")
681 func check2ndField(x interface{}, offs uintptr, t *testing.T) {
682 s := NewValue(x).(*StructValue)
683 f := s.Type().(*StructType).Field(1)
684 if f.Offset != offs {
685 t.Error("mismatched offsets in structure alignment:", f.Offset, offs)
689 // Check that structure alignment & offsets viewed through reflect agree with those
690 // from the compiler itself.
691 func TestAlignment(t *testing.T) {
692 type T1inner struct {
699 type T2inner struct {
707 x := T1{T1inner{2}, 17}
708 check2ndField(x, uintptr(unsafe.Pointer(&x.f))-uintptr(unsafe.Pointer(&x)), t)
710 x1 := T2{T2inner{2, 3}, 17}
711 check2ndField(x1, uintptr(unsafe.Pointer(&x1.f))-uintptr(unsafe.Pointer(&x1)), t)
714 type IsNiller interface {
718 func Nil(a interface{}, t *testing.T) {
719 n := NewValue(a).(*StructValue).Field(0).(IsNiller)
721 t.Errorf("%v should be nil", a)
725 func NotNil(a interface{}, t *testing.T) {
726 n := NewValue(a).(*StructValue).Field(0).(IsNiller)
728 t.Errorf("value of type %v should not be nil", NewValue(a).Type().String())
732 func TestIsNil(t *testing.T) {
733 // These do not implement IsNil
734 doNotNil := []interface{}{int(0), float32(0), struct{ a int }{}}
735 for _, ts := range doNotNil {
738 if _, ok := v.(IsNiller); ok {
739 t.Errorf("%s is nilable; should not be", ts)
743 // These do implement IsNil.
744 // Wrap in extra struct to hide interface type.
745 doNil := []interface{}{
747 struct{ x interface{} }{},
748 struct{ x map[string]int }{},
749 struct{ x func() bool }{},
750 struct{ x chan int }{},
751 struct{ x []string }{},
753 for _, ts := range doNil {
754 ty := Typeof(ts).(*StructType).Field(0).Type
756 if _, ok := v.(IsNiller); !ok {
757 t.Errorf("%s %T is not nilable; should be", ts, v)
761 // Check the implementations
773 si.x = make([]int, 10)
780 ci.x = make(chan int)
787 mi.x = make(map[int]int)
805 func TestInterfaceExtraction(t *testing.T) {
811 v := Indirect(NewValue(&s)).(*StructValue).Field(0).Interface()
812 if v != s.w.(interface{}) {
813 t.Error("Interface() on interface: ", v, s.w)
817 func TestInterfaceEditing(t *testing.T) {
818 // strings are bigger than one word,
819 // so the interface conversion allocates
820 // memory to hold a string and puts that
821 // pointer in the interface.
822 var i interface{} = "hello"
824 // if i pass the interface value by value
825 // to NewValue, i should get a fresh copy
829 // and setting that copy to "bye" should
830 // not change the value stored in i.
831 v.(*StringValue).Set("bye")
832 if i.(string) != "hello" {
833 t.Errorf(`Set("bye") changed i to %s`, i.(string))
836 // the same should be true of smaller items.
839 v.(*IntValue).Set(234)
841 t.Errorf("Set(234) changed i to %d", i.(int))
845 func TestNilPtrValueSub(t *testing.T) {
847 if pv := NewValue(pi).(*PtrValue); pv.Elem() != nil {
848 t.Error("NewValue((*int)(nil)).(*PtrValue).Elem() != nil")
852 func TestMap(t *testing.T) {
853 m := map[string]int{"a": 1, "b": 2}
854 mv := NewValue(m).(*MapValue)
855 if n := mv.Len(); n != len(m) {
856 t.Errorf("Len = %d, want %d", n, len(m))
860 newmap := MakeMap(mv.Type().(*MapType))
861 for k, v := range m {
862 // Check that returned Keys match keys in range.
863 // These aren't required to be in the same order,
864 // but they are in this implementation, which makes
867 t.Errorf("Missing key #%d %q", i, k)
868 } else if kv := keys[i].(*StringValue); kv.Get() != k {
869 t.Errorf("Keys[%d] = %q, want %q", i, kv.Get(), k)
873 // Check that value lookup is correct.
874 vv := mv.Elem(NewValue(k))
875 if vi := vv.(*IntValue).Get(); vi != int64(v) {
876 t.Errorf("Key %q: have value %d, want %d", vi, v)
879 // Copy into new map.
880 newmap.SetElem(NewValue(k), NewValue(v))
882 vv := mv.Elem(NewValue("not-present"))
884 t.Errorf("Invalid key: got non-nil value %s", valueToString(vv))
887 newm := newmap.Interface().(map[string]int)
888 if len(newm) != len(m) {
889 t.Errorf("length after copy: newm=%d, m=%d", newm, m)
892 for k, v := range newm {
895 t.Errorf("newm[%q] = %d, but m[%q] = %d, %v", k, v, k, mv, ok)
899 newmap.SetElem(NewValue("a"), nil)
902 t.Errorf("newm[\"a\"] = %d after delete", v)
905 mv = NewValue(&m).(*PtrValue).Elem().(*MapValue)
908 t.Errorf("mv.Set(nil) failed")
912 func TestChan(t *testing.T) {
913 for loop := 0; loop < 2; loop++ {
917 // check both ways to allocate channels
920 c = make(chan int, 1)
921 cv = NewValue(c).(*ChanValue)
923 cv = MakeChan(Typeof(c).(*ChanType), 1)
924 c = cv.Interface().(chan int)
929 if i := <-c; i != 2 {
930 t.Errorf("reflect Send 2, native recv %d", i)
935 if i := cv.Recv().(*IntValue).Get(); i != 3 {
936 t.Errorf("native send 3, reflect Recv %d", i)
942 t.Errorf("TryRecv on empty chan: %s", valueToString(val))
949 t.Errorf("TryRecv on ready chan got nil")
950 } else if i := val.(*IntValue).Get(); i != 4 {
951 t.Errorf("native send 4, TryRecv %d", i)
956 ok := cv.TrySend(NewValue(5))
959 t.Errorf("TrySend on full chan succeeded: value %d", i)
963 ok = cv.TrySend(NewValue(6))
965 t.Errorf("TrySend on empty chan failed")
968 t.Errorf("TrySend 6, recv %d", i)
976 t.Errorf("closed too soon - 1")
978 if i := cv.Recv().(*IntValue).Get(); i != 123 {
979 t.Errorf("send 123 then close; Recv %d", i)
982 t.Errorf("closed too soon - 2")
984 if i := cv.Recv().(*IntValue).Get(); i != 0 {
985 t.Errorf("after close Recv %d", i)
988 t.Errorf("not closed")
992 // check creation of unbuffered channel
994 cv := MakeChan(Typeof(c).(*ChanType), 0)
995 c = cv.Interface().(chan int)
996 if cv.TrySend(NewValue(7)) {
997 t.Errorf("TrySend on sync chan succeeded")
999 if cv.TryRecv() != nil {
1000 t.Errorf("TryRecv on sync chan succeeded")
1004 cv = MakeChan(Typeof(c).(*ChanType), 10)
1005 c = cv.Interface().(chan int)
1006 for i := 0; i < 3; i++ {
1009 if l, m := cv.Len(), cv.Cap(); l != len(c) || m != cap(c) {
1010 t.Errorf("Len/Cap = %d/%d want %d/%d", l, m, len(c), cap(c))
1015 // Difficult test for function call because of
1016 // implicit padding between arguments.
1017 func dummy(b byte, c int, d byte) (i byte, j int, k byte) {
1021 func TestFunc(t *testing.T) {
1022 ret := NewValue(dummy).(*FuncValue).Call([]Value{NewValue(byte(10)), NewValue(20), NewValue(byte(30))})
1024 t.Fatalf("Call returned %d values, want 3", len(ret))
1027 i := ret[0].(*UintValue).Get()
1028 j := ret[1].(*IntValue).Get()
1029 k := ret[2].(*UintValue).Get()
1030 if i != 10 || j != 20 || k != 30 {
1031 t.Errorf("Call returned %d, %d, %d; want 10, 20, 30", i, j, k)
1039 func (p Point) Dist(scale int) int { return p.x*p.x*scale + p.y*p.y*scale }
1041 func TestMethod(t *testing.T) {
1042 // Non-curried method of type.
1044 i := Typeof(p).Method(0).Func.Call([]Value{NewValue(p), NewValue(10)})[0].(*IntValue).Get()
1046 t.Errorf("Type Method returned %d; want 250", i)
1049 i = Typeof(&p).Method(0).Func.Call([]Value{NewValue(&p), NewValue(10)})[0].(*IntValue).Get()
1051 t.Errorf("Pointer Type Method returned %d; want 250", i)
1054 // Curried method of value.
1055 i = NewValue(p).Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
1057 t.Errorf("Value Method returned %d; want 250", i)
1060 // Curried method of interface value.
1061 // Have to wrap interface value in a struct to get at it.
1062 // Passing it to NewValue directly would
1063 // access the underlying Point, not the interface.
1069 pv := NewValue(s).(*StructValue).Field(0)
1070 i = pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
1072 t.Errorf("Interface Method returned %d; want 250", i)
1076 func TestInterfaceSet(t *testing.T) {
1085 sv := NewValue(&s).(*PtrValue).Elem().(*StructValue)
1086 sv.Field(0).(*InterfaceValue).Set(NewValue(p))
1087 if q := s.I.(*Point); q != p {
1088 t.Errorf("i: have %p want %p", q, p)
1091 pv := sv.Field(1).(*InterfaceValue)
1093 if q := s.P.(*Point); q != p {
1094 t.Errorf("i: have %p want %p", q, p)
1097 i := pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
1099 t.Errorf("Interface Method returned %d; want 250", i)
1108 func TestAnonymousFields(t *testing.T) {
1109 var field StructField
1112 type1 := Typeof(t1).(*StructType)
1113 if field, ok = type1.FieldByName("int"); !ok {
1114 t.Error("no field 'int'")
1116 if field.Index[0] != 1 {
1117 t.Error("field index should be 1; is", field.Index)
1171 var fieldTests = []FTest{
1172 {struct{}{}, "", nil, 0},
1173 {struct{}{}, "foo", nil, 0},
1174 {S0{a: 'a'}, "a", []int{0}, 'a'},
1175 {S0{}, "d", nil, 0},
1176 {S1{S0: S0{a: 'a'}}, "a", []int{1, 0}, 'a'},
1177 {S1{b: 'b'}, "b", []int{0}, 'b'},
1178 {S1{}, "S0", []int{1}, 0},
1179 {S1{S0: S0{c: 'c'}}, "c", []int{1, 2}, 'c'},
1180 {S2{a: 'a'}, "a", []int{0}, 'a'},
1181 {S2{}, "S1", []int{1}, 0},
1182 {S2{S1: &S1{b: 'b'}}, "b", []int{1, 0}, 'b'},
1183 {S2{S1: &S1{S0: S0{c: 'c'}}}, "c", []int{1, 1, 2}, 'c'},
1184 {S2{}, "d", nil, 0},
1185 {S3{}, "S1", nil, 0},
1186 {S3{S2: S2{a: 'a'}}, "a", []int{1, 0}, 'a'},
1187 {S3{}, "b", nil, 0},
1188 {S3{d: 'd'}, "d", []int{2}, 0},
1189 {S3{e: 'e'}, "e", []int{3}, 'e'},
1190 {S4{a: 'a'}, "a", []int{1}, 'a'},
1191 {S4{}, "b", nil, 0},
1194 func TestFieldByIndex(t *testing.T) {
1195 for _, test := range fieldTests {
1196 s := Typeof(test.s).(*StructType)
1197 f := s.FieldByIndex(test.index)
1199 if test.index != nil {
1200 if f.Name != test.name {
1201 t.Errorf("%s.%s found; want %s", s.Name(), f.Name, test.name)
1204 t.Errorf("%s.%s found", s.Name(), f.Name)
1206 } else if len(test.index) > 0 {
1207 t.Errorf("%s.%s not found", s.Name(), test.name)
1210 if test.value != 0 {
1211 v := NewValue(test.s).(*StructValue).FieldByIndex(test.index)
1213 if x, ok := v.Interface().(int); ok {
1214 if x != test.value {
1215 t.Errorf("%s%v is %d; want %d", s.Name(), test.index, x, test.value)
1218 t.Errorf("%s%v value not an int", s.Name(), test.index)
1221 t.Errorf("%s%v value not found", s.Name(), test.index)
1227 func TestFieldByName(t *testing.T) {
1228 for _, test := range fieldTests {
1229 s := Typeof(test.s).(*StructType)
1230 f, found := s.FieldByName(test.name)
1232 if test.index != nil {
1233 // Verify field depth and index.
1234 if len(f.Index) != len(test.index) {
1235 t.Errorf("%s.%s depth %d; want %d", s.Name(), test.name, len(f.Index), len(test.index))
1237 for i, x := range f.Index {
1238 if x != test.index[i] {
1239 t.Errorf("%s.%s.Index[%d] is %d; want %d", s.Name(), test.name, i, x, test.index[i])
1244 t.Errorf("%s.%s found", s.Name(), f.Name)
1246 } else if len(test.index) > 0 {
1247 t.Errorf("%s.%s not found", s.Name(), test.name)
1250 if test.value != 0 {
1251 v := NewValue(test.s).(*StructValue).FieldByName(test.name)
1253 if x, ok := v.Interface().(int); ok {
1254 if x != test.value {
1255 t.Errorf("%s.%s is %d; want %d", s.Name(), test.name, x, test.value)
1258 t.Errorf("%s.%s value not an int", s.Name(), test.name)
1261 t.Errorf("%s.%s value not found", s.Name(), test.name)
1267 func TestImportPath(t *testing.T) {
1268 if path := Typeof(vector.Vector{}).PkgPath(); path != "libgo_container.vector" {
1269 t.Errorf("Typeof(vector.Vector{}).PkgPath() = %q, want \"libgo_container.vector\"", path)
1273 func TestDotDotDot(t *testing.T) {
1274 // Test example from FuncType.DotDotDot documentation.
1275 var f func(x int, y ...float)
1276 typ := Typeof(f).(*FuncType)
1277 if typ.NumIn() == 2 && typ.In(0) == Typeof(int(0)) {
1278 sl, ok := typ.In(1).(*SliceType)
1280 if sl.Elem() == Typeof(float(0)) {
1288 t.Errorf("want NumIn() = 2, In(0) = int, In(1) = []float")
1289 s := fmt.Sprintf("have NumIn() = %d", typ.NumIn())
1290 for i := 0; i < typ.NumIn(); i++ {
1291 s += fmt.Sprintf(", In(%d) = %s", i, typ.In(i))
1305 func (*inner) m() {}
1306 func (*outer) m() {}
1308 func TestNestedMethods(t *testing.T) {
1309 typ := Typeof((*outer)(nil))
1310 if typ.NumMethod() != 1 || typ.Method(0).Func.Get() != NewValue((*outer).m).(*FuncValue).Get() {
1311 t.Errorf("Wrong method table for outer: (m=%p)", (*outer).m)
1312 for i := 0; i < typ.NumMethod(); i++ {
1314 t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Get())
1319 type innerInt struct {
1323 type outerInt struct {
1328 func (i *innerInt) m() int {
1332 func TestEmbeddedMethods(t *testing.T) {
1333 typ := Typeof((*outerInt)(nil))
1334 if typ.NumMethod() != 1 || typ.Method(0).Func.Get() != NewValue((*outerInt).m).(*FuncValue).Get() {
1335 t.Errorf("Wrong method table for outerInt: (m=%p)", (*outerInt).m)
1336 for i := 0; i < typ.NumMethod(); i++ {
1338 t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Get())
1343 if v := NewValue(i).Method(0).Call(nil)[0].(*IntValue).Get(); v != 3 {
1344 t.Errorf("i.m() = %d, want 3", v)
1347 o := &outerInt{1, innerInt{2}}
1348 if v := NewValue(o).Method(0).Call(nil)[0].(*IntValue).Get(); v != 2 {
1349 t.Errorf("i.m() = %d, want 2", v)
1353 if v := f(o); v != 2 {
1354 t.Errorf("f(o) = %d, want 2", v)