OSDN Git Service

Remove unused functions from sparc backend.
[pf3gnuchains/gcc-fork.git] / libgo / go / exp / datafmt / datafmt_test.go
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 datafmt
6
7 import (
8         "fmt"
9         "testing"
10         "go/token"
11 )
12
13 var fset = token.NewFileSet()
14
15 func parse(t *testing.T, form string, fmap FormatterMap) Format {
16         f, err := Parse(fset, "", []byte(form), fmap)
17         if err != nil {
18                 t.Errorf("Parse(%s): %v", form, err)
19                 return nil
20         }
21         return f
22 }
23
24 func verify(t *testing.T, f Format, expected string, args ...interface{}) {
25         if f == nil {
26                 return // allow other tests to run
27         }
28         result := f.Sprint(args...)
29         if result != expected {
30                 t.Errorf(
31                         "result  : `%s`\nexpected: `%s`\n\n",
32                         result, expected)
33         }
34 }
35
36 func formatter(s *State, value interface{}, rule_name string) bool {
37         switch rule_name {
38         case "/":
39                 fmt.Fprintf(s, "%d %d %d", s.Pos().Line, s.LinePos().Column, s.Pos().Column)
40                 return true
41         case "blank":
42                 s.Write([]byte{' '})
43                 return true
44         case "int":
45                 if value.(int)&1 == 0 {
46                         fmt.Fprint(s, "even ")
47                 } else {
48                         fmt.Fprint(s, "odd ")
49                 }
50                 return true
51         case "nil":
52                 return false
53         case "testing.T":
54                 s.Write([]byte("testing.T"))
55                 return true
56         }
57         panic("unreachable")
58         return false
59 }
60
61 func TestCustomFormatters(t *testing.T) {
62         fmap0 := FormatterMap{"/": formatter}
63         fmap1 := FormatterMap{"int": formatter, "blank": formatter, "nil": formatter}
64         fmap2 := FormatterMap{"testing.T": formatter}
65
66         f := parse(t, `int=`, fmap0)
67         verify(t, f, ``, 1, 2, 3)
68
69         f = parse(t, `int="#"`, nil)
70         verify(t, f, `###`, 1, 2, 3)
71
72         f = parse(t, `int="#";string="%s"`, fmap0)
73         verify(t, f, "#1 0 1#1 0 7#1 0 13\n2 0 0foo2 0 8\n", 1, 2, 3, "\n", "foo", "\n")
74
75         f = parse(t, ``, fmap1)
76         verify(t, f, `even odd even odd `, 0, 1, 2, 3)
77
78         f = parse(t, `/ =@:blank; float64="#"`, fmap1)
79         verify(t, f, `# # #`, 0.0, 1.0, 2.0)
80
81         f = parse(t, `float64=@:nil`, fmap1)
82         verify(t, f, ``, 0.0, 1.0, 2.0)
83
84         f = parse(t, `testing "testing"; ptr=*`, fmap2)
85         verify(t, f, `testing.T`, t)
86
87         // TODO needs more tests
88 }
89
90 // ----------------------------------------------------------------------------
91 // Formatting of basic and simple composite types
92
93 func check(t *testing.T, form, expected string, args ...interface{}) {
94         f := parse(t, form, nil)
95         if f == nil {
96                 return // allow other tests to run
97         }
98         result := f.Sprint(args...)
99         if result != expected {
100                 t.Errorf(
101                         "format  : %s\nresult  : `%s`\nexpected: `%s`\n\n",
102                         form, result, expected)
103         }
104 }
105
106 func TestBasicTypes(t *testing.T) {
107         check(t, ``, ``)
108         check(t, `bool=":%v"`, `:true:false`, true, false)
109         check(t, `int="%b %d %o 0x%x"`, `101010 42 52 0x2a`, 42)
110
111         check(t, `int="%"`, `%`, 42)
112         check(t, `int="%%"`, `%`, 42)
113         check(t, `int="**%%**"`, `**%**`, 42)
114         check(t, `int="%%%%%%"`, `%%%`, 42)
115         check(t, `int="%%%d%%"`, `%42%`, 42)
116
117         const i = -42
118         const is = `-42`
119         check(t, `int  ="%d"`, is, i)
120         check(t, `int8 ="%d"`, is, int8(i))
121         check(t, `int16="%d"`, is, int16(i))
122         check(t, `int32="%d"`, is, int32(i))
123         check(t, `int64="%d"`, is, int64(i))
124
125         const u = 42
126         const us = `42`
127         check(t, `uint  ="%d"`, us, uint(u))
128         check(t, `uint8 ="%d"`, us, uint8(u))
129         check(t, `uint16="%d"`, us, uint16(u))
130         check(t, `uint32="%d"`, us, uint32(u))
131         check(t, `uint64="%d"`, us, uint64(u))
132
133         const f = 3.141592
134         const fs = `3.141592`
135         check(t, `float64="%g"`, fs, f)
136         check(t, `float32="%g"`, fs, float32(f))
137         check(t, `float64="%g"`, fs, float64(f))
138 }
139
140 func TestArrayTypes(t *testing.T) {
141         var a0 [10]int
142         check(t, `array="array";`, `array`, a0)
143
144         a1 := [...]int{1, 2, 3}
145         check(t, `array="array";`, `array`, a1)
146         check(t, `array={*}; int="%d";`, `123`, a1)
147         check(t, `array={* / ", "}; int="%d";`, `1, 2, 3`, a1)
148         check(t, `array={* / *}; int="%d";`, `12233`, a1)
149
150         a2 := []interface{}{42, "foo", 3.14}
151         check(t, `array={* / ", "}; interface=*; string="bar"; default="%v";`, `42, bar, 3.14`, a2)
152 }
153
154 func TestChanTypes(t *testing.T) {
155         var c0 chan int
156         check(t, `chan="chan"`, `chan`, c0)
157
158         c1 := make(chan int)
159         go func() { c1 <- 42 }()
160         check(t, `chan="chan"`, `chan`, c1)
161         // check(t, `chan=*`, `42`, c1);  // reflection support for chans incomplete
162 }
163
164 func TestFuncTypes(t *testing.T) {
165         var f0 func() int
166         check(t, `func="func"`, `func`, f0)
167
168         f1 := func() int { return 42 }
169         check(t, `func="func"`, `func`, f1)
170         // check(t, `func=*`, `42`, f1);  // reflection support for funcs incomplete
171 }
172
173 func TestMapTypes(t *testing.T) {
174         var m0 map[string]int
175         check(t, `map="map"`, `map`, m0)
176
177         m1 := map[string]int{}
178         check(t, `map="map"`, `map`, m1)
179         // check(t, `map=*`, ``, m1);  // reflection support for maps incomplete
180 }
181
182 func TestPointerTypes(t *testing.T) {
183         var p0 *int
184         check(t, `ptr="ptr"`, `ptr`, p0)
185         check(t, `ptr=*`, ``, p0)
186         check(t, `ptr=*|"nil"`, `nil`, p0)
187
188         x := 99991
189         p1 := &x
190         check(t, `ptr="ptr"`, `ptr`, p1)
191         check(t, `ptr=*; int="%d"`, `99991`, p1)
192 }
193
194 func TestDefaultRule(t *testing.T) {
195         check(t, `default="%v"`, `42foo3.14`, 42, "foo", 3.14)
196         check(t, `default="%v"; int="%x"`, `abcdef`, 10, 11, 12, 13, 14, 15)
197         check(t, `default="%v"; int="%x"`, `ab**ef`, 10, 11, "**", 14, 15)
198         check(t, `default="%x"; int=@:default`, `abcdef`, 10, 11, 12, 13, 14, 15)
199 }
200
201 func TestGlobalSeparatorRule(t *testing.T) {
202         check(t, `int="%d"; / ="-"`, `1-2-3-4`, 1, 2, 3, 4)
203         check(t, `int="%x%x"; / ="*"`, `aa*aa`, 10, 10)
204 }
205
206 // ----------------------------------------------------------------------------
207 // Formatting of a struct
208
209 type T1 struct {
210         a int
211 }
212
213 const F1 = `datafmt "datafmt";` +
214         `int = "%d";` +
215         `datafmt.T1 = "<" a ">";`
216
217 func TestStruct1(t *testing.T) { check(t, F1, "<42>", T1{42}) }
218
219 // ----------------------------------------------------------------------------
220 // Formatting of a struct with an optional field (ptr)
221
222 type T2 struct {
223         s string
224         p *T1
225 }
226
227 const F2a = F1 +
228         `string = "%s";` +
229         `ptr = *;` +
230         `datafmt.T2 = s ["-" p "-"];`
231
232 const F2b = F1 +
233         `string = "%s";` +
234         `ptr = *;` +
235         `datafmt.T2 = s ("-" p "-" | "empty");`
236
237 func TestStruct2(t *testing.T) {
238         check(t, F2a, "foo", T2{"foo", nil})
239         check(t, F2a, "bar-<17>-", T2{"bar", &T1{17}})
240         check(t, F2b, "fooempty", T2{"foo", nil})
241 }
242
243 // ----------------------------------------------------------------------------
244 // Formatting of a struct with a repetitive field (slice)
245
246 type T3 struct {
247         s string
248         a []int
249 }
250
251 const F3a = `datafmt "datafmt";` +
252         `default = "%v";` +
253         `array = *;` +
254         `datafmt.T3 = s  {" " a a / ","};`
255
256 const F3b = `datafmt "datafmt";` +
257         `int = "%d";` +
258         `string = "%s";` +
259         `array = *;` +
260         `nil = ;` +
261         `empty = *:nil;` +
262         `datafmt.T3 = s [a:empty ": " {a / "-"}]`
263
264 func TestStruct3(t *testing.T) {
265         check(t, F3a, "foo", T3{"foo", nil})
266         check(t, F3a, "foo 00, 11, 22", T3{"foo", []int{0, 1, 2}})
267         check(t, F3b, "bar", T3{"bar", nil})
268         check(t, F3b, "bal: 2-3-5", T3{"bal", []int{2, 3, 5}})
269 }
270
271 // ----------------------------------------------------------------------------
272 // Formatting of a struct with alternative field
273
274 type T4 struct {
275         x *int
276         a []int
277 }
278
279 const F4a = `datafmt "datafmt";` +
280         `int = "%d";` +
281         `ptr = *;` +
282         `array = *;` +
283         `nil = ;` +
284         `empty = *:nil;` +
285         `datafmt.T4 = "<" (x:empty x | "-") ">" `
286
287 const F4b = `datafmt "datafmt";` +
288         `int = "%d";` +
289         `ptr = *;` +
290         `array = *;` +
291         `nil = ;` +
292         `empty = *:nil;` +
293         `datafmt.T4 = "<" (a:empty {a / ", "} | "-") ">" `
294
295 func TestStruct4(t *testing.T) {
296         x := 7
297         check(t, F4a, "<->", T4{nil, nil})
298         check(t, F4a, "<7>", T4{&x, nil})
299         check(t, F4b, "<->", T4{nil, nil})
300         check(t, F4b, "<2, 3, 7>", T4{nil, []int{2, 3, 7}})
301 }
302
303 // ----------------------------------------------------------------------------
304 // Formatting a struct (documentation example)
305
306 type Point struct {
307         name string
308         x, y int
309 }
310
311 const FPoint = `datafmt "datafmt";` +
312         `int = "%d";` +
313         `hexInt = "0x%x";` +
314         `string = "---%s---";` +
315         `datafmt.Point = name "{" x ", " y:hexInt "}";`
316
317 func TestStructPoint(t *testing.T) {
318         p := Point{"foo", 3, 15}
319         check(t, FPoint, "---foo---{3, 0xf}", p)
320 }
321
322 // ----------------------------------------------------------------------------
323 // Formatting a slice (documentation example)
324
325 const FSlice = `int = "%b";` +
326         `array = { * / ", " }`
327
328 func TestSlice(t *testing.T) { check(t, FSlice, "10, 11, 101, 111", []int{2, 3, 5, 7}) }
329
330 // TODO add more tests