OSDN Git Service

Update to current version of Go library.
[pf3gnuchains/gcc-fork.git] / libgo / go / reflect / all_test.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package reflect_test
6
7 import (
8         "container/vector"
9         "fmt"
10         "io"
11         "os"
12         . "reflect"
13         "testing"
14         "unsafe"
15 )
16
17 type integer int
18 type T struct {
19         a int
20         b float64
21         c string
22         d *int
23 }
24
25 type pair struct {
26         i interface{}
27         s string
28 }
29
30 func isDigit(c uint8) bool { return '0' <= c && c <= '9' }
31
32 func assert(t *testing.T, s, want string) {
33         if s != want {
34                 t.Errorf("have %#q want %#q", s, want)
35         }
36 }
37
38 func typestring(i interface{}) string { return Typeof(i).String() }
39
40 var typeTests = []pair{
41         {struct{ x int }{}, "int"},
42         {struct{ x int8 }{}, "int8"},
43         {struct{ x int16 }{}, "int16"},
44         {struct{ x int32 }{}, "int32"},
45         {struct{ x int64 }{}, "int64"},
46         {struct{ x uint }{}, "uint"},
47         {struct{ x uint8 }{}, "uint8"},
48         {struct{ x uint16 }{}, "uint16"},
49         {struct{ x uint32 }{}, "uint32"},
50         {struct{ x uint64 }{}, "uint64"},
51         {struct{ x float32 }{}, "float32"},
52         {struct{ x float64 }{}, "float64"},
53         {struct{ x int8 }{}, "int8"},
54         {struct{ x (**int8) }{}, "**int8"},
55         {struct{ x (**integer) }{}, "**reflect_test.integer"},
56         {struct{ x ([32]int32) }{}, "[32]int32"},
57         {struct{ x ([]int8) }{}, "[]int8"},
58         {struct{ x (map[string]int32) }{}, "map[string] int32"},
59         {struct{ x (chan<- string) }{}, "chan<- string"},
60         {struct {
61                 x struct {
62                         c chan *int32
63                         d float32
64                 }
65         }{},
66                 "struct { c chan *int32; d float32 }",
67         },
68         {struct{ x (func(a int8, b int32)) }{}, "func(int8, int32)"},
69         {struct {
70                 x struct {
71                         c func(chan *integer, *int8)
72                 }
73         }{},
74                 "struct { c func(chan *reflect_test.integer, *int8) }",
75         },
76         {struct {
77                 x struct {
78                         a int8
79                         b int32
80                 }
81         }{},
82                 "struct { a int8; b int32 }",
83         },
84         {struct {
85                 x struct {
86                         a int8
87                         b int8
88                         c int32
89                 }
90         }{},
91                 "struct { a int8; b int8; c int32 }",
92         },
93         {struct {
94                 x struct {
95                         a int8
96                         b int8
97                         c int8
98                         d int32
99                 }
100         }{},
101                 "struct { a int8; b int8; c int8; d int32 }",
102         },
103         {struct {
104                 x struct {
105                         a int8
106                         b int8
107                         c int8
108                         d int8
109                         e int32
110                 }
111         }{},
112                 "struct { a int8; b int8; c int8; d int8; e int32 }",
113         },
114         {struct {
115                 x struct {
116                         a int8
117                         b int8
118                         c int8
119                         d int8
120                         e int8
121                         f int32
122                 }
123         }{},
124                 "struct { a int8; b int8; c int8; d int8; e int8; f int32 }",
125         },
126         {struct {
127                 x struct {
128                         a int8 "hi there"
129                 }
130         }{},
131                 `struct { a int8 "hi there" }`,
132         },
133         {struct {
134                 x struct {
135                         a int8 "hi \x00there\t\n\"\\"
136                 }
137         }{},
138                 `struct { a int8 "hi \x00there\t\n\"\\" }`,
139         },
140         {struct {
141                 x struct {
142                         f func(args ...int)
143                 }
144         }{},
145                 "struct { f func(...int) }",
146         },
147         {struct {
148                 x (interface {
149                         a(func(func(int) int) func(func(int)) int)
150                         b()
151                 })
152         }{},
153                 "interface { a(func(func(int) int) func(func(int)) int); b() }",
154         },
155 }
156
157 var valueTests = []pair{
158         {(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 {
177                 c chan *int32
178                 d float32
179         }{},
180                 "struct { c chan *int32; d float32 }{chan *int32, 0}",
181         },
182         {(func(a int8, b int32))(nil), "func(int8, int32)(0)"},
183         {struct{ c func(chan *integer, *int8) }{},
184                 "struct { c func(chan *reflect_test.integer, *int8) }{func(chan *reflect_test.integer, *int8)(0)}",
185         },
186         {struct {
187                 a int8
188                 b int32
189         }{},
190                 "struct { a int8; b int32 }{0, 0}",
191         },
192         {struct {
193                 a int8
194                 b int8
195                 c int32
196         }{},
197                 "struct { a int8; b int8; c int32 }{0, 0, 0}",
198         },
199 }
200
201 func testType(t *testing.T, i int, typ Type, want string) {
202         s := typ.String()
203         if s != want {
204                 t.Errorf("#%d: have %#q, want %#q", i, s, want)
205         }
206 }
207
208 func TestTypes(t *testing.T) {
209         for i, tt := range typeTests {
210                 testType(t, i, NewValue(tt.i).(*StructValue).Field(0).Type(), tt.s)
211         }
212 }
213
214 func TestSet(t *testing.T) {
215         for i, tt := range valueTests {
216                 v := NewValue(tt.i)
217                 switch v := v.(type) {
218                 case *IntValue:
219                         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                         }
231                 case *UintValue:
232                         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                         }
244                 case *FloatValue:
245                         switch v.Type().Kind() {
246                         case Float32:
247                                 v.Set(256.25)
248                         case Float64:
249                                 v.Set(512.125)
250                         }
251                 case *ComplexValue:
252                         switch v.Type().Kind() {
253                         case Complex64:
254                                 v.Set(532.125 + 10i)
255                         case Complex128:
256                                 v.Set(564.25 + 1i)
257                         }
258                 case *StringValue:
259                         v.Set("stringy cheese")
260                 case *BoolValue:
261                         v.Set(true)
262                 }
263                 s := valueToString(v)
264                 if s != tt.s {
265                         t.Errorf("#%d: have %#q, want %#q", i, s, tt.s)
266                 }
267         }
268 }
269
270 func TestSetValue(t *testing.T) {
271         for i, tt := range valueTests {
272                 v := NewValue(tt.i)
273                 switch v := v.(type) {
274                 case *IntValue:
275                         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                         }
287                 case *UintValue:
288                         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                         }
300                 case *FloatValue:
301                         switch v.Type().Kind() {
302                         case Float32:
303                                 v.SetValue(NewValue(float32(256.25)))
304                         case Float64:
305                                 v.SetValue(NewValue(512.125))
306                         }
307                 case *ComplexValue:
308                         switch v.Type().Kind() {
309                         case Complex64:
310                                 v.SetValue(NewValue(complex64(532.125 + 10i)))
311                         case Complex128:
312                                 v.SetValue(NewValue(complex128(564.25 + 1i)))
313                         }
314
315                 case *StringValue:
316                         v.SetValue(NewValue("stringy cheese"))
317                 case *BoolValue:
318                         v.SetValue(NewValue(true))
319                 }
320                 s := valueToString(v)
321                 if s != tt.s {
322                         t.Errorf("#%d: have %#q, want %#q", i, s, tt.s)
323                 }
324         }
325 }
326
327 var _i = 7
328
329 var valueToStringTests = []pair{
330         {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})"},
340 }
341
342 func TestValueToString(t *testing.T) {
343         for i, test := range valueToStringTests {
344                 s := valueToString(NewValue(test.i))
345                 if s != test.s {
346                         t.Errorf("#%d: have %#q, want %#q", i, s, test.s)
347                 }
348         }
349 }
350
351 func TestArrayElemSet(t *testing.T) {
352         v := NewValue([10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
353         v.(*ArrayValue).Elem(4).(*IntValue).Set(123)
354         s := valueToString(v)
355         const want = "[10]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"
356         if s != want {
357                 t.Errorf("[10]int: have %#q want %#q", s, want)
358         }
359
360         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}"
364         if s != want1 {
365                 t.Errorf("[]int: have %#q want %#q", s, want1)
366         }
367 }
368
369 func TestPtrPointTo(t *testing.T) {
370         var ip *int32
371         var i int32 = 1234
372         vip := NewValue(&ip)
373         vi := NewValue(i)
374         vip.(*PtrValue).Elem().(*PtrValue).PointTo(vi)
375         if *ip != 1234 {
376                 t.Errorf("got %d, want 1234", *ip)
377         }
378
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         }
385 }
386
387 func 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
397 func 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
407 func TestAll(t *testing.T) {
408         testType(t, 1, Typeof((int8)(0)), "int8")
409         testType(t, 2, Typeof((*int8)(nil)).(*PtrType).Elem(), "int8")
410
411         typ := Typeof((*struct {
412                 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")
421
422         f, present := styp.FieldByName("d")
423         if !present {
424                 t.Errorf("FieldByName says present field is absent")
425         }
426         testType(t, 6, f.Type, "float32")
427
428         f, present = styp.FieldByName("absent")
429         if present {
430                 t.Errorf("FieldByName says absent field is present")
431         }
432
433         typ = Typeof([32]int32{})
434         testType(t, 7, typ, "[32]int32")
435         testType(t, 8, typ.(*ArrayType).Elem(), "int32")
436
437         typ = Typeof((map[string]*int32)(nil))
438         testType(t, 9, typ, "map[string] *int32")
439         mtyp := typ.(*MapType)
440         testType(t, 10, mtyp.Key(), "string")
441         testType(t, 11, mtyp.Elem(), "*int32")
442
443         typ = Typeof((chan<- string)(nil))
444         testType(t, 12, typ, "chan<- string")
445         testType(t, 13, typ.(*ChanType).Elem(), "string")
446
447         // make sure tag strings are not part of element type
448         typ = Typeof(struct {
449                 d []uint32 "TAG"
450         }{}).(*StructType).Field(0).Type
451         testType(t, 14, typ, "[]uint32")
452 }
453
454 func TestInterfaceGet(t *testing.T) {
455         var inter struct {
456                 e interface{}
457         }
458         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)
464         assert(t, v3.Type().String(), "float64")
465 }
466
467 func TestInterfaceValue(t *testing.T) {
468         var inter struct {
469                 e interface{}
470         }
471         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()
476         assert(t, v3.Type().String(), "float64")
477
478         i3 := v2.Interface()
479         if _, ok := i3.(float64); !ok {
480                 t.Error("v2.Interface() did not return float64, got ", Typeof(i3))
481         }
482 }
483
484 func TestFunctionValue(t *testing.T) {
485         v := NewValue(func() {})
486         if v.Interface() != v.Interface() {
487                 t.Fatalf("TestFunction != itself")
488         }
489         assert(t, v.Type().String(), "func()")
490 }
491
492 var 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
499 func TestAppend(t *testing.T) {
500         for i, test := range appendTests {
501                 origLen, extraLen := len(test.orig), len(test.extra)
502                 want := append(test.orig, test.extra...)
503                 // Convert extra from []int to []Value.
504                 e0 := make([]Value, len(test.extra))
505                 for j, e := range test.extra {
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
539 func TestCopy(t *testing.T) {
540         a := []int{1, 2, 3, 4, 10, 9, 8, 7}
541         b := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44}
542         c := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44}
543         for i := 0; i < len(b); i++ {
544                 if b[i] != c[i] {
545                         t.Fatalf("b != c before test")
546                 }
547         }
548         aa := NewValue(a).(*SliceValue)
549         ab := NewValue(b).(*SliceValue)
550         for tocopy := 1; tocopy <= 7; tocopy++ {
551                 aa.SetLen(tocopy)
552                 Copy(ab, aa)
553                 aa.SetLen(8)
554                 for i := 0; i < tocopy; i++ {
555                         if a[i] != b[i] {
556                                 t.Errorf("(i) tocopy=%d a[%d]=%d, b[%d]=%d",
557                                         tocopy, i, a[i], i, b[i])
558                         }
559                 }
560                 for i := tocopy; i < len(b); i++ {
561                         if b[i] != c[i] {
562                                 if i < len(a) {
563                                         t.Errorf("(ii) tocopy=%d a[%d]=%d, b[%d]=%d, c[%d]=%d",
564                                                 tocopy, i, a[i], i, b[i], i, c[i])
565                                 } else {
566                                         t.Errorf("(iii) tocopy=%d b[%d]=%d, c[%d]=%d",
567                                                 tocopy, i, b[i], i, c[i])
568                                 }
569                         } else {
570                                 t.Logf("tocopy=%d elem %d is okay\n", tocopy, i)
571                         }
572                 }
573         }
574 }
575
576 func TestBigUnnamedStruct(t *testing.T) {
577         b := struct{ a, b, c, d int64 }{1, 2, 3, 4}
578         v := NewValue(b)
579         b1 := v.Interface().(struct {
580                 a, b, c, d int64
581         })
582         if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d {
583                 t.Errorf("NewValue(%v).Interface().(*Big) = %v", b, b1)
584         }
585 }
586
587 type big struct {
588         a, b, c, d, e int64
589 }
590
591 func TestBigStruct(t *testing.T) {
592         b := big{1, 2, 3, 4, 5}
593         v := NewValue(b)
594         b1 := v.Interface().(big)
595         if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d || b1.e != b.e {
596                 t.Errorf("NewValue(%v).Interface().(big) = %v", b, b1)
597         }
598 }
599
600 type Basic struct {
601         x int
602         y float32
603 }
604
605 type NotBasic Basic
606
607 type DeepEqualTest struct {
608         a, b interface{}
609         eq   bool
610 }
611
612 var deepEqualTests = []DeepEqualTest{
613         // Equalities
614         {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},
624
625         // Inequalities
626         {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},
641
642         // Mismatched types
643         {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},
650 }
651
652 func TestDeepEqual(t *testing.T) {
653         for _, test := range deepEqualTests {
654                 if r := DeepEqual(test.a, test.b); r != test.eq {
655                         t.Errorf("DeepEqual(%v, %v) = %v, want %v", test.a, test.b, r, test.eq)
656                 }
657         }
658 }
659
660 func TestTypeof(t *testing.T) {
661         for _, test := range deepEqualTests {
662                 v := NewValue(test.a)
663                 if v == nil {
664                         continue
665                 }
666                 typ := Typeof(test.a)
667                 if typ != v.Type() {
668                         t.Errorf("Typeof(%v) = %v, but NewValue(%v).Type() = %v", test.a, typ, test.a, v.Type())
669                 }
670         }
671 }
672
673 type Recursive struct {
674         x int
675         r *Recursive
676 }
677
678 func TestDeepEqualRecursiveStruct(t *testing.T) {
679         a, b := new(Recursive), new(Recursive)
680         *a = Recursive{12, a}
681         *b = Recursive{12, b}
682         if !DeepEqual(a, b) {
683                 t.Error("DeepEqual(recursive same) = false, want true")
684         }
685 }
686
687 type _Complex struct {
688         a int
689         b [3]*_Complex
690         c *string
691         d map[float64]float64
692 }
693
694 func TestDeepEqualComplexStruct(t *testing.T) {
695         m := make(map[float64]float64)
696         stra, strb := "hello", "hello"
697         a, b := new(_Complex), new(_Complex)
698         *a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m}
699         *b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m}
700         if !DeepEqual(a, b) {
701                 t.Error("DeepEqual(complex same) = false, want true")
702         }
703 }
704
705 func TestDeepEqualComplexStructInequality(t *testing.T) {
706         m := make(map[float64]float64)
707         stra, strb := "hello", "helloo" // Difference is here
708         a, b := new(_Complex), new(_Complex)
709         *a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m}
710         *b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m}
711         if DeepEqual(a, b) {
712                 t.Error("DeepEqual(complex different) = true, want false")
713         }
714 }
715
716
717 func check2ndField(x interface{}, offs uintptr, t *testing.T) {
718         s := NewValue(x).(*StructValue)
719         f := s.Type().(*StructType).Field(1)
720         if f.Offset != offs {
721                 t.Error("mismatched offsets in structure alignment:", f.Offset, offs)
722         }
723 }
724
725 // Check that structure alignment & offsets viewed through reflect agree with those
726 // from the compiler itself.
727 func TestAlignment(t *testing.T) {
728         type T1inner struct {
729                 a int
730         }
731         type T1 struct {
732                 T1inner
733                 f int
734         }
735         type T2inner struct {
736                 a, b int
737         }
738         type T2 struct {
739                 T2inner
740                 f int
741         }
742
743         x := T1{T1inner{2}, 17}
744         check2ndField(x, uintptr(unsafe.Pointer(&x.f))-uintptr(unsafe.Pointer(&x)), t)
745
746         x1 := T2{T2inner{2, 3}, 17}
747         check2ndField(x1, uintptr(unsafe.Pointer(&x1.f))-uintptr(unsafe.Pointer(&x1)), t)
748 }
749
750 type IsNiller interface {
751         IsNil() bool
752 }
753
754 func Nil(a interface{}, t *testing.T) {
755         n := NewValue(a).(*StructValue).Field(0).(IsNiller)
756         if !n.IsNil() {
757                 t.Errorf("%v should be nil", a)
758         }
759 }
760
761 func NotNil(a interface{}, t *testing.T) {
762         n := NewValue(a).(*StructValue).Field(0).(IsNiller)
763         if n.IsNil() {
764                 t.Errorf("value of type %v should not be nil", NewValue(a).Type().String())
765         }
766 }
767
768 func TestIsNil(t *testing.T) {
769         // These do not implement IsNil
770         doNotNil := []interface{}{int(0), float32(0), struct{ a int }{}}
771         for _, ts := range doNotNil {
772                 ty := Typeof(ts)
773                 v := MakeZero(ty)
774                 if _, ok := v.(IsNiller); ok {
775                         t.Errorf("%s is nilable; should not be", ts)
776                 }
777         }
778
779         // These do implement IsNil.
780         // Wrap in extra struct to hide interface type.
781         doNil := []interface{}{
782                 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 }{},
788         }
789         for _, ts := range doNil {
790                 ty := Typeof(ts).(*StructType).Field(0).Type
791                 v := MakeZero(ty)
792                 if _, ok := v.(IsNiller); !ok {
793                         t.Errorf("%s %T is not nilable; should be", ts, v)
794                 }
795         }
796
797         // Check the implementations
798         var pi struct {
799                 x *int
800         }
801         Nil(pi, t)
802         pi.x = new(int)
803         NotNil(pi, t)
804
805         var si struct {
806                 x []int
807         }
808         Nil(si, t)
809         si.x = make([]int, 10)
810         NotNil(si, t)
811
812         var ci struct {
813                 x chan int
814         }
815         Nil(ci, t)
816         ci.x = make(chan int)
817         NotNil(ci, t)
818
819         var mi struct {
820                 x map[int]int
821         }
822         Nil(mi, t)
823         mi.x = make(map[int]int)
824         NotNil(mi, t)
825
826         var ii struct {
827                 x interface{}
828         }
829         Nil(ii, t)
830         ii.x = 2
831         NotNil(ii, t)
832
833         var fi struct {
834                 x func(t *testing.T)
835         }
836         Nil(fi, t)
837         fi.x = TestIsNil
838         NotNil(fi, t)
839 }
840
841 func TestInterfaceExtraction(t *testing.T) {
842         var s struct {
843                 w io.Writer
844         }
845
846         s.w = os.Stdout
847         v := Indirect(NewValue(&s)).(*StructValue).Field(0).Interface()
848         if v != s.w.(interface{}) {
849                 t.Error("Interface() on interface: ", v, s.w)
850         }
851 }
852
853 func TestInterfaceEditing(t *testing.T) {
854         // strings are bigger than one word,
855         // so the interface conversion allocates
856         // memory to hold a string and puts that
857         // pointer in the interface.
858         var i interface{} = "hello"
859
860         // if i pass the interface value by value
861         // to NewValue, i should get a fresh copy
862         // of the value.
863         v := NewValue(i)
864
865         // and setting that copy to "bye" should
866         // not change the value stored in i.
867         v.(*StringValue).Set("bye")
868         if i.(string) != "hello" {
869                 t.Errorf(`Set("bye") changed i to %s`, i.(string))
870         }
871
872         // the same should be true of smaller items.
873         i = 123
874         v = NewValue(i)
875         v.(*IntValue).Set(234)
876         if i.(int) != 123 {
877                 t.Errorf("Set(234) changed i to %d", i.(int))
878         }
879 }
880
881 func TestNilPtrValueSub(t *testing.T) {
882         var pi *int
883         if pv := NewValue(pi).(*PtrValue); pv.Elem() != nil {
884                 t.Error("NewValue((*int)(nil)).(*PtrValue).Elem() != nil")
885         }
886 }
887
888 func TestMap(t *testing.T) {
889         m := map[string]int{"a": 1, "b": 2}
890         mv := NewValue(m).(*MapValue)
891         if n := mv.Len(); n != len(m) {
892                 t.Errorf("Len = %d, want %d", n, len(m))
893         }
894         keys := mv.Keys()
895         i := 0
896         newmap := MakeMap(mv.Type().(*MapType))
897         for k, v := range m {
898                 // Check that returned Keys match keys in range.
899                 // These aren't required to be in the same order,
900                 // but they are in this implementation, which makes
901                 // the test easier.
902                 if i >= len(keys) {
903                         t.Errorf("Missing key #%d %q", i, k)
904                 } else if kv := keys[i].(*StringValue); kv.Get() != k {
905                         t.Errorf("Keys[%d] = %q, want %q", i, kv.Get(), k)
906                 }
907                 i++
908
909                 // Check that value lookup is correct.
910                 vv := mv.Elem(NewValue(k))
911                 if vi := vv.(*IntValue).Get(); vi != int64(v) {
912                         t.Errorf("Key %q: have value %d, want %d", k, vi, v)
913                 }
914
915                 // Copy into new map.
916                 newmap.SetElem(NewValue(k), NewValue(v))
917         }
918         vv := mv.Elem(NewValue("not-present"))
919         if vv != nil {
920                 t.Errorf("Invalid key: got non-nil value %s", valueToString(vv))
921         }
922
923         newm := newmap.Interface().(map[string]int)
924         if len(newm) != len(m) {
925                 t.Errorf("length after copy: newm=%d, m=%d", newm, m)
926         }
927
928         for k, v := range newm {
929                 mv, ok := m[k]
930                 if mv != v {
931                         t.Errorf("newm[%q] = %d, but m[%q] = %d, %v", k, v, k, mv, ok)
932                 }
933         }
934
935         newmap.SetElem(NewValue("a"), nil)
936         v, ok := newm["a"]
937         if ok {
938                 t.Errorf("newm[\"a\"] = %d after delete", v)
939         }
940
941         mv = NewValue(&m).(*PtrValue).Elem().(*MapValue)
942         mv.Set(nil)
943         if m != nil {
944                 t.Errorf("mv.Set(nil) failed")
945         }
946 }
947
948 func TestChan(t *testing.T) {
949         for loop := 0; loop < 2; loop++ {
950                 var c chan int
951                 var cv *ChanValue
952
953                 // check both ways to allocate channels
954                 switch loop {
955                 case 1:
956                         c = make(chan int, 1)
957                         cv = NewValue(c).(*ChanValue)
958                 case 0:
959                         cv = MakeChan(Typeof(c).(*ChanType), 1)
960                         c = cv.Interface().(chan int)
961                 }
962
963                 // Send
964                 cv.Send(NewValue(2))
965                 if i := <-c; i != 2 {
966                         t.Errorf("reflect Send 2, native recv %d", i)
967                 }
968
969                 // Recv
970                 c <- 3
971                 if i, ok := cv.Recv(); i.(*IntValue).Get() != 3 || !ok {
972                         t.Errorf("native send 3, reflect Recv %d, %t", i.(*IntValue).Get(), ok)
973                 }
974
975                 // TryRecv fail
976                 val, ok := cv.TryRecv()
977                 if val != nil || ok {
978                         t.Errorf("TryRecv on empty chan: %s, %t", valueToString(val), ok)
979                 }
980
981                 // TryRecv success
982                 c <- 4
983                 val, ok = cv.TryRecv()
984                 if val == nil {
985                         t.Errorf("TryRecv on ready chan got nil")
986                 } else if i := val.(*IntValue).Get(); i != 4 || !ok {
987                         t.Errorf("native send 4, TryRecv %d, %t", i, ok)
988                 }
989
990                 // TrySend fail
991                 c <- 100
992                 ok = cv.TrySend(NewValue(5))
993                 i := <-c
994                 if ok {
995                         t.Errorf("TrySend on full chan succeeded: value %d", i)
996                 }
997
998                 // TrySend success
999                 ok = cv.TrySend(NewValue(6))
1000                 if !ok {
1001                         t.Errorf("TrySend on empty chan failed")
1002                 } else {
1003                         if i = <-c; i != 6 {
1004                                 t.Errorf("TrySend 6, recv %d", i)
1005                         }
1006                 }
1007
1008                 // Close
1009                 c <- 123
1010                 cv.Close()
1011                 if i, ok := cv.Recv(); i.(*IntValue).Get() != 123 || !ok {
1012                         t.Errorf("send 123 then close; Recv %d, %t", i.(*IntValue).Get(), ok)
1013                 }
1014                 if i, ok := cv.Recv(); i.(*IntValue).Get() != 0 || ok {
1015                         t.Errorf("after close Recv %d, %t", i.(*IntValue).Get(), ok)
1016                 }
1017         }
1018
1019         // check creation of unbuffered channel
1020         var c chan int
1021         cv := MakeChan(Typeof(c).(*ChanType), 0)
1022         c = cv.Interface().(chan int)
1023         if cv.TrySend(NewValue(7)) {
1024                 t.Errorf("TrySend on sync chan succeeded")
1025         }
1026         if v, ok := cv.TryRecv(); v != nil || ok {
1027                 t.Errorf("TryRecv on sync chan succeeded")
1028         }
1029
1030         // len/cap
1031         cv = MakeChan(Typeof(c).(*ChanType), 10)
1032         c = cv.Interface().(chan int)
1033         for i := 0; i < 3; i++ {
1034                 c <- i
1035         }
1036         if l, m := cv.Len(), cv.Cap(); l != len(c) || m != cap(c) {
1037                 t.Errorf("Len/Cap = %d/%d want %d/%d", l, m, len(c), cap(c))
1038         }
1039
1040 }
1041
1042 // Difficult test for function call because of
1043 // implicit padding between arguments.
1044 func dummy(b byte, c int, d byte) (i byte, j int, k byte) {
1045         return b, c, d
1046 }
1047
1048 func TestFunc(t *testing.T) {
1049         ret := NewValue(dummy).(*FuncValue).Call([]Value{NewValue(byte(10)), NewValue(20), NewValue(byte(30))})
1050         if len(ret) != 3 {
1051                 t.Fatalf("Call returned %d values, want 3", len(ret))
1052         }
1053
1054         i := ret[0].(*UintValue).Get()
1055         j := ret[1].(*IntValue).Get()
1056         k := ret[2].(*UintValue).Get()
1057         if i != 10 || j != 20 || k != 30 {
1058                 t.Errorf("Call returned %d, %d, %d; want 10, 20, 30", i, j, k)
1059         }
1060 }
1061
1062 type Point struct {
1063         x, y int
1064 }
1065
1066 func (p Point) Dist(scale int) int { return p.x*p.x*scale + p.y*p.y*scale }
1067
1068 func TestMethod(t *testing.T) {
1069         // Non-curried method of type.
1070         p := Point{3, 4}
1071         i := Typeof(p).Method(0).Func.Call([]Value{NewValue(p), NewValue(10)})[0].(*IntValue).Get()
1072         if i != 250 {
1073                 t.Errorf("Type Method returned %d; want 250", i)
1074         }
1075
1076         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
1081         // Curried method of value.
1082         i = NewValue(p).Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
1083         if i != 250 {
1084                 t.Errorf("Value Method returned %d; want 250", i)
1085         }
1086
1087         // 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
1099         // Curried method of interface value.
1100         // Have to wrap interface value in a struct to get at it.
1101         // Passing it to NewValue directly would
1102         // access the underlying Point, not the interface.
1103         var s = struct {
1104                 x interface {
1105                         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()
1110         if i != 250 {
1111                 t.Errorf("Interface Method returned %d; want 250", i)
1112         }
1113 }
1114
1115 func TestInterfaceSet(t *testing.T) {
1116         p := &Point{3, 4}
1117
1118         var s struct {
1119                 I interface{}
1120                 P interface {
1121                         Dist(int) int
1122                 }
1123         }
1124         sv := NewValue(&s).(*PtrValue).Elem().(*StructValue)
1125         sv.Field(0).(*InterfaceValue).Set(NewValue(p))
1126         if q := s.I.(*Point); q != p {
1127                 t.Errorf("i: have %p want %p", q, p)
1128         }
1129
1130         pv := sv.Field(1).(*InterfaceValue)
1131         pv.Set(NewValue(p))
1132         if q := s.P.(*Point); q != p {
1133                 t.Errorf("i: have %p want %p", q, p)
1134         }
1135
1136         i := pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get()
1137         if i != 250 {
1138                 t.Errorf("Interface Method returned %d; want 250", i)
1139         }
1140 }
1141
1142 type T1 struct {
1143         a string
1144         int
1145 }
1146
1147 func TestAnonymousFields(t *testing.T) {
1148         var field StructField
1149         var ok bool
1150         var t1 T1
1151         type1 := Typeof(t1).(*StructType)
1152         if field, ok = type1.FieldByName("int"); !ok {
1153                 t.Error("no field 'int'")
1154         }
1155         if field.Index[0] != 1 {
1156                 t.Error("field index should be 1; is", field.Index)
1157         }
1158 }
1159
1160 type FTest struct {
1161         s     interface{}
1162         name  string
1163         index []int
1164         value int
1165 }
1166
1167 type D1 struct {
1168         d int
1169 }
1170 type D2 struct {
1171         d int
1172 }
1173
1174 type S0 struct {
1175         a, b, c int
1176         D1
1177         D2
1178 }
1179
1180 type S1 struct {
1181         b int
1182         S0
1183 }
1184
1185 type S2 struct {
1186         a int
1187         *S1
1188 }
1189
1190 type S1x struct {
1191         S1
1192 }
1193
1194 type S1y struct {
1195         S1
1196 }
1197
1198 type S3 struct {
1199         S1x
1200         S2
1201         d, e int
1202         *S1y
1203 }
1204
1205 type S4 struct {
1206         *S4
1207         a int
1208 }
1209
1210 var fieldTests = []FTest{
1211         {struct{}{}, "", nil, 0},
1212         {struct{}{}, "foo", nil, 0},
1213         {S0{a: 'a'}, "a", []int{0}, 'a'},
1214         {S0{}, "d", nil, 0},
1215         {S1{S0: S0{a: 'a'}}, "a", []int{1, 0}, 'a'},
1216         {S1{b: 'b'}, "b", []int{0}, 'b'},
1217         {S1{}, "S0", []int{1}, 0},
1218         {S1{S0: S0{c: 'c'}}, "c", []int{1, 2}, 'c'},
1219         {S2{a: 'a'}, "a", []int{0}, 'a'},
1220         {S2{}, "S1", []int{1}, 0},
1221         {S2{S1: &S1{b: 'b'}}, "b", []int{1, 0}, 'b'},
1222         {S2{S1: &S1{S0: S0{c: 'c'}}}, "c", []int{1, 1, 2}, 'c'},
1223         {S2{}, "d", nil, 0},
1224         {S3{}, "S1", nil, 0},
1225         {S3{S2: S2{a: 'a'}}, "a", []int{1, 0}, 'a'},
1226         {S3{}, "b", nil, 0},
1227         {S3{d: 'd'}, "d", []int{2}, 0},
1228         {S3{e: 'e'}, "e", []int{3}, 'e'},
1229         {S4{a: 'a'}, "a", []int{1}, 'a'},
1230         {S4{}, "b", nil, 0},
1231 }
1232
1233 func TestFieldByIndex(t *testing.T) {
1234         for _, test := range fieldTests {
1235                 s := Typeof(test.s).(*StructType)
1236                 f := s.FieldByIndex(test.index)
1237                 if f.Name != "" {
1238                         if test.index != nil {
1239                                 if f.Name != test.name {
1240                                         t.Errorf("%s.%s found; want %s", s.Name(), f.Name, test.name)
1241                                 }
1242                         } else {
1243                                 t.Errorf("%s.%s found", s.Name(), f.Name)
1244                         }
1245                 } else if len(test.index) > 0 {
1246                         t.Errorf("%s.%s not found", s.Name(), test.name)
1247                 }
1248
1249                 if test.value != 0 {
1250                         v := NewValue(test.s).(*StructValue).FieldByIndex(test.index)
1251                         if v != nil {
1252                                 if x, ok := v.Interface().(int); ok {
1253                                         if x != test.value {
1254                                                 t.Errorf("%s%v is %d; want %d", s.Name(), test.index, x, test.value)
1255                                         }
1256                                 } else {
1257                                         t.Errorf("%s%v value not an int", s.Name(), test.index)
1258                                 }
1259                         } else {
1260                                 t.Errorf("%s%v value not found", s.Name(), test.index)
1261                         }
1262                 }
1263         }
1264 }
1265
1266 func TestFieldByName(t *testing.T) {
1267         for _, test := range fieldTests {
1268                 s := Typeof(test.s).(*StructType)
1269                 f, found := s.FieldByName(test.name)
1270                 if found {
1271                         if test.index != nil {
1272                                 // Verify field depth and index.
1273                                 if len(f.Index) != len(test.index) {
1274                                         t.Errorf("%s.%s depth %d; want %d", s.Name(), test.name, len(f.Index), len(test.index))
1275                                 } else {
1276                                         for i, x := range f.Index {
1277                                                 if x != test.index[i] {
1278                                                         t.Errorf("%s.%s.Index[%d] is %d; want %d", s.Name(), test.name, i, x, test.index[i])
1279                                                 }
1280                                         }
1281                                 }
1282                         } else {
1283                                 t.Errorf("%s.%s found", s.Name(), f.Name)
1284                         }
1285                 } else if len(test.index) > 0 {
1286                         t.Errorf("%s.%s not found", s.Name(), test.name)
1287                 }
1288
1289                 if test.value != 0 {
1290                         v := NewValue(test.s).(*StructValue).FieldByName(test.name)
1291                         if v != nil {
1292                                 if x, ok := v.Interface().(int); ok {
1293                                         if x != test.value {
1294                                                 t.Errorf("%s.%s is %d; want %d", s.Name(), test.name, x, test.value)
1295                                         }
1296                                 } else {
1297                                         t.Errorf("%s.%s value not an int", s.Name(), test.name)
1298                                 }
1299                         } else {
1300                                 t.Errorf("%s.%s value not found", s.Name(), test.name)
1301                         }
1302                 }
1303         }
1304 }
1305
1306 func TestImportPath(t *testing.T) {
1307         if path := Typeof(vector.Vector{}).PkgPath(); path != "libgo_container.vector" {
1308                 t.Errorf("Typeof(vector.Vector{}).PkgPath() = %q, want \"libgo_container.vector\"", path)
1309         }
1310 }
1311
1312 func TestDotDotDot(t *testing.T) {
1313         // Test example from FuncType.DotDotDot documentation.
1314         var f func(x int, y ...float64)
1315         typ := Typeof(f).(*FuncType)
1316         if typ.NumIn() == 2 && typ.In(0) == Typeof(int(0)) {
1317                 sl, ok := typ.In(1).(*SliceType)
1318                 if ok {
1319                         if sl.Elem() == Typeof(0.0) {
1320                                 // ok
1321                                 return
1322                         }
1323                 }
1324         }
1325
1326         // Failed
1327         t.Errorf("want NumIn() = 2, In(0) = int, In(1) = []float64")
1328         s := fmt.Sprintf("have NumIn() = %d", typ.NumIn())
1329         for i := 0; i < typ.NumIn(); i++ {
1330                 s += fmt.Sprintf(", In(%d) = %s", i, typ.In(i))
1331         }
1332         t.Error(s)
1333 }
1334
1335 type inner struct {
1336         x int
1337 }
1338
1339 type outer struct {
1340         y int
1341         inner
1342 }
1343
1344 func (*inner) m() {}
1345 func (*outer) m() {}
1346
1347 func TestNestedMethods(t *testing.T) {
1348         typ := Typeof((*outer)(nil))
1349         if typ.NumMethod() != 1 || typ.Method(0).Func.Get() != NewValue((*outer).m).(*FuncValue).Get() {
1350                 t.Errorf("Wrong method table for outer: (m=%p)", (*outer).m)
1351                 for i := 0; i < typ.NumMethod(); i++ {
1352                         m := typ.Method(i)
1353                         t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Get())
1354                 }
1355         }
1356 }
1357
1358 type innerInt struct {
1359         x int
1360 }
1361
1362 type outerInt struct {
1363         y int
1364         innerInt
1365 }
1366
1367 func (i *innerInt) m() int {
1368         return i.x
1369 }
1370
1371 func TestEmbeddedMethods(t *testing.T) {
1372         typ := Typeof((*outerInt)(nil))
1373         if typ.NumMethod() != 1 || typ.Method(0).Func.Get() != NewValue((*outerInt).m).(*FuncValue).Get() {
1374                 t.Errorf("Wrong method table for outerInt: (m=%p)", (*outerInt).m)
1375                 for i := 0; i < typ.NumMethod(); i++ {
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 }
1396
1397 func 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
1412 func 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 }