Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 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 | |
Rob Pike | 12254b6 | 2008-11-19 19:11:01 -0800 | [diff] [blame] | 5 | package reflect |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 6 | |
| 7 | import ( |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 8 | "io"; |
| 9 | "os"; |
Rob Pike | 12254b6 | 2008-11-19 19:11:01 -0800 | [diff] [blame] | 10 | "reflect"; |
Rob Pike | 93831d2 | 2009-04-29 22:16:53 -0700 | [diff] [blame] | 11 | "testing"; |
| 12 | "unsafe"; |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 13 | ) |
| 14 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 15 | type integer int |
| 16 | type T struct { a int; b float64; c string; d *int } |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 17 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 18 | type pair struct { |
| 19 | i interface{}; |
| 20 | s string; |
| 21 | } |
| 22 | |
| 23 | func isDigit(c uint8) bool { |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 24 | return '0' <= c && c <= '9' |
| 25 | } |
| 26 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 27 | func assert(t *testing.T, s, want string) { |
| 28 | if s != want { |
| 29 | t.Errorf("have %#q want %#q", s, want); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func typestring(i interface{}) string { |
| 34 | return Typeof(i).String(); |
| 35 | } |
| 36 | |
| 37 | var typeTests = []pair { |
| 38 | pair { struct { x int }{}, "int" }, |
| 39 | pair { struct { x int8 }{}, "int8" }, |
| 40 | pair { struct { x int16 }{}, "int16" }, |
| 41 | pair { struct { x int32 }{}, "int32" }, |
| 42 | pair { struct { x int64 }{}, "int64" }, |
| 43 | pair { struct { x uint }{}, "uint" }, |
| 44 | pair { struct { x uint8 }{}, "uint8" }, |
| 45 | pair { struct { x uint16 }{}, "uint16" }, |
| 46 | pair { struct { x uint32 }{}, "uint32" }, |
| 47 | pair { struct { x uint64 }{}, "uint64" }, |
| 48 | pair { struct { x float }{}, "float" }, |
| 49 | pair { struct { x float32 }{}, "float32" }, |
| 50 | pair { struct { x float64 }{}, "float64" }, |
| 51 | pair { struct { x int8 }{}, "int8" }, |
| 52 | pair { struct { x (**int8) }{}, "**int8" }, |
| 53 | pair { struct { x (**reflect.integer) }{}, "**reflect.integer" }, |
| 54 | pair { struct { x ([32]int32) }{}, "[32]int32" }, |
| 55 | pair { struct { x ([]int8) }{}, "[]int8" }, |
| 56 | pair { struct { x (map[string]int32) }{}, "map[string] int32" }, |
| 57 | pair { struct { x (chan<-string) }{}, "chan<- string" }, |
| 58 | pair { struct { x struct {c chan *int32; d float32} }{}, "struct { c chan *int32; d float32 }" }, |
| 59 | pair { struct { x (func(a int8, b int32)) }{}, "func(int8, int32)" }, |
| 60 | pair { struct { x struct {c func(chan *reflect.integer, *int8)} }{}, "struct { c func(chan *reflect.integer, *int8) }" }, |
| 61 | pair { struct { x struct {a int8; b int32} }{}, "struct { a int8; b int32 }" }, |
| 62 | pair { struct { x struct {a int8; b int8; b int32} }{}, "struct { a int8; b int8; b int32 }" }, |
| 63 | pair { struct { x struct {a int8; b int8; c int8; b int32} }{}, "struct { a int8; b int8; c int8; b int32 }" }, |
| 64 | pair { struct { x struct {a int8; b int8; c int8; d int8; b int32} }{}, "struct { a int8; b int8; c int8; d int8; b int32 }" }, |
| 65 | pair { struct { x struct {a int8; b int8; c int8; d int8; e int8; b int32} }{}, "struct { a int8; b int8; c int8; d int8; e int8; b int32 }" }, |
| 66 | pair { struct { x struct {a int8 "hi there"; } }{}, `struct { a int8 "hi there" }` }, |
| 67 | pair { struct { x struct {a int8 "hi \x00there\t\n\"\\"; } }{}, `struct { a int8 "hi \x00there\t\n\"\\" }` }, |
| 68 | pair { struct { x struct {f func(args ...)} }{}, "struct { f func(...) }" }, |
| 69 | pair { struct { x (interface { a(func(func(int)(int))(func(func(int))(int))); b() }) }{}, "interface { a (func(func(int) (int)) (func(func(int)) (int))); b () }" }, |
| 70 | } |
| 71 | |
| 72 | var valueTests = []pair { |
| 73 | pair { (int8)(0), "8" }, |
| 74 | pair { (int16)(0), "16" }, |
| 75 | pair { (int32)(0), "32" }, |
| 76 | pair { (int64)(0), "64" }, |
| 77 | pair { (uint8)(0), "8" }, |
| 78 | pair { (uint16)(0), "16" }, |
| 79 | pair { (uint32)(0), "32" }, |
| 80 | pair { (uint64)(0), "64" }, |
| 81 | pair { (float32)(0), "32.1" }, |
| 82 | pair { (float64)(0), "64.2" }, |
| 83 | pair { (string)(""), "stringy cheese" }, |
| 84 | pair { (bool)(false), "true" }, |
| 85 | pair { (*int8)(nil), "*int8(0)" }, |
| 86 | pair { (**int8)(nil), "**int8(0)" }, |
| 87 | pair { ([5]int32){}, "[5]int32{0, 0, 0, 0, 0}" }, |
| 88 | pair { (**reflect.integer)(nil), "**reflect.integer(0)" }, |
| 89 | pair { (map[string]int32)(nil), "map[string] int32{<can't iterate on maps>}" }, |
| 90 | pair { (chan<-string)(nil), "chan<- string" }, |
| 91 | pair { (struct {c chan *int32; d float32}){}, "struct { c chan *int32; d float32 }{chan *int32, 0}" }, |
| 92 | pair { (func(a int8, b int32))(nil), "func(int8, int32)(0)" }, |
| 93 | pair { (struct {c func(chan *reflect.integer, *int8)}){}, "struct { c func(chan *reflect.integer, *int8) }{func(chan *reflect.integer, *int8)(0)}" }, |
| 94 | pair { (struct {a int8; b int32}){}, "struct { a int8; b int32 }{0, 0}" }, |
| 95 | pair { (struct {a int8; b int8; b int32}){}, "struct { a int8; b int8; b int32 }{0, 0, 0}" }, |
| 96 | } |
| 97 | |
| 98 | func testType(t *testing.T, i int, typ Type, want string) { |
| 99 | s := typ.String(); |
| 100 | if s != want { |
| 101 | t.Errorf("#%d: have %#q, want %#q", i, s, want); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestTypes(t *testing.T) { |
| 106 | for i, tt := range typeTests { |
| 107 | testType(t, i, NewValue(tt.i).(*StructValue).Field(0).Type(), tt.s); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func TestValue(t *testing.T) { |
| 112 | for i, tt := range valueTests { |
| 113 | v := NewValue(tt.i); |
| 114 | switch v := v.(type) { |
| 115 | case *reflect.IntValue: |
| 116 | v.Set(132); |
| 117 | case *reflect.Int8Value: |
| 118 | v.Set(8); |
| 119 | case *reflect.Int16Value: |
| 120 | v.Set(16); |
| 121 | case *reflect.Int32Value: |
| 122 | v.Set(32); |
| 123 | case *reflect.Int64Value: |
| 124 | v.Set(64); |
| 125 | case *reflect.UintValue: |
| 126 | v.Set(132); |
| 127 | case *reflect.Uint8Value: |
| 128 | v.Set(8); |
| 129 | case *reflect.Uint16Value: |
| 130 | v.Set(16); |
| 131 | case *reflect.Uint32Value: |
| 132 | v.Set(32); |
| 133 | case *reflect.Uint64Value: |
| 134 | v.Set(64); |
| 135 | case *reflect.FloatValue: |
| 136 | v.Set(3200.0); |
| 137 | case *reflect.Float32Value: |
| 138 | v.Set(32.1); |
| 139 | case *reflect.Float64Value: |
| 140 | v.Set(64.2); |
| 141 | case *reflect.StringValue: |
| 142 | v.Set("stringy cheese"); |
| 143 | case *reflect.BoolValue: |
| 144 | v.Set(true); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 145 | } |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 146 | s := valueToString(v); |
| 147 | if s != tt.s { |
| 148 | t.Errorf("#%d: have %#q, want %#q", i, s, tt.s); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 153 | var _i = 7; |
| 154 | |
| 155 | var valueToStringTests = []pair { |
| 156 | pair { 123, "123" }, |
| 157 | pair { 123.4, "123.4" }, |
| 158 | pair { byte(123), "123" }, |
| 159 | pair { "abc", "abc" }, |
| 160 | pair { T{123, 456.75, "hello", &_i}, "reflect.T{123, 456.75, hello, *int(&7)}" }, |
| 161 | pair { new(chan *T), "*chan *reflect.T(&chan *reflect.T)" }, |
| 162 | pair { [10]int{1,2,3,4,5,6,7,8,9,10}, "[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}" }, |
| 163 | pair { &[10]int{1,2,3,4,5,6,7,8,9,10}, "*[10]int(&[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})" }, |
| 164 | pair { []int{1,2,3,4,5,6,7,8,9,10}, "[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}" }, |
| 165 | pair { &[]int{1,2,3,4,5,6,7,8,9,10}, "*[]int(&[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})" } |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 168 | func TestValueToString(t *testing.T) { |
| 169 | for i, test := range valueToStringTests { |
| 170 | s := valueToString(NewValue(test.i)); |
| 171 | if s != test.s { |
| 172 | t.Errorf("#%d: have %#q, want %#q", i, s, test.s); |
Rob Pike | 419e1e0 | 2008-11-12 19:05:05 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 175 | } |
Rob Pike | 419e1e0 | 2008-11-12 19:05:05 -0800 | [diff] [blame] | 176 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 177 | func TestArrayElemSet(t *testing.T) { |
| 178 | v := NewValue([10]int{1,2,3,4,5,6,7,8,9,10}); |
| 179 | v.(*ArrayValue).Elem(4).(*IntValue).Set(123); |
| 180 | s := valueToString(v); |
| 181 | const want = "[10]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"; |
| 182 | if s != want { |
| 183 | t.Errorf("[10]int: have %#q want %#q", s, want); |
| 184 | } |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 185 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 186 | v = NewValue([]int{1,2,3,4,5,6,7,8,9,10}); |
| 187 | v.(*SliceValue).Elem(4).(*IntValue).Set(123); |
| 188 | s = valueToString(v); |
| 189 | const want1 = "[]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"; |
| 190 | if s != want1 { |
| 191 | t.Errorf("[]int: have %#q want %#q", s, want1); |
| 192 | } |
| 193 | } |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 194 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 195 | func TestPtrPointTo(t *testing.T) { |
| 196 | var ip *int32; |
| 197 | var i int32 = 1234; |
| 198 | vip := NewValue(&ip); |
| 199 | vi := NewValue(i); |
| 200 | vip.(*PtrValue).Elem().(*PtrValue).PointTo(vi); |
| 201 | if *ip != 1234 { |
| 202 | t.Errorf("got %d, want 1234", *ip); |
| 203 | } |
| 204 | } |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 205 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 206 | func TestAll(t *testing.T) { // TODO(r): wrap up better |
| 207 | testType(t, 1, Typeof((int8)(0)), "int8"); |
| 208 | testType(t, 2, Typeof((*int8)(nil)).(*PtrType).Elem(), "int8"); |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 209 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 210 | typ := Typeof((*struct{c chan *int32; d float32})(nil)); |
| 211 | testType(t, 3, typ, "*struct { c chan *int32; d float32 }"); |
| 212 | etyp := typ.(*PtrType).Elem(); |
| 213 | testType(t, 4, etyp, "struct { c chan *int32; d float32 }"); |
| 214 | styp := etyp.(*StructType); |
| 215 | f := styp.Field(0); |
| 216 | testType(t, 5, f.Type, "chan *int32"); |
| 217 | f = styp.Field(1); |
| 218 | testType(t, 6, f.Type, "float32"); |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 219 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 220 | typ = Typeof(([32]int32)(nil)); |
| 221 | testType(t, 7, typ, "[32]int32"); |
| 222 | testType(t, 8, typ.(*ArrayType).Elem(), "int32"); |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 223 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 224 | typ = Typeof((map[string]*int32)(nil)); |
| 225 | testType(t, 9, typ, "map[string] *int32"); |
| 226 | mtyp := typ.(*MapType); |
| 227 | testType(t, 10, mtyp.Key(), "string"); |
| 228 | testType(t, 11, mtyp.Elem(), "*int32"); |
Rob Pike | bdbb958 | 2008-11-05 08:17:01 -0800 | [diff] [blame] | 229 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 230 | typ = Typeof((chan<-string)(nil)); |
| 231 | testType(t, 12, typ, "chan<- string"); |
| 232 | testType(t, 13, typ.(*ChanType).Elem(), "string"); |
Rob Pike | 5a1cbe8 | 2008-11-05 13:01:33 -0800 | [diff] [blame] | 233 | |
| 234 | // make sure tag strings are not part of element type |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 235 | typ = Typeof(struct{d []uint32 "TAG"}{}).(*StructType).Field(0).Type; |
| 236 | testType(t, 14, typ, "[]uint32"); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 237 | } |
Russ Cox | 387df5e | 2008-11-24 14:51:33 -0800 | [diff] [blame] | 238 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 239 | func TestInterfaceGet(t *testing.T) { |
Russ Cox | 387df5e | 2008-11-24 14:51:33 -0800 | [diff] [blame] | 240 | var inter struct { e interface{ } }; |
| 241 | inter.e = 123.456; |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 242 | v1 := NewValue(&inter); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 243 | v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0); |
| 244 | assert(t, v2.Type().String(), "interface { }"); |
| 245 | i2 := v2.(*InterfaceValue).Interface(); |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 246 | v3 := NewValue(i2); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 247 | assert(t, v3.Type().String(), "float"); |
Russ Cox | 387df5e | 2008-11-24 14:51:33 -0800 | [diff] [blame] | 248 | } |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 249 | |
Russ Cox | ac6ebfd | 2009-04-06 21:28:04 -0700 | [diff] [blame] | 250 | func TestInterfaceValue(t *testing.T) { |
| 251 | var inter struct { e interface{ } }; |
| 252 | inter.e = 123.456; |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 253 | v1 := NewValue(&inter); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 254 | v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0); |
| 255 | assert(t, v2.Type().String(), "interface { }"); |
| 256 | v3 := v2.(*InterfaceValue).Elem(); |
| 257 | assert(t, v3.Type().String(), "float"); |
Russ Cox | 64627b0 | 2009-04-15 00:55:58 -0700 | [diff] [blame] | 258 | |
Russ Cox | 4b8c13d | 2009-04-14 19:03:57 -0700 | [diff] [blame] | 259 | i3 := v2.Interface(); |
| 260 | if f, ok := i3.(float); !ok { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 261 | t.Error("v2.Interface() did not return float, got ", Typeof(i3)); |
Russ Cox | 4b8c13d | 2009-04-14 19:03:57 -0700 | [diff] [blame] | 262 | } |
Russ Cox | ac6ebfd | 2009-04-06 21:28:04 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Ian Lance Taylor | ca9765d | 2009-04-14 06:46:01 -0700 | [diff] [blame] | 265 | func TestFunctionValue(t *testing.T) { |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 266 | v := NewValue(func() {}); |
Ian Lance Taylor | ca9765d | 2009-04-14 06:46:01 -0700 | [diff] [blame] | 267 | if v.Interface() != v.Interface() { |
| 268 | t.Fatalf("TestFunction != itself"); |
| 269 | } |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 270 | assert(t, v.Type().String(), "func()"); |
Ian Lance Taylor | ca9765d | 2009-04-14 06:46:01 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 273 | func TestCopyArray(t *testing.T) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 274 | a := []int{ 1, 2, 3, 4, 10, 9, 8, 7 }; |
| 275 | b := []int{ 11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44 }; |
| 276 | c := []int{ 11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44 }; |
Russ Cox | d47d888 | 2008-12-18 22:37:22 -0800 | [diff] [blame] | 277 | va := NewValue(&a); |
| 278 | vb := NewValue(&b); |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 279 | for i := 0; i < len(b); i++ { |
| 280 | if b[i] != c[i] { |
| 281 | t.Fatalf("b != c before test"); |
| 282 | } |
| 283 | } |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 284 | aa := va.(*PtrValue).Elem().(*SliceValue); |
| 285 | ab := vb.(*PtrValue).Elem().(*SliceValue); |
Russ Cox | d47d888 | 2008-12-18 22:37:22 -0800 | [diff] [blame] | 286 | for tocopy := 1; tocopy <= 7; tocopy++ { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 287 | aa.SetLen(tocopy); |
| 288 | ArrayCopy(ab, aa); |
| 289 | aa.SetLen(8); |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 290 | for i := 0; i < tocopy; i++ { |
| 291 | if a[i] != b[i] { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 292 | t.Errorf("(i) tocopy=%d a[%d]=%d, b[%d]=%d", |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 293 | tocopy, i, a[i], i, b[i]); |
| 294 | } |
| 295 | } |
| 296 | for i := tocopy; i < len(b); i++ { |
| 297 | if b[i] != c[i] { |
| 298 | if i < len(a) { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 299 | t.Errorf("(ii) tocopy=%d a[%d]=%d, b[%d]=%d, c[%d]=%d", |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 300 | tocopy, i, a[i], i, b[i], i, c[i]); |
| 301 | } else { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 302 | t.Errorf("(iii) tocopy=%d b[%d]=%d, c[%d]=%d", |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 303 | tocopy, i, b[i], i, c[i]); |
| 304 | } |
Russ Cox | d47d888 | 2008-12-18 22:37:22 -0800 | [diff] [blame] | 305 | } else { |
| 306 | t.Logf("tocopy=%d elem %d is okay\n", tocopy, i); |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | } |
| 310 | } |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 311 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 312 | func TestBigUnnamedStruct(t *testing.T) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 313 | b := struct{a,b,c,d int64}{1, 2, 3, 4}; |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 314 | v := NewValue(b); |
| 315 | b1 := v.Interface().(struct{a,b,c,d int64}); |
| 316 | if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 317 | t.Errorf("NewValue(%v).Interface().(*Big) = %v", b, b1); |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 321 | type big struct { |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 322 | a, b, c, d, e int64 |
| 323 | } |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 324 | func TestBigStruct(t *testing.T) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 325 | b := big{1, 2, 3, 4, 5}; |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 326 | v := NewValue(b); |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 327 | b1 := v.Interface().(big); |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 328 | if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d || b1.e != b.e { |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 329 | t.Errorf("NewValue(%v).Interface().(big) = %v", b, b1); |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 330 | } |
| 331 | } |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 332 | |
| 333 | type Basic struct { |
| 334 | x int; |
| 335 | y float32 |
| 336 | } |
| 337 | |
Russ Cox | 64627b0 | 2009-04-15 00:55:58 -0700 | [diff] [blame] | 338 | type NotBasic Basic |
| 339 | |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 340 | type DeepEqualTest struct { |
| 341 | a, b interface{}; |
| 342 | eq bool; |
| 343 | } |
| 344 | |
| 345 | var deepEqualTests = []DeepEqualTest { |
| 346 | // Equalities |
| 347 | DeepEqualTest{ 1, 1, true }, |
| 348 | DeepEqualTest{ int32(1), int32(1), true }, |
| 349 | DeepEqualTest{ 0.5, 0.5, true }, |
| 350 | DeepEqualTest{ float32(0.5), float32(0.5), true }, |
| 351 | DeepEqualTest{ "hello", "hello", true }, |
| 352 | DeepEqualTest{ make([]int, 10), make([]int, 10), true }, |
| 353 | DeepEqualTest{ &[3]int{ 1, 2, 3 }, &[3]int{ 1, 2, 3 }, true }, |
| 354 | DeepEqualTest{ Basic{ 1, 0.5 }, Basic{ 1, 0.5 }, true }, |
Russ Cox | 4866223 | 2009-06-25 14:25:38 -0700 | [diff] [blame] | 355 | DeepEqualTest{ os.Error(nil), os.Error(nil), true }, |
Rob Pike | 1880b90 | 2009-07-10 11:20:10 -0700 | [diff] [blame] | 356 | DeepEqualTest{ map[int]string{ 1:"one", 2:"two" }, map[int]string{ 2:"two", 1:"one" }, true }, |
Russ Cox | a439f66 | 2009-07-01 16:45:09 -0700 | [diff] [blame] | 357 | |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 358 | // Inequalities |
| 359 | DeepEqualTest{ 1, 2, false }, |
| 360 | DeepEqualTest{ int32(1), int32(2), false }, |
| 361 | DeepEqualTest{ 0.5, 0.6, false }, |
| 362 | DeepEqualTest{ float32(0.5), float32(0.6), false }, |
| 363 | DeepEqualTest{ "hello", "hey", false }, |
| 364 | DeepEqualTest{ make([]int, 10), make([]int, 11), false }, |
| 365 | DeepEqualTest{ &[3]int{ 1, 2, 3 }, &[3]int{ 1, 2, 4 }, false }, |
| 366 | DeepEqualTest{ Basic{ 1, 0.5 }, Basic{ 1, 0.6 }, false }, |
Russ Cox | a439f66 | 2009-07-01 16:45:09 -0700 | [diff] [blame] | 367 | DeepEqualTest{ Basic{ 1, 0 }, Basic{ 2, 0 }, false }, |
Rob Pike | 1880b90 | 2009-07-10 11:20:10 -0700 | [diff] [blame] | 368 | DeepEqualTest{ map[int]string{ 1:"one", 3:"two" }, map[int]string{ 2:"two", 1:"one" }, false }, |
| 369 | DeepEqualTest{ map[int]string{ 1:"one", 2:"txo" }, map[int]string{ 2:"two", 1:"one" }, false }, |
| 370 | DeepEqualTest{ map[int]string{ 1:"one", }, map[int]string{ 2:"two", 1:"one" }, false }, |
| 371 | DeepEqualTest{ map[int]string{ 2:"two", 1:"one" }, map[int]string{ 1:"one", }, false }, |
Russ Cox | a439f66 | 2009-07-01 16:45:09 -0700 | [diff] [blame] | 372 | |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 373 | // Mismatched types |
| 374 | DeepEqualTest{ 1, 1.0, false }, |
| 375 | DeepEqualTest{ int32(1), int64(1), false }, |
| 376 | DeepEqualTest{ 0.5, "hello", false }, |
| 377 | DeepEqualTest{ []int{ 1, 2, 3 }, [3]int{ 1, 2, 3 }, false }, |
| 378 | DeepEqualTest{ &[3]interface{} { 1, 2, 4 }, &[3]interface{} { 1, 2, "s" }, false }, |
Russ Cox | 64627b0 | 2009-04-15 00:55:58 -0700 | [diff] [blame] | 379 | DeepEqualTest{ Basic{ 1, 0.5 }, NotBasic{ 1, 0.5 }, false }, |
Rob Pike | 1880b90 | 2009-07-10 11:20:10 -0700 | [diff] [blame] | 380 | DeepEqualTest{ map[uint]string{ 1:"one", 2:"two" }, map[int]string{ 2:"two", 1:"one" }, false }, |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | func TestDeepEqual(t *testing.T) { |
| 384 | for i, test := range deepEqualTests { |
| 385 | if r := DeepEqual(test.a, test.b); r != test.eq { |
| 386 | t.Errorf("DeepEqual(%v, %v) = %v, want %v", test.a, test.b, r, test.eq); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
Russ Cox | 4866223 | 2009-06-25 14:25:38 -0700 | [diff] [blame] | 391 | func TestTypeof(t *testing.T) { |
| 392 | for i, test := range deepEqualTests { |
| 393 | v := NewValue(test.a); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 394 | if v == nil { |
| 395 | continue; |
| 396 | } |
Russ Cox | 4866223 | 2009-06-25 14:25:38 -0700 | [diff] [blame] | 397 | typ := Typeof(test.a); |
| 398 | if typ != v.Type() { |
| 399 | t.Errorf("Typeof(%v) = %v, but NewValue(%v).Type() = %v", test.a, typ, test.a, v.Type()); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
Rob Pike | 1880b90 | 2009-07-10 11:20:10 -0700 | [diff] [blame] | 404 | type Recursive struct { |
| 405 | x int; |
| 406 | r *Recursive |
| 407 | } |
| 408 | |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 409 | func TestDeepEqualRecursiveStruct(t *testing.T) { |
| 410 | a, b := new(Recursive), new(Recursive); |
| 411 | *a = Recursive{ 12, a }; |
| 412 | *b = Recursive{ 12, b }; |
| 413 | if !DeepEqual(a, b) { |
| 414 | t.Error("DeepEqual(recursive same) = false, want true"); |
| 415 | } |
| 416 | } |
| 417 | |
Rob Pike | 1880b90 | 2009-07-10 11:20:10 -0700 | [diff] [blame] | 418 | type Complex struct { |
| 419 | a int; |
| 420 | b [3]*Complex; |
| 421 | c *string; |
| 422 | d map[float]float |
| 423 | } |
| 424 | |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 425 | func TestDeepEqualComplexStruct(t *testing.T) { |
| 426 | m := make(map[float]float); |
| 427 | stra, strb := "hello", "hello"; |
| 428 | a, b := new(Complex), new(Complex); |
| 429 | *a = Complex{5, [3]*Complex{a, b, a}, &stra, m}; |
| 430 | *b = Complex{5, [3]*Complex{b, a, a}, &strb, m}; |
| 431 | if !DeepEqual(a, b) { |
| 432 | t.Error("DeepEqual(complex same) = false, want true"); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | func TestDeepEqualComplexStructInequality(t *testing.T) { |
| 437 | m := make(map[float]float); |
| 438 | stra, strb := "hello", "helloo"; // Difference is here |
| 439 | a, b := new(Complex), new(Complex); |
| 440 | *a = Complex{5, [3]*Complex{a, b, a}, &stra, m}; |
| 441 | *b = Complex{5, [3]*Complex{b, a, a}, &strb, m}; |
| 442 | if DeepEqual(a, b) { |
| 443 | t.Error("DeepEqual(complex different) = true, want false"); |
| 444 | } |
| 445 | } |
Rob Pike | 93831d2 | 2009-04-29 22:16:53 -0700 | [diff] [blame] | 446 | |
| 447 | |
| 448 | func check2ndField(x interface{}, offs uintptr, t *testing.T) { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 449 | s := NewValue(x).(*StructValue); |
| 450 | f := s.Type().(*StructType).Field(1); |
| 451 | if f.Offset != offs { |
| 452 | t.Error("mismatched offsets in structure alignment:", f.Offset, offs); |
Rob Pike | 93831d2 | 2009-04-29 22:16:53 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
| 456 | // Check that structure alignment & offsets viewed through reflect agree with those |
| 457 | // from the compiler itself. |
| 458 | func TestAlignment(t *testing.T) { |
| 459 | type T1inner struct { |
| 460 | a int |
| 461 | } |
| 462 | type T1 struct { |
| 463 | T1inner; |
| 464 | f int; |
| 465 | } |
| 466 | type T2inner struct { |
| 467 | a, b int |
| 468 | } |
| 469 | type T2 struct { |
| 470 | T2inner; |
| 471 | f int; |
| 472 | } |
| 473 | |
| 474 | x := T1{T1inner{2}, 17}; |
| 475 | check2ndField(x, uintptr(unsafe.Pointer(&x.f)) - uintptr(unsafe.Pointer(&x)), t); |
| 476 | |
| 477 | x1 := T2{T2inner{2, 3}, 17}; |
| 478 | check2ndField(x1, uintptr(unsafe.Pointer(&x1.f)) - uintptr(unsafe.Pointer(&x1)), t); |
| 479 | } |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 480 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 481 | type IsNiller interface { |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 482 | IsNil() bool |
| 483 | } |
| 484 | |
| 485 | func Nil(a interface{}, t *testing.T) { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 486 | n := NewValue(a).(*StructValue).Field(0).(IsNiller); |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 487 | if !n.IsNil() { |
| 488 | t.Errorf("%v should be nil", a) |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | func NotNil(a interface{}, t *testing.T) { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 493 | n := NewValue(a).(*StructValue).Field(0).(IsNiller); |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 494 | if n.IsNil() { |
| 495 | t.Errorf("value of type %v should not be nil", NewValue(a).Type().String()) |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | func TestIsNil(t *testing.T) { |
| 500 | // These do not implement IsNil |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 501 | doNotNil := []interface{}{ int(0), float32(0), struct{a int}{} }; |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 502 | for i, ts := range doNotNil { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 503 | ty := Typeof(ts); |
| 504 | v := MakeZero(ty); |
| 505 | if nilable, ok := v.(IsNiller); ok { |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 506 | t.Errorf("%s is nilable; should not be", ts) |
| 507 | } |
| 508 | } |
| 509 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 510 | // These do implement IsNil. |
| 511 | // Wrap in extra struct to hide interface type. |
| 512 | doNil := []interface{}{ |
| 513 | struct{x *int}{}, |
| 514 | struct{x interface{}}{}, |
| 515 | struct{x map[string]int}{}, |
| 516 | struct{x func()bool}{}, |
| 517 | struct{x chan int}{}, |
| 518 | struct{x []string}{} |
| 519 | }; |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 520 | for i, ts := range doNil { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 521 | ty := Typeof(ts).(*StructType).Field(0).Type; |
| 522 | v := MakeZero(ty); |
| 523 | if nilable, ok := v.(IsNiller); !ok { |
Russ Cox | 2d5d4a1 | 2009-05-20 15:42:14 -0700 | [diff] [blame] | 524 | t.Errorf("%s %T is not nilable; should be", ts, v) |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 527 | |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 528 | // Check the implementations |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 529 | var pi struct {x *int} |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 530 | Nil(pi, t); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 531 | pi.x = new(int); |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 532 | NotNil(pi, t); |
| 533 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 534 | var si struct {x []int} |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 535 | Nil(si, t); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 536 | si.x = make([]int, 10); |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 537 | NotNil(si, t); |
| 538 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 539 | var ci struct {x chan int} |
| 540 | Nil(ci, t); |
| 541 | ci.x = make(chan int); |
| 542 | NotNil(ci, t); |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 543 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 544 | var mi struct {x map[int]int} |
| 545 | Nil(mi, t); |
| 546 | mi.x = make(map[int]int); |
| 547 | NotNil(mi, t); |
| 548 | |
| 549 | var ii struct {x interface {}} |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 550 | Nil(ii, t); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 551 | ii.x = 2; |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 552 | NotNil(ii, t); |
| 553 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 554 | var fi struct {x func(t *testing.T)} |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 555 | Nil(fi, t); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 556 | fi.x = TestIsNil; |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 557 | NotNil(fi, t); |
| 558 | } |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 559 | |
| 560 | func TestInterfaceExtraction(t *testing.T) { |
| 561 | var s struct { |
| 562 | w io.Writer; |
| 563 | } |
| 564 | |
| 565 | s.w = os.Stdout; |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 566 | v := Indirect(NewValue(&s)).(*StructValue).Field(0).Interface(); |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 567 | if v != s.w.(interface{}) { |
Rob Pike | b75df2f | 2009-07-16 15:01:21 -0700 | [diff] [blame] | 568 | t.Error("Interface() on interface: ", v, s.w); |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
Russ Cox | f966623 | 2009-05-21 14:06:43 -0700 | [diff] [blame] | 571 | |
| 572 | func TestInterfaceEditing(t *testing.T) { |
| 573 | // strings are bigger than one word, |
| 574 | // so the interface conversion allocates |
| 575 | // memory to hold a string and puts that |
| 576 | // pointer in the interface. |
| 577 | var i interface{} = "hello"; |
| 578 | |
| 579 | // if i pass the interface value by value |
| 580 | // to NewValue, i should get a fresh copy |
| 581 | // of the value. |
| 582 | v := NewValue(i); |
| 583 | |
| 584 | // and setting that copy to "bye" should |
| 585 | // not change the value stored in i. |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 586 | v.(*StringValue).Set("bye"); |
Russ Cox | f966623 | 2009-05-21 14:06:43 -0700 | [diff] [blame] | 587 | if i.(string) != "hello" { |
| 588 | t.Errorf(`Set("bye") changed i to %s`, i.(string)); |
| 589 | } |
| 590 | |
| 591 | // the same should be true of smaller items. |
| 592 | i = 123; |
| 593 | v = NewValue(i); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 594 | v.(*IntValue).Set(234); |
Russ Cox | f966623 | 2009-05-21 14:06:43 -0700 | [diff] [blame] | 595 | if i.(int) != 123 { |
| 596 | t.Errorf("Set(234) changed i to %d", i.(int)); |
| 597 | } |
| 598 | } |
David Symonds | d4e57ff | 2009-06-15 18:35:04 -0700 | [diff] [blame] | 599 | |
| 600 | func TestNilPtrValueSub(t *testing.T) { |
| 601 | var pi *int; |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 602 | if pv := NewValue(pi).(*PtrValue); pv.Elem() != nil { |
| 603 | t.Error("NewValue((*int)(nil)).(*PtrValue).Elem() != nil"); |
David Symonds | d4e57ff | 2009-06-15 18:35:04 -0700 | [diff] [blame] | 604 | } |
| 605 | } |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 606 | |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 607 | func TestMap(t *testing.T) { |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 608 | m := map[string]int{ "a": 1, "b": 2 }; |
| 609 | mv := NewValue(m).(*MapValue); |
| 610 | if n := mv.Len(); n != len(m) { |
| 611 | t.Errorf("Len = %d, want %d", n, len(m)); |
| 612 | } |
| 613 | keys := mv.Keys(); |
| 614 | i := 0; |
| 615 | newmap := MakeMap(mv.Type().(*MapType)); |
| 616 | for k, v := range m { |
| 617 | // Check that returned Keys match keys in range. |
| 618 | // These aren't required to be in the same order, |
| 619 | // but they are in this implementation, which makes |
| 620 | // the test easier. |
| 621 | if i >= len(keys) { |
| 622 | t.Errorf("Missing key #%d %q", i, k); |
| 623 | } else if kv := keys[i].(*StringValue); kv.Get() != k { |
| 624 | t.Errorf("Keys[%d] = %q, want %q", i, kv.Get(), k); |
| 625 | } |
| 626 | i++; |
| 627 | |
| 628 | // Check that value lookup is correct. |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 629 | vv := mv.Elem(NewValue(k)); |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 630 | if vi := vv.(*IntValue).Get(); vi != v { |
| 631 | t.Errorf("Key %q: have value %d, want %d", vi, v); |
| 632 | } |
| 633 | |
| 634 | // Copy into new map. |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 635 | newmap.SetElem(NewValue(k), NewValue(v)); |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 636 | } |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 637 | vv := mv.Elem(NewValue("not-present")); |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 638 | if vv != nil { |
| 639 | t.Errorf("Invalid key: got non-nil value %s", valueToString(vv)); |
| 640 | } |
| 641 | |
| 642 | newm := newmap.Interface().(map[string]int); |
| 643 | if len(newm) != len(m) { |
| 644 | t.Errorf("length after copy: newm=%d, m=%d", newm, m); |
| 645 | } |
| 646 | |
| 647 | for k, v := range newm { |
| 648 | mv, ok := m[k]; |
| 649 | if mv != v { |
| 650 | t.Errorf("newm[%q] = %d, but m[%q] = %d, %v", k, v, k, mv, ok); |
| 651 | } |
| 652 | } |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 653 | |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 654 | newmap.SetElem(NewValue("a"), nil); |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 655 | v, ok := newm["a"]; |
| 656 | if ok { |
| 657 | t.Errorf("newm[\"a\"] = %d after delete", v); |
| 658 | } |
| 659 | } |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 660 | |
| 661 | func TestChan(t *testing.T) { |
| 662 | for loop := 0; loop < 2; loop++ { |
| 663 | var c chan int; |
| 664 | var cv *ChanValue; |
| 665 | |
| 666 | // check both ways to allocate channels |
| 667 | switch loop { |
| 668 | case 1: |
| 669 | c = make(chan int, 1); |
| 670 | cv = NewValue(c).(*ChanValue); |
| 671 | case 0: |
| 672 | cv = MakeChan(Typeof(c).(*ChanType), 1); |
| 673 | c = cv.Interface().(chan int); |
| 674 | } |
| 675 | |
| 676 | // Send |
| 677 | cv.Send(NewValue(2)); |
| 678 | if i := <-c; i != 2 { |
| 679 | t.Errorf("reflect Send 2, native recv %d", i); |
| 680 | } |
| 681 | |
| 682 | // Recv |
| 683 | c <- 3; |
| 684 | if i := cv.Recv().(*IntValue).Get(); i != 3 { |
| 685 | t.Errorf("native send 3, reflect Recv %d", i); |
| 686 | } |
| 687 | |
| 688 | // TryRecv fail |
| 689 | val := cv.TryRecv(); |
| 690 | if val != nil { |
| 691 | t.Errorf("TryRecv on empty chan: %s", valueToString(val)); |
| 692 | } |
| 693 | |
| 694 | // TryRecv success |
| 695 | c <- 4; |
| 696 | val = cv.TryRecv(); |
| 697 | if val == nil { |
| 698 | t.Errorf("TryRecv on ready chan got nil"); |
| 699 | } else if i := val.(*IntValue).Get(); i != 4 { |
| 700 | t.Errorf("native send 4, TryRecv %d", i); |
| 701 | } |
| 702 | |
| 703 | // TrySend fail |
| 704 | c <- 100; |
| 705 | ok := cv.TrySend(NewValue(5)); |
| 706 | i := <-c; |
| 707 | if ok { |
| 708 | t.Errorf("TrySend on full chan succeeded: value %d", i); |
| 709 | } |
| 710 | |
| 711 | // TrySend success |
| 712 | ok = cv.TrySend(NewValue(6)); |
| 713 | if !ok { |
| 714 | t.Errorf("TrySend on empty chan failed"); |
| 715 | } else { |
| 716 | if i = <-c; i != 6 { |
| 717 | t.Errorf("TrySend 6, recv %d", i); |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | // check creation of unbuffered channel |
| 723 | var c chan int; |
| 724 | cv := MakeChan(Typeof(c).(*ChanType), 0); |
| 725 | c = cv.Interface().(chan int); |
| 726 | if cv.TrySend(NewValue(7)) { |
| 727 | t.Errorf("TrySend on sync chan succeeded"); |
| 728 | } |
| 729 | if cv.TryRecv() != nil { |
| 730 | t.Errorf("TryRecv on sync chan succeeded"); |
| 731 | } |
| 732 | } |
| 733 | |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 734 | // Difficult test for function call because of |
| 735 | // implicit padding between arguments. |
| 736 | func dummy(b byte, c int, d byte) (i byte, j int, k byte){ |
| 737 | return b, c, d; |
| 738 | } |
| 739 | |
| 740 | func TestFunc(t *testing.T) { |
| 741 | ret := NewValue(dummy).(*FuncValue).Call([]Value{NewValue(byte(10)), NewValue(20), NewValue(byte(30))}); |
| 742 | if len(ret) != 3 { |
| 743 | t.Fatalf("Call returned %d values, want 3", len(ret)); |
| 744 | } |
| 745 | |
| 746 | i := ret[0].(*Uint8Value).Get(); |
| 747 | j := ret[1].(*IntValue).Get(); |
| 748 | k := ret[2].(*Uint8Value).Get(); |
| 749 | if i != 10 || j != 20 || k != 30 { |
| 750 | t.Errorf("Call returned %d, %d, %d; want 10, 20, 30", i, j, k); |
| 751 | } |
| 752 | } |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 753 | |
| 754 | type Point struct { |
| 755 | x, y int; |
| 756 | } |
| 757 | |
| 758 | func (p Point) Dist(scale int) int { |
| 759 | return p.x*p.x*scale + p.y*p.y*scale; |
| 760 | } |
| 761 | |
| 762 | func TestMethod(t *testing.T) { |
| 763 | // Non-curried method of type. |
| 764 | p := Point{3, 4}; |
| 765 | i := reflect.Typeof(p).Method(0).Func.Call([]Value{NewValue(p), NewValue(10)})[0].(*IntValue).Get(); |
| 766 | if i != 250 { |
| 767 | t.Errorf("Type Method returned %d; want 250", i); |
| 768 | } |
| 769 | |
| 770 | // Curried method of value. |
| 771 | i = NewValue(p).Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get(); |
| 772 | if i != 250 { |
| 773 | t.Errorf("Value Method returned %d; want 250", i); |
| 774 | } |
| 775 | |
| 776 | // Curried method of interface value. |
| 777 | // Have to wrap interface value in a struct to get at it. |
| 778 | // Passing it to NewValue directly would |
| 779 | // access the underlying Point, not the interface. |
| 780 | var s = struct{x interface{Dist(int) int}}{p}; |
| 781 | pv := NewValue(s).(*StructValue).Field(0); |
| 782 | i = pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get(); |
| 783 | if i != 250 { |
| 784 | t.Errorf("Interface Method returned %d; want 250", i); |
| 785 | } |
| 786 | } |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 787 | |
| 788 | func TestInterfaceSet(t *testing.T) { |
| 789 | p := &Point{3, 4}; |
| 790 | |
| 791 | var s struct { |
| 792 | I interface {}; |
| 793 | P interface { Dist(int)int }; |
| 794 | } |
| 795 | sv := NewValue(&s).(*PtrValue).Elem().(*StructValue); |
| 796 | sv.Field(0).(*InterfaceValue).Set(NewValue(p)); |
| 797 | if q := s.I.(*Point); q != p { |
| 798 | t.Errorf("i: have %p want %p", q, p); |
| 799 | } |
| 800 | |
| 801 | pv := sv.Field(1).(*InterfaceValue); |
| 802 | pv.Set(NewValue(p)); |
| 803 | if q := s.P.(*Point); q != p { |
| 804 | t.Errorf("i: have %p want %p", q, p); |
| 805 | } |
| 806 | |
| 807 | i := pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get(); |
| 808 | if i != 250 { |
| 809 | t.Errorf("Interface Method returned %d; want 250", i); |
| 810 | } |
| 811 | } |