blob: 52e85f411f570b654a4d0e336a1d17c76f97d8da [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
Rob Pike12254b62008-11-19 19:11:01 -08005package reflect
Rob Pike536b1f22008-10-23 17:13:34 -07006
7import (
Russ Cox96cfd152009-05-21 11:50:20 -07008 "io";
9 "os";
Rob Pike12254b62008-11-19 19:11:01 -080010 "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" },
53 pair { struct { x (**reflect.integer) }{}, "**reflect.integer" },
54 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)" },
60 pair { struct { x struct {c func(chan *reflect.integer, *int8)} }{}, "struct { c func(chan *reflect.integer, *int8) }" },
61 pair { struct { x struct {a int8; b int32} }{}, "struct { a int8; b int32 }" },
62 pair { struct { x struct {a int8; b int8; b int32} }{}, "struct { a int8; b int8; b int32 }" },
63 pair { struct { x struct {a int8; b int8; c int8; b int32} }{}, "struct { a int8; b int8; c int8; b int32 }" },
64 pair { struct { x struct {a int8; b int8; c int8; d int8; b int32} }{}, "struct { a int8; b int8; c int8; d int8; b int32 }" },
65 pair { struct { x struct {a int8; b int8; c int8; d int8; e int8; b int32} }{}, "struct { a int8; b int8; c int8; d int8; e int8; b int32 }" },
66 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}" },
88 pair { (**reflect.integer)(nil), "**reflect.integer(0)" },
89 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)" },
93 pair { (struct {c func(chan *reflect.integer, *int8)}){}, "struct { c func(chan *reflect.integer, *int8) }{func(chan *reflect.integer, *int8)(0)}" },
94 pair { (struct {a int8; b int32}){}, "struct { a int8; b int32 }{0, 0}" },
95 pair { (struct {a int8; b int8; b int32}){}, "struct { a int8; b int8; b int32 }{0, 0, 0}" },
96}
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) {
115 case *reflect.IntValue:
116 v.Set(132);
117 case *reflect.Int8Value:
118 v.Set(8);
119 case *reflect.Int16Value:
120 v.Set(16);
121 case *reflect.Int32Value:
122 v.Set(32);
123 case *reflect.Int64Value:
124 v.Set(64);
125 case *reflect.UintValue:
126 v.Set(132);
127 case *reflect.Uint8Value:
128 v.Set(8);
129 case *reflect.Uint16Value:
130 v.Set(16);
131 case *reflect.Uint32Value:
132 v.Set(32);
133 case *reflect.Uint64Value:
134 v.Set(64);
135 case *reflect.FloatValue:
136 v.Set(3200.0);
137 case *reflect.Float32Value:
138 v.Set(32.1);
139 case *reflect.Float64Value:
140 v.Set(64.2);
141 case *reflect.StringValue:
142 v.Set("stringy cheese");
143 case *reflect.BoolValue:
144 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" },
160 pair { T{123, 456.75, "hello", &_i}, "reflect.T{123, 456.75, hello, *int(&7)}" },
161 pair { new(chan *T), "*chan *reflect.T(&chan *reflect.T)" },
162 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");
217 f = styp.Field(1);
218 testType(t, 6, f.Type, "float32");
Rob Pikea45f9472008-11-04 22:54:11 -0800219
Russ Cox64f4e0b2009-07-07 11:03:12 -0700220 typ = Typeof(([32]int32)(nil));
221 testType(t, 7, typ, "[32]int32");
222 testType(t, 8, typ.(*ArrayType).Elem(), "int32");
Rob Pikea45f9472008-11-04 22:54:11 -0800223
Russ Cox64f4e0b2009-07-07 11:03:12 -0700224 typ = Typeof((map[string]*int32)(nil));
225 testType(t, 9, typ, "map[string] *int32");
226 mtyp := typ.(*MapType);
227 testType(t, 10, mtyp.Key(), "string");
228 testType(t, 11, mtyp.Elem(), "*int32");
Rob Pikebdbb9582008-11-05 08:17:01 -0800229
Russ Cox64f4e0b2009-07-07 11:03:12 -0700230 typ = Typeof((chan<-string)(nil));
231 testType(t, 12, typ, "chan<- string");
232 testType(t, 13, typ.(*ChanType).Elem(), "string");
Rob Pike5a1cbe82008-11-05 13:01:33 -0800233
234 // make sure tag strings are not part of element type
Russ Cox64f4e0b2009-07-07 11:03:12 -0700235 typ = Typeof(struct{d []uint32 "TAG"}{}).(*StructType).Field(0).Type;
236 testType(t, 14, typ, "[]uint32");
Rob Pike536b1f22008-10-23 17:13:34 -0700237}
Russ Cox387df5e2008-11-24 14:51:33 -0800238
Russ Cox839a6842009-01-20 14:40:40 -0800239func TestInterfaceGet(t *testing.T) {
Russ Cox387df5e2008-11-24 14:51:33 -0800240 var inter struct { e interface{ } };
241 inter.e = 123.456;
Russ Cox96cfd152009-05-21 11:50:20 -0700242 v1 := NewValue(&inter);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700243 v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0);
244 assert(t, v2.Type().String(), "interface { }");
245 i2 := v2.(*InterfaceValue).Interface();
Russ Cox96cfd152009-05-21 11:50:20 -0700246 v3 := NewValue(i2);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700247 assert(t, v3.Type().String(), "float");
Russ Cox387df5e2008-11-24 14:51:33 -0800248}
Russ Coxd0e30cd2008-12-10 15:55:59 -0800249
Russ Coxac6ebfd2009-04-06 21:28:04 -0700250func TestInterfaceValue(t *testing.T) {
251 var inter struct { e interface{ } };
252 inter.e = 123.456;
Russ Cox96cfd152009-05-21 11:50:20 -0700253 v1 := NewValue(&inter);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700254 v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0);
255 assert(t, v2.Type().String(), "interface { }");
256 v3 := v2.(*InterfaceValue).Elem();
257 assert(t, v3.Type().String(), "float");
Russ Cox64627b02009-04-15 00:55:58 -0700258
Russ Cox4b8c13d2009-04-14 19:03:57 -0700259 i3 := v2.Interface();
260 if f, ok := i3.(float); !ok {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700261 t.Error("v2.Interface() did not return float, got ", Typeof(i3));
Russ Cox4b8c13d2009-04-14 19:03:57 -0700262 }
Russ Coxac6ebfd2009-04-06 21:28:04 -0700263}
264
Ian Lance Taylorca9765d2009-04-14 06:46:01 -0700265func TestFunctionValue(t *testing.T) {
Russ Cox96cfd152009-05-21 11:50:20 -0700266 v := NewValue(func() {});
Ian Lance Taylorca9765d2009-04-14 06:46:01 -0700267 if v.Interface() != v.Interface() {
268 t.Fatalf("TestFunction != itself");
269 }
Russ Cox64f4e0b2009-07-07 11:03:12 -0700270 assert(t, v.Type().String(), "func()");
Ian Lance Taylorca9765d2009-04-14 06:46:01 -0700271}
272
Russ Cox839a6842009-01-20 14:40:40 -0800273func TestCopyArray(t *testing.T) {
Russ Coxbe2edb52009-03-03 08:39:12 -0800274 a := []int{ 1, 2, 3, 4, 10, 9, 8, 7 };
275 b := []int{ 11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44 };
276 c := []int{ 11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44 };
Russ Coxd47d8882008-12-18 22:37:22 -0800277 va := NewValue(&a);
278 vb := NewValue(&b);
Russ Coxd0e30cd2008-12-10 15:55:59 -0800279 for i := 0; i < len(b); i++ {
280 if b[i] != c[i] {
281 t.Fatalf("b != c before test");
282 }
283 }
Russ Cox64f4e0b2009-07-07 11:03:12 -0700284 aa := va.(*PtrValue).Elem().(*SliceValue);
285 ab := vb.(*PtrValue).Elem().(*SliceValue);
Russ Coxd47d8882008-12-18 22:37:22 -0800286 for tocopy := 1; tocopy <= 7; tocopy++ {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700287 aa.SetLen(tocopy);
288 ArrayCopy(ab, aa);
289 aa.SetLen(8);
Russ Coxd0e30cd2008-12-10 15:55:59 -0800290 for i := 0; i < tocopy; i++ {
291 if a[i] != b[i] {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700292 t.Errorf("(i) tocopy=%d a[%d]=%d, b[%d]=%d",
Russ Coxd0e30cd2008-12-10 15:55:59 -0800293 tocopy, i, a[i], i, b[i]);
294 }
295 }
296 for i := tocopy; i < len(b); i++ {
297 if b[i] != c[i] {
298 if i < len(a) {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700299 t.Errorf("(ii) tocopy=%d a[%d]=%d, b[%d]=%d, c[%d]=%d",
Russ Coxd0e30cd2008-12-10 15:55:59 -0800300 tocopy, i, a[i], i, b[i], i, c[i]);
301 } else {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700302 t.Errorf("(iii) tocopy=%d b[%d]=%d, c[%d]=%d",
Russ Coxd0e30cd2008-12-10 15:55:59 -0800303 tocopy, i, b[i], i, c[i]);
304 }
Russ Coxd47d8882008-12-18 22:37:22 -0800305 } else {
306 t.Logf("tocopy=%d elem %d is okay\n", tocopy, i);
Russ Coxd0e30cd2008-12-10 15:55:59 -0800307 }
308 }
309 }
310}
Russ Cox484ba932009-01-09 00:17:46 -0800311
Russ Cox839a6842009-01-20 14:40:40 -0800312func TestBigUnnamedStruct(t *testing.T) {
Russ Coxbe2edb52009-03-03 08:39:12 -0800313 b := struct{a,b,c,d int64}{1, 2, 3, 4};
Russ Cox484ba932009-01-09 00:17:46 -0800314 v := NewValue(b);
315 b1 := v.Interface().(struct{a,b,c,d int64});
316 if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700317 t.Errorf("NewValue(%v).Interface().(*Big) = %v", b, b1);
Russ Cox484ba932009-01-09 00:17:46 -0800318 }
319}
320
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800321type big struct {
Russ Cox484ba932009-01-09 00:17:46 -0800322 a, b, c, d, e int64
323}
Russ Cox839a6842009-01-20 14:40:40 -0800324func TestBigStruct(t *testing.T) {
Russ Coxbe2edb52009-03-03 08:39:12 -0800325 b := big{1, 2, 3, 4, 5};
Russ Cox484ba932009-01-09 00:17:46 -0800326 v := NewValue(b);
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800327 b1 := v.Interface().(big);
Russ Cox484ba932009-01-09 00:17:46 -0800328 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 -0800329 t.Errorf("NewValue(%v).Interface().(big) = %v", b, b1);
Russ Cox484ba932009-01-09 00:17:46 -0800330 }
331}
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700332
333type Basic struct {
334 x int;
335 y float32
336}
337
Russ Cox64627b02009-04-15 00:55:58 -0700338type NotBasic Basic
339
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700340type DeepEqualTest struct {
341 a, b interface{};
342 eq bool;
343}
344
345var deepEqualTests = []DeepEqualTest {
346 // Equalities
347 DeepEqualTest{ 1, 1, true },
348 DeepEqualTest{ int32(1), int32(1), true },
349 DeepEqualTest{ 0.5, 0.5, true },
350 DeepEqualTest{ float32(0.5), float32(0.5), true },
351 DeepEqualTest{ "hello", "hello", true },
352 DeepEqualTest{ make([]int, 10), make([]int, 10), true },
353 DeepEqualTest{ &[3]int{ 1, 2, 3 }, &[3]int{ 1, 2, 3 }, true },
354 DeepEqualTest{ Basic{ 1, 0.5 }, Basic{ 1, 0.5 }, true },
Russ Cox48662232009-06-25 14:25:38 -0700355 DeepEqualTest{ os.Error(nil), os.Error(nil), true },
Rob Pike1880b902009-07-10 11:20:10 -0700356 DeepEqualTest{ map[int]string{ 1:"one", 2:"two" }, map[int]string{ 2:"two", 1:"one" }, true },
Russ Coxa439f662009-07-01 16:45:09 -0700357
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700358 // Inequalities
359 DeepEqualTest{ 1, 2, false },
360 DeepEqualTest{ int32(1), int32(2), false },
361 DeepEqualTest{ 0.5, 0.6, false },
362 DeepEqualTest{ float32(0.5), float32(0.6), false },
363 DeepEqualTest{ "hello", "hey", false },
364 DeepEqualTest{ make([]int, 10), make([]int, 11), false },
365 DeepEqualTest{ &[3]int{ 1, 2, 3 }, &[3]int{ 1, 2, 4 }, false },
366 DeepEqualTest{ Basic{ 1, 0.5 }, Basic{ 1, 0.6 }, false },
Russ Coxa439f662009-07-01 16:45:09 -0700367 DeepEqualTest{ Basic{ 1, 0 }, Basic{ 2, 0 }, false },
Rob Pike1880b902009-07-10 11:20:10 -0700368 DeepEqualTest{ map[int]string{ 1:"one", 3:"two" }, map[int]string{ 2:"two", 1:"one" }, false },
369 DeepEqualTest{ map[int]string{ 1:"one", 2:"txo" }, map[int]string{ 2:"two", 1:"one" }, false },
370 DeepEqualTest{ map[int]string{ 1:"one", }, map[int]string{ 2:"two", 1:"one" }, false },
371 DeepEqualTest{ map[int]string{ 2:"two", 1:"one" }, map[int]string{ 1:"one", }, false },
Russ Coxa439f662009-07-01 16:45:09 -0700372
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700373 // Mismatched types
374 DeepEqualTest{ 1, 1.0, false },
375 DeepEqualTest{ int32(1), int64(1), false },
376 DeepEqualTest{ 0.5, "hello", false },
377 DeepEqualTest{ []int{ 1, 2, 3 }, [3]int{ 1, 2, 3 }, false },
378 DeepEqualTest{ &[3]interface{} { 1, 2, 4 }, &[3]interface{} { 1, 2, "s" }, false },
Russ Cox64627b02009-04-15 00:55:58 -0700379 DeepEqualTest{ Basic{ 1, 0.5 }, NotBasic{ 1, 0.5 }, false },
Rob Pike1880b902009-07-10 11:20:10 -0700380 DeepEqualTest{ map[uint]string{ 1:"one", 2:"two" }, map[int]string{ 2:"two", 1:"one" }, false },
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700381}
382
383func TestDeepEqual(t *testing.T) {
384 for i, test := range deepEqualTests {
385 if r := DeepEqual(test.a, test.b); r != test.eq {
386 t.Errorf("DeepEqual(%v, %v) = %v, want %v", test.a, test.b, r, test.eq);
387 }
388 }
389}
390
Russ Cox48662232009-06-25 14:25:38 -0700391func TestTypeof(t *testing.T) {
392 for i, test := range deepEqualTests {
393 v := NewValue(test.a);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700394 if v == nil {
395 continue;
396 }
Russ Cox48662232009-06-25 14:25:38 -0700397 typ := Typeof(test.a);
398 if typ != v.Type() {
399 t.Errorf("Typeof(%v) = %v, but NewValue(%v).Type() = %v", test.a, typ, test.a, v.Type());
400 }
401 }
402}
403
Rob Pike1880b902009-07-10 11:20:10 -0700404type Recursive struct {
405 x int;
406 r *Recursive
407}
408
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700409func TestDeepEqualRecursiveStruct(t *testing.T) {
410 a, b := new(Recursive), new(Recursive);
411 *a = Recursive{ 12, a };
412 *b = Recursive{ 12, b };
413 if !DeepEqual(a, b) {
414 t.Error("DeepEqual(recursive same) = false, want true");
415 }
416}
417
Rob Pike1880b902009-07-10 11:20:10 -0700418type Complex struct {
419 a int;
420 b [3]*Complex;
421 c *string;
422 d map[float]float
423}
424
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700425func TestDeepEqualComplexStruct(t *testing.T) {
426 m := make(map[float]float);
427 stra, strb := "hello", "hello";
428 a, b := new(Complex), new(Complex);
429 *a = Complex{5, [3]*Complex{a, b, a}, &stra, m};
430 *b = Complex{5, [3]*Complex{b, a, a}, &strb, m};
431 if !DeepEqual(a, b) {
432 t.Error("DeepEqual(complex same) = false, want true");
433 }
434}
435
436func TestDeepEqualComplexStructInequality(t *testing.T) {
437 m := make(map[float]float);
438 stra, strb := "hello", "helloo"; // Difference is here
439 a, b := new(Complex), new(Complex);
440 *a = Complex{5, [3]*Complex{a, b, a}, &stra, m};
441 *b = Complex{5, [3]*Complex{b, a, a}, &strb, m};
442 if DeepEqual(a, b) {
443 t.Error("DeepEqual(complex different) = true, want false");
444 }
445}
Rob Pike93831d22009-04-29 22:16:53 -0700446
447
448func check2ndField(x interface{}, offs uintptr, t *testing.T) {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700449 s := NewValue(x).(*StructValue);
450 f := s.Type().(*StructType).Field(1);
451 if f.Offset != offs {
452 t.Error("mismatched offsets in structure alignment:", f.Offset, offs);
Rob Pike93831d22009-04-29 22:16:53 -0700453 }
454}
455
456// Check that structure alignment & offsets viewed through reflect agree with those
457// from the compiler itself.
458func TestAlignment(t *testing.T) {
459 type T1inner struct {
460 a int
461 }
462 type T1 struct {
463 T1inner;
464 f int;
465 }
466 type T2inner struct {
467 a, b int
468 }
469 type T2 struct {
470 T2inner;
471 f int;
472 }
473
474 x := T1{T1inner{2}, 17};
475 check2ndField(x, uintptr(unsafe.Pointer(&x.f)) - uintptr(unsafe.Pointer(&x)), t);
476
477 x1 := T2{T2inner{2, 3}, 17};
478 check2ndField(x1, uintptr(unsafe.Pointer(&x1.f)) - uintptr(unsafe.Pointer(&x1)), t);
479}
Rob Pikea8f6e382009-05-12 14:57:44 -0700480
Russ Cox64f4e0b2009-07-07 11:03:12 -0700481type IsNiller interface {
Rob Pikea8f6e382009-05-12 14:57:44 -0700482 IsNil() bool
483}
484
485func Nil(a interface{}, t *testing.T) {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700486 n := NewValue(a).(*StructValue).Field(0).(IsNiller);
Rob Pikea8f6e382009-05-12 14:57:44 -0700487 if !n.IsNil() {
488 t.Errorf("%v should be nil", a)
489 }
490}
491
492func NotNil(a interface{}, t *testing.T) {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700493 n := NewValue(a).(*StructValue).Field(0).(IsNiller);
Rob Pikea8f6e382009-05-12 14:57:44 -0700494 if n.IsNil() {
495 t.Errorf("value of type %v should not be nil", NewValue(a).Type().String())
496 }
497}
498
499func TestIsNil(t *testing.T) {
500 // These do not implement IsNil
Russ Cox64f4e0b2009-07-07 11:03:12 -0700501 doNotNil := []interface{}{ int(0), float32(0), struct{a int}{} };
Rob Pikea8f6e382009-05-12 14:57:44 -0700502 for i, ts := range doNotNil {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700503 ty := Typeof(ts);
504 v := MakeZero(ty);
505 if nilable, ok := v.(IsNiller); ok {
Rob Pikea8f6e382009-05-12 14:57:44 -0700506 t.Errorf("%s is nilable; should not be", ts)
507 }
508 }
509
Russ Cox64f4e0b2009-07-07 11:03:12 -0700510 // These do implement IsNil.
511 // Wrap in extra struct to hide interface type.
512 doNil := []interface{}{
513 struct{x *int}{},
514 struct{x interface{}}{},
515 struct{x map[string]int}{},
516 struct{x func()bool}{},
517 struct{x chan int}{},
518 struct{x []string}{}
519 };
Rob Pikea8f6e382009-05-12 14:57:44 -0700520 for i, ts := range doNil {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700521 ty := Typeof(ts).(*StructType).Field(0).Type;
522 v := MakeZero(ty);
523 if nilable, ok := v.(IsNiller); !ok {
Russ Cox2d5d4a12009-05-20 15:42:14 -0700524 t.Errorf("%s %T is not nilable; should be", ts, v)
Rob Pikea8f6e382009-05-12 14:57:44 -0700525 }
526 }
Russ Cox64f4e0b2009-07-07 11:03:12 -0700527
Rob Pikea8f6e382009-05-12 14:57:44 -0700528 // Check the implementations
Russ Cox64f4e0b2009-07-07 11:03:12 -0700529 var pi struct {x *int}
Rob Pikea8f6e382009-05-12 14:57:44 -0700530 Nil(pi, t);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700531 pi.x = new(int);
Rob Pikea8f6e382009-05-12 14:57:44 -0700532 NotNil(pi, t);
533
Russ Cox64f4e0b2009-07-07 11:03:12 -0700534 var si struct {x []int}
Rob Pikea8f6e382009-05-12 14:57:44 -0700535 Nil(si, t);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700536 si.x = make([]int, 10);
Rob Pikea8f6e382009-05-12 14:57:44 -0700537 NotNil(si, t);
538
Russ Cox64f4e0b2009-07-07 11:03:12 -0700539 var ci struct {x chan int}
540 Nil(ci, t);
541 ci.x = make(chan int);
542 NotNil(ci, t);
Rob Pikea8f6e382009-05-12 14:57:44 -0700543
Russ Cox64f4e0b2009-07-07 11:03:12 -0700544 var mi struct {x map[int]int}
545 Nil(mi, t);
546 mi.x = make(map[int]int);
547 NotNil(mi, t);
548
549 var ii struct {x interface {}}
Rob Pikea8f6e382009-05-12 14:57:44 -0700550 Nil(ii, t);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700551 ii.x = 2;
Rob Pikea8f6e382009-05-12 14:57:44 -0700552 NotNil(ii, t);
553
Russ Cox64f4e0b2009-07-07 11:03:12 -0700554 var fi struct {x func(t *testing.T)}
Rob Pikea8f6e382009-05-12 14:57:44 -0700555 Nil(fi, t);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700556 fi.x = TestIsNil;
Rob Pikea8f6e382009-05-12 14:57:44 -0700557 NotNil(fi, t);
558}
Russ Cox96cfd152009-05-21 11:50:20 -0700559
560func TestInterfaceExtraction(t *testing.T) {
561 var s struct {
562 w io.Writer;
563 }
564
565 s.w = os.Stdout;
Russ Cox64f4e0b2009-07-07 11:03:12 -0700566 v := Indirect(NewValue(&s)).(*StructValue).Field(0).Interface();
Russ Cox96cfd152009-05-21 11:50:20 -0700567 if v != s.w.(interface{}) {
Rob Pikeb75df2f2009-07-16 15:01:21 -0700568 t.Error("Interface() on interface: ", v, s.w);
Russ Cox96cfd152009-05-21 11:50:20 -0700569 }
570}
Russ Coxf9666232009-05-21 14:06:43 -0700571
572func TestInterfaceEditing(t *testing.T) {
573 // strings are bigger than one word,
574 // so the interface conversion allocates
575 // memory to hold a string and puts that
576 // pointer in the interface.
577 var i interface{} = "hello";
578
579 // if i pass the interface value by value
580 // to NewValue, i should get a fresh copy
581 // of the value.
582 v := NewValue(i);
583
584 // and setting that copy to "bye" should
585 // not change the value stored in i.
Russ Cox64f4e0b2009-07-07 11:03:12 -0700586 v.(*StringValue).Set("bye");
Russ Coxf9666232009-05-21 14:06:43 -0700587 if i.(string) != "hello" {
588 t.Errorf(`Set("bye") changed i to %s`, i.(string));
589 }
590
591 // the same should be true of smaller items.
592 i = 123;
593 v = NewValue(i);
Russ Cox64f4e0b2009-07-07 11:03:12 -0700594 v.(*IntValue).Set(234);
Russ Coxf9666232009-05-21 14:06:43 -0700595 if i.(int) != 123 {
596 t.Errorf("Set(234) changed i to %d", i.(int));
597 }
598}
David Symondsd4e57ff2009-06-15 18:35:04 -0700599
600func TestNilPtrValueSub(t *testing.T) {
601 var pi *int;
Russ Cox64f4e0b2009-07-07 11:03:12 -0700602 if pv := NewValue(pi).(*PtrValue); pv.Elem() != nil {
603 t.Error("NewValue((*int)(nil)).(*PtrValue).Elem() != nil");
David Symondsd4e57ff2009-06-15 18:35:04 -0700604 }
605}
Russ Cox764b6ec2009-07-08 13:55:57 -0700606
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700607func TestMap(t *testing.T) {
Russ Cox764b6ec2009-07-08 13:55:57 -0700608 m := map[string]int{ "a": 1, "b": 2 };
609 mv := NewValue(m).(*MapValue);
610 if n := mv.Len(); n != len(m) {
611 t.Errorf("Len = %d, want %d", n, len(m));
612 }
613 keys := mv.Keys();
614 i := 0;
615 newmap := MakeMap(mv.Type().(*MapType));
616 for k, v := range m {
617 // Check that returned Keys match keys in range.
618 // These aren't required to be in the same order,
619 // but they are in this implementation, which makes
620 // the test easier.
621 if i >= len(keys) {
622 t.Errorf("Missing key #%d %q", i, k);
623 } else if kv := keys[i].(*StringValue); kv.Get() != k {
624 t.Errorf("Keys[%d] = %q, want %q", i, kv.Get(), k);
625 }
626 i++;
627
628 // Check that value lookup is correct.
Russ Cox92e92572009-07-10 16:32:26 -0700629 vv := mv.Elem(NewValue(k));
Russ Cox764b6ec2009-07-08 13:55:57 -0700630 if vi := vv.(*IntValue).Get(); vi != v {
631 t.Errorf("Key %q: have value %d, want %d", vi, v);
632 }
633
634 // Copy into new map.
Russ Cox92e92572009-07-10 16:32:26 -0700635 newmap.SetElem(NewValue(k), NewValue(v));
Russ Cox764b6ec2009-07-08 13:55:57 -0700636 }
Russ Cox92e92572009-07-10 16:32:26 -0700637 vv := mv.Elem(NewValue("not-present"));
Russ Cox764b6ec2009-07-08 13:55:57 -0700638 if vv != nil {
639 t.Errorf("Invalid key: got non-nil value %s", valueToString(vv));
640 }
641
642 newm := newmap.Interface().(map[string]int);
643 if len(newm) != len(m) {
644 t.Errorf("length after copy: newm=%d, m=%d", newm, m);
645 }
646
647 for k, v := range newm {
648 mv, ok := m[k];
649 if mv != v {
650 t.Errorf("newm[%q] = %d, but m[%q] = %d, %v", k, v, k, mv, ok);
651 }
652 }
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700653
Russ Cox92e92572009-07-10 16:32:26 -0700654 newmap.SetElem(NewValue("a"), nil);
Russ Cox764b6ec2009-07-08 13:55:57 -0700655 v, ok := newm["a"];
656 if ok {
657 t.Errorf("newm[\"a\"] = %d after delete", v);
658 }
659}
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700660
661func TestChan(t *testing.T) {
662 for loop := 0; loop < 2; loop++ {
663 var c chan int;
664 var cv *ChanValue;
665
666 // check both ways to allocate channels
667 switch loop {
668 case 1:
669 c = make(chan int, 1);
670 cv = NewValue(c).(*ChanValue);
671 case 0:
672 cv = MakeChan(Typeof(c).(*ChanType), 1);
673 c = cv.Interface().(chan int);
674 }
675
676 // Send
677 cv.Send(NewValue(2));
678 if i := <-c; i != 2 {
679 t.Errorf("reflect Send 2, native recv %d", i);
680 }
681
682 // Recv
683 c <- 3;
684 if i := cv.Recv().(*IntValue).Get(); i != 3 {
685 t.Errorf("native send 3, reflect Recv %d", i);
686 }
687
688 // TryRecv fail
689 val := cv.TryRecv();
690 if val != nil {
691 t.Errorf("TryRecv on empty chan: %s", valueToString(val));
692 }
693
694 // TryRecv success
695 c <- 4;
696 val = cv.TryRecv();
697 if val == nil {
698 t.Errorf("TryRecv on ready chan got nil");
699 } else if i := val.(*IntValue).Get(); i != 4 {
700 t.Errorf("native send 4, TryRecv %d", i);
701 }
702
703 // TrySend fail
704 c <- 100;
705 ok := cv.TrySend(NewValue(5));
706 i := <-c;
707 if ok {
708 t.Errorf("TrySend on full chan succeeded: value %d", i);
709 }
710
711 // TrySend success
712 ok = cv.TrySend(NewValue(6));
713 if !ok {
714 t.Errorf("TrySend on empty chan failed");
715 } else {
716 if i = <-c; i != 6 {
717 t.Errorf("TrySend 6, recv %d", i);
718 }
719 }
720 }
721
722 // check creation of unbuffered channel
723 var c chan int;
724 cv := MakeChan(Typeof(c).(*ChanType), 0);
725 c = cv.Interface().(chan int);
726 if cv.TrySend(NewValue(7)) {
727 t.Errorf("TrySend on sync chan succeeded");
728 }
729 if cv.TryRecv() != nil {
730 t.Errorf("TryRecv on sync chan succeeded");
731 }
732}
733
Russ Coxbba278a2009-07-08 18:16:09 -0700734// Difficult test for function call because of
735// implicit padding between arguments.
736func dummy(b byte, c int, d byte) (i byte, j int, k byte){
737 return b, c, d;
738}
739
740func TestFunc(t *testing.T) {
741 ret := NewValue(dummy).(*FuncValue).Call([]Value{NewValue(byte(10)), NewValue(20), NewValue(byte(30))});
742 if len(ret) != 3 {
743 t.Fatalf("Call returned %d values, want 3", len(ret));
744 }
745
746 i := ret[0].(*Uint8Value).Get();
747 j := ret[1].(*IntValue).Get();
748 k := ret[2].(*Uint8Value).Get();
749 if i != 10 || j != 20 || k != 30 {
750 t.Errorf("Call returned %d, %d, %d; want 10, 20, 30", i, j, k);
751 }
752}
Russ Cox12ebbe72009-07-09 17:27:49 -0700753
754type Point struct {
755 x, y int;
756}
757
758func (p Point) Dist(scale int) int {
759 return p.x*p.x*scale + p.y*p.y*scale;
760}
761
762func TestMethod(t *testing.T) {
763 // Non-curried method of type.
764 p := Point{3, 4};
765 i := reflect.Typeof(p).Method(0).Func.Call([]Value{NewValue(p), NewValue(10)})[0].(*IntValue).Get();
766 if i != 250 {
767 t.Errorf("Type Method returned %d; want 250", i);
768 }
769
770 // Curried method of value.
771 i = NewValue(p).Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get();
772 if i != 250 {
773 t.Errorf("Value Method returned %d; want 250", i);
774 }
775
776 // Curried method of interface value.
777 // Have to wrap interface value in a struct to get at it.
778 // Passing it to NewValue directly would
779 // access the underlying Point, not the interface.
780 var s = struct{x interface{Dist(int) int}}{p};
781 pv := NewValue(s).(*StructValue).Field(0);
782 i = pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get();
783 if i != 250 {
784 t.Errorf("Interface Method returned %d; want 250", i);
785 }
786}
Russ Cox92e92572009-07-10 16:32:26 -0700787
788func TestInterfaceSet(t *testing.T) {
789 p := &Point{3, 4};
790
791 var s struct {
792 I interface {};
793 P interface { Dist(int)int };
794 }
795 sv := NewValue(&s).(*PtrValue).Elem().(*StructValue);
796 sv.Field(0).(*InterfaceValue).Set(NewValue(p));
797 if q := s.I.(*Point); q != p {
798 t.Errorf("i: have %p want %p", q, p);
799 }
800
801 pv := sv.Field(1).(*InterfaceValue);
802 pv.Set(NewValue(p));
803 if q := s.P.(*Point); q != p {
804 t.Errorf("i: have %p want %p", q, p);
805 }
806
807 i := pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get();
808 if i != 250 {
809 t.Errorf("Interface Method returned %d; want 250", i);
810 }
811}