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 float32 }{}, "float32"},
52 {struct{ x float64 }{}, "float64"},
53 {struct{ x int8 }{}, "int8"},
54 {struct{ x (**int8) }{}, "**int8"},
55 {struct{ x (**integer) }{}, "**reflect_test.integer"},
56 {struct{ x ([32]int32) }{}, "[32]int32"},
57 {struct{ x ([]int8) }{}, "[]int8"},
58 {struct{ x (map[string]int32) }{}, "map[string] int32"},
59 {struct{ x (chan<- string) }{}, "chan<- string"},
66 "struct { c chan *int32; d float32 }",
68 {struct{ x (func(a int8, b int32)) }{}, "func(int8, int32)"},
71 c func(chan *integer, *int8)
74 "struct { c func(chan *reflect_test.integer, *int8) }",
82 "struct { a int8; b int32 }",
91 "struct { a int8; b int8; c int32 }",
101 "struct { a int8; b int8; c int8; d int32 }",
112 "struct { a int8; b int8; c int8; d int8; e int32 }",
124 "struct { a int8; b int8; c int8; d int8; e int8; f int32 }",
131 `struct { a int8 "hi there" }`,
135 a int8 "hi \x00there\t\n\"\\"
138 `struct { a int8 "hi \x00there\t\n\"\\" }`,
145 "struct { f func(...int) }",
149 a(func(func(int) int) func(func(int)) int)
153 "interface { a(func(func(int) int) func(func(int)) int); b() }",
157 var valueTests = []pair{
166 {(float32)(0), "256.25"},
167 {(float64)(0), "512.125"},
168 {(string)(""), "stringy cheese"},
169 {(bool)(false), "true"},
170 {(*int8)(nil), "*int8(0)"},
171 {(**int8)(nil), "**int8(0)"},
172 {[5]int32{}, "[5]int32{0, 0, 0, 0, 0}"},
173 {(**integer)(nil), "**reflect_test.integer(0)"},
174 {(map[string]int32)(nil), "map[string] int32{<can't iterate on maps>}"},
175 {(chan<- string)(nil), "chan<- string"},
180 "struct { c chan *int32; d float32 }{chan *int32, 0}",
182 {(func(a int8, b int32))(nil), "func(int8, int32)(0)"},
183 {struct{ c func(chan *integer, *int8) }{},
184 "struct { c func(chan *reflect_test.integer, *int8) }{func(chan *reflect_test.integer, *int8)(0)}",
190 "struct { a int8; b int32 }{0, 0}",
197 "struct { a int8; b int8; c int32 }{0, 0, 0}",
201 func testType(t *testing.T, i int, typ Type, want string) {
204 t.Errorf("#%d: have %#q, want %#q", i, s, want)
208 func TestTypes(t *testing.T) {
209 for i, tt := range typeTests {
210 testType(t, i, NewValue(tt.i).(*StructValue).Field(0).Type(), tt.s)
214 func TestSet(t *testing.T) {
215 for i, tt := range valueTests {
217 switch v := v.(type) {
219 switch v.Type().Kind() {
232 switch v.Type().Kind() {
245 switch v.Type().Kind() {
252 switch v.Type().Kind() {
259 v.Set("stringy cheese")
263 s := valueToString(v)
265 t.Errorf("#%d: have %#q, want %#q", i, s, tt.s)
270 func TestSetValue(t *testing.T) {
271 for i, tt := range valueTests {
273 switch v := v.(type) {
275 switch v.Type().Kind() {
277 v.SetValue(NewValue(int(132)))
279 v.SetValue(NewValue(int8(8)))
281 v.SetValue(NewValue(int16(16)))
283 v.SetValue(NewValue(int32(32)))
285 v.SetValue(NewValue(int64(64)))
288 switch v.Type().Kind() {
290 v.SetValue(NewValue(uint(132)))
292 v.SetValue(NewValue(uint8(8)))
294 v.SetValue(NewValue(uint16(16)))
296 v.SetValue(NewValue(uint32(32)))
298 v.SetValue(NewValue(uint64(64)))
301 switch v.Type().Kind() {
303 v.SetValue(NewValue(float32(256.25)))
305 v.SetValue(NewValue(512.125))
308 switch v.Type().Kind() {
310 v.SetValue(NewValue(complex64(532.125 + 10i)))
312 v.SetValue(NewValue(complex128(564.25 + 1i)))
316 v.SetValue(NewValue("stringy cheese"))
318 v.SetValue(NewValue(true))
320 s := valueToString(v)
322 t.Errorf("#%d: have %#q, want %#q", i, s, tt.s)
329 var valueToStringTests = []pair{
334 {T{123, 456.75, "hello", &_i}, "reflect_test.T{123, 456.75, hello, *int(&7)}"},
335 {new(chan *T), "*chan *reflect_test.T(&chan *reflect_test.T)"},
336 {[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"},
337 {&[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})"},
338 {[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"},
339 {&[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "*[]int(&[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})"},
342 func TestValueToString(t *testing.T) {
343 for i, test := range valueToStringTests {
344 s := valueToString(NewValue(test.i))
346 t.Errorf("#%d: have %#q, want %#q", i, s, test.s)
351 func TestArrayElemSet(t *testing.T) {
352 v := NewValue([10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
353 v.(*ArrayValue).Elem(4).(*IntValue).Set(123)
354 s := valueToString(v)
355 const want = "[10]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"
357 t.Errorf("[10]int: have %#q want %#q", s, want)
360 v = NewValue([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
361 v.(*SliceValue).Elem(4).(*IntValue).Set(123)
363 const want1 = "[]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"
365 t.Errorf("[]int: have %#q want %#q", s, want1)
369 func TestPtrPointTo(t *testing.T) {
374 vip.(*PtrValue).Elem().(*PtrValue).PointTo(vi)
376 t.Errorf("got %d, want 1234", *ip)
380 vp := NewValue(ip).(*PtrValue)
381 vp.PointTo(vp.Elem())
383 t.Errorf("got non-nil (%p), want nil", ip)
387 func TestPtrSetNil(t *testing.T) {
391 vip.(*PtrValue).Elem().(*PtrValue).Set(nil)
393 t.Errorf("got non-nil (%d), want nil", *ip)
397 func TestMapSetNil(t *testing.T) {
398 m := make(map[string]int)
400 vm.(*PtrValue).Elem().(*MapValue).Set(nil)
402 t.Errorf("got non-nil (%p), want nil", m)
407 func TestAll(t *testing.T) {
408 testType(t, 1, Typeof((int8)(0)), "int8")
409 testType(t, 2, Typeof((*int8)(nil)).(*PtrType).Elem(), "int8")
411 typ := Typeof((*struct {
415 testType(t, 3, typ, "*struct { c chan *int32; d float32 }")
416 etyp := typ.(*PtrType).Elem()
417 testType(t, 4, etyp, "struct { c chan *int32; d float32 }")
418 styp := etyp.(*StructType)
420 testType(t, 5, f.Type, "chan *int32")
422 f, present := styp.FieldByName("d")
424 t.Errorf("FieldByName says present field is absent")
426 testType(t, 6, f.Type, "float32")
428 f, present = styp.FieldByName("absent")
430 t.Errorf("FieldByName says absent field is present")
433 typ = Typeof([32]int32{})
434 testType(t, 7, typ, "[32]int32")
435 testType(t, 8, typ.(*ArrayType).Elem(), "int32")
437 typ = Typeof((map[string]*int32)(nil))
438 testType(t, 9, typ, "map[string] *int32")
439 mtyp := typ.(*MapType)
440 testType(t, 10, mtyp.Key(), "string")
441 testType(t, 11, mtyp.Elem(), "*int32")
443 typ = Typeof((chan<- string)(nil))
444 testType(t, 12, typ, "chan<- string")
445 testType(t, 13, typ.(*ChanType).Elem(), "string")
447 // make sure tag strings are not part of element type
448 typ = Typeof(struct {
450 }{}).(*StructType).Field(0).Type
451 testType(t, 14, typ, "[]uint32")
454 func TestInterfaceGet(t *testing.T) {
459 v1 := NewValue(&inter)
460 v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0)
461 assert(t, v2.Type().String(), "interface { }")
462 i2 := v2.(*InterfaceValue).Interface()
464 assert(t, v3.Type().String(), "float64")
467 func TestInterfaceValue(t *testing.T) {
472 v1 := NewValue(&inter)
473 v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0)
474 assert(t, v2.Type().String(), "interface { }")
475 v3 := v2.(*InterfaceValue).Elem()
476 assert(t, v3.Type().String(), "float64")
479 if _, ok := i3.(float64); !ok {
480 t.Error("v2.Interface() did not return float64, got ", Typeof(i3))
484 func TestFunctionValue(t *testing.T) {
485 v := NewValue(func() {})
486 if v.Interface() != v.Interface() {
487 t.Fatalf("TestFunction != itself")
489 assert(t, v.Type().String(), "func()")
492 var appendTests = []struct {
495 {make([]int, 2, 4), []int{22}},
496 {make([]int, 2, 4), []int{22, 33, 44}},
499 func TestAppend(t *testing.T) {
500 for i, test := range appendTests {
501 origLen, extraLen := len(test.orig), len(test.extra)
502 want := append(test.orig, test.extra...)
503 // Convert extra from []int to []Value.
504 e0 := make([]Value, len(test.extra))
505 for j, e := range test.extra {
508 // Convert extra from []int to *SliceValue.
509 e1 := NewValue(test.extra).(*SliceValue)
511 a0 := NewValue(test.orig).(*SliceValue)
512 have0 := Append(a0, e0...).Interface().([]int)
513 if !DeepEqual(have0, want) {
514 t.Errorf("Append #%d: have %v, want %v", i, have0, want)
516 // Check that the orig and extra slices were not modified.
517 if len(test.orig) != origLen {
518 t.Errorf("Append #%d origLen: have %v, want %v", i, len(test.orig), origLen)
520 if len(test.extra) != extraLen {
521 t.Errorf("Append #%d extraLen: have %v, want %v", i, len(test.extra), extraLen)
524 a1 := NewValue(test.orig).(*SliceValue)
525 have1 := AppendSlice(a1, e1).Interface().([]int)
526 if !DeepEqual(have1, want) {
527 t.Errorf("AppendSlice #%d: have %v, want %v", i, have1, want)
529 // Check that the orig and extra slices were not modified.
530 if len(test.orig) != origLen {
531 t.Errorf("AppendSlice #%d origLen: have %v, want %v", i, len(test.orig), origLen)
533 if len(test.extra) != extraLen {
534 t.Errorf("AppendSlice #%d extraLen: have %v, want %v", i, len(test.extra), extraLen)
539 func TestCopy(t *testing.T) {
540 a := []int{1, 2, 3, 4, 10, 9, 8, 7}
541 b := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44}
542 c := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44}
543 for i := 0; i < len(b); i++ {
545 t.Fatalf("b != c before test")
548 aa := NewValue(a).(*SliceValue)
549 ab := NewValue(b).(*SliceValue)
550 for tocopy := 1; tocopy <= 7; tocopy++ {
554 for i := 0; i < tocopy; i++ {
556 t.Errorf("(i) tocopy=%d a[%d]=%d, b[%d]=%d",
557 tocopy, i, a[i], i, b[i])
560 for i := tocopy; i < len(b); i++ {
563 t.Errorf("(ii) tocopy=%d a[%d]=%d, b[%d]=%d, c[%d]=%d",
564 tocopy, i, a[i], i, b[i], i, c[i])
566 t.Errorf("(iii) tocopy=%d b[%d]=%d, c[%d]=%d",
567 tocopy, i, b[i], i, c[i])
570 t.Logf("tocopy=%d elem %d is okay\n", tocopy, i)
576 func TestBigUnnamedStruct(t *testing.T) {
577 b := struct{ a, b, c, d int64 }{1, 2, 3, 4}
579 b1 := v.Interface().(struct {
582 if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d {
583 t.Errorf("NewValue(%v).Interface().(*Big) = %v", b, b1)
591 func TestBigStruct(t *testing.T) {
592 b := big{1, 2, 3, 4, 5}
594 b1 := v.Interface().(big)
595 if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d || b1.e != b.e {
596 t.Errorf("NewValue(%v).Interface().(big) = %v", b, b1)
607 type DeepEqualTest struct {
612 var deepEqualTests = []DeepEqualTest{
615 {int32(1), int32(1), true},
617 {float32(0.5), float32(0.5), true},
618 {"hello", "hello", true},
619 {make([]int, 10), make([]int, 10), true},
620 {&[3]int{1, 2, 3}, &[3]int{1, 2, 3}, true},
621 {Basic{1, 0.5}, Basic{1, 0.5}, true},
622 {os.Error(nil), os.Error(nil), true},
623 {map[int]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}, true},
627 {int32(1), int32(2), false},
629 {float32(0.5), float32(0.6), false},
630 {"hello", "hey", false},
631 {make([]int, 10), make([]int, 11), false},
632 {&[3]int{1, 2, 3}, &[3]int{1, 2, 4}, false},
633 {Basic{1, 0.5}, Basic{1, 0.6}, false},
634 {Basic{1, 0}, Basic{2, 0}, false},
635 {map[int]string{1: "one", 3: "two"}, map[int]string{2: "two", 1: "one"}, false},
636 {map[int]string{1: "one", 2: "txo"}, map[int]string{2: "two", 1: "one"}, false},
637 {map[int]string{1: "one"}, map[int]string{2: "two", 1: "one"}, false},
638 {map[int]string{2: "two", 1: "one"}, map[int]string{1: "one"}, false},
644 {int32(1), int64(1), false},
645 {0.5, "hello", false},
646 {[]int{1, 2, 3}, [3]int{1, 2, 3}, false},
647 {&[3]interface{}{1, 2, 4}, &[3]interface{}{1, 2, "s"}, false},
648 {Basic{1, 0.5}, NotBasic{1, 0.5}, false},
649 {map[uint]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}, false},
652 func TestDeepEqual(t *testing.T) {
653 for _, test := range deepEqualTests {
654 if r := DeepEqual(test.a, test.b); r != test.eq {
655 t.Errorf("DeepEqual(%v, %v) = %v, want %v", test.a, test.b, r, test.eq)
660 func TestTypeof(t *testing.T) {
661 for _, test := range deepEqualTests {
662 v := NewValue(test.a)
666 typ := Typeof(test.a)
668 t.Errorf("Typeof(%v) = %v, but NewValue(%v).Type() = %v", test.a, typ, test.a, v.Type())
673 type Recursive struct {
678 func TestDeepEqualRecursiveStruct(t *testing.T) {
679 a, b := new(Recursive), new(Recursive)
680 *a = Recursive{12, a}
681 *b = Recursive{12, b}
682 if !DeepEqual(a, b) {
683 t.Error("DeepEqual(recursive same) = false, want true")
687 type _Complex struct {
691 d map[float64]float64
694 func TestDeepEqualComplexStruct(t *testing.T) {
695 m := make(map[float64]float64)
696 stra, strb := "hello", "hello"
697 a, b := new(_Complex), new(_Complex)
698 *a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m}
699 *b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m}
700 if !DeepEqual(a, b) {
701 t.Error("DeepEqual(complex same) = false, want true")
705 func TestDeepEqualComplexStructInequality(t *testing.T) {
706 m := make(map[float64]float64)
707 stra, strb := "hello", "helloo" // Difference is here
708 a, b := new(_Complex), new(_Complex)
709 *a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m}
710 *b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m}
712 t.Error("DeepEqual(complex different) = true, want false")
717 func check2ndField(x interface{}, offs uintptr, t *testing.T) {
718 s := NewValue(x).(*StructValue)
719 f := s.Type().(*StructType).Field(1)
720 if f.Offset != offs {
721 t.Error("mismatched offsets in structure alignment:", f.Offset, offs)
725 // Check that structure alignment & offsets viewed through reflect agree with those
726 // from the compiler itself.
727 func TestAlignment(t *testing.T) {
728 type T1inner struct {
735 type T2inner struct {
743 x := T1{T1inner{2}, 17}
744 check2ndField(x, uintptr(unsafe.Pointer(&x.f))-uintptr(unsafe.Pointer(&x)), t)
746 x1 := T2{T2inner{2, 3}, 17}
747 check2ndField(x1, uintptr(unsafe.Pointer(&x1.f))-uintptr(unsafe.Pointer(&x1)), t)
750 type IsNiller interface {
754 func Nil(a interface{}, t *testing.T) {
755 n := NewValue(a).(*StructValue).Field(0).(IsNiller)
757 t.Errorf("%v should be nil", a)
761 func NotNil(a interface{}, t *testing.T) {
762 n := NewValue(a).(*StructValue).Field(0).(IsNiller)
764 t.Errorf("value of type %v should not be nil", NewValue(a).Type().String())
768 func TestIsNil(t *testing.T) {
769 // These do not implement IsNil
770 doNotNil := []interface{}{int(0), float32(0), struct{ a int }{}}
771 for _, ts := range doNotNil {
774 if _, ok := v.(IsNiller); ok {
775 t.Errorf("%s is nilable; should not be", ts)
779 // These do implement IsNil.
780 // Wrap in extra struct to hide interface type.
781 doNil := []interface{}{
783 struct{ x interface{} }{},
784 struct{ x map[string]int }{},
785 struct{ x func() bool }{},
786 struct{ x chan int }{},
787 struct{ x []string }{},
789 for _, ts := range doNil {
790 ty := Typeof(ts).(*StructType).Field(0).Type
792 if _, ok := v.(IsNiller); !ok {
793 t.Errorf("%s %T is not nilable; should be", ts, v)
797 // Check the implementations
809 si.x = make([]int, 10)
816 ci.x = make(chan int)
823 mi.x = make(map[int]int)
841 func TestInterfaceExtraction(t *testing.T) {
847 v := Indirect(NewValue(&s)).(*StructValue).Field(0).Interface()
848 if v != s.w.(interface{}) {
849 t.Error("Interface() on interface: ", v, s.w)
853 func TestInterfaceEditing(t *testing.T) {
854 // strings are bigger than one word,
855 // so the interface conversion allocates
856 // memory to hold a string and puts that
857 // pointer in the interface.
858 var i interface{} = "hello"
860 // if i pass the interface value by value
861 // to NewValue, i should get a fresh copy
865 // and setting that copy to "bye" should
866 // not change the value stored in i.
867 v.(*StringValue).Set("bye")
868 if i.(string) != "hello" {
869 t.Errorf(`Set("bye") changed i to %s`, i.(string))
872 // the same should be true of smaller items.
875 v.(*IntValue).Set(234)
877 t.Errorf("Set(234) changed i to %d", i.(int))
881 func TestNilPtrValueSub(t *testing.T) {
883 if pv := NewValue(pi).(*PtrValue); pv.Elem() != nil {
884 t.Error("NewValue((*int)(nil)).(*PtrValue).Elem() != nil")
888 func TestMap(t *testing.T) {
889 m := map[string]int{"a": 1, "b": 2}
890 mv := NewValue(m).(*MapValue)
891 if n := mv.Len(); n != len(m) {
892 t.Errorf("Len = %d, want %d", n, len(m))
896 newmap := MakeMap(mv.Type().(*MapType))
897 for k, v := range m {
898 // Check that returned Keys match keys in range.
899 // These aren't required to be in the same order,
900 // but they are in this implementation, which makes
903 t.Errorf("Missing key #%d %q", i, k)
904 } else if kv := keys[i].(*StringValue); kv.Get() != k {
905 t.Errorf("Keys[%d] = %q, want %q", i, kv.Get(), k)
909 // Check that value lookup is correct.
910 vv := mv.Elem(NewValue(k))
911 if vi := vv.(*IntValue).Get(); vi != int64(v) {
912 t.Errorf("Key %q: have value %d, want %d", k, vi, v)
915 // Copy into new map.
916 newmap.SetElem(NewValue(k), NewValue(v))
918 vv := mv.Elem(NewValue("not-present"))
920 t.Errorf("Invalid key: got non-nil value %s", valueToString(vv))
923 newm := newmap.Interface().(map[string]int)
924 if len(newm) != len(m) {
925 t.Errorf("length after copy: newm=%d, m=%d", newm, m)
928 for k, v := range newm {
931 t.Errorf("newm[%q] = %d, but m[%q] = %d, %v", k, v, k, mv, ok)
935 newmap.SetElem(NewValue("a"), nil)
938 t.Errorf("newm[\"a\"] = %d after delete", v)
941 mv = NewValue(&m).(*PtrValue).Elem().(*MapValue)
944 t.Errorf("mv.Set(nil) failed")
948 func TestChan(t *testing.T) {
949 for loop := 0; loop < 2; loop++ {
953 // check both ways to allocate channels
956 c = make(chan int, 1)
957 cv = NewValue(c).(*ChanValue)
959 cv = MakeChan(Typeof(c).(*ChanType), 1)
960 c = cv.Interface().(chan int)
965 if i := <-c; i != 2 {
966 t.Errorf("reflect Send 2, native recv %d", i)
971 if i, ok := cv.Recv(); i.(*IntValue).Get() != 3 || !ok {
972 t.Errorf("native send 3, reflect Recv %d, %t", i.(*IntValue).Get(), ok)
976 val, ok := cv.TryRecv()
977 if val != nil || ok {
978 t.Errorf("TryRecv on empty chan: %s, %t", valueToString(val), ok)
983 val, ok = cv.TryRecv()
985 t.Errorf("TryRecv on ready chan got nil")
986 } else if i := val.(*IntValue).Get(); i != 4 || !ok {
987 t.Errorf("native send 4, TryRecv %d, %t", i, ok)
992 ok = cv.TrySend(NewValue(5))
995 t.Errorf("TrySend on full chan succeeded: value %d", i)
999 ok = cv.TrySend(NewValue(6))
1001 t.Errorf("TrySend on empty chan failed")
1003 if i = <-c; i != 6 {
1004 t.Errorf("TrySend 6, recv %d", i)
1011 if i, ok := cv.Recv(); i.(*IntValue).Get() != 123 || !ok {
1012 t.Errorf("send 123 then close; Recv %d, %t", i.(*IntValue).Get(), ok)
1014 if i, ok := cv.Recv(); i.(*IntValue).Get() != 0 || ok {
1015 t.Errorf("after close Recv %d, %t", i.(*IntValue).Get(), ok)
1019 // check creation of unbuffered channel
1021 cv := MakeChan(Typeof(c).(*ChanType), 0)
1022 c = cv.Interface().(chan int)
1023 if cv.TrySend(NewValue(7)) {
1024 t.Errorf("TrySend on sync chan succeeded")
1026 if v, ok := cv.TryRecv(); v != nil || ok {
1027 t.Errorf("TryRecv on sync chan succeeded")
1031 cv = MakeChan(Typeof(c).(*ChanType), 10)
1032 c = cv.Interface().(chan int)
1033 for i := 0; i < 3; i++ {
1036 if l, m := cv.Len(), cv.Cap(); l != len(c) || m != cap(c) {
1037 t.Errorf("Len/Cap = %d/%d want %d/%d", l, m, len(c), cap(c))
1042 // Difficult test for function call because of
1043 // implicit padding between arguments.
1044 func dummy(b byte, c int, d byte) (i byte, j int, k byte) {
1048 func TestFunc(t *testing.T) {
1049 ret := NewValue(dummy).(*FuncValue).Call([]Value{NewValue(byte(10)), NewValue(20), NewValue(byte(30))})
1051 t.Fatalf("Call returned %d values, want 3", len(ret))
1054 i := ret[0].(*UintValue).Get()
1055 j := ret[1].(*IntValue).Get()
1056 k := ret[2].(*UintValue).Get()
1057 if i != 10 || j != 20 || k != 30 {
1058 t.Errorf("Call returned %d, %d, %d; want 10, 20, 30", i, j, k)
1066 func (p Point) Dist(scale int) int { return p.x*p.x*scale + p.y*p.y*scale }
1068 func TestMethod(t *testing.T) {
1069 // Non-curried method of type.
1071 i := Typeof(p).Method(0).Func.Call([]Value{NewValue(p), NewValue(10)})[0].(*IntValue).Get()
1073 t.Errorf("Type Method returned %d; want 250", i)
1076 i = Typeof(&p).Method(0).Func.Call([]Value{NewValue(&p), NewValue(10)})[0].(*IntValue).Get()
1078 t.Errorf("Pointer Type Method returned %d; want 250", i)
1081 // Curried method of value.
1082 i = NewValue(p).Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
1084 t.Errorf("Value Method returned %d; want 250", i)
1087 // Curried method of pointer.
1088 i = NewValue(&p).Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
1090 t.Errorf("Value Method returned %d; want 250", i)
1093 // Curried method of pointer to value.
1094 i = NewValue(p).Addr().Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
1096 t.Errorf("Value Method returned %d; want 250", i)
1099 // Curried method of interface value.
1100 // Have to wrap interface value in a struct to get at it.
1101 // Passing it to NewValue directly would
1102 // access the underlying Point, not the interface.
1108 pv := NewValue(s).(*StructValue).Field(0)
1109 i = pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
1111 t.Errorf("Interface Method returned %d; want 250", i)
1115 func TestInterfaceSet(t *testing.T) {
1124 sv := NewValue(&s).(*PtrValue).Elem().(*StructValue)
1125 sv.Field(0).(*InterfaceValue).Set(NewValue(p))
1126 if q := s.I.(*Point); q != p {
1127 t.Errorf("i: have %p want %p", q, p)
1130 pv := sv.Field(1).(*InterfaceValue)
1132 if q := s.P.(*Point); q != p {
1133 t.Errorf("i: have %p want %p", q, p)
1136 i := pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
1138 t.Errorf("Interface Method returned %d; want 250", i)
1147 func TestAnonymousFields(t *testing.T) {
1148 var field StructField
1151 type1 := Typeof(t1).(*StructType)
1152 if field, ok = type1.FieldByName("int"); !ok {
1153 t.Error("no field 'int'")
1155 if field.Index[0] != 1 {
1156 t.Error("field index should be 1; is", field.Index)
1210 var fieldTests = []FTest{
1211 {struct{}{}, "", nil, 0},
1212 {struct{}{}, "foo", nil, 0},
1213 {S0{a: 'a'}, "a", []int{0}, 'a'},
1214 {S0{}, "d", nil, 0},
1215 {S1{S0: S0{a: 'a'}}, "a", []int{1, 0}, 'a'},
1216 {S1{b: 'b'}, "b", []int{0}, 'b'},
1217 {S1{}, "S0", []int{1}, 0},
1218 {S1{S0: S0{c: 'c'}}, "c", []int{1, 2}, 'c'},
1219 {S2{a: 'a'}, "a", []int{0}, 'a'},
1220 {S2{}, "S1", []int{1}, 0},
1221 {S2{S1: &S1{b: 'b'}}, "b", []int{1, 0}, 'b'},
1222 {S2{S1: &S1{S0: S0{c: 'c'}}}, "c", []int{1, 1, 2}, 'c'},
1223 {S2{}, "d", nil, 0},
1224 {S3{}, "S1", nil, 0},
1225 {S3{S2: S2{a: 'a'}}, "a", []int{1, 0}, 'a'},
1226 {S3{}, "b", nil, 0},
1227 {S3{d: 'd'}, "d", []int{2}, 0},
1228 {S3{e: 'e'}, "e", []int{3}, 'e'},
1229 {S4{a: 'a'}, "a", []int{1}, 'a'},
1230 {S4{}, "b", nil, 0},
1233 func TestFieldByIndex(t *testing.T) {
1234 for _, test := range fieldTests {
1235 s := Typeof(test.s).(*StructType)
1236 f := s.FieldByIndex(test.index)
1238 if test.index != nil {
1239 if f.Name != test.name {
1240 t.Errorf("%s.%s found; want %s", s.Name(), f.Name, test.name)
1243 t.Errorf("%s.%s found", s.Name(), f.Name)
1245 } else if len(test.index) > 0 {
1246 t.Errorf("%s.%s not found", s.Name(), test.name)
1249 if test.value != 0 {
1250 v := NewValue(test.s).(*StructValue).FieldByIndex(test.index)
1252 if x, ok := v.Interface().(int); ok {
1253 if x != test.value {
1254 t.Errorf("%s%v is %d; want %d", s.Name(), test.index, x, test.value)
1257 t.Errorf("%s%v value not an int", s.Name(), test.index)
1260 t.Errorf("%s%v value not found", s.Name(), test.index)
1266 func TestFieldByName(t *testing.T) {
1267 for _, test := range fieldTests {
1268 s := Typeof(test.s).(*StructType)
1269 f, found := s.FieldByName(test.name)
1271 if test.index != nil {
1272 // Verify field depth and index.
1273 if len(f.Index) != len(test.index) {
1274 t.Errorf("%s.%s depth %d; want %d", s.Name(), test.name, len(f.Index), len(test.index))
1276 for i, x := range f.Index {
1277 if x != test.index[i] {
1278 t.Errorf("%s.%s.Index[%d] is %d; want %d", s.Name(), test.name, i, x, test.index[i])
1283 t.Errorf("%s.%s found", s.Name(), f.Name)
1285 } else if len(test.index) > 0 {
1286 t.Errorf("%s.%s not found", s.Name(), test.name)
1289 if test.value != 0 {
1290 v := NewValue(test.s).(*StructValue).FieldByName(test.name)
1292 if x, ok := v.Interface().(int); ok {
1293 if x != test.value {
1294 t.Errorf("%s.%s is %d; want %d", s.Name(), test.name, x, test.value)
1297 t.Errorf("%s.%s value not an int", s.Name(), test.name)
1300 t.Errorf("%s.%s value not found", s.Name(), test.name)
1306 func TestImportPath(t *testing.T) {
1307 if path := Typeof(vector.Vector{}).PkgPath(); path != "libgo_container.vector" {
1308 t.Errorf("Typeof(vector.Vector{}).PkgPath() = %q, want \"libgo_container.vector\"", path)
1312 func TestDotDotDot(t *testing.T) {
1313 // Test example from FuncType.DotDotDot documentation.
1314 var f func(x int, y ...float64)
1315 typ := Typeof(f).(*FuncType)
1316 if typ.NumIn() == 2 && typ.In(0) == Typeof(int(0)) {
1317 sl, ok := typ.In(1).(*SliceType)
1319 if sl.Elem() == Typeof(0.0) {
1327 t.Errorf("want NumIn() = 2, In(0) = int, In(1) = []float64")
1328 s := fmt.Sprintf("have NumIn() = %d", typ.NumIn())
1329 for i := 0; i < typ.NumIn(); i++ {
1330 s += fmt.Sprintf(", In(%d) = %s", i, typ.In(i))
1344 func (*inner) m() {}
1345 func (*outer) m() {}
1347 func TestNestedMethods(t *testing.T) {
1348 typ := Typeof((*outer)(nil))
1349 if typ.NumMethod() != 1 || typ.Method(0).Func.Get() != NewValue((*outer).m).(*FuncValue).Get() {
1350 t.Errorf("Wrong method table for outer: (m=%p)", (*outer).m)
1351 for i := 0; i < typ.NumMethod(); i++ {
1353 t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Get())
1358 type innerInt struct {
1362 type outerInt struct {
1367 func (i *innerInt) m() int {
1371 func TestEmbeddedMethods(t *testing.T) {
1372 typ := Typeof((*outerInt)(nil))
1373 if typ.NumMethod() != 1 || typ.Method(0).Func.Get() != NewValue((*outerInt).m).(*FuncValue).Get() {
1374 t.Errorf("Wrong method table for outerInt: (m=%p)", (*outerInt).m)
1375 for i := 0; i < typ.NumMethod(); i++ {
1377 t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Get())
1382 if v := NewValue(i).Method(0).Call(nil)[0].(*IntValue).Get(); v != 3 {
1383 t.Errorf("i.m() = %d, want 3", v)
1386 o := &outerInt{1, innerInt{2}}
1387 if v := NewValue(o).Method(0).Call(nil)[0].(*IntValue).Get(); v != 2 {
1388 t.Errorf("i.m() = %d, want 2", v)
1392 if v := f(o); v != 2 {
1393 t.Errorf("f(o) = %d, want 2", v)
1397 func TestPtrTo(t *testing.T) {
1401 for i = 0; i < 100; i++ {
1404 for i = 0; i < 100; i++ {
1405 typ = typ.(*PtrType).Elem()
1407 if typ != Typeof(i) {
1408 t.Errorf("after 100 PtrTo and Elem, have %s, want %s", typ, Typeof(i))
1412 func TestAddr(t *testing.T) {
1418 v = v.(*PtrValue).Elem()
1420 v = v.(*PtrValue).Elem()
1421 v = v.(*StructValue).Field(0)
1422 v.(*IntValue).Set(2)
1424 t.Errorf("Addr.Elem.Set failed to set value")
1427 // Again but take address of the NewValue value.
1428 // Exercises generation of PtrTypes not present in the binary.
1431 v = v.(*PtrValue).Elem()
1432 v = v.(*PtrValue).Elem()
1434 v = v.(*PtrValue).Elem()
1435 v = v.(*StructValue).Field(0)
1436 v.(*IntValue).Set(3)
1438 t.Errorf("Addr.Elem.Set failed to set value")
1441 // Starting without pointer we should get changed value
1446 v = v.(*PtrValue).Elem()
1447 v = v.(*StructValue).Field(0)
1448 v.(*IntValue).Set(4)
1449 if p.X != 3 { // should be unchanged from last time
1450 t.Errorf("somehow value Set changed original p")
1452 p = v0.Interface().(struct {
1456 t.Errorf("Addr.Elem.Set valued to set value in top value")