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 | e1ee3b5 | 2011-04-20 16:24:45 -0400 | [diff] [blame] | 8 | "bytes" |
Rob Pike | ab44a81 | 2011-08-22 13:22:42 +1000 | [diff] [blame] | 9 | "encoding/base64" |
Russ Cox | 46f379c | 2012-09-22 08:52:27 -0400 | [diff] [blame] | 10 | "flag" |
Russ Cox | 6672b40 | 2010-06-14 11:23:11 -0700 | [diff] [blame] | 11 | "fmt" |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 12 | "io" |
Russ Cox | 370ae05 | 2012-09-18 14:22:41 -0400 | [diff] [blame] | 13 | "math/rand" |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 14 | "os" |
| 15 | . "reflect" |
Albert Strasheim | 0a71a5b | 2013-03-06 15:52:32 -0800 | [diff] [blame] | 16 | "runtime" |
Russ Cox | 3660b53 | 2013-03-26 11:50:29 -0700 | [diff] [blame] | 17 | "sort" |
Russ Cox | 5984732 | 2014-02-21 13:51:22 -0500 | [diff] [blame] | 18 | "strings" |
Russ Cox | 370ae05 | 2012-09-18 14:22:41 -0400 | [diff] [blame] | 19 | "sync" |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 20 | "testing" |
Russ Cox | 370ae05 | 2012-09-18 14:22:41 -0400 | [diff] [blame] | 21 | "time" |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 22 | "unsafe" |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
Russ Cox | a479a45 | 2011-11-16 19:18:25 -0500 | [diff] [blame] | 25 | func TestBool(t *testing.T) { |
| 26 | v := ValueOf(true) |
| 27 | if v.Bool() != true { |
| 28 | t.Fatal("ValueOf(true).Bool() = false") |
| 29 | } |
| 30 | } |
| 31 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 32 | type integer int |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 33 | type T struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 34 | a int |
| 35 | b float64 |
| 36 | c string |
| 37 | d *int |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 38 | } |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 39 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 40 | type pair struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 41 | i interface{} |
| 42 | s string |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 45 | func isDigit(c uint8) bool { return '0' <= c && c <= '9' } |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 46 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 47 | func assert(t *testing.T, s, want string) { |
| 48 | if s != want { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 49 | t.Errorf("have %#q want %#q", s, want) |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 53 | func typestring(i interface{}) string { return TypeOf(i).String() } |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 54 | |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 55 | var typeTests = []pair{ |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 56 | {struct{ x int }{}, "int"}, |
| 57 | {struct{ x int8 }{}, "int8"}, |
| 58 | {struct{ x int16 }{}, "int16"}, |
| 59 | {struct{ x int32 }{}, "int32"}, |
| 60 | {struct{ x int64 }{}, "int64"}, |
| 61 | {struct{ x uint }{}, "uint"}, |
| 62 | {struct{ x uint8 }{}, "uint8"}, |
| 63 | {struct{ x uint16 }{}, "uint16"}, |
| 64 | {struct{ x uint32 }{}, "uint32"}, |
| 65 | {struct{ x uint64 }{}, "uint64"}, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 66 | {struct{ x float32 }{}, "float32"}, |
| 67 | {struct{ x float64 }{}, "float64"}, |
| 68 | {struct{ x int8 }{}, "int8"}, |
| 69 | {struct{ x (**int8) }{}, "**int8"}, |
| 70 | {struct{ x (**integer) }{}, "**reflect_test.integer"}, |
| 71 | {struct{ x ([32]int32) }{}, "[32]int32"}, |
| 72 | {struct{ x ([]int8) }{}, "[]int8"}, |
Russ Cox | 434a6c8 | 2011-12-02 14:45:07 -0500 | [diff] [blame] | 73 | {struct{ x (map[string]int32) }{}, "map[string]int32"}, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 74 | {struct{ x (chan<- string) }{}, "chan<- string"}, |
| 75 | {struct { |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 76 | x struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 77 | c chan *int32 |
| 78 | d float32 |
| 79 | } |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 80 | }{}, |
| 81 | "struct { c chan *int32; d float32 }", |
| 82 | }, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 83 | {struct{ x (func(a int8, b int32)) }{}, "func(int8, int32)"}, |
| 84 | {struct { |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 85 | x struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 86 | c func(chan *integer, *int8) |
| 87 | } |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 88 | }{}, |
| 89 | "struct { c func(chan *reflect_test.integer, *int8) }", |
| 90 | }, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 91 | {struct { |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 92 | x struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 93 | a int8 |
| 94 | b int32 |
| 95 | } |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 96 | }{}, |
| 97 | "struct { a int8; b int32 }", |
| 98 | }, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 99 | {struct { |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 100 | x struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 101 | a int8 |
| 102 | b int8 |
| 103 | c int32 |
| 104 | } |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 105 | }{}, |
| 106 | "struct { a int8; b int8; c int32 }", |
| 107 | }, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 108 | {struct { |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 109 | x struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 110 | a int8 |
| 111 | b int8 |
| 112 | c int8 |
| 113 | d int32 |
| 114 | } |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 115 | }{}, |
| 116 | "struct { a int8; b int8; c int8; d int32 }", |
| 117 | }, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 118 | {struct { |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 119 | x struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 120 | a int8 |
| 121 | b int8 |
| 122 | c int8 |
| 123 | d int8 |
| 124 | e int32 |
| 125 | } |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 126 | }{}, |
| 127 | "struct { a int8; b int8; c int8; d int8; e int32 }", |
| 128 | }, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 129 | {struct { |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 130 | x struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 131 | a int8 |
| 132 | b int8 |
| 133 | c int8 |
| 134 | d int8 |
| 135 | e int8 |
| 136 | f int32 |
| 137 | } |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 138 | }{}, |
| 139 | "struct { a int8; b int8; c int8; d int8; e int8; f int32 }", |
| 140 | }, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 141 | {struct { |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 142 | x struct { |
Russ Cox | 25733a9 | 2011-06-29 09:52:34 -0400 | [diff] [blame] | 143 | a int8 `reflect:"hi there"` |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 144 | } |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 145 | }{}, |
Russ Cox | 25733a9 | 2011-06-29 09:52:34 -0400 | [diff] [blame] | 146 | `struct { a int8 "reflect:\"hi there\"" }`, |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 147 | }, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 148 | {struct { |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 149 | x struct { |
Russ Cox | 25733a9 | 2011-06-29 09:52:34 -0400 | [diff] [blame] | 150 | a int8 `reflect:"hi \x00there\t\n\"\\"` |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 151 | } |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 152 | }{}, |
Russ Cox | 25733a9 | 2011-06-29 09:52:34 -0400 | [diff] [blame] | 153 | `struct { a int8 "reflect:\"hi \\x00there\\t\\n\\\"\\\\\"" }`, |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 154 | }, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 155 | {struct { |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 156 | x struct { |
Russ Cox | 6672b40 | 2010-06-14 11:23:11 -0700 | [diff] [blame] | 157 | f func(args ...int) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 158 | } |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 159 | }{}, |
Russ Cox | 6672b40 | 2010-06-14 11:23:11 -0700 | [diff] [blame] | 160 | "struct { f func(...int) }", |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 161 | }, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 162 | {struct { |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 163 | x (interface { |
Robert Griesemer | 1be05bb | 2010-02-24 13:24:37 -0800 | [diff] [blame] | 164 | a(func(func(int) int) func(func(int)) int) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 165 | b() |
| 166 | }) |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 167 | }{}, |
Russ Cox | 5ff3336 | 2011-04-21 08:14:50 -0400 | [diff] [blame] | 168 | "interface { reflect_test.a(func(func(int) int) func(func(int)) int); reflect_test.b() }", |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 169 | }, |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 172 | var valueTests = []pair{ |
Todd Wang | 0e73497 | 2013-08-21 14:41:55 +1000 | [diff] [blame] | 173 | {new(int), "132"}, |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 174 | {new(int8), "8"}, |
| 175 | {new(int16), "16"}, |
| 176 | {new(int32), "32"}, |
| 177 | {new(int64), "64"}, |
Todd Wang | 0e73497 | 2013-08-21 14:41:55 +1000 | [diff] [blame] | 178 | {new(uint), "132"}, |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 179 | {new(uint8), "8"}, |
| 180 | {new(uint16), "16"}, |
| 181 | {new(uint32), "32"}, |
| 182 | {new(uint64), "64"}, |
| 183 | {new(float32), "256.25"}, |
| 184 | {new(float64), "512.125"}, |
Todd Wang | 0e73497 | 2013-08-21 14:41:55 +1000 | [diff] [blame] | 185 | {new(complex64), "532.125+10i"}, |
| 186 | {new(complex128), "564.25+1i"}, |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 187 | {new(string), "stringy cheese"}, |
| 188 | {new(bool), "true"}, |
| 189 | {new(*int8), "*int8(0)"}, |
| 190 | {new(**int8), "**int8(0)"}, |
| 191 | {new([5]int32), "[5]int32{0, 0, 0, 0, 0}"}, |
| 192 | {new(**integer), "**reflect_test.integer(0)"}, |
Russ Cox | 434a6c8 | 2011-12-02 14:45:07 -0500 | [diff] [blame] | 193 | {new(map[string]int32), "map[string]int32{<can't iterate on maps>}"}, |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 194 | {new(chan<- string), "chan<- string"}, |
| 195 | {new(func(a int8, b int32)), "func(int8, int32)(0)"}, |
| 196 | {new(struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 197 | c chan *int32 |
| 198 | d float32 |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 199 | }), |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 200 | "struct { c chan *int32; d float32 }{chan *int32, 0}", |
| 201 | }, |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 202 | {new(struct{ c func(chan *integer, *int8) }), |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 203 | "struct { c func(chan *reflect_test.integer, *int8) }{func(chan *reflect_test.integer, *int8)(0)}", |
| 204 | }, |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 205 | {new(struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 206 | a int8 |
| 207 | b int32 |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 208 | }), |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 209 | "struct { a int8; b int32 }{0, 0}", |
| 210 | }, |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 211 | {new(struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 212 | a int8 |
| 213 | b int8 |
| 214 | c int32 |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 215 | }), |
Robert Griesemer | 7151d23 | 2009-11-05 18:27:30 -0800 | [diff] [blame] | 216 | "struct { a int8; b int8; c int32 }{0, 0, 0}", |
| 217 | }, |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | func testType(t *testing.T, i int, typ Type, want string) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 221 | s := typ.String() |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 222 | if s != want { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 223 | t.Errorf("#%d: have %#q, want %#q", i, s, want) |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | func TestTypes(t *testing.T) { |
| 228 | for i, tt := range typeTests { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 229 | testType(t, i, ValueOf(tt.i).Field(0).Type(), tt.s) |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Adam Langley | a8a678f | 2009-10-21 19:51:27 -0700 | [diff] [blame] | 233 | func TestSet(t *testing.T) { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 234 | for i, tt := range valueTests { |
Russ Cox | a479a45 | 2011-11-16 19:18:25 -0500 | [diff] [blame] | 235 | v := ValueOf(tt.i) |
| 236 | v = v.Elem() |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 237 | switch v.Kind() { |
| 238 | case Int: |
| 239 | v.SetInt(132) |
| 240 | case Int8: |
| 241 | v.SetInt(8) |
| 242 | case Int16: |
| 243 | v.SetInt(16) |
| 244 | case Int32: |
| 245 | v.SetInt(32) |
| 246 | case Int64: |
| 247 | v.SetInt(64) |
| 248 | case Uint: |
| 249 | v.SetUint(132) |
| 250 | case Uint8: |
| 251 | v.SetUint(8) |
| 252 | case Uint16: |
| 253 | v.SetUint(16) |
| 254 | case Uint32: |
| 255 | v.SetUint(32) |
| 256 | case Uint64: |
| 257 | v.SetUint(64) |
| 258 | case Float32: |
| 259 | v.SetFloat(256.25) |
| 260 | case Float64: |
| 261 | v.SetFloat(512.125) |
| 262 | case Complex64: |
| 263 | v.SetComplex(532.125 + 10i) |
| 264 | case Complex128: |
| 265 | v.SetComplex(564.25 + 1i) |
| 266 | case String: |
| 267 | v.SetString("stringy cheese") |
| 268 | case Bool: |
| 269 | v.SetBool(true) |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 270 | } |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 271 | s := valueToString(v) |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 272 | if s != tt.s { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 273 | t.Errorf("#%d: have %#q, want %#q", i, s, tt.s) |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Adam Langley | a8a678f | 2009-10-21 19:51:27 -0700 | [diff] [blame] | 278 | func TestSetValue(t *testing.T) { |
| 279 | for i, tt := range valueTests { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 280 | v := ValueOf(tt.i).Elem() |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 281 | switch v.Kind() { |
| 282 | case Int: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 283 | v.Set(ValueOf(int(132))) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 284 | case Int8: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 285 | v.Set(ValueOf(int8(8))) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 286 | case Int16: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 287 | v.Set(ValueOf(int16(16))) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 288 | case Int32: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 289 | v.Set(ValueOf(int32(32))) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 290 | case Int64: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 291 | v.Set(ValueOf(int64(64))) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 292 | case Uint: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 293 | v.Set(ValueOf(uint(132))) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 294 | case Uint8: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 295 | v.Set(ValueOf(uint8(8))) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 296 | case Uint16: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 297 | v.Set(ValueOf(uint16(16))) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 298 | case Uint32: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 299 | v.Set(ValueOf(uint32(32))) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 300 | case Uint64: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 301 | v.Set(ValueOf(uint64(64))) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 302 | case Float32: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 303 | v.Set(ValueOf(float32(256.25))) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 304 | case Float64: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 305 | v.Set(ValueOf(512.125)) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 306 | case Complex64: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 307 | v.Set(ValueOf(complex64(532.125 + 10i))) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 308 | case Complex128: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 309 | v.Set(ValueOf(complex128(564.25 + 1i))) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 310 | case String: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 311 | v.Set(ValueOf("stringy cheese")) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 312 | case Bool: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 313 | v.Set(ValueOf(true)) |
Adam Langley | a8a678f | 2009-10-21 19:51:27 -0700 | [diff] [blame] | 314 | } |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 315 | s := valueToString(v) |
Adam Langley | a8a678f | 2009-10-21 19:51:27 -0700 | [diff] [blame] | 316 | if s != tt.s { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 317 | t.Errorf("#%d: have %#q, want %#q", i, s, tt.s) |
Adam Langley | a8a678f | 2009-10-21 19:51:27 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 322 | var _i = 7 |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 323 | |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 324 | var valueToStringTests = []pair{ |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 325 | {123, "123"}, |
| 326 | {123.5, "123.5"}, |
| 327 | {byte(123), "123"}, |
| 328 | {"abc", "abc"}, |
| 329 | {T{123, 456.75, "hello", &_i}, "reflect_test.T{123, 456.75, hello, *int(&7)}"}, |
| 330 | {new(chan *T), "*chan *reflect_test.T(&chan *reflect_test.T)"}, |
| 331 | {[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"}, |
| 332 | {&[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})"}, |
| 333 | {[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, "[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"}, |
| 334 | {&[]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] | 335 | } |
| 336 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 337 | func TestValueToString(t *testing.T) { |
| 338 | for i, test := range valueToStringTests { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 339 | s := valueToString(ValueOf(test.i)) |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 340 | if s != test.s { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 341 | t.Errorf("#%d: have %#q, want %#q", i, s, test.s) |
Rob Pike | 419e1e0 | 2008-11-12 19:05:05 -0800 | [diff] [blame] | 342 | } |
| 343 | } |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 344 | } |
Rob Pike | 419e1e0 | 2008-11-12 19:05:05 -0800 | [diff] [blame] | 345 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 346 | func TestArrayElemSet(t *testing.T) { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 347 | v := ValueOf(&[10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).Elem() |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 348 | v.Index(4).SetInt(123) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 349 | s := valueToString(v) |
| 350 | const want = "[10]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}" |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 351 | if s != want { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 352 | t.Errorf("[10]int: have %#q want %#q", s, want) |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 353 | } |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 354 | |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 355 | v = ValueOf([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 356 | v.Index(4).SetInt(123) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 357 | s = valueToString(v) |
| 358 | const want1 = "[]int{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}" |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 359 | if s != want1 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 360 | t.Errorf("[]int: have %#q want %#q", s, want1) |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 363 | |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 364 | func TestPtrPointTo(t *testing.T) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 365 | var ip *int32 |
| 366 | var i int32 = 1234 |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 367 | vip := ValueOf(&ip) |
| 368 | vi := ValueOf(&i).Elem() |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 369 | vip.Elem().Set(vi.Addr()) |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 370 | if *ip != 1234 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 371 | t.Errorf("got %d, want 1234", *ip) |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 372 | } |
Robert Griesemer | a48b35e | 2010-08-17 15:12:28 -0700 | [diff] [blame] | 373 | |
| 374 | ip = nil |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 375 | vp := ValueOf(&ip).Elem() |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 376 | vp.Set(Zero(vp.Type())) |
Robert Griesemer | a48b35e | 2010-08-17 15:12:28 -0700 | [diff] [blame] | 377 | if ip != nil { |
| 378 | t.Errorf("got non-nil (%p), want nil", ip) |
| 379 | } |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 380 | } |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 381 | |
Russ Cox | 7295b61 | 2010-04-20 17:02:08 -0700 | [diff] [blame] | 382 | func TestPtrSetNil(t *testing.T) { |
| 383 | var i int32 = 1234 |
| 384 | ip := &i |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 385 | vip := ValueOf(&ip) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 386 | vip.Elem().Set(Zero(vip.Elem().Type())) |
Russ Cox | 7295b61 | 2010-04-20 17:02:08 -0700 | [diff] [blame] | 387 | if ip != nil { |
| 388 | t.Errorf("got non-nil (%d), want nil", *ip) |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | func TestMapSetNil(t *testing.T) { |
| 393 | m := make(map[string]int) |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 394 | vm := ValueOf(&m) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 395 | vm.Elem().Set(Zero(vm.Elem().Type())) |
Russ Cox | 7295b61 | 2010-04-20 17:02:08 -0700 | [diff] [blame] | 396 | if m != nil { |
| 397 | t.Errorf("got non-nil (%p), want nil", m) |
| 398 | } |
| 399 | } |
| 400 | |
Rob Pike | e02f2b5 | 2009-11-08 21:57:59 -0800 | [diff] [blame] | 401 | func TestAll(t *testing.T) { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 402 | testType(t, 1, TypeOf((int8)(0)), "int8") |
| 403 | testType(t, 2, TypeOf((*int8)(nil)).Elem(), "int8") |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 404 | |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 405 | typ := TypeOf((*struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 406 | c chan *int32 |
| 407 | d float32 |
| 408 | })(nil)) |
| 409 | testType(t, 3, typ, "*struct { c chan *int32; d float32 }") |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 410 | etyp := typ.Elem() |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 411 | testType(t, 4, etyp, "struct { c chan *int32; d float32 }") |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 412 | styp := etyp |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 413 | f := styp.Field(0) |
| 414 | testType(t, 5, f.Type, "chan *int32") |
Rob Pike | a93c5c8 | 2009-07-16 18:21:14 -0700 | [diff] [blame] | 415 | |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 416 | f, present := styp.FieldByName("d") |
Rob Pike | a93c5c8 | 2009-07-16 18:21:14 -0700 | [diff] [blame] | 417 | if !present { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 418 | t.Errorf("FieldByName says present field is absent") |
Rob Pike | a93c5c8 | 2009-07-16 18:21:14 -0700 | [diff] [blame] | 419 | } |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 420 | testType(t, 6, f.Type, "float32") |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 421 | |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 422 | f, present = styp.FieldByName("absent") |
Rob Pike | a93c5c8 | 2009-07-16 18:21:14 -0700 | [diff] [blame] | 423 | if present { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 424 | t.Errorf("FieldByName says absent field is present") |
Rob Pike | a93c5c8 | 2009-07-16 18:21:14 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 427 | typ = TypeOf([32]int32{}) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 428 | testType(t, 7, typ, "[32]int32") |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 429 | testType(t, 8, typ.Elem(), "int32") |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 430 | |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 431 | typ = TypeOf((map[string]*int32)(nil)) |
Russ Cox | 434a6c8 | 2011-12-02 14:45:07 -0500 | [diff] [blame] | 432 | testType(t, 9, typ, "map[string]*int32") |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 433 | mtyp := typ |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 434 | testType(t, 10, mtyp.Key(), "string") |
| 435 | testType(t, 11, mtyp.Elem(), "*int32") |
Rob Pike | bdbb958 | 2008-11-05 08:17:01 -0800 | [diff] [blame] | 436 | |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 437 | typ = TypeOf((chan<- string)(nil)) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 438 | testType(t, 12, typ, "chan<- string") |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 439 | testType(t, 13, typ.Elem(), "string") |
Rob Pike | 5a1cbe8 | 2008-11-05 13:01:33 -0800 | [diff] [blame] | 440 | |
| 441 | // make sure tag strings are not part of element type |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 442 | typ = TypeOf(struct { |
Russ Cox | 25733a9 | 2011-06-29 09:52:34 -0400 | [diff] [blame] | 443 | d []uint32 `reflect:"TAG"` |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 444 | }{}).Field(0).Type |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 445 | testType(t, 14, typ, "[]uint32") |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 446 | } |
Russ Cox | 387df5e | 2008-11-24 14:51:33 -0800 | [diff] [blame] | 447 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 448 | func TestInterfaceGet(t *testing.T) { |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 449 | var inter struct { |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 450 | E interface{} |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 451 | } |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 452 | inter.E = 123.456 |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 453 | v1 := ValueOf(&inter) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 454 | v2 := v1.Elem().Field(0) |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 455 | assert(t, v2.Type().String(), "interface {}") |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 456 | i2 := v2.Interface() |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 457 | v3 := ValueOf(i2) |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 458 | assert(t, v3.Type().String(), "float64") |
Russ Cox | 387df5e | 2008-11-24 14:51:33 -0800 | [diff] [blame] | 459 | } |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 460 | |
Russ Cox | ac6ebfd | 2009-04-06 21:28:04 -0700 | [diff] [blame] | 461 | func TestInterfaceValue(t *testing.T) { |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 462 | var inter struct { |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 463 | E interface{} |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 464 | } |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 465 | inter.E = 123.456 |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 466 | v1 := ValueOf(&inter) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 467 | v2 := v1.Elem().Field(0) |
Luuk van Dijk | 50110c9 | 2011-10-31 18:09:40 +0100 | [diff] [blame] | 468 | assert(t, v2.Type().String(), "interface {}") |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 469 | v3 := v2.Elem() |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 470 | assert(t, v3.Type().String(), "float64") |
Russ Cox | 64627b0 | 2009-04-15 00:55:58 -0700 | [diff] [blame] | 471 | |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 472 | i3 := v2.Interface() |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 473 | if _, ok := i3.(float64); !ok { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 474 | t.Error("v2.Interface() did not return float64, got ", TypeOf(i3)) |
Russ Cox | 4b8c13d | 2009-04-14 19:03:57 -0700 | [diff] [blame] | 475 | } |
Russ Cox | ac6ebfd | 2009-04-06 21:28:04 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Ian Lance Taylor | ca9765d | 2009-04-14 06:46:01 -0700 | [diff] [blame] | 478 | func TestFunctionValue(t *testing.T) { |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 479 | var x interface{} = func() {} |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 480 | v := ValueOf(x) |
Russ Cox | 46deaa2 | 2011-12-06 10:48:17 -0500 | [diff] [blame] | 481 | if fmt.Sprint(v.Interface()) != fmt.Sprint(x) { |
| 482 | t.Fatalf("TestFunction returned wrong pointer") |
Ian Lance Taylor | ca9765d | 2009-04-14 06:46:01 -0700 | [diff] [blame] | 483 | } |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 484 | assert(t, v.Type().String(), "func()") |
Ian Lance Taylor | ca9765d | 2009-04-14 06:46:01 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Nigel Tao | 8b64cd9 | 2010-12-15 08:50:08 +1100 | [diff] [blame] | 487 | var appendTests = []struct { |
| 488 | orig, extra []int |
| 489 | }{ |
| 490 | {make([]int, 2, 4), []int{22}}, |
| 491 | {make([]int, 2, 4), []int{22, 33, 44}}, |
| 492 | } |
| 493 | |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 494 | func sameInts(x, y []int) bool { |
| 495 | if len(x) != len(y) { |
| 496 | return false |
| 497 | } |
| 498 | for i, xx := range x { |
| 499 | if xx != y[i] { |
| 500 | return false |
| 501 | } |
| 502 | } |
| 503 | return true |
| 504 | } |
| 505 | |
Nigel Tao | 8b64cd9 | 2010-12-15 08:50:08 +1100 | [diff] [blame] | 506 | func TestAppend(t *testing.T) { |
| 507 | for i, test := range appendTests { |
| 508 | origLen, extraLen := len(test.orig), len(test.extra) |
| 509 | want := append(test.orig, test.extra...) |
| 510 | // Convert extra from []int to []Value. |
| 511 | e0 := make([]Value, len(test.extra)) |
| 512 | for j, e := range test.extra { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 513 | e0[j] = ValueOf(e) |
Nigel Tao | 8b64cd9 | 2010-12-15 08:50:08 +1100 | [diff] [blame] | 514 | } |
| 515 | // Convert extra from []int to *SliceValue. |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 516 | e1 := ValueOf(test.extra) |
Nigel Tao | 8b64cd9 | 2010-12-15 08:50:08 +1100 | [diff] [blame] | 517 | // Test Append. |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 518 | a0 := ValueOf(test.orig) |
Nigel Tao | 8b64cd9 | 2010-12-15 08:50:08 +1100 | [diff] [blame] | 519 | have0 := Append(a0, e0...).Interface().([]int) |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 520 | if !sameInts(have0, want) { |
| 521 | t.Errorf("Append #%d: have %v, want %v (%p %p)", i, have0, want, test.orig, have0) |
Nigel Tao | 8b64cd9 | 2010-12-15 08:50:08 +1100 | [diff] [blame] | 522 | } |
| 523 | // Check that the orig and extra slices were not modified. |
| 524 | if len(test.orig) != origLen { |
| 525 | t.Errorf("Append #%d origLen: have %v, want %v", i, len(test.orig), origLen) |
| 526 | } |
| 527 | if len(test.extra) != extraLen { |
| 528 | t.Errorf("Append #%d extraLen: have %v, want %v", i, len(test.extra), extraLen) |
| 529 | } |
| 530 | // Test AppendSlice. |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 531 | a1 := ValueOf(test.orig) |
Nigel Tao | 8b64cd9 | 2010-12-15 08:50:08 +1100 | [diff] [blame] | 532 | have1 := AppendSlice(a1, e1).Interface().([]int) |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 533 | if !sameInts(have1, want) { |
Nigel Tao | 8b64cd9 | 2010-12-15 08:50:08 +1100 | [diff] [blame] | 534 | t.Errorf("AppendSlice #%d: have %v, want %v", i, have1, want) |
| 535 | } |
| 536 | // Check that the orig and extra slices were not modified. |
| 537 | if len(test.orig) != origLen { |
| 538 | t.Errorf("AppendSlice #%d origLen: have %v, want %v", i, len(test.orig), origLen) |
| 539 | } |
| 540 | if len(test.extra) != extraLen { |
| 541 | t.Errorf("AppendSlice #%d extraLen: have %v, want %v", i, len(test.extra), extraLen) |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | func TestCopy(t *testing.T) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 547 | a := []int{1, 2, 3, 4, 10, 9, 8, 7} |
| 548 | b := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44} |
| 549 | c := []int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44} |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 550 | for i := 0; i < len(b); i++ { |
| 551 | if b[i] != c[i] { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 552 | t.Fatalf("b != c before test") |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 553 | } |
| 554 | } |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 555 | a1 := a |
| 556 | b1 := b |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 557 | aa := ValueOf(&a1).Elem() |
| 558 | ab := ValueOf(&b1).Elem() |
Russ Cox | d47d888 | 2008-12-18 22:37:22 -0800 | [diff] [blame] | 559 | for tocopy := 1; tocopy <= 7; tocopy++ { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 560 | aa.SetLen(tocopy) |
Nigel Tao | 73fd298 | 2010-12-12 20:27:29 +1100 | [diff] [blame] | 561 | Copy(ab, aa) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 562 | aa.SetLen(8) |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 563 | for i := 0; i < tocopy; i++ { |
| 564 | if a[i] != b[i] { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 565 | t.Errorf("(i) tocopy=%d a[%d]=%d, b[%d]=%d", |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 566 | tocopy, i, a[i], i, b[i]) |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | for i := tocopy; i < len(b); i++ { |
| 570 | if b[i] != c[i] { |
| 571 | if i < len(a) { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 572 | t.Errorf("(ii) tocopy=%d a[%d]=%d, b[%d]=%d, c[%d]=%d", |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 573 | tocopy, i, a[i], i, b[i], i, c[i]) |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 574 | } else { |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 575 | t.Errorf("(iii) tocopy=%d b[%d]=%d, c[%d]=%d", |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 576 | tocopy, i, b[i], i, c[i]) |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 577 | } |
Russ Cox | d47d888 | 2008-12-18 22:37:22 -0800 | [diff] [blame] | 578 | } else { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 579 | t.Logf("tocopy=%d elem %d is okay\n", tocopy, i) |
Russ Cox | d0e30cd | 2008-12-10 15:55:59 -0800 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | } |
| 583 | } |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 584 | |
Gustavo Niemeyer | 6850dba | 2011-04-27 18:22:53 -0300 | [diff] [blame] | 585 | func TestCopyArray(t *testing.T) { |
| 586 | a := [8]int{1, 2, 3, 4, 10, 9, 8, 7} |
| 587 | b := [11]int{11, 22, 33, 44, 1010, 99, 88, 77, 66, 55, 44} |
| 588 | c := b |
| 589 | aa := ValueOf(&a).Elem() |
| 590 | ab := ValueOf(&b).Elem() |
| 591 | Copy(ab, aa) |
| 592 | for i := 0; i < len(a); i++ { |
| 593 | if a[i] != b[i] { |
| 594 | t.Errorf("(i) a[%d]=%d, b[%d]=%d", i, a[i], i, b[i]) |
| 595 | } |
| 596 | } |
| 597 | for i := len(a); i < len(b); i++ { |
| 598 | if b[i] != c[i] { |
Gustavo Niemeyer | 3e9a1d5 | 2011-04-28 14:16:41 -0300 | [diff] [blame] | 599 | t.Errorf("(ii) b[%d]=%d, c[%d]=%d", i, b[i], i, c[i]) |
Gustavo Niemeyer | 6850dba | 2011-04-27 18:22:53 -0300 | [diff] [blame] | 600 | } else { |
| 601 | t.Logf("elem %d is okay\n", i) |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 606 | func TestBigUnnamedStruct(t *testing.T) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 607 | b := struct{ a, b, c, d int64 }{1, 2, 3, 4} |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 608 | v := ValueOf(b) |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 609 | b1 := v.Interface().(struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 610 | a, b, c, d int64 |
| 611 | }) |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 612 | if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 613 | t.Errorf("ValueOf(%v).Interface().(*Big) = %v", b, b1) |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
Rob Pike | ed2ac9b | 2009-01-16 12:48:07 -0800 | [diff] [blame] | 617 | type big struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 618 | a, b, c, d, e int64 |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 619 | } |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 620 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 621 | func TestBigStruct(t *testing.T) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 622 | b := big{1, 2, 3, 4, 5} |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 623 | v := ValueOf(b) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 624 | b1 := v.Interface().(big) |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 625 | if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d || b1.e != b.e { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 626 | t.Errorf("ValueOf(%v).Interface().(big) = %v", b, b1) |
Russ Cox | 484ba93 | 2009-01-09 00:17:46 -0800 | [diff] [blame] | 627 | } |
| 628 | } |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 629 | |
| 630 | type Basic struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 631 | x int |
| 632 | y float32 |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Russ Cox | 64627b0 | 2009-04-15 00:55:58 -0700 | [diff] [blame] | 635 | type NotBasic Basic |
| 636 | |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 637 | type DeepEqualTest struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 638 | a, b interface{} |
| 639 | eq bool |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Rob Pike | 3a1c226 | 2012-02-24 16:25:39 +1100 | [diff] [blame] | 642 | // Simple functions for DeepEqual tests. |
| 643 | var ( |
| 644 | fn1 func() // nil. |
| 645 | fn2 func() // nil. |
| 646 | fn3 = func() { fn1() } // Not nil. |
| 647 | ) |
| 648 | |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 649 | var deepEqualTests = []DeepEqualTest{ |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 650 | // Equalities |
Rob Pike | 5337290 | 2012-04-23 12:07:02 +1000 | [diff] [blame] | 651 | {nil, nil, true}, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 652 | {1, 1, true}, |
| 653 | {int32(1), int32(1), true}, |
| 654 | {0.5, 0.5, true}, |
| 655 | {float32(0.5), float32(0.5), true}, |
| 656 | {"hello", "hello", true}, |
| 657 | {make([]int, 10), make([]int, 10), true}, |
| 658 | {&[3]int{1, 2, 3}, &[3]int{1, 2, 3}, true}, |
| 659 | {Basic{1, 0.5}, Basic{1, 0.5}, true}, |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 660 | {error(nil), error(nil), true}, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 661 | {map[int]string{1: "one", 2: "two"}, map[int]string{2: "two", 1: "one"}, true}, |
Rob Pike | 3a1c226 | 2012-02-24 16:25:39 +1100 | [diff] [blame] | 662 | {fn1, fn2, true}, |
Russ Cox | a439f66 | 2009-07-01 16:45:09 -0700 | [diff] [blame] | 663 | |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 664 | // Inequalities |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 665 | {1, 2, false}, |
| 666 | {int32(1), int32(2), false}, |
| 667 | {0.5, 0.6, false}, |
| 668 | {float32(0.5), float32(0.6), false}, |
| 669 | {"hello", "hey", false}, |
| 670 | {make([]int, 10), make([]int, 11), false}, |
| 671 | {&[3]int{1, 2, 3}, &[3]int{1, 2, 4}, false}, |
| 672 | {Basic{1, 0.5}, Basic{1, 0.6}, false}, |
| 673 | {Basic{1, 0}, Basic{2, 0}, false}, |
| 674 | {map[int]string{1: "one", 3: "two"}, map[int]string{2: "two", 1: "one"}, false}, |
| 675 | {map[int]string{1: "one", 2: "txo"}, map[int]string{2: "two", 1: "one"}, false}, |
| 676 | {map[int]string{1: "one"}, map[int]string{2: "two", 1: "one"}, false}, |
| 677 | {map[int]string{2: "two", 1: "one"}, map[int]string{1: "one"}, false}, |
| 678 | {nil, 1, false}, |
| 679 | {1, nil, false}, |
Rob Pike | 3a1c226 | 2012-02-24 16:25:39 +1100 | [diff] [blame] | 680 | {fn1, fn3, false}, |
| 681 | {fn3, fn3, false}, |
Shawn Smith | 96ee10f | 2013-12-30 11:39:47 -0800 | [diff] [blame] | 682 | {[][]int{[]int{1}}, [][]int{[]int{2}}, false}, |
Russ Cox | a439f66 | 2009-07-01 16:45:09 -0700 | [diff] [blame] | 683 | |
Russ Cox | 4e65478 | 2011-11-14 16:11:15 -0500 | [diff] [blame] | 684 | // Nil vs empty: not the same. |
| 685 | {[]int{}, []int(nil), false}, |
| 686 | {[]int{}, []int{}, true}, |
| 687 | {[]int(nil), []int(nil), true}, |
| 688 | {map[int]int{}, map[int]int(nil), false}, |
| 689 | {map[int]int{}, map[int]int{}, true}, |
| 690 | {map[int]int(nil), map[int]int(nil), true}, |
| 691 | |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 692 | // Mismatched types |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 693 | {1, 1.0, false}, |
| 694 | {int32(1), int64(1), false}, |
| 695 | {0.5, "hello", false}, |
| 696 | {[]int{1, 2, 3}, [3]int{1, 2, 3}, false}, |
| 697 | {&[3]interface{}{1, 2, 4}, &[3]interface{}{1, 2, "s"}, false}, |
| 698 | {Basic{1, 0.5}, NotBasic{1, 0.5}, false}, |
| 699 | {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] | 700 | } |
| 701 | |
| 702 | func TestDeepEqual(t *testing.T) { |
Russ Cox | ca6a0fe | 2009-09-15 09:41:59 -0700 | [diff] [blame] | 703 | for _, test := range deepEqualTests { |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 704 | if r := DeepEqual(test.a, test.b); r != test.eq { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 705 | t.Errorf("DeepEqual(%v, %v) = %v, want %v", test.a, test.b, r, test.eq) |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 710 | func TestTypeOf(t *testing.T) { |
Rob Pike | 5337290 | 2012-04-23 12:07:02 +1000 | [diff] [blame] | 711 | // Special case for nil |
| 712 | if typ := TypeOf(nil); typ != nil { |
| 713 | t.Errorf("expected nil type for nil value; got %v", typ) |
| 714 | } |
Russ Cox | ca6a0fe | 2009-09-15 09:41:59 -0700 | [diff] [blame] | 715 | for _, test := range deepEqualTests { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 716 | v := ValueOf(test.a) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 717 | if !v.IsValid() { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 718 | continue |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 719 | } |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 720 | typ := TypeOf(test.a) |
Russ Cox | 4866223 | 2009-06-25 14:25:38 -0700 | [diff] [blame] | 721 | if typ != v.Type() { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 722 | t.Errorf("TypeOf(%v) = %v, but ValueOf(%v).Type() = %v", test.a, typ, test.a, v.Type()) |
Russ Cox | 4866223 | 2009-06-25 14:25:38 -0700 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
Rob Pike | 1880b90 | 2009-07-10 11:20:10 -0700 | [diff] [blame] | 727 | type Recursive struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 728 | x int |
| 729 | r *Recursive |
Rob Pike | 1880b90 | 2009-07-10 11:20:10 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 732 | func TestDeepEqualRecursiveStruct(t *testing.T) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 733 | a, b := new(Recursive), new(Recursive) |
| 734 | *a = Recursive{12, a} |
| 735 | *b = Recursive{12, b} |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 736 | if !DeepEqual(a, b) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 737 | t.Error("DeepEqual(recursive same) = false, want true") |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
Russ Cox | 45bdf03 | 2010-06-20 12:16:25 -0700 | [diff] [blame] | 741 | type _Complex struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 742 | a int |
Russ Cox | 45bdf03 | 2010-06-20 12:16:25 -0700 | [diff] [blame] | 743 | b [3]*_Complex |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 744 | c *string |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 745 | d map[float64]float64 |
Rob Pike | 1880b90 | 2009-07-10 11:20:10 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 748 | func TestDeepEqualComplexStruct(t *testing.T) { |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 749 | m := make(map[float64]float64) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 750 | stra, strb := "hello", "hello" |
Russ Cox | 45bdf03 | 2010-06-20 12:16:25 -0700 | [diff] [blame] | 751 | a, b := new(_Complex), new(_Complex) |
| 752 | *a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m} |
| 753 | *b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m} |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 754 | if !DeepEqual(a, b) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 755 | t.Error("DeepEqual(complex same) = false, want true") |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
| 759 | func TestDeepEqualComplexStructInequality(t *testing.T) { |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 760 | m := make(map[float64]float64) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 761 | stra, strb := "hello", "helloo" // Difference is here |
Russ Cox | 45bdf03 | 2010-06-20 12:16:25 -0700 | [diff] [blame] | 762 | a, b := new(_Complex), new(_Complex) |
| 763 | *a = _Complex{5, [3]*_Complex{a, b, a}, &stra, m} |
| 764 | *b = _Complex{5, [3]*_Complex{b, a, a}, &strb, m} |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 765 | if DeepEqual(a, b) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 766 | t.Error("DeepEqual(complex different) = true, want false") |
Daniel Nadasi | c4ad4f9 | 2009-04-01 22:20:18 -0700 | [diff] [blame] | 767 | } |
| 768 | } |
Rob Pike | 93831d2 | 2009-04-29 22:16:53 -0700 | [diff] [blame] | 769 | |
Russ Cox | 86e6a44 | 2011-05-03 10:38:37 -0400 | [diff] [blame] | 770 | type UnexpT struct { |
| 771 | m map[int]int |
| 772 | } |
| 773 | |
| 774 | func TestDeepEqualUnexportedMap(t *testing.T) { |
| 775 | // Check that DeepEqual can look at unexported fields. |
| 776 | x1 := UnexpT{map[int]int{1: 2}} |
| 777 | x2 := UnexpT{map[int]int{1: 2}} |
| 778 | if !DeepEqual(&x1, &x2) { |
| 779 | t.Error("DeepEqual(x1, x2) = false, want true") |
| 780 | } |
| 781 | |
| 782 | y1 := UnexpT{map[int]int{2: 3}} |
| 783 | if DeepEqual(&x1, &y1) { |
| 784 | t.Error("DeepEqual(x1, y1) = true, want false") |
| 785 | } |
| 786 | } |
| 787 | |
Rob Pike | 93831d2 | 2009-04-29 22:16:53 -0700 | [diff] [blame] | 788 | func check2ndField(x interface{}, offs uintptr, t *testing.T) { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 789 | s := ValueOf(x) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 790 | f := s.Type().Field(1) |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 791 | if f.Offset != offs { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 792 | t.Error("mismatched offsets in structure alignment:", f.Offset, offs) |
Rob Pike | 93831d2 | 2009-04-29 22:16:53 -0700 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
| 796 | // Check that structure alignment & offsets viewed through reflect agree with those |
| 797 | // from the compiler itself. |
| 798 | func TestAlignment(t *testing.T) { |
| 799 | type T1inner struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 800 | a int |
Rob Pike | 93831d2 | 2009-04-29 22:16:53 -0700 | [diff] [blame] | 801 | } |
| 802 | type T1 struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 803 | T1inner |
| 804 | f int |
Rob Pike | 93831d2 | 2009-04-29 22:16:53 -0700 | [diff] [blame] | 805 | } |
| 806 | type T2inner struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 807 | a, b int |
Rob Pike | 93831d2 | 2009-04-29 22:16:53 -0700 | [diff] [blame] | 808 | } |
| 809 | type T2 struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 810 | T2inner |
| 811 | f int |
Rob Pike | 93831d2 | 2009-04-29 22:16:53 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 814 | x := T1{T1inner{2}, 17} |
| 815 | check2ndField(x, uintptr(unsafe.Pointer(&x.f))-uintptr(unsafe.Pointer(&x)), t) |
Rob Pike | 93831d2 | 2009-04-29 22:16:53 -0700 | [diff] [blame] | 816 | |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 817 | x1 := T2{T2inner{2, 3}, 17} |
| 818 | check2ndField(x1, uintptr(unsafe.Pointer(&x1.f))-uintptr(unsafe.Pointer(&x1)), t) |
Rob Pike | 93831d2 | 2009-04-29 22:16:53 -0700 | [diff] [blame] | 819 | } |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 820 | |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 821 | func Nil(a interface{}, t *testing.T) { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 822 | n := ValueOf(a).Field(0) |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 823 | if !n.IsNil() { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 824 | t.Errorf("%v should be nil", a) |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | |
| 828 | func NotNil(a interface{}, t *testing.T) { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 829 | n := ValueOf(a).Field(0) |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 830 | if n.IsNil() { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 831 | t.Errorf("value of type %v should not be nil", ValueOf(a).Type().String()) |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | |
| 835 | func TestIsNil(t *testing.T) { |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 836 | // These implement IsNil. |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 837 | // Wrap in extra struct to hide interface type. |
| 838 | doNil := []interface{}{ |
Robert Griesemer | a05a546 | 2009-11-06 16:33:53 -0800 | [diff] [blame] | 839 | struct{ x *int }{}, |
| 840 | struct{ x interface{} }{}, |
| 841 | struct{ x map[string]int }{}, |
| 842 | struct{ x func() bool }{}, |
| 843 | struct{ x chan int }{}, |
| 844 | struct{ x []string }{}, |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 845 | } |
Russ Cox | ca6a0fe | 2009-09-15 09:41:59 -0700 | [diff] [blame] | 846 | for _, ts := range doNil { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 847 | ty := TypeOf(ts).Field(0).Type |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 848 | v := Zero(ty) |
| 849 | v.IsNil() // panics if not okay to call |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 850 | } |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 851 | |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 852 | // Check the implementations |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 853 | var pi struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 854 | x *int |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 855 | } |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 856 | Nil(pi, t) |
| 857 | pi.x = new(int) |
| 858 | NotNil(pi, t) |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 859 | |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 860 | var si struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 861 | x []int |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 862 | } |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 863 | Nil(si, t) |
| 864 | si.x = make([]int, 10) |
| 865 | NotNil(si, t) |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 866 | |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 867 | var ci struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 868 | x chan int |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 869 | } |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 870 | Nil(ci, t) |
| 871 | ci.x = make(chan int) |
| 872 | NotNil(ci, t) |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 873 | |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 874 | var mi struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 875 | x map[int]int |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 876 | } |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 877 | Nil(mi, t) |
| 878 | mi.x = make(map[int]int) |
| 879 | NotNil(mi, t) |
Russ Cox | 64f4e0b | 2009-07-07 11:03:12 -0700 | [diff] [blame] | 880 | |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 881 | var ii struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 882 | x interface{} |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 883 | } |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 884 | Nil(ii, t) |
| 885 | ii.x = 2 |
| 886 | NotNil(ii, t) |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 887 | |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 888 | var fi struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 889 | x func(t *testing.T) |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 890 | } |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 891 | Nil(fi, t) |
| 892 | fi.x = TestIsNil |
| 893 | NotNil(fi, t) |
Rob Pike | a8f6e38 | 2009-05-12 14:57:44 -0700 | [diff] [blame] | 894 | } |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 895 | |
| 896 | func TestInterfaceExtraction(t *testing.T) { |
| 897 | var s struct { |
Russ Cox | 304cf4d | 2011-10-17 18:48:45 -0400 | [diff] [blame] | 898 | W io.Writer |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Russ Cox | 304cf4d | 2011-10-17 18:48:45 -0400 | [diff] [blame] | 901 | s.W = os.Stdout |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 902 | v := Indirect(ValueOf(&s)).Field(0).Interface() |
Russ Cox | 304cf4d | 2011-10-17 18:48:45 -0400 | [diff] [blame] | 903 | if v != s.W.(interface{}) { |
| 904 | t.Error("Interface() on interface: ", v, s.W) |
Russ Cox | 96cfd15 | 2009-05-21 11:50:20 -0700 | [diff] [blame] | 905 | } |
| 906 | } |
Russ Cox | f966623 | 2009-05-21 14:06:43 -0700 | [diff] [blame] | 907 | |
David Symonds | d4e57ff | 2009-06-15 18:35:04 -0700 | [diff] [blame] | 908 | func TestNilPtrValueSub(t *testing.T) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 909 | var pi *int |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 910 | if pv := ValueOf(pi); pv.Elem().IsValid() { |
| 911 | t.Error("ValueOf((*int)(nil)).Elem().IsValid()") |
David Symonds | d4e57ff | 2009-06-15 18:35:04 -0700 | [diff] [blame] | 912 | } |
| 913 | } |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 914 | |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 915 | func TestMap(t *testing.T) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 916 | m := map[string]int{"a": 1, "b": 2} |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 917 | mv := ValueOf(m) |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 918 | if n := mv.Len(); n != len(m) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 919 | t.Errorf("Len = %d, want %d", n, len(m)) |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 920 | } |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 921 | keys := mv.MapKeys() |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 922 | newmap := MakeMap(mv.Type()) |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 923 | for k, v := range m { |
| 924 | // Check that returned Keys match keys in range. |
David Symonds | 9049abb | 2011-10-18 12:47:34 +1100 | [diff] [blame] | 925 | // These aren't required to be in the same order. |
| 926 | seen := false |
| 927 | for _, kv := range keys { |
| 928 | if kv.String() == k { |
| 929 | seen = true |
| 930 | break |
| 931 | } |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 932 | } |
David Symonds | 9049abb | 2011-10-18 12:47:34 +1100 | [diff] [blame] | 933 | if !seen { |
| 934 | t.Errorf("Missing key %q", k) |
| 935 | } |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 936 | |
| 937 | // Check that value lookup is correct. |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 938 | vv := mv.MapIndex(ValueOf(k)) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 939 | if vi := vv.Int(); vi != int64(v) { |
Rob Pike | 1ce6245 | 2010-12-07 16:42:54 -0500 | [diff] [blame] | 940 | t.Errorf("Key %q: have value %d, want %d", k, vi, v) |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | // Copy into new map. |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 944 | newmap.SetMapIndex(ValueOf(k), ValueOf(v)) |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 945 | } |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 946 | vv := mv.MapIndex(ValueOf("not-present")) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 947 | if vv.IsValid() { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 948 | t.Errorf("Invalid key: got non-nil value %s", valueToString(vv)) |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 949 | } |
| 950 | |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 951 | newm := newmap.Interface().(map[string]int) |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 952 | if len(newm) != len(m) { |
Kamil Kisiel | e07b5ba | 2013-09-23 13:19:08 -0400 | [diff] [blame] | 953 | t.Errorf("length after copy: newm=%d, m=%d", len(newm), len(m)) |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | for k, v := range newm { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 957 | mv, ok := m[k] |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 958 | if mv != v { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 959 | t.Errorf("newm[%q] = %d, but m[%q] = %d, %v", k, v, k, mv, ok) |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 960 | } |
| 961 | } |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 962 | |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 963 | newmap.SetMapIndex(ValueOf("a"), Value{}) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 964 | v, ok := newm["a"] |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 965 | if ok { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 966 | t.Errorf("newm[\"a\"] = %d after delete", v) |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 967 | } |
Russ Cox | 7295b61 | 2010-04-20 17:02:08 -0700 | [diff] [blame] | 968 | |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 969 | mv = ValueOf(&m).Elem() |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 970 | mv.Set(Zero(mv.Type())) |
Russ Cox | 7295b61 | 2010-04-20 17:02:08 -0700 | [diff] [blame] | 971 | if m != nil { |
| 972 | t.Errorf("mv.Set(nil) failed") |
| 973 | } |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 974 | } |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 975 | |
| 976 | func TestChan(t *testing.T) { |
| 977 | for loop := 0; loop < 2; loop++ { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 978 | var c chan int |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 979 | var cv Value |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 980 | |
| 981 | // check both ways to allocate channels |
| 982 | switch loop { |
| 983 | case 1: |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 984 | c = make(chan int, 1) |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 985 | cv = ValueOf(c) |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 986 | case 0: |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 987 | cv = MakeChan(TypeOf(c), 1) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 988 | c = cv.Interface().(chan int) |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | // Send |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 992 | cv.Send(ValueOf(2)) |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 993 | if i := <-c; i != 2 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 994 | t.Errorf("reflect Send 2, native recv %d", i) |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | // Recv |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 998 | c <- 3 |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 999 | if i, ok := cv.Recv(); i.Int() != 3 || !ok { |
| 1000 | t.Errorf("native send 3, reflect Recv %d, %t", i.Int(), ok) |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | // TryRecv fail |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 1004 | val, ok := cv.TryRecv() |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 1005 | if val.IsValid() || ok { |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 1006 | t.Errorf("TryRecv on empty chan: %s, %t", valueToString(val), ok) |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | // TryRecv success |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1010 | c <- 4 |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 1011 | val, ok = cv.TryRecv() |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 1012 | if !val.IsValid() { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 1013 | t.Errorf("TryRecv on ready chan got nil") |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 1014 | } else if i := val.Int(); i != 4 || !ok { |
Russ Cox | 3f915f5 | 2011-03-11 14:47:44 -0500 | [diff] [blame] | 1015 | t.Errorf("native send 4, TryRecv %d, %t", i, ok) |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | // TrySend fail |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1019 | c <- 100 |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 1020 | ok = cv.TrySend(ValueOf(5)) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1021 | i := <-c |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 1022 | if ok { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 1023 | t.Errorf("TrySend on full chan succeeded: value %d", i) |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | // TrySend success |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 1027 | ok = cv.TrySend(ValueOf(6)) |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 1028 | if !ok { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 1029 | t.Errorf("TrySend on empty chan failed") |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 1030 | } else { |
| 1031 | if i = <-c; i != 6 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 1032 | t.Errorf("TrySend 6, recv %d", i) |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 1033 | } |
| 1034 | } |
Russ Cox | 653cef1 | 2009-08-26 10:47:18 -0700 | [diff] [blame] | 1035 | |
| 1036 | // Close |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1037 | c <- 123 |
| 1038 | cv.Close() |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 1039 | if i, ok := cv.Recv(); i.Int() != 123 || !ok { |
| 1040 | t.Errorf("send 123 then close; Recv %d, %t", i.Int(), ok) |
Russ Cox | 653cef1 | 2009-08-26 10:47:18 -0700 | [diff] [blame] | 1041 | } |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 1042 | if i, ok := cv.Recv(); i.Int() != 0 || ok { |
| 1043 | t.Errorf("after close Recv %d, %t", i.Int(), ok) |
Russ Cox | 653cef1 | 2009-08-26 10:47:18 -0700 | [diff] [blame] | 1044 | } |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | // check creation of unbuffered channel |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1048 | var c chan int |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 1049 | cv := MakeChan(TypeOf(c), 0) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1050 | c = cv.Interface().(chan int) |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 1051 | if cv.TrySend(ValueOf(7)) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 1052 | t.Errorf("TrySend on sync chan succeeded") |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 1053 | } |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 1054 | if v, ok := cv.TryRecv(); v.IsValid() || ok { |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 1055 | t.Errorf("TryRecv on sync chan succeeded: isvalid=%v ok=%v", v.IsValid(), ok) |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 1056 | } |
Russ Cox | de7920e | 2009-08-26 12:42:22 -0700 | [diff] [blame] | 1057 | |
| 1058 | // len/cap |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 1059 | cv = MakeChan(TypeOf(c), 10) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1060 | c = cv.Interface().(chan int) |
Russ Cox | de7920e | 2009-08-26 12:42:22 -0700 | [diff] [blame] | 1061 | for i := 0; i < 3; i++ { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 1062 | c <- i |
Russ Cox | de7920e | 2009-08-26 12:42:22 -0700 | [diff] [blame] | 1063 | } |
| 1064 | if l, m := cv.Len(), cv.Cap(); l != len(c) || m != cap(c) { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 1065 | t.Errorf("Len/Cap = %d/%d want %d/%d", l, m, len(c), cap(c)) |
Russ Cox | de7920e | 2009-08-26 12:42:22 -0700 | [diff] [blame] | 1066 | } |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Russ Cox | 370ae05 | 2012-09-18 14:22:41 -0400 | [diff] [blame] | 1069 | // caseInfo describes a single case in a select test. |
| 1070 | type caseInfo struct { |
| 1071 | desc string |
| 1072 | canSelect bool |
| 1073 | recv Value |
| 1074 | closed bool |
| 1075 | helper func() |
| 1076 | panic bool |
| 1077 | } |
| 1078 | |
Russ Cox | 46f379c | 2012-09-22 08:52:27 -0400 | [diff] [blame] | 1079 | var allselect = flag.Bool("allselect", false, "exhaustive select test") |
| 1080 | |
Russ Cox | 370ae05 | 2012-09-18 14:22:41 -0400 | [diff] [blame] | 1081 | func TestSelect(t *testing.T) { |
| 1082 | selectWatch.once.Do(func() { go selectWatcher() }) |
| 1083 | |
| 1084 | var x exhaustive |
| 1085 | nch := 0 |
| 1086 | newop := func(n int, cap int) (ch, val Value) { |
| 1087 | nch++ |
| 1088 | if nch%101%2 == 1 { |
| 1089 | c := make(chan int, cap) |
| 1090 | ch = ValueOf(c) |
| 1091 | val = ValueOf(n) |
| 1092 | } else { |
| 1093 | c := make(chan string, cap) |
| 1094 | ch = ValueOf(c) |
| 1095 | val = ValueOf(fmt.Sprint(n)) |
| 1096 | } |
| 1097 | return |
| 1098 | } |
| 1099 | |
| 1100 | for n := 0; x.Next(); n++ { |
| 1101 | if testing.Short() && n >= 1000 { |
| 1102 | break |
| 1103 | } |
Russ Cox | 46f379c | 2012-09-22 08:52:27 -0400 | [diff] [blame] | 1104 | if n >= 100000 && !*allselect { |
| 1105 | break |
| 1106 | } |
Russ Cox | 370ae05 | 2012-09-18 14:22:41 -0400 | [diff] [blame] | 1107 | if n%100000 == 0 && testing.Verbose() { |
| 1108 | println("TestSelect", n) |
| 1109 | } |
| 1110 | var cases []SelectCase |
| 1111 | var info []caseInfo |
| 1112 | |
| 1113 | // Ready send. |
| 1114 | if x.Maybe() { |
| 1115 | ch, val := newop(len(cases), 1) |
| 1116 | cases = append(cases, SelectCase{ |
| 1117 | Dir: SelectSend, |
| 1118 | Chan: ch, |
| 1119 | Send: val, |
| 1120 | }) |
| 1121 | info = append(info, caseInfo{desc: "ready send", canSelect: true}) |
| 1122 | } |
| 1123 | |
| 1124 | // Ready recv. |
| 1125 | if x.Maybe() { |
| 1126 | ch, val := newop(len(cases), 1) |
| 1127 | ch.Send(val) |
| 1128 | cases = append(cases, SelectCase{ |
| 1129 | Dir: SelectRecv, |
| 1130 | Chan: ch, |
| 1131 | }) |
| 1132 | info = append(info, caseInfo{desc: "ready recv", canSelect: true, recv: val}) |
| 1133 | } |
| 1134 | |
| 1135 | // Blocking send. |
| 1136 | if x.Maybe() { |
| 1137 | ch, val := newop(len(cases), 0) |
| 1138 | cases = append(cases, SelectCase{ |
| 1139 | Dir: SelectSend, |
| 1140 | Chan: ch, |
| 1141 | Send: val, |
| 1142 | }) |
| 1143 | // Let it execute? |
| 1144 | if x.Maybe() { |
| 1145 | f := func() { ch.Recv() } |
| 1146 | info = append(info, caseInfo{desc: "blocking send", helper: f}) |
| 1147 | } else { |
| 1148 | info = append(info, caseInfo{desc: "blocking send"}) |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | // Blocking recv. |
| 1153 | if x.Maybe() { |
| 1154 | ch, val := newop(len(cases), 0) |
| 1155 | cases = append(cases, SelectCase{ |
| 1156 | Dir: SelectRecv, |
| 1157 | Chan: ch, |
| 1158 | }) |
| 1159 | // Let it execute? |
| 1160 | if x.Maybe() { |
| 1161 | f := func() { ch.Send(val) } |
| 1162 | info = append(info, caseInfo{desc: "blocking recv", recv: val, helper: f}) |
| 1163 | } else { |
| 1164 | info = append(info, caseInfo{desc: "blocking recv"}) |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | // Zero Chan send. |
| 1169 | if x.Maybe() { |
| 1170 | // Maybe include value to send. |
| 1171 | var val Value |
| 1172 | if x.Maybe() { |
| 1173 | val = ValueOf(100) |
| 1174 | } |
| 1175 | cases = append(cases, SelectCase{ |
| 1176 | Dir: SelectSend, |
| 1177 | Send: val, |
| 1178 | }) |
| 1179 | info = append(info, caseInfo{desc: "zero Chan send"}) |
| 1180 | } |
| 1181 | |
| 1182 | // Zero Chan receive. |
| 1183 | if x.Maybe() { |
| 1184 | cases = append(cases, SelectCase{ |
| 1185 | Dir: SelectRecv, |
| 1186 | }) |
| 1187 | info = append(info, caseInfo{desc: "zero Chan recv"}) |
| 1188 | } |
| 1189 | |
| 1190 | // nil Chan send. |
| 1191 | if x.Maybe() { |
| 1192 | cases = append(cases, SelectCase{ |
| 1193 | Dir: SelectSend, |
| 1194 | Chan: ValueOf((chan int)(nil)), |
| 1195 | Send: ValueOf(101), |
| 1196 | }) |
| 1197 | info = append(info, caseInfo{desc: "nil Chan send"}) |
| 1198 | } |
| 1199 | |
| 1200 | // nil Chan recv. |
| 1201 | if x.Maybe() { |
| 1202 | cases = append(cases, SelectCase{ |
| 1203 | Dir: SelectRecv, |
| 1204 | Chan: ValueOf((chan int)(nil)), |
| 1205 | }) |
| 1206 | info = append(info, caseInfo{desc: "nil Chan recv"}) |
| 1207 | } |
| 1208 | |
| 1209 | // closed Chan send. |
| 1210 | if x.Maybe() { |
| 1211 | ch := make(chan int) |
| 1212 | close(ch) |
| 1213 | cases = append(cases, SelectCase{ |
| 1214 | Dir: SelectSend, |
| 1215 | Chan: ValueOf(ch), |
| 1216 | Send: ValueOf(101), |
| 1217 | }) |
| 1218 | info = append(info, caseInfo{desc: "closed Chan send", canSelect: true, panic: true}) |
| 1219 | } |
| 1220 | |
| 1221 | // closed Chan recv. |
| 1222 | if x.Maybe() { |
| 1223 | ch, val := newop(len(cases), 0) |
| 1224 | ch.Close() |
| 1225 | val = Zero(val.Type()) |
| 1226 | cases = append(cases, SelectCase{ |
| 1227 | Dir: SelectRecv, |
| 1228 | Chan: ch, |
| 1229 | }) |
| 1230 | info = append(info, caseInfo{desc: "closed Chan recv", canSelect: true, closed: true, recv: val}) |
| 1231 | } |
| 1232 | |
| 1233 | var helper func() // goroutine to help the select complete |
| 1234 | |
| 1235 | // Add default? Must be last case here, but will permute. |
| 1236 | // Add the default if the select would otherwise |
| 1237 | // block forever, and maybe add it anyway. |
| 1238 | numCanSelect := 0 |
| 1239 | canProceed := false |
| 1240 | canBlock := true |
| 1241 | canPanic := false |
| 1242 | helpers := []int{} |
| 1243 | for i, c := range info { |
| 1244 | if c.canSelect { |
| 1245 | canProceed = true |
| 1246 | canBlock = false |
| 1247 | numCanSelect++ |
| 1248 | if c.panic { |
| 1249 | canPanic = true |
| 1250 | } |
| 1251 | } else if c.helper != nil { |
| 1252 | canProceed = true |
| 1253 | helpers = append(helpers, i) |
| 1254 | } |
| 1255 | } |
| 1256 | if !canProceed || x.Maybe() { |
| 1257 | cases = append(cases, SelectCase{ |
| 1258 | Dir: SelectDefault, |
| 1259 | }) |
| 1260 | info = append(info, caseInfo{desc: "default", canSelect: canBlock}) |
| 1261 | numCanSelect++ |
| 1262 | } else if canBlock { |
| 1263 | // Select needs to communicate with another goroutine. |
| 1264 | cas := &info[helpers[x.Choose(len(helpers))]] |
| 1265 | helper = cas.helper |
| 1266 | cas.canSelect = true |
| 1267 | numCanSelect++ |
| 1268 | } |
| 1269 | |
| 1270 | // Permute cases and case info. |
| 1271 | // Doing too much here makes the exhaustive loop |
| 1272 | // too exhausting, so just do two swaps. |
| 1273 | for loop := 0; loop < 2; loop++ { |
| 1274 | i := x.Choose(len(cases)) |
| 1275 | j := x.Choose(len(cases)) |
| 1276 | cases[i], cases[j] = cases[j], cases[i] |
| 1277 | info[i], info[j] = info[j], info[i] |
| 1278 | } |
| 1279 | |
| 1280 | if helper != nil { |
| 1281 | // We wait before kicking off a goroutine to satisfy a blocked select. |
| 1282 | // The pause needs to be big enough to let the select block before |
| 1283 | // we run the helper, but if we lose that race once in a while it's okay: the |
| 1284 | // select will just proceed immediately. Not a big deal. |
| 1285 | // For short tests we can grow [sic] the timeout a bit without fear of taking too long |
| 1286 | pause := 10 * time.Microsecond |
| 1287 | if testing.Short() { |
| 1288 | pause = 100 * time.Microsecond |
| 1289 | } |
| 1290 | time.AfterFunc(pause, helper) |
| 1291 | } |
| 1292 | |
| 1293 | // Run select. |
| 1294 | i, recv, recvOK, panicErr := runSelect(cases, info) |
| 1295 | if panicErr != nil && !canPanic { |
| 1296 | t.Fatalf("%s\npanicked unexpectedly: %v", fmtSelect(info), panicErr) |
| 1297 | } |
| 1298 | if panicErr == nil && canPanic && numCanSelect == 1 { |
| 1299 | t.Fatalf("%s\nselected #%d incorrectly (should panic)", fmtSelect(info), i) |
| 1300 | } |
| 1301 | if panicErr != nil { |
| 1302 | continue |
| 1303 | } |
| 1304 | |
| 1305 | cas := info[i] |
| 1306 | if !cas.canSelect { |
| 1307 | recvStr := "" |
| 1308 | if recv.IsValid() { |
| 1309 | recvStr = fmt.Sprintf(", received %v, %v", recv.Interface(), recvOK) |
| 1310 | } |
| 1311 | t.Fatalf("%s\nselected #%d incorrectly%s", fmtSelect(info), i, recvStr) |
| 1312 | continue |
| 1313 | } |
| 1314 | if cas.panic { |
| 1315 | t.Fatalf("%s\nselected #%d incorrectly (case should panic)", fmtSelect(info), i) |
| 1316 | continue |
| 1317 | } |
| 1318 | |
| 1319 | if cases[i].Dir == SelectRecv { |
| 1320 | if !recv.IsValid() { |
| 1321 | t.Fatalf("%s\nselected #%d but got %v, %v, want %v, %v", fmtSelect(info), i, recv, recvOK, cas.recv.Interface(), !cas.closed) |
| 1322 | } |
| 1323 | if !cas.recv.IsValid() { |
| 1324 | t.Fatalf("%s\nselected #%d but internal error: missing recv value", fmtSelect(info), i) |
| 1325 | } |
| 1326 | if recv.Interface() != cas.recv.Interface() || recvOK != !cas.closed { |
| 1327 | if recv.Interface() == cas.recv.Interface() && recvOK == !cas.closed { |
| 1328 | t.Fatalf("%s\nselected #%d, got %#v, %v, and DeepEqual is broken on %T", fmtSelect(info), i, recv.Interface(), recvOK, recv.Interface()) |
| 1329 | } |
| 1330 | t.Fatalf("%s\nselected #%d but got %#v, %v, want %#v, %v", fmtSelect(info), i, recv.Interface(), recvOK, cas.recv.Interface(), !cas.closed) |
| 1331 | } |
| 1332 | } else { |
| 1333 | if recv.IsValid() || recvOK { |
| 1334 | t.Fatalf("%s\nselected #%d but got %v, %v, want %v, %v", fmtSelect(info), i, recv, recvOK, Value{}, false) |
| 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | // selectWatch and the selectWatcher are a watchdog mechanism for running Select. |
| 1341 | // If the selectWatcher notices that the select has been blocked for >1 second, it prints |
Robert Griesemer | 465b9c3 | 2012-10-30 13:38:01 -0700 | [diff] [blame] | 1342 | // an error describing the select and panics the entire test binary. |
Russ Cox | 370ae05 | 2012-09-18 14:22:41 -0400 | [diff] [blame] | 1343 | var selectWatch struct { |
| 1344 | sync.Mutex |
| 1345 | once sync.Once |
| 1346 | now time.Time |
| 1347 | info []caseInfo |
| 1348 | } |
| 1349 | |
| 1350 | func selectWatcher() { |
| 1351 | for { |
| 1352 | time.Sleep(1 * time.Second) |
| 1353 | selectWatch.Lock() |
| 1354 | if selectWatch.info != nil && time.Since(selectWatch.now) > 1*time.Second { |
| 1355 | fmt.Fprintf(os.Stderr, "TestSelect:\n%s blocked indefinitely\n", fmtSelect(selectWatch.info)) |
| 1356 | panic("select stuck") |
| 1357 | } |
| 1358 | selectWatch.Unlock() |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | // runSelect runs a single select test. |
| 1363 | // It returns the values returned by Select but also returns |
| 1364 | // a panic value if the Select panics. |
| 1365 | func runSelect(cases []SelectCase, info []caseInfo) (chosen int, recv Value, recvOK bool, panicErr interface{}) { |
| 1366 | defer func() { |
| 1367 | panicErr = recover() |
| 1368 | |
| 1369 | selectWatch.Lock() |
| 1370 | selectWatch.info = nil |
| 1371 | selectWatch.Unlock() |
| 1372 | }() |
| 1373 | |
| 1374 | selectWatch.Lock() |
| 1375 | selectWatch.now = time.Now() |
| 1376 | selectWatch.info = info |
| 1377 | selectWatch.Unlock() |
| 1378 | |
| 1379 | chosen, recv, recvOK = Select(cases) |
| 1380 | return |
| 1381 | } |
| 1382 | |
| 1383 | // fmtSelect formats the information about a single select test. |
| 1384 | func fmtSelect(info []caseInfo) string { |
| 1385 | var buf bytes.Buffer |
| 1386 | fmt.Fprintf(&buf, "\nselect {\n") |
| 1387 | for i, cas := range info { |
| 1388 | fmt.Fprintf(&buf, "%d: %s", i, cas.desc) |
| 1389 | if cas.recv.IsValid() { |
| 1390 | fmt.Fprintf(&buf, " val=%#v", cas.recv.Interface()) |
| 1391 | } |
| 1392 | if cas.canSelect { |
| 1393 | fmt.Fprintf(&buf, " canselect") |
| 1394 | } |
| 1395 | if cas.panic { |
| 1396 | fmt.Fprintf(&buf, " panic") |
| 1397 | } |
| 1398 | fmt.Fprintf(&buf, "\n") |
| 1399 | } |
| 1400 | fmt.Fprintf(&buf, "}") |
| 1401 | return buf.String() |
| 1402 | } |
| 1403 | |
Russ Cox | ba4625c | 2012-09-24 20:06:32 -0400 | [diff] [blame] | 1404 | type two [2]uintptr |
| 1405 | |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 1406 | // Difficult test for function call because of |
| 1407 | // implicit padding between arguments. |
Russ Cox | ba4625c | 2012-09-24 20:06:32 -0400 | [diff] [blame] | 1408 | func dummy(b byte, c int, d byte, e two, f byte, g float32, h byte) (i byte, j int, k byte, l two, m byte, n float32, o byte) { |
| 1409 | return b, c, d, e, f, g, h |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | func TestFunc(t *testing.T) { |
Russ Cox | ba4625c | 2012-09-24 20:06:32 -0400 | [diff] [blame] | 1413 | ret := ValueOf(dummy).Call([]Value{ |
| 1414 | ValueOf(byte(10)), |
| 1415 | ValueOf(20), |
| 1416 | ValueOf(byte(30)), |
| 1417 | ValueOf(two{40, 50}), |
| 1418 | ValueOf(byte(60)), |
| 1419 | ValueOf(float32(70)), |
| 1420 | ValueOf(byte(80)), |
| 1421 | }) |
| 1422 | if len(ret) != 7 { |
| 1423 | t.Fatalf("Call returned %d values, want 7", len(ret)) |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 1426 | i := byte(ret[0].Uint()) |
| 1427 | j := int(ret[1].Int()) |
| 1428 | k := byte(ret[2].Uint()) |
Russ Cox | ba4625c | 2012-09-24 20:06:32 -0400 | [diff] [blame] | 1429 | l := ret[3].Interface().(two) |
| 1430 | m := byte(ret[4].Uint()) |
| 1431 | n := float32(ret[5].Float()) |
| 1432 | o := byte(ret[6].Uint()) |
| 1433 | |
| 1434 | if i != 10 || j != 20 || k != 30 || l != (two{40, 50}) || m != 60 || n != 70 || o != 80 { |
| 1435 | t.Errorf("Call returned %d, %d, %d, %v, %d, %g, %d; want 10, 20, 30, [40, 50], 60, 70, 80", i, j, k, l, m, n, o) |
| 1436 | } |
| 1437 | } |
| 1438 | |
Michael Hudson-Doyle | 0a6ad46 | 2013-12-17 14:49:51 -0800 | [diff] [blame] | 1439 | type emptyStruct struct{} |
| 1440 | |
| 1441 | type nonEmptyStruct struct { |
| 1442 | member int |
| 1443 | } |
| 1444 | |
| 1445 | func returnEmpty() emptyStruct { |
| 1446 | return emptyStruct{} |
| 1447 | } |
| 1448 | |
| 1449 | func takesEmpty(e emptyStruct) { |
| 1450 | } |
| 1451 | |
| 1452 | func returnNonEmpty(i int) nonEmptyStruct { |
| 1453 | return nonEmptyStruct{member: i} |
| 1454 | } |
| 1455 | |
| 1456 | func takesNonEmpty(n nonEmptyStruct) int { |
| 1457 | return n.member |
| 1458 | } |
| 1459 | |
| 1460 | func TestCallWithStruct(t *testing.T) { |
| 1461 | r := ValueOf(returnEmpty).Call(nil) |
| 1462 | if len(r) != 1 || r[0].Type() != TypeOf(emptyStruct{}) { |
| 1463 | t.Errorf("returning empty struct returned %#v instead", r) |
| 1464 | } |
| 1465 | r = ValueOf(takesEmpty).Call([]Value{ValueOf(emptyStruct{})}) |
| 1466 | if len(r) != 0 { |
| 1467 | t.Errorf("takesEmpty returned values: %#v", r) |
| 1468 | } |
| 1469 | r = ValueOf(returnNonEmpty).Call([]Value{ValueOf(42)}) |
| 1470 | if len(r) != 1 || r[0].Type() != TypeOf(nonEmptyStruct{}) || r[0].Field(0).Int() != 42 { |
| 1471 | t.Errorf("returnNonEmpty returned %#v", r) |
| 1472 | } |
| 1473 | r = ValueOf(takesNonEmpty).Call([]Value{ValueOf(nonEmptyStruct{member: 42})}) |
| 1474 | if len(r) != 1 || r[0].Type() != TypeOf(1) || r[0].Int() != 42 { |
| 1475 | t.Errorf("takesNonEmpty returned %#v", r) |
| 1476 | } |
| 1477 | } |
| 1478 | |
Russ Cox | ba4625c | 2012-09-24 20:06:32 -0400 | [diff] [blame] | 1479 | func TestMakeFunc(t *testing.T) { |
| 1480 | f := dummy |
| 1481 | fv := MakeFunc(TypeOf(f), func(in []Value) []Value { return in }) |
| 1482 | ValueOf(&f).Elem().Set(fv) |
| 1483 | |
| 1484 | // Call g with small arguments so that there is |
| 1485 | // something predictable (and different from the |
| 1486 | // correct results) in those positions on the stack. |
| 1487 | g := dummy |
| 1488 | g(1, 2, 3, two{4, 5}, 6, 7, 8) |
| 1489 | |
| 1490 | // Call constructed function f. |
| 1491 | i, j, k, l, m, n, o := f(10, 20, 30, two{40, 50}, 60, 70, 80) |
| 1492 | if i != 10 || j != 20 || k != 30 || l != (two{40, 50}) || m != 60 || n != 70 || o != 80 { |
| 1493 | t.Errorf("Call returned %d, %d, %d, %v, %d, %g, %d; want 10, 20, 30, [40, 50], 60, 70, 80", i, j, k, l, m, n, o) |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 1494 | } |
| 1495 | } |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 1496 | |
Ian Lance Taylor | ef4e12a | 2013-10-04 13:12:50 -0700 | [diff] [blame] | 1497 | func TestMakeFuncInterface(t *testing.T) { |
| 1498 | fn := func(i int) int { return i } |
| 1499 | incr := func(in []Value) []Value { |
| 1500 | return []Value{ValueOf(int(in[0].Int() + 1))} |
| 1501 | } |
| 1502 | fv := MakeFunc(TypeOf(fn), incr) |
| 1503 | ValueOf(&fn).Elem().Set(fv) |
| 1504 | if r := fn(2); r != 3 { |
| 1505 | t.Errorf("Call returned %d, want 3", r) |
| 1506 | } |
| 1507 | if r := fv.Call([]Value{ValueOf(14)})[0].Int(); r != 15 { |
| 1508 | t.Errorf("Call returned %d, want 15", r) |
| 1509 | } |
| 1510 | if r := fv.Interface().(func(int) int)(26); r != 27 { |
| 1511 | t.Errorf("Call returned %d, want 27", r) |
| 1512 | } |
| 1513 | } |
| 1514 | |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 1515 | type Point struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1516 | x, y int |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
Rob Pike | 22484e2 | 2011-06-29 13:11:49 +1000 | [diff] [blame] | 1519 | // This will be index 0. |
| 1520 | func (p Point) AnotherMethod(scale int) int { |
| 1521 | return -1 |
| 1522 | } |
| 1523 | |
| 1524 | // This will be index 1. |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 1525 | func (p Point) Dist(scale int) int { |
Russ Cox | 3be7036 | 2013-03-21 16:59:16 -0400 | [diff] [blame] | 1526 | //println("Point.Dist", p.x, p.y, scale) |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 1527 | return p.x*p.x*scale + p.y*p.y*scale |
| 1528 | } |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 1529 | |
| 1530 | func TestMethod(t *testing.T) { |
| 1531 | // Non-curried method of type. |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1532 | p := Point{3, 4} |
Rob Pike | 22484e2 | 2011-06-29 13:11:49 +1000 | [diff] [blame] | 1533 | i := TypeOf(p).Method(1).Func.Call([]Value{ValueOf(p), ValueOf(10)})[0].Int() |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 1534 | if i != 250 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 1535 | t.Errorf("Type Method returned %d; want 250", i) |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
Rob Pike | 22484e2 | 2011-06-29 13:11:49 +1000 | [diff] [blame] | 1538 | m, ok := TypeOf(p).MethodByName("Dist") |
| 1539 | if !ok { |
| 1540 | t.Fatalf("method by name failed") |
| 1541 | } |
Russ Cox | 3be7036 | 2013-03-21 16:59:16 -0400 | [diff] [blame] | 1542 | i = m.Func.Call([]Value{ValueOf(p), ValueOf(11)})[0].Int() |
| 1543 | if i != 275 { |
| 1544 | t.Errorf("Type MethodByName returned %d; want 275", i) |
Rob Pike | 22484e2 | 2011-06-29 13:11:49 +1000 | [diff] [blame] | 1545 | } |
| 1546 | |
Russ Cox | 3be7036 | 2013-03-21 16:59:16 -0400 | [diff] [blame] | 1547 | i = TypeOf(&p).Method(1).Func.Call([]Value{ValueOf(&p), ValueOf(12)})[0].Int() |
| 1548 | if i != 300 { |
| 1549 | t.Errorf("Pointer Type Method returned %d; want 300", i) |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 1550 | } |
| 1551 | |
Rob Pike | 22484e2 | 2011-06-29 13:11:49 +1000 | [diff] [blame] | 1552 | m, ok = TypeOf(&p).MethodByName("Dist") |
| 1553 | if !ok { |
| 1554 | t.Fatalf("ptr method by name failed") |
| 1555 | } |
Russ Cox | 3be7036 | 2013-03-21 16:59:16 -0400 | [diff] [blame] | 1556 | i = m.Func.Call([]Value{ValueOf(&p), ValueOf(13)})[0].Int() |
| 1557 | if i != 325 { |
| 1558 | t.Errorf("Pointer Type MethodByName returned %d; want 325", i) |
Rob Pike | 22484e2 | 2011-06-29 13:11:49 +1000 | [diff] [blame] | 1559 | } |
| 1560 | |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 1561 | // Curried method of value. |
Robert Griesemer | 1065c6f | 2012-10-04 21:03:50 -0700 | [diff] [blame] | 1562 | tfunc := TypeOf((func(int) int)(nil)) |
Russ Cox | a479a45 | 2011-11-16 19:18:25 -0500 | [diff] [blame] | 1563 | v := ValueOf(p).Method(1) |
| 1564 | if tt := v.Type(); tt != tfunc { |
| 1565 | t.Errorf("Value Method Type is %s; want %s", tt, tfunc) |
| 1566 | } |
Russ Cox | 3be7036 | 2013-03-21 16:59:16 -0400 | [diff] [blame] | 1567 | i = v.Call([]Value{ValueOf(14)})[0].Int() |
| 1568 | if i != 350 { |
| 1569 | t.Errorf("Value Method returned %d; want 350", i) |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 1570 | } |
Russ Cox | a479a45 | 2011-11-16 19:18:25 -0500 | [diff] [blame] | 1571 | v = ValueOf(p).MethodByName("Dist") |
| 1572 | if tt := v.Type(); tt != tfunc { |
| 1573 | t.Errorf("Value MethodByName Type is %s; want %s", tt, tfunc) |
| 1574 | } |
Russ Cox | 3be7036 | 2013-03-21 16:59:16 -0400 | [diff] [blame] | 1575 | i = v.Call([]Value{ValueOf(15)})[0].Int() |
| 1576 | if i != 375 { |
| 1577 | t.Errorf("Value MethodByName returned %d; want 375", i) |
Rob Pike | 22484e2 | 2011-06-29 13:11:49 +1000 | [diff] [blame] | 1578 | } |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 1579 | |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 1580 | // Curried method of pointer. |
Russ Cox | a479a45 | 2011-11-16 19:18:25 -0500 | [diff] [blame] | 1581 | v = ValueOf(&p).Method(1) |
| 1582 | if tt := v.Type(); tt != tfunc { |
| 1583 | t.Errorf("Pointer Value Method Type is %s; want %s", tt, tfunc) |
| 1584 | } |
Russ Cox | 3be7036 | 2013-03-21 16:59:16 -0400 | [diff] [blame] | 1585 | i = v.Call([]Value{ValueOf(16)})[0].Int() |
| 1586 | if i != 400 { |
| 1587 | t.Errorf("Pointer Value Method returned %d; want 400", i) |
Rob Pike | 22484e2 | 2011-06-29 13:11:49 +1000 | [diff] [blame] | 1588 | } |
Russ Cox | a479a45 | 2011-11-16 19:18:25 -0500 | [diff] [blame] | 1589 | v = ValueOf(&p).MethodByName("Dist") |
| 1590 | if tt := v.Type(); tt != tfunc { |
| 1591 | t.Errorf("Pointer Value MethodByName Type is %s; want %s", tt, tfunc) |
| 1592 | } |
Russ Cox | 3be7036 | 2013-03-21 16:59:16 -0400 | [diff] [blame] | 1593 | i = v.Call([]Value{ValueOf(17)})[0].Int() |
| 1594 | if i != 425 { |
| 1595 | t.Errorf("Pointer Value MethodByName returned %d; want 425", i) |
| 1596 | } |
| 1597 | |
| 1598 | // Curried method of interface value. |
| 1599 | // Have to wrap interface value in a struct to get at it. |
| 1600 | // Passing it to ValueOf directly would |
| 1601 | // access the underlying Point, not the interface. |
| 1602 | var x interface { |
| 1603 | Dist(int) int |
| 1604 | } = p |
| 1605 | pv := ValueOf(&x).Elem() |
| 1606 | v = pv.Method(0) |
| 1607 | if tt := v.Type(); tt != tfunc { |
| 1608 | t.Errorf("Interface Method Type is %s; want %s", tt, tfunc) |
| 1609 | } |
| 1610 | i = v.Call([]Value{ValueOf(18)})[0].Int() |
| 1611 | if i != 450 { |
| 1612 | t.Errorf("Interface Method returned %d; want 450", i) |
| 1613 | } |
| 1614 | v = pv.MethodByName("Dist") |
| 1615 | if tt := v.Type(); tt != tfunc { |
| 1616 | t.Errorf("Interface MethodByName Type is %s; want %s", tt, tfunc) |
| 1617 | } |
| 1618 | i = v.Call([]Value{ValueOf(19)})[0].Int() |
| 1619 | if i != 475 { |
| 1620 | t.Errorf("Interface MethodByName returned %d; want 475", i) |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | func TestMethodValue(t *testing.T) { |
| 1625 | p := Point{3, 4} |
| 1626 | var i int64 |
| 1627 | |
| 1628 | // Curried method of value. |
| 1629 | tfunc := TypeOf((func(int) int)(nil)) |
| 1630 | v := ValueOf(p).Method(1) |
| 1631 | if tt := v.Type(); tt != tfunc { |
| 1632 | t.Errorf("Value Method Type is %s; want %s", tt, tfunc) |
| 1633 | } |
| 1634 | i = ValueOf(v.Interface()).Call([]Value{ValueOf(10)})[0].Int() |
Rob Pike | 22484e2 | 2011-06-29 13:11:49 +1000 | [diff] [blame] | 1635 | if i != 250 { |
Russ Cox | 3be7036 | 2013-03-21 16:59:16 -0400 | [diff] [blame] | 1636 | t.Errorf("Value Method returned %d; want 250", i) |
| 1637 | } |
| 1638 | v = ValueOf(p).MethodByName("Dist") |
| 1639 | if tt := v.Type(); tt != tfunc { |
| 1640 | t.Errorf("Value MethodByName Type is %s; want %s", tt, tfunc) |
| 1641 | } |
| 1642 | i = ValueOf(v.Interface()).Call([]Value{ValueOf(11)})[0].Int() |
| 1643 | if i != 275 { |
| 1644 | t.Errorf("Value MethodByName returned %d; want 275", i) |
| 1645 | } |
| 1646 | |
| 1647 | // Curried method of pointer. |
| 1648 | v = ValueOf(&p).Method(1) |
| 1649 | if tt := v.Type(); tt != tfunc { |
| 1650 | t.Errorf("Pointer Value Method Type is %s; want %s", tt, tfunc) |
| 1651 | } |
| 1652 | i = ValueOf(v.Interface()).Call([]Value{ValueOf(12)})[0].Int() |
| 1653 | if i != 300 { |
| 1654 | t.Errorf("Pointer Value Method returned %d; want 300", i) |
| 1655 | } |
| 1656 | v = ValueOf(&p).MethodByName("Dist") |
| 1657 | if tt := v.Type(); tt != tfunc { |
| 1658 | t.Errorf("Pointer Value MethodByName Type is %s; want %s", tt, tfunc) |
| 1659 | } |
| 1660 | i = ValueOf(v.Interface()).Call([]Value{ValueOf(13)})[0].Int() |
| 1661 | if i != 325 { |
| 1662 | t.Errorf("Pointer Value MethodByName returned %d; want 325", i) |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 1663 | } |
| 1664 | |
Ian Lance Taylor | c757020 | 2013-09-17 15:22:42 -0700 | [diff] [blame] | 1665 | // Curried method of pointer to pointer. |
| 1666 | pp := &p |
| 1667 | v = ValueOf(&pp).Elem().Method(1) |
| 1668 | if tt := v.Type(); tt != tfunc { |
| 1669 | t.Errorf("Pointer Pointer Value Method Type is %s; want %s", tt, tfunc) |
| 1670 | } |
| 1671 | i = ValueOf(v.Interface()).Call([]Value{ValueOf(14)})[0].Int() |
| 1672 | if i != 350 { |
| 1673 | t.Errorf("Pointer Pointer Value Method returned %d; want 350", i) |
| 1674 | } |
| 1675 | v = ValueOf(&pp).Elem().MethodByName("Dist") |
| 1676 | if tt := v.Type(); tt != tfunc { |
| 1677 | t.Errorf("Pointer Pointer Value MethodByName Type is %s; want %s", tt, tfunc) |
| 1678 | } |
| 1679 | i = ValueOf(v.Interface()).Call([]Value{ValueOf(15)})[0].Int() |
| 1680 | if i != 375 { |
| 1681 | t.Errorf("Pointer Pointer Value MethodByName returned %d; want 375", i) |
| 1682 | } |
| 1683 | |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 1684 | // Curried method of interface value. |
| 1685 | // Have to wrap interface value in a struct to get at it. |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 1686 | // Passing it to ValueOf directly would |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 1687 | // access the underlying Point, not the interface. |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 1688 | var s = struct { |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 1689 | X interface { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1690 | Dist(int) int |
| 1691 | } |
| 1692 | }{p} |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 1693 | pv := ValueOf(s).Field(0) |
Russ Cox | a479a45 | 2011-11-16 19:18:25 -0500 | [diff] [blame] | 1694 | v = pv.Method(0) |
| 1695 | if tt := v.Type(); tt != tfunc { |
| 1696 | t.Errorf("Interface Method Type is %s; want %s", tt, tfunc) |
| 1697 | } |
Ian Lance Taylor | c757020 | 2013-09-17 15:22:42 -0700 | [diff] [blame] | 1698 | i = ValueOf(v.Interface()).Call([]Value{ValueOf(16)})[0].Int() |
| 1699 | if i != 400 { |
| 1700 | t.Errorf("Interface Method returned %d; want 400", i) |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 1701 | } |
Russ Cox | a479a45 | 2011-11-16 19:18:25 -0500 | [diff] [blame] | 1702 | v = pv.MethodByName("Dist") |
| 1703 | if tt := v.Type(); tt != tfunc { |
| 1704 | t.Errorf("Interface MethodByName Type is %s; want %s", tt, tfunc) |
| 1705 | } |
Ian Lance Taylor | c757020 | 2013-09-17 15:22:42 -0700 | [diff] [blame] | 1706 | i = ValueOf(v.Interface()).Call([]Value{ValueOf(17)})[0].Int() |
| 1707 | if i != 425 { |
| 1708 | t.Errorf("Interface MethodByName returned %d; want 425", i) |
Rob Pike | 22484e2 | 2011-06-29 13:11:49 +1000 | [diff] [blame] | 1709 | } |
Russ Cox | 12ebbe7 | 2009-07-09 17:27:49 -0700 | [diff] [blame] | 1710 | } |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 1711 | |
Russ Cox | 3be7036 | 2013-03-21 16:59:16 -0400 | [diff] [blame] | 1712 | // Reflect version of $GOROOT/test/method5.go |
| 1713 | |
| 1714 | // Concrete types implementing M method. |
| 1715 | // Smaller than a word, word-sized, larger than a word. |
| 1716 | // Value and pointer receivers. |
| 1717 | |
| 1718 | type Tinter interface { |
| 1719 | M(int, byte) (byte, int) |
| 1720 | } |
| 1721 | |
| 1722 | type Tsmallv byte |
| 1723 | |
| 1724 | func (v Tsmallv) M(x int, b byte) (byte, int) { return b, x + int(v) } |
| 1725 | |
| 1726 | type Tsmallp byte |
| 1727 | |
| 1728 | func (p *Tsmallp) M(x int, b byte) (byte, int) { return b, x + int(*p) } |
| 1729 | |
| 1730 | type Twordv uintptr |
| 1731 | |
| 1732 | func (v Twordv) M(x int, b byte) (byte, int) { return b, x + int(v) } |
| 1733 | |
| 1734 | type Twordp uintptr |
| 1735 | |
| 1736 | func (p *Twordp) M(x int, b byte) (byte, int) { return b, x + int(*p) } |
| 1737 | |
| 1738 | type Tbigv [2]uintptr |
| 1739 | |
| 1740 | func (v Tbigv) M(x int, b byte) (byte, int) { return b, x + int(v[0]) + int(v[1]) } |
| 1741 | |
| 1742 | type Tbigp [2]uintptr |
| 1743 | |
| 1744 | func (p *Tbigp) M(x int, b byte) (byte, int) { return b, x + int(p[0]) + int(p[1]) } |
| 1745 | |
| 1746 | // Again, with an unexported method. |
| 1747 | |
| 1748 | type tsmallv byte |
| 1749 | |
| 1750 | func (v tsmallv) m(x int, b byte) (byte, int) { return b, x + int(v) } |
| 1751 | |
| 1752 | type tsmallp byte |
| 1753 | |
| 1754 | func (p *tsmallp) m(x int, b byte) (byte, int) { return b, x + int(*p) } |
| 1755 | |
| 1756 | type twordv uintptr |
| 1757 | |
| 1758 | func (v twordv) m(x int, b byte) (byte, int) { return b, x + int(v) } |
| 1759 | |
| 1760 | type twordp uintptr |
| 1761 | |
| 1762 | func (p *twordp) m(x int, b byte) (byte, int) { return b, x + int(*p) } |
| 1763 | |
| 1764 | type tbigv [2]uintptr |
| 1765 | |
| 1766 | func (v tbigv) m(x int, b byte) (byte, int) { return b, x + int(v[0]) + int(v[1]) } |
| 1767 | |
| 1768 | type tbigp [2]uintptr |
| 1769 | |
| 1770 | func (p *tbigp) m(x int, b byte) (byte, int) { return b, x + int(p[0]) + int(p[1]) } |
| 1771 | |
| 1772 | type tinter interface { |
| 1773 | m(int, byte) (byte, int) |
| 1774 | } |
| 1775 | |
| 1776 | // Embedding via pointer. |
| 1777 | |
| 1778 | type Tm1 struct { |
| 1779 | Tm2 |
| 1780 | } |
| 1781 | |
| 1782 | type Tm2 struct { |
| 1783 | *Tm3 |
| 1784 | } |
| 1785 | |
| 1786 | type Tm3 struct { |
| 1787 | *Tm4 |
| 1788 | } |
| 1789 | |
| 1790 | type Tm4 struct { |
| 1791 | } |
| 1792 | |
| 1793 | func (t4 Tm4) M(x int, b byte) (byte, int) { return b, x + 40 } |
| 1794 | |
| 1795 | func TestMethod5(t *testing.T) { |
| 1796 | CheckF := func(name string, f func(int, byte) (byte, int), inc int) { |
| 1797 | b, x := f(1000, 99) |
| 1798 | if b != 99 || x != 1000+inc { |
| 1799 | t.Errorf("%s(1000, 99) = %v, %v, want 99, %v", name, b, x, 1000+inc) |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | CheckV := func(name string, i Value, inc int) { |
| 1804 | bx := i.Method(0).Call([]Value{ValueOf(1000), ValueOf(byte(99))}) |
| 1805 | b := bx[0].Interface() |
| 1806 | x := bx[1].Interface() |
| 1807 | if b != byte(99) || x != 1000+inc { |
| 1808 | t.Errorf("direct %s.M(1000, 99) = %v, %v, want 99, %v", name, b, x, 1000+inc) |
| 1809 | } |
| 1810 | |
| 1811 | CheckF(name+".M", i.Method(0).Interface().(func(int, byte) (byte, int)), inc) |
| 1812 | } |
| 1813 | |
| 1814 | var TinterType = TypeOf(new(Tinter)).Elem() |
| 1815 | var tinterType = TypeOf(new(tinter)).Elem() |
| 1816 | |
| 1817 | CheckI := func(name string, i interface{}, inc int) { |
| 1818 | v := ValueOf(i) |
| 1819 | CheckV(name, v, inc) |
| 1820 | CheckV("(i="+name+")", v.Convert(TinterType), inc) |
| 1821 | } |
| 1822 | |
| 1823 | sv := Tsmallv(1) |
| 1824 | CheckI("sv", sv, 1) |
| 1825 | CheckI("&sv", &sv, 1) |
| 1826 | |
| 1827 | sp := Tsmallp(2) |
| 1828 | CheckI("&sp", &sp, 2) |
| 1829 | |
| 1830 | wv := Twordv(3) |
| 1831 | CheckI("wv", wv, 3) |
| 1832 | CheckI("&wv", &wv, 3) |
| 1833 | |
| 1834 | wp := Twordp(4) |
| 1835 | CheckI("&wp", &wp, 4) |
| 1836 | |
| 1837 | bv := Tbigv([2]uintptr{5, 6}) |
| 1838 | CheckI("bv", bv, 11) |
| 1839 | CheckI("&bv", &bv, 11) |
| 1840 | |
| 1841 | bp := Tbigp([2]uintptr{7, 8}) |
| 1842 | CheckI("&bp", &bp, 15) |
| 1843 | |
| 1844 | t4 := Tm4{} |
| 1845 | t3 := Tm3{&t4} |
| 1846 | t2 := Tm2{&t3} |
| 1847 | t1 := Tm1{t2} |
| 1848 | CheckI("t4", t4, 40) |
| 1849 | CheckI("&t4", &t4, 40) |
| 1850 | CheckI("t3", t3, 40) |
| 1851 | CheckI("&t3", &t3, 40) |
| 1852 | CheckI("t2", t2, 40) |
| 1853 | CheckI("&t2", &t2, 40) |
| 1854 | CheckI("t1", t1, 40) |
| 1855 | CheckI("&t1", &t1, 40) |
| 1856 | |
| 1857 | methodShouldPanic := func(name string, i interface{}) { |
| 1858 | v := ValueOf(i) |
| 1859 | m := v.Method(0) |
| 1860 | shouldPanic(func() { m.Call([]Value{ValueOf(1000), ValueOf(byte(99))}) }) |
| 1861 | shouldPanic(func() { m.Interface() }) |
| 1862 | |
| 1863 | v = v.Convert(tinterType) |
| 1864 | m = v.Method(0) |
| 1865 | shouldPanic(func() { m.Call([]Value{ValueOf(1000), ValueOf(byte(99))}) }) |
| 1866 | shouldPanic(func() { m.Interface() }) |
| 1867 | } |
| 1868 | |
| 1869 | _sv := tsmallv(1) |
| 1870 | methodShouldPanic("_sv", _sv) |
| 1871 | methodShouldPanic("&_sv", &_sv) |
| 1872 | |
| 1873 | _sp := tsmallp(2) |
| 1874 | methodShouldPanic("&_sp", &_sp) |
| 1875 | |
| 1876 | _wv := twordv(3) |
| 1877 | methodShouldPanic("_wv", _wv) |
| 1878 | methodShouldPanic("&_wv", &_wv) |
| 1879 | |
| 1880 | _wp := twordp(4) |
| 1881 | methodShouldPanic("&_wp", &_wp) |
| 1882 | |
| 1883 | _bv := tbigv([2]uintptr{5, 6}) |
| 1884 | methodShouldPanic("_bv", _bv) |
| 1885 | methodShouldPanic("&_bv", &_bv) |
| 1886 | |
| 1887 | _bp := tbigp([2]uintptr{7, 8}) |
| 1888 | methodShouldPanic("&_bp", &_bp) |
| 1889 | |
| 1890 | var tnil Tinter |
| 1891 | vnil := ValueOf(&tnil).Elem() |
| 1892 | shouldPanic(func() { vnil.Method(0) }) |
| 1893 | } |
| 1894 | |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 1895 | func TestInterfaceSet(t *testing.T) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1896 | p := &Point{3, 4} |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 1897 | |
| 1898 | var s struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1899 | I interface{} |
| 1900 | P interface { |
| 1901 | Dist(int) int |
| 1902 | } |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 1903 | } |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 1904 | sv := ValueOf(&s).Elem() |
| 1905 | sv.Field(0).Set(ValueOf(p)) |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 1906 | if q := s.I.(*Point); q != p { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 1907 | t.Errorf("i: have %p want %p", q, p) |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 1908 | } |
| 1909 | |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 1910 | pv := sv.Field(1) |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 1911 | pv.Set(ValueOf(p)) |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 1912 | if q := s.P.(*Point); q != p { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 1913 | t.Errorf("i: have %p want %p", q, p) |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 1914 | } |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 1915 | |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 1916 | i := pv.Method(0).Call([]Value{ValueOf(10)})[0].Int() |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 1917 | if i != 250 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 1918 | t.Errorf("Interface Method returned %d; want 250", i) |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 1919 | } |
| 1920 | } |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 1921 | |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 1922 | type T1 struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1923 | a string |
| 1924 | int |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 1925 | } |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 1926 | |
| 1927 | func TestAnonymousFields(t *testing.T) { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1928 | var field StructField |
| 1929 | var ok bool |
| 1930 | var t1 T1 |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 1931 | type1 := TypeOf(t1) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 1932 | if field, ok = type1.FieldByName("int"); !ok { |
Russ Cox | 5e3224c | 2012-09-05 09:35:53 -0400 | [diff] [blame] | 1933 | t.Fatal("no field 'int'") |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 1934 | } |
| 1935 | if field.Index[0] != 1 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 1936 | t.Error("field index should be 1; is", field.Index) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | type FTest struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1941 | s interface{} |
| 1942 | name string |
| 1943 | index []int |
| 1944 | value int |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 1945 | } |
| 1946 | |
Russ Cox | 92543da | 2009-08-24 17:04:12 -0700 | [diff] [blame] | 1947 | type D1 struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1948 | d int |
Russ Cox | 92543da | 2009-08-24 17:04:12 -0700 | [diff] [blame] | 1949 | } |
| 1950 | type D2 struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1951 | d int |
Russ Cox | 92543da | 2009-08-24 17:04:12 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 1954 | type S0 struct { |
Russ Cox | 304cf4d | 2011-10-17 18:48:45 -0400 | [diff] [blame] | 1955 | A, B, C int |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1956 | D1 |
| 1957 | D2 |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | type S1 struct { |
Russ Cox | 304cf4d | 2011-10-17 18:48:45 -0400 | [diff] [blame] | 1961 | B int |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1962 | S0 |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | type S2 struct { |
Russ Cox | 304cf4d | 2011-10-17 18:48:45 -0400 | [diff] [blame] | 1966 | A int |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1967 | *S1 |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 1968 | } |
| 1969 | |
Russ Cox | 92543da | 2009-08-24 17:04:12 -0700 | [diff] [blame] | 1970 | type S1x struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1971 | S1 |
Russ Cox | 92543da | 2009-08-24 17:04:12 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | type S1y struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1975 | S1 |
Russ Cox | 92543da | 2009-08-24 17:04:12 -0700 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | type S3 struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1979 | S1x |
| 1980 | S2 |
Russ Cox | 304cf4d | 2011-10-17 18:48:45 -0400 | [diff] [blame] | 1981 | D, E int |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1982 | *S1y |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | type S4 struct { |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 1986 | *S4 |
Russ Cox | 304cf4d | 2011-10-17 18:48:45 -0400 | [diff] [blame] | 1987 | A int |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 1988 | } |
| 1989 | |
Russ Cox | 5e3224c | 2012-09-05 09:35:53 -0400 | [diff] [blame] | 1990 | // The X in S6 and S7 annihilate, but they also block the X in S8.S9. |
| 1991 | type S5 struct { |
| 1992 | S6 |
| 1993 | S7 |
| 1994 | S8 |
| 1995 | } |
| 1996 | |
| 1997 | type S6 struct { |
| 1998 | X int |
| 1999 | } |
| 2000 | |
| 2001 | type S7 S6 |
| 2002 | |
| 2003 | type S8 struct { |
| 2004 | S9 |
| 2005 | } |
| 2006 | |
| 2007 | type S9 struct { |
| 2008 | X int |
| 2009 | Y int |
| 2010 | } |
| 2011 | |
| 2012 | // The X in S11.S6 and S12.S6 annihilate, but they also block the X in S13.S8.S9. |
| 2013 | type S10 struct { |
| 2014 | S11 |
| 2015 | S12 |
| 2016 | S13 |
| 2017 | } |
| 2018 | |
| 2019 | type S11 struct { |
| 2020 | S6 |
| 2021 | } |
| 2022 | |
| 2023 | type S12 struct { |
| 2024 | S6 |
| 2025 | } |
| 2026 | |
| 2027 | type S13 struct { |
| 2028 | S8 |
| 2029 | } |
| 2030 | |
Robert Griesemer | aa38801 | 2012-11-13 10:45:30 -0800 | [diff] [blame] | 2031 | // The X in S15.S11.S1 and S16.S11.S1 annihilate. |
| 2032 | type S14 struct { |
| 2033 | S15 |
| 2034 | S16 |
| 2035 | } |
| 2036 | |
| 2037 | type S15 struct { |
| 2038 | S11 |
| 2039 | } |
| 2040 | |
| 2041 | type S16 struct { |
| 2042 | S11 |
| 2043 | } |
| 2044 | |
Robert Griesemer | 77334b98 | 2009-11-05 14:23:20 -0800 | [diff] [blame] | 2045 | var fieldTests = []FTest{ |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 2046 | {struct{}{}, "", nil, 0}, |
Russ Cox | 304cf4d | 2011-10-17 18:48:45 -0400 | [diff] [blame] | 2047 | {struct{}{}, "Foo", nil, 0}, |
| 2048 | {S0{A: 'a'}, "A", []int{0}, 'a'}, |
| 2049 | {S0{}, "D", nil, 0}, |
| 2050 | {S1{S0: S0{A: 'a'}}, "A", []int{1, 0}, 'a'}, |
| 2051 | {S1{B: 'b'}, "B", []int{0}, 'b'}, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 2052 | {S1{}, "S0", []int{1}, 0}, |
Russ Cox | 304cf4d | 2011-10-17 18:48:45 -0400 | [diff] [blame] | 2053 | {S1{S0: S0{C: 'c'}}, "C", []int{1, 2}, 'c'}, |
| 2054 | {S2{A: 'a'}, "A", []int{0}, 'a'}, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 2055 | {S2{}, "S1", []int{1}, 0}, |
Russ Cox | 304cf4d | 2011-10-17 18:48:45 -0400 | [diff] [blame] | 2056 | {S2{S1: &S1{B: 'b'}}, "B", []int{1, 0}, 'b'}, |
| 2057 | {S2{S1: &S1{S0: S0{C: 'c'}}}, "C", []int{1, 1, 2}, 'c'}, |
| 2058 | {S2{}, "D", nil, 0}, |
Robert Griesemer | 3478891 | 2010-10-22 10:06:33 -0700 | [diff] [blame] | 2059 | {S3{}, "S1", nil, 0}, |
Russ Cox | 304cf4d | 2011-10-17 18:48:45 -0400 | [diff] [blame] | 2060 | {S3{S2: S2{A: 'a'}}, "A", []int{1, 0}, 'a'}, |
| 2061 | {S3{}, "B", nil, 0}, |
| 2062 | {S3{D: 'd'}, "D", []int{2}, 0}, |
| 2063 | {S3{E: 'e'}, "E", []int{3}, 'e'}, |
| 2064 | {S4{A: 'a'}, "A", []int{1}, 'a'}, |
| 2065 | {S4{}, "B", nil, 0}, |
Russ Cox | 5e3224c | 2012-09-05 09:35:53 -0400 | [diff] [blame] | 2066 | {S5{}, "X", nil, 0}, |
| 2067 | {S5{}, "Y", []int{2, 0, 1}, 0}, |
| 2068 | {S10{}, "X", nil, 0}, |
| 2069 | {S10{}, "Y", []int{2, 0, 0, 1}, 0}, |
Robert Griesemer | aa38801 | 2012-11-13 10:45:30 -0800 | [diff] [blame] | 2070 | {S14{}, "X", nil, 0}, |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | func TestFieldByIndex(t *testing.T) { |
| 2074 | for _, test := range fieldTests { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2075 | s := TypeOf(test.s) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 2076 | f := s.FieldByIndex(test.index) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2077 | if f.Name != "" { |
| 2078 | if test.index != nil { |
| 2079 | if f.Name != test.name { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 2080 | t.Errorf("%s.%s found; want %s", s.Name(), f.Name, test.name) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2081 | } |
| 2082 | } else { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 2083 | t.Errorf("%s.%s found", s.Name(), f.Name) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2084 | } |
| 2085 | } else if len(test.index) > 0 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 2086 | t.Errorf("%s.%s not found", s.Name(), test.name) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | if test.value != 0 { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2090 | v := ValueOf(test.s).FieldByIndex(test.index) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 2091 | if v.IsValid() { |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2092 | if x, ok := v.Interface().(int); ok { |
| 2093 | if x != test.value { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 2094 | t.Errorf("%s%v is %d; want %d", s.Name(), test.index, x, test.value) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2095 | } |
| 2096 | } else { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 2097 | t.Errorf("%s%v value not an int", s.Name(), test.index) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2098 | } |
| 2099 | } else { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 2100 | t.Errorf("%s%v value not found", s.Name(), test.index) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2101 | } |
| 2102 | } |
| 2103 | } |
| 2104 | } |
| 2105 | |
| 2106 | func TestFieldByName(t *testing.T) { |
| 2107 | for _, test := range fieldTests { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2108 | s := TypeOf(test.s) |
Robert Griesemer | d65a5cc | 2009-12-15 15:40:16 -0800 | [diff] [blame] | 2109 | f, found := s.FieldByName(test.name) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2110 | if found { |
| 2111 | if test.index != nil { |
| 2112 | // Verify field depth and index. |
| 2113 | if len(f.Index) != len(test.index) { |
Russ Cox | 5e3224c | 2012-09-05 09:35:53 -0400 | [diff] [blame] | 2114 | t.Errorf("%s.%s depth %d; want %d: %v vs %v", s.Name(), test.name, len(f.Index), len(test.index), f.Index, test.index) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2115 | } else { |
| 2116 | for i, x := range f.Index { |
| 2117 | if x != test.index[i] { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 2118 | t.Errorf("%s.%s.Index[%d] is %d; want %d", s.Name(), test.name, i, x, test.index[i]) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2119 | } |
| 2120 | } |
| 2121 | } |
| 2122 | } else { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 2123 | t.Errorf("%s.%s found", s.Name(), f.Name) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2124 | } |
| 2125 | } else if len(test.index) > 0 { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 2126 | t.Errorf("%s.%s not found", s.Name(), test.name) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2127 | } |
Russ Cox | 3b864e4 | 2009-08-12 13:18:37 -0700 | [diff] [blame] | 2128 | |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2129 | if test.value != 0 { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2130 | v := ValueOf(test.s).FieldByName(test.name) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 2131 | if v.IsValid() { |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2132 | if x, ok := v.Interface().(int); ok { |
| 2133 | if x != test.value { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 2134 | t.Errorf("%s.%s is %d; want %d", s.Name(), test.name, x, test.value) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2135 | } |
| 2136 | } else { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 2137 | t.Errorf("%s.%s value not an int", s.Name(), test.name) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2138 | } |
| 2139 | } else { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 2140 | t.Errorf("%s.%s value not found", s.Name(), test.name) |
Robert Griesemer | a288095 | 2009-08-05 15:56:44 -0700 | [diff] [blame] | 2141 | } |
| 2142 | } |
| 2143 | } |
| 2144 | } |
Russ Cox | 1cecac8 | 2010-01-24 23:33:59 -0800 | [diff] [blame] | 2145 | |
| 2146 | func TestImportPath(t *testing.T) { |
David Symonds | ee09a8c | 2012-01-20 09:26:17 +1100 | [diff] [blame] | 2147 | tests := []struct { |
| 2148 | t Type |
| 2149 | path string |
| 2150 | }{ |
| 2151 | {TypeOf(&base64.Encoding{}).Elem(), "encoding/base64"}, |
Russ Cox | a96c2b8 | 2012-09-01 19:55:55 -0400 | [diff] [blame] | 2152 | {TypeOf(int(0)), ""}, |
| 2153 | {TypeOf(int8(0)), ""}, |
| 2154 | {TypeOf(int16(0)), ""}, |
| 2155 | {TypeOf(int32(0)), ""}, |
| 2156 | {TypeOf(int64(0)), ""}, |
David Symonds | ee09a8c | 2012-01-20 09:26:17 +1100 | [diff] [blame] | 2157 | {TypeOf(uint(0)), ""}, |
Russ Cox | a96c2b8 | 2012-09-01 19:55:55 -0400 | [diff] [blame] | 2158 | {TypeOf(uint8(0)), ""}, |
| 2159 | {TypeOf(uint16(0)), ""}, |
| 2160 | {TypeOf(uint32(0)), ""}, |
| 2161 | {TypeOf(uint64(0)), ""}, |
| 2162 | {TypeOf(uintptr(0)), ""}, |
| 2163 | {TypeOf(float32(0)), ""}, |
| 2164 | {TypeOf(float64(0)), ""}, |
| 2165 | {TypeOf(complex64(0)), ""}, |
| 2166 | {TypeOf(complex128(0)), ""}, |
| 2167 | {TypeOf(byte(0)), ""}, |
| 2168 | {TypeOf(rune(0)), ""}, |
| 2169 | {TypeOf([]byte(nil)), ""}, |
| 2170 | {TypeOf([]rune(nil)), ""}, |
| 2171 | {TypeOf(string("")), ""}, |
| 2172 | {TypeOf((*interface{})(nil)).Elem(), ""}, |
| 2173 | {TypeOf((*byte)(nil)), ""}, |
| 2174 | {TypeOf((*rune)(nil)), ""}, |
| 2175 | {TypeOf((*int64)(nil)), ""}, |
David Symonds | ee09a8c | 2012-01-20 09:26:17 +1100 | [diff] [blame] | 2176 | {TypeOf(map[string]int{}), ""}, |
| 2177 | {TypeOf((*error)(nil)).Elem(), ""}, |
| 2178 | } |
| 2179 | for _, test := range tests { |
| 2180 | if path := test.t.PkgPath(); path != test.path { |
| 2181 | t.Errorf("%v.PkgPath() = %q, want %q", test.t, path, test.path) |
| 2182 | } |
Russ Cox | 1cecac8 | 2010-01-24 23:33:59 -0800 | [diff] [blame] | 2183 | } |
| 2184 | } |
Russ Cox | 6672b40 | 2010-06-14 11:23:11 -0700 | [diff] [blame] | 2185 | |
David Symonds | c913cb8 | 2011-07-27 13:44:57 +1000 | [diff] [blame] | 2186 | func TestVariadicType(t *testing.T) { |
David Symonds | fc1cf58 | 2011-07-27 13:29:44 +1000 | [diff] [blame] | 2187 | // Test example from Type documentation. |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 2188 | var f func(x int, y ...float64) |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2189 | typ := TypeOf(f) |
| 2190 | if typ.NumIn() == 2 && typ.In(0) == TypeOf(int(0)) { |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 2191 | sl := typ.In(1) |
| 2192 | if sl.Kind() == Slice { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2193 | if sl.Elem() == TypeOf(0.0) { |
Russ Cox | 6672b40 | 2010-06-14 11:23:11 -0700 | [diff] [blame] | 2194 | // ok |
| 2195 | return |
| 2196 | } |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | // Failed |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 2201 | t.Errorf("want NumIn() = 2, In(0) = int, In(1) = []float64") |
Russ Cox | 6672b40 | 2010-06-14 11:23:11 -0700 | [diff] [blame] | 2202 | s := fmt.Sprintf("have NumIn() = %d", typ.NumIn()) |
| 2203 | for i := 0; i < typ.NumIn(); i++ { |
| 2204 | s += fmt.Sprintf(", In(%d) = %s", i, typ.In(i)) |
| 2205 | } |
| 2206 | t.Error(s) |
| 2207 | } |
Russ Cox | 2d5e732 | 2010-09-27 14:09:10 -0400 | [diff] [blame] | 2208 | |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2209 | type inner struct { |
| 2210 | x int |
| 2211 | } |
Russ Cox | 2d5e732 | 2010-09-27 14:09:10 -0400 | [diff] [blame] | 2212 | |
| 2213 | type outer struct { |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2214 | y int |
Russ Cox | 2d5e732 | 2010-09-27 14:09:10 -0400 | [diff] [blame] | 2215 | inner |
| 2216 | } |
| 2217 | |
| 2218 | func (*inner) m() {} |
| 2219 | func (*outer) m() {} |
| 2220 | |
| 2221 | func TestNestedMethods(t *testing.T) { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2222 | typ := TypeOf((*outer)(nil)) |
| 2223 | if typ.NumMethod() != 1 || typ.Method(0).Func.Pointer() != ValueOf((*outer).m).Pointer() { |
Russ Cox | 2d5e732 | 2010-09-27 14:09:10 -0400 | [diff] [blame] | 2224 | t.Errorf("Wrong method table for outer: (m=%p)", (*outer).m) |
| 2225 | for i := 0; i < typ.NumMethod(); i++ { |
| 2226 | m := typ.Method(i) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 2227 | t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Pointer()) |
Russ Cox | 2d5e732 | 2010-09-27 14:09:10 -0400 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | } |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2231 | |
Russ Cox | 5ff3336 | 2011-04-21 08:14:50 -0400 | [diff] [blame] | 2232 | type InnerInt struct { |
| 2233 | X int |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2234 | } |
| 2235 | |
Russ Cox | 5ff3336 | 2011-04-21 08:14:50 -0400 | [diff] [blame] | 2236 | type OuterInt struct { |
| 2237 | Y int |
| 2238 | InnerInt |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2239 | } |
| 2240 | |
Russ Cox | 5ff3336 | 2011-04-21 08:14:50 -0400 | [diff] [blame] | 2241 | func (i *InnerInt) M() int { |
| 2242 | return i.X |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | func TestEmbeddedMethods(t *testing.T) { |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2246 | typ := TypeOf((*OuterInt)(nil)) |
| 2247 | if typ.NumMethod() != 1 || typ.Method(0).Func.Pointer() != ValueOf((*OuterInt).M).Pointer() { |
Russ Cox | 5ff3336 | 2011-04-21 08:14:50 -0400 | [diff] [blame] | 2248 | t.Errorf("Wrong method table for OuterInt: (m=%p)", (*OuterInt).M) |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2249 | for i := 0; i < typ.NumMethod(); i++ { |
| 2250 | m := typ.Method(i) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 2251 | t.Errorf("\t%d: %s %#x\n", i, m.Name, m.Func.Pointer()) |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2252 | } |
| 2253 | } |
| 2254 | |
Russ Cox | 5ff3336 | 2011-04-21 08:14:50 -0400 | [diff] [blame] | 2255 | i := &InnerInt{3} |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2256 | if v := ValueOf(i).Method(0).Call(nil)[0].Int(); v != 3 { |
Russ Cox | 5ff3336 | 2011-04-21 08:14:50 -0400 | [diff] [blame] | 2257 | t.Errorf("i.M() = %d, want 3", v) |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2258 | } |
| 2259 | |
Russ Cox | 5ff3336 | 2011-04-21 08:14:50 -0400 | [diff] [blame] | 2260 | o := &OuterInt{1, InnerInt{2}} |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2261 | if v := ValueOf(o).Method(0).Call(nil)[0].Int(); v != 2 { |
Russ Cox | 5ff3336 | 2011-04-21 08:14:50 -0400 | [diff] [blame] | 2262 | t.Errorf("i.M() = %d, want 2", v) |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2263 | } |
| 2264 | |
Russ Cox | 5ff3336 | 2011-04-21 08:14:50 -0400 | [diff] [blame] | 2265 | f := (*OuterInt).M |
Russ Cox | 00ffd59 | 2010-09-28 13:43:50 -0400 | [diff] [blame] | 2266 | if v := f(o); v != 2 { |
| 2267 | t.Errorf("f(o) = %d, want 2", v) |
| 2268 | } |
| 2269 | } |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 2270 | |
| 2271 | func TestPtrTo(t *testing.T) { |
| 2272 | var i int |
| 2273 | |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2274 | typ := TypeOf(i) |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 2275 | for i = 0; i < 100; i++ { |
| 2276 | typ = PtrTo(typ) |
| 2277 | } |
| 2278 | for i = 0; i < 100; i++ { |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 2279 | typ = typ.Elem() |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 2280 | } |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2281 | if typ != TypeOf(i) { |
| 2282 | t.Errorf("after 100 PtrTo and Elem, have %s, want %s", typ, TypeOf(i)) |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 2283 | } |
| 2284 | } |
| 2285 | |
Russ Cox | 3660b53 | 2013-03-26 11:50:29 -0700 | [diff] [blame] | 2286 | func TestPtrToGC(t *testing.T) { |
| 2287 | type T *uintptr |
| 2288 | tt := TypeOf(T(nil)) |
| 2289 | pt := PtrTo(tt) |
| 2290 | const n = 100 |
| 2291 | var x []interface{} |
| 2292 | for i := 0; i < n; i++ { |
| 2293 | v := New(pt) |
| 2294 | p := new(*uintptr) |
| 2295 | *p = new(uintptr) |
| 2296 | **p = uintptr(i) |
| 2297 | v.Elem().Set(ValueOf(p).Convert(pt)) |
| 2298 | x = append(x, v.Interface()) |
| 2299 | } |
| 2300 | runtime.GC() |
| 2301 | |
| 2302 | for i, xi := range x { |
| 2303 | k := ValueOf(xi).Elem().Elem().Elem().Interface().(uintptr) |
| 2304 | if k != uintptr(i) { |
| 2305 | t.Errorf("lost x[%d] = %d, want %d", i, k, i) |
| 2306 | } |
| 2307 | } |
| 2308 | } |
| 2309 | |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 2310 | func TestAddr(t *testing.T) { |
| 2311 | var p struct { |
| 2312 | X, Y int |
| 2313 | } |
| 2314 | |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2315 | v := ValueOf(&p) |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 2316 | v = v.Elem() |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 2317 | v = v.Addr() |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 2318 | v = v.Elem() |
| 2319 | v = v.Field(0) |
| 2320 | v.SetInt(2) |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 2321 | if p.X != 2 { |
| 2322 | t.Errorf("Addr.Elem.Set failed to set value") |
| 2323 | } |
| 2324 | |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2325 | // Again but take address of the ValueOf value. |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 2326 | // Exercises generation of PtrTypes not present in the binary. |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 2327 | q := &p |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2328 | v = ValueOf(&q).Elem() |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 2329 | v = v.Addr() |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 2330 | v = v.Elem() |
| 2331 | v = v.Elem() |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 2332 | v = v.Addr() |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 2333 | v = v.Elem() |
| 2334 | v = v.Field(0) |
| 2335 | v.SetInt(3) |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 2336 | if p.X != 3 { |
| 2337 | t.Errorf("Addr.Elem.Set failed to set value") |
| 2338 | } |
| 2339 | |
| 2340 | // Starting without pointer we should get changed value |
| 2341 | // in interface. |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 2342 | qq := p |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2343 | v = ValueOf(&qq).Elem() |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 2344 | v0 := v |
| 2345 | v = v.Addr() |
Russ Cox | fb175cf | 2011-04-08 12:26:51 -0400 | [diff] [blame] | 2346 | v = v.Elem() |
| 2347 | v = v.Field(0) |
| 2348 | v.SetInt(4) |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 2349 | if p.X != 3 { // should be unchanged from last time |
| 2350 | t.Errorf("somehow value Set changed original p") |
| 2351 | } |
| 2352 | p = v0.Interface().(struct { |
| 2353 | X, Y int |
| 2354 | }) |
| 2355 | if p.X != 4 { |
| 2356 | t.Errorf("Addr.Elem.Set valued to set value in top value") |
| 2357 | } |
Ian Lance Taylor | ae5c4ea | 2012-02-03 17:36:25 -0800 | [diff] [blame] | 2358 | |
| 2359 | // Verify that taking the address of a type gives us a pointer |
| 2360 | // which we can convert back using the usual interface |
| 2361 | // notation. |
| 2362 | var s struct { |
| 2363 | B *bool |
| 2364 | } |
| 2365 | ps := ValueOf(&s).Elem().Field(0).Addr().Interface() |
| 2366 | *(ps.(**bool)) = new(bool) |
| 2367 | if s.B == nil { |
| 2368 | t.Errorf("Addr.Interface direct assignment failed") |
| 2369 | } |
Russ Cox | e46acb0 | 2011-03-03 13:20:17 -0500 | [diff] [blame] | 2370 | } |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 2371 | |
| 2372 | func noAlloc(t *testing.T, n int, f func(int)) { |
Rob Pike | f578726 | 2013-08-21 14:00:45 +1000 | [diff] [blame] | 2373 | if testing.Short() { |
| 2374 | t.Skip("skipping malloc count in short mode") |
| 2375 | } |
Albert Strasheim | 0a71a5b | 2013-03-06 15:52:32 -0800 | [diff] [blame] | 2376 | if runtime.GOMAXPROCS(0) > 1 { |
| 2377 | t.Skip("skipping; GOMAXPROCS>1") |
| 2378 | } |
Kyle Lemons | 9bfd3c3 | 2013-02-02 22:52:29 -0500 | [diff] [blame] | 2379 | i := -1 |
| 2380 | allocs := testing.AllocsPerRun(n, func() { |
| 2381 | f(i) |
| 2382 | i++ |
| 2383 | }) |
| 2384 | if allocs > 0 { |
| 2385 | t.Errorf("%d iterations: got %v mallocs, want 0", n, allocs) |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | func TestAllocations(t *testing.T) { |
| 2390 | noAlloc(t, 100, func(j int) { |
| 2391 | var i interface{} |
| 2392 | var v Value |
| 2393 | i = 42 + j |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2394 | v = ValueOf(i) |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 2395 | if int(v.Int()) != 42+j { |
| 2396 | panic("wrong int") |
| 2397 | } |
| 2398 | }) |
| 2399 | } |
| 2400 | |
| 2401 | func TestSmallNegativeInt(t *testing.T) { |
| 2402 | i := int16(-1) |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2403 | v := ValueOf(i) |
Russ Cox | 40fccbc | 2011-04-18 14:35:33 -0400 | [diff] [blame] | 2404 | if v.Int() != -1 { |
| 2405 | t.Errorf("int16(-1).Int() returned %v", v.Int()) |
| 2406 | } |
| 2407 | } |
Russ Cox | 3bac16a | 2011-04-18 20:00:42 -0400 | [diff] [blame] | 2408 | |
Evan Shaw | 772decb | 2012-10-21 17:02:10 -0400 | [diff] [blame] | 2409 | func TestIndex(t *testing.T) { |
| 2410 | xs := []byte{1, 2, 3, 4, 5, 6, 7, 8} |
| 2411 | v := ValueOf(xs).Index(3).Interface().(byte) |
| 2412 | if v != xs[3] { |
| 2413 | t.Errorf("xs.Index(3) = %v; expected %v", v, xs[3]) |
| 2414 | } |
| 2415 | xa := [8]byte{10, 20, 30, 40, 50, 60, 70, 80} |
| 2416 | v = ValueOf(xa).Index(2).Interface().(byte) |
| 2417 | if v != xa[2] { |
| 2418 | t.Errorf("xa.Index(2) = %v; expected %v", v, xa[2]) |
| 2419 | } |
| 2420 | s := "0123456789" |
| 2421 | v = ValueOf(s).Index(3).Interface().(byte) |
| 2422 | if v != s[3] { |
| 2423 | t.Errorf("s.Index(3) = %v; expected %v", v, s[3]) |
| 2424 | } |
| 2425 | } |
| 2426 | |
Russ Cox | 3bac16a | 2011-04-18 20:00:42 -0400 | [diff] [blame] | 2427 | func TestSlice(t *testing.T) { |
| 2428 | xs := []int{1, 2, 3, 4, 5, 6, 7, 8} |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2429 | v := ValueOf(xs).Slice(3, 5).Interface().([]int) |
Gustavo Niemeyer | 3dc278d | 2011-12-12 19:45:40 -0200 | [diff] [blame] | 2430 | if len(v) != 2 { |
| 2431 | t.Errorf("len(xs.Slice(3, 5)) = %d", len(v)) |
| 2432 | } |
| 2433 | if cap(v) != 5 { |
| 2434 | t.Errorf("cap(xs.Slice(3, 5)) = %d", cap(v)) |
| 2435 | } |
| 2436 | if !DeepEqual(v[0:5], xs[3:]) { |
| 2437 | t.Errorf("xs.Slice(3, 5)[0:5] = %v", v[0:5]) |
Russ Cox | 3bac16a | 2011-04-18 20:00:42 -0400 | [diff] [blame] | 2438 | } |
Gustavo Niemeyer | 3dc278d | 2011-12-12 19:45:40 -0200 | [diff] [blame] | 2439 | xa := [8]int{10, 20, 30, 40, 50, 60, 70, 80} |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2440 | v = ValueOf(&xa).Elem().Slice(2, 5).Interface().([]int) |
Gustavo Niemeyer | 3dc278d | 2011-12-12 19:45:40 -0200 | [diff] [blame] | 2441 | if len(v) != 3 { |
| 2442 | t.Errorf("len(xa.Slice(2, 5)) = %d", len(v)) |
| 2443 | } |
| 2444 | if cap(v) != 6 { |
| 2445 | t.Errorf("cap(xa.Slice(2, 5)) = %d", cap(v)) |
| 2446 | } |
| 2447 | if !DeepEqual(v[0:6], xa[2:]) { |
| 2448 | t.Errorf("xs.Slice(2, 5)[0:6] = %v", v[0:6]) |
Russ Cox | 3bac16a | 2011-04-18 20:00:42 -0400 | [diff] [blame] | 2449 | } |
Evan Shaw | 772decb | 2012-10-21 17:02:10 -0400 | [diff] [blame] | 2450 | s := "0123456789" |
| 2451 | vs := ValueOf(s).Slice(3, 5).Interface().(string) |
| 2452 | if vs != s[3:5] { |
| 2453 | t.Errorf("s.Slice(3, 5) = %q; expected %q", vs, s[3:5]) |
| 2454 | } |
Russ Cox | 3bac16a | 2011-04-18 20:00:42 -0400 | [diff] [blame] | 2455 | } |
Russ Cox | e1ee3b5 | 2011-04-20 16:24:45 -0400 | [diff] [blame] | 2456 | |
Russ Cox | 4d8aefd | 2013-07-01 20:32:53 -0400 | [diff] [blame] | 2457 | func TestSlice3(t *testing.T) { |
| 2458 | xs := []int{1, 2, 3, 4, 5, 6, 7, 8} |
| 2459 | v := ValueOf(xs).Slice3(3, 5, 7).Interface().([]int) |
| 2460 | if len(v) != 2 { |
| 2461 | t.Errorf("len(xs.Slice3(3, 5, 7)) = %d", len(v)) |
| 2462 | } |
| 2463 | if cap(v) != 4 { |
| 2464 | t.Errorf("cap(xs.Slice3(3, 5, 7)) = %d", cap(v)) |
| 2465 | } |
| 2466 | if !DeepEqual(v[0:4], xs[3:7:7]) { |
| 2467 | t.Errorf("xs.Slice3(3, 5, 7)[0:4] = %v", v[0:4]) |
| 2468 | } |
| 2469 | rv := ValueOf(&xs).Elem() |
| 2470 | shouldPanic(func() { rv.Slice3(1, 2, 1) }) |
| 2471 | shouldPanic(func() { rv.Slice3(1, 1, 11) }) |
| 2472 | shouldPanic(func() { rv.Slice3(2, 2, 1) }) |
| 2473 | |
| 2474 | xa := [8]int{10, 20, 30, 40, 50, 60, 70, 80} |
| 2475 | v = ValueOf(&xa).Elem().Slice3(2, 5, 6).Interface().([]int) |
| 2476 | if len(v) != 3 { |
| 2477 | t.Errorf("len(xa.Slice(2, 5, 6)) = %d", len(v)) |
| 2478 | } |
| 2479 | if cap(v) != 4 { |
| 2480 | t.Errorf("cap(xa.Slice(2, 5, 6)) = %d", cap(v)) |
| 2481 | } |
| 2482 | if !DeepEqual(v[0:4], xa[2:6:6]) { |
| 2483 | t.Errorf("xs.Slice(2, 5, 6)[0:4] = %v", v[0:4]) |
| 2484 | } |
| 2485 | rv = ValueOf(&xa).Elem() |
| 2486 | shouldPanic(func() { rv.Slice3(1, 2, 1) }) |
| 2487 | shouldPanic(func() { rv.Slice3(1, 1, 11) }) |
| 2488 | shouldPanic(func() { rv.Slice3(2, 2, 1) }) |
| 2489 | |
| 2490 | s := "hello world" |
| 2491 | rv = ValueOf(&s).Elem() |
| 2492 | shouldPanic(func() { rv.Slice3(1, 2, 3) }) |
| 2493 | } |
| 2494 | |
| 2495 | func TestSetLenCap(t *testing.T) { |
| 2496 | xs := []int{1, 2, 3, 4, 5, 6, 7, 8} |
| 2497 | xa := [8]int{10, 20, 30, 40, 50, 60, 70, 80} |
| 2498 | |
| 2499 | vs := ValueOf(&xs).Elem() |
| 2500 | shouldPanic(func() { vs.SetLen(10) }) |
| 2501 | shouldPanic(func() { vs.SetCap(10) }) |
| 2502 | shouldPanic(func() { vs.SetLen(-1) }) |
| 2503 | shouldPanic(func() { vs.SetCap(-1) }) |
| 2504 | shouldPanic(func() { vs.SetCap(6) }) // smaller than len |
| 2505 | vs.SetLen(5) |
| 2506 | if len(xs) != 5 || cap(xs) != 8 { |
| 2507 | t.Errorf("after SetLen(5), len, cap = %d, %d, want 5, 8", len(xs), cap(xs)) |
| 2508 | } |
| 2509 | vs.SetCap(6) |
| 2510 | if len(xs) != 5 || cap(xs) != 6 { |
| 2511 | t.Errorf("after SetCap(6), len, cap = %d, %d, want 5, 6", len(xs), cap(xs)) |
| 2512 | } |
| 2513 | vs.SetCap(5) |
| 2514 | if len(xs) != 5 || cap(xs) != 5 { |
| 2515 | t.Errorf("after SetCap(5), len, cap = %d, %d, want 5, 5", len(xs), cap(xs)) |
| 2516 | } |
| 2517 | shouldPanic(func() { vs.SetCap(4) }) // smaller than len |
| 2518 | shouldPanic(func() { vs.SetLen(6) }) // bigger than cap |
| 2519 | |
| 2520 | va := ValueOf(&xa).Elem() |
| 2521 | shouldPanic(func() { va.SetLen(8) }) |
| 2522 | shouldPanic(func() { va.SetCap(8) }) |
| 2523 | } |
| 2524 | |
Russ Cox | e1ee3b5 | 2011-04-20 16:24:45 -0400 | [diff] [blame] | 2525 | func TestVariadic(t *testing.T) { |
| 2526 | var b bytes.Buffer |
Russ Cox | 0e2bb62 | 2011-04-25 13:39:16 -0400 | [diff] [blame] | 2527 | V := ValueOf |
Russ Cox | e1ee3b5 | 2011-04-20 16:24:45 -0400 | [diff] [blame] | 2528 | |
| 2529 | b.Reset() |
| 2530 | V(fmt.Fprintf).Call([]Value{V(&b), V("%s, %d world"), V("hello"), V(42)}) |
| 2531 | if b.String() != "hello, 42 world" { |
| 2532 | t.Errorf("after Fprintf Call: %q != %q", b.String(), "hello 42 world") |
| 2533 | } |
| 2534 | |
| 2535 | b.Reset() |
| 2536 | V(fmt.Fprintf).CallSlice([]Value{V(&b), V("%s, %d world"), V([]interface{}{"hello", 42})}) |
| 2537 | if b.String() != "hello, 42 world" { |
| 2538 | t.Errorf("after Fprintf CallSlice: %q != %q", b.String(), "hello 42 world") |
| 2539 | } |
| 2540 | } |
Russ Cox | 25733a9 | 2011-06-29 09:52:34 -0400 | [diff] [blame] | 2541 | |
Ian Lance Taylor | e59db90 | 2013-10-03 13:23:02 -0700 | [diff] [blame] | 2542 | func TestFuncArg(t *testing.T) { |
| 2543 | f1 := func(i int, f func(int) int) int { return f(i) } |
| 2544 | f2 := func(i int) int { return i + 1 } |
| 2545 | r := ValueOf(f1).Call([]Value{ValueOf(100), ValueOf(f2)}) |
| 2546 | if r[0].Int() != 101 { |
| 2547 | t.Errorf("function returned %d, want 101", r[0].Int()) |
| 2548 | } |
| 2549 | } |
| 2550 | |
Russ Cox | 25733a9 | 2011-06-29 09:52:34 -0400 | [diff] [blame] | 2551 | var tagGetTests = []struct { |
| 2552 | Tag StructTag |
| 2553 | Key string |
| 2554 | Value string |
| 2555 | }{ |
| 2556 | {`protobuf:"PB(1,2)"`, `protobuf`, `PB(1,2)`}, |
| 2557 | {`protobuf:"PB(1,2)"`, `foo`, ``}, |
| 2558 | {`protobuf:"PB(1,2)"`, `rotobuf`, ``}, |
| 2559 | {`protobuf:"PB(1,2)" json:"name"`, `json`, `name`}, |
| 2560 | {`protobuf:"PB(1,2)" json:"name"`, `protobuf`, `PB(1,2)`}, |
| 2561 | } |
| 2562 | |
| 2563 | func TestTagGet(t *testing.T) { |
| 2564 | for _, tt := range tagGetTests { |
| 2565 | if v := tt.Tag.Get(tt.Key); v != tt.Value { |
| 2566 | t.Errorf("StructTag(%#q).Get(%#q) = %#q, want %#q", tt.Tag, tt.Key, v, tt.Value) |
| 2567 | } |
| 2568 | } |
| 2569 | } |
Russ Cox | 00d64c7 | 2011-08-23 22:50:08 -0400 | [diff] [blame] | 2570 | |
| 2571 | func TestBytes(t *testing.T) { |
| 2572 | type B []byte |
| 2573 | x := B{1, 2, 3, 4} |
| 2574 | y := ValueOf(x).Bytes() |
| 2575 | if !bytes.Equal(x, y) { |
| 2576 | t.Fatalf("ValueOf(%v).Bytes() = %v", x, y) |
| 2577 | } |
| 2578 | if &x[0] != &y[0] { |
| 2579 | t.Errorf("ValueOf(%p).Bytes() = %p", &x[0], &y[0]) |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | func TestSetBytes(t *testing.T) { |
| 2584 | type B []byte |
| 2585 | var x B |
| 2586 | y := []byte{1, 2, 3, 4} |
| 2587 | ValueOf(&x).Elem().SetBytes(y) |
| 2588 | if !bytes.Equal(x, y) { |
| 2589 | t.Fatalf("ValueOf(%v).Bytes() = %v", x, y) |
| 2590 | } |
| 2591 | if &x[0] != &y[0] { |
| 2592 | t.Errorf("ValueOf(%p).Bytes() = %p", &x[0], &y[0]) |
| 2593 | } |
| 2594 | } |
Russ Cox | 304cf4d | 2011-10-17 18:48:45 -0400 | [diff] [blame] | 2595 | |
| 2596 | type Private struct { |
| 2597 | x int |
| 2598 | y **int |
| 2599 | } |
| 2600 | |
| 2601 | func (p *Private) m() { |
| 2602 | } |
| 2603 | |
| 2604 | type Public struct { |
| 2605 | X int |
| 2606 | Y **int |
| 2607 | } |
| 2608 | |
| 2609 | func (p *Public) M() { |
| 2610 | } |
| 2611 | |
| 2612 | func TestUnexported(t *testing.T) { |
| 2613 | var pub Public |
| 2614 | v := ValueOf(&pub) |
| 2615 | isValid(v.Elem().Field(0)) |
| 2616 | isValid(v.Elem().Field(1)) |
| 2617 | isValid(v.Elem().FieldByName("X")) |
| 2618 | isValid(v.Elem().FieldByName("Y")) |
| 2619 | isValid(v.Type().Method(0).Func) |
| 2620 | isNonNil(v.Elem().Field(0).Interface()) |
| 2621 | isNonNil(v.Elem().Field(1).Interface()) |
| 2622 | isNonNil(v.Elem().FieldByName("X").Interface()) |
| 2623 | isNonNil(v.Elem().FieldByName("Y").Interface()) |
| 2624 | isNonNil(v.Type().Method(0).Func.Interface()) |
| 2625 | |
| 2626 | var priv Private |
| 2627 | v = ValueOf(&priv) |
| 2628 | isValid(v.Elem().Field(0)) |
| 2629 | isValid(v.Elem().Field(1)) |
| 2630 | isValid(v.Elem().FieldByName("x")) |
| 2631 | isValid(v.Elem().FieldByName("y")) |
| 2632 | isValid(v.Type().Method(0).Func) |
| 2633 | shouldPanic(func() { v.Elem().Field(0).Interface() }) |
| 2634 | shouldPanic(func() { v.Elem().Field(1).Interface() }) |
| 2635 | shouldPanic(func() { v.Elem().FieldByName("x").Interface() }) |
| 2636 | shouldPanic(func() { v.Elem().FieldByName("y").Interface() }) |
| 2637 | shouldPanic(func() { v.Type().Method(0).Func.Interface() }) |
| 2638 | } |
| 2639 | |
| 2640 | func shouldPanic(f func()) { |
| 2641 | defer func() { |
| 2642 | if recover() == nil { |
| 2643 | panic("did not panic") |
| 2644 | } |
| 2645 | }() |
| 2646 | f() |
| 2647 | } |
| 2648 | |
| 2649 | func isNonNil(x interface{}) { |
| 2650 | if x == nil { |
| 2651 | panic("nil interface") |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | func isValid(v Value) { |
| 2656 | if !v.IsValid() { |
| 2657 | panic("zero Value") |
| 2658 | } |
| 2659 | } |
Russ Cox | a72b87e | 2012-03-01 11:48:27 -0500 | [diff] [blame] | 2660 | |
| 2661 | func TestAlias(t *testing.T) { |
| 2662 | x := string("hello") |
| 2663 | v := ValueOf(&x).Elem() |
| 2664 | oldvalue := v.Interface() |
| 2665 | v.SetString("world") |
| 2666 | newvalue := v.Interface() |
| 2667 | |
| 2668 | if oldvalue != "hello" || newvalue != "world" { |
| 2669 | t.Errorf("aliasing: old=%q new=%q, want hello, world", oldvalue, newvalue) |
| 2670 | } |
| 2671 | } |
Russ Cox | 5e3224c | 2012-09-05 09:35:53 -0400 | [diff] [blame] | 2672 | |
Russ Cox | 46f379c | 2012-09-22 08:52:27 -0400 | [diff] [blame] | 2673 | var V = ValueOf |
| 2674 | |
| 2675 | func EmptyInterfaceV(x interface{}) Value { |
| 2676 | return ValueOf(&x).Elem() |
| 2677 | } |
| 2678 | |
| 2679 | func ReaderV(x io.Reader) Value { |
| 2680 | return ValueOf(&x).Elem() |
| 2681 | } |
| 2682 | |
| 2683 | func ReadWriterV(x io.ReadWriter) Value { |
| 2684 | return ValueOf(&x).Elem() |
| 2685 | } |
| 2686 | |
| 2687 | type Empty struct{} |
| 2688 | type MyString string |
| 2689 | type MyBytes []byte |
| 2690 | type MyRunes []int32 |
| 2691 | type MyFunc func() |
| 2692 | type MyByte byte |
| 2693 | |
| 2694 | var convertTests = []struct { |
| 2695 | in Value |
| 2696 | out Value |
| 2697 | }{ |
| 2698 | // numbers |
| 2699 | /* |
| 2700 | Edit .+1,/\*\//-1>cat >/tmp/x.go && go run /tmp/x.go |
| 2701 | |
| 2702 | package main |
| 2703 | |
| 2704 | import "fmt" |
| 2705 | |
| 2706 | var numbers = []string{ |
| 2707 | "int8", "uint8", "int16", "uint16", |
| 2708 | "int32", "uint32", "int64", "uint64", |
| 2709 | "int", "uint", "uintptr", |
| 2710 | "float32", "float64", |
| 2711 | } |
| 2712 | |
| 2713 | func main() { |
| 2714 | // all pairs but in an unusual order, |
| 2715 | // to emit all the int8, uint8 cases |
| 2716 | // before n grows too big. |
| 2717 | n := 1 |
| 2718 | for i, f := range numbers { |
| 2719 | for _, g := range numbers[i:] { |
| 2720 | fmt.Printf("\t{V(%s(%d)), V(%s(%d))},\n", f, n, g, n) |
| 2721 | n++ |
| 2722 | if f != g { |
| 2723 | fmt.Printf("\t{V(%s(%d)), V(%s(%d))},\n", g, n, f, n) |
| 2724 | n++ |
| 2725 | } |
| 2726 | } |
| 2727 | } |
| 2728 | } |
| 2729 | */ |
| 2730 | {V(int8(1)), V(int8(1))}, |
| 2731 | {V(int8(2)), V(uint8(2))}, |
| 2732 | {V(uint8(3)), V(int8(3))}, |
| 2733 | {V(int8(4)), V(int16(4))}, |
| 2734 | {V(int16(5)), V(int8(5))}, |
| 2735 | {V(int8(6)), V(uint16(6))}, |
| 2736 | {V(uint16(7)), V(int8(7))}, |
| 2737 | {V(int8(8)), V(int32(8))}, |
| 2738 | {V(int32(9)), V(int8(9))}, |
| 2739 | {V(int8(10)), V(uint32(10))}, |
| 2740 | {V(uint32(11)), V(int8(11))}, |
| 2741 | {V(int8(12)), V(int64(12))}, |
| 2742 | {V(int64(13)), V(int8(13))}, |
| 2743 | {V(int8(14)), V(uint64(14))}, |
| 2744 | {V(uint64(15)), V(int8(15))}, |
| 2745 | {V(int8(16)), V(int(16))}, |
| 2746 | {V(int(17)), V(int8(17))}, |
| 2747 | {V(int8(18)), V(uint(18))}, |
| 2748 | {V(uint(19)), V(int8(19))}, |
| 2749 | {V(int8(20)), V(uintptr(20))}, |
| 2750 | {V(uintptr(21)), V(int8(21))}, |
| 2751 | {V(int8(22)), V(float32(22))}, |
| 2752 | {V(float32(23)), V(int8(23))}, |
| 2753 | {V(int8(24)), V(float64(24))}, |
| 2754 | {V(float64(25)), V(int8(25))}, |
| 2755 | {V(uint8(26)), V(uint8(26))}, |
| 2756 | {V(uint8(27)), V(int16(27))}, |
| 2757 | {V(int16(28)), V(uint8(28))}, |
| 2758 | {V(uint8(29)), V(uint16(29))}, |
| 2759 | {V(uint16(30)), V(uint8(30))}, |
| 2760 | {V(uint8(31)), V(int32(31))}, |
| 2761 | {V(int32(32)), V(uint8(32))}, |
| 2762 | {V(uint8(33)), V(uint32(33))}, |
| 2763 | {V(uint32(34)), V(uint8(34))}, |
| 2764 | {V(uint8(35)), V(int64(35))}, |
| 2765 | {V(int64(36)), V(uint8(36))}, |
| 2766 | {V(uint8(37)), V(uint64(37))}, |
| 2767 | {V(uint64(38)), V(uint8(38))}, |
| 2768 | {V(uint8(39)), V(int(39))}, |
| 2769 | {V(int(40)), V(uint8(40))}, |
| 2770 | {V(uint8(41)), V(uint(41))}, |
| 2771 | {V(uint(42)), V(uint8(42))}, |
| 2772 | {V(uint8(43)), V(uintptr(43))}, |
| 2773 | {V(uintptr(44)), V(uint8(44))}, |
| 2774 | {V(uint8(45)), V(float32(45))}, |
| 2775 | {V(float32(46)), V(uint8(46))}, |
| 2776 | {V(uint8(47)), V(float64(47))}, |
| 2777 | {V(float64(48)), V(uint8(48))}, |
| 2778 | {V(int16(49)), V(int16(49))}, |
| 2779 | {V(int16(50)), V(uint16(50))}, |
| 2780 | {V(uint16(51)), V(int16(51))}, |
| 2781 | {V(int16(52)), V(int32(52))}, |
| 2782 | {V(int32(53)), V(int16(53))}, |
| 2783 | {V(int16(54)), V(uint32(54))}, |
| 2784 | {V(uint32(55)), V(int16(55))}, |
| 2785 | {V(int16(56)), V(int64(56))}, |
| 2786 | {V(int64(57)), V(int16(57))}, |
| 2787 | {V(int16(58)), V(uint64(58))}, |
| 2788 | {V(uint64(59)), V(int16(59))}, |
| 2789 | {V(int16(60)), V(int(60))}, |
| 2790 | {V(int(61)), V(int16(61))}, |
| 2791 | {V(int16(62)), V(uint(62))}, |
| 2792 | {V(uint(63)), V(int16(63))}, |
| 2793 | {V(int16(64)), V(uintptr(64))}, |
| 2794 | {V(uintptr(65)), V(int16(65))}, |
| 2795 | {V(int16(66)), V(float32(66))}, |
| 2796 | {V(float32(67)), V(int16(67))}, |
| 2797 | {V(int16(68)), V(float64(68))}, |
| 2798 | {V(float64(69)), V(int16(69))}, |
| 2799 | {V(uint16(70)), V(uint16(70))}, |
| 2800 | {V(uint16(71)), V(int32(71))}, |
| 2801 | {V(int32(72)), V(uint16(72))}, |
| 2802 | {V(uint16(73)), V(uint32(73))}, |
| 2803 | {V(uint32(74)), V(uint16(74))}, |
| 2804 | {V(uint16(75)), V(int64(75))}, |
| 2805 | {V(int64(76)), V(uint16(76))}, |
| 2806 | {V(uint16(77)), V(uint64(77))}, |
| 2807 | {V(uint64(78)), V(uint16(78))}, |
| 2808 | {V(uint16(79)), V(int(79))}, |
| 2809 | {V(int(80)), V(uint16(80))}, |
| 2810 | {V(uint16(81)), V(uint(81))}, |
| 2811 | {V(uint(82)), V(uint16(82))}, |
| 2812 | {V(uint16(83)), V(uintptr(83))}, |
| 2813 | {V(uintptr(84)), V(uint16(84))}, |
| 2814 | {V(uint16(85)), V(float32(85))}, |
| 2815 | {V(float32(86)), V(uint16(86))}, |
| 2816 | {V(uint16(87)), V(float64(87))}, |
| 2817 | {V(float64(88)), V(uint16(88))}, |
| 2818 | {V(int32(89)), V(int32(89))}, |
| 2819 | {V(int32(90)), V(uint32(90))}, |
| 2820 | {V(uint32(91)), V(int32(91))}, |
| 2821 | {V(int32(92)), V(int64(92))}, |
| 2822 | {V(int64(93)), V(int32(93))}, |
| 2823 | {V(int32(94)), V(uint64(94))}, |
| 2824 | {V(uint64(95)), V(int32(95))}, |
| 2825 | {V(int32(96)), V(int(96))}, |
| 2826 | {V(int(97)), V(int32(97))}, |
| 2827 | {V(int32(98)), V(uint(98))}, |
| 2828 | {V(uint(99)), V(int32(99))}, |
| 2829 | {V(int32(100)), V(uintptr(100))}, |
| 2830 | {V(uintptr(101)), V(int32(101))}, |
| 2831 | {V(int32(102)), V(float32(102))}, |
| 2832 | {V(float32(103)), V(int32(103))}, |
| 2833 | {V(int32(104)), V(float64(104))}, |
| 2834 | {V(float64(105)), V(int32(105))}, |
| 2835 | {V(uint32(106)), V(uint32(106))}, |
| 2836 | {V(uint32(107)), V(int64(107))}, |
| 2837 | {V(int64(108)), V(uint32(108))}, |
| 2838 | {V(uint32(109)), V(uint64(109))}, |
| 2839 | {V(uint64(110)), V(uint32(110))}, |
| 2840 | {V(uint32(111)), V(int(111))}, |
| 2841 | {V(int(112)), V(uint32(112))}, |
| 2842 | {V(uint32(113)), V(uint(113))}, |
| 2843 | {V(uint(114)), V(uint32(114))}, |
| 2844 | {V(uint32(115)), V(uintptr(115))}, |
| 2845 | {V(uintptr(116)), V(uint32(116))}, |
| 2846 | {V(uint32(117)), V(float32(117))}, |
| 2847 | {V(float32(118)), V(uint32(118))}, |
| 2848 | {V(uint32(119)), V(float64(119))}, |
| 2849 | {V(float64(120)), V(uint32(120))}, |
| 2850 | {V(int64(121)), V(int64(121))}, |
| 2851 | {V(int64(122)), V(uint64(122))}, |
| 2852 | {V(uint64(123)), V(int64(123))}, |
| 2853 | {V(int64(124)), V(int(124))}, |
| 2854 | {V(int(125)), V(int64(125))}, |
| 2855 | {V(int64(126)), V(uint(126))}, |
| 2856 | {V(uint(127)), V(int64(127))}, |
| 2857 | {V(int64(128)), V(uintptr(128))}, |
| 2858 | {V(uintptr(129)), V(int64(129))}, |
| 2859 | {V(int64(130)), V(float32(130))}, |
| 2860 | {V(float32(131)), V(int64(131))}, |
| 2861 | {V(int64(132)), V(float64(132))}, |
| 2862 | {V(float64(133)), V(int64(133))}, |
| 2863 | {V(uint64(134)), V(uint64(134))}, |
| 2864 | {V(uint64(135)), V(int(135))}, |
| 2865 | {V(int(136)), V(uint64(136))}, |
| 2866 | {V(uint64(137)), V(uint(137))}, |
| 2867 | {V(uint(138)), V(uint64(138))}, |
| 2868 | {V(uint64(139)), V(uintptr(139))}, |
| 2869 | {V(uintptr(140)), V(uint64(140))}, |
| 2870 | {V(uint64(141)), V(float32(141))}, |
| 2871 | {V(float32(142)), V(uint64(142))}, |
| 2872 | {V(uint64(143)), V(float64(143))}, |
| 2873 | {V(float64(144)), V(uint64(144))}, |
| 2874 | {V(int(145)), V(int(145))}, |
| 2875 | {V(int(146)), V(uint(146))}, |
| 2876 | {V(uint(147)), V(int(147))}, |
| 2877 | {V(int(148)), V(uintptr(148))}, |
| 2878 | {V(uintptr(149)), V(int(149))}, |
| 2879 | {V(int(150)), V(float32(150))}, |
| 2880 | {V(float32(151)), V(int(151))}, |
| 2881 | {V(int(152)), V(float64(152))}, |
| 2882 | {V(float64(153)), V(int(153))}, |
| 2883 | {V(uint(154)), V(uint(154))}, |
| 2884 | {V(uint(155)), V(uintptr(155))}, |
| 2885 | {V(uintptr(156)), V(uint(156))}, |
| 2886 | {V(uint(157)), V(float32(157))}, |
| 2887 | {V(float32(158)), V(uint(158))}, |
| 2888 | {V(uint(159)), V(float64(159))}, |
| 2889 | {V(float64(160)), V(uint(160))}, |
| 2890 | {V(uintptr(161)), V(uintptr(161))}, |
| 2891 | {V(uintptr(162)), V(float32(162))}, |
| 2892 | {V(float32(163)), V(uintptr(163))}, |
| 2893 | {V(uintptr(164)), V(float64(164))}, |
| 2894 | {V(float64(165)), V(uintptr(165))}, |
| 2895 | {V(float32(166)), V(float32(166))}, |
| 2896 | {V(float32(167)), V(float64(167))}, |
| 2897 | {V(float64(168)), V(float32(168))}, |
| 2898 | {V(float64(169)), V(float64(169))}, |
| 2899 | |
| 2900 | // truncation |
| 2901 | {V(float64(1.5)), V(int(1))}, |
| 2902 | |
| 2903 | // complex |
| 2904 | {V(complex64(1i)), V(complex64(1i))}, |
| 2905 | {V(complex64(2i)), V(complex128(2i))}, |
| 2906 | {V(complex128(3i)), V(complex64(3i))}, |
| 2907 | {V(complex128(4i)), V(complex128(4i))}, |
| 2908 | |
| 2909 | // string |
| 2910 | {V(string("hello")), V(string("hello"))}, |
| 2911 | {V(string("bytes1")), V([]byte("bytes1"))}, |
| 2912 | {V([]byte("bytes2")), V(string("bytes2"))}, |
| 2913 | {V([]byte("bytes3")), V([]byte("bytes3"))}, |
| 2914 | {V(string("runes♝")), V([]rune("runes♝"))}, |
| 2915 | {V([]rune("runes♕")), V(string("runes♕"))}, |
| 2916 | {V([]rune("runes🙈🙉🙊")), V([]rune("runes🙈🙉🙊"))}, |
| 2917 | {V(int('a')), V(string("a"))}, |
| 2918 | {V(int8('a')), V(string("a"))}, |
| 2919 | {V(int16('a')), V(string("a"))}, |
| 2920 | {V(int32('a')), V(string("a"))}, |
| 2921 | {V(int64('a')), V(string("a"))}, |
| 2922 | {V(uint('a')), V(string("a"))}, |
| 2923 | {V(uint8('a')), V(string("a"))}, |
| 2924 | {V(uint16('a')), V(string("a"))}, |
| 2925 | {V(uint32('a')), V(string("a"))}, |
| 2926 | {V(uint64('a')), V(string("a"))}, |
| 2927 | {V(uintptr('a')), V(string("a"))}, |
| 2928 | {V(int(-1)), V(string("\uFFFD"))}, |
| 2929 | {V(int8(-2)), V(string("\uFFFD"))}, |
| 2930 | {V(int16(-3)), V(string("\uFFFD"))}, |
| 2931 | {V(int32(-4)), V(string("\uFFFD"))}, |
| 2932 | {V(int64(-5)), V(string("\uFFFD"))}, |
| 2933 | {V(uint(0x110001)), V(string("\uFFFD"))}, |
| 2934 | {V(uint32(0x110002)), V(string("\uFFFD"))}, |
| 2935 | {V(uint64(0x110003)), V(string("\uFFFD"))}, |
| 2936 | {V(uintptr(0x110004)), V(string("\uFFFD"))}, |
| 2937 | |
| 2938 | // named string |
| 2939 | {V(MyString("hello")), V(string("hello"))}, |
| 2940 | {V(string("hello")), V(MyString("hello"))}, |
| 2941 | {V(string("hello")), V(string("hello"))}, |
| 2942 | {V(MyString("hello")), V(MyString("hello"))}, |
| 2943 | {V(MyString("bytes1")), V([]byte("bytes1"))}, |
| 2944 | {V([]byte("bytes2")), V(MyString("bytes2"))}, |
| 2945 | {V([]byte("bytes3")), V([]byte("bytes3"))}, |
| 2946 | {V(MyString("runes♝")), V([]rune("runes♝"))}, |
| 2947 | {V([]rune("runes♕")), V(MyString("runes♕"))}, |
| 2948 | {V([]rune("runes🙈🙉🙊")), V([]rune("runes🙈🙉🙊"))}, |
| 2949 | {V([]rune("runes🙈🙉🙊")), V(MyRunes("runes🙈🙉🙊"))}, |
| 2950 | {V(MyRunes("runes🙈🙉🙊")), V([]rune("runes🙈🙉🙊"))}, |
| 2951 | {V(int('a')), V(MyString("a"))}, |
| 2952 | {V(int8('a')), V(MyString("a"))}, |
| 2953 | {V(int16('a')), V(MyString("a"))}, |
| 2954 | {V(int32('a')), V(MyString("a"))}, |
| 2955 | {V(int64('a')), V(MyString("a"))}, |
| 2956 | {V(uint('a')), V(MyString("a"))}, |
| 2957 | {V(uint8('a')), V(MyString("a"))}, |
| 2958 | {V(uint16('a')), V(MyString("a"))}, |
| 2959 | {V(uint32('a')), V(MyString("a"))}, |
| 2960 | {V(uint64('a')), V(MyString("a"))}, |
| 2961 | {V(uintptr('a')), V(MyString("a"))}, |
| 2962 | {V(int(-1)), V(MyString("\uFFFD"))}, |
| 2963 | {V(int8(-2)), V(MyString("\uFFFD"))}, |
| 2964 | {V(int16(-3)), V(MyString("\uFFFD"))}, |
| 2965 | {V(int32(-4)), V(MyString("\uFFFD"))}, |
| 2966 | {V(int64(-5)), V(MyString("\uFFFD"))}, |
| 2967 | {V(uint(0x110001)), V(MyString("\uFFFD"))}, |
| 2968 | {V(uint32(0x110002)), V(MyString("\uFFFD"))}, |
| 2969 | {V(uint64(0x110003)), V(MyString("\uFFFD"))}, |
| 2970 | {V(uintptr(0x110004)), V(MyString("\uFFFD"))}, |
| 2971 | |
| 2972 | // named []byte |
| 2973 | {V(string("bytes1")), V(MyBytes("bytes1"))}, |
| 2974 | {V(MyBytes("bytes2")), V(string("bytes2"))}, |
| 2975 | {V(MyBytes("bytes3")), V(MyBytes("bytes3"))}, |
| 2976 | {V(MyString("bytes1")), V(MyBytes("bytes1"))}, |
| 2977 | {V(MyBytes("bytes2")), V(MyString("bytes2"))}, |
| 2978 | |
| 2979 | // named []rune |
| 2980 | {V(string("runes♝")), V(MyRunes("runes♝"))}, |
| 2981 | {V(MyRunes("runes♕")), V(string("runes♕"))}, |
| 2982 | {V(MyRunes("runes🙈🙉🙊")), V(MyRunes("runes🙈🙉🙊"))}, |
| 2983 | {V(MyString("runes♝")), V(MyRunes("runes♝"))}, |
| 2984 | {V(MyRunes("runes♕")), V(MyString("runes♕"))}, |
| 2985 | |
| 2986 | // named types and equal underlying types |
| 2987 | {V(new(int)), V(new(integer))}, |
| 2988 | {V(new(integer)), V(new(int))}, |
| 2989 | {V(Empty{}), V(struct{}{})}, |
| 2990 | {V(new(Empty)), V(new(struct{}))}, |
| 2991 | {V(struct{}{}), V(Empty{})}, |
| 2992 | {V(new(struct{})), V(new(Empty))}, |
| 2993 | {V(Empty{}), V(Empty{})}, |
| 2994 | {V(MyBytes{}), V([]byte{})}, |
| 2995 | {V([]byte{}), V(MyBytes{})}, |
| 2996 | {V((func())(nil)), V(MyFunc(nil))}, |
| 2997 | {V((MyFunc)(nil)), V((func())(nil))}, |
| 2998 | |
| 2999 | // can convert *byte and *MyByte |
| 3000 | {V((*byte)(nil)), V((*MyByte)(nil))}, |
| 3001 | {V((*MyByte)(nil)), V((*byte)(nil))}, |
| 3002 | |
| 3003 | // cannot convert mismatched array sizes |
| 3004 | {V([2]byte{}), V([2]byte{})}, |
| 3005 | {V([3]byte{}), V([3]byte{})}, |
| 3006 | |
| 3007 | // cannot convert other instances |
| 3008 | {V((**byte)(nil)), V((**byte)(nil))}, |
| 3009 | {V((**MyByte)(nil)), V((**MyByte)(nil))}, |
| 3010 | {V((chan byte)(nil)), V((chan byte)(nil))}, |
| 3011 | {V((chan MyByte)(nil)), V((chan MyByte)(nil))}, |
| 3012 | {V(([]byte)(nil)), V(([]byte)(nil))}, |
| 3013 | {V(([]MyByte)(nil)), V(([]MyByte)(nil))}, |
| 3014 | {V((map[int]byte)(nil)), V((map[int]byte)(nil))}, |
| 3015 | {V((map[int]MyByte)(nil)), V((map[int]MyByte)(nil))}, |
| 3016 | {V((map[byte]int)(nil)), V((map[byte]int)(nil))}, |
| 3017 | {V((map[MyByte]int)(nil)), V((map[MyByte]int)(nil))}, |
| 3018 | {V([2]byte{}), V([2]byte{})}, |
| 3019 | {V([2]MyByte{}), V([2]MyByte{})}, |
| 3020 | |
| 3021 | // other |
| 3022 | {V((***int)(nil)), V((***int)(nil))}, |
| 3023 | {V((***byte)(nil)), V((***byte)(nil))}, |
| 3024 | {V((***int32)(nil)), V((***int32)(nil))}, |
| 3025 | {V((***int64)(nil)), V((***int64)(nil))}, |
| 3026 | {V((chan int)(nil)), V((<-chan int)(nil))}, |
| 3027 | {V((chan int)(nil)), V((chan<- int)(nil))}, |
| 3028 | {V((chan string)(nil)), V((<-chan string)(nil))}, |
| 3029 | {V((chan string)(nil)), V((chan<- string)(nil))}, |
| 3030 | {V((chan byte)(nil)), V((chan byte)(nil))}, |
| 3031 | {V((chan MyByte)(nil)), V((chan MyByte)(nil))}, |
| 3032 | {V((map[int]bool)(nil)), V((map[int]bool)(nil))}, |
| 3033 | {V((map[int]byte)(nil)), V((map[int]byte)(nil))}, |
| 3034 | {V((map[uint]bool)(nil)), V((map[uint]bool)(nil))}, |
| 3035 | {V([]uint(nil)), V([]uint(nil))}, |
| 3036 | {V([]int(nil)), V([]int(nil))}, |
| 3037 | {V(new(interface{})), V(new(interface{}))}, |
| 3038 | {V(new(io.Reader)), V(new(io.Reader))}, |
| 3039 | {V(new(io.Writer)), V(new(io.Writer))}, |
| 3040 | |
| 3041 | // interfaces |
| 3042 | {V(int(1)), EmptyInterfaceV(int(1))}, |
| 3043 | {V(string("hello")), EmptyInterfaceV(string("hello"))}, |
| 3044 | {V(new(bytes.Buffer)), ReaderV(new(bytes.Buffer))}, |
| 3045 | {ReadWriterV(new(bytes.Buffer)), ReaderV(new(bytes.Buffer))}, |
| 3046 | {V(new(bytes.Buffer)), ReadWriterV(new(bytes.Buffer))}, |
| 3047 | } |
| 3048 | |
| 3049 | func TestConvert(t *testing.T) { |
| 3050 | canConvert := map[[2]Type]bool{} |
| 3051 | all := map[Type]bool{} |
| 3052 | |
| 3053 | for _, tt := range convertTests { |
| 3054 | t1 := tt.in.Type() |
| 3055 | if !t1.ConvertibleTo(t1) { |
| 3056 | t.Errorf("(%s).ConvertibleTo(%s) = false, want true", t1, t1) |
| 3057 | continue |
| 3058 | } |
| 3059 | |
| 3060 | t2 := tt.out.Type() |
| 3061 | if !t1.ConvertibleTo(t2) { |
| 3062 | t.Errorf("(%s).ConvertibleTo(%s) = false, want true", t1, t2) |
| 3063 | continue |
| 3064 | } |
| 3065 | |
| 3066 | all[t1] = true |
| 3067 | all[t2] = true |
| 3068 | canConvert[[2]Type{t1, t2}] = true |
| 3069 | |
Todd Wang | 0e73497 | 2013-08-21 14:41:55 +1000 | [diff] [blame] | 3070 | // vout1 represents the in value converted to the in type. |
Russ Cox | 46f379c | 2012-09-22 08:52:27 -0400 | [diff] [blame] | 3071 | v1 := tt.in |
| 3072 | vout1 := v1.Convert(t1) |
| 3073 | out1 := vout1.Interface() |
| 3074 | if vout1.Type() != tt.in.Type() || !DeepEqual(out1, tt.in.Interface()) { |
Todd Wang | 0e73497 | 2013-08-21 14:41:55 +1000 | [diff] [blame] | 3075 | t.Errorf("ValueOf(%T(%[1]v)).Convert(%s) = %T(%[3]v), want %T(%[4]v)", tt.in.Interface(), t1, out1, tt.in.Interface()) |
Russ Cox | 46f379c | 2012-09-22 08:52:27 -0400 | [diff] [blame] | 3076 | } |
| 3077 | |
Todd Wang | 0e73497 | 2013-08-21 14:41:55 +1000 | [diff] [blame] | 3078 | // vout2 represents the in value converted to the out type. |
| 3079 | vout2 := v1.Convert(t2) |
| 3080 | out2 := vout2.Interface() |
| 3081 | if vout2.Type() != tt.out.Type() || !DeepEqual(out2, tt.out.Interface()) { |
| 3082 | t.Errorf("ValueOf(%T(%[1]v)).Convert(%s) = %T(%[3]v), want %T(%[4]v)", tt.in.Interface(), t2, out2, tt.out.Interface()) |
| 3083 | } |
| 3084 | |
| 3085 | // vout3 represents a new value of the out type, set to vout2. This makes |
| 3086 | // sure the converted value vout2 is really usable as a regular value. |
| 3087 | vout3 := New(t2).Elem() |
| 3088 | vout3.Set(vout2) |
| 3089 | out3 := vout3.Interface() |
| 3090 | if vout3.Type() != tt.out.Type() || !DeepEqual(out3, tt.out.Interface()) { |
| 3091 | t.Errorf("Set(ValueOf(%T(%[1]v)).Convert(%s)) = %T(%[3]v), want %T(%[4]v)", tt.in.Interface(), t2, out3, tt.out.Interface()) |
Russ Cox | 46f379c | 2012-09-22 08:52:27 -0400 | [diff] [blame] | 3092 | } |
| 3093 | |
| 3094 | if IsRO(v1) { |
| 3095 | t.Errorf("table entry %v is RO, should not be", v1) |
| 3096 | } |
| 3097 | if IsRO(vout1) { |
| 3098 | t.Errorf("self-conversion output %v is RO, should not be", vout1) |
| 3099 | } |
Todd Wang | 0e73497 | 2013-08-21 14:41:55 +1000 | [diff] [blame] | 3100 | if IsRO(vout2) { |
| 3101 | t.Errorf("conversion output %v is RO, should not be", vout2) |
| 3102 | } |
| 3103 | if IsRO(vout3) { |
| 3104 | t.Errorf("set(conversion output) %v is RO, should not be", vout3) |
Russ Cox | 46f379c | 2012-09-22 08:52:27 -0400 | [diff] [blame] | 3105 | } |
| 3106 | if !IsRO(MakeRO(v1).Convert(t1)) { |
| 3107 | t.Errorf("RO self-conversion output %v is not RO, should be", v1) |
| 3108 | } |
| 3109 | if !IsRO(MakeRO(v1).Convert(t2)) { |
| 3110 | t.Errorf("RO conversion output %v is not RO, should be", v1) |
| 3111 | } |
| 3112 | } |
| 3113 | |
| 3114 | // Assume that of all the types we saw during the tests, |
| 3115 | // if there wasn't an explicit entry for a conversion between |
| 3116 | // a pair of types, then it's not to be allowed. This checks for |
| 3117 | // things like 'int64' converting to '*int'. |
| 3118 | for t1 := range all { |
| 3119 | for t2 := range all { |
| 3120 | expectOK := t1 == t2 || canConvert[[2]Type{t1, t2}] || t2.Kind() == Interface && t2.NumMethod() == 0 |
| 3121 | if ok := t1.ConvertibleTo(t2); ok != expectOK { |
| 3122 | t.Errorf("(%s).ConvertibleTo(%s) = %v, want %v", t1, t2, ok, expectOK) |
| 3123 | } |
| 3124 | } |
| 3125 | } |
| 3126 | } |
| 3127 | |
Rémy Oudompheng | 38070a7 | 2012-10-26 08:39:36 +0200 | [diff] [blame] | 3128 | func TestOverflow(t *testing.T) { |
| 3129 | if ovf := V(float64(0)).OverflowFloat(1e300); ovf { |
| 3130 | t.Errorf("%v wrongly overflows float64", 1e300) |
| 3131 | } |
| 3132 | |
| 3133 | maxFloat32 := float64((1<<24 - 1) << (127 - 23)) |
| 3134 | if ovf := V(float32(0)).OverflowFloat(maxFloat32); ovf { |
| 3135 | t.Errorf("%v wrongly overflows float32", maxFloat32) |
| 3136 | } |
| 3137 | ovfFloat32 := float64((1<<24-1)<<(127-23) + 1<<(127-52)) |
| 3138 | if ovf := V(float32(0)).OverflowFloat(ovfFloat32); !ovf { |
| 3139 | t.Errorf("%v should overflow float32", ovfFloat32) |
| 3140 | } |
| 3141 | if ovf := V(float32(0)).OverflowFloat(-ovfFloat32); !ovf { |
| 3142 | t.Errorf("%v should overflow float32", -ovfFloat32) |
| 3143 | } |
| 3144 | |
| 3145 | maxInt32 := int64(0x7fffffff) |
| 3146 | if ovf := V(int32(0)).OverflowInt(maxInt32); ovf { |
| 3147 | t.Errorf("%v wrongly overflows int32", maxInt32) |
| 3148 | } |
| 3149 | if ovf := V(int32(0)).OverflowInt(-1 << 31); ovf { |
| 3150 | t.Errorf("%v wrongly overflows int32", -int64(1)<<31) |
| 3151 | } |
| 3152 | ovfInt32 := int64(1 << 31) |
| 3153 | if ovf := V(int32(0)).OverflowInt(ovfInt32); !ovf { |
| 3154 | t.Errorf("%v should overflow int32", ovfInt32) |
| 3155 | } |
| 3156 | |
| 3157 | maxUint32 := uint64(0xffffffff) |
| 3158 | if ovf := V(uint32(0)).OverflowUint(maxUint32); ovf { |
| 3159 | t.Errorf("%v wrongly overflows uint32", maxUint32) |
| 3160 | } |
| 3161 | ovfUint32 := uint64(1 << 32) |
| 3162 | if ovf := V(uint32(0)).OverflowUint(ovfUint32); !ovf { |
| 3163 | t.Errorf("%v should overflow uint32", ovfUint32) |
| 3164 | } |
| 3165 | } |
| 3166 | |
Russ Cox | 1120982 | 2012-11-13 13:06:29 -0500 | [diff] [blame] | 3167 | func checkSameType(t *testing.T, x, y interface{}) { |
| 3168 | if TypeOf(x) != TypeOf(y) { |
| 3169 | t.Errorf("did not find preexisting type for %s (vs %s)", TypeOf(x), TypeOf(y)) |
| 3170 | } |
| 3171 | } |
| 3172 | |
| 3173 | func TestArrayOf(t *testing.T) { |
| 3174 | // check construction and use of type not in binary |
| 3175 | type T int |
| 3176 | at := ArrayOf(10, TypeOf(T(1))) |
| 3177 | v := New(at).Elem() |
| 3178 | for i := 0; i < v.Len(); i++ { |
| 3179 | v.Index(i).Set(ValueOf(T(i))) |
| 3180 | } |
| 3181 | s := fmt.Sprint(v.Interface()) |
| 3182 | want := "[0 1 2 3 4 5 6 7 8 9]" |
| 3183 | if s != want { |
| 3184 | t.Errorf("constructed array = %s, want %s", s, want) |
| 3185 | } |
| 3186 | |
| 3187 | // check that type already in binary is found |
| 3188 | checkSameType(t, Zero(ArrayOf(5, TypeOf(T(1)))).Interface(), [5]T{}) |
| 3189 | } |
| 3190 | |
| 3191 | func TestSliceOf(t *testing.T) { |
| 3192 | // check construction and use of type not in binary |
| 3193 | type T int |
| 3194 | st := SliceOf(TypeOf(T(1))) |
| 3195 | v := MakeSlice(st, 10, 10) |
Russ Cox | 3660b53 | 2013-03-26 11:50:29 -0700 | [diff] [blame] | 3196 | runtime.GC() |
Russ Cox | 1120982 | 2012-11-13 13:06:29 -0500 | [diff] [blame] | 3197 | for i := 0; i < v.Len(); i++ { |
| 3198 | v.Index(i).Set(ValueOf(T(i))) |
Russ Cox | 3660b53 | 2013-03-26 11:50:29 -0700 | [diff] [blame] | 3199 | runtime.GC() |
Russ Cox | 1120982 | 2012-11-13 13:06:29 -0500 | [diff] [blame] | 3200 | } |
| 3201 | s := fmt.Sprint(v.Interface()) |
| 3202 | want := "[0 1 2 3 4 5 6 7 8 9]" |
| 3203 | if s != want { |
| 3204 | t.Errorf("constructed slice = %s, want %s", s, want) |
| 3205 | } |
| 3206 | |
| 3207 | // check that type already in binary is found |
| 3208 | type T1 int |
| 3209 | checkSameType(t, Zero(SliceOf(TypeOf(T1(1)))).Interface(), []T1{}) |
| 3210 | } |
| 3211 | |
Dmitriy Vyukov | 5782f41 | 2013-05-27 11:29:11 +0400 | [diff] [blame] | 3212 | func TestSliceOverflow(t *testing.T) { |
| 3213 | // check that MakeSlice panics when size of slice overflows uint |
| 3214 | const S = 1e6 |
| 3215 | s := uint(S) |
| 3216 | l := (1<<(unsafe.Sizeof((*byte)(nil))*8)-1)/s + 1 |
| 3217 | if l*s >= s { |
| 3218 | t.Fatal("slice size does not overflow") |
| 3219 | } |
| 3220 | var x [S]byte |
| 3221 | st := SliceOf(TypeOf(x)) |
| 3222 | defer func() { |
| 3223 | err := recover() |
| 3224 | if err == nil { |
| 3225 | t.Fatal("slice overflow does not panic") |
| 3226 | } |
| 3227 | }() |
| 3228 | MakeSlice(st, int(l), int(l)) |
| 3229 | } |
| 3230 | |
Russ Cox | 3660b53 | 2013-03-26 11:50:29 -0700 | [diff] [blame] | 3231 | func TestSliceOfGC(t *testing.T) { |
| 3232 | type T *uintptr |
| 3233 | tt := TypeOf(T(nil)) |
| 3234 | st := SliceOf(tt) |
| 3235 | const n = 100 |
| 3236 | var x []interface{} |
| 3237 | for i := 0; i < n; i++ { |
| 3238 | v := MakeSlice(st, n, n) |
| 3239 | for j := 0; j < v.Len(); j++ { |
| 3240 | p := new(uintptr) |
| 3241 | *p = uintptr(i*n + j) |
| 3242 | v.Index(j).Set(ValueOf(p).Convert(tt)) |
| 3243 | } |
| 3244 | x = append(x, v.Interface()) |
| 3245 | } |
| 3246 | runtime.GC() |
| 3247 | |
| 3248 | for i, xi := range x { |
| 3249 | v := ValueOf(xi) |
| 3250 | for j := 0; j < v.Len(); j++ { |
| 3251 | k := v.Index(j).Elem().Interface() |
| 3252 | if k != uintptr(i*n+j) { |
| 3253 | t.Errorf("lost x[%d][%d] = %d, want %d", i, j, k, i*n+j) |
| 3254 | } |
| 3255 | } |
| 3256 | } |
| 3257 | } |
| 3258 | |
Russ Cox | 1120982 | 2012-11-13 13:06:29 -0500 | [diff] [blame] | 3259 | func TestChanOf(t *testing.T) { |
| 3260 | // check construction and use of type not in binary |
| 3261 | type T string |
| 3262 | ct := ChanOf(BothDir, TypeOf(T(""))) |
| 3263 | v := MakeChan(ct, 2) |
Russ Cox | 3660b53 | 2013-03-26 11:50:29 -0700 | [diff] [blame] | 3264 | runtime.GC() |
Russ Cox | 1120982 | 2012-11-13 13:06:29 -0500 | [diff] [blame] | 3265 | v.Send(ValueOf(T("hello"))) |
Russ Cox | 3660b53 | 2013-03-26 11:50:29 -0700 | [diff] [blame] | 3266 | runtime.GC() |
Russ Cox | 1120982 | 2012-11-13 13:06:29 -0500 | [diff] [blame] | 3267 | v.Send(ValueOf(T("world"))) |
Russ Cox | 3660b53 | 2013-03-26 11:50:29 -0700 | [diff] [blame] | 3268 | runtime.GC() |
Russ Cox | 1120982 | 2012-11-13 13:06:29 -0500 | [diff] [blame] | 3269 | |
| 3270 | sv1, _ := v.Recv() |
| 3271 | sv2, _ := v.Recv() |
| 3272 | s1 := sv1.String() |
| 3273 | s2 := sv2.String() |
| 3274 | if s1 != "hello" || s2 != "world" { |
| 3275 | t.Errorf("constructed chan: have %q, %q, want %q, %q", s1, s2, "hello", "world") |
| 3276 | } |
| 3277 | |
| 3278 | // check that type already in binary is found |
| 3279 | type T1 int |
| 3280 | checkSameType(t, Zero(ChanOf(BothDir, TypeOf(T1(1)))).Interface(), (chan T1)(nil)) |
| 3281 | } |
| 3282 | |
Russ Cox | 3660b53 | 2013-03-26 11:50:29 -0700 | [diff] [blame] | 3283 | func TestChanOfGC(t *testing.T) { |
| 3284 | done := make(chan bool, 1) |
| 3285 | go func() { |
| 3286 | select { |
| 3287 | case <-done: |
| 3288 | case <-time.After(5 * time.Second): |
| 3289 | panic("deadlock in TestChanOfGC") |
| 3290 | } |
| 3291 | }() |
| 3292 | |
| 3293 | defer func() { |
| 3294 | done <- true |
| 3295 | }() |
| 3296 | |
| 3297 | type T *uintptr |
| 3298 | tt := TypeOf(T(nil)) |
| 3299 | ct := ChanOf(BothDir, tt) |
| 3300 | |
| 3301 | // NOTE: The garbage collector handles allocated channels specially, |
| 3302 | // so we have to save pointers to channels in x; the pointer code will |
| 3303 | // use the gc info in the newly constructed chan type. |
| 3304 | const n = 100 |
| 3305 | var x []interface{} |
| 3306 | for i := 0; i < n; i++ { |
| 3307 | v := MakeChan(ct, n) |
| 3308 | for j := 0; j < n; j++ { |
| 3309 | p := new(uintptr) |
| 3310 | *p = uintptr(i*n + j) |
| 3311 | v.Send(ValueOf(p).Convert(tt)) |
| 3312 | } |
| 3313 | pv := New(ct) |
| 3314 | pv.Elem().Set(v) |
| 3315 | x = append(x, pv.Interface()) |
| 3316 | } |
| 3317 | runtime.GC() |
| 3318 | |
| 3319 | for i, xi := range x { |
| 3320 | v := ValueOf(xi).Elem() |
| 3321 | for j := 0; j < n; j++ { |
| 3322 | pv, _ := v.Recv() |
| 3323 | k := pv.Elem().Interface() |
| 3324 | if k != uintptr(i*n+j) { |
| 3325 | t.Errorf("lost x[%d][%d] = %d, want %d", i, j, k, i*n+j) |
| 3326 | } |
| 3327 | } |
| 3328 | } |
| 3329 | } |
| 3330 | |
Russ Cox | 1120982 | 2012-11-13 13:06:29 -0500 | [diff] [blame] | 3331 | func TestMapOf(t *testing.T) { |
| 3332 | // check construction and use of type not in binary |
| 3333 | type K string |
| 3334 | type V float64 |
| 3335 | |
| 3336 | v := MakeMap(MapOf(TypeOf(K("")), TypeOf(V(0)))) |
Russ Cox | 3660b53 | 2013-03-26 11:50:29 -0700 | [diff] [blame] | 3337 | runtime.GC() |
Russ Cox | 1120982 | 2012-11-13 13:06:29 -0500 | [diff] [blame] | 3338 | v.SetMapIndex(ValueOf(K("a")), ValueOf(V(1))) |
Russ Cox | 3660b53 | 2013-03-26 11:50:29 -0700 | [diff] [blame] | 3339 | runtime.GC() |
Russ Cox | 1120982 | 2012-11-13 13:06:29 -0500 | [diff] [blame] | 3340 | |
| 3341 | s := fmt.Sprint(v.Interface()) |
| 3342 | want := "map[a:1]" |
| 3343 | if s != want { |
| 3344 | t.Errorf("constructed map = %s, want %s", s, want) |
| 3345 | } |
| 3346 | |
| 3347 | // check that type already in binary is found |
| 3348 | checkSameType(t, Zero(MapOf(TypeOf(V(0)), TypeOf(K("")))).Interface(), map[V]K(nil)) |
Russ Cox | 3660b53 | 2013-03-26 11:50:29 -0700 | [diff] [blame] | 3349 | |
| 3350 | // check that invalid key type panics |
| 3351 | shouldPanic(func() { MapOf(TypeOf((func())(nil)), TypeOf(false)) }) |
| 3352 | } |
| 3353 | |
| 3354 | func TestMapOfGCKeys(t *testing.T) { |
| 3355 | type T *uintptr |
| 3356 | tt := TypeOf(T(nil)) |
| 3357 | mt := MapOf(tt, TypeOf(false)) |
| 3358 | |
| 3359 | // NOTE: The garbage collector handles allocated maps specially, |
| 3360 | // so we have to save pointers to maps in x; the pointer code will |
| 3361 | // use the gc info in the newly constructed map type. |
| 3362 | const n = 100 |
| 3363 | var x []interface{} |
| 3364 | for i := 0; i < n; i++ { |
| 3365 | v := MakeMap(mt) |
| 3366 | for j := 0; j < n; j++ { |
| 3367 | p := new(uintptr) |
| 3368 | *p = uintptr(i*n + j) |
| 3369 | v.SetMapIndex(ValueOf(p).Convert(tt), ValueOf(true)) |
| 3370 | } |
| 3371 | pv := New(mt) |
| 3372 | pv.Elem().Set(v) |
| 3373 | x = append(x, pv.Interface()) |
| 3374 | } |
| 3375 | runtime.GC() |
| 3376 | |
| 3377 | for i, xi := range x { |
| 3378 | v := ValueOf(xi).Elem() |
| 3379 | var out []int |
| 3380 | for _, kv := range v.MapKeys() { |
| 3381 | out = append(out, int(kv.Elem().Interface().(uintptr))) |
| 3382 | } |
| 3383 | sort.Ints(out) |
| 3384 | for j, k := range out { |
| 3385 | if k != i*n+j { |
| 3386 | t.Errorf("lost x[%d][%d] = %d, want %d", i, j, k, i*n+j) |
| 3387 | } |
| 3388 | } |
| 3389 | } |
| 3390 | } |
| 3391 | |
| 3392 | func TestMapOfGCValues(t *testing.T) { |
| 3393 | type T *uintptr |
| 3394 | tt := TypeOf(T(nil)) |
| 3395 | mt := MapOf(TypeOf(1), tt) |
| 3396 | |
| 3397 | // NOTE: The garbage collector handles allocated maps specially, |
| 3398 | // so we have to save pointers to maps in x; the pointer code will |
| 3399 | // use the gc info in the newly constructed map type. |
| 3400 | const n = 100 |
| 3401 | var x []interface{} |
| 3402 | for i := 0; i < n; i++ { |
| 3403 | v := MakeMap(mt) |
| 3404 | for j := 0; j < n; j++ { |
| 3405 | p := new(uintptr) |
| 3406 | *p = uintptr(i*n + j) |
| 3407 | v.SetMapIndex(ValueOf(j), ValueOf(p).Convert(tt)) |
| 3408 | } |
| 3409 | pv := New(mt) |
| 3410 | pv.Elem().Set(v) |
| 3411 | x = append(x, pv.Interface()) |
| 3412 | } |
| 3413 | runtime.GC() |
| 3414 | |
| 3415 | for i, xi := range x { |
| 3416 | v := ValueOf(xi).Elem() |
| 3417 | for j := 0; j < n; j++ { |
| 3418 | k := v.MapIndex(ValueOf(j)).Elem().Interface().(uintptr) |
| 3419 | if k != uintptr(i*n+j) { |
| 3420 | t.Errorf("lost x[%d][%d] = %d, want %d", i, j, k, i*n+j) |
| 3421 | } |
| 3422 | } |
| 3423 | } |
Russ Cox | 1120982 | 2012-11-13 13:06:29 -0500 | [diff] [blame] | 3424 | } |
| 3425 | |
Russ Cox | 5e3224c | 2012-09-05 09:35:53 -0400 | [diff] [blame] | 3426 | type B1 struct { |
| 3427 | X int |
| 3428 | Y int |
| 3429 | Z int |
| 3430 | } |
| 3431 | |
| 3432 | func BenchmarkFieldByName1(b *testing.B) { |
| 3433 | t := TypeOf(B1{}) |
| 3434 | for i := 0; i < b.N; i++ { |
| 3435 | t.FieldByName("Z") |
| 3436 | } |
| 3437 | } |
| 3438 | |
| 3439 | func BenchmarkFieldByName2(b *testing.B) { |
| 3440 | t := TypeOf(S3{}) |
| 3441 | for i := 0; i < b.N; i++ { |
| 3442 | t.FieldByName("B") |
| 3443 | } |
| 3444 | } |
| 3445 | |
| 3446 | type R0 struct { |
| 3447 | *R1 |
| 3448 | *R2 |
| 3449 | *R3 |
| 3450 | *R4 |
| 3451 | } |
| 3452 | |
| 3453 | type R1 struct { |
| 3454 | *R5 |
| 3455 | *R6 |
| 3456 | *R7 |
| 3457 | *R8 |
| 3458 | } |
| 3459 | |
| 3460 | type R2 R1 |
| 3461 | type R3 R1 |
| 3462 | type R4 R1 |
| 3463 | |
| 3464 | type R5 struct { |
| 3465 | *R9 |
| 3466 | *R10 |
| 3467 | *R11 |
| 3468 | *R12 |
| 3469 | } |
| 3470 | |
| 3471 | type R6 R5 |
| 3472 | type R7 R5 |
| 3473 | type R8 R5 |
| 3474 | |
| 3475 | type R9 struct { |
| 3476 | *R13 |
| 3477 | *R14 |
| 3478 | *R15 |
| 3479 | *R16 |
| 3480 | } |
| 3481 | |
| 3482 | type R10 R9 |
| 3483 | type R11 R9 |
| 3484 | type R12 R9 |
| 3485 | |
| 3486 | type R13 struct { |
| 3487 | *R17 |
| 3488 | *R18 |
| 3489 | *R19 |
| 3490 | *R20 |
| 3491 | } |
| 3492 | |
| 3493 | type R14 R13 |
| 3494 | type R15 R13 |
| 3495 | type R16 R13 |
| 3496 | |
| 3497 | type R17 struct { |
| 3498 | *R21 |
| 3499 | *R22 |
| 3500 | *R23 |
| 3501 | *R24 |
| 3502 | } |
| 3503 | |
| 3504 | type R18 R17 |
| 3505 | type R19 R17 |
| 3506 | type R20 R17 |
| 3507 | |
| 3508 | type R21 struct { |
| 3509 | X int |
| 3510 | } |
| 3511 | |
| 3512 | type R22 R21 |
| 3513 | type R23 R21 |
| 3514 | type R24 R21 |
| 3515 | |
| 3516 | func TestEmbed(t *testing.T) { |
| 3517 | typ := TypeOf(R0{}) |
| 3518 | f, ok := typ.FieldByName("X") |
| 3519 | if ok { |
| 3520 | t.Fatalf(`FieldByName("X") should fail, returned %v`, f.Index) |
| 3521 | } |
| 3522 | } |
| 3523 | |
| 3524 | func BenchmarkFieldByName3(b *testing.B) { |
| 3525 | t := TypeOf(R0{}) |
| 3526 | for i := 0; i < b.N; i++ { |
| 3527 | t.FieldByName("X") |
| 3528 | } |
| 3529 | } |
Russ Cox | 370ae05 | 2012-09-18 14:22:41 -0400 | [diff] [blame] | 3530 | |
Rob Pike | 94179d6 | 2013-08-09 10:49:01 +1000 | [diff] [blame] | 3531 | type S struct { |
| 3532 | i1 int64 |
| 3533 | i2 int64 |
| 3534 | } |
| 3535 | |
| 3536 | func BenchmarkInterfaceBig(b *testing.B) { |
| 3537 | v := ValueOf(S{}) |
| 3538 | for i := 0; i < b.N; i++ { |
| 3539 | v.Interface() |
| 3540 | } |
| 3541 | b.StopTimer() |
| 3542 | } |
| 3543 | |
| 3544 | func TestAllocsInterfaceBig(t *testing.T) { |
Rob Pike | f578726 | 2013-08-21 14:00:45 +1000 | [diff] [blame] | 3545 | if testing.Short() { |
| 3546 | t.Skip("skipping malloc count in short mode") |
| 3547 | } |
Rob Pike | 94179d6 | 2013-08-09 10:49:01 +1000 | [diff] [blame] | 3548 | v := ValueOf(S{}) |
| 3549 | if allocs := testing.AllocsPerRun(100, func() { v.Interface() }); allocs > 0 { |
Kamil Kisiel | e07b5ba | 2013-09-23 13:19:08 -0400 | [diff] [blame] | 3550 | t.Error("allocs:", allocs) |
Rob Pike | 94179d6 | 2013-08-09 10:49:01 +1000 | [diff] [blame] | 3551 | } |
| 3552 | } |
| 3553 | |
| 3554 | func BenchmarkInterfaceSmall(b *testing.B) { |
| 3555 | v := ValueOf(int64(0)) |
| 3556 | for i := 0; i < b.N; i++ { |
| 3557 | v.Interface() |
| 3558 | } |
| 3559 | } |
| 3560 | |
| 3561 | func TestAllocsInterfaceSmall(t *testing.T) { |
Rob Pike | f578726 | 2013-08-21 14:00:45 +1000 | [diff] [blame] | 3562 | if testing.Short() { |
| 3563 | t.Skip("skipping malloc count in short mode") |
| 3564 | } |
Rob Pike | 94179d6 | 2013-08-09 10:49:01 +1000 | [diff] [blame] | 3565 | v := ValueOf(int64(0)) |
| 3566 | if allocs := testing.AllocsPerRun(100, func() { v.Interface() }); allocs > 0 { |
Kamil Kisiel | e07b5ba | 2013-09-23 13:19:08 -0400 | [diff] [blame] | 3567 | t.Error("allocs:", allocs) |
Rob Pike | 94179d6 | 2013-08-09 10:49:01 +1000 | [diff] [blame] | 3568 | } |
| 3569 | } |
| 3570 | |
Russ Cox | 370ae05 | 2012-09-18 14:22:41 -0400 | [diff] [blame] | 3571 | // An exhaustive is a mechanism for writing exhaustive or stochastic tests. |
| 3572 | // The basic usage is: |
| 3573 | // |
| 3574 | // for x.Next() { |
| 3575 | // ... code using x.Maybe() or x.Choice(n) to create test cases ... |
| 3576 | // } |
| 3577 | // |
| 3578 | // Each iteration of the loop returns a different set of results, until all |
| 3579 | // possible result sets have been explored. It is okay for different code paths |
| 3580 | // to make different method call sequences on x, but there must be no |
| 3581 | // other source of non-determinism in the call sequences. |
| 3582 | // |
| 3583 | // When faced with a new decision, x chooses randomly. Future explorations |
| 3584 | // of that path will choose successive values for the result. Thus, stopping |
| 3585 | // the loop after a fixed number of iterations gives somewhat stochastic |
| 3586 | // testing. |
| 3587 | // |
| 3588 | // Example: |
| 3589 | // |
| 3590 | // for x.Next() { |
| 3591 | // v := make([]bool, x.Choose(4)) |
| 3592 | // for i := range v { |
| 3593 | // v[i] = x.Maybe() |
| 3594 | // } |
| 3595 | // fmt.Println(v) |
| 3596 | // } |
| 3597 | // |
| 3598 | // prints (in some order): |
| 3599 | // |
| 3600 | // [] |
| 3601 | // [false] |
| 3602 | // [true] |
| 3603 | // [false false] |
| 3604 | // [false true] |
| 3605 | // ... |
| 3606 | // [true true] |
| 3607 | // [false false false] |
| 3608 | // ... |
| 3609 | // [true true true] |
| 3610 | // [false false false false] |
| 3611 | // ... |
| 3612 | // [true true true true] |
| 3613 | // |
| 3614 | type exhaustive struct { |
| 3615 | r *rand.Rand |
| 3616 | pos int |
| 3617 | last []choice |
| 3618 | } |
| 3619 | |
| 3620 | type choice struct { |
| 3621 | off int |
| 3622 | n int |
| 3623 | max int |
| 3624 | } |
| 3625 | |
| 3626 | func (x *exhaustive) Next() bool { |
| 3627 | if x.r == nil { |
| 3628 | x.r = rand.New(rand.NewSource(time.Now().UnixNano())) |
| 3629 | } |
| 3630 | x.pos = 0 |
| 3631 | if x.last == nil { |
| 3632 | x.last = []choice{} |
| 3633 | return true |
| 3634 | } |
| 3635 | for i := len(x.last) - 1; i >= 0; i-- { |
| 3636 | c := &x.last[i] |
| 3637 | if c.n+1 < c.max { |
| 3638 | c.n++ |
| 3639 | x.last = x.last[:i+1] |
| 3640 | return true |
| 3641 | } |
| 3642 | } |
| 3643 | return false |
| 3644 | } |
| 3645 | |
| 3646 | func (x *exhaustive) Choose(max int) int { |
| 3647 | if x.pos >= len(x.last) { |
| 3648 | x.last = append(x.last, choice{x.r.Intn(max), 0, max}) |
| 3649 | } |
| 3650 | c := &x.last[x.pos] |
| 3651 | x.pos++ |
| 3652 | if c.max != max { |
| 3653 | panic("inconsistent use of exhaustive tester") |
| 3654 | } |
| 3655 | return (c.n + c.off) % max |
| 3656 | } |
| 3657 | |
| 3658 | func (x *exhaustive) Maybe() bool { |
| 3659 | return x.Choose(2) == 1 |
| 3660 | } |
Keith Randall | 85138da | 2013-12-02 13:36:50 -0800 | [diff] [blame] | 3661 | |
| 3662 | func GCFunc(args []Value) []Value { |
| 3663 | runtime.GC() |
| 3664 | return []Value{} |
| 3665 | } |
| 3666 | |
| 3667 | func TestReflectFuncTraceback(t *testing.T) { |
| 3668 | f := MakeFunc(TypeOf(func() {}), GCFunc) |
| 3669 | f.Call([]Value{}) |
| 3670 | } |
| 3671 | |
| 3672 | func (p Point) GCMethod(k int) int { |
| 3673 | runtime.GC() |
| 3674 | return k + p.x |
| 3675 | } |
| 3676 | |
| 3677 | func TestReflectMethodTraceback(t *testing.T) { |
| 3678 | p := Point{3, 4} |
| 3679 | m := ValueOf(p).MethodByName("GCMethod") |
| 3680 | i := ValueOf(m.Interface()).Call([]Value{ValueOf(5)})[0].Int() |
| 3681 | if i != 8 { |
| 3682 | t.Errorf("Call returned %d; want 8", i) |
| 3683 | } |
| 3684 | } |
Keith Randall | 742f755 | 2013-12-02 17:58:19 -0800 | [diff] [blame] | 3685 | |
| 3686 | func TestBigZero(t *testing.T) { |
| 3687 | const size = 1 << 10 |
| 3688 | var v [size]byte |
| 3689 | z := Zero(ValueOf(v).Type()).Interface().([size]byte) |
| 3690 | for i := 0; i < size; i++ { |
| 3691 | if z[i] != 0 { |
| 3692 | t.Fatalf("Zero object not all zero, index %d", i) |
| 3693 | } |
| 3694 | } |
| 3695 | } |
Russ Cox | 5984732 | 2014-02-21 13:51:22 -0500 | [diff] [blame] | 3696 | |
| 3697 | func TestFieldByIndexNil(t *testing.T) { |
| 3698 | type P struct { |
| 3699 | F int |
| 3700 | } |
| 3701 | type T struct { |
| 3702 | *P |
| 3703 | } |
| 3704 | v := ValueOf(T{}) |
| 3705 | |
| 3706 | v.FieldByName("P") // should be fine |
| 3707 | |
| 3708 | defer func() { |
| 3709 | if err := recover(); err == nil { |
| 3710 | t.Fatalf("no error") |
| 3711 | } else if !strings.Contains(fmt.Sprint(err), "nil pointer to embedded struct") { |
| 3712 | t.Fatalf(`err=%q, wanted error containing "nil pointer to embedded struct"`, err) |
| 3713 | } |
| 3714 | }() |
| 3715 | v.FieldByName("F") // should panic |
| 3716 | |
| 3717 | t.Fatalf("did not panic") |
| 3718 | } |
Russ Cox | 72c5d5e | 2014-04-08 11:11:35 -0400 | [diff] [blame^] | 3719 | |
| 3720 | // Given |
| 3721 | // type Outer struct { |
| 3722 | // *Inner |
| 3723 | // ... |
| 3724 | // } |
| 3725 | // the compiler generates the implementation of (*Outer).M dispatching to the embedded Inner. |
| 3726 | // The implementation is logically: |
| 3727 | // func (p *Outer) M() { |
| 3728 | // (p.Inner).M() |
| 3729 | // } |
| 3730 | // but since the only change here is the replacement of one pointer receiver with another, |
| 3731 | // the actual generated code overwrites the original receiver with the p.Inner pointer and |
| 3732 | // then jumps to the M method expecting the *Inner receiver. |
| 3733 | // |
| 3734 | // During reflect.Value.Call, we create an argument frame and the associated data structures |
| 3735 | // to describe it to the garbage collector, populate the frame, call reflect.call to |
| 3736 | // run a function call using that frame, and then copy the results back out of the frame. |
| 3737 | // The reflect.call function does a memmove of the frame structure onto the |
| 3738 | // stack (to set up the inputs), runs the call, and the memmoves the stack back to |
| 3739 | // the frame structure (to preserve the outputs). |
| 3740 | // |
| 3741 | // Originally reflect.call did not distinguish inputs from outputs: both memmoves |
| 3742 | // were for the full stack frame. However, in the case where the called function was |
| 3743 | // one of these wrappers, the rewritten receiver is almost certainly a different type |
| 3744 | // than the original receiver. This is not a problem on the stack, where we use the |
| 3745 | // program counter to determine the type information and understand that |
| 3746 | // during (*Outer).M the receiver is an *Outer while during (*Inner).M the receiver in the same |
| 3747 | // memory word is now an *Inner. But in the statically typed argument frame created |
| 3748 | // by reflect, the receiver is always an *Outer. Copying the modified receiver pointer |
| 3749 | // off the stack into the frame will store an *Inner there, and then if a garbage collection |
| 3750 | // happens to scan that argument frame before it is discarded, it will scan the *Inner |
| 3751 | // memory as if it were an *Outer. If the two have different memory layouts, the |
| 3752 | // collection will intepret the memory incorrectly. |
| 3753 | // |
| 3754 | // One such possible incorrect interpretation is to treat two arbitrary memory words |
| 3755 | // (Inner.P1 and Inner.P2 below) as an interface (Outer.R below). Because interpreting |
| 3756 | // an interface requires dereferencing the itab word, the misinterpretation will try to |
| 3757 | // deference Inner.P1, causing a crash during garbage collection. |
| 3758 | // |
| 3759 | // This came up in a real program in issue 7725. |
| 3760 | |
| 3761 | type Outer struct { |
| 3762 | *Inner |
| 3763 | R io.Reader |
| 3764 | } |
| 3765 | |
| 3766 | type Inner struct { |
| 3767 | X *Outer |
| 3768 | P1 uintptr |
| 3769 | P2 uintptr |
| 3770 | } |
| 3771 | |
| 3772 | func (pi *Inner) M() { |
| 3773 | // Clear references to pi so that the only way the |
| 3774 | // garbage collection will find the pointer is in the |
| 3775 | // argument frame, typed as a *Outer. |
| 3776 | pi.X.Inner = nil |
| 3777 | |
| 3778 | // Set up an interface value that will cause a crash. |
| 3779 | // P1 = 1 is a non-zero, so the interface looks non-nil. |
| 3780 | // P2 = pi ensures that the data word points into the |
| 3781 | // allocated heap; if not the collection skips the interface |
| 3782 | // value as irrelevant, without dereferencing P1. |
| 3783 | pi.P1 = 1 |
| 3784 | pi.P2 = uintptr(unsafe.Pointer(pi)) |
| 3785 | } |
| 3786 | |
| 3787 | func TestCallMethodJump(t *testing.T) { |
| 3788 | // In reflect.Value.Call, trigger a garbage collection after reflect.call |
| 3789 | // returns but before the args frame has been discarded. |
| 3790 | // This is a little clumsy but makes the failure repeatable. |
| 3791 | *CallGC = true |
| 3792 | |
| 3793 | p := &Outer{Inner: new(Inner)} |
| 3794 | p.Inner.X = p |
| 3795 | ValueOf(p).Method(0).Call(nil) |
| 3796 | |
| 3797 | // Stop garbage collecting during reflect.call. |
| 3798 | *CallGC = false |
| 3799 | } |