LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package reflect_test | 5 package reflect_test |
6 | 6 |
7 import ( | 7 import ( |
8 "container/vector" | 8 "container/vector" |
9 "fmt" | 9 "fmt" |
10 "io" | 10 "io" |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 | 207 |
208 func TestTypes(t *testing.T) { | 208 func TestTypes(t *testing.T) { |
209 for i, tt := range typeTests { | 209 for i, tt := range typeTests { |
210 testType(t, i, NewValue(tt.i).Field(0).Type(), tt.s) | 210 testType(t, i, NewValue(tt.i).Field(0).Type(), tt.s) |
211 } | 211 } |
212 } | 212 } |
213 | 213 |
214 func TestSet(t *testing.T) { | 214 func TestSet(t *testing.T) { |
215 for i, tt := range valueTests { | 215 for i, tt := range valueTests { |
216 v := NewValue(tt.i) | 216 v := NewValue(tt.i) |
217 switch v.Kind() { | 217 switch v.Kind() { |
218 case Int: | 218 case Int: |
219 v.SetInt(132) | 219 v.SetInt(132) |
220 case Int8: | 220 case Int8: |
221 v.SetInt(8) | 221 v.SetInt(8) |
222 case Int16: | 222 case Int16: |
223 v.SetInt(16) | 223 v.SetInt(16) |
224 case Int32: | 224 case Int32: |
225 v.SetInt(32) | 225 v.SetInt(32) |
226 case Int64: | 226 case Int64: |
227 v.SetInt(64) | 227 v.SetInt(64) |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 var i int32 = 1234 | 346 var i int32 = 1234 |
347 vip := NewValue(&ip) | 347 vip := NewValue(&ip) |
348 vi := NewValue(i) | 348 vi := NewValue(i) |
349 vip.Elem().Set(vi.Addr()) | 349 vip.Elem().Set(vi.Addr()) |
350 if *ip != 1234 { | 350 if *ip != 1234 { |
351 t.Errorf("got %d, want 1234", *ip) | 351 t.Errorf("got %d, want 1234", *ip) |
352 } | 352 } |
353 | 353 |
354 ip = nil | 354 ip = nil |
355 vp := NewValue(ip) | 355 vp := NewValue(ip) |
356 » vp.Set(Value{}) | 356 » vp.Set(Zero(vp.Type())) |
357 if ip != nil { | 357 if ip != nil { |
358 t.Errorf("got non-nil (%p), want nil", ip) | 358 t.Errorf("got non-nil (%p), want nil", ip) |
359 } | 359 } |
360 } | 360 } |
361 | 361 |
362 func TestPtrSetNil(t *testing.T) { | 362 func TestPtrSetNil(t *testing.T) { |
363 var i int32 = 1234 | 363 var i int32 = 1234 |
364 ip := &i | 364 ip := &i |
365 vip := NewValue(&ip) | 365 vip := NewValue(&ip) |
366 » vip.Elem().Set(Value{}) | 366 » vip.Elem().Set(Zero(vip.Elem().Type())) |
367 if ip != nil { | 367 if ip != nil { |
368 t.Errorf("got non-nil (%d), want nil", *ip) | 368 t.Errorf("got non-nil (%d), want nil", *ip) |
369 } | 369 } |
370 } | 370 } |
371 | 371 |
372 func TestMapSetNil(t *testing.T) { | 372 func TestMapSetNil(t *testing.T) { |
373 m := make(map[string]int) | 373 m := make(map[string]int) |
374 vm := NewValue(&m) | 374 vm := NewValue(&m) |
375 » vm.Elem().Set(Value{}) | 375 » vm.Elem().Set(Zero(vm.Elem().Type())) |
376 if m != nil { | 376 if m != nil { |
377 t.Errorf("got non-nil (%p), want nil", m) | 377 t.Errorf("got non-nil (%p), want nil", m) |
378 } | 378 } |
379 } | 379 } |
380 | 380 |
381 | 381 |
382 func TestAll(t *testing.T) { | 382 func TestAll(t *testing.T) { |
383 testType(t, 1, Typeof((int8)(0)), "int8") | 383 testType(t, 1, Typeof((int8)(0)), "int8") |
384 testType(t, 2, Typeof((*int8)(nil)).Elem(), "int8") | 384 testType(t, 2, Typeof((*int8)(nil)).Elem(), "int8") |
385 | 385 |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
742 doNil := []interface{}{ | 742 doNil := []interface{}{ |
743 struct{ x *int }{}, | 743 struct{ x *int }{}, |
744 struct{ x interface{} }{}, | 744 struct{ x interface{} }{}, |
745 struct{ x map[string]int }{}, | 745 struct{ x map[string]int }{}, |
746 struct{ x func() bool }{}, | 746 struct{ x func() bool }{}, |
747 struct{ x chan int }{}, | 747 struct{ x chan int }{}, |
748 struct{ x []string }{}, | 748 struct{ x []string }{}, |
749 } | 749 } |
750 for _, ts := range doNil { | 750 for _, ts := range doNil { |
751 ty := Typeof(ts).Field(0).Type | 751 ty := Typeof(ts).Field(0).Type |
752 » » v := MakeZero(ty) | 752 » » v := Zero(ty) |
753 v.IsNil() // panics if not okay to call | 753 v.IsNil() // panics if not okay to call |
754 } | 754 } |
755 | 755 |
756 // Check the implementations | 756 // Check the implementations |
757 var pi struct { | 757 var pi struct { |
758 x *int | 758 x *int |
759 } | 759 } |
760 Nil(pi, t) | 760 Nil(pi, t) |
761 pi.x = new(int) | 761 pi.x = new(int) |
762 NotNil(pi, t) | 762 NotNil(pi, t) |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
891 } | 891 } |
892 } | 892 } |
893 | 893 |
894 newmap.SetMapIndex(NewValue("a"), Value{}) | 894 newmap.SetMapIndex(NewValue("a"), Value{}) |
895 v, ok := newm["a"] | 895 v, ok := newm["a"] |
896 if ok { | 896 if ok { |
897 t.Errorf("newm[\"a\"] = %d after delete", v) | 897 t.Errorf("newm[\"a\"] = %d after delete", v) |
898 } | 898 } |
899 | 899 |
900 mv = NewValue(&m).Elem() | 900 mv = NewValue(&m).Elem() |
901 » mv.Set(Value{}) | 901 » mv.Set(Zero(mv.Type())) |
902 if m != nil { | 902 if m != nil { |
903 t.Errorf("mv.Set(nil) failed") | 903 t.Errorf("mv.Set(nil) failed") |
904 } | 904 } |
905 } | 905 } |
906 | 906 |
907 func TestChan(t *testing.T) { | 907 func TestChan(t *testing.T) { |
908 for loop := 0; loop < 2; loop++ { | 908 for loop := 0; loop < 2; loop++ { |
909 var c chan int | 909 var c chan int |
910 var cv Value | 910 var cv Value |
911 | 911 |
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1408 if p.X != 3 { // should be unchanged from last time | 1408 if p.X != 3 { // should be unchanged from last time |
1409 t.Errorf("somehow value Set changed original p") | 1409 t.Errorf("somehow value Set changed original p") |
1410 } | 1410 } |
1411 p = v0.Interface().(struct { | 1411 p = v0.Interface().(struct { |
1412 X, Y int | 1412 X, Y int |
1413 }) | 1413 }) |
1414 if p.X != 4 { | 1414 if p.X != 4 { |
1415 t.Errorf("Addr.Elem.Set valued to set value in top value") | 1415 t.Errorf("Addr.Elem.Set valued to set value in top value") |
1416 } | 1416 } |
1417 } | 1417 } |
LEFT | RIGHT |