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 | |
| 5 | package main |
| 6 | |
| 7 | import ( |
| 8 | "reflect" |
| 9 | ) |
| 10 | |
| 11 | var doprint bool = false |
| 12 | |
| 13 | func is_digit(c uint8) bool { |
| 14 | return '0' <= c && c <= '9' |
| 15 | } |
| 16 | |
| 17 | // streq, but '@' in t matches a string of digits |
| 18 | func match(s, t string) bool { |
| 19 | for i, j := 0, 0; i < len(s) && j < len(t); i, j = i+1, j+1 { |
| 20 | if s[i] == t[j] { |
| 21 | continue |
| 22 | } |
| 23 | if is_digit(s[i]) && t[j] == '@' { |
| 24 | for is_digit(s[i+1]) { |
| 25 | i++ |
| 26 | } |
| 27 | } else { |
| 28 | return false |
| 29 | } |
| 30 | } |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | func assert(s, t string) { |
| 35 | if doprint { |
| 36 | println(t) |
| 37 | } |
| 38 | if !match(s, t) { |
| 39 | panicln(s, t) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func typedump(s, t string) { |
| 44 | typ := reflect.ParseTypeString("", s); |
| 45 | assert(reflect.TypeToString(typ, true), t); |
| 46 | } |
| 47 | |
| 48 | func valuedump(s, t string) { |
| 49 | typ := reflect.ParseTypeString("", s); |
| 50 | v := reflect.NewInitValue(typ); |
| 51 | switch v.Kind() { |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 52 | case reflect.IntKind: |
| 53 | v.(reflect.IntValue).Put(132); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 54 | case reflect.Int8Kind: |
| 55 | v.(reflect.Int8Value).Put(8); |
| 56 | case reflect.Int16Kind: |
| 57 | v.(reflect.Int16Value).Put(16); |
| 58 | case reflect.Int32Kind: |
| 59 | v.(reflect.Int32Value).Put(32); |
| 60 | case reflect.Int64Kind: |
| 61 | v.(reflect.Int64Value).Put(64); |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 62 | case reflect.UintKind: |
| 63 | v.(reflect.UintValue).Put(132); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 64 | case reflect.Uint8Kind: |
| 65 | v.(reflect.Uint8Value).Put(8); |
| 66 | case reflect.Uint16Kind: |
| 67 | v.(reflect.Uint16Value).Put(16); |
| 68 | case reflect.Uint32Kind: |
| 69 | v.(reflect.Uint32Value).Put(32); |
| 70 | case reflect.Uint64Kind: |
| 71 | v.(reflect.Uint64Value).Put(64); |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 72 | case reflect.FloatKind: |
| 73 | v.(reflect.FloatValue).Put(3200.0); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 74 | case reflect.Float32Kind: |
| 75 | v.(reflect.Float32Value).Put(32.0); |
| 76 | case reflect.Float64Kind: |
| 77 | v.(reflect.Float64Value).Put(64.0); |
| 78 | case reflect.StringKind: |
| 79 | v.(reflect.StringValue).Put("stringy cheese"); |
Ian Lance Taylor | 16fd356 | 2008-10-31 16:34:47 -0700 | [diff] [blame] | 80 | case reflect.BoolKind: |
| 81 | v.(reflect.BoolValue).Put(true); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 82 | } |
| 83 | assert(reflect.ValueToString(v), t); |
| 84 | } |
| 85 | |
| 86 | export type empty interface {} |
| 87 | |
| 88 | export type T struct { a int; b float64; c string; d *int } |
| 89 | |
| 90 | func main() { |
| 91 | var s string; |
| 92 | var t reflect.Type; |
| 93 | |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 94 | // Types |
Rob Pike | 178e37e | 2008-11-02 12:32:14 -0800 | [diff] [blame] | 95 | typedump("missing", "$missing$"); |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 96 | typedump("int", "int"); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 97 | typedump("int8", "int8"); |
| 98 | typedump("int16", "int16"); |
| 99 | typedump("int32", "int32"); |
| 100 | typedump("int64", "int64"); |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 101 | typedump("uint", "uint"); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 102 | typedump("uint8", "uint8"); |
| 103 | typedump("uint16", "uint16"); |
| 104 | typedump("uint32", "uint32"); |
| 105 | typedump("uint64", "uint64"); |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 106 | typedump("float", "float"); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 107 | typedump("float32", "float32"); |
| 108 | typedump("float64", "float64"); |
| 109 | typedump("float80", "float80"); |
| 110 | typedump("int8", "int8"); |
Rob Pike | 178e37e | 2008-11-02 12:32:14 -0800 | [diff] [blame] | 111 | typedump("whoknows.whatsthis", "$missing$"); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 112 | typedump("**int8", "**int8"); |
| 113 | typedump("**P.integer", "**P.integer"); |
| 114 | typedump("[32]int32", "[32]int32"); |
| 115 | typedump("[]int8", "[]int8"); |
| 116 | typedump("*map[string]int32", "*map[string]int32"); |
| 117 | typedump("*chan<-string", "*chan<-string"); |
| 118 | typedump("struct {c *chan *int32; d float32}", "struct{c *chan*int32; d float32}"); |
| 119 | typedump("*(a int8, b int32)", "*(a int8, b int32)"); |
| 120 | typedump("struct {c *(? *chan *P.integer, ? *int8)}", "struct{c *(? *chan*P.integer, ? *int8)}"); |
| 121 | typedump("struct {a int8; b int32}", "struct{a int8; b int32}"); |
| 122 | typedump("struct {a int8; b int8; b int32}", "struct{a int8; b int8; b int32}"); |
| 123 | typedump("struct {a int8; b int8; c int8; b int32}", "struct{a int8; b int8; c int8; b int32}"); |
| 124 | typedump("struct {a int8; b int8; c int8; d int8; b int32}", "struct{a int8; b int8; c int8; d int8; b int32}"); |
| 125 | typedump("struct {a int8; b int8; c int8; d int8; e int8; b int32}", "struct{a int8; b int8; c int8; d int8; e int8; b int32}"); |
Rob Pike | 12a3435 | 2008-10-30 17:29:53 -0700 | [diff] [blame] | 126 | typedump("struct {a int8 \"hi there\"; }", "struct{a int8 \"hi there\"}"); |
Rob Pike | 613a5c8 | 2008-10-31 15:26:14 -0700 | [diff] [blame] | 127 | typedump("struct {a int8 \"hi \\x00there\\t\\n\\\"\\\\\"; }", "struct{a int8 \"hi \\x00there\\t\\n\\\"\\\\\"}"); |
Rob Pike | 730fd70 | 2008-11-03 15:50:11 -0800 | [diff] [blame] | 128 | typedump("struct {f *(args ...)}", "struct{f *(args ...)}"); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 129 | |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 130 | // Values |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 131 | valuedump("int8", "8"); |
| 132 | valuedump("int16", "16"); |
| 133 | valuedump("int32", "32"); |
| 134 | valuedump("int64", "64"); |
| 135 | valuedump("uint8", "8"); |
| 136 | valuedump("uint16", "16"); |
| 137 | valuedump("uint32", "32"); |
| 138 | valuedump("uint64", "64"); |
| 139 | valuedump("float32", "+3.200000e+01"); |
| 140 | valuedump("float64", "+6.400000e+01"); |
| 141 | valuedump("string", "stringy cheese"); |
Ian Lance Taylor | 16fd356 | 2008-10-31 16:34:47 -0700 | [diff] [blame] | 142 | valuedump("bool", "true"); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 143 | valuedump("*int8", "*int8(0)"); |
| 144 | valuedump("**int8", "**int8(0)"); |
| 145 | valuedump("[5]int32", "[5]int32{0, 0, 0, 0, 0}"); |
| 146 | valuedump("**P.integer", "**P.integer(0)"); |
| 147 | valuedump("*map[string]int32", "*map[string]int32(0)"); |
| 148 | valuedump("*chan<-string", "*chan<-string(0)"); |
| 149 | valuedump("struct {c *chan *int32; d float32}", "struct{c *chan*int32; d float32}{*chan*int32(0), +0.000000e+00}"); |
| 150 | valuedump("*(a int8, b int32)", "*(a int8, b int32)(0)"); |
| 151 | valuedump("struct {c *(? *chan *P.integer, ? *int8)}", "struct{c *(? *chan*P.integer, ? *int8)}{*(? *chan*P.integer, ? *int8)(0)}"); |
| 152 | valuedump("struct {a int8; b int32}", "struct{a int8; b int32}{0, 0}"); |
| 153 | valuedump("struct {a int8; b int8; b int32}", "struct{a int8; b int8; b int32}{0, 0, 0}"); |
| 154 | |
| 155 | { var tmp = 123; |
| 156 | value := reflect.NewValue(tmp); |
| 157 | assert(reflect.ValueToString(value), "123"); |
| 158 | } |
| 159 | { var tmp = 123.4; |
| 160 | value := reflect.NewValue(tmp); |
| 161 | assert(reflect.ValueToString(value), "+1.234000e+02"); |
| 162 | } |
| 163 | { var tmp = "abc"; |
| 164 | value := reflect.NewValue(tmp); |
| 165 | assert(reflect.ValueToString(value), "abc"); |
| 166 | } |
| 167 | { |
| 168 | var i int = 7; |
| 169 | var tmp = &T{123, 456.0, "hello", &i}; |
| 170 | value := reflect.NewValue(tmp); |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 171 | assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.T{123, +4.560000e+02, hello, *int(@)}"); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 172 | } |
| 173 | { |
| 174 | type C chan *T; // TODO: should not be necessary |
| 175 | var tmp = new(C); |
| 176 | value := reflect.NewValue(tmp); |
Russ Cox | c4969a3 | 2008-11-03 15:38:27 -0800 | [diff] [blame] | 177 | assert(reflect.ValueToString(value), "*main.C·test(@)"); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 178 | } |
| 179 | { |
| 180 | type A [10]int; |
| 181 | var tmp A = A{1,2,3,4,5,6,7,8,9,10}; |
| 182 | value := reflect.NewValue(&tmp); |
Russ Cox | c4969a3 | 2008-11-03 15:38:27 -0800 | [diff] [blame] | 183 | assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.A·test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"); |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 184 | value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.IntValue).Put(123); |
Russ Cox | c4969a3 | 2008-11-03 15:38:27 -0800 | [diff] [blame] | 185 | assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.A·test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 186 | } |
| 187 | { |
| 188 | type AA []int; |
| 189 | tmp1 := [10]int{1,2,3,4,5,6,7,8,9,10}; // TODO: should not be necessary to use tmp1 |
| 190 | var tmp *AA = &tmp1; |
| 191 | value := reflect.NewValue(tmp); |
Russ Cox | c4969a3 | 2008-11-03 15:38:27 -0800 | [diff] [blame] | 192 | assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.AA·test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}"); |
Rob Pike | 282493b | 2008-10-29 15:31:02 -0700 | [diff] [blame] | 193 | value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.IntValue).Put(123); |
Russ Cox | c4969a3 | 2008-11-03 15:38:27 -0800 | [diff] [blame] | 194 | assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.AA·test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}"); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 195 | } |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 196 | |
| 197 | var pt reflect.PtrType; |
| 198 | var st reflect.StructType; |
| 199 | var mt reflect.MapType; |
| 200 | var at reflect.ArrayType; |
| 201 | var ct reflect.ChanType; |
| 202 | var name string; |
| 203 | var typ reflect.Type; |
| 204 | var tag string; |
| 205 | var offset uint64; |
| 206 | |
| 207 | // Type strings |
| 208 | t = reflect.ParseTypeString("", "int8"); |
| 209 | assert(t.String(), "int8"); |
| 210 | |
| 211 | t = reflect.ParseTypeString("", "*int8"); |
| 212 | assert(t.String(), "*int8"); |
| 213 | pt = t.(reflect.PtrType); |
| 214 | assert(pt.Sub().String(), "int8"); |
| 215 | |
| 216 | t = reflect.ParseTypeString("", "*struct {c *chan *int32; d float32}"); |
| 217 | assert(t.String(), "*struct {c *chan *int32; d float32}"); |
| 218 | pt = t.(reflect.PtrType); |
| 219 | assert(pt.Sub().String(), "struct {c *chan *int32; d float32}"); |
| 220 | st = pt.Sub().(reflect.StructType); |
| 221 | name, typ, tag, offset = st.Field(0); |
| 222 | assert(typ.String(), "*chan *int32"); |
| 223 | name, typ, tag, offset = st.Field(1); |
| 224 | assert(typ.String(), "float32"); |
| 225 | |
Rob Pike | bdbb958 | 2008-11-05 08:17:01 -0800 | [diff] [blame^] | 226 | t = reflect.ParseTypeString("", "interface {a() *int}"); |
| 227 | assert(t.String(), "interface {a() *int}"); |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 228 | |
| 229 | t = reflect.ParseTypeString("", "*(a int8, b int32)"); |
| 230 | assert(t.String(), "*(a int8, b int32)"); |
| 231 | |
Rob Pike | bdbb958 | 2008-11-05 08:17:01 -0800 | [diff] [blame^] | 232 | t = reflect.ParseTypeString("", "*(a int8, b int32) float"); |
| 233 | assert(t.String(), "*(a int8, b int32) float"); |
| 234 | |
| 235 | t = reflect.ParseTypeString("", "*(a int8, b int32) (a float, b float)"); |
| 236 | assert(t.String(), "*(a int8, b int32) (a float, b float)"); |
| 237 | |
Rob Pike | a45f947 | 2008-11-04 22:54:11 -0800 | [diff] [blame] | 238 | t = reflect.ParseTypeString("", "[32]int32"); |
| 239 | assert(t.String(), "[32]int32"); |
| 240 | at = t.(reflect.ArrayType); |
| 241 | assert(at.Elem().String(), "int32"); |
| 242 | |
| 243 | t = reflect.ParseTypeString("", "map[string]*int32"); |
| 244 | assert(t.String(), "map[string]*int32"); |
| 245 | mt = t.(reflect.MapType); |
| 246 | assert(mt.Key().String(), "string"); |
| 247 | assert(mt.Elem().String(), "*int32"); |
| 248 | |
| 249 | t = reflect.ParseTypeString("", "chan<-string"); |
| 250 | assert(t.String(), "chan<-string"); |
| 251 | ct = t.(reflect.ChanType); |
| 252 | assert(ct.Elem().String(), "string"); |
Rob Pike | 536b1f2 | 2008-10-23 17:13:34 -0700 | [diff] [blame] | 253 | } |