blob: 3a1c220daf780d79143b2b67177df9f7b353b053 [file] [log] [blame]
Rob Pike536b1f22008-10-23 17:13:34 -07001// 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.
4
Russ Cox3b864e42009-08-12 13:18:37 -07005package reflect_test
Rob Pike536b1f22008-10-23 17:13:34 -07006
7import (
Russ Cox96cfd152009-05-21 11:50:20 -07008 "io";
9 "os";
Russ Cox3b864e42009-08-12 13:18:37 -070010 . "reflect";
Rob Pike93831d22009-04-29 22:16:53 -070011 "testing";
12 "unsafe";
Rob Pike536b1f22008-10-23 17:13:34 -070013)
14
Russ Cox64f4e0b2009-07-07 11:03:12 -070015type integer int
16type T struct { a int; b float64; c string; d *int }
Rob Pike536b1f22008-10-23 17:13:34 -070017
Russ Cox64f4e0b2009-07-07 11:03:12 -070018type pair struct {
19 i interface{};
20 s string;
21}
22
23func isDigit(c uint8) bool {
Rob Pike536b1f22008-10-23 17:13:34 -070024 return '0' <= c && c <= '9'
25}
26
Russ Cox64f4e0b2009-07-07 11:03:12 -070027func assert(t *testing.T, s, want string) {
28 if s != want {
29 t.Errorf("have %#q want %#q", s, want);
30 }
31}
32
33func typestring(i interface{}) string {
34 return Typeof(i).String();
35}
36
37var typeTests = []pair {
38 pair { struct { x int }{}, "int" },
39 pair { struct { x int8 }{}, "int8" },
40 pair { struct { x int16 }{}, "int16" },
41 pair { struct { x int32 }{}, "int32" },
42 pair { struct { x int64 }{}, "int64" },
43 pair { struct { x uint }{}, "uint" },
44 pair { struct { x uint8 }{}, "uint8" },
45 pair { struct { x uint16 }{}, "uint16" },
46 pair { struct { x uint32 }{}, "uint32" },
47 pair { struct { x uint64 }{}, "uint64" },
48 pair { struct { x float }{}, "float" },
49 pair { struct { x float32 }{}, "float32" },
50 pair { struct { x float64 }{}, "float64" },
51 pair { struct { x int8 }{}, "int8" },
52 pair { struct { x (**int8) }{}, "**int8" },
Russ Cox3b864e42009-08-12 13:18:37 -070053 pair { struct { x (**integer) }{}, "**reflect_test.integer" },
Russ Cox64f4e0b2009-07-07 11:03:12 -070054 pair { struct { x ([32]int32) }{}, "[32]int32" },
55 pair { struct { x ([]int8) }{}, "[]int8" },
56 pair { struct { x (map[string]int32) }{}, "map[string] int32" },
57 pair { struct { x (chan<-string) }{}, "chan<- string" },
58 pair { struct { x struct {c chan *int32; d float32} }{}, "struct { c chan *int32; d float32 }" },
59 pair { struct { x (func(a int8, b int32)) }{}, "func(int8, int32)" },
Russ Cox3b864e42009-08-12 13:18:37 -070060 pair { struct { x struct {c func(chan *integer, *int8)} }{}, "struct { c func(chan *reflect_test.integer, *int8) }" },
Russ Cox64f4e0b2009-07-07 11:03:12 -070061 pair { struct { x struct {a int8; b int32} }{}, "struct { a int8; b int32 }" },
Ian Lance Taylor39808db2009-07-20 23:26:03 -070062 pair { struct { x struct {a int8; b int8; c int32} }{}, "struct { a int8; b int8; c int32 }" },
63 pair { struct { x struct {a int8; b int8; c int8; d int32} }{}, "struct { a int8; b int8; c int8; d int32 }" },
64 pair { struct { x struct {a int8; b int8; c int8; d int8; e int32} }{}, "struct { a int8; b int8; c int8; d int8; e int32 }" },
65 pair { struct { x struct {a int8; b int8; c int8; d int8; e int8; f int32} }{}, "struct { a int8; b int8; c int8; d int8; e int8; f int32 }" },
Russ Cox64f4e0b2009-07-07 11:03:12 -070066 pair { struct { x struct {a int8 "hi there"; } }{}, `struct { a int8 "hi there" }` },
67 pair { struct { x struct {a int8 "hi \x00there\t\n\"\\"; } }{}, `struct { a int8 "hi \x00there\t\n\"\\" }` },
68 pair { struct { x struct {f func(args ...)} }{}, "struct { f func(...) }" },
69 pair { struct { x (interface { a(func(func(int)(int))(func(func(int))(int))); b() }) }{}, "interface { a (func(func(int) (int)) (func(func(int)) (int))); b () }" },
70}
71
72var valueTests = []pair {
73 pair { (int8)(0), "8" },
74 pair { (int16)(0), "16" },
75 pair { (int32)(0), "32" },
76 pair { (int64)(0), "64" },
77 pair { (uint8)(0), "8" },
78 pair { (uint16)(0), "16" },
79 pair { (uint32)(0), "32" },
80 pair { (uint64)(0), "64" },
81 pair { (float32)(0), "32.1" },
82 pair { (float64)(0), "64.2" },
83 pair { (string)(""), "stringy cheese" },
84 pair { (bool)(false), "true" },
85 pair { (*int8)(nil), "*int8(0)" },
86 pair { (**int8)(nil), "**int8(0)" },
87 pair { ([5]int32){}, "[5]int32{0, 0, 0, 0, 0}" },
Russ Cox3b864e42009-08-12 13:18:37 -070088 pair { (**integer)(nil), "**reflect_test.integer(0)" },
Russ Cox64f4e0b2009-07-07 11:03:12 -070089 pair { (map[string]int32)(nil), "map[string] int32{<can't iterate on maps>}" },
90 pair { (chan<-string)(nil), "chan<- string" },
91 pair { (struct {c chan *int32; d float32}){}, "struct { c chan *int32; d float32 }{chan *int32, 0}" },
92 pair { (func(a int8, b int32))(nil), "func(int8, int32)(0)" },
Russ Cox3b864e42009-08-12 13:18:37 -070093 pair { (struct {c func(chan *integer, *int8)}){}, "struct { c func(chan *reflect_test.integer, *int8) }{func(chan *reflect_test.integer, *int8)(0)}" },
Russ Cox64f4e0b2009-07-07 11:03:12 -070094 pair { (struct {a int8; b int32}){}, "struct { a int8; b int32 }{0, 0}" },
Ian Lance Taylor39808db2009-07-20 23:26:03 -070095 pair { (struct {a int8; b int8; c int32}){}, "struct { a int8; b int8; c int32 }{0, 0, 0}" },
Russ Cox64f4e0b2009-07-07 11:03:12 -070096}
97
98func testType(t *testing.T, i int, typ Type, want string) {
99 s := typ.String();
100 if s != want {
101 t.Errorf("#%d: have %#q, want %#q", i, s, want);
102 }
103}
104
105func TestTypes(t *testing.T) {
106 for i, tt := range typeTests {
107 testType(t, i, NewValue(tt.i).(*StructValue).Field(0).Type(), tt.s);
108 }
109}
110
111func TestValue(t *testing.T) {
112 for i, tt := range valueTests {
113 v := NewValue(tt.i);
114 switch v := v.(type) {
Russ Cox3b864e42009-08-12 13:18:37 -0700115 case *IntValue:
Russ Cox64f4e0b2009-07-07 11:03:12 -0700116 v.Set(132);
Russ Cox3b864e42009-08-12 13:18:37 -0700117 case *Int8Value:
Russ Cox64f4e0b2009-07-07 11:03:12 -0700118 v.Set(8);
Russ Cox3b864e42009-08-12 13:18:37 -0700119 case *Int16Value:
Russ Cox64f4e0b2009-07-07 11:03:12 -0700120 v.Set(16);
Russ Cox3b864e42009-08-12 13:18:37 -0700121 case *Int32Value:
Russ Cox64f4e0b2009-07-07 11:03:12 -0700122 v.Set(32);
Russ Cox3b864e42009-08-12 13:18:37 -0700123 case *Int64Value:
Russ Cox64f4e0b2009-07-07 11:03:12 -0700124 v.Set(64);
Russ Cox3b864e42009-08-12 13:18:37 -0700125 case *UintValue:
Russ Cox64f4e0b2009-07-07 11:03:12 -0700126 v.Set(132);
Russ Cox3b864e42009-08-12 13:18:37 -0700127 case *Uint8Value:
Russ Cox64f4e0b2009-07-07 11:03:12 -0700128 v.Set(8);
Russ Cox3b864e42009-08-12 13:18:37 -0700129 case *Uint16Value:
Russ Cox64f4e0b2009-07-07 11:03:12 -0700130 v.Set(16);
Russ Cox3b864e42009-08-12 13:18:37 -0700131 case *Uint32Value:
Russ Cox64f4e0b2009-07-07 11:03:12 -0700132 v.Set(32);
Russ Cox3b864e42009-08-12 13:18:37 -0700133 case *Uint64Value:
Russ Cox64f4e0b2009-07-07 11:03:12 -0700134 v.Set(64);
Russ Cox3b864e42009-08-12 13:18:37 -0700135 case *FloatValue:
Russ Cox64f4e0b2009-07-07 11:03:12 -0700136 v.Set(3200.0);
Russ Cox3b864e42009-08-12 13:18:37 -0700137 case *Float32Value:
Russ Cox64f4e0b2009-07-07 11:03:12 -0700138 v.Set(32.1);
Russ Cox3b864e42009-08-12 13:18:37 -0700139 case *Float64Value:
Russ Cox64f4e0b2009-07-07 11:03:12 -0700140 v.Set(64.2);
Russ Cox3b864e42009-08-12 13:18:37 -0700141 case *StringValue:
Russ Cox64f4e0b2009-07-07 11:03:12 -0700142 v.Set("stringy cheese");
Russ Cox3b864e42009-08-12 13:18:37 -0700143 case *BoolValue:
Russ Cox64f4e0b2009-07-07 11:03:12 -0700144 v.Set(true);
Rob Pike536b1f22008-10-23 17:13:34 -0700145 }
Russ Cox64f4e0b2009-07-07 11:03:12 -0700146 s := valueToString(v);
147 if s != tt.s {
148 t.Errorf("#%d: have %#q, want %#q", i, s, tt.s);
Rob Pike536b1f22008-10-23 17:13:34 -0700149 }
150 }
Rob Pike536b1f22008-10-23 17:13:34 -0700151}
152
Russ Cox64f4e0b2009-07-07 11:03:12 -0700153var _i = 7;
154
155var valueToStringTests = []pair {
156 pair { 123, "123" },
157 pair { 123.4, "123.4" },
158 pair { byte(123), "123" },
159 pair { "abc", "abc" },
Russ Cox3b864e42009-08-12 13:18:37 -0700160 pair { T{123, 456.75, "hello", &_i}, "reflect_test.T{123, 456.75, hello, *int(&7)}" },
161 pair { new(chan *T), "*chan *reflect_test.T(&chan *reflect_test.T)" },
Russ Cox64f4e0b2009-07-07 11:03:12 -0700162 pair { [10]int{1,2,3,4,5,6,7,8,9,10}, "[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}" },
163 pair { &[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})" },
164 pair { []int{1,2,3,4,5,6,7,8,9,10}, "[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}" },
165 pair { &[]int{1,2,3,4,5,6,7,8,9,10}, "*[]int(&[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})" }
Rob Pike536b1f22008-10-23 17:13:34 -0700166}
167
Russ Cox64f4e0b2009-07-07 11:03:12 -0700168func TestValueToString(t *testing.T) {
169 for i, test := range valueToStringTests {
170 s := valueToString(NewValue(test.i));
171 if s != test.s {
172 t.Errorf("#%d: have %#q, want %#q", i, s, test.s);
Rob Pike419e1e02008-11-12 19:05:05 -0800173 }
174 }
Russ Cox64f4e0b2009-07-07 11:03:12 -0700175}
Rob Pike419e1e02008-11-12 19:05:05 -0800176
Russ Cox64f4e0b2009-07-07 11:03:12 -0700177func TestArrayElemSet(t *testing.T) {
178 v := NewValue([10]int{1,2,3,4,5,6,7,8,9,10});
179 v.(*ArrayValue).Elem(4).(*IntValue).Set(123);
180 s := valueToString(v);
181 const want = "[10]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}";
182 if s != want {
183 t.Errorf("[10]int: have %#q want %#q", s, want);
184 }
Rob Pikea45f9472008-11-04 22:54:11 -0800185
Russ Cox64f4e0b2009-07-07 11:03:12 -0700186 v = NewValue([]int{1,2,3,4,5,6,7,8,9,10});
187 v.(*SliceValue).Elem(4).(*IntValue).Set(123);
188 s = valueToString(v);
189 const want1 = "[]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}";
190 if s != want1 {
191 t.Errorf("[]int: have %#q want %#q", s, want1);
192 }
193}
Rob Pikea45f9472008-11-04 22:54:11 -0800194
Russ Cox64f4e0b2009-07-07 11:03:12 -0700195func TestPtrPointTo(t *testing.T) {
196 var ip *int32;
197 var i int32 = 1234;
198 vip := NewValue(&ip);
199 vi := NewValue(i);
200 vip.(*PtrValue).Elem().(*PtrValue).PointTo(vi);
201 if *ip != 1234 {
202 t.Errorf("got %d, want 1234", *ip);
203 }
204}
Rob Pikea45f9472008-11-04 22:54:11 -0800205
Russ Cox64f4e0b2009-07-07 11:03:12 -0700206func TestAll(t *testing.T) { // TODO(r): wrap up better
207 testType(t, 1, Typeof((int8)(0)), "int8");
208 testType(t, 2, Typeof((*int8)(nil)).(*PtrType).Elem(), "int8");
Rob Pikea45f9472008-11-04 22:54:11 -0800209
Russ Cox64f4e0b2009-07-07 11:03:12 -0700210 typ := Typeof((*struct{c chan *int32; d float32})(nil));
211 testType(t, 3, typ, "*struct { c chan *int32; d float32 }");
212 etyp := typ.(*PtrType).Elem();
213 testType(t, 4, etyp, "struct { c chan *int32; d float32 }");
214 styp := etyp.(*StructType);
215 f := styp.Field(0);
216 testType(t, 5, f.Type, "chan *int32");
Rob Pikea93c5c82009-07-16 18:21:14 -0700217
218 f, present := styp.FieldByName("d");
219 if !present {
220 t.Errorf("FieldByName says present field is absent");
221 }
Russ Cox64f4e0b2009-07-07 11:03:12 -0700222 testType(t, 6, f.Type, "float32");
Rob Pikea45f9472008-11-04 22:54:11 -0800223
Rob Pikea93c5c82009-07-16 18:21:14 -0700224 f, present = styp.FieldByName("absent");
225 if present {
226 t.Errorf("FieldByName says absent field is present");
227 }
228
Ian Lance Taylorf0c00f72009-07-21 14:06:14 -0700229 typ = Typeof([32]int32{});
Russ Cox64f4e0b2009-07-07 11:03:12 -0700230 testType(t, 7, typ, "[32]int32");
231 testType(t, 8, typ.(*ArrayType).Elem(), "int32");
Rob Pikea45f9472008-11-04 22:54:11 -0800232
Russ Cox64f4e0b2009-07-07 11:03:12 -0700233 typ = Typeof((map[string]*int32)(nil));
234 testType(t, 9, typ, "map[string] *int32");
235 mtyp := typ.(*MapType);
236 testType(t, 10, mtyp.Key(), "string");
237 testType(t, 11, mtyp.Elem(), "*int32");
Rob Pikebdbb9582008-11-05 08:17:01 -0800238
Russ Cox64f4e0b2009-07-07 11:03:12 -0700239 typ = Typeof((chan<-string)(nil));
240 testType(t, 12, typ, "chan<- string");
241 testType(t, 13, typ.(*ChanType).Elem(), "string");
Rob Pike5a1cbe82008-11-05 13:01:33 -0800242
243 // make sure tag strings are not part of element type
Russ Cox64f4e0b2009-07-07 11:03:12 -0700244 typ = Typeof(struct{d []uint32 "TAG"}{}).(*StructType).Field(0).Type;
245 testType(t, 14, typ, "[]uint32");
Rob Pike536b1f22008-10-23 17:13:34 -0700246}
Russ Cox387df5e2008-11-24 14:51:33 -0800247
Russ Cox839a6842009-01-20 14:40:40 -0800248func TestInterfaceGet(t *testing.T) {
Russ Cox387df5e2008-11-24 14:51:33 -0800249 var inter struct { e interface{ } };
250 inter.e = 123.456;
Russ Cox96cfd152009-05-21 11:50:20 -0700251 v1 := NewValue(&inter);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700252 v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0);
253 assert(t, v2.Type().String(), "interface { }");
254 i2 := v2.(*InterfaceValue).Interface();
Russ Cox96cfd152009-05-21 11:50:20 -0700255 v3 := NewValue(i2);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700256 assert(t, v3.Type().String(), "float");
Russ Cox387df5e2008-11-24 14:51:33 -0800257}
Russ Coxd0e30cd2008-12-10 15:55:59 -0800258
Russ Coxac6ebfd2009-04-06 21:28:04 -0700259func TestInterfaceValue(t *testing.T) {
260 var inter struct { e interface{ } };
261 inter.e = 123.456;
Russ Cox96cfd152009-05-21 11:50:20 -0700262 v1 := NewValue(&inter);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700263 v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0);
264 assert(t, v2.Type().String(), "interface { }");
265 v3 := v2.(*InterfaceValue).Elem();
266 assert(t, v3.Type().String(), "float");
Russ Cox64627b02009-04-15 00:55:58 -0700267
Russ Cox4b8c13d2009-04-14 19:03:57 -0700268 i3 := v2.Interface();
269 if f, ok := i3.(float); !ok {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700270 t.Error("v2.Interface() did not return float, got ", Typeof(i3));
Russ Cox4b8c13d2009-04-14 19:03:57 -0700271 }
Russ Coxac6ebfd2009-04-06 21:28:04 -0700272}
273
Ian Lance Taylorca9765d2009-04-14 06:46:01 -0700274func TestFunctionValue(t *testing.T) {
Russ Cox96cfd152009-05-21 11:50:20 -0700275 v := NewValue(func() {});
Ian Lance Taylorca9765d2009-04-14 06:46:01 -0700276 if v.Interface() != v.Interface() {
277 t.Fatalf("TestFunction != itself");
278 }
Russ Cox64f4e0b2009-07-07 11:03:12 -0700279 assert(t, v.Type().String(), "func()");
Ian Lance Taylorca9765d2009-04-14 06:46:01 -0700280}
281
Russ Cox839a6842009-01-20 14:40:40 -0800282func TestCopyArray(t *testing.T) {
Russ Coxbe2edb52009-03-03 08:39:12 -0800283 a := []int{ 1, 2, 3, 4, 10, 9, 8, 7 };
284 b := []int{ 11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44 };
285 c := []int{ 11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44 };
Russ Coxd47d8882008-12-18 22:37:22 -0800286 va := NewValue(&a);
287 vb := NewValue(&b);
Russ Coxd0e30cd2008-12-10 15:55:59 -0800288 for i := 0; i < len(b); i++ {
289 if b[i] != c[i] {
290 t.Fatalf("b != c before test");
291 }
292 }
Russ Cox64f4e0b2009-07-07 11:03:12 -0700293 aa := va.(*PtrValue).Elem().(*SliceValue);
294 ab := vb.(*PtrValue).Elem().(*SliceValue);
Russ Coxd47d8882008-12-18 22:37:22 -0800295 for tocopy := 1; tocopy <= 7; tocopy++ {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700296 aa.SetLen(tocopy);
297 ArrayCopy(ab, aa);
298 aa.SetLen(8);
Russ Coxd0e30cd2008-12-10 15:55:59 -0800299 for i := 0; i < tocopy; i++ {
300 if a[i] != b[i] {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700301 t.Errorf("(i) tocopy=%d a[%d]=%d, b[%d]=%d",
Russ Coxd0e30cd2008-12-10 15:55:59 -0800302 tocopy, i, a[i], i, b[i]);
303 }
304 }
305 for i := tocopy; i < len(b); i++ {
306 if b[i] != c[i] {
307 if i < len(a) {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700308 t.Errorf("(ii) tocopy=%d a[%d]=%d, b[%d]=%d, c[%d]=%d",
Russ Coxd0e30cd2008-12-10 15:55:59 -0800309 tocopy, i, a[i], i, b[i], i, c[i]);
310 } else {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700311 t.Errorf("(iii) tocopy=%d b[%d]=%d, c[%d]=%d",
Russ Coxd0e30cd2008-12-10 15:55:59 -0800312 tocopy, i, b[i], i, c[i]);
313 }
Russ Coxd47d8882008-12-18 22:37:22 -0800314 } else {
315 t.Logf("tocopy=%d elem %d is okay\n", tocopy, i);
Russ Coxd0e30cd2008-12-10 15:55:59 -0800316 }
317 }
318 }
319}
Russ Cox484ba932009-01-09 00:17:46 -0800320
Russ Cox839a6842009-01-20 14:40:40 -0800321func TestBigUnnamedStruct(t *testing.T) {
Russ Coxbe2edb52009-03-03 08:39:12 -0800322 b := struct{a,b,c,d int64}{1, 2, 3, 4};
Russ Cox484ba932009-01-09 00:17:46 -0800323 v := NewValue(b);
324 b1 := v.Interface().(struct{a,b,c,d int64});
325 if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700326 t.Errorf("NewValue(%v).Interface().(*Big) = %v", b, b1);
Russ Cox484ba932009-01-09 00:17:46 -0800327 }
328}
329
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800330type big struct {
Russ Cox484ba932009-01-09 00:17:46 -0800331 a, b, c, d, e int64
332}
Russ Cox839a6842009-01-20 14:40:40 -0800333func TestBigStruct(t *testing.T) {
Russ Coxbe2edb52009-03-03 08:39:12 -0800334 b := big{1, 2, 3, 4, 5};
Russ Cox484ba932009-01-09 00:17:46 -0800335 v := NewValue(b);
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800336 b1 := v.Interface().(big);
Russ Cox484ba932009-01-09 00:17:46 -0800337 if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d || b1.e != b.e {
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800338 t.Errorf("NewValue(%v).Interface().(big) = %v", b, b1);
Russ Cox484ba932009-01-09 00:17:46 -0800339 }
340}
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700341
342type Basic struct {
343 x int;
344 y float32
345}
346
Russ Cox64627b02009-04-15 00:55:58 -0700347type NotBasic Basic
348
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700349type DeepEqualTest struct {
350 a, b interface{};
351 eq bool;
352}
353
354var deepEqualTests = []DeepEqualTest {
355 // Equalities
356 DeepEqualTest{ 1, 1, true },
357 DeepEqualTest{ int32(1), int32(1), true },
358 DeepEqualTest{ 0.5, 0.5, true },
359 DeepEqualTest{ float32(0.5), float32(0.5), true },
360 DeepEqualTest{ "hello", "hello", true },
361 DeepEqualTest{ make([]int, 10), make([]int, 10), true },
362 DeepEqualTest{ &[3]int{ 1, 2, 3 }, &[3]int{ 1, 2, 3 }, true },
363 DeepEqualTest{ Basic{ 1, 0.5 }, Basic{ 1, 0.5 }, true },
Russ Cox48662232009-06-25 14:25:38 -0700364 DeepEqualTest{ os.Error(nil), os.Error(nil), true },
Rob Pike1880b902009-07-10 11:20:10 -0700365 DeepEqualTest{ map[int]string{ 1:"one", 2:"two" }, map[int]string{ 2:"two", 1:"one" }, true },
Russ Coxa439f662009-07-01 16:45:09 -0700366
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700367 // Inequalities
368 DeepEqualTest{ 1, 2, false },
369 DeepEqualTest{ int32(1), int32(2), false },
370 DeepEqualTest{ 0.5, 0.6, false },
371 DeepEqualTest{ float32(0.5), float32(0.6), false },
372 DeepEqualTest{ "hello", "hey", false },
373 DeepEqualTest{ make([]int, 10), make([]int, 11), false },
374 DeepEqualTest{ &[3]int{ 1, 2, 3 }, &[3]int{ 1, 2, 4 }, false },
375 DeepEqualTest{ Basic{ 1, 0.5 }, Basic{ 1, 0.6 }, false },
Russ Coxa439f662009-07-01 16:45:09 -0700376 DeepEqualTest{ Basic{ 1, 0 }, Basic{ 2, 0 }, false },
Rob Pike1880b902009-07-10 11:20:10 -0700377 DeepEqualTest{ map[int]string{ 1:"one", 3:"two" }, map[int]string{ 2:"two", 1:"one" }, false },
378 DeepEqualTest{ map[int]string{ 1:"one", 2:"txo" }, map[int]string{ 2:"two", 1:"one" }, false },
379 DeepEqualTest{ map[int]string{ 1:"one", }, map[int]string{ 2:"two", 1:"one" }, false },
380 DeepEqualTest{ map[int]string{ 2:"two", 1:"one" }, map[int]string{ 1:"one", }, false },
Russ Coxa439f662009-07-01 16:45:09 -0700381
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700382 // Mismatched types
383 DeepEqualTest{ 1, 1.0, false },
384 DeepEqualTest{ int32(1), int64(1), false },
385 DeepEqualTest{ 0.5, "hello", false },
386 DeepEqualTest{ []int{ 1, 2, 3 }, [3]int{ 1, 2, 3 }, false },
387 DeepEqualTest{ &[3]interface{} { 1, 2, 4 }, &[3]interface{} { 1, 2, "s" }, false },
Russ Cox64627b02009-04-15 00:55:58 -0700388 DeepEqualTest{ Basic{ 1, 0.5 }, NotBasic{ 1, 0.5 }, false },
Rob Pike1880b902009-07-10 11:20:10 -0700389 DeepEqualTest{ map[uint]string{ 1:"one", 2:"two" }, map[int]string{ 2:"two", 1:"one" }, false },
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700390}
391
392func TestDeepEqual(t *testing.T) {
393 for i, test := range deepEqualTests {
394 if r := DeepEqual(test.a, test.b); r != test.eq {
395 t.Errorf("DeepEqual(%v, %v) = %v, want %v", test.a, test.b, r, test.eq);
396 }
397 }
398}
399
Russ Cox48662232009-06-25 14:25:38 -0700400func TestTypeof(t *testing.T) {
401 for i, test := range deepEqualTests {
402 v := NewValue(test.a);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700403 if v == nil {
404 continue;
405 }
Russ Cox48662232009-06-25 14:25:38 -0700406 typ := Typeof(test.a);
407 if typ != v.Type() {
408 t.Errorf("Typeof(%v) = %v, but NewValue(%v).Type() = %v", test.a, typ, test.a, v.Type());
409 }
410 }
411}
412
Rob Pike1880b902009-07-10 11:20:10 -0700413type Recursive struct {
414 x int;
415 r *Recursive
416}
417
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700418func TestDeepEqualRecursiveStruct(t *testing.T) {
419 a, b := new(Recursive), new(Recursive);
420 *a = Recursive{ 12, a };
421 *b = Recursive{ 12, b };
422 if !DeepEqual(a, b) {
423 t.Error("DeepEqual(recursive same) = false, want true");
424 }
425}
426
Rob Pike1880b902009-07-10 11:20:10 -0700427type Complex struct {
428 a int;
429 b [3]*Complex;
430 c *string;
431 d map[float]float
432}
433
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700434func TestDeepEqualComplexStruct(t *testing.T) {
435 m := make(map[float]float);
436 stra, strb := "hello", "hello";
437 a, b := new(Complex), new(Complex);
438 *a = Complex{5, [3]*Complex{a, b, a}, &stra, m};
439 *b = Complex{5, [3]*Complex{b, a, a}, &strb, m};
440 if !DeepEqual(a, b) {
441 t.Error("DeepEqual(complex same) = false, want true");
442 }
443}
444
445func TestDeepEqualComplexStructInequality(t *testing.T) {
446 m := make(map[float]float);
447 stra, strb := "hello", "helloo"; // Difference is here
448 a, b := new(Complex), new(Complex);
449 *a = Complex{5, [3]*Complex{a, b, a}, &stra, m};
450 *b = Complex{5, [3]*Complex{b, a, a}, &strb, m};
451 if DeepEqual(a, b) {
452 t.Error("DeepEqual(complex different) = true, want false");
453 }
454}
Rob Pike93831d22009-04-29 22:16:53 -0700455
456
457func check2ndField(x interface{}, offs uintptr, t *testing.T) {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700458 s := NewValue(x).(*StructValue);
459 f := s.Type().(*StructType).Field(1);
460 if f.Offset != offs {
461 t.Error("mismatched offsets in structure alignment:", f.Offset, offs);
Rob Pike93831d22009-04-29 22:16:53 -0700462 }
463}
464
465// Check that structure alignment & offsets viewed through reflect agree with those
466// from the compiler itself.
467func TestAlignment(t *testing.T) {
468 type T1inner struct {
469 a int
470 }
471 type T1 struct {
472 T1inner;
473 f int;
474 }
475 type T2inner struct {
476 a, b int
477 }
478 type T2 struct {
479 T2inner;
480 f int;
481 }
482
483 x := T1{T1inner{2}, 17};
484 check2ndField(x, uintptr(unsafe.Pointer(&x.f)) - uintptr(unsafe.Pointer(&x)), t);
485
486 x1 := T2{T2inner{2, 3}, 17};
487 check2ndField(x1, uintptr(unsafe.Pointer(&x1.f)) - uintptr(unsafe.Pointer(&x1)), t);
488}
Rob Pikea8f6e382009-05-12 14:57:44 -0700489
Russ Cox64f4e0b2009-07-07 11:03:12 -0700490type IsNiller interface {
Rob Pikea8f6e382009-05-12 14:57:44 -0700491 IsNil() bool
492}
493
494func Nil(a interface{}, t *testing.T) {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700495 n := NewValue(a).(*StructValue).Field(0).(IsNiller);
Rob Pikea8f6e382009-05-12 14:57:44 -0700496 if !n.IsNil() {
497 t.Errorf("%v should be nil", a)
498 }
499}
500
501func NotNil(a interface{}, t *testing.T) {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700502 n := NewValue(a).(*StructValue).Field(0).(IsNiller);
Rob Pikea8f6e382009-05-12 14:57:44 -0700503 if n.IsNil() {
504 t.Errorf("value of type %v should not be nil", NewValue(a).Type().String())
505 }
506}
507
508func TestIsNil(t *testing.T) {
509 // These do not implement IsNil
Russ Cox64f4e0b2009-07-07 11:03:12 -0700510 doNotNil := []interface{}{ int(0), float32(0), struct{a int}{} };
Rob Pikea8f6e382009-05-12 14:57:44 -0700511 for i, ts := range doNotNil {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700512 ty := Typeof(ts);
513 v := MakeZero(ty);
514 if nilable, ok := v.(IsNiller); ok {
Rob Pikea8f6e382009-05-12 14:57:44 -0700515 t.Errorf("%s is nilable; should not be", ts)
516 }
517 }
518
Russ Cox64f4e0b2009-07-07 11:03:12 -0700519 // These do implement IsNil.
520 // Wrap in extra struct to hide interface type.
521 doNil := []interface{}{
522 struct{x *int}{},
523 struct{x interface{}}{},
524 struct{x map[string]int}{},
525 struct{x func()bool}{},
526 struct{x chan int}{},
527 struct{x []string}{}
528 };
Rob Pikea8f6e382009-05-12 14:57:44 -0700529 for i, ts := range doNil {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700530 ty := Typeof(ts).(*StructType).Field(0).Type;
531 v := MakeZero(ty);
532 if nilable, ok := v.(IsNiller); !ok {
Russ Cox2d5d4a12009-05-20 15:42:14 -0700533 t.Errorf("%s %T is not nilable; should be", ts, v)
Rob Pikea8f6e382009-05-12 14:57:44 -0700534 }
535 }
Russ Cox64f4e0b2009-07-07 11:03:12 -0700536
Rob Pikea8f6e382009-05-12 14:57:44 -0700537 // Check the implementations
Russ Cox64f4e0b2009-07-07 11:03:12 -0700538 var pi struct {x *int}
Rob Pikea8f6e382009-05-12 14:57:44 -0700539 Nil(pi, t);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700540 pi.x = new(int);
Rob Pikea8f6e382009-05-12 14:57:44 -0700541 NotNil(pi, t);
542
Russ Cox64f4e0b2009-07-07 11:03:12 -0700543 var si struct {x []int}
Rob Pikea8f6e382009-05-12 14:57:44 -0700544 Nil(si, t);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700545 si.x = make([]int, 10);
Rob Pikea8f6e382009-05-12 14:57:44 -0700546 NotNil(si, t);
547
Russ Cox64f4e0b2009-07-07 11:03:12 -0700548 var ci struct {x chan int}
549 Nil(ci, t);
550 ci.x = make(chan int);
551 NotNil(ci, t);
Rob Pikea8f6e382009-05-12 14:57:44 -0700552
Russ Cox64f4e0b2009-07-07 11:03:12 -0700553 var mi struct {x map[int]int}
554 Nil(mi, t);
555 mi.x = make(map[int]int);
556 NotNil(mi, t);
557
558 var ii struct {x interface {}}
Rob Pikea8f6e382009-05-12 14:57:44 -0700559 Nil(ii, t);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700560 ii.x = 2;
Rob Pikea8f6e382009-05-12 14:57:44 -0700561 NotNil(ii, t);
562
Russ Cox64f4e0b2009-07-07 11:03:12 -0700563 var fi struct {x func(t *testing.T)}
Rob Pikea8f6e382009-05-12 14:57:44 -0700564 Nil(fi, t);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700565 fi.x = TestIsNil;
Rob Pikea8f6e382009-05-12 14:57:44 -0700566 NotNil(fi, t);
567}
Russ Cox96cfd152009-05-21 11:50:20 -0700568
569func TestInterfaceExtraction(t *testing.T) {
570 var s struct {
571 w io.Writer;
572 }
573
574 s.w = os.Stdout;
Russ Cox64f4e0b2009-07-07 11:03:12 -0700575 v := Indirect(NewValue(&s)).(*StructValue).Field(0).Interface();
Russ Cox96cfd152009-05-21 11:50:20 -0700576 if v != s.w.(interface{}) {
Rob Pikeb75df2f2009-07-16 15:01:21 -0700577 t.Error("Interface() on interface: ", v, s.w);
Russ Cox96cfd152009-05-21 11:50:20 -0700578 }
579}
Russ Coxf9666232009-05-21 14:06:43 -0700580
581func TestInterfaceEditing(t *testing.T) {
582 // strings are bigger than one word,
583 // so the interface conversion allocates
584 // memory to hold a string and puts that
585 // pointer in the interface.
586 var i interface{} = "hello";
587
588 // if i pass the interface value by value
589 // to NewValue, i should get a fresh copy
590 // of the value.
591 v := NewValue(i);
592
593 // and setting that copy to "bye" should
594 // not change the value stored in i.
Russ Cox64f4e0b2009-07-07 11:03:12 -0700595 v.(*StringValue).Set("bye");
Russ Coxf9666232009-05-21 14:06:43 -0700596 if i.(string) != "hello" {
597 t.Errorf(`Set("bye") changed i to %s`, i.(string));
598 }
599
600 // the same should be true of smaller items.
601 i = 123;
602 v = NewValue(i);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700603 v.(*IntValue).Set(234);
Russ Coxf9666232009-05-21 14:06:43 -0700604 if i.(int) != 123 {
605 t.Errorf("Set(234) changed i to %d", i.(int));
606 }
607}
David Symondsd4e57ff2009-06-15 18:35:04 -0700608
609func TestNilPtrValueSub(t *testing.T) {
610 var pi *int;
Russ Cox64f4e0b2009-07-07 11:03:12 -0700611 if pv := NewValue(pi).(*PtrValue); pv.Elem() != nil {
612 t.Error("NewValue((*int)(nil)).(*PtrValue).Elem() != nil");
David Symondsd4e57ff2009-06-15 18:35:04 -0700613 }
614}
Russ Cox764b6ec2009-07-08 13:55:57 -0700615
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700616func TestMap(t *testing.T) {
Russ Cox764b6ec2009-07-08 13:55:57 -0700617 m := map[string]int{ "a": 1, "b": 2 };
618 mv := NewValue(m).(*MapValue);
619 if n := mv.Len(); n != len(m) {
620 t.Errorf("Len = %d, want %d", n, len(m));
621 }
622 keys := mv.Keys();
623 i := 0;
624 newmap := MakeMap(mv.Type().(*MapType));
625 for k, v := range m {
626 // Check that returned Keys match keys in range.
627 // These aren't required to be in the same order,
628 // but they are in this implementation, which makes
629 // the test easier.
630 if i >= len(keys) {
631 t.Errorf("Missing key #%d %q", i, k);
632 } else if kv := keys[i].(*StringValue); kv.Get() != k {
633 t.Errorf("Keys[%d] = %q, want %q", i, kv.Get(), k);
634 }
635 i++;
636
637 // Check that value lookup is correct.
Russ Cox92e92572009-07-10 16:32:26 -0700638 vv := mv.Elem(NewValue(k));
Russ Cox764b6ec2009-07-08 13:55:57 -0700639 if vi := vv.(*IntValue).Get(); vi != v {
640 t.Errorf("Key %q: have value %d, want %d", vi, v);
641 }
642
643 // Copy into new map.
Russ Cox92e92572009-07-10 16:32:26 -0700644 newmap.SetElem(NewValue(k), NewValue(v));
Russ Cox764b6ec2009-07-08 13:55:57 -0700645 }
Russ Cox92e92572009-07-10 16:32:26 -0700646 vv := mv.Elem(NewValue("not-present"));
Russ Cox764b6ec2009-07-08 13:55:57 -0700647 if vv != nil {
648 t.Errorf("Invalid key: got non-nil value %s", valueToString(vv));
649 }
650
651 newm := newmap.Interface().(map[string]int);
652 if len(newm) != len(m) {
653 t.Errorf("length after copy: newm=%d, m=%d", newm, m);
654 }
655
656 for k, v := range newm {
657 mv, ok := m[k];
658 if mv != v {
659 t.Errorf("newm[%q] = %d, but m[%q] = %d, %v", k, v, k, mv, ok);
660 }
661 }
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700662
Russ Cox92e92572009-07-10 16:32:26 -0700663 newmap.SetElem(NewValue("a"), nil);
Russ Cox764b6ec2009-07-08 13:55:57 -0700664 v, ok := newm["a"];
665 if ok {
666 t.Errorf("newm[\"a\"] = %d after delete", v);
667 }
668}
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700669
670func TestChan(t *testing.T) {
671 for loop := 0; loop < 2; loop++ {
672 var c chan int;
673 var cv *ChanValue;
674
675 // check both ways to allocate channels
676 switch loop {
677 case 1:
678 c = make(chan int, 1);
679 cv = NewValue(c).(*ChanValue);
680 case 0:
681 cv = MakeChan(Typeof(c).(*ChanType), 1);
682 c = cv.Interface().(chan int);
683 }
684
685 // Send
686 cv.Send(NewValue(2));
687 if i := <-c; i != 2 {
688 t.Errorf("reflect Send 2, native recv %d", i);
689 }
690
691 // Recv
692 c <- 3;
693 if i := cv.Recv().(*IntValue).Get(); i != 3 {
694 t.Errorf("native send 3, reflect Recv %d", i);
695 }
696
697 // TryRecv fail
698 val := cv.TryRecv();
699 if val != nil {
700 t.Errorf("TryRecv on empty chan: %s", valueToString(val));
701 }
702
703 // TryRecv success
704 c <- 4;
705 val = cv.TryRecv();
706 if val == nil {
707 t.Errorf("TryRecv on ready chan got nil");
708 } else if i := val.(*IntValue).Get(); i != 4 {
709 t.Errorf("native send 4, TryRecv %d", i);
710 }
711
712 // TrySend fail
713 c <- 100;
714 ok := cv.TrySend(NewValue(5));
715 i := <-c;
716 if ok {
717 t.Errorf("TrySend on full chan succeeded: value %d", i);
718 }
719
720 // TrySend success
721 ok = cv.TrySend(NewValue(6));
722 if !ok {
723 t.Errorf("TrySend on empty chan failed");
724 } else {
725 if i = <-c; i != 6 {
726 t.Errorf("TrySend 6, recv %d", i);
727 }
728 }
Russ Cox653cef12009-08-26 10:47:18 -0700729
730 // Close
731 c <- 123;
732 cv.Close();
733 if cv.Closed() {
734 t.Errorf("closed too soon - 1");
735 }
736 if i := cv.Recv().(*IntValue).Get(); i != 123 {
737 t.Errorf("send 123 then close; Recv %d", i);
738 }
739 if cv.Closed() {
740 t.Errorf("closed too soon - 2");
741 }
742 if i := cv.Recv().(*IntValue).Get(); i != 0 {
743 t.Errorf("after close Recv %d", i);
744 }
745 if !cv.Closed() {
746 t.Errorf("not closed");
747 }
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700748 }
749
750 // check creation of unbuffered channel
751 var c chan int;
752 cv := MakeChan(Typeof(c).(*ChanType), 0);
753 c = cv.Interface().(chan int);
754 if cv.TrySend(NewValue(7)) {
755 t.Errorf("TrySend on sync chan succeeded");
756 }
757 if cv.TryRecv() != nil {
758 t.Errorf("TryRecv on sync chan succeeded");
759 }
760}
761
Russ Coxbba278a2009-07-08 18:16:09 -0700762// Difficult test for function call because of
763// implicit padding between arguments.
764func dummy(b byte, c int, d byte) (i byte, j int, k byte){
765 return b, c, d;
766}
767
768func TestFunc(t *testing.T) {
769 ret := NewValue(dummy).(*FuncValue).Call([]Value{NewValue(byte(10)), NewValue(20), NewValue(byte(30))});
770 if len(ret) != 3 {
771 t.Fatalf("Call returned %d values, want 3", len(ret));
772 }
773
774 i := ret[0].(*Uint8Value).Get();
775 j := ret[1].(*IntValue).Get();
776 k := ret[2].(*Uint8Value).Get();
777 if i != 10 || j != 20 || k != 30 {
778 t.Errorf("Call returned %d, %d, %d; want 10, 20, 30", i, j, k);
779 }
780}
Russ Cox12ebbe72009-07-09 17:27:49 -0700781
782type Point struct {
783 x, y int;
784}
785
786func (p Point) Dist(scale int) int {
787 return p.x*p.x*scale + p.y*p.y*scale;
788}
789
790func TestMethod(t *testing.T) {
791 // Non-curried method of type.
792 p := Point{3, 4};
Russ Cox3b864e42009-08-12 13:18:37 -0700793 i := Typeof(p).Method(0).Func.Call([]Value{NewValue(p), NewValue(10)})[0].(*IntValue).Get();
Russ Cox12ebbe72009-07-09 17:27:49 -0700794 if i != 250 {
795 t.Errorf("Type Method returned %d; want 250", i);
796 }
797
798 // Curried method of value.
799 i = NewValue(p).Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get();
800 if i != 250 {
801 t.Errorf("Value Method returned %d; want 250", i);
802 }
803
804 // Curried method of interface value.
805 // Have to wrap interface value in a struct to get at it.
806 // Passing it to NewValue directly would
807 // access the underlying Point, not the interface.
808 var s = struct{x interface{Dist(int) int}}{p};
809 pv := NewValue(s).(*StructValue).Field(0);
810 i = pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get();
811 if i != 250 {
812 t.Errorf("Interface Method returned %d; want 250", i);
813 }
814}
Russ Cox92e92572009-07-10 16:32:26 -0700815
816func TestInterfaceSet(t *testing.T) {
817 p := &Point{3, 4};
818
819 var s struct {
820 I interface {};
821 P interface { Dist(int)int };
822 }
823 sv := NewValue(&s).(*PtrValue).Elem().(*StructValue);
824 sv.Field(0).(*InterfaceValue).Set(NewValue(p));
825 if q := s.I.(*Point); q != p {
826 t.Errorf("i: have %p want %p", q, p);
827 }
828
829 pv := sv.Field(1).(*InterfaceValue);
830 pv.Set(NewValue(p));
831 if q := s.P.(*Point); q != p {
832 t.Errorf("i: have %p want %p", q, p);
833 }
Russ Cox3b864e42009-08-12 13:18:37 -0700834
Russ Cox92e92572009-07-10 16:32:26 -0700835 i := pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get();
836 if i != 250 {
837 t.Errorf("Interface Method returned %d; want 250", i);
838 }
839}
Robert Griesemera2880952009-08-05 15:56:44 -0700840
841type T1 struct { a string; int; }
842
843func TestAnonymousFields(t *testing.T) {
844 var field StructField;
845 var ok bool;
846 var t1 T1;
847 type1 := Typeof(t1).(*StructType);
848 if field, ok = type1.FieldByName("int"); !ok {
849 t.Error("no field 'int'");
850 }
851 if field.Index[0] != 1 {
852 t.Error("field index should be 1; is", field.Index);
853 }
854}
855
856type FTest struct {
857 s interface{};
858 name string;
859 index []int;
860 value int;
861}
862
Russ Cox92543da2009-08-24 17:04:12 -0700863type D1 struct {
864 d int;
865}
866type D2 struct {
867 d int;
868}
869
Robert Griesemera2880952009-08-05 15:56:44 -0700870type S0 struct {
Russ Cox92543da2009-08-24 17:04:12 -0700871 a, b, c int;
872 D1;
873 D2;
Robert Griesemera2880952009-08-05 15:56:44 -0700874}
875
876type S1 struct {
877 b int;
878 S0;
879}
880
881type S2 struct {
882 a int;
883 *S1;
884}
885
Russ Cox92543da2009-08-24 17:04:12 -0700886type S1x struct {
Robert Griesemera2880952009-08-05 15:56:44 -0700887 S1;
Russ Cox92543da2009-08-24 17:04:12 -0700888}
889
890type S1y struct {
891 S1;
892}
893
894type S3 struct {
895 S1x;
Robert Griesemera2880952009-08-05 15:56:44 -0700896 S2;
897 d, e int;
Russ Cox92543da2009-08-24 17:04:12 -0700898 *S1y;
Robert Griesemera2880952009-08-05 15:56:44 -0700899}
900
901type S4 struct {
902 *S4;
903 a int;
904}
905
906var fieldTests = []FTest {
907 FTest{ struct{ }{}, "", nil, 0 },
908 FTest{ struct{ }{}, "foo", nil, 0 },
909 FTest{ S0{a: 'a'}, "a", []int{0}, 'a' },
910 FTest{ S0{}, "d", nil, 0 },
911 FTest{ S1{S0: S0{a: 'a'}}, "a", []int{1, 0}, 'a' },
912 FTest{ S1{b: 'b'}, "b", []int{0}, 'b' },
913 FTest{ S1{}, "S0", []int{1}, 0 },
914 FTest{ S1{S0: S0{c: 'c'}}, "c", []int{1, 2}, 'c' },
915 FTest{ S2{a: 'a'}, "a", []int{0}, 'a' },
916 FTest{ S2{}, "S1", []int{1}, 0 },
917 FTest{ S2{S1: &S1{b: 'b'}}, "b", []int{1, 0}, 'b' },
918 FTest{ S2{S1: &S1{S0: S0{c: 'c'}}}, "c", []int{1, 1, 2}, 'c' },
919 FTest{ S2{}, "d", nil, 0 },
920 FTest{ S3{}, "S1", nil, 0 },
921 FTest{ S3{S2: S2{a: 'a'}}, "a", []int{1, 0}, 'a' },
922 FTest{ S3{}, "b", nil, 0 },
923 FTest{ S3{d: 'd'}, "d", []int{2}, 0 },
924 FTest{ S3{e: 'e'}, "e", []int{3}, 'e' },
925 FTest{ S4{a: 'a'}, "a", []int{1}, 'a' },
926 FTest{ S4{}, "b", nil, 0 },
927}
928
929func TestFieldByIndex(t *testing.T) {
930 for _, test := range fieldTests {
931 s := Typeof(test.s).(*StructType);
932 f := s.FieldByIndex(test.index);
933 if f.Name != "" {
934 if test.index != nil {
935 if f.Name != test.name {
936 t.Errorf("%s.%s found; want %s", s.Name(), f.Name, test.name);
937 }
938 } else {
939 t.Errorf("%s.%s found", s.Name(), f.Name);
940 }
941 } else if len(test.index) > 0 {
942 t.Errorf("%s.%s not found", s.Name(), test.name);
943 }
944
945 if test.value != 0 {
Russ Cox3b864e42009-08-12 13:18:37 -0700946 v := NewValue(test.s).(*StructValue).FieldByIndex(test.index);
Robert Griesemera2880952009-08-05 15:56:44 -0700947 if v != nil {
948 if x, ok := v.Interface().(int); ok {
949 if x != test.value {
950 t.Errorf("%s%v is %d; want %d", s.Name(), test.index, x, test.value);
951 }
952 } else {
953 t.Errorf("%s%v value not an int", s.Name(), test.index);
954 }
955 } else {
956 t.Errorf("%s%v value not found", s.Name(), test.index);
957 }
958 }
959 }
960}
961
962func TestFieldByName(t *testing.T) {
963 for _, test := range fieldTests {
964 s := Typeof(test.s).(*StructType);
965 f, found := s.FieldByName(test.name);
966 if found {
967 if test.index != nil {
968 // Verify field depth and index.
969 if len(f.Index) != len(test.index) {
970 t.Errorf("%s.%s depth %d; want %d", s.Name(), test.name, len(f.Index), len(test.index));
971 } else {
972 for i, x := range f.Index {
973 if x != test.index[i] {
974 t.Errorf("%s.%s.Index[%d] is %d; want %d", s.Name(), test.name, i, x, test.index[i]);
975 }
976 }
977 }
978 } else {
979 t.Errorf("%s.%s found", s.Name(), f.Name);
980 }
981 } else if len(test.index) > 0 {
982 t.Errorf("%s.%s not found", s.Name(), test.name);
983 }
Russ Cox3b864e42009-08-12 13:18:37 -0700984
Robert Griesemera2880952009-08-05 15:56:44 -0700985 if test.value != 0 {
Russ Cox3b864e42009-08-12 13:18:37 -0700986 v := NewValue(test.s).(*StructValue).FieldByName(test.name);
Robert Griesemera2880952009-08-05 15:56:44 -0700987 if v != nil {
988 if x, ok := v.Interface().(int); ok {
989 if x != test.value {
990 t.Errorf("%s.%s is %d; want %d", s.Name(), test.name, x, test.value);
991 }
992 } else {
993 t.Errorf("%s.%s value not an int", s.Name(), test.name);
994 }
995 } else {
996 t.Errorf("%s.%s value not found", s.Name(), test.name);
997 }
998 }
999 }
1000}