blob: 61d7f2c2479344d5712d4df392f44c6654ae10c8 [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 Griesemera05a5462009-11-06 16:33:53 -080041 pair{struct{ x int }{}, "int"},
42 pair{struct{ x int8 }{}, "int8"},
43 pair{struct{ x int16 }{}, "int16"},
44 pair{struct{ x int32 }{}, "int32"},
45 pair{struct{ x int64 }{}, "int64"},
46 pair{struct{ x uint }{}, "uint"},
47 pair{struct{ x uint8 }{}, "uint8"},
48 pair{struct{ x uint16 }{}, "uint16"},
49 pair{struct{ x uint32 }{}, "uint32"},
50 pair{struct{ x uint64 }{}, "uint64"},
51 pair{struct{ x float }{}, "float"},
52 pair{struct{ x float32 }{}, "float32"},
53 pair{struct{ x float64 }{}, "float64"},
54 pair{struct{ x int8 }{}, "int8"},
55 pair{struct{ x (**int8) }{}, "**int8"},
56 pair{struct{ x (**integer) }{}, "**reflect_test.integer"},
57 pair{struct{ x ([32]int32) }{}, "[32]int32"},
58 pair{struct{ x ([]int8) }{}, "[]int8"},
59 pair{struct{ x (map[string]int32) }{}, "map[string] int32"},
60 pair{struct{ x (chan<- string) }{}, "chan<- string"},
Robert Griesemer77334b982009-11-05 14:23:20 -080061 pair{struct {
62 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080063 c chan *int32
64 d float32
65 }
Robert Griesemer7151d232009-11-05 18:27:30 -080066 }{},
67 "struct { c chan *int32; d float32 }",
68 },
Robert Griesemera05a5462009-11-06 16:33:53 -080069 pair{struct{ x (func(a int8, b int32)) }{}, "func(int8, int32)"},
Robert Griesemer77334b982009-11-05 14:23:20 -080070 pair{struct {
71 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080072 c func(chan *integer, *int8)
73 }
Robert Griesemer7151d232009-11-05 18:27:30 -080074 }{},
75 "struct { c func(chan *reflect_test.integer, *int8) }",
76 },
Robert Griesemer77334b982009-11-05 14:23:20 -080077 pair{struct {
78 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080079 a int8
80 b int32
81 }
Robert Griesemer7151d232009-11-05 18:27:30 -080082 }{},
83 "struct { a int8; b int32 }",
84 },
Robert Griesemer77334b982009-11-05 14:23:20 -080085 pair{struct {
86 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080087 a int8
88 b int8
89 c int32
90 }
Robert Griesemer7151d232009-11-05 18:27:30 -080091 }{},
92 "struct { a int8; b int8; c int32 }",
93 },
Robert Griesemer77334b982009-11-05 14:23:20 -080094 pair{struct {
95 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -080096 a int8
97 b int8
98 c int8
99 d int32
100 }
Robert Griesemer7151d232009-11-05 18:27:30 -0800101 }{},
102 "struct { a int8; b int8; c int8; d int32 }",
103 },
Robert Griesemer77334b982009-11-05 14:23:20 -0800104 pair{struct {
105 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800106 a int8
107 b int8
108 c int8
109 d int8
110 e int32
111 }
Robert Griesemer7151d232009-11-05 18:27:30 -0800112 }{},
113 "struct { a int8; b int8; c int8; d int8; e int32 }",
114 },
Robert Griesemer77334b982009-11-05 14:23:20 -0800115 pair{struct {
116 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800117 a int8
118 b int8
119 c int8
120 d int8
121 e int8
122 f int32
123 }
Robert Griesemer7151d232009-11-05 18:27:30 -0800124 }{},
125 "struct { a int8; b int8; c int8; d int8; e int8; f int32 }",
126 },
Robert Griesemer77334b982009-11-05 14:23:20 -0800127 pair{struct {
128 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800129 a int8 "hi there"
130 }
Robert Griesemer7151d232009-11-05 18:27:30 -0800131 }{},
132 `struct { a int8 "hi there" }`,
133 },
Robert Griesemer77334b982009-11-05 14:23:20 -0800134 pair{struct {
135 x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800136 a int8 "hi \x00there\t\n\"\\"
137 }
Robert Griesemer7151d232009-11-05 18:27:30 -0800138 }{},
139 `struct { a int8 "hi \x00there\t\n\"\\" }`,
140 },
Robert Griesemer77334b982009-11-05 14:23:20 -0800141 pair{struct {
142 x struct {
Russ Cox6672b402010-06-14 11:23:11 -0700143 f func(args ...int)
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800144 }
Robert Griesemer7151d232009-11-05 18:27:30 -0800145 }{},
Russ Cox6672b402010-06-14 11:23:11 -0700146 "struct { f func(...int) }",
Robert Griesemer7151d232009-11-05 18:27:30 -0800147 },
Robert Griesemer77334b982009-11-05 14:23:20 -0800148 pair{struct {
149 x (interface {
Robert Griesemer1be05bb2010-02-24 13:24:37 -0800150 a(func(func(int) int) func(func(int)) int)
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800151 b()
152 })
Robert Griesemer7151d232009-11-05 18:27:30 -0800153 }{},
Russ Cox7abb4b32010-01-26 10:40:28 -0800154 "interface { a(func(func(int) int) func(func(int)) int); b() }",
Robert Griesemer7151d232009-11-05 18:27:30 -0800155 },
Russ Cox64f4e0b2009-07-07 11:03:12 -0700156}
157
Robert Griesemer77334b982009-11-05 14:23:20 -0800158var valueTests = []pair{
159 pair{(int8)(0), "8"},
160 pair{(int16)(0), "16"},
161 pair{(int32)(0), "32"},
162 pair{(int64)(0), "64"},
163 pair{(uint8)(0), "8"},
164 pair{(uint16)(0), "16"},
165 pair{(uint32)(0), "32"},
166 pair{(uint64)(0), "64"},
Russ Cox45bdf032010-06-20 12:16:25 -0700167 pair{(float32)(0), "256.25"},
168 pair{(float64)(0), "512.125"},
Robert Griesemer77334b982009-11-05 14:23:20 -0800169 pair{(string)(""), "stringy cheese"},
170 pair{(bool)(false), "true"},
171 pair{(*int8)(nil), "*int8(0)"},
172 pair{(**int8)(nil), "**int8(0)"},
Russ Coxc6cb3032010-08-03 00:53:32 -0700173 pair{[5]int32{}, "[5]int32{0, 0, 0, 0, 0}"},
Robert Griesemer77334b982009-11-05 14:23:20 -0800174 pair{(**integer)(nil), "**reflect_test.integer(0)"},
175 pair{(map[string]int32)(nil), "map[string] int32{<can't iterate on maps>}"},
176 pair{(chan<- string)(nil), "chan<- string"},
Russ Coxc6cb3032010-08-03 00:53:32 -0700177 pair{struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800178 c chan *int32
179 d float32
Russ Coxc6cb3032010-08-03 00:53:32 -0700180 }{},
Robert Griesemer7151d232009-11-05 18:27:30 -0800181 "struct { c chan *int32; d float32 }{chan *int32, 0}",
182 },
Robert Griesemer77334b982009-11-05 14:23:20 -0800183 pair{(func(a int8, b int32))(nil), "func(int8, int32)(0)"},
Russ Coxc6cb3032010-08-03 00:53:32 -0700184 pair{struct{ c func(chan *integer, *int8) }{},
Robert Griesemer7151d232009-11-05 18:27:30 -0800185 "struct { c func(chan *reflect_test.integer, *int8) }{func(chan *reflect_test.integer, *int8)(0)}",
186 },
Russ Coxc6cb3032010-08-03 00:53:32 -0700187 pair{struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800188 a int8
189 b int32
Russ Coxc6cb3032010-08-03 00:53:32 -0700190 }{},
Robert Griesemer7151d232009-11-05 18:27:30 -0800191 "struct { a int8; b int32 }{0, 0}",
192 },
Russ Coxc6cb3032010-08-03 00:53:32 -0700193 pair{struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800194 a int8
195 b int8
196 c int32
Russ Coxc6cb3032010-08-03 00:53:32 -0700197 }{},
Robert Griesemer7151d232009-11-05 18:27:30 -0800198 "struct { a int8; b int8; c int32 }{0, 0, 0}",
199 },
Russ Cox64f4e0b2009-07-07 11:03:12 -0700200}
201
202func testType(t *testing.T, i int, typ Type, want string) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800203 s := typ.String()
Russ Cox64f4e0b2009-07-07 11:03:12 -0700204 if s != want {
Robert Griesemer40621d52009-11-09 12:07:39 -0800205 t.Errorf("#%d: have %#q, want %#q", i, s, want)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700206 }
207}
208
209func TestTypes(t *testing.T) {
210 for i, tt := range typeTests {
Robert Griesemer40621d52009-11-09 12:07:39 -0800211 testType(t, i, NewValue(tt.i).(*StructValue).Field(0).Type(), tt.s)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700212 }
213}
214
Adam Langleya8a678f2009-10-21 19:51:27 -0700215func TestSet(t *testing.T) {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700216 for i, tt := range valueTests {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800217 v := NewValue(tt.i)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700218 switch v := v.(type) {
Russ Cox3b864e42009-08-12 13:18:37 -0700219 case *IntValue:
Russ Cox45bdf032010-06-20 12:16:25 -0700220 switch v.Type().Kind() {
221 case Int:
222 v.Set(132)
223 case Int8:
224 v.Set(8)
225 case Int16:
226 v.Set(16)
227 case Int32:
228 v.Set(32)
229 case Int64:
230 v.Set(64)
231 }
Russ Cox3b864e42009-08-12 13:18:37 -0700232 case *UintValue:
Russ Cox45bdf032010-06-20 12:16:25 -0700233 switch v.Type().Kind() {
234 case Uint:
235 v.Set(132)
236 case Uint8:
237 v.Set(8)
238 case Uint16:
239 v.Set(16)
240 case Uint32:
241 v.Set(32)
242 case Uint64:
243 v.Set(64)
244 }
Russ Cox3b864e42009-08-12 13:18:37 -0700245 case *FloatValue:
Russ Cox45bdf032010-06-20 12:16:25 -0700246 switch v.Type().Kind() {
247 case Float:
248 v.Set(128.5)
249 case Float32:
250 v.Set(256.25)
251 case Float64:
252 v.Set(512.125)
253 }
254 case *ComplexValue:
255 switch v.Type().Kind() {
256 case Complex:
257 v.Set(53200.0 + 100i)
258 case Complex64:
259 v.Set(532.125 + 10i)
260 case Complex128:
261 v.Set(564.25 + 1i)
262 }
Russ Cox3b864e42009-08-12 13:18:37 -0700263 case *StringValue:
Robert Griesemer40621d52009-11-09 12:07:39 -0800264 v.Set("stringy cheese")
Russ Cox3b864e42009-08-12 13:18:37 -0700265 case *BoolValue:
Robert Griesemer40621d52009-11-09 12:07:39 -0800266 v.Set(true)
Rob Pike536b1f22008-10-23 17:13:34 -0700267 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800268 s := valueToString(v)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700269 if s != tt.s {
Robert Griesemer40621d52009-11-09 12:07:39 -0800270 t.Errorf("#%d: have %#q, want %#q", i, s, tt.s)
Rob Pike536b1f22008-10-23 17:13:34 -0700271 }
272 }
Rob Pike536b1f22008-10-23 17:13:34 -0700273}
274
Adam Langleya8a678f2009-10-21 19:51:27 -0700275func TestSetValue(t *testing.T) {
276 for i, tt := range valueTests {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800277 v := NewValue(tt.i)
Adam Langleya8a678f2009-10-21 19:51:27 -0700278 switch v := v.(type) {
279 case *IntValue:
Russ Cox45bdf032010-06-20 12:16:25 -0700280 switch v.Type().Kind() {
281 case Int:
282 v.SetValue(NewValue(int(132)))
283 case Int8:
284 v.SetValue(NewValue(int8(8)))
285 case Int16:
286 v.SetValue(NewValue(int16(16)))
287 case Int32:
288 v.SetValue(NewValue(int32(32)))
289 case Int64:
290 v.SetValue(NewValue(int64(64)))
291 }
Adam Langleya8a678f2009-10-21 19:51:27 -0700292 case *UintValue:
Russ Cox45bdf032010-06-20 12:16:25 -0700293 switch v.Type().Kind() {
294 case Uint:
295 v.SetValue(NewValue(uint(132)))
296 case Uint8:
297 v.SetValue(NewValue(uint8(8)))
298 case Uint16:
299 v.SetValue(NewValue(uint16(16)))
300 case Uint32:
301 v.SetValue(NewValue(uint32(32)))
302 case Uint64:
303 v.SetValue(NewValue(uint64(64)))
304 }
Adam Langleya8a678f2009-10-21 19:51:27 -0700305 case *FloatValue:
Russ Cox45bdf032010-06-20 12:16:25 -0700306 switch v.Type().Kind() {
307 case Float:
308 v.SetValue(NewValue(float(128.5)))
309 case Float32:
310 v.SetValue(NewValue(float32(256.25)))
311 case Float64:
312 v.SetValue(NewValue(float64(512.125)))
313 }
314 case *ComplexValue:
315 switch v.Type().Kind() {
316 case Complex:
317 v.SetValue(NewValue(complex(53200.0 + 100i)))
318 case Complex64:
319 v.SetValue(NewValue(complex64(532.125 + 10i)))
320 case Complex128:
321 v.SetValue(NewValue(complex128(564.25 + 1i)))
322 }
323
Adam Langleya8a678f2009-10-21 19:51:27 -0700324 case *StringValue:
Robert Griesemer40621d52009-11-09 12:07:39 -0800325 v.SetValue(NewValue("stringy cheese"))
Adam Langleya8a678f2009-10-21 19:51:27 -0700326 case *BoolValue:
Robert Griesemer40621d52009-11-09 12:07:39 -0800327 v.SetValue(NewValue(true))
Adam Langleya8a678f2009-10-21 19:51:27 -0700328 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800329 s := valueToString(v)
Adam Langleya8a678f2009-10-21 19:51:27 -0700330 if s != tt.s {
Robert Griesemer40621d52009-11-09 12:07:39 -0800331 t.Errorf("#%d: have %#q, want %#q", i, s, tt.s)
Adam Langleya8a678f2009-10-21 19:51:27 -0700332 }
333 }
334}
335
Robert Griesemer77334b982009-11-05 14:23:20 -0800336var _i = 7
Russ Cox64f4e0b2009-07-07 11:03:12 -0700337
Robert Griesemer77334b982009-11-05 14:23:20 -0800338var valueToStringTests = []pair{
339 pair{123, "123"},
Russ Cox45bdf032010-06-20 12:16:25 -0700340 pair{123.5, "123.5"},
Robert Griesemer77334b982009-11-05 14:23:20 -0800341 pair{byte(123), "123"},
342 pair{"abc", "abc"},
343 pair{T{123, 456.75, "hello", &_i}, "reflect_test.T{123, 456.75, hello, *int(&7)}"},
344 pair{new(chan *T), "*chan *reflect_test.T(&chan *reflect_test.T)"},
345 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}"},
346 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})"},
347 pair{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"},
348 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 -0700349}
350
Russ Cox64f4e0b2009-07-07 11:03:12 -0700351func TestValueToString(t *testing.T) {
352 for i, test := range valueToStringTests {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800353 s := valueToString(NewValue(test.i))
Russ Cox64f4e0b2009-07-07 11:03:12 -0700354 if s != test.s {
Robert Griesemer40621d52009-11-09 12:07:39 -0800355 t.Errorf("#%d: have %#q, want %#q", i, s, test.s)
Rob Pike419e1e02008-11-12 19:05:05 -0800356 }
357 }
Russ Cox64f4e0b2009-07-07 11:03:12 -0700358}
Rob Pike419e1e02008-11-12 19:05:05 -0800359
Russ Cox64f4e0b2009-07-07 11:03:12 -0700360func TestArrayElemSet(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800361 v := NewValue([10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
362 v.(*ArrayValue).Elem(4).(*IntValue).Set(123)
363 s := valueToString(v)
364 const want = "[10]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"
Russ Cox64f4e0b2009-07-07 11:03:12 -0700365 if s != want {
Robert Griesemer40621d52009-11-09 12:07:39 -0800366 t.Errorf("[10]int: have %#q want %#q", s, want)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700367 }
Rob Pikea45f9472008-11-04 22:54:11 -0800368
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800369 v = NewValue([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
370 v.(*SliceValue).Elem(4).(*IntValue).Set(123)
371 s = valueToString(v)
372 const want1 = "[]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"
Russ Cox64f4e0b2009-07-07 11:03:12 -0700373 if s != want1 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800374 t.Errorf("[]int: have %#q want %#q", s, want1)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700375 }
376}
Rob Pikea45f9472008-11-04 22:54:11 -0800377
Russ Cox64f4e0b2009-07-07 11:03:12 -0700378func TestPtrPointTo(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800379 var ip *int32
380 var i int32 = 1234
381 vip := NewValue(&ip)
382 vi := NewValue(i)
383 vip.(*PtrValue).Elem().(*PtrValue).PointTo(vi)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700384 if *ip != 1234 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800385 t.Errorf("got %d, want 1234", *ip)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700386 }
Robert Griesemera48b35e2010-08-17 15:12:28 -0700387
388 ip = nil
389 vp := NewValue(ip).(*PtrValue)
390 vp.PointTo(vp.Elem())
391 if ip != nil {
392 t.Errorf("got non-nil (%p), want nil", ip)
393 }
Russ Cox64f4e0b2009-07-07 11:03:12 -0700394}
Rob Pikea45f9472008-11-04 22:54:11 -0800395
Russ Cox7295b612010-04-20 17:02:08 -0700396func TestPtrSetNil(t *testing.T) {
397 var i int32 = 1234
398 ip := &i
399 vip := NewValue(&ip)
400 vip.(*PtrValue).Elem().(*PtrValue).Set(nil)
401 if ip != nil {
402 t.Errorf("got non-nil (%d), want nil", *ip)
403 }
404}
405
406func TestMapSetNil(t *testing.T) {
407 m := make(map[string]int)
408 vm := NewValue(&m)
409 vm.(*PtrValue).Elem().(*MapValue).Set(nil)
410 if m != nil {
411 t.Errorf("got non-nil (%p), want nil", m)
412 }
413}
414
415
Rob Pikee02f2b52009-11-08 21:57:59 -0800416func TestAll(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800417 testType(t, 1, Typeof((int8)(0)), "int8")
418 testType(t, 2, Typeof((*int8)(nil)).(*PtrType).Elem(), "int8")
Rob Pikea45f9472008-11-04 22:54:11 -0800419
Robert Griesemer77334b982009-11-05 14:23:20 -0800420 typ := Typeof((*struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800421 c chan *int32
422 d float32
423 })(nil))
424 testType(t, 3, typ, "*struct { c chan *int32; d float32 }")
425 etyp := typ.(*PtrType).Elem()
426 testType(t, 4, etyp, "struct { c chan *int32; d float32 }")
427 styp := etyp.(*StructType)
428 f := styp.Field(0)
429 testType(t, 5, f.Type, "chan *int32")
Rob Pikea93c5c82009-07-16 18:21:14 -0700430
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800431 f, present := styp.FieldByName("d")
Rob Pikea93c5c82009-07-16 18:21:14 -0700432 if !present {
Robert Griesemer40621d52009-11-09 12:07:39 -0800433 t.Errorf("FieldByName says present field is absent")
Rob Pikea93c5c82009-07-16 18:21:14 -0700434 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800435 testType(t, 6, f.Type, "float32")
Rob Pikea45f9472008-11-04 22:54:11 -0800436
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800437 f, present = styp.FieldByName("absent")
Rob Pikea93c5c82009-07-16 18:21:14 -0700438 if present {
Robert Griesemer40621d52009-11-09 12:07:39 -0800439 t.Errorf("FieldByName says absent field is present")
Rob Pikea93c5c82009-07-16 18:21:14 -0700440 }
441
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800442 typ = Typeof([32]int32{})
443 testType(t, 7, typ, "[32]int32")
444 testType(t, 8, typ.(*ArrayType).Elem(), "int32")
Rob Pikea45f9472008-11-04 22:54:11 -0800445
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800446 typ = Typeof((map[string]*int32)(nil))
447 testType(t, 9, typ, "map[string] *int32")
448 mtyp := typ.(*MapType)
449 testType(t, 10, mtyp.Key(), "string")
450 testType(t, 11, mtyp.Elem(), "*int32")
Rob Pikebdbb9582008-11-05 08:17:01 -0800451
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800452 typ = Typeof((chan<- string)(nil))
453 testType(t, 12, typ, "chan<- string")
454 testType(t, 13, typ.(*ChanType).Elem(), "string")
Rob Pike5a1cbe82008-11-05 13:01:33 -0800455
456 // make sure tag strings are not part of element type
Robert Griesemer77334b982009-11-05 14:23:20 -0800457 typ = Typeof(struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800458 d []uint32 "TAG"
459 }{}).(*StructType).Field(0).Type
460 testType(t, 14, typ, "[]uint32")
Rob Pike536b1f22008-10-23 17:13:34 -0700461}
Russ Cox387df5e2008-11-24 14:51:33 -0800462
Russ Cox839a6842009-01-20 14:40:40 -0800463func TestInterfaceGet(t *testing.T) {
Robert Griesemer77334b982009-11-05 14:23:20 -0800464 var inter struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800465 e interface{}
Robert Griesemer77334b982009-11-05 14:23:20 -0800466 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800467 inter.e = 123.456
468 v1 := NewValue(&inter)
469 v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0)
470 assert(t, v2.Type().String(), "interface { }")
471 i2 := v2.(*InterfaceValue).Interface()
472 v3 := NewValue(i2)
473 assert(t, v3.Type().String(), "float")
Russ Cox387df5e2008-11-24 14:51:33 -0800474}
Russ Coxd0e30cd2008-12-10 15:55:59 -0800475
Russ Coxac6ebfd2009-04-06 21:28:04 -0700476func TestInterfaceValue(t *testing.T) {
Robert Griesemer77334b982009-11-05 14:23:20 -0800477 var inter struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800478 e interface{}
Robert Griesemer77334b982009-11-05 14:23:20 -0800479 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800480 inter.e = 123.456
481 v1 := NewValue(&inter)
482 v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0)
483 assert(t, v2.Type().String(), "interface { }")
484 v3 := v2.(*InterfaceValue).Elem()
485 assert(t, v3.Type().String(), "float")
Russ Cox64627b02009-04-15 00:55:58 -0700486
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800487 i3 := v2.Interface()
Russ Coxca6a0fe2009-09-15 09:41:59 -0700488 if _, ok := i3.(float); !ok {
Robert Griesemer40621d52009-11-09 12:07:39 -0800489 t.Error("v2.Interface() did not return float, got ", Typeof(i3))
Russ Cox4b8c13d2009-04-14 19:03:57 -0700490 }
Russ Coxac6ebfd2009-04-06 21:28:04 -0700491}
492
Ian Lance Taylorca9765d2009-04-14 06:46:01 -0700493func TestFunctionValue(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800494 v := NewValue(func() {})
Ian Lance Taylorca9765d2009-04-14 06:46:01 -0700495 if v.Interface() != v.Interface() {
Robert Griesemer40621d52009-11-09 12:07:39 -0800496 t.Fatalf("TestFunction != itself")
Ian Lance Taylorca9765d2009-04-14 06:46:01 -0700497 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800498 assert(t, v.Type().String(), "func()")
Ian Lance Taylorca9765d2009-04-14 06:46:01 -0700499}
500
Russ Cox839a6842009-01-20 14:40:40 -0800501func TestCopyArray(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800502 a := []int{1, 2, 3, 4, 10, 9, 8, 7}
503 b := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44}
504 c := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44}
505 va := NewValue(&a)
506 vb := NewValue(&b)
Russ Coxd0e30cd2008-12-10 15:55:59 -0800507 for i := 0; i < len(b); i++ {
508 if b[i] != c[i] {
Robert Griesemer40621d52009-11-09 12:07:39 -0800509 t.Fatalf("b != c before test")
Russ Coxd0e30cd2008-12-10 15:55:59 -0800510 }
511 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800512 aa := va.(*PtrValue).Elem().(*SliceValue)
513 ab := vb.(*PtrValue).Elem().(*SliceValue)
Russ Coxd47d8882008-12-18 22:37:22 -0800514 for tocopy := 1; tocopy <= 7; tocopy++ {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800515 aa.SetLen(tocopy)
516 ArrayCopy(ab, aa)
517 aa.SetLen(8)
Russ Coxd0e30cd2008-12-10 15:55:59 -0800518 for i := 0; i < tocopy; i++ {
519 if a[i] != b[i] {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700520 t.Errorf("(i) tocopy=%d a[%d]=%d, b[%d]=%d",
Robert Griesemer40621d52009-11-09 12:07:39 -0800521 tocopy, i, a[i], i, b[i])
Russ Coxd0e30cd2008-12-10 15:55:59 -0800522 }
523 }
524 for i := tocopy; i < len(b); i++ {
525 if b[i] != c[i] {
526 if i < len(a) {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700527 t.Errorf("(ii) tocopy=%d a[%d]=%d, b[%d]=%d, c[%d]=%d",
Robert Griesemer40621d52009-11-09 12:07:39 -0800528 tocopy, i, a[i], i, b[i], i, c[i])
Russ Coxd0e30cd2008-12-10 15:55:59 -0800529 } else {
Russ Cox64f4e0b2009-07-07 11:03:12 -0700530 t.Errorf("(iii) tocopy=%d b[%d]=%d, c[%d]=%d",
Robert Griesemer40621d52009-11-09 12:07:39 -0800531 tocopy, i, b[i], i, c[i])
Russ Coxd0e30cd2008-12-10 15:55:59 -0800532 }
Russ Coxd47d8882008-12-18 22:37:22 -0800533 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -0800534 t.Logf("tocopy=%d elem %d is okay\n", tocopy, i)
Russ Coxd0e30cd2008-12-10 15:55:59 -0800535 }
536 }
537 }
538}
Russ Cox484ba932009-01-09 00:17:46 -0800539
Russ Cox839a6842009-01-20 14:40:40 -0800540func TestBigUnnamedStruct(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800541 b := struct{ a, b, c, d int64 }{1, 2, 3, 4}
542 v := NewValue(b)
Robert Griesemer77334b982009-11-05 14:23:20 -0800543 b1 := v.Interface().(struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800544 a, b, c, d int64
545 })
Russ Cox484ba932009-01-09 00:17:46 -0800546 if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d {
Robert Griesemer40621d52009-11-09 12:07:39 -0800547 t.Errorf("NewValue(%v).Interface().(*Big) = %v", b, b1)
Russ Cox484ba932009-01-09 00:17:46 -0800548 }
549}
550
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800551type big struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800552 a, b, c, d, e int64
Russ Cox484ba932009-01-09 00:17:46 -0800553}
Robert Griesemer77334b982009-11-05 14:23:20 -0800554
Russ Cox839a6842009-01-20 14:40:40 -0800555func TestBigStruct(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800556 b := big{1, 2, 3, 4, 5}
557 v := NewValue(b)
558 b1 := v.Interface().(big)
Russ Cox484ba932009-01-09 00:17:46 -0800559 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 -0800560 t.Errorf("NewValue(%v).Interface().(big) = %v", b, b1)
Russ Cox484ba932009-01-09 00:17:46 -0800561 }
562}
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700563
564type Basic struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800565 x int
566 y float32
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700567}
568
Russ Cox64627b02009-04-15 00:55:58 -0700569type NotBasic Basic
570
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700571type DeepEqualTest struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800572 a, b interface{}
573 eq bool
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700574}
575
Robert Griesemer77334b982009-11-05 14:23:20 -0800576var deepEqualTests = []DeepEqualTest{
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700577 // Equalities
Robert Griesemer77334b982009-11-05 14:23:20 -0800578 DeepEqualTest{1, 1, true},
579 DeepEqualTest{int32(1), int32(1), true},
580 DeepEqualTest{0.5, 0.5, true},
581 DeepEqualTest{float32(0.5), float32(0.5), true},
582 DeepEqualTest{"hello", "hello", true},
583 DeepEqualTest{make([]int, 10), make([]int, 10), true},
584 DeepEqualTest{&[3]int{1, 2, 3}, &[3]int{1, 2, 3}, true},
585 DeepEqualTest{Basic{1, 0.5}, Basic{1, 0.5}, true},
586 DeepEqualTest{os.Error(nil), os.Error(nil), true},
587 DeepEqualTest{map[int]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}, true},
Russ Coxa439f662009-07-01 16:45:09 -0700588
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700589 // Inequalities
Robert Griesemer77334b982009-11-05 14:23:20 -0800590 DeepEqualTest{1, 2, false},
591 DeepEqualTest{int32(1), int32(2), false},
592 DeepEqualTest{0.5, 0.6, false},
593 DeepEqualTest{float32(0.5), float32(0.6), false},
594 DeepEqualTest{"hello", "hey", false},
595 DeepEqualTest{make([]int, 10), make([]int, 11), false},
596 DeepEqualTest{&[3]int{1, 2, 3}, &[3]int{1, 2, 4}, false},
597 DeepEqualTest{Basic{1, 0.5}, Basic{1, 0.6}, false},
598 DeepEqualTest{Basic{1, 0}, Basic{2, 0}, false},
599 DeepEqualTest{map[int]string{1: "one", 3: "two"}, map[int]string{2: "two", 1: "one"}, false},
600 DeepEqualTest{map[int]string{1: "one", 2: "txo"}, map[int]string{2: "two", 1: "one"}, false},
601 DeepEqualTest{map[int]string{1: "one"}, map[int]string{2: "two", 1: "one"}, false},
602 DeepEqualTest{map[int]string{2: "two", 1: "one"}, map[int]string{1: "one"}, false},
603 DeepEqualTest{nil, 1, false},
604 DeepEqualTest{1, nil, false},
Russ Coxa439f662009-07-01 16:45:09 -0700605
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700606 // Mismatched types
Robert Griesemer77334b982009-11-05 14:23:20 -0800607 DeepEqualTest{1, 1.0, false},
608 DeepEqualTest{int32(1), int64(1), false},
609 DeepEqualTest{0.5, "hello", false},
610 DeepEqualTest{[]int{1, 2, 3}, [3]int{1, 2, 3}, false},
611 DeepEqualTest{&[3]interface{}{1, 2, 4}, &[3]interface{}{1, 2, "s"}, false},
612 DeepEqualTest{Basic{1, 0.5}, NotBasic{1, 0.5}, false},
613 DeepEqualTest{map[uint]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}, false},
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700614}
615
616func TestDeepEqual(t *testing.T) {
Russ Coxca6a0fe2009-09-15 09:41:59 -0700617 for _, test := range deepEqualTests {
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700618 if r := DeepEqual(test.a, test.b); r != test.eq {
Robert Griesemer40621d52009-11-09 12:07:39 -0800619 t.Errorf("DeepEqual(%v, %v) = %v, want %v", test.a, test.b, r, test.eq)
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700620 }
621 }
622}
623
Russ Cox48662232009-06-25 14:25:38 -0700624func TestTypeof(t *testing.T) {
Russ Coxca6a0fe2009-09-15 09:41:59 -0700625 for _, test := range deepEqualTests {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800626 v := NewValue(test.a)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700627 if v == nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800628 continue
Russ Cox64f4e0b2009-07-07 11:03:12 -0700629 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800630 typ := Typeof(test.a)
Russ Cox48662232009-06-25 14:25:38 -0700631 if typ != v.Type() {
Robert Griesemer40621d52009-11-09 12:07:39 -0800632 t.Errorf("Typeof(%v) = %v, but NewValue(%v).Type() = %v", test.a, typ, test.a, v.Type())
Russ Cox48662232009-06-25 14:25:38 -0700633 }
634 }
635}
636
Rob Pike1880b902009-07-10 11:20:10 -0700637type Recursive struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800638 x int
639 r *Recursive
Rob Pike1880b902009-07-10 11:20:10 -0700640}
641
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700642func TestDeepEqualRecursiveStruct(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800643 a, b := new(Recursive), new(Recursive)
644 *a = Recursive{12, a}
645 *b = Recursive{12, b}
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700646 if !DeepEqual(a, b) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800647 t.Error("DeepEqual(recursive same) = false, want true")
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700648 }
649}
650
Russ Cox45bdf032010-06-20 12:16:25 -0700651type _Complex struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800652 a int
Russ Cox45bdf032010-06-20 12:16:25 -0700653 b [3]*_Complex
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800654 c *string
655 d map[float]float
Rob Pike1880b902009-07-10 11:20:10 -0700656}
657
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700658func TestDeepEqualComplexStruct(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800659 m := make(map[float]float)
660 stra, strb := "hello", "hello"
Russ Cox45bdf032010-06-20 12:16:25 -0700661 a, b := new(_Complex), new(_Complex)
662 *a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m}
663 *b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m}
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700664 if !DeepEqual(a, b) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800665 t.Error("DeepEqual(complex same) = false, want true")
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700666 }
667}
668
669func TestDeepEqualComplexStructInequality(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800670 m := make(map[float]float)
671 stra, strb := "hello", "helloo" // Difference is here
Russ Cox45bdf032010-06-20 12:16:25 -0700672 a, b := new(_Complex), new(_Complex)
673 *a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m}
674 *b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m}
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700675 if DeepEqual(a, b) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800676 t.Error("DeepEqual(complex different) = true, want false")
Daniel Nadasic4ad4f92009-04-01 22:20:18 -0700677 }
678}
Rob Pike93831d22009-04-29 22:16:53 -0700679
680
681func check2ndField(x interface{}, offs uintptr, t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800682 s := NewValue(x).(*StructValue)
683 f := s.Type().(*StructType).Field(1)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700684 if f.Offset != offs {
Robert Griesemer40621d52009-11-09 12:07:39 -0800685 t.Error("mismatched offsets in structure alignment:", f.Offset, offs)
Rob Pike93831d22009-04-29 22:16:53 -0700686 }
687}
688
689// Check that structure alignment & offsets viewed through reflect agree with those
690// from the compiler itself.
691func TestAlignment(t *testing.T) {
692 type T1inner struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800693 a int
Rob Pike93831d22009-04-29 22:16:53 -0700694 }
695 type T1 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800696 T1inner
697 f int
Rob Pike93831d22009-04-29 22:16:53 -0700698 }
699 type T2inner struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800700 a, b int
Rob Pike93831d22009-04-29 22:16:53 -0700701 }
702 type T2 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800703 T2inner
704 f int
Rob Pike93831d22009-04-29 22:16:53 -0700705 }
706
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800707 x := T1{T1inner{2}, 17}
708 check2ndField(x, uintptr(unsafe.Pointer(&x.f))-uintptr(unsafe.Pointer(&x)), t)
Rob Pike93831d22009-04-29 22:16:53 -0700709
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800710 x1 := T2{T2inner{2, 3}, 17}
711 check2ndField(x1, uintptr(unsafe.Pointer(&x1.f))-uintptr(unsafe.Pointer(&x1)), t)
Rob Pike93831d22009-04-29 22:16:53 -0700712}
Rob Pikea8f6e382009-05-12 14:57:44 -0700713
Russ Cox64f4e0b2009-07-07 11:03:12 -0700714type IsNiller interface {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800715 IsNil() bool
Rob Pikea8f6e382009-05-12 14:57:44 -0700716}
717
718func Nil(a interface{}, t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800719 n := NewValue(a).(*StructValue).Field(0).(IsNiller)
Rob Pikea8f6e382009-05-12 14:57:44 -0700720 if !n.IsNil() {
Robert Griesemer40621d52009-11-09 12:07:39 -0800721 t.Errorf("%v should be nil", a)
Rob Pikea8f6e382009-05-12 14:57:44 -0700722 }
723}
724
725func NotNil(a interface{}, t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800726 n := NewValue(a).(*StructValue).Field(0).(IsNiller)
Rob Pikea8f6e382009-05-12 14:57:44 -0700727 if n.IsNil() {
Robert Griesemer40621d52009-11-09 12:07:39 -0800728 t.Errorf("value of type %v should not be nil", NewValue(a).Type().String())
Rob Pikea8f6e382009-05-12 14:57:44 -0700729 }
730}
731
732func TestIsNil(t *testing.T) {
733 // These do not implement IsNil
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800734 doNotNil := []interface{}{int(0), float32(0), struct{ a int }{}}
Russ Coxca6a0fe2009-09-15 09:41:59 -0700735 for _, ts := range doNotNil {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800736 ty := Typeof(ts)
737 v := MakeZero(ty)
Russ Coxca6a0fe2009-09-15 09:41:59 -0700738 if _, ok := v.(IsNiller); ok {
Robert Griesemer40621d52009-11-09 12:07:39 -0800739 t.Errorf("%s is nilable; should not be", ts)
Rob Pikea8f6e382009-05-12 14:57:44 -0700740 }
741 }
742
Russ Cox64f4e0b2009-07-07 11:03:12 -0700743 // These do implement IsNil.
744 // Wrap in extra struct to hide interface type.
745 doNil := []interface{}{
Robert Griesemera05a5462009-11-06 16:33:53 -0800746 struct{ x *int }{},
747 struct{ x interface{} }{},
748 struct{ x map[string]int }{},
749 struct{ x func() bool }{},
750 struct{ x chan int }{},
751 struct{ x []string }{},
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800752 }
Russ Coxca6a0fe2009-09-15 09:41:59 -0700753 for _, ts := range doNil {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800754 ty := Typeof(ts).(*StructType).Field(0).Type
755 v := MakeZero(ty)
Russ Coxca6a0fe2009-09-15 09:41:59 -0700756 if _, ok := v.(IsNiller); !ok {
Robert Griesemer40621d52009-11-09 12:07:39 -0800757 t.Errorf("%s %T is not nilable; should be", ts, v)
Rob Pikea8f6e382009-05-12 14:57:44 -0700758 }
759 }
Russ Cox64f4e0b2009-07-07 11:03:12 -0700760
Rob Pikea8f6e382009-05-12 14:57:44 -0700761 // Check the implementations
Robert Griesemer77334b982009-11-05 14:23:20 -0800762 var pi struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800763 x *int
Robert Griesemer77334b982009-11-05 14:23:20 -0800764 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800765 Nil(pi, t)
766 pi.x = new(int)
767 NotNil(pi, t)
Rob Pikea8f6e382009-05-12 14:57:44 -0700768
Robert Griesemer77334b982009-11-05 14:23:20 -0800769 var si struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800770 x []int
Robert Griesemer77334b982009-11-05 14:23:20 -0800771 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800772 Nil(si, t)
773 si.x = make([]int, 10)
774 NotNil(si, t)
Rob Pikea8f6e382009-05-12 14:57:44 -0700775
Robert Griesemer77334b982009-11-05 14:23:20 -0800776 var ci struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800777 x chan int
Robert Griesemer77334b982009-11-05 14:23:20 -0800778 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800779 Nil(ci, t)
780 ci.x = make(chan int)
781 NotNil(ci, t)
Rob Pikea8f6e382009-05-12 14:57:44 -0700782
Robert Griesemer77334b982009-11-05 14:23:20 -0800783 var mi struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800784 x map[int]int
Robert Griesemer77334b982009-11-05 14:23:20 -0800785 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800786 Nil(mi, t)
787 mi.x = make(map[int]int)
788 NotNil(mi, t)
Russ Cox64f4e0b2009-07-07 11:03:12 -0700789
Robert Griesemer77334b982009-11-05 14:23:20 -0800790 var ii struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800791 x interface{}
Robert Griesemer77334b982009-11-05 14:23:20 -0800792 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800793 Nil(ii, t)
794 ii.x = 2
795 NotNil(ii, t)
Rob Pikea8f6e382009-05-12 14:57:44 -0700796
Robert Griesemer77334b982009-11-05 14:23:20 -0800797 var fi struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800798 x func(t *testing.T)
Robert Griesemer77334b982009-11-05 14:23:20 -0800799 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800800 Nil(fi, t)
801 fi.x = TestIsNil
802 NotNil(fi, t)
Rob Pikea8f6e382009-05-12 14:57:44 -0700803}
Russ Cox96cfd152009-05-21 11:50:20 -0700804
805func TestInterfaceExtraction(t *testing.T) {
806 var s struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800807 w io.Writer
Russ Cox96cfd152009-05-21 11:50:20 -0700808 }
809
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800810 s.w = os.Stdout
811 v := Indirect(NewValue(&s)).(*StructValue).Field(0).Interface()
Russ Cox96cfd152009-05-21 11:50:20 -0700812 if v != s.w.(interface{}) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800813 t.Error("Interface() on interface: ", v, s.w)
Russ Cox96cfd152009-05-21 11:50:20 -0700814 }
815}
Russ Coxf9666232009-05-21 14:06:43 -0700816
817func TestInterfaceEditing(t *testing.T) {
818 // strings are bigger than one word,
819 // so the interface conversion allocates
820 // memory to hold a string and puts that
821 // pointer in the interface.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800822 var i interface{} = "hello"
Russ Coxf9666232009-05-21 14:06:43 -0700823
824 // if i pass the interface value by value
825 // to NewValue, i should get a fresh copy
826 // of the value.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800827 v := NewValue(i)
Russ Coxf9666232009-05-21 14:06:43 -0700828
829 // and setting that copy to "bye" should
830 // not change the value stored in i.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800831 v.(*StringValue).Set("bye")
Russ Coxf9666232009-05-21 14:06:43 -0700832 if i.(string) != "hello" {
Robert Griesemer40621d52009-11-09 12:07:39 -0800833 t.Errorf(`Set("bye") changed i to %s`, i.(string))
Russ Coxf9666232009-05-21 14:06:43 -0700834 }
835
836 // the same should be true of smaller items.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800837 i = 123
838 v = NewValue(i)
839 v.(*IntValue).Set(234)
Russ Coxf9666232009-05-21 14:06:43 -0700840 if i.(int) != 123 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800841 t.Errorf("Set(234) changed i to %d", i.(int))
Russ Coxf9666232009-05-21 14:06:43 -0700842 }
843}
David Symondsd4e57ff2009-06-15 18:35:04 -0700844
845func TestNilPtrValueSub(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800846 var pi *int
Russ Cox64f4e0b2009-07-07 11:03:12 -0700847 if pv := NewValue(pi).(*PtrValue); pv.Elem() != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800848 t.Error("NewValue((*int)(nil)).(*PtrValue).Elem() != nil")
David Symondsd4e57ff2009-06-15 18:35:04 -0700849 }
850}
Russ Cox764b6ec2009-07-08 13:55:57 -0700851
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700852func TestMap(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800853 m := map[string]int{"a": 1, "b": 2}
854 mv := NewValue(m).(*MapValue)
Russ Cox764b6ec2009-07-08 13:55:57 -0700855 if n := mv.Len(); n != len(m) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800856 t.Errorf("Len = %d, want %d", n, len(m))
Russ Cox764b6ec2009-07-08 13:55:57 -0700857 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800858 keys := mv.Keys()
859 i := 0
860 newmap := MakeMap(mv.Type().(*MapType))
Russ Cox764b6ec2009-07-08 13:55:57 -0700861 for k, v := range m {
862 // Check that returned Keys match keys in range.
863 // These aren't required to be in the same order,
864 // but they are in this implementation, which makes
865 // the test easier.
866 if i >= len(keys) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800867 t.Errorf("Missing key #%d %q", i, k)
Russ Cox764b6ec2009-07-08 13:55:57 -0700868 } else if kv := keys[i].(*StringValue); kv.Get() != k {
Robert Griesemer40621d52009-11-09 12:07:39 -0800869 t.Errorf("Keys[%d] = %q, want %q", i, kv.Get(), k)
Russ Cox764b6ec2009-07-08 13:55:57 -0700870 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800871 i++
Russ Cox764b6ec2009-07-08 13:55:57 -0700872
873 // Check that value lookup is correct.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800874 vv := mv.Elem(NewValue(k))
Russ Cox45bdf032010-06-20 12:16:25 -0700875 if vi := vv.(*IntValue).Get(); vi != int64(v) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800876 t.Errorf("Key %q: have value %d, want %d", vi, v)
Russ Cox764b6ec2009-07-08 13:55:57 -0700877 }
878
879 // Copy into new map.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800880 newmap.SetElem(NewValue(k), NewValue(v))
Russ Cox764b6ec2009-07-08 13:55:57 -0700881 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800882 vv := mv.Elem(NewValue("not-present"))
Russ Cox764b6ec2009-07-08 13:55:57 -0700883 if vv != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800884 t.Errorf("Invalid key: got non-nil value %s", valueToString(vv))
Russ Cox764b6ec2009-07-08 13:55:57 -0700885 }
886
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800887 newm := newmap.Interface().(map[string]int)
Russ Cox764b6ec2009-07-08 13:55:57 -0700888 if len(newm) != len(m) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800889 t.Errorf("length after copy: newm=%d, m=%d", newm, m)
Russ Cox764b6ec2009-07-08 13:55:57 -0700890 }
891
892 for k, v := range newm {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800893 mv, ok := m[k]
Russ Cox764b6ec2009-07-08 13:55:57 -0700894 if mv != v {
Robert Griesemer40621d52009-11-09 12:07:39 -0800895 t.Errorf("newm[%q] = %d, but m[%q] = %d, %v", k, v, k, mv, ok)
Russ Cox764b6ec2009-07-08 13:55:57 -0700896 }
897 }
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700898
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800899 newmap.SetElem(NewValue("a"), nil)
900 v, ok := newm["a"]
Russ Cox764b6ec2009-07-08 13:55:57 -0700901 if ok {
Robert Griesemer40621d52009-11-09 12:07:39 -0800902 t.Errorf("newm[\"a\"] = %d after delete", v)
Russ Cox764b6ec2009-07-08 13:55:57 -0700903 }
Russ Cox7295b612010-04-20 17:02:08 -0700904
905 mv = NewValue(&m).(*PtrValue).Elem().(*MapValue)
906 mv.Set(nil)
907 if m != nil {
908 t.Errorf("mv.Set(nil) failed")
909 }
Russ Cox764b6ec2009-07-08 13:55:57 -0700910}
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700911
912func TestChan(t *testing.T) {
913 for loop := 0; loop < 2; loop++ {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800914 var c chan int
915 var cv *ChanValue
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700916
917 // check both ways to allocate channels
918 switch loop {
919 case 1:
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800920 c = make(chan int, 1)
921 cv = NewValue(c).(*ChanValue)
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700922 case 0:
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800923 cv = MakeChan(Typeof(c).(*ChanType), 1)
924 c = cv.Interface().(chan int)
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700925 }
926
927 // Send
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800928 cv.Send(NewValue(2))
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700929 if i := <-c; i != 2 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800930 t.Errorf("reflect Send 2, native recv %d", i)
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700931 }
932
933 // Recv
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800934 c <- 3
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700935 if i := cv.Recv().(*IntValue).Get(); i != 3 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800936 t.Errorf("native send 3, reflect Recv %d", i)
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700937 }
938
939 // TryRecv fail
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800940 val := cv.TryRecv()
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700941 if val != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800942 t.Errorf("TryRecv on empty chan: %s", valueToString(val))
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700943 }
944
945 // TryRecv success
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800946 c <- 4
947 val = cv.TryRecv()
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700948 if val == nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800949 t.Errorf("TryRecv on ready chan got nil")
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700950 } else if i := val.(*IntValue).Get(); i != 4 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800951 t.Errorf("native send 4, TryRecv %d", i)
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700952 }
953
954 // TrySend fail
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800955 c <- 100
956 ok := cv.TrySend(NewValue(5))
957 i := <-c
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700958 if ok {
Robert Griesemer40621d52009-11-09 12:07:39 -0800959 t.Errorf("TrySend on full chan succeeded: value %d", i)
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700960 }
961
962 // TrySend success
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800963 ok = cv.TrySend(NewValue(6))
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700964 if !ok {
Robert Griesemer40621d52009-11-09 12:07:39 -0800965 t.Errorf("TrySend on empty chan failed")
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700966 } else {
967 if i = <-c; i != 6 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800968 t.Errorf("TrySend 6, recv %d", i)
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700969 }
970 }
Russ Cox653cef12009-08-26 10:47:18 -0700971
972 // Close
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800973 c <- 123
974 cv.Close()
Russ Cox653cef12009-08-26 10:47:18 -0700975 if cv.Closed() {
Robert Griesemer40621d52009-11-09 12:07:39 -0800976 t.Errorf("closed too soon - 1")
Russ Cox653cef12009-08-26 10:47:18 -0700977 }
978 if i := cv.Recv().(*IntValue).Get(); i != 123 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800979 t.Errorf("send 123 then close; Recv %d", i)
Russ Cox653cef12009-08-26 10:47:18 -0700980 }
981 if cv.Closed() {
Robert Griesemer40621d52009-11-09 12:07:39 -0800982 t.Errorf("closed too soon - 2")
Russ Cox653cef12009-08-26 10:47:18 -0700983 }
984 if i := cv.Recv().(*IntValue).Get(); i != 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800985 t.Errorf("after close Recv %d", i)
Russ Cox653cef12009-08-26 10:47:18 -0700986 }
987 if !cv.Closed() {
Robert Griesemer40621d52009-11-09 12:07:39 -0800988 t.Errorf("not closed")
Russ Cox653cef12009-08-26 10:47:18 -0700989 }
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700990 }
991
992 // check creation of unbuffered channel
Robert Griesemerd65a5cc2009-12-15 15:40:16 -0800993 var c chan int
994 cv := MakeChan(Typeof(c).(*ChanType), 0)
995 c = cv.Interface().(chan int)
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700996 if cv.TrySend(NewValue(7)) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800997 t.Errorf("TrySend on sync chan succeeded")
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700998 }
999 if cv.TryRecv() != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -08001000 t.Errorf("TryRecv on sync chan succeeded")
Russ Cox5ddaf9a2009-07-08 15:00:54 -07001001 }
Russ Coxde7920e2009-08-26 12:42:22 -07001002
1003 // len/cap
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001004 cv = MakeChan(Typeof(c).(*ChanType), 10)
1005 c = cv.Interface().(chan int)
Russ Coxde7920e2009-08-26 12:42:22 -07001006 for i := 0; i < 3; i++ {
Robert Griesemer40621d52009-11-09 12:07:39 -08001007 c <- i
Russ Coxde7920e2009-08-26 12:42:22 -07001008 }
1009 if l, m := cv.Len(), cv.Cap(); l != len(c) || m != cap(c) {
Robert Griesemer40621d52009-11-09 12:07:39 -08001010 t.Errorf("Len/Cap = %d/%d want %d/%d", l, m, len(c), cap(c))
Russ Coxde7920e2009-08-26 12:42:22 -07001011 }
1012
Russ Cox5ddaf9a2009-07-08 15:00:54 -07001013}
1014
Russ Coxbba278a2009-07-08 18:16:09 -07001015// Difficult test for function call because of
1016// implicit padding between arguments.
Robert Griesemer77334b982009-11-05 14:23:20 -08001017func dummy(b byte, c int, d byte) (i byte, j int, k byte) {
Robert Griesemer40621d52009-11-09 12:07:39 -08001018 return b, c, d
Russ Coxbba278a2009-07-08 18:16:09 -07001019}
1020
1021func TestFunc(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001022 ret := NewValue(dummy).(*FuncValue).Call([]Value{NewValue(byte(10)), NewValue(20), NewValue(byte(30))})
Russ Coxbba278a2009-07-08 18:16:09 -07001023 if len(ret) != 3 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001024 t.Fatalf("Call returned %d values, want 3", len(ret))
Russ Coxbba278a2009-07-08 18:16:09 -07001025 }
1026
Russ Cox45bdf032010-06-20 12:16:25 -07001027 i := ret[0].(*UintValue).Get()
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001028 j := ret[1].(*IntValue).Get()
Russ Cox45bdf032010-06-20 12:16:25 -07001029 k := ret[2].(*UintValue).Get()
Russ Coxbba278a2009-07-08 18:16:09 -07001030 if i != 10 || j != 20 || k != 30 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001031 t.Errorf("Call returned %d, %d, %d; want 10, 20, 30", i, j, k)
Russ Coxbba278a2009-07-08 18:16:09 -07001032 }
1033}
Russ Cox12ebbe72009-07-09 17:27:49 -07001034
1035type Point struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001036 x, y int
Russ Cox12ebbe72009-07-09 17:27:49 -07001037}
1038
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001039func (p Point) Dist(scale int) int { return p.x*p.x*scale + p.y*p.y*scale }
Russ Cox12ebbe72009-07-09 17:27:49 -07001040
1041func TestMethod(t *testing.T) {
1042 // Non-curried method of type.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001043 p := Point{3, 4}
1044 i := Typeof(p).Method(0).Func.Call([]Value{NewValue(p), NewValue(10)})[0].(*IntValue).Get()
Russ Cox12ebbe72009-07-09 17:27:49 -07001045 if i != 250 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001046 t.Errorf("Type Method returned %d; want 250", i)
Russ Cox12ebbe72009-07-09 17:27:49 -07001047 }
1048
1049 // Curried method of value.
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001050 i = NewValue(p).Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
Russ Cox12ebbe72009-07-09 17:27:49 -07001051 if i != 250 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001052 t.Errorf("Value Method returned %d; want 250", i)
Russ Cox12ebbe72009-07-09 17:27:49 -07001053 }
1054
1055 // Curried method of interface value.
1056 // Have to wrap interface value in a struct to get at it.
1057 // Passing it to NewValue directly would
1058 // access the underlying Point, not the interface.
Robert Griesemer77334b982009-11-05 14:23:20 -08001059 var s = struct {
1060 x interface {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001061 Dist(int) int
1062 }
1063 }{p}
1064 pv := NewValue(s).(*StructValue).Field(0)
1065 i = pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
Russ Cox12ebbe72009-07-09 17:27:49 -07001066 if i != 250 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001067 t.Errorf("Interface Method returned %d; want 250", i)
Russ Cox12ebbe72009-07-09 17:27:49 -07001068 }
1069}
Russ Cox92e92572009-07-10 16:32:26 -07001070
1071func TestInterfaceSet(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001072 p := &Point{3, 4}
Russ Cox92e92572009-07-10 16:32:26 -07001073
1074 var s struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001075 I interface{}
1076 P interface {
1077 Dist(int) int
1078 }
Russ Cox92e92572009-07-10 16:32:26 -07001079 }
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001080 sv := NewValue(&s).(*PtrValue).Elem().(*StructValue)
1081 sv.Field(0).(*InterfaceValue).Set(NewValue(p))
Russ Cox92e92572009-07-10 16:32:26 -07001082 if q := s.I.(*Point); q != p {
Robert Griesemer40621d52009-11-09 12:07:39 -08001083 t.Errorf("i: have %p want %p", q, p)
Russ Cox92e92572009-07-10 16:32:26 -07001084 }
1085
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001086 pv := sv.Field(1).(*InterfaceValue)
1087 pv.Set(NewValue(p))
Russ Cox92e92572009-07-10 16:32:26 -07001088 if q := s.P.(*Point); q != p {
Robert Griesemer40621d52009-11-09 12:07:39 -08001089 t.Errorf("i: have %p want %p", q, p)
Russ Cox92e92572009-07-10 16:32:26 -07001090 }
Russ Cox3b864e42009-08-12 13:18:37 -07001091
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001092 i := pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
Russ Cox92e92572009-07-10 16:32:26 -07001093 if i != 250 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001094 t.Errorf("Interface Method returned %d; want 250", i)
Russ Cox92e92572009-07-10 16:32:26 -07001095 }
1096}
Robert Griesemera2880952009-08-05 15:56:44 -07001097
Robert Griesemer77334b982009-11-05 14:23:20 -08001098type T1 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001099 a string
1100 int
Robert Griesemer77334b982009-11-05 14:23:20 -08001101}
Robert Griesemera2880952009-08-05 15:56:44 -07001102
1103func TestAnonymousFields(t *testing.T) {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001104 var field StructField
1105 var ok bool
1106 var t1 T1
1107 type1 := Typeof(t1).(*StructType)
Robert Griesemera2880952009-08-05 15:56:44 -07001108 if field, ok = type1.FieldByName("int"); !ok {
Robert Griesemer40621d52009-11-09 12:07:39 -08001109 t.Error("no field 'int'")
Robert Griesemera2880952009-08-05 15:56:44 -07001110 }
1111 if field.Index[0] != 1 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001112 t.Error("field index should be 1; is", field.Index)
Robert Griesemera2880952009-08-05 15:56:44 -07001113 }
1114}
1115
1116type FTest struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001117 s interface{}
1118 name string
1119 index []int
1120 value int
Robert Griesemera2880952009-08-05 15:56:44 -07001121}
1122
Russ Cox92543da2009-08-24 17:04:12 -07001123type D1 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001124 d int
Russ Cox92543da2009-08-24 17:04:12 -07001125}
1126type D2 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001127 d int
Russ Cox92543da2009-08-24 17:04:12 -07001128}
1129
Robert Griesemera2880952009-08-05 15:56:44 -07001130type S0 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001131 a, b, c int
1132 D1
1133 D2
Robert Griesemera2880952009-08-05 15:56:44 -07001134}
1135
1136type S1 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001137 b int
1138 S0
Robert Griesemera2880952009-08-05 15:56:44 -07001139}
1140
1141type S2 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001142 a int
1143 *S1
Robert Griesemera2880952009-08-05 15:56:44 -07001144}
1145
Russ Cox92543da2009-08-24 17:04:12 -07001146type S1x struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001147 S1
Russ Cox92543da2009-08-24 17:04:12 -07001148}
1149
1150type S1y struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001151 S1
Russ Cox92543da2009-08-24 17:04:12 -07001152}
1153
1154type S3 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001155 S1x
1156 S2
1157 d, e int
1158 *S1y
Robert Griesemera2880952009-08-05 15:56:44 -07001159}
1160
1161type S4 struct {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001162 *S4
1163 a int
Robert Griesemera2880952009-08-05 15:56:44 -07001164}
1165
Robert Griesemer77334b982009-11-05 14:23:20 -08001166var fieldTests = []FTest{
1167 FTest{struct{}{}, "", nil, 0},
1168 FTest{struct{}{}, "foo", nil, 0},
1169 FTest{S0{a: 'a'}, "a", []int{0}, 'a'},
1170 FTest{S0{}, "d", nil, 0},
1171 FTest{S1{S0: S0{a: 'a'}}, "a", []int{1, 0}, 'a'},
1172 FTest{S1{b: 'b'}, "b", []int{0}, 'b'},
1173 FTest{S1{}, "S0", []int{1}, 0},
1174 FTest{S1{S0: S0{c: 'c'}}, "c", []int{1, 2}, 'c'},
1175 FTest{S2{a: 'a'}, "a", []int{0}, 'a'},
1176 FTest{S2{}, "S1", []int{1}, 0},
1177 FTest{S2{S1: &S1{b: 'b'}}, "b", []int{1, 0}, 'b'},
1178 FTest{S2{S1: &S1{S0: S0{c: 'c'}}}, "c", []int{1, 1, 2}, 'c'},
1179 FTest{S2{}, "d", nil, 0},
1180 FTest{S3{}, "S1", nil, 0},
1181 FTest{S3{S2: S2{a: 'a'}}, "a", []int{1, 0}, 'a'},
1182 FTest{S3{}, "b", nil, 0},
1183 FTest{S3{d: 'd'}, "d", []int{2}, 0},
1184 FTest{S3{e: 'e'}, "e", []int{3}, 'e'},
1185 FTest{S4{a: 'a'}, "a", []int{1}, 'a'},
1186 FTest{S4{}, "b", nil, 0},
Robert Griesemera2880952009-08-05 15:56:44 -07001187}
1188
1189func TestFieldByIndex(t *testing.T) {
1190 for _, test := range fieldTests {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001191 s := Typeof(test.s).(*StructType)
1192 f := s.FieldByIndex(test.index)
Robert Griesemera2880952009-08-05 15:56:44 -07001193 if f.Name != "" {
1194 if test.index != nil {
1195 if f.Name != test.name {
Robert Griesemer40621d52009-11-09 12:07:39 -08001196 t.Errorf("%s.%s found; want %s", s.Name(), f.Name, test.name)
Robert Griesemera2880952009-08-05 15:56:44 -07001197 }
1198 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -08001199 t.Errorf("%s.%s found", s.Name(), f.Name)
Robert Griesemera2880952009-08-05 15:56:44 -07001200 }
1201 } else if len(test.index) > 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001202 t.Errorf("%s.%s not found", s.Name(), test.name)
Robert Griesemera2880952009-08-05 15:56:44 -07001203 }
1204
1205 if test.value != 0 {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001206 v := NewValue(test.s).(*StructValue).FieldByIndex(test.index)
Robert Griesemera2880952009-08-05 15:56:44 -07001207 if v != nil {
1208 if x, ok := v.Interface().(int); ok {
1209 if x != test.value {
Robert Griesemer40621d52009-11-09 12:07:39 -08001210 t.Errorf("%s%v is %d; want %d", s.Name(), test.index, x, test.value)
Robert Griesemera2880952009-08-05 15:56:44 -07001211 }
1212 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -08001213 t.Errorf("%s%v value not an int", s.Name(), test.index)
Robert Griesemera2880952009-08-05 15:56:44 -07001214 }
1215 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -08001216 t.Errorf("%s%v value not found", s.Name(), test.index)
Robert Griesemera2880952009-08-05 15:56:44 -07001217 }
1218 }
1219 }
1220}
1221
1222func TestFieldByName(t *testing.T) {
1223 for _, test := range fieldTests {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001224 s := Typeof(test.s).(*StructType)
1225 f, found := s.FieldByName(test.name)
Robert Griesemera2880952009-08-05 15:56:44 -07001226 if found {
1227 if test.index != nil {
1228 // Verify field depth and index.
1229 if len(f.Index) != len(test.index) {
Robert Griesemer40621d52009-11-09 12:07:39 -08001230 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 -07001231 } else {
1232 for i, x := range f.Index {
1233 if x != test.index[i] {
Robert Griesemer40621d52009-11-09 12:07:39 -08001234 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 -07001235 }
1236 }
1237 }
1238 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -08001239 t.Errorf("%s.%s found", s.Name(), f.Name)
Robert Griesemera2880952009-08-05 15:56:44 -07001240 }
1241 } else if len(test.index) > 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -08001242 t.Errorf("%s.%s not found", s.Name(), test.name)
Robert Griesemera2880952009-08-05 15:56:44 -07001243 }
Russ Cox3b864e42009-08-12 13:18:37 -07001244
Robert Griesemera2880952009-08-05 15:56:44 -07001245 if test.value != 0 {
Robert Griesemerd65a5cc2009-12-15 15:40:16 -08001246 v := NewValue(test.s).(*StructValue).FieldByName(test.name)
Robert Griesemera2880952009-08-05 15:56:44 -07001247 if v != nil {
1248 if x, ok := v.Interface().(int); ok {
1249 if x != test.value {
Robert Griesemer40621d52009-11-09 12:07:39 -08001250 t.Errorf("%s.%s is %d; want %d", s.Name(), test.name, x, test.value)
Robert Griesemera2880952009-08-05 15:56:44 -07001251 }
1252 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -08001253 t.Errorf("%s.%s value not an int", s.Name(), test.name)
Robert Griesemera2880952009-08-05 15:56:44 -07001254 }
1255 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -08001256 t.Errorf("%s.%s value not found", s.Name(), test.name)
Robert Griesemera2880952009-08-05 15:56:44 -07001257 }
1258 }
1259 }
1260}
Russ Cox1cecac82010-01-24 23:33:59 -08001261
1262func TestImportPath(t *testing.T) {
1263 if path := Typeof(vector.Vector{}).PkgPath(); path != "container/vector" {
1264 t.Errorf("Typeof(vector.Vector{}).PkgPath() = %q, want \"container/vector\"", path)
1265 }
1266}
Russ Cox6672b402010-06-14 11:23:11 -07001267
1268func TestDotDotDot(t *testing.T) {
1269 // Test example from FuncType.DotDotDot documentation.
1270 var f func(x int, y ...float)
1271 typ := Typeof(f).(*FuncType)
1272 if typ.NumIn() == 2 && typ.In(0) == Typeof(int(0)) {
1273 sl, ok := typ.In(1).(*SliceType)
1274 if ok {
1275 if sl.Elem() == Typeof(float(0)) {
1276 // ok
1277 return
1278 }
1279 }
1280 }
1281
1282 // Failed
1283 t.Errorf("want NumIn() = 2, In(0) = int, In(1) = []float")
1284 s := fmt.Sprintf("have NumIn() = %d", typ.NumIn())
1285 for i := 0; i < typ.NumIn(); i++ {
1286 s += fmt.Sprintf(", In(%d) = %s", i, typ.In(i))
1287 }
1288 t.Error(s)
1289}
Russ Cox2d5e7322010-09-27 14:09:10 -04001290
1291type inner struct{}
1292
1293type outer struct {
1294 inner
1295}
1296
1297func (*inner) m() {}
1298func (*outer) m() {}
1299
1300func TestNestedMethods(t *testing.T) {
1301 typ := Typeof((*outer)(nil))
1302 if typ.NumMethod() != 1 || typ.Method(0).Func.Get() != NewValue((*outer).m).(*FuncValue).Get() {
1303 t.Errorf("Wrong method table for outer: (m=%p)", (*outer).m)
1304 for i := 0; i < typ.NumMethod(); i++ {
1305 m := typ.Method(i)
1306 t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Get())
1307 }
1308 }
1309}