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 | |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 5 | package reflect_test |
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"; |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [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" }, |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 53 | pair { struct { x (**integer) }{}, "**reflect_test.integer" }, |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 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)" }, |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 60 | pair { struct { x struct {c func(chan *integer, *int8)} }{}, "struct { c func(chan *reflect_test.integer, *int8) }" }, |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 61 | pair { struct { x struct {a int8; b int32} }{}, "struct { a int8; b int32 }" }, |
Ian Lance Taylor | 39808db | 2009-07-20 23:26:03 -0700 | [diff] [blame] | 62 | pair { struct { x struct {a int8; b int8; c int32} }{}, "struct { a int8; b int8; c int32 }" }, |
| 63 | pair { struct { x struct {a int8; b int8; c int8; d int32} }{}, "struct { a int8; b int8; c int8; d int32 }" }, |
| 64 | pair { struct { x struct {a int8; b int8; c int8; d int8; e int32} }{}, "struct { a int8; b int8; c int8; d int8; e int32 }" }, |
| 65 | pair { struct { x struct {a int8; b int8; c int8; d int8; e int8; f int32} }{}, "struct { a int8; b int8; c int8; d int8; e int8; f int32 }" }, |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 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}" }, |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 88 | pair { (**integer)(nil), "**reflect_test.integer(0)" }, |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 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)" }, |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 93 | pair { (struct {c func(chan *integer, *int8)}){}, "struct { c func(chan *reflect_test.integer, *int8) }{func(chan *reflect_test.integer, *int8)(0)}" }, |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 94 | pair { (struct {a int8; b int32}){}, "struct { a int8; b int32 }{0, 0}" }, |
Ian Lance Taylor | 39808db | 2009-07-20 23:26:03 -0700 | [diff] [blame] | 95 | pair { (struct {a int8; b int8; c int32}){}, "struct { a int8; b int8; c int32 }{0, 0, 0}" }, |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 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) { |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 115 | case *IntValue: |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 116 | v.Set(132); |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 117 | case *Int8Value: |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 118 | v.Set(8); |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 119 | case *Int16Value: |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 120 | v.Set(16); |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 121 | case *Int32Value: |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 122 | v.Set(32); |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 123 | case *Int64Value: |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 124 | v.Set(64); |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 125 | case *UintValue: |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 126 | v.Set(132); |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 127 | case *Uint8Value: |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 128 | v.Set(8); |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 129 | case *Uint16Value: |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 130 | v.Set(16); |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 131 | case *Uint32Value: |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 132 | v.Set(32); |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 133 | case *Uint64Value: |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 134 | v.Set(64); |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 135 | case *FloatValue: |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 136 | v.Set(3200.0); |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 137 | case *Float32Value: |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 138 | v.Set(32.1); |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 139 | case *Float64Value: |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 140 | v.Set(64.2); |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 141 | case *StringValue: |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 142 | v.Set("stringy cheese"); |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 143 | case *BoolValue: |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 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" }, |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 160 | pair { T{123, 456.75, "hello", &_i}, "reflect_test.T{123, 456.75, hello, *int(&7)}" }, |
| 161 | pair { new(chan *T), "*chan *reflect_test.T(&chan *reflect_test.T)" }, |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 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"); |
Rob Pike | a93c5c8 | 2009-07-16 18:21:14 -0700 | [diff] [blame] | 217 | |
| 218 | f, present := styp.FieldByName("d"); |
| 219 | if !present { |
| 220 | t.Errorf("FieldByName says present field is absent"); |
| 221 | } |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 222 | testType(t, 6, f.Type, "float32"); |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 223 | |
Rob Pike | a93c5c8 | 2009-07-16 18:21:14 -0700 | [diff] [blame] | 224 | f, present = styp.FieldByName("absent"); |
| 225 | if present { |
| 226 | t.Errorf("FieldByName says absent field is present"); |
| 227 | } |
| 228 | |
Ian Lance Taylor | f0c00f7 | 2009-07-21 14:06:14 -0700 | [diff] [blame] | 229 | typ = Typeof([32]int32{}); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 230 | testType(t, 7, typ, "[32]int32"); |
| 231 | testType(t, 8, typ.(*ArrayType).Elem(), "int32"); |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 232 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 233 | typ = Typeof((map[string]*int32)(nil)); |
| 234 | testType(t, 9, typ, "map[string] *int32"); |
| 235 | mtyp := typ.(*MapType); |
| 236 | testType(t, 10, mtyp.Key(), "string"); |
| 237 | testType(t, 11, mtyp.Elem(), "*int32"); |
Rob Pike | bdbb958 | 2008-11-05 08:17:01 -0800 | [diff] [blame] | 238 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 239 | typ = Typeof((chan<-string)(nil)); |
| 240 | testType(t, 12, typ, "chan<- string"); |
| 241 | testType(t, 13, typ.(*ChanType).Elem(), "string"); |
Rob Pike | 5a1cbe8 | 2008-11-05 13:01:33 -0800 | [diff] [blame] | 242 | |
| 243 | // make sure tag strings are not part of element type |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 244 | typ = Typeof(struct{d []uint32 "TAG"}{}).(*StructType).Field(0).Type; |
| 245 | testType(t, 14, typ, "[]uint32"); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 246 | } |
Russ Cox | 387df5e | 2008-11-24 14:51:33 -0800 | [diff] [blame] | 247 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 248 | func TestInterfaceGet(t *testing.T) { |
Russ Cox | 387df5e | 2008-11-24 14:51:33 -0800 | [diff] [blame] | 249 | var inter struct { e interface{ } }; |
| 250 | inter.e = 123.456; |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 251 | v1 := NewValue(&inter); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 252 | v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0); |
| 253 | assert(t, v2.Type().String(), "interface { }"); |
| 254 | i2 := v2.(*InterfaceValue).Interface(); |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 255 | v3 := NewValue(i2); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 256 | assert(t, v3.Type().String(), "float"); |
Russ Cox | 387df5e | 2008-11-24 14:51:33 -0800 | [diff] [blame] | 257 | } |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 258 | |
Russ Cox | ac6ebfd | 2009-04-06 21:28:04 -0700 | [diff] [blame] | 259 | func TestInterfaceValue(t *testing.T) { |
| 260 | var inter struct { e interface{ } }; |
| 261 | inter.e = 123.456; |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 262 | v1 := NewValue(&inter); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 263 | v2 := v1.(*PtrValue).Elem().(*StructValue).Field(0); |
| 264 | assert(t, v2.Type().String(), "interface { }"); |
| 265 | v3 := v2.(*InterfaceValue).Elem(); |
| 266 | assert(t, v3.Type().String(), "float"); |
Russ Cox | 64627b0 | 2009-04-15 00:55:58 -0700 | [diff] [blame] | 267 | |
Russ Cox | 4b8c13d | 2009-04-14 19:03:57 -0700 | [diff] [blame] | 268 | i3 := v2.Interface(); |
| 269 | if f, ok := i3.(float); !ok { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 270 | t.Error("v2.Interface() did not return float, got ", Typeof(i3)); |
Russ Cox | 4b8c13d | 2009-04-14 19:03:57 -0700 | [diff] [blame] | 271 | } |
Russ Cox | ac6ebfd | 2009-04-06 21:28:04 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Ian Lance Taylor | ca9765d | 2009-04-14 06:46:01 -0700 | [diff] [blame] | 274 | func TestFunctionValue(t *testing.T) { |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 275 | v := NewValue(func() {}); |
Ian Lance Taylor | ca9765d | 2009-04-14 06:46:01 -0700 | [diff] [blame] | 276 | if v.Interface() != v.Interface() { |
| 277 | t.Fatalf("TestFunction != itself"); |
| 278 | } |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 279 | assert(t, v.Type().String(), "func()"); |
Ian Lance Taylor | ca9765d | 2009-04-14 06:46:01 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 282 | func TestCopyArray(t *testing.T) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 283 | a := []int{ 1, 2, 3, 4, 10, 9, 8, 7 }; |
| 284 | b := []int{ 11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44 }; |
| 285 | 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] | 286 | va := NewValue(&a); |
| 287 | vb := NewValue(&b); |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 288 | for i := 0; i < len(b); i++ { |
| 289 | if b[i] != c[i] { |
| 290 | t.Fatalf("b != c before test"); |
| 291 | } |
| 292 | } |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 293 | aa := va.(*PtrValue).Elem().(*SliceValue); |
| 294 | ab := vb.(*PtrValue).Elem().(*SliceValue); |
Russ Cox | d47d888 | 2008-12-18 22:37:22 -0800 | [diff] [blame] | 295 | for tocopy := 1; tocopy <= 7; tocopy++ { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 296 | aa.SetLen(tocopy); |
| 297 | ArrayCopy(ab, aa); |
| 298 | aa.SetLen(8); |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 299 | for i := 0; i < tocopy; i++ { |
| 300 | if a[i] != b[i] { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 301 | t.Errorf("(i) tocopy=%d a[%d]=%d, b[%d]=%d", |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 302 | tocopy, i, a[i], i, b[i]); |
| 303 | } |
| 304 | } |
| 305 | for i := tocopy; i < len(b); i++ { |
| 306 | if b[i] != c[i] { |
| 307 | if i < len(a) { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 308 | 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] | 309 | tocopy, i, a[i], i, b[i], i, c[i]); |
| 310 | } else { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 311 | t.Errorf("(iii) tocopy=%d b[%d]=%d, c[%d]=%d", |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 312 | tocopy, i, b[i], i, c[i]); |
| 313 | } |
Russ Cox | d47d888 | 2008-12-18 22:37:22 -0800 | [diff] [blame] | 314 | } else { |
| 315 | t.Logf("tocopy=%d elem %d is okay\n", tocopy, i); |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 320 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 321 | func TestBigUnnamedStruct(t *testing.T) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 322 | b := struct{a,b,c,d int64}{1, 2, 3, 4}; |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 323 | v := NewValue(b); |
| 324 | b1 := v.Interface().(struct{a,b,c,d int64}); |
| 325 | 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] | 326 | t.Errorf("NewValue(%v).Interface().(*Big) = %v", b, b1); |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 330 | type big struct { |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 331 | a, b, c, d, e int64 |
| 332 | } |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 333 | func TestBigStruct(t *testing.T) { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 334 | b := big{1, 2, 3, 4, 5}; |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 335 | v := NewValue(b); |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 336 | b1 := v.Interface().(big); |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 337 | 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] | 338 | t.Errorf("NewValue(%v).Interface().(big) = %v", b, b1); |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 339 | } |
| 340 | } |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 341 | |
| 342 | type Basic struct { |
| 343 | x int; |
| 344 | y float32 |
| 345 | } |
| 346 | |
Russ Cox | 64627b0 | 2009-04-15 00:55:58 -0700 | [diff] [blame] | 347 | type NotBasic Basic |
| 348 | |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 349 | type DeepEqualTest struct { |
| 350 | a, b interface{}; |
| 351 | eq bool; |
| 352 | } |
| 353 | |
| 354 | var deepEqualTests = []DeepEqualTest { |
| 355 | // Equalities |
| 356 | DeepEqualTest{ 1, 1, true }, |
| 357 | DeepEqualTest{ int32(1), int32(1), true }, |
| 358 | DeepEqualTest{ 0.5, 0.5, true }, |
| 359 | DeepEqualTest{ float32(0.5), float32(0.5), true }, |
| 360 | DeepEqualTest{ "hello", "hello", true }, |
| 361 | DeepEqualTest{ make([]int, 10), make([]int, 10), true }, |
| 362 | DeepEqualTest{ &[3]int{ 1, 2, 3 }, &[3]int{ 1, 2, 3 }, true }, |
| 363 | DeepEqualTest{ Basic{ 1, 0.5 }, Basic{ 1, 0.5 }, true }, |
Russ Cox | 4866223 | 2009-06-25 14:25:38 -0700 | [diff] [blame] | 364 | DeepEqualTest{ os.Error(nil), os.Error(nil), true }, |
Rob Pike | 1880b90 | 2009-07-10 11:20:10 -0700 | [diff] [blame] | 365 | 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] | 366 | |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 367 | // Inequalities |
| 368 | DeepEqualTest{ 1, 2, false }, |
| 369 | DeepEqualTest{ int32(1), int32(2), false }, |
| 370 | DeepEqualTest{ 0.5, 0.6, false }, |
| 371 | DeepEqualTest{ float32(0.5), float32(0.6), false }, |
| 372 | DeepEqualTest{ "hello", "hey", false }, |
| 373 | DeepEqualTest{ make([]int, 10), make([]int, 11), false }, |
| 374 | DeepEqualTest{ &[3]int{ 1, 2, 3 }, &[3]int{ 1, 2, 4 }, false }, |
| 375 | DeepEqualTest{ Basic{ 1, 0.5 }, Basic{ 1, 0.6 }, false }, |
Russ Cox | a439f66 | 2009-07-01 16:45:09 -0700 | [diff] [blame] | 376 | DeepEqualTest{ Basic{ 1, 0 }, Basic{ 2, 0 }, false }, |
Rob Pike | 1880b90 | 2009-07-10 11:20:10 -0700 | [diff] [blame] | 377 | DeepEqualTest{ map[int]string{ 1:"one", 3:"two" }, map[int]string{ 2:"two", 1:"one" }, false }, |
| 378 | DeepEqualTest{ map[int]string{ 1:"one", 2:"txo" }, map[int]string{ 2:"two", 1:"one" }, false }, |
| 379 | DeepEqualTest{ map[int]string{ 1:"one", }, map[int]string{ 2:"two", 1:"one" }, false }, |
| 380 | 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] | 381 | |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 382 | // Mismatched types |
| 383 | DeepEqualTest{ 1, 1.0, false }, |
| 384 | DeepEqualTest{ int32(1), int64(1), false }, |
| 385 | DeepEqualTest{ 0.5, "hello", false }, |
| 386 | DeepEqualTest{ []int{ 1, 2, 3 }, [3]int{ 1, 2, 3 }, false }, |
| 387 | DeepEqualTest{ &[3]interface{} { 1, 2, 4 }, &[3]interface{} { 1, 2, "s" }, false }, |
Russ Cox | 64627b0 | 2009-04-15 00:55:58 -0700 | [diff] [blame] | 388 | DeepEqualTest{ Basic{ 1, 0.5 }, NotBasic{ 1, 0.5 }, false }, |
Rob Pike | 1880b90 | 2009-07-10 11:20:10 -0700 | [diff] [blame] | 389 | 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] | 390 | } |
| 391 | |
| 392 | func TestDeepEqual(t *testing.T) { |
| 393 | for i, test := range deepEqualTests { |
| 394 | if r := DeepEqual(test.a, test.b); r != test.eq { |
| 395 | t.Errorf("DeepEqual(%v, %v) = %v, want %v", test.a, test.b, r, test.eq); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
Russ Cox | 4866223 | 2009-06-25 14:25:38 -0700 | [diff] [blame] | 400 | func TestTypeof(t *testing.T) { |
| 401 | for i, test := range deepEqualTests { |
| 402 | v := NewValue(test.a); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 403 | if v == nil { |
| 404 | continue; |
| 405 | } |
Russ Cox | 4866223 | 2009-06-25 14:25:38 -0700 | [diff] [blame] | 406 | typ := Typeof(test.a); |
| 407 | if typ != v.Type() { |
| 408 | t.Errorf("Typeof(%v) = %v, but NewValue(%v).Type() = %v", test.a, typ, test.a, v.Type()); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
Rob Pike | 1880b90 | 2009-07-10 11:20:10 -0700 | [diff] [blame] | 413 | type Recursive struct { |
| 414 | x int; |
| 415 | r *Recursive |
| 416 | } |
| 417 | |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 418 | func TestDeepEqualRecursiveStruct(t *testing.T) { |
| 419 | a, b := new(Recursive), new(Recursive); |
| 420 | *a = Recursive{ 12, a }; |
| 421 | *b = Recursive{ 12, b }; |
| 422 | if !DeepEqual(a, b) { |
| 423 | t.Error("DeepEqual(recursive same) = false, want true"); |
| 424 | } |
| 425 | } |
| 426 | |
Rob Pike | 1880b90 | 2009-07-10 11:20:10 -0700 | [diff] [blame] | 427 | type Complex struct { |
| 428 | a int; |
| 429 | b [3]*Complex; |
| 430 | c *string; |
| 431 | d map[float]float |
| 432 | } |
| 433 | |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 434 | func TestDeepEqualComplexStruct(t *testing.T) { |
| 435 | m := make(map[float]float); |
| 436 | stra, strb := "hello", "hello"; |
| 437 | a, b := new(Complex), new(Complex); |
| 438 | *a = Complex{5, [3]*Complex{a, b, a}, &stra, m}; |
| 439 | *b = Complex{5, [3]*Complex{b, a, a}, &strb, m}; |
| 440 | if !DeepEqual(a, b) { |
| 441 | t.Error("DeepEqual(complex same) = false, want true"); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | func TestDeepEqualComplexStructInequality(t *testing.T) { |
| 446 | m := make(map[float]float); |
| 447 | stra, strb := "hello", "helloo"; // Difference is here |
| 448 | a, b := new(Complex), new(Complex); |
| 449 | *a = Complex{5, [3]*Complex{a, b, a}, &stra, m}; |
| 450 | *b = Complex{5, [3]*Complex{b, a, a}, &strb, m}; |
| 451 | if DeepEqual(a, b) { |
| 452 | t.Error("DeepEqual(complex different) = true, want false"); |
| 453 | } |
| 454 | } |
Rob Pike | 93831d2 | 2009-04-29 22:16:53 -0700 | [diff] [blame] | 455 | |
| 456 | |
| 457 | func check2ndField(x interface{}, offs uintptr, t *testing.T) { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 458 | s := NewValue(x).(*StructValue); |
| 459 | f := s.Type().(*StructType).Field(1); |
| 460 | if f.Offset != offs { |
| 461 | t.Error("mismatched offsets in structure alignment:", f.Offset, offs); |
Rob Pike | 93831d2 | 2009-04-29 22:16:53 -0700 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
| 465 | // Check that structure alignment & offsets viewed through reflect agree with those |
| 466 | // from the compiler itself. |
| 467 | func TestAlignment(t *testing.T) { |
| 468 | type T1inner struct { |
| 469 | a int |
| 470 | } |
| 471 | type T1 struct { |
| 472 | T1inner; |
| 473 | f int; |
| 474 | } |
| 475 | type T2inner struct { |
| 476 | a, b int |
| 477 | } |
| 478 | type T2 struct { |
| 479 | T2inner; |
| 480 | f int; |
| 481 | } |
| 482 | |
| 483 | x := T1{T1inner{2}, 17}; |
| 484 | check2ndField(x, uintptr(unsafe.Pointer(&x.f)) - uintptr(unsafe.Pointer(&x)), t); |
| 485 | |
| 486 | x1 := T2{T2inner{2, 3}, 17}; |
| 487 | check2ndField(x1, uintptr(unsafe.Pointer(&x1.f)) - uintptr(unsafe.Pointer(&x1)), t); |
| 488 | } |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 489 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 490 | type IsNiller interface { |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 491 | IsNil() bool |
| 492 | } |
| 493 | |
| 494 | func Nil(a interface{}, t *testing.T) { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 495 | n := NewValue(a).(*StructValue).Field(0).(IsNiller); |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 496 | if !n.IsNil() { |
| 497 | t.Errorf("%v should be nil", a) |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | func NotNil(a interface{}, t *testing.T) { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 502 | n := NewValue(a).(*StructValue).Field(0).(IsNiller); |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 503 | if n.IsNil() { |
| 504 | t.Errorf("value of type %v should not be nil", NewValue(a).Type().String()) |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | func TestIsNil(t *testing.T) { |
| 509 | // These do not implement IsNil |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 510 | doNotNil := []interface{}{ int(0), float32(0), struct{a int}{} }; |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 511 | for i, ts := range doNotNil { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 512 | ty := Typeof(ts); |
| 513 | v := MakeZero(ty); |
| 514 | if nilable, ok := v.(IsNiller); ok { |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 515 | t.Errorf("%s is nilable; should not be", ts) |
| 516 | } |
| 517 | } |
| 518 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 519 | // These do implement IsNil. |
| 520 | // Wrap in extra struct to hide interface type. |
| 521 | doNil := []interface{}{ |
| 522 | struct{x *int}{}, |
| 523 | struct{x interface{}}{}, |
| 524 | struct{x map[string]int}{}, |
| 525 | struct{x func()bool}{}, |
| 526 | struct{x chan int}{}, |
| 527 | struct{x []string}{} |
| 528 | }; |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 529 | for i, ts := range doNil { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 530 | ty := Typeof(ts).(*StructType).Field(0).Type; |
| 531 | v := MakeZero(ty); |
| 532 | if nilable, ok := v.(IsNiller); !ok { |
Russ Cox | 2d5d4a1 | 2009-05-20 15:42:14 -0700 | [diff] [blame] | 533 | t.Errorf("%s %T is not nilable; should be", ts, v) |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 534 | } |
| 535 | } |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 536 | |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 537 | // Check the implementations |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 538 | var pi struct {x *int} |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 539 | Nil(pi, t); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 540 | pi.x = new(int); |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 541 | NotNil(pi, t); |
| 542 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 543 | var si struct {x []int} |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 544 | Nil(si, t); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 545 | si.x = make([]int, 10); |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 546 | NotNil(si, t); |
| 547 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 548 | var ci struct {x chan int} |
| 549 | Nil(ci, t); |
| 550 | ci.x = make(chan int); |
| 551 | NotNil(ci, t); |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 552 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 553 | var mi struct {x map[int]int} |
| 554 | Nil(mi, t); |
| 555 | mi.x = make(map[int]int); |
| 556 | NotNil(mi, t); |
| 557 | |
| 558 | var ii struct {x interface {}} |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 559 | Nil(ii, t); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 560 | ii.x = 2; |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 561 | NotNil(ii, t); |
| 562 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 563 | var fi struct {x func(t *testing.T)} |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 564 | Nil(fi, t); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 565 | fi.x = TestIsNil; |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 566 | NotNil(fi, t); |
| 567 | } |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 568 | |
| 569 | func TestInterfaceExtraction(t *testing.T) { |
| 570 | var s struct { |
| 571 | w io.Writer; |
| 572 | } |
| 573 | |
| 574 | s.w = os.Stdout; |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 575 | v := Indirect(NewValue(&s)).(*StructValue).Field(0).Interface(); |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 576 | if v != s.w.(interface{}) { |
Rob Pike | b75df2f | 2009-07-16 15:01:21 -0700 | [diff] [blame] | 577 | t.Error("Interface() on interface: ", v, s.w); |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 578 | } |
| 579 | } |
Russ Cox | f966623 | 2009-05-21 14:06:43 -0700 | [diff] [blame] | 580 | |
| 581 | func TestInterfaceEditing(t *testing.T) { |
| 582 | // strings are bigger than one word, |
| 583 | // so the interface conversion allocates |
| 584 | // memory to hold a string and puts that |
| 585 | // pointer in the interface. |
| 586 | var i interface{} = "hello"; |
| 587 | |
| 588 | // if i pass the interface value by value |
| 589 | // to NewValue, i should get a fresh copy |
| 590 | // of the value. |
| 591 | v := NewValue(i); |
| 592 | |
| 593 | // and setting that copy to "bye" should |
| 594 | // not change the value stored in i. |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 595 | v.(*StringValue).Set("bye"); |
Russ Cox | f966623 | 2009-05-21 14:06:43 -0700 | [diff] [blame] | 596 | if i.(string) != "hello" { |
| 597 | t.Errorf(`Set("bye") changed i to %s`, i.(string)); |
| 598 | } |
| 599 | |
| 600 | // the same should be true of smaller items. |
| 601 | i = 123; |
| 602 | v = NewValue(i); |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 603 | v.(*IntValue).Set(234); |
Russ Cox | f966623 | 2009-05-21 14:06:43 -0700 | [diff] [blame] | 604 | if i.(int) != 123 { |
| 605 | t.Errorf("Set(234) changed i to %d", i.(int)); |
| 606 | } |
| 607 | } |
David Symonds | d4e57ff | 2009-06-15 18:35:04 -0700 | [diff] [blame] | 608 | |
| 609 | func TestNilPtrValueSub(t *testing.T) { |
| 610 | var pi *int; |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 611 | if pv := NewValue(pi).(*PtrValue); pv.Elem() != nil { |
| 612 | t.Error("NewValue((*int)(nil)).(*PtrValue).Elem() != nil"); |
David Symonds | d4e57ff | 2009-06-15 18:35:04 -0700 | [diff] [blame] | 613 | } |
| 614 | } |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 615 | |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 616 | func TestMap(t *testing.T) { |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 617 | m := map[string]int{ "a": 1, "b": 2 }; |
| 618 | mv := NewValue(m).(*MapValue); |
| 619 | if n := mv.Len(); n != len(m) { |
| 620 | t.Errorf("Len = %d, want %d", n, len(m)); |
| 621 | } |
| 622 | keys := mv.Keys(); |
| 623 | i := 0; |
| 624 | newmap := MakeMap(mv.Type().(*MapType)); |
| 625 | for k, v := range m { |
| 626 | // Check that returned Keys match keys in range. |
| 627 | // These aren't required to be in the same order, |
| 628 | // but they are in this implementation, which makes |
| 629 | // the test easier. |
| 630 | if i >= len(keys) { |
| 631 | t.Errorf("Missing key #%d %q", i, k); |
| 632 | } else if kv := keys[i].(*StringValue); kv.Get() != k { |
| 633 | t.Errorf("Keys[%d] = %q, want %q", i, kv.Get(), k); |
| 634 | } |
| 635 | i++; |
| 636 | |
| 637 | // Check that value lookup is correct. |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 638 | vv := mv.Elem(NewValue(k)); |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 639 | if vi := vv.(*IntValue).Get(); vi != v { |
| 640 | t.Errorf("Key %q: have value %d, want %d", vi, v); |
| 641 | } |
| 642 | |
| 643 | // Copy into new map. |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 644 | newmap.SetElem(NewValue(k), NewValue(v)); |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 645 | } |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 646 | vv := mv.Elem(NewValue("not-present")); |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 647 | if vv != nil { |
| 648 | t.Errorf("Invalid key: got non-nil value %s", valueToString(vv)); |
| 649 | } |
| 650 | |
| 651 | newm := newmap.Interface().(map[string]int); |
| 652 | if len(newm) != len(m) { |
| 653 | t.Errorf("length after copy: newm=%d, m=%d", newm, m); |
| 654 | } |
| 655 | |
| 656 | for k, v := range newm { |
| 657 | mv, ok := m[k]; |
| 658 | if mv != v { |
| 659 | t.Errorf("newm[%q] = %d, but m[%q] = %d, %v", k, v, k, mv, ok); |
| 660 | } |
| 661 | } |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 662 | |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 663 | newmap.SetElem(NewValue("a"), nil); |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 664 | v, ok := newm["a"]; |
| 665 | if ok { |
| 666 | t.Errorf("newm[\"a\"] = %d after delete", v); |
| 667 | } |
| 668 | } |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 669 | |
| 670 | func TestChan(t *testing.T) { |
| 671 | for loop := 0; loop < 2; loop++ { |
| 672 | var c chan int; |
| 673 | var cv *ChanValue; |
| 674 | |
| 675 | // check both ways to allocate channels |
| 676 | switch loop { |
| 677 | case 1: |
| 678 | c = make(chan int, 1); |
| 679 | cv = NewValue(c).(*ChanValue); |
| 680 | case 0: |
| 681 | cv = MakeChan(Typeof(c).(*ChanType), 1); |
| 682 | c = cv.Interface().(chan int); |
| 683 | } |
| 684 | |
| 685 | // Send |
| 686 | cv.Send(NewValue(2)); |
| 687 | if i := <-c; i != 2 { |
| 688 | t.Errorf("reflect Send 2, native recv %d", i); |
| 689 | } |
| 690 | |
| 691 | // Recv |
| 692 | c <- 3; |
| 693 | if i := cv.Recv().(*IntValue).Get(); i != 3 { |
| 694 | t.Errorf("native send 3, reflect Recv %d", i); |
| 695 | } |
| 696 | |
| 697 | // TryRecv fail |
| 698 | val := cv.TryRecv(); |
| 699 | if val != nil { |
| 700 | t.Errorf("TryRecv on empty chan: %s", valueToString(val)); |
| 701 | } |
| 702 | |
| 703 | // TryRecv success |
| 704 | c <- 4; |
| 705 | val = cv.TryRecv(); |
| 706 | if val == nil { |
| 707 | t.Errorf("TryRecv on ready chan got nil"); |
| 708 | } else if i := val.(*IntValue).Get(); i != 4 { |
| 709 | t.Errorf("native send 4, TryRecv %d", i); |
| 710 | } |
| 711 | |
| 712 | // TrySend fail |
| 713 | c <- 100; |
| 714 | ok := cv.TrySend(NewValue(5)); |
| 715 | i := <-c; |
| 716 | if ok { |
| 717 | t.Errorf("TrySend on full chan succeeded: value %d", i); |
| 718 | } |
| 719 | |
| 720 | // TrySend success |
| 721 | ok = cv.TrySend(NewValue(6)); |
| 722 | if !ok { |
| 723 | t.Errorf("TrySend on empty chan failed"); |
| 724 | } else { |
| 725 | if i = <-c; i != 6 { |
| 726 | t.Errorf("TrySend 6, recv %d", i); |
| 727 | } |
| 728 | } |
Russ Cox | 653cef1 | 2009-08-26 10:47:18 -0700 | [diff] [blame^] | 729 | |
| 730 | // Close |
| 731 | c <- 123; |
| 732 | cv.Close(); |
| 733 | if cv.Closed() { |
| 734 | t.Errorf("closed too soon - 1"); |
| 735 | } |
| 736 | if i := cv.Recv().(*IntValue).Get(); i != 123 { |
| 737 | t.Errorf("send 123 then close; Recv %d", i); |
| 738 | } |
| 739 | if cv.Closed() { |
| 740 | t.Errorf("closed too soon - 2"); |
| 741 | } |
| 742 | if i := cv.Recv().(*IntValue).Get(); i != 0 { |
| 743 | t.Errorf("after close Recv %d", i); |
| 744 | } |
| 745 | if !cv.Closed() { |
| 746 | t.Errorf("not closed"); |
| 747 | } |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | // check creation of unbuffered channel |
| 751 | var c chan int; |
| 752 | cv := MakeChan(Typeof(c).(*ChanType), 0); |
| 753 | c = cv.Interface().(chan int); |
| 754 | if cv.TrySend(NewValue(7)) { |
| 755 | t.Errorf("TrySend on sync chan succeeded"); |
| 756 | } |
| 757 | if cv.TryRecv() != nil { |
| 758 | t.Errorf("TryRecv on sync chan succeeded"); |
| 759 | } |
| 760 | } |
| 761 | |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 762 | // Difficult test for function call because of |
| 763 | // implicit padding between arguments. |
| 764 | func dummy(b byte, c int, d byte) (i byte, j int, k byte){ |
| 765 | return b, c, d; |
| 766 | } |
| 767 | |
| 768 | func TestFunc(t *testing.T) { |
| 769 | ret := NewValue(dummy).(*FuncValue).Call([]Value{NewValue(byte(10)), NewValue(20), NewValue(byte(30))}); |
| 770 | if len(ret) != 3 { |
| 771 | t.Fatalf("Call returned %d values, want 3", len(ret)); |
| 772 | } |
| 773 | |
| 774 | i := ret[0].(*Uint8Value).Get(); |
| 775 | j := ret[1].(*IntValue).Get(); |
| 776 | k := ret[2].(*Uint8Value).Get(); |
| 777 | if i != 10 || j != 20 || k != 30 { |
| 778 | t.Errorf("Call returned %d, %d, %d; want 10, 20, 30", i, j, k); |
| 779 | } |
| 780 | } |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 781 | |
| 782 | type Point struct { |
| 783 | x, y int; |
| 784 | } |
| 785 | |
| 786 | func (p Point) Dist(scale int) int { |
| 787 | return p.x*p.x*scale + p.y*p.y*scale; |
| 788 | } |
| 789 | |
| 790 | func TestMethod(t *testing.T) { |
| 791 | // Non-curried method of type. |
| 792 | p := Point{3, 4}; |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 793 | i := Typeof(p).Method(0).Func.Call([]Value{NewValue(p), NewValue(10)})[0].(*IntValue).Get(); |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 794 | if i != 250 { |
| 795 | t.Errorf("Type Method returned %d; want 250", i); |
| 796 | } |
| 797 | |
| 798 | // Curried method of value. |
| 799 | i = NewValue(p).Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get(); |
| 800 | if i != 250 { |
| 801 | t.Errorf("Value Method returned %d; want 250", i); |
| 802 | } |
| 803 | |
| 804 | // Curried method of interface value. |
| 805 | // Have to wrap interface value in a struct to get at it. |
| 806 | // Passing it to NewValue directly would |
| 807 | // access the underlying Point, not the interface. |
| 808 | var s = struct{x interface{Dist(int) int}}{p}; |
| 809 | pv := NewValue(s).(*StructValue).Field(0); |
| 810 | i = pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get(); |
| 811 | if i != 250 { |
| 812 | t.Errorf("Interface Method returned %d; want 250", i); |
| 813 | } |
| 814 | } |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 815 | |
| 816 | func TestInterfaceSet(t *testing.T) { |
| 817 | p := &Point{3, 4}; |
| 818 | |
| 819 | var s struct { |
| 820 | I interface {}; |
| 821 | P interface { Dist(int)int }; |
| 822 | } |
| 823 | sv := NewValue(&s).(*PtrValue).Elem().(*StructValue); |
| 824 | sv.Field(0).(*InterfaceValue).Set(NewValue(p)); |
| 825 | if q := s.I.(*Point); q != p { |
| 826 | t.Errorf("i: have %p want %p", q, p); |
| 827 | } |
| 828 | |
| 829 | pv := sv.Field(1).(*InterfaceValue); |
| 830 | pv.Set(NewValue(p)); |
| 831 | if q := s.P.(*Point); q != p { |
| 832 | t.Errorf("i: have %p want %p", q, p); |
| 833 | } |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 834 | |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 835 | i := pv.Method(0).Call([]Value{NewValue(10)})[0].(*IntValue).Get(); |
| 836 | if i != 250 { |
| 837 | t.Errorf("Interface Method returned %d; want 250", i); |
| 838 | } |
| 839 | } |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 840 | |
| 841 | type T1 struct { a string; int; } |
| 842 | |
| 843 | func TestAnonymousFields(t *testing.T) { |
| 844 | var field StructField; |
| 845 | var ok bool; |
| 846 | var t1 T1; |
| 847 | type1 := Typeof(t1).(*StructType); |
| 848 | if field, ok = type1.FieldByName("int"); !ok { |
| 849 | t.Error("no field 'int'"); |
| 850 | } |
| 851 | if field.Index[0] != 1 { |
| 852 | t.Error("field index should be 1; is", field.Index); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | type FTest struct { |
| 857 | s interface{}; |
| 858 | name string; |
| 859 | index []int; |
| 860 | value int; |
| 861 | } |
| 862 | |
Russ Cox | 92543da | 2009-08-24 17:04:12 -0700 | [diff] [blame] | 863 | type D1 struct { |
| 864 | d int; |
| 865 | } |
| 866 | type D2 struct { |
| 867 | d int; |
| 868 | } |
| 869 | |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 870 | type S0 struct { |
Russ Cox | 92543da | 2009-08-24 17:04:12 -0700 | [diff] [blame] | 871 | a, b, c int; |
| 872 | D1; |
| 873 | D2; |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | type S1 struct { |
| 877 | b int; |
| 878 | S0; |
| 879 | } |
| 880 | |
| 881 | type S2 struct { |
| 882 | a int; |
| 883 | *S1; |
| 884 | } |
| 885 | |
Russ Cox | 92543da | 2009-08-24 17:04:12 -0700 | [diff] [blame] | 886 | type S1x struct { |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 887 | S1; |
Russ Cox | 92543da | 2009-08-24 17:04:12 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | type S1y struct { |
| 891 | S1; |
| 892 | } |
| 893 | |
| 894 | type S3 struct { |
| 895 | S1x; |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 896 | S2; |
| 897 | d, e int; |
Russ Cox | 92543da | 2009-08-24 17:04:12 -0700 | [diff] [blame] | 898 | *S1y; |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | type S4 struct { |
| 902 | *S4; |
| 903 | a int; |
| 904 | } |
| 905 | |
| 906 | var fieldTests = []FTest { |
| 907 | FTest{ struct{ }{}, "", nil, 0 }, |
| 908 | FTest{ struct{ }{}, "foo", nil, 0 }, |
| 909 | FTest{ S0{a: 'a'}, "a", []int{0}, 'a' }, |
| 910 | FTest{ S0{}, "d", nil, 0 }, |
| 911 | FTest{ S1{S0: S0{a: 'a'}}, "a", []int{1, 0}, 'a' }, |
| 912 | FTest{ S1{b: 'b'}, "b", []int{0}, 'b' }, |
| 913 | FTest{ S1{}, "S0", []int{1}, 0 }, |
| 914 | FTest{ S1{S0: S0{c: 'c'}}, "c", []int{1, 2}, 'c' }, |
| 915 | FTest{ S2{a: 'a'}, "a", []int{0}, 'a' }, |
| 916 | FTest{ S2{}, "S1", []int{1}, 0 }, |
| 917 | FTest{ S2{S1: &S1{b: 'b'}}, "b", []int{1, 0}, 'b' }, |
| 918 | FTest{ S2{S1: &S1{S0: S0{c: 'c'}}}, "c", []int{1, 1, 2}, 'c' }, |
| 919 | FTest{ S2{}, "d", nil, 0 }, |
| 920 | FTest{ S3{}, "S1", nil, 0 }, |
| 921 | FTest{ S3{S2: S2{a: 'a'}}, "a", []int{1, 0}, 'a' }, |
| 922 | FTest{ S3{}, "b", nil, 0 }, |
| 923 | FTest{ S3{d: 'd'}, "d", []int{2}, 0 }, |
| 924 | FTest{ S3{e: 'e'}, "e", []int{3}, 'e' }, |
| 925 | FTest{ S4{a: 'a'}, "a", []int{1}, 'a' }, |
| 926 | FTest{ S4{}, "b", nil, 0 }, |
| 927 | } |
| 928 | |
| 929 | func TestFieldByIndex(t *testing.T) { |
| 930 | for _, test := range fieldTests { |
| 931 | s := Typeof(test.s).(*StructType); |
| 932 | f := s.FieldByIndex(test.index); |
| 933 | if f.Name != "" { |
| 934 | if test.index != nil { |
| 935 | if f.Name != test.name { |
| 936 | t.Errorf("%s.%s found; want %s", s.Name(), f.Name, test.name); |
| 937 | } |
| 938 | } else { |
| 939 | t.Errorf("%s.%s found", s.Name(), f.Name); |
| 940 | } |
| 941 | } else if len(test.index) > 0 { |
| 942 | t.Errorf("%s.%s not found", s.Name(), test.name); |
| 943 | } |
| 944 | |
| 945 | if test.value != 0 { |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 946 | v := NewValue(test.s).(*StructValue).FieldByIndex(test.index); |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 947 | if v != nil { |
| 948 | if x, ok := v.Interface().(int); ok { |
| 949 | if x != test.value { |
| 950 | t.Errorf("%s%v is %d; want %d", s.Name(), test.index, x, test.value); |
| 951 | } |
| 952 | } else { |
| 953 | t.Errorf("%s%v value not an int", s.Name(), test.index); |
| 954 | } |
| 955 | } else { |
| 956 | t.Errorf("%s%v value not found", s.Name(), test.index); |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | func TestFieldByName(t *testing.T) { |
| 963 | for _, test := range fieldTests { |
| 964 | s := Typeof(test.s).(*StructType); |
| 965 | f, found := s.FieldByName(test.name); |
| 966 | if found { |
| 967 | if test.index != nil { |
| 968 | // Verify field depth and index. |
| 969 | if len(f.Index) != len(test.index) { |
| 970 | t.Errorf("%s.%s depth %d; want %d", s.Name(), test.name, len(f.Index), len(test.index)); |
| 971 | } else { |
| 972 | for i, x := range f.Index { |
| 973 | if x != test.index[i] { |
| 974 | t.Errorf("%s.%s.Index[%d] is %d; want %d", s.Name(), test.name, i, x, test.index[i]); |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | } else { |
| 979 | t.Errorf("%s.%s found", s.Name(), f.Name); |
| 980 | } |
| 981 | } else if len(test.index) > 0 { |
| 982 | t.Errorf("%s.%s not found", s.Name(), test.name); |
| 983 | } |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 984 | |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 985 | if test.value != 0 { |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 986 | v := NewValue(test.s).(*StructValue).FieldByName(test.name); |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 987 | if v != nil { |
| 988 | if x, ok := v.Interface().(int); ok { |
| 989 | if x != test.value { |
| 990 | t.Errorf("%s.%s is %d; want %d", s.Name(), test.name, x, test.value); |
| 991 | } |
| 992 | } else { |
| 993 | t.Errorf("%s.%s value not an int", s.Name(), test.name); |
| 994 | } |
| 995 | } else { |
| 996 | t.Errorf("%s.%s value not found", s.Name(), test.name); |
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | } |