blob: 13be64cc0835168e4bea0d219b838eff3183ece6 [file] [log] [blame]
Rob Pike536b1f22008-10-23 17:13:34 -07001// 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
5package main
6
7import (
8 "reflect"
9)
10
11var doprint bool = false
12
13func is_digit(c uint8) bool {
14 return '0' <= c && c <= '9'
15}
16
17// streq, but '@' in t matches a string of digits
18func 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
34func assert(s, t string) {
35 if doprint {
36 println(t)
37 }
38 if !match(s, t) {
39 panicln(s, t)
40 }
41}
42
43func typedump(s, t string) {
44 typ := reflect.ParseTypeString("", s);
45 assert(reflect.TypeToString(typ, true), t);
46}
47
48func valuedump(s, t string) {
49 typ := reflect.ParseTypeString("", s);
50 v := reflect.NewInitValue(typ);
51 switch v.Kind() {
Rob Pike282493b2008-10-29 15:31:02 -070052 case reflect.IntKind:
53 v.(reflect.IntValue).Put(132);
Rob Pike536b1f22008-10-23 17:13:34 -070054 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 Pike282493b2008-10-29 15:31:02 -070062 case reflect.UintKind:
63 v.(reflect.UintValue).Put(132);
Rob Pike536b1f22008-10-23 17:13:34 -070064 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 Pike282493b2008-10-29 15:31:02 -070072 case reflect.FloatKind:
73 v.(reflect.FloatValue).Put(3200.0);
Rob Pike536b1f22008-10-23 17:13:34 -070074 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 Taylor16fd3562008-10-31 16:34:47 -070080 case reflect.BoolKind:
81 v.(reflect.BoolValue).Put(true);
Rob Pike536b1f22008-10-23 17:13:34 -070082 }
83 assert(reflect.ValueToString(v), t);
84}
85
86export type empty interface {}
87
88export type T struct { a int; b float64; c string; d *int }
89
90func main() {
91 var s string;
92 var t reflect.Type;
93
Rob Pikea45f9472008-11-04 22:54:11 -080094 // Types
Rob Pike178e37e2008-11-02 12:32:14 -080095 typedump("missing", "$missing$");
Rob Pike282493b2008-10-29 15:31:02 -070096 typedump("int", "int");
Rob Pike536b1f22008-10-23 17:13:34 -070097 typedump("int8", "int8");
98 typedump("int16", "int16");
99 typedump("int32", "int32");
100 typedump("int64", "int64");
Rob Pike282493b2008-10-29 15:31:02 -0700101 typedump("uint", "uint");
Rob Pike536b1f22008-10-23 17:13:34 -0700102 typedump("uint8", "uint8");
103 typedump("uint16", "uint16");
104 typedump("uint32", "uint32");
105 typedump("uint64", "uint64");
Rob Pike282493b2008-10-29 15:31:02 -0700106 typedump("float", "float");
Rob Pike536b1f22008-10-23 17:13:34 -0700107 typedump("float32", "float32");
108 typedump("float64", "float64");
109 typedump("float80", "float80");
110 typedump("int8", "int8");
Rob Pike178e37e2008-11-02 12:32:14 -0800111 typedump("whoknows.whatsthis", "$missing$");
Rob Pike536b1f22008-10-23 17:13:34 -0700112 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 Pike12a34352008-10-30 17:29:53 -0700126 typedump("struct {a int8 \"hi there\"; }", "struct{a int8 \"hi there\"}");
Rob Pike613a5c82008-10-31 15:26:14 -0700127 typedump("struct {a int8 \"hi \\x00there\\t\\n\\\"\\\\\"; }", "struct{a int8 \"hi \\x00there\\t\\n\\\"\\\\\"}");
Rob Pike730fd702008-11-03 15:50:11 -0800128 typedump("struct {f *(args ...)}", "struct{f *(args ...)}");
Rob Pike536b1f22008-10-23 17:13:34 -0700129
Rob Pikea45f9472008-11-04 22:54:11 -0800130 // Values
Rob Pike536b1f22008-10-23 17:13:34 -0700131 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 Taylor16fd3562008-10-31 16:34:47 -0700142 valuedump("bool", "true");
Rob Pike536b1f22008-10-23 17:13:34 -0700143 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 Pike282493b2008-10-29 15:31:02 -0700171 assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.T{123, +4.560000e+02, hello, *int(@)}");
Rob Pike536b1f22008-10-23 17:13:34 -0700172 }
173 {
174 type C chan *T; // TODO: should not be necessary
175 var tmp = new(C);
176 value := reflect.NewValue(tmp);
Russ Coxc4969a32008-11-03 15:38:27 -0800177 assert(reflect.ValueToString(value), "*main.C·test(@)");
Rob Pike536b1f22008-10-23 17:13:34 -0700178 }
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 Coxc4969a32008-11-03 15:38:27 -0800183 assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.A·test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}");
Rob Pike282493b2008-10-29 15:31:02 -0700184 value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.IntValue).Put(123);
Russ Coxc4969a32008-11-03 15:38:27 -0800185 assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.A·test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}");
Rob Pike536b1f22008-10-23 17:13:34 -0700186 }
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 Coxc4969a32008-11-03 15:38:27 -0800192 assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.AA·test{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}");
Rob Pike282493b2008-10-29 15:31:02 -0700193 value.(reflect.PtrValue).Sub().(reflect.ArrayValue).Elem(4).(reflect.IntValue).Put(123);
Russ Coxc4969a32008-11-03 15:38:27 -0800194 assert(reflect.ValueToString(value.(reflect.PtrValue).Sub()), "main.AA·test{1, 2, 3, 4, 123, 6, 7, 8, 9, 10}");
Rob Pike536b1f22008-10-23 17:13:34 -0700195 }
Rob Pikea45f9472008-11-04 22:54:11 -0800196
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 Pikebdbb9582008-11-05 08:17:01 -0800226 t = reflect.ParseTypeString("", "interface {a() *int}");
227 assert(t.String(), "interface {a() *int}");
Rob Pikea45f9472008-11-04 22:54:11 -0800228
229 t = reflect.ParseTypeString("", "*(a int8, b int32)");
230 assert(t.String(), "*(a int8, b int32)");
231
Rob Pikebdbb9582008-11-05 08:17:01 -0800232 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 Pikea45f9472008-11-04 22:54:11 -0800238 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 Pike536b1f22008-10-23 17:13:34 -0700253}