blob: f616adaac3600fad3e709e4390ebe59609964d7d [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 Cox1cecac82010-01-24 23:33:59 -08008 "container/vector"
Russ Cox6672b402010-06-14 11:23:11 -07009 "fmt"
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080010 "io"
11 "os"
12 . "reflect"
13 "testing"
14 "unsafe"
Rob Pike536b1f22008-10-23 17:13:34 -070015)
16
Russ Cox64f4e0b2009-07-07 11:03:12 -070017type integer int
Robert Griesemer77334b982009-11-05 14:23:20 -080018type T struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080019 a int
20 b float64
21 c string
22 d *int
Robert Griesemer77334b982009-11-05 14:23:20 -080023}
Rob Pike536b1f22008-10-23 17:13:34 -070024
Russ Cox64f4e0b2009-07-07 11:03:12 -070025type pair struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080026 i interface{}
27 s string
Russ Cox64f4e0b2009-07-07 11:03:12 -070028}
29
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080030func isDigit(c uint8) bool { return '0' <= c && c <= '9' }
Rob Pike536b1f22008-10-23 17:13:34 -070031
Russ Cox64f4e0b2009-07-07 11:03:12 -070032func assert(t *testing.T, s, want string) {
33 if s != want {
Robert Griesemer40621d52009-11-09 12:07:39 -080034 t.Errorf("have %#q want %#q", s, want)
Russ Cox64f4e0b2009-07-07 11:03:12 -070035 }
36}
37
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080038func typestring(i interface{}) string { return Typeof(i).String() }
Russ Cox64f4e0b2009-07-07 11:03:12 -070039
Robert Griesemer77334b982009-11-05 14:23:20 -080040var typeTests = []pair{
Robert Griesemer34788912010-10-22 10:06:33 -070041 {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"},
Robert Griesemer34788912010-10-22 10:06:33 -070051 {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"},
60 {struct {
Robert Griesemer77334b982009-11-05 14:23:20 -080061 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080062 c chan *int32
63 d float32
64 }
Robert Griesemer7151d232009-11-05 18:27:30 -080065 }{},
66 "struct { c chan *int32; d float32 }",
67 },
Robert Griesemer34788912010-10-22 10:06:33 -070068 {struct{ x (func(a int8, b int32)) }{}, "func(int8, int32)"},
69 {struct {
Robert Griesemer77334b982009-11-05 14:23:20 -080070 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080071 c func(chan *integer, *int8)
72 }
Robert Griesemer7151d232009-11-05 18:27:30 -080073 }{},
74 "struct { c func(chan *reflect_test.integer, *int8) }",
75 },
Robert Griesemer34788912010-10-22 10:06:33 -070076 {struct {
Robert Griesemer77334b982009-11-05 14:23:20 -080077 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080078 a int8
79 b int32
80 }
Robert Griesemer7151d232009-11-05 18:27:30 -080081 }{},
82 "struct { a int8; b int32 }",
83 },
Robert Griesemer34788912010-10-22 10:06:33 -070084 {struct {
Robert Griesemer77334b982009-11-05 14:23:20 -080085 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080086 a int8
87 b int8
88 c int32
89 }
Robert Griesemer7151d232009-11-05 18:27:30 -080090 }{},
91 "struct { a int8; b int8; c int32 }",
92 },
Robert Griesemer34788912010-10-22 10:06:33 -070093 {struct {
Robert Griesemer77334b982009-11-05 14:23:20 -080094 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080095 a int8
96 b int8
97 c int8
98 d int32
99 }
Robert Griesemer7151d232009-11-05 18:27:30 -0800100 }{},
101 "struct { a int8; b int8; c int8; d int32 }",
102 },
Robert Griesemer34788912010-10-22 10:06:33 -0700103 {struct {
Robert Griesemer77334b982009-11-05 14:23:20 -0800104 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800105 a int8
106 b int8
107 c int8
108 d int8
109 e int32
110 }
Robert Griesemer7151d232009-11-05 18:27:30 -0800111 }{},
112 "struct { a int8; b int8; c int8; d int8; e int32 }",
113 },
Robert Griesemer34788912010-10-22 10:06:33 -0700114 {struct {
Robert Griesemer77334b982009-11-05 14:23:20 -0800115 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800116 a int8
117 b int8
118 c int8
119 d int8
120 e int8
121 f int32
122 }
Robert Griesemer7151d232009-11-05 18:27:30 -0800123 }{},
124 "struct { a int8; b int8; c int8; d int8; e int8; f int32 }",
125 },
Robert Griesemer34788912010-10-22 10:06:33 -0700126 {struct {
Robert Griesemer77334b982009-11-05 14:23:20 -0800127 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800128 a int8 "hi there"
129 }
Robert Griesemer7151d232009-11-05 18:27:30 -0800130 }{},
131 `struct { a int8 "hi there" }`,
132 },
Robert Griesemer34788912010-10-22 10:06:33 -0700133 {struct {
Robert Griesemer77334b982009-11-05 14:23:20 -0800134 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800135 a int8 "hi \x00there\t\n\"\\"
136 }
Robert Griesemer7151d232009-11-05 18:27:30 -0800137 }{},
138 `struct { a int8 "hi \x00there\t\n\"\\" }`,
139 },
Robert Griesemer34788912010-10-22 10:06:33 -0700140 {struct {
Robert Griesemer77334b982009-11-05 14:23:20 -0800141 x struct {
Russ Cox6672b402010-06-14 11:23:11 -0700142 f func(args ...int)
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800143 }
Robert Griesemer7151d232009-11-05 18:27:30 -0800144 }{},
Russ Cox6672b402010-06-14 11:23:11 -0700145 "struct { f func(...int) }",
Robert Griesemer7151d232009-11-05 18:27:30 -0800146 },
Robert Griesemer34788912010-10-22 10:06:33 -0700147 {struct {
Robert Griesemer77334b982009-11-05 14:23:20 -0800148 x (interface {
Robert Griesemer1be05bb2010-02-24 13:24:37 -0800149 a(func(func(int) int) func(func(int)) int)
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800150 b()
151 })
Robert Griesemer7151d232009-11-05 18:27:30 -0800152 }{},
Russ Cox7abb4b32010-01-26 10:40:28 -0800153 "interface { a(func(func(int) int) func(func(int)) int); b() }",
Robert Griesemer7151d232009-11-05 18:27:30 -0800154 },
Russ Cox64f4e0b2009-07-07 11:03:12 -0700155}
156
Robert Griesemer77334b982009-11-05 14:23:20 -0800157var valueTests = []pair{
Robert Griesemer34788912010-10-22 10:06:33 -0700158 {(int8)(0), "8"},
159 {(int16)(0), "16"},
160 {(int32)(0), "32"},
161 {(int64)(0), "64"},
162 {(uint8)(0), "8"},
163 {(uint16)(0), "16"},
164 {(uint32)(0), "32"},
165 {(uint64)(0), "64"},
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"},
176 {struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800177 c chan *int32
178 d float32
Russ Coxc6cb3032010-08-03 00:53:32 -0700179 }{},
Robert Griesemer7151d232009-11-05 18:27:30 -0800180 "struct { c chan *int32; d float32 }{chan *int32, 0}",
181 },
Robert Griesemer34788912010-10-22 10:06:33 -0700182 {(func(a int8, b int32))(nil), "func(int8, int32)(0)"},
183 {struct{ c func(chan *integer, *int8) }{},
Robert Griesemer7151d232009-11-05 18:27:30 -0800184 "struct { c func(chan *reflect_test.integer, *int8) }{func(chan *reflect_test.integer, *int8)(0)}",
185 },
Robert Griesemer34788912010-10-22 10:06:33 -0700186 {struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800187 a int8
188 b int32
Russ Coxc6cb3032010-08-03 00:53:32 -0700189 }{},
Robert Griesemer7151d232009-11-05 18:27:30 -0800190 "struct { a int8; b int32 }{0, 0}",
191 },
Robert Griesemer34788912010-10-22 10:06:33 -0700192 {struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800193 a int8
194 b int8
195 c int32
Russ Coxc6cb3032010-08-03 00:53:32 -0700196 }{},
Robert Griesemer7151d232009-11-05 18:27:30 -0800197 "struct { a int8; b int8; c int32 }{0, 0, 0}",
198 },
Russ Cox64f4e0b2009-07-07 11:03:12 -0700199}
200
201func testType(t *testing.T, i int, typ Type, want string) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800202 s := typ.String()
Russ Cox64f4e0b2009-07-07 11:03:12 -0700203 if s != want {
Robert Griesemer40621d52009-11-09 12:07:39 -0800204 t.Errorf("#%d: have %#q, want %#q", i, s, want)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700205 }
206}
207
208func TestTypes(t *testing.T) {
209 for i, tt := range typeTests {
Robert Griesemer40621d52009-11-09 12:07:39 -0800210 testType(t, i, NewValue(tt.i).(*StructValue).Field(0).Type(), tt.s)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700211 }
212}
213
Adam Langleya8a678f2009-10-21 19:51:27 -0700214func TestSet(t *testing.T) {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700215 for i, tt := range valueTests {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800216 v := NewValue(tt.i)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700217 switch v := v.(type) {
Russ Cox3b864e42009-08-12 13:18:37 -0700218 case *IntValue:
Russ Cox45bdf032010-06-20 12:16:25 -0700219 switch v.Type().Kind() {
220 case Int:
221 v.Set(132)
222 case Int8:
223 v.Set(8)
224 case Int16:
225 v.Set(16)
226 case Int32:
227 v.Set(32)
228 case Int64:
229 v.Set(64)
230 }
Russ Cox3b864e42009-08-12 13:18:37 -0700231 case *UintValue:
Russ Cox45bdf032010-06-20 12:16:25 -0700232 switch v.Type().Kind() {
233 case Uint:
234 v.Set(132)
235 case Uint8:
236 v.Set(8)
237 case Uint16:
238 v.Set(16)
239 case Uint32:
240 v.Set(32)
241 case Uint64:
242 v.Set(64)
243 }
Russ Cox3b864e42009-08-12 13:18:37 -0700244 case *FloatValue:
Russ Cox45bdf032010-06-20 12:16:25 -0700245 switch v.Type().Kind() {
Russ Cox45bdf032010-06-20 12:16:25 -0700246 case Float32:
247 v.Set(256.25)
248 case Float64:
249 v.Set(512.125)
250 }
251 case *ComplexValue:
252 switch v.Type().Kind() {
Russ Cox45bdf032010-06-20 12:16:25 -0700253 case Complex64:
254 v.Set(532.125 + 10i)
255 case Complex128:
256 v.Set(564.25 + 1i)
257 }
Russ Cox3b864e42009-08-12 13:18:37 -0700258 case *StringValue:
Robert Griesemer40621d52009-11-09 12:07:39 -0800259 v.Set("stringy cheese")
Russ Cox3b864e42009-08-12 13:18:37 -0700260 case *BoolValue:
Robert Griesemer40621d52009-11-09 12:07:39 -0800261 v.Set(true)
Rob Pike536b1f22008-10-23 17:13:34 -0700262 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800263 s := valueToString(v)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700264 if s != tt.s {
Robert Griesemer40621d52009-11-09 12:07:39 -0800265 t.Errorf("#%d: have %#q, want %#q", i, s, tt.s)
Rob Pike536b1f22008-10-23 17:13:34 -0700266 }
267 }
Rob Pike536b1f22008-10-23 17:13:34 -0700268}
269
Adam Langleya8a678f2009-10-21 19:51:27 -0700270func TestSetValue(t *testing.T) {
271 for i, tt := range valueTests {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800272 v := NewValue(tt.i)
Adam Langleya8a678f2009-10-21 19:51:27 -0700273 switch v := v.(type) {
274 case *IntValue:
Russ Cox45bdf032010-06-20 12:16:25 -0700275 switch v.Type().Kind() {
276 case Int:
277 v.SetValue(NewValue(int(132)))
278 case Int8:
279 v.SetValue(NewValue(int8(8)))
280 case Int16:
281 v.SetValue(NewValue(int16(16)))
282 case Int32:
283 v.SetValue(NewValue(int32(32)))
284 case Int64:
285 v.SetValue(NewValue(int64(64)))
286 }
Adam Langleya8a678f2009-10-21 19:51:27 -0700287 case *UintValue:
Russ Cox45bdf032010-06-20 12:16:25 -0700288 switch v.Type().Kind() {
289 case Uint:
290 v.SetValue(NewValue(uint(132)))
291 case Uint8:
292 v.SetValue(NewValue(uint8(8)))
293 case Uint16:
294 v.SetValue(NewValue(uint16(16)))
295 case Uint32:
296 v.SetValue(NewValue(uint32(32)))
297 case Uint64:
298 v.SetValue(NewValue(uint64(64)))
299 }
Adam Langleya8a678f2009-10-21 19:51:27 -0700300 case *FloatValue:
Russ Cox45bdf032010-06-20 12:16:25 -0700301 switch v.Type().Kind() {
Russ Cox45bdf032010-06-20 12:16:25 -0700302 case Float32:
303 v.SetValue(NewValue(float32(256.25)))
304 case Float64:
Russ Coxf2b5a072011-01-19 23:09:00 -0500305 v.SetValue(NewValue(512.125))
Russ Cox45bdf032010-06-20 12:16:25 -0700306 }
307 case *ComplexValue:
308 switch v.Type().Kind() {
Russ Cox45bdf032010-06-20 12:16:25 -0700309 case Complex64:
310 v.SetValue(NewValue(complex64(532.125 + 10i)))
311 case Complex128:
312 v.SetValue(NewValue(complex128(564.25 + 1i)))
313 }
314
Adam Langleya8a678f2009-10-21 19:51:27 -0700315 case *StringValue:
Robert Griesemer40621d52009-11-09 12:07:39 -0800316 v.SetValue(NewValue("stringy cheese"))
Adam Langleya8a678f2009-10-21 19:51:27 -0700317 case *BoolValue:
Robert Griesemer40621d52009-11-09 12:07:39 -0800318 v.SetValue(NewValue(true))
Adam Langleya8a678f2009-10-21 19:51:27 -0700319 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800320 s := valueToString(v)
Adam Langleya8a678f2009-10-21 19:51:27 -0700321 if s != tt.s {
Robert Griesemer40621d52009-11-09 12:07:39 -0800322 t.Errorf("#%d: have %#q, want %#q", i, s, tt.s)
Adam Langleya8a678f2009-10-21 19:51:27 -0700323 }
324 }
325}
326
Robert Griesemer77334b982009-11-05 14:23:20 -0800327var _i = 7
Russ Cox64f4e0b2009-07-07 11:03:12 -0700328
Robert Griesemer77334b982009-11-05 14:23:20 -0800329var valueToStringTests = []pair{
Robert Griesemer34788912010-10-22 10:06:33 -0700330 {123, "123"},
331 {123.5, "123.5"},
332 {byte(123), "123"},
333 {"abc", "abc"},
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})"},
Rob Pike536b1f22008-10-23 17:13:34 -0700340}
341
Russ Cox64f4e0b2009-07-07 11:03:12 -0700342func TestValueToString(t *testing.T) {
343 for i, test := range valueToStringTests {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800344 s := valueToString(NewValue(test.i))
Russ Cox64f4e0b2009-07-07 11:03:12 -0700345 if s != test.s {
Robert Griesemer40621d52009-11-09 12:07:39 -0800346 t.Errorf("#%d: have %#q, want %#q", i, s, test.s)
Rob Pike419e1e02008-11-12 19:05:05 -0800347 }
348 }
Russ Cox64f4e0b2009-07-07 11:03:12 -0700349}
Rob Pike419e1e02008-11-12 19:05:05 -0800350
Russ Cox64f4e0b2009-07-07 11:03:12 -0700351func TestArrayElemSet(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800352 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}"
Russ Cox64f4e0b2009-07-07 11:03:12 -0700356 if s != want {
Robert Griesemer40621d52009-11-09 12:07:39 -0800357 t.Errorf("[10]int: have %#q want %#q", s, want)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700358 }
Rob Pikea45f9472008-11-04 22:54:11 -0800359
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800360 v = NewValue([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
361 v.(*SliceValue).Elem(4).(*IntValue).Set(123)
362 s = valueToString(v)
363 const want1 = "[]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"
Russ Cox64f4e0b2009-07-07 11:03:12 -0700364 if s != want1 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800365 t.Errorf("[]int: have %#q want %#q", s, want1)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700366 }
367}
Rob Pikea45f9472008-11-04 22:54:11 -0800368
Russ Cox64f4e0b2009-07-07 11:03:12 -0700369func TestPtrPointTo(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800370 var ip *int32
371 var i int32 = 1234
372 vip := NewValue(&ip)
373 vi := NewValue(i)
374 vip.(*PtrValue).Elem().(*PtrValue).PointTo(vi)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700375 if *ip != 1234 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800376 t.Errorf("got %d, want 1234", *ip)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700377 }
Robert Griesemera48b35e2010-08-17 15:12:28 -0700378
379 ip = nil
380 vp := NewValue(ip).(*PtrValue)
381 vp.PointTo(vp.Elem())
382 if ip != nil {
383 t.Errorf("got non-nil (%p), want nil", ip)
384 }
Russ Cox64f4e0b2009-07-07 11:03:12 -0700385}
Rob Pikea45f9472008-11-04 22:54:11 -0800386
Russ Cox7295b612010-04-20 17:02:08 -0700387func TestPtrSetNil(t *testing.T) {
388 var i int32 = 1234
389 ip := &i
390 vip := NewValue(&ip)
391 vip.(*PtrValue).Elem().(*PtrValue).Set(nil)
392 if ip != nil {
393 t.Errorf("got non-nil (%d), want nil", *ip)
394 }
395}
396
397func TestMapSetNil(t *testing.T) {
398 m := make(map[string]int)
399 vm := NewValue(&m)
400 vm.(*PtrValue).Elem().(*MapValue).Set(nil)
401 if m != nil {
402 t.Errorf("got non-nil (%p), want nil", m)
403 }
404}
405
406
Rob Pikee02f2b52009-11-08 21:57:59 -0800407func TestAll(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800408 testType(t, 1, Typeof((int8)(0)), "int8")
409 testType(t, 2, Typeof((*int8)(nil)).(*PtrType).Elem(), "int8")
Rob Pikea45f9472008-11-04 22:54:11 -0800410
Robert Griesemer77334b982009-11-05 14:23:20 -0800411 typ := Typeof((*struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800412 c chan *int32
413 d float32
414 })(nil))
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)
419 f := styp.Field(0)
420 testType(t, 5, f.Type, "chan *int32")
Rob Pikea93c5c82009-07-16 18:21:14 -0700421
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800422 f, present := styp.FieldByName("d")
Rob Pikea93c5c82009-07-16 18:21:14 -0700423 if !present {
Robert Griesemer40621d52009-11-09 12:07:39 -0800424 t.Errorf("FieldByName says present field is absent")
Rob Pikea93c5c82009-07-16 18:21:14 -0700425 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800426 testType(t, 6, f.Type, "float32")
Rob Pikea45f9472008-11-04 22:54:11 -0800427
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800428 f, present = styp.FieldByName("absent")
Rob Pikea93c5c82009-07-16 18:21:14 -0700429 if present {
Robert Griesemer40621d52009-11-09 12:07:39 -0800430 t.Errorf("FieldByName says absent field is present")
Rob Pikea93c5c82009-07-16 18:21:14 -0700431 }
432
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800433 typ = Typeof([32]int32{})
434 testType(t, 7, typ, "[32]int32")
435 testType(t, 8, typ.(*ArrayType).Elem(), "int32")
Rob Pikea45f9472008-11-04 22:54:11 -0800436
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800437 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")
Rob Pikebdbb9582008-11-05 08:17:01 -0800442
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800443 typ = Typeof((chan<- string)(nil))
444 testType(t, 12, typ, "chan<- string")
445 testType(t, 13, typ.(*ChanType).Elem(), "string")
Rob Pike5a1cbe82008-11-05 13:01:33 -0800446
447 // make sure tag strings are not part of element type
Robert Griesemer77334b982009-11-05 14:23:20 -0800448 typ = Typeof(struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800449 d []uint32 "TAG"
450 }{}).(*StructType).Field(0).Type
451 testType(t, 14, typ, "[]uint32")
Rob Pike536b1f22008-10-23 17:13:34 -0700452}
Russ Cox387df5e2008-11-24 14:51:33 -0800453
Russ Cox839a6842009-01-20 14:40:40 -0800454func TestInterfaceGet(t *testing.T) {
Robert Griesemer77334b982009-11-05 14:23:20 -0800455 var inter struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800456 e interface{}
Robert Griesemer77334b982009-11-05 14:23:20 -0800457 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800458 inter.e = 123.456
459 v1 := NewValue(&inter)
460 v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0)
461 assert(t, v2.Type().String(), "interface { }")
462 i2 := v2.(*InterfaceValue).Interface()
463 v3 := NewValue(i2)
Russ Coxf2b5a072011-01-19 23:09:00 -0500464 assert(t, v3.Type().String(), "float64")
Russ Cox387df5e2008-11-24 14:51:33 -0800465}
Russ Coxd0e30cd2008-12-10 15:55:59 -0800466
Russ Coxac6ebfd2009-04-06 21:28:04 -0700467func TestInterfaceValue(t *testing.T) {
Robert Griesemer77334b982009-11-05 14:23:20 -0800468 var inter struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800469 e interface{}
Robert Griesemer77334b982009-11-05 14:23:20 -0800470 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800471 inter.e = 123.456
472 v1 := NewValue(&inter)
473 v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0)
474 assert(t, v2.Type().String(), "interface { }")
475 v3 := v2.(*InterfaceValue).Elem()
Russ Coxf2b5a072011-01-19 23:09:00 -0500476 assert(t, v3.Type().String(), "float64")
Russ Cox64627b02009-04-15 00:55:58 -0700477
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800478 i3 := v2.Interface()
Russ Coxf2b5a072011-01-19 23:09:00 -0500479 if _, ok := i3.(float64); !ok {
480 t.Error("v2.Interface() did not return float64, got ", Typeof(i3))
Russ Cox4b8c13d2009-04-14 19:03:57 -0700481 }
Russ Coxac6ebfd2009-04-06 21:28:04 -0700482}
483
Ian Lance Taylorca9765d2009-04-14 06:46:01 -0700484func TestFunctionValue(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800485 v := NewValue(func() {})
Ian Lance Taylorca9765d2009-04-14 06:46:01 -0700486 if v.Interface() != v.Interface() {
Robert Griesemer40621d52009-11-09 12:07:39 -0800487 t.Fatalf("TestFunction != itself")
Ian Lance Taylorca9765d2009-04-14 06:46:01 -0700488 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800489 assert(t, v.Type().String(), "func()")
Ian Lance Taylorca9765d2009-04-14 06:46:01 -0700490}
491
Nigel Tao8b64cd92010-12-15 08:50:08 +1100492var appendTests = []struct {
493 orig, extra []int
494}{
495 {make([]int, 2, 4), []int{22}},
496 {make([]int, 2, 4), []int{22, 33, 44}},
497}
498
499func 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 {
506 e0[j] = NewValue(e)
507 }
508 // Convert extra from []int to *SliceValue.
509 e1 := NewValue(test.extra).(*SliceValue)
510 // Test Append.
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)
515 }
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)
519 }
520 if len(test.extra) != extraLen {
521 t.Errorf("Append #%d extraLen: have %v, want %v", i, len(test.extra), extraLen)
522 }
523 // Test AppendSlice.
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)
528 }
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)
532 }
533 if len(test.extra) != extraLen {
534 t.Errorf("AppendSlice #%d extraLen: have %v, want %v", i, len(test.extra), extraLen)
535 }
536 }
537}
538
539func TestCopy(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800540 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}
Russ Coxd0e30cd2008-12-10 15:55:59 -0800543 for i := 0; i < len(b); i++ {
544 if b[i] != c[i] {
Robert Griesemer40621d52009-11-09 12:07:39 -0800545 t.Fatalf("b != c before test")
Russ Coxd0e30cd2008-12-10 15:55:59 -0800546 }
547 }
Nigel Taod96685e2010-12-15 20:54:11 +1100548 aa := NewValue(a).(*SliceValue)
549 ab := NewValue(b).(*SliceValue)
Russ Coxd47d8882008-12-18 22:37:22 -0800550 for tocopy := 1; tocopy <= 7; tocopy++ {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800551 aa.SetLen(tocopy)
Nigel Tao73fd2982010-12-12 20:27:29 +1100552 Copy(ab, aa)
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800553 aa.SetLen(8)
Russ Coxd0e30cd2008-12-10 15:55:59 -0800554 for i := 0; i < tocopy; i++ {
555 if a[i] != b[i] {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700556 t.Errorf("(i) tocopy=%d a[%d]=%d, b[%d]=%d",
Robert Griesemer40621d52009-11-09 12:07:39 -0800557 tocopy, i, a[i], i, b[i])
Russ Coxd0e30cd2008-12-10 15:55:59 -0800558 }
559 }
560 for i := tocopy; i < len(b); i++ {
561 if b[i] != c[i] {
562 if i < len(a) {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700563 t.Errorf("(ii) tocopy=%d a[%d]=%d, b[%d]=%d, c[%d]=%d",
Robert Griesemer40621d52009-11-09 12:07:39 -0800564 tocopy, i, a[i], i, b[i], i, c[i])
Russ Coxd0e30cd2008-12-10 15:55:59 -0800565 } else {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700566 t.Errorf("(iii) tocopy=%d b[%d]=%d, c[%d]=%d",
Robert Griesemer40621d52009-11-09 12:07:39 -0800567 tocopy, i, b[i], i, c[i])
Russ Coxd0e30cd2008-12-10 15:55:59 -0800568 }
Russ Coxd47d8882008-12-18 22:37:22 -0800569 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -0800570 t.Logf("tocopy=%d elem %d is okay\n", tocopy, i)
Russ Coxd0e30cd2008-12-10 15:55:59 -0800571 }
572 }
573 }
574}
Russ Cox484ba932009-01-09 00:17:46 -0800575
Russ Cox839a6842009-01-20 14:40:40 -0800576func TestBigUnnamedStruct(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800577 b := struct{ a, b, c, d int64 }{1, 2, 3, 4}
578 v := NewValue(b)
Robert Griesemer77334b982009-11-05 14:23:20 -0800579 b1 := v.Interface().(struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800580 a, b, c, d int64
581 })
Russ Cox484ba932009-01-09 00:17:46 -0800582 if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d {
Robert Griesemer40621d52009-11-09 12:07:39 -0800583 t.Errorf("NewValue(%v).Interface().(*Big) = %v", b, b1)
Russ Cox484ba932009-01-09 00:17:46 -0800584 }
585}
586
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800587type big struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800588 a, b, c, d, e int64
Russ Cox484ba932009-01-09 00:17:46 -0800589}
Robert Griesemer77334b982009-11-05 14:23:20 -0800590
Russ Cox839a6842009-01-20 14:40:40 -0800591func TestBigStruct(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800592 b := big{1, 2, 3, 4, 5}
593 v := NewValue(b)
594 b1 := v.Interface().(big)
Russ Cox484ba932009-01-09 00:17:46 -0800595 if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d || b1.e != b.e {
Robert Griesemer40621d52009-11-09 12:07:39 -0800596 t.Errorf("NewValue(%v).Interface().(big) = %v", b, b1)
Russ Cox484ba932009-01-09 00:17:46 -0800597 }
598}
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700599
600type Basic struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800601 x int
602 y float32
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700603}
604
Russ Cox64627b02009-04-15 00:55:58 -0700605type NotBasic Basic
606
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700607type DeepEqualTest struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800608 a, b interface{}
609 eq bool
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700610}
611
Robert Griesemer77334b982009-11-05 14:23:20 -0800612var deepEqualTests = []DeepEqualTest{
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700613 // Equalities
Robert Griesemer34788912010-10-22 10:06:33 -0700614 {1, 1, true},
615 {int32(1), int32(1), true},
616 {0.5, 0.5, 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},
Russ Coxa439f662009-07-01 16:45:09 -0700624
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700625 // Inequalities
Robert Griesemer34788912010-10-22 10:06:33 -0700626 {1, 2, false},
627 {int32(1), int32(2), false},
628 {0.5, 0.6, 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},
639 {nil, 1, false},
640 {1, nil, false},
Russ Coxa439f662009-07-01 16:45:09 -0700641
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700642 // Mismatched types
Robert Griesemer34788912010-10-22 10:06:33 -0700643 {1, 1.0, 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},
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700650}
651
652func TestDeepEqual(t *testing.T) {
Russ Coxca6a0fe2009-09-15 09:41:59 -0700653 for _, test := range deepEqualTests {
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700654 if r := DeepEqual(test.a, test.b); r != test.eq {
Robert Griesemer40621d52009-11-09 12:07:39 -0800655 t.Errorf("DeepEqual(%v, %v) = %v, want %v", test.a, test.b, r, test.eq)
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700656 }
657 }
658}
659
Russ Cox48662232009-06-25 14:25:38 -0700660func TestTypeof(t *testing.T) {
Russ Coxca6a0fe2009-09-15 09:41:59 -0700661 for _, test := range deepEqualTests {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800662 v := NewValue(test.a)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700663 if v == nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800664 continue
Russ Cox64f4e0b2009-07-07 11:03:12 -0700665 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800666 typ := Typeof(test.a)
Russ Cox48662232009-06-25 14:25:38 -0700667 if typ != v.Type() {
Robert Griesemer40621d52009-11-09 12:07:39 -0800668 t.Errorf("Typeof(%v) = %v, but NewValue(%v).Type() = %v", test.a, typ, test.a, v.Type())
Russ Cox48662232009-06-25 14:25:38 -0700669 }
670 }
671}
672
Rob Pike1880b902009-07-10 11:20:10 -0700673type Recursive struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800674 x int
675 r *Recursive
Rob Pike1880b902009-07-10 11:20:10 -0700676}
677
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700678func TestDeepEqualRecursiveStruct(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800679 a, b := new(Recursive), new(Recursive)
680 *a = Recursive{12, a}
681 *b = Recursive{12, b}
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700682 if !DeepEqual(a, b) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800683 t.Error("DeepEqual(recursive same) = false, want true")
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700684 }
685}
686
Russ Cox45bdf032010-06-20 12:16:25 -0700687type _Complex struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800688 a int
Russ Cox45bdf032010-06-20 12:16:25 -0700689 b [3]*_Complex
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800690 c *string
Russ Coxf2b5a072011-01-19 23:09:00 -0500691 d map[float64]float64
Rob Pike1880b902009-07-10 11:20:10 -0700692}
693
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700694func TestDeepEqualComplexStruct(t *testing.T) {
Russ Coxf2b5a072011-01-19 23:09:00 -0500695 m := make(map[float64]float64)
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800696 stra, strb := "hello", "hello"
Russ Cox45bdf032010-06-20 12:16:25 -0700697 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}
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700700 if !DeepEqual(a, b) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800701 t.Error("DeepEqual(complex same) = false, want true")
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700702 }
703}
704
705func TestDeepEqualComplexStructInequality(t *testing.T) {
Russ Coxf2b5a072011-01-19 23:09:00 -0500706 m := make(map[float64]float64)
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800707 stra, strb := "hello", "helloo" // Difference is here
Russ Cox45bdf032010-06-20 12:16:25 -0700708 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}
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700711 if DeepEqual(a, b) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800712 t.Error("DeepEqual(complex different) = true, want false")
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700713 }
714}
Rob Pike93831d22009-04-29 22:16:53 -0700715
716
717func check2ndField(x interface{}, offs uintptr, t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800718 s := NewValue(x).(*StructValue)
719 f := s.Type().(*StructType).Field(1)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700720 if f.Offset != offs {
Robert Griesemer40621d52009-11-09 12:07:39 -0800721 t.Error("mismatched offsets in structure alignment:", f.Offset, offs)
Rob Pike93831d22009-04-29 22:16:53 -0700722 }
723}
724
725// Check that structure alignment & offsets viewed through reflect agree with those
726// from the compiler itself.
727func TestAlignment(t *testing.T) {
728 type T1inner struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800729 a int
Rob Pike93831d22009-04-29 22:16:53 -0700730 }
731 type T1 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800732 T1inner
733 f int
Rob Pike93831d22009-04-29 22:16:53 -0700734 }
735 type T2inner struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800736 a, b int
Rob Pike93831d22009-04-29 22:16:53 -0700737 }
738 type T2 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800739 T2inner
740 f int
Rob Pike93831d22009-04-29 22:16:53 -0700741 }
742
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800743 x := T1{T1inner{2}, 17}
744 check2ndField(x, uintptr(unsafe.Pointer(&x.f))-uintptr(unsafe.Pointer(&x)), t)
Rob Pike93831d22009-04-29 22:16:53 -0700745
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800746 x1 := T2{T2inner{2, 3}, 17}
747 check2ndField(x1, uintptr(unsafe.Pointer(&x1.f))-uintptr(unsafe.Pointer(&x1)), t)
Rob Pike93831d22009-04-29 22:16:53 -0700748}
Rob Pikea8f6e382009-05-12 14:57:44 -0700749
Russ Cox64f4e0b2009-07-07 11:03:12 -0700750type IsNiller interface {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800751 IsNil() bool
Rob Pikea8f6e382009-05-12 14:57:44 -0700752}
753
754func Nil(a interface{}, t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800755 n := NewValue(a).(*StructValue).Field(0).(IsNiller)
Rob Pikea8f6e382009-05-12 14:57:44 -0700756 if !n.IsNil() {
Robert Griesemer40621d52009-11-09 12:07:39 -0800757 t.Errorf("%v should be nil", a)
Rob Pikea8f6e382009-05-12 14:57:44 -0700758 }
759}
760
761func NotNil(a interface{}, t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800762 n := NewValue(a).(*StructValue).Field(0).(IsNiller)
Rob Pikea8f6e382009-05-12 14:57:44 -0700763 if n.IsNil() {
Robert Griesemer40621d52009-11-09 12:07:39 -0800764 t.Errorf("value of type %v should not be nil", NewValue(a).Type().String())
Rob Pikea8f6e382009-05-12 14:57:44 -0700765 }
766}
767
768func TestIsNil(t *testing.T) {
769 // These do not implement IsNil
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800770 doNotNil := []interface{}{int(0), float32(0), struct{ a int }{}}
Russ Coxca6a0fe2009-09-15 09:41:59 -0700771 for _, ts := range doNotNil {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800772 ty := Typeof(ts)
773 v := MakeZero(ty)
Russ Coxca6a0fe2009-09-15 09:41:59 -0700774 if _, ok := v.(IsNiller); ok {
Robert Griesemer40621d52009-11-09 12:07:39 -0800775 t.Errorf("%s is nilable; should not be", ts)
Rob Pikea8f6e382009-05-12 14:57:44 -0700776 }
777 }
778
Russ Cox64f4e0b2009-07-07 11:03:12 -0700779 // These do implement IsNil.
780 // Wrap in extra struct to hide interface type.
781 doNil := []interface{}{
Robert Griesemera05a5462009-11-06 16:33:53 -0800782 struct{ x *int }{},
783 struct{ x interface{} }{},
784 struct{ x map[string]int }{},
785 struct{ x func() bool }{},
786 struct{ x chan int }{},
787 struct{ x []string }{},
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800788 }
Russ Coxca6a0fe2009-09-15 09:41:59 -0700789 for _, ts := range doNil {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800790 ty := Typeof(ts).(*StructType).Field(0).Type
791 v := MakeZero(ty)
Russ Coxca6a0fe2009-09-15 09:41:59 -0700792 if _, ok := v.(IsNiller); !ok {
Robert Griesemer40621d52009-11-09 12:07:39 -0800793 t.Errorf("%s %T is not nilable; should be", ts, v)
Rob Pikea8f6e382009-05-12 14:57:44 -0700794 }
795 }
Russ Cox64f4e0b2009-07-07 11:03:12 -0700796
Rob Pikea8f6e382009-05-12 14:57:44 -0700797 // Check the implementations
Robert Griesemer77334b982009-11-05 14:23:20 -0800798 var pi struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800799 x *int
Robert Griesemer77334b982009-11-05 14:23:20 -0800800 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800801 Nil(pi, t)
802 pi.x = new(int)
803 NotNil(pi, t)
Rob Pikea8f6e382009-05-12 14:57:44 -0700804
Robert Griesemer77334b982009-11-05 14:23:20 -0800805 var si struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800806 x []int
Robert Griesemer77334b982009-11-05 14:23:20 -0800807 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800808 Nil(si, t)
809 si.x = make([]int, 10)
810 NotNil(si, t)
Rob Pikea8f6e382009-05-12 14:57:44 -0700811
Robert Griesemer77334b982009-11-05 14:23:20 -0800812 var ci struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800813 x chan int
Robert Griesemer77334b982009-11-05 14:23:20 -0800814 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800815 Nil(ci, t)
816 ci.x = make(chan int)
817 NotNil(ci, t)
Rob Pikea8f6e382009-05-12 14:57:44 -0700818
Robert Griesemer77334b982009-11-05 14:23:20 -0800819 var mi struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800820 x map[int]int
Robert Griesemer77334b982009-11-05 14:23:20 -0800821 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800822 Nil(mi, t)
823 mi.x = make(map[int]int)
824 NotNil(mi, t)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700825
Robert Griesemer77334b982009-11-05 14:23:20 -0800826 var ii struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800827 x interface{}
Robert Griesemer77334b982009-11-05 14:23:20 -0800828 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800829 Nil(ii, t)
830 ii.x = 2
831 NotNil(ii, t)
Rob Pikea8f6e382009-05-12 14:57:44 -0700832
Robert Griesemer77334b982009-11-05 14:23:20 -0800833 var fi struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800834 x func(t *testing.T)
Robert Griesemer77334b982009-11-05 14:23:20 -0800835 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800836 Nil(fi, t)
837 fi.x = TestIsNil
838 NotNil(fi, t)
Rob Pikea8f6e382009-05-12 14:57:44 -0700839}
Russ Cox96cfd152009-05-21 11:50:20 -0700840
841func TestInterfaceExtraction(t *testing.T) {
842 var s struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800843 w io.Writer
Russ Cox96cfd152009-05-21 11:50:20 -0700844 }
845
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800846 s.w = os.Stdout
847 v := Indirect(NewValue(&s)).(*StructValue).Field(0).Interface()
Russ Cox96cfd152009-05-21 11:50:20 -0700848 if v != s.w.(interface{}) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800849 t.Error("Interface() on interface: ", v, s.w)
Russ Cox96cfd152009-05-21 11:50:20 -0700850 }
851}
Russ Coxf9666232009-05-21 14:06:43 -0700852
853func 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.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800858 var i interface{} = "hello"
Russ Coxf9666232009-05-21 14:06:43 -0700859
860 // if i pass the interface value by value
861 // to NewValue, i should get a fresh copy
862 // of the value.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800863 v := NewValue(i)
Russ Coxf9666232009-05-21 14:06:43 -0700864
865 // and setting that copy to "bye" should
866 // not change the value stored in i.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800867 v.(*StringValue).Set("bye")
Russ Coxf9666232009-05-21 14:06:43 -0700868 if i.(string) != "hello" {
Robert Griesemer40621d52009-11-09 12:07:39 -0800869 t.Errorf(`Set("bye") changed i to %s`, i.(string))
Russ Coxf9666232009-05-21 14:06:43 -0700870 }
871
872 // the same should be true of smaller items.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800873 i = 123
874 v = NewValue(i)
875 v.(*IntValue).Set(234)
Russ Coxf9666232009-05-21 14:06:43 -0700876 if i.(int) != 123 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800877 t.Errorf("Set(234) changed i to %d", i.(int))
Russ Coxf9666232009-05-21 14:06:43 -0700878 }
879}
David Symondsd4e57ff2009-06-15 18:35:04 -0700880
881func TestNilPtrValueSub(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800882 var pi *int
Russ Cox64f4e0b2009-07-07 11:03:12 -0700883 if pv := NewValue(pi).(*PtrValue); pv.Elem() != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800884 t.Error("NewValue((*int)(nil)).(*PtrValue).Elem() != nil")
David Symondsd4e57ff2009-06-15 18:35:04 -0700885 }
886}
Russ Cox764b6ec2009-07-08 13:55:57 -0700887
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700888func TestMap(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800889 m := map[string]int{"a": 1, "b": 2}
890 mv := NewValue(m).(*MapValue)
Russ Cox764b6ec2009-07-08 13:55:57 -0700891 if n := mv.Len(); n != len(m) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800892 t.Errorf("Len = %d, want %d", n, len(m))
Russ Cox764b6ec2009-07-08 13:55:57 -0700893 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800894 keys := mv.Keys()
895 i := 0
896 newmap := MakeMap(mv.Type().(*MapType))
Russ Cox764b6ec2009-07-08 13:55:57 -0700897 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
901 // the test easier.
902 if i >= len(keys) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800903 t.Errorf("Missing key #%d %q", i, k)
Russ Cox764b6ec2009-07-08 13:55:57 -0700904 } else if kv := keys[i].(*StringValue); kv.Get() != k {
Robert Griesemer40621d52009-11-09 12:07:39 -0800905 t.Errorf("Keys[%d] = %q, want %q", i, kv.Get(), k)
Russ Cox764b6ec2009-07-08 13:55:57 -0700906 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800907 i++
Russ Cox764b6ec2009-07-08 13:55:57 -0700908
909 // Check that value lookup is correct.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800910 vv := mv.Elem(NewValue(k))
Russ Cox45bdf032010-06-20 12:16:25 -0700911 if vi := vv.(*IntValue).Get(); vi != int64(v) {
Rob Pike1ce62452010-12-07 16:42:54 -0500912 t.Errorf("Key %q: have value %d, want %d", k, vi, v)
Russ Cox764b6ec2009-07-08 13:55:57 -0700913 }
914
915 // Copy into new map.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800916 newmap.SetElem(NewValue(k), NewValue(v))
Russ Cox764b6ec2009-07-08 13:55:57 -0700917 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800918 vv := mv.Elem(NewValue("not-present"))
Russ Cox764b6ec2009-07-08 13:55:57 -0700919 if vv != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800920 t.Errorf("Invalid key: got non-nil value %s", valueToString(vv))
Russ Cox764b6ec2009-07-08 13:55:57 -0700921 }
922
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800923 newm := newmap.Interface().(map[string]int)
Russ Cox764b6ec2009-07-08 13:55:57 -0700924 if len(newm) != len(m) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800925 t.Errorf("length after copy: newm=%d, m=%d", newm, m)
Russ Cox764b6ec2009-07-08 13:55:57 -0700926 }
927
928 for k, v := range newm {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800929 mv, ok := m[k]
Russ Cox764b6ec2009-07-08 13:55:57 -0700930 if mv != v {
Robert Griesemer40621d52009-11-09 12:07:39 -0800931 t.Errorf("newm[%q] = %d, but m[%q] = %d, %v", k, v, k, mv, ok)
Russ Cox764b6ec2009-07-08 13:55:57 -0700932 }
933 }
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700934
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800935 newmap.SetElem(NewValue("a"), nil)
936 v, ok := newm["a"]
Russ Cox764b6ec2009-07-08 13:55:57 -0700937 if ok {
Robert Griesemer40621d52009-11-09 12:07:39 -0800938 t.Errorf("newm[\"a\"] = %d after delete", v)
Russ Cox764b6ec2009-07-08 13:55:57 -0700939 }
Russ Cox7295b612010-04-20 17:02:08 -0700940
941 mv = NewValue(&m).(*PtrValue).Elem().(*MapValue)
942 mv.Set(nil)
943 if m != nil {
944 t.Errorf("mv.Set(nil) failed")
945 }
Russ Cox764b6ec2009-07-08 13:55:57 -0700946}
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700947
948func TestChan(t *testing.T) {
949 for loop := 0; loop < 2; loop++ {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800950 var c chan int
951 var cv *ChanValue
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700952
953 // check both ways to allocate channels
954 switch loop {
955 case 1:
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800956 c = make(chan int, 1)
957 cv = NewValue(c).(*ChanValue)
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700958 case 0:
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800959 cv = MakeChan(Typeof(c).(*ChanType), 1)
960 c = cv.Interface().(chan int)
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700961 }
962
963 // Send
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800964 cv.Send(NewValue(2))
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700965 if i := <-c; i != 2 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800966 t.Errorf("reflect Send 2, native recv %d", i)
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700967 }
968
969 // Recv
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800970 c <- 3
Russ Cox3f915f52011-03-11 14:47:44 -0500971 if i, ok := cv.Recv(); i.(*IntValue).Get() != 3 || !ok {
972 t.Errorf("native send 3, reflect Recv %d, %t", i.(*IntValue).Get(), ok)
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700973 }
974
975 // TryRecv fail
Russ Cox3f915f52011-03-11 14:47:44 -0500976 val, ok := cv.TryRecv()
977 if val != nil || ok {
978 t.Errorf("TryRecv on empty chan: %s, %t", valueToString(val), ok)
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700979 }
980
981 // TryRecv success
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800982 c <- 4
Russ Cox3f915f52011-03-11 14:47:44 -0500983 val, ok = cv.TryRecv()
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700984 if val == nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800985 t.Errorf("TryRecv on ready chan got nil")
Russ Cox3f915f52011-03-11 14:47:44 -0500986 } else if i := val.(*IntValue).Get(); i != 4 || !ok {
987 t.Errorf("native send 4, TryRecv %d, %t", i, ok)
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700988 }
989
990 // TrySend fail
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800991 c <- 100
Russ Cox3f915f52011-03-11 14:47:44 -0500992 ok = cv.TrySend(NewValue(5))
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800993 i := <-c
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700994 if ok {
Robert Griesemer40621d52009-11-09 12:07:39 -0800995 t.Errorf("TrySend on full chan succeeded: value %d", i)
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700996 }
997
998 // TrySend success
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800999 ok = cv.TrySend(NewValue(6))
Russ Cox5ddaf9a2009-07-08 15:00:54 -07001000 if !ok {
Robert Griesemer40621d52009-11-09 12:07:39 -08001001 t.Errorf("TrySend on empty chan failed")
Russ Cox5ddaf9a2009-07-08 15:00:54 -07001002 } else {
1003 if i = <-c; i != 6 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001004 t.Errorf("TrySend 6, recv %d", i)
Russ Cox5ddaf9a2009-07-08 15:00:54 -07001005 }
1006 }
Russ Cox653cef12009-08-26 10:47:18 -07001007
1008 // Close
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001009 c <- 123
1010 cv.Close()
Russ Cox3f915f52011-03-11 14:47:44 -05001011 if i, ok := cv.Recv(); i.(*IntValue).Get() != 123 || !ok {
1012 t.Errorf("send 123 then close; Recv %d, %t", i.(*IntValue).Get(), ok)
Russ Cox653cef12009-08-26 10:47:18 -07001013 }
Russ Cox3f915f52011-03-11 14:47:44 -05001014 if i, ok := cv.Recv(); i.(*IntValue).Get() != 0 || ok {
1015 t.Errorf("after close Recv %d, %t", i.(*IntValue).Get(), ok)
Russ Cox653cef12009-08-26 10:47:18 -07001016 }
Russ Cox5ddaf9a2009-07-08 15:00:54 -07001017 }
1018
1019 // check creation of unbuffered channel
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001020 var c chan int
1021 cv := MakeChan(Typeof(c).(*ChanType), 0)
1022 c = cv.Interface().(chan int)
Russ Cox5ddaf9a2009-07-08 15:00:54 -07001023 if cv.TrySend(NewValue(7)) {
Robert Griesemer40621d52009-11-09 12:07:39 -08001024 t.Errorf("TrySend on sync chan succeeded")
Russ Cox5ddaf9a2009-07-08 15:00:54 -07001025 }
Russ Cox3f915f52011-03-11 14:47:44 -05001026 if v, ok := cv.TryRecv(); v != nil || ok {
Robert Griesemer40621d52009-11-09 12:07:39 -08001027 t.Errorf("TryRecv on sync chan succeeded")
Russ Cox5ddaf9a2009-07-08 15:00:54 -07001028 }
Russ Coxde7920e2009-08-26 12:42:22 -07001029
1030 // len/cap
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001031 cv = MakeChan(Typeof(c).(*ChanType), 10)
1032 c = cv.Interface().(chan int)
Russ Coxde7920e2009-08-26 12:42:22 -07001033 for i := 0; i < 3; i++ {
Robert Griesemer40621d52009-11-09 12:07:39 -08001034 c <- i
Russ Coxde7920e2009-08-26 12:42:22 -07001035 }
1036 if l, m := cv.Len(), cv.Cap(); l != len(c) || m != cap(c) {
Robert Griesemer40621d52009-11-09 12:07:39 -08001037 t.Errorf("Len/Cap = %d/%d want %d/%d", l, m, len(c), cap(c))
Russ Coxde7920e2009-08-26 12:42:22 -07001038 }
1039
Russ Cox5ddaf9a2009-07-08 15:00:54 -07001040}
1041
Russ Coxbba278a2009-07-08 18:16:09 -07001042// Difficult test for function call because of
1043// implicit padding between arguments.
Robert Griesemer77334b982009-11-05 14:23:20 -08001044func dummy(b byte, c int, d byte) (i byte, j int, k byte) {
Robert Griesemer40621d52009-11-09 12:07:39 -08001045 return b, c, d
Russ Coxbba278a2009-07-08 18:16:09 -07001046}
1047
1048func TestFunc(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001049 ret := NewValue(dummy).(*FuncValue).Call([]Value{NewValue(byte(10)), NewValue(20), NewValue(byte(30))})
Russ Coxbba278a2009-07-08 18:16:09 -07001050 if len(ret) != 3 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001051 t.Fatalf("Call returned %d values, want 3", len(ret))
Russ Coxbba278a2009-07-08 18:16:09 -07001052 }
1053
Russ Cox45bdf032010-06-20 12:16:25 -07001054 i := ret[0].(*UintValue).Get()
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001055 j := ret[1].(*IntValue).Get()
Russ Cox45bdf032010-06-20 12:16:25 -07001056 k := ret[2].(*UintValue).Get()
Russ Coxbba278a2009-07-08 18:16:09 -07001057 if i != 10 || j != 20 || k != 30 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001058 t.Errorf("Call returned %d, %d, %d; want 10, 20, 30", i, j, k)
Russ Coxbba278a2009-07-08 18:16:09 -07001059 }
1060}
Russ Cox12ebbe72009-07-09 17:27:49 -07001061
1062type Point struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001063 x, y int
Russ Cox12ebbe72009-07-09 17:27:49 -07001064}
1065
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001066func (p Point) Dist(scale int) int { return p.x*p.x*scale + p.y*p.y*scale }
Russ Cox12ebbe72009-07-09 17:27:49 -07001067
1068func TestMethod(t *testing.T) {
1069 // Non-curried method of type.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001070 p := Point{3, 4}
1071 i := Typeof(p).Method(0).Func.Call([]Value{NewValue(p), NewValue(10)})[0].(*IntValue).Get()
Russ Cox12ebbe72009-07-09 17:27:49 -07001072 if i != 250 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001073 t.Errorf("Type Method returned %d; want 250", i)
Russ Cox12ebbe72009-07-09 17:27:49 -07001074 }
1075
Russ Cox00ffd592010-09-28 13:43:50 -04001076 i = Typeof(&p).Method(0).Func.Call([]Value{NewValue(&p), NewValue(10)})[0].(*IntValue).Get()
1077 if i != 250 {
1078 t.Errorf("Pointer Type Method returned %d; want 250", i)
1079 }
1080
Russ Cox12ebbe72009-07-09 17:27:49 -07001081 // Curried method of value.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001082 i = NewValue(p).Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
Russ Cox12ebbe72009-07-09 17:27:49 -07001083 if i != 250 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001084 t.Errorf("Value Method returned %d; want 250", i)
Russ Cox12ebbe72009-07-09 17:27:49 -07001085 }
1086
Russ Coxe46acb02011-03-03 13:20:17 -05001087 // Curried method of pointer.
1088 i = NewValue(&p).Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
1089 if i != 250 {
1090 t.Errorf("Value Method returned %d; want 250", i)
1091 }
1092
1093 // Curried method of pointer to value.
1094 i = NewValue(p).Addr().Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
1095 if i != 250 {
1096 t.Errorf("Value Method returned %d; want 250", i)
1097 }
1098
Russ Cox12ebbe72009-07-09 17:27:49 -07001099 // 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.
Robert Griesemer77334b982009-11-05 14:23:20 -08001103 var s = struct {
1104 x interface {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001105 Dist(int) int
1106 }
1107 }{p}
1108 pv := NewValue(s).(*StructValue).Field(0)
1109 i = pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
Russ Cox12ebbe72009-07-09 17:27:49 -07001110 if i != 250 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001111 t.Errorf("Interface Method returned %d; want 250", i)
Russ Cox12ebbe72009-07-09 17:27:49 -07001112 }
1113}
Russ Cox92e92572009-07-10 16:32:26 -07001114
1115func TestInterfaceSet(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001116 p := &Point{3, 4}
Russ Cox92e92572009-07-10 16:32:26 -07001117
1118 var s struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001119 I interface{}
1120 P interface {
1121 Dist(int) int
1122 }
Russ Cox92e92572009-07-10 16:32:26 -07001123 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001124 sv := NewValue(&s).(*PtrValue).Elem().(*StructValue)
1125 sv.Field(0).(*InterfaceValue).Set(NewValue(p))
Russ Cox92e92572009-07-10 16:32:26 -07001126 if q := s.I.(*Point); q != p {
Robert Griesemer40621d52009-11-09 12:07:39 -08001127 t.Errorf("i: have %p want %p", q, p)
Russ Cox92e92572009-07-10 16:32:26 -07001128 }
1129
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001130 pv := sv.Field(1).(*InterfaceValue)
1131 pv.Set(NewValue(p))
Russ Cox92e92572009-07-10 16:32:26 -07001132 if q := s.P.(*Point); q != p {
Robert Griesemer40621d52009-11-09 12:07:39 -08001133 t.Errorf("i: have %p want %p", q, p)
Russ Cox92e92572009-07-10 16:32:26 -07001134 }
Russ Cox3b864e42009-08-12 13:18:37 -07001135
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001136 i := pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
Russ Cox92e92572009-07-10 16:32:26 -07001137 if i != 250 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001138 t.Errorf("Interface Method returned %d; want 250", i)
Russ Cox92e92572009-07-10 16:32:26 -07001139 }
1140}
Robert Griesemera2880952009-08-05 15:56:44 -07001141
Robert Griesemer77334b982009-11-05 14:23:20 -08001142type T1 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001143 a string
1144 int
Robert Griesemer77334b982009-11-05 14:23:20 -08001145}
Robert Griesemera2880952009-08-05 15:56:44 -07001146
1147func TestAnonymousFields(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001148 var field StructField
1149 var ok bool
1150 var t1 T1
1151 type1 := Typeof(t1).(*StructType)
Robert Griesemera2880952009-08-05 15:56:44 -07001152 if field, ok = type1.FieldByName("int"); !ok {
Robert Griesemer40621d52009-11-09 12:07:39 -08001153 t.Error("no field 'int'")
Robert Griesemera2880952009-08-05 15:56:44 -07001154 }
1155 if field.Index[0] != 1 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001156 t.Error("field index should be 1; is", field.Index)
Robert Griesemera2880952009-08-05 15:56:44 -07001157 }
1158}
1159
1160type FTest struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001161 s interface{}
1162 name string
1163 index []int
1164 value int
Robert Griesemera2880952009-08-05 15:56:44 -07001165}
1166
Russ Cox92543da2009-08-24 17:04:12 -07001167type D1 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001168 d int
Russ Cox92543da2009-08-24 17:04:12 -07001169}
1170type D2 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001171 d int
Russ Cox92543da2009-08-24 17:04:12 -07001172}
1173
Robert Griesemera2880952009-08-05 15:56:44 -07001174type S0 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001175 a, b, c int
1176 D1
1177 D2
Robert Griesemera2880952009-08-05 15:56:44 -07001178}
1179
1180type S1 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001181 b int
1182 S0
Robert Griesemera2880952009-08-05 15:56:44 -07001183}
1184
1185type S2 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001186 a int
1187 *S1
Robert Griesemera2880952009-08-05 15:56:44 -07001188}
1189
Russ Cox92543da2009-08-24 17:04:12 -07001190type S1x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001191 S1
Russ Cox92543da2009-08-24 17:04:12 -07001192}
1193
1194type S1y struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001195 S1
Russ Cox92543da2009-08-24 17:04:12 -07001196}
1197
1198type S3 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001199 S1x
1200 S2
1201 d, e int
1202 *S1y
Robert Griesemera2880952009-08-05 15:56:44 -07001203}
1204
1205type S4 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001206 *S4
1207 a int
Robert Griesemera2880952009-08-05 15:56:44 -07001208}
1209
Robert Griesemer77334b982009-11-05 14:23:20 -08001210var fieldTests = []FTest{
Robert Griesemer34788912010-10-22 10:06:33 -07001211 {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},
Robert Griesemera2880952009-08-05 15:56:44 -07001231}
1232
1233func TestFieldByIndex(t *testing.T) {
1234 for _, test := range fieldTests {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001235 s := Typeof(test.s).(*StructType)
1236 f := s.FieldByIndex(test.index)
Robert Griesemera2880952009-08-05 15:56:44 -07001237 if f.Name != "" {
1238 if test.index != nil {
1239 if f.Name != test.name {
Robert Griesemer40621d52009-11-09 12:07:39 -08001240 t.Errorf("%s.%s found; want %s", s.Name(), f.Name, test.name)
Robert Griesemera2880952009-08-05 15:56:44 -07001241 }
1242 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -08001243 t.Errorf("%s.%s found", s.Name(), f.Name)
Robert Griesemera2880952009-08-05 15:56:44 -07001244 }
1245 } else if len(test.index) > 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001246 t.Errorf("%s.%s not found", s.Name(), test.name)
Robert Griesemera2880952009-08-05 15:56:44 -07001247 }
1248
1249 if test.value != 0 {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001250 v := NewValue(test.s).(*StructValue).FieldByIndex(test.index)
Robert Griesemera2880952009-08-05 15:56:44 -07001251 if v != nil {
1252 if x, ok := v.Interface().(int); ok {
1253 if x != test.value {
Robert Griesemer40621d52009-11-09 12:07:39 -08001254 t.Errorf("%s%v is %d; want %d", s.Name(), test.index, x, test.value)
Robert Griesemera2880952009-08-05 15:56:44 -07001255 }
1256 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -08001257 t.Errorf("%s%v value not an int", s.Name(), test.index)
Robert Griesemera2880952009-08-05 15:56:44 -07001258 }
1259 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -08001260 t.Errorf("%s%v value not found", s.Name(), test.index)
Robert Griesemera2880952009-08-05 15:56:44 -07001261 }
1262 }
1263 }
1264}
1265
1266func TestFieldByName(t *testing.T) {
1267 for _, test := range fieldTests {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001268 s := Typeof(test.s).(*StructType)
1269 f, found := s.FieldByName(test.name)
Robert Griesemera2880952009-08-05 15:56:44 -07001270 if found {
1271 if test.index != nil {
1272 // Verify field depth and index.
1273 if len(f.Index) != len(test.index) {
Robert Griesemer40621d52009-11-09 12:07:39 -08001274 t.Errorf("%s.%s depth %d; want %d", s.Name(), test.name, len(f.Index), len(test.index))
Robert Griesemera2880952009-08-05 15:56:44 -07001275 } else {
1276 for i, x := range f.Index {
1277 if x != test.index[i] {
Robert Griesemer40621d52009-11-09 12:07:39 -08001278 t.Errorf("%s.%s.Index[%d] is %d; want %d", s.Name(), test.name, i, x, test.index[i])
Robert Griesemera2880952009-08-05 15:56:44 -07001279 }
1280 }
1281 }
1282 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -08001283 t.Errorf("%s.%s found", s.Name(), f.Name)
Robert Griesemera2880952009-08-05 15:56:44 -07001284 }
1285 } else if len(test.index) > 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001286 t.Errorf("%s.%s not found", s.Name(), test.name)
Robert Griesemera2880952009-08-05 15:56:44 -07001287 }
Russ Cox3b864e42009-08-12 13:18:37 -07001288
Robert Griesemera2880952009-08-05 15:56:44 -07001289 if test.value != 0 {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001290 v := NewValue(test.s).(*StructValue).FieldByName(test.name)
Robert Griesemera2880952009-08-05 15:56:44 -07001291 if v != nil {
1292 if x, ok := v.Interface().(int); ok {
1293 if x != test.value {
Robert Griesemer40621d52009-11-09 12:07:39 -08001294 t.Errorf("%s.%s is %d; want %d", s.Name(), test.name, x, test.value)
Robert Griesemera2880952009-08-05 15:56:44 -07001295 }
1296 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -08001297 t.Errorf("%s.%s value not an int", s.Name(), test.name)
Robert Griesemera2880952009-08-05 15:56:44 -07001298 }
1299 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -08001300 t.Errorf("%s.%s value not found", s.Name(), test.name)
Robert Griesemera2880952009-08-05 15:56:44 -07001301 }
1302 }
1303 }
1304}
Russ Cox1cecac82010-01-24 23:33:59 -08001305
1306func TestImportPath(t *testing.T) {
1307 if path := Typeof(vector.Vector{}).PkgPath(); path != "container/vector" {
1308 t.Errorf("Typeof(vector.Vector{}).PkgPath() = %q, want \"container/vector\"", path)
1309 }
1310}
Russ Cox6672b402010-06-14 11:23:11 -07001311
1312func TestDotDotDot(t *testing.T) {
1313 // Test example from FuncType.DotDotDot documentation.
Russ Coxf2b5a072011-01-19 23:09:00 -05001314 var f func(x int, y ...float64)
Russ Cox6672b402010-06-14 11:23:11 -07001315 typ := Typeof(f).(*FuncType)
1316 if typ.NumIn() == 2 && typ.In(0) == Typeof(int(0)) {
1317 sl, ok := typ.In(1).(*SliceType)
1318 if ok {
Russ Coxf2b5a072011-01-19 23:09:00 -05001319 if sl.Elem() == Typeof(0.0) {
Russ Cox6672b402010-06-14 11:23:11 -07001320 // ok
1321 return
1322 }
1323 }
1324 }
1325
1326 // Failed
Russ Coxf2b5a072011-01-19 23:09:00 -05001327 t.Errorf("want NumIn() = 2, In(0) = int, In(1) = []float64")
Russ Cox6672b402010-06-14 11:23:11 -07001328 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))
1331 }
1332 t.Error(s)
1333}
Russ Cox2d5e7322010-09-27 14:09:10 -04001334
Russ Cox00ffd592010-09-28 13:43:50 -04001335type inner struct {
1336 x int
1337}
Russ Cox2d5e7322010-09-27 14:09:10 -04001338
1339type outer struct {
Russ Cox00ffd592010-09-28 13:43:50 -04001340 y int
Russ Cox2d5e7322010-09-27 14:09:10 -04001341 inner
1342}
1343
1344func (*inner) m() {}
1345func (*outer) m() {}
1346
1347func 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++ {
1352 m := typ.Method(i)
1353 t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Get())
1354 }
1355 }
1356}
Russ Cox00ffd592010-09-28 13:43:50 -04001357
1358type innerInt struct {
1359 x int
1360}
1361
1362type outerInt struct {
1363 y int
1364 innerInt
1365}
1366
1367func (i *innerInt) m() int {
1368 return i.x
1369}
1370
1371func 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++ {
1376 m := typ.Method(i)
1377 t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Get())
1378 }
1379 }
1380
1381 i := &innerInt{3}
1382 if v := NewValue(i).Method(0).Call(nil)[0].(*IntValue).Get(); v != 3 {
1383 t.Errorf("i.m() = %d, want 3", v)
1384 }
1385
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)
1389 }
1390
1391 f := (*outerInt).m
1392 if v := f(o); v != 2 {
1393 t.Errorf("f(o) = %d, want 2", v)
1394 }
1395}
Russ Coxe46acb02011-03-03 13:20:17 -05001396
1397func TestPtrTo(t *testing.T) {
1398 var i int
1399
1400 typ := Typeof(i)
1401 for i = 0; i < 100; i++ {
1402 typ = PtrTo(typ)
1403 }
1404 for i = 0; i < 100; i++ {
1405 typ = typ.(*PtrType).Elem()
1406 }
1407 if typ != Typeof(i) {
1408 t.Errorf("after 100 PtrTo and Elem, have %s, want %s", typ, Typeof(i))
1409 }
1410}
1411
1412func TestAddr(t *testing.T) {
1413 var p struct {
1414 X, Y int
1415 }
1416
1417 v := NewValue(&p)
1418 v = v.(*PtrValue).Elem()
1419 v = v.Addr()
1420 v = v.(*PtrValue).Elem()
1421 v = v.(*StructValue).Field(0)
1422 v.(*IntValue).Set(2)
1423 if p.X != 2 {
1424 t.Errorf("Addr.Elem.Set failed to set value")
1425 }
1426
1427 // Again but take address of the NewValue value.
1428 // Exercises generation of PtrTypes not present in the binary.
1429 v = NewValue(&p)
1430 v = v.Addr()
1431 v = v.(*PtrValue).Elem()
1432 v = v.(*PtrValue).Elem()
1433 v = v.Addr()
1434 v = v.(*PtrValue).Elem()
1435 v = v.(*StructValue).Field(0)
1436 v.(*IntValue).Set(3)
1437 if p.X != 3 {
1438 t.Errorf("Addr.Elem.Set failed to set value")
1439 }
1440
1441 // Starting without pointer we should get changed value
1442 // in interface.
1443 v = NewValue(p)
1444 v0 := v
1445 v = v.Addr()
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")
1451 }
1452 p = v0.Interface().(struct {
1453 X, Y int
1454 })
1455 if p.X != 4 {
1456 t.Errorf("Addr.Elem.Set valued to set value in top value")
1457 }
1458}