OSDN Git Service

Update to current Go testsuite.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / go.test / test / cmp.go
1 // $G $D/$F.go && $L $F.$A && ./$A.out
2
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 package main
8
9 import "unsafe"
10
11 var global bool
12 func use(b bool) { global = b }
13
14 func stringptr(s string) uintptr { return *(*uintptr)(unsafe.Pointer(&s)) }
15
16 func isfalse(b bool) {
17         if b {
18                 // stack will explain where
19                 panic("wanted false, got true")
20         }
21 }
22
23 func istrue(b bool) {
24         if !b {
25                 // stack will explain where
26                 panic("wanted true, got false")
27         }
28 }
29
30 type T *int
31
32 func main() {
33         var a []int
34         var b map[string]int
35
36         var c string = "hello"
37         var d string = "hel" // try to get different pointer
38         d = d + "lo"
39         if stringptr(c) == stringptr(d) {
40                 panic("compiler too smart -- got same string")
41         }
42
43         var e = make(chan int)
44
45         var ia interface{} = a
46         var ib interface{} = b
47         var ic interface{} = c
48         var id interface{} = d
49         var ie interface{} = e
50
51         // these comparisons are okay because
52         // string compare is okay and the others
53         // are comparisons where the types differ.
54         isfalse(ia == ib)
55         isfalse(ia == ic)
56         isfalse(ia == id)
57         isfalse(ib == ic)
58         isfalse(ib == id)
59         istrue(ic == id)
60         istrue(ie == ie)
61
62         istrue(ia != ib)
63         istrue(ia != ic)
64         istrue(ia != id)
65         istrue(ib != ic)
66         istrue(ib != id)
67         isfalse(ic != id)
68         isfalse(ie != ie)
69
70         // these are not okay, because there is no comparison on slices or maps.
71         //isfalse(a == ib)
72         //isfalse(a == ic)
73         //isfalse(a == id)
74         //isfalse(b == ic)
75         //isfalse(b == id)
76
77         istrue(c == id)
78         istrue(e == ie)
79
80         //isfalse(ia == b)
81         isfalse(ia == c)
82         isfalse(ia == d)
83         isfalse(ib == c)
84         isfalse(ib == d)
85         istrue(ic == d)
86         istrue(ie == e)
87
88         //istrue(a != ib)
89         //istrue(a != ic)
90         //istrue(a != id)
91         //istrue(b != ic)
92         //istrue(b != id)
93         isfalse(c != id)
94         isfalse(e != ie)
95
96         //istrue(ia != b)
97         istrue(ia != c)
98         istrue(ia != d)
99         istrue(ib != c)
100         istrue(ib != d)
101         isfalse(ic != d)
102         isfalse(ie != e)
103
104         // 6g used to let this go through as true.
105         var g uint64 = 123
106         var h int64 = 123
107         var ig interface{} = g
108         var ih interface{} = h
109         isfalse(ig == ih)
110         istrue(ig != ih)
111
112         // map of interface should use == on interface values,
113         // not memory.
114         var m = make(map[interface{}]int)
115         m[ic] = 1
116         m[id] = 2
117         if m[c] != 2 {
118                 println("m[c] = ", m[c])
119                 panic("bad m[c]")
120         }
121
122         // non-interface comparisons
123         {
124                 c := make(chan int)
125                 c1 := (<-chan int)(c)
126                 c2 := (chan<- int)(c)
127                 istrue(c == c1)
128                 istrue(c == c2)
129                 istrue(c1 == c)
130                 istrue(c2 == c)
131
132                 isfalse(c != c1)
133                 isfalse(c != c2)
134                 isfalse(c1 != c)
135                 isfalse(c2 != c)
136
137                 d := make(chan int)
138                 isfalse(c == d)
139                 isfalse(d == c)
140                 isfalse(d == c1)
141                 isfalse(d == c2)
142                 isfalse(c1 == d)
143                 isfalse(c2 == d)
144
145                 istrue(c != d)
146                 istrue(d != c)
147                 istrue(d != c1)
148                 istrue(d != c2)
149                 istrue(c1 != d)
150                 istrue(c2 != d)
151         }
152
153         // named types vs not
154         {
155                 var x = new(int)
156                 var y T
157                 var z T = x
158
159                 isfalse(x == y)
160                 istrue(x == z)
161                 isfalse(y == z)
162
163                 isfalse(y == x)
164                 istrue(z == x)
165                 isfalse(z == y)
166
167                 istrue(x != y)
168                 isfalse(x != z)
169                 istrue(y != z)
170
171                 istrue(y != x)
172                 isfalse(z != x)
173                 istrue(z != y)
174         }
175
176         // structs
177         {
178                 var x = struct {
179                         x int
180                         y string
181                 }{1, "hi"}
182                 var y = struct {
183                         x int
184                         y string
185                 }{2, "bye"}
186                 var z = struct {
187                         x int
188                         y string
189                 }{1, "hi"}
190
191                 isfalse(x == y)
192                 isfalse(y == x)
193                 isfalse(y == z)
194                 isfalse(z == y)
195                 istrue(x == z)
196                 istrue(z == x)
197
198                 istrue(x != y)
199                 istrue(y != x)
200                 istrue(y != z)
201                 istrue(z != y)
202                 isfalse(x != z)
203                 isfalse(z != x)
204
205                 var m = make(map[struct {
206                         x int
207                         y string
208                 }]int)
209                 m[x] = 10
210                 m[y] = 20
211                 m[z] = 30
212                 istrue(m[x] == 30)
213                 istrue(m[y] == 20)
214                 istrue(m[z] == 30)
215                 istrue(m[x] != 10)
216                 isfalse(m[x] != 30)
217                 isfalse(m[y] != 20)
218                 isfalse(m[z] != 30)
219                 isfalse(m[x] == 10)
220
221                 var m1 = make(map[struct {
222                         x int
223                         y string
224                 }]struct {
225                         x int
226                         y string
227                 })
228                 m1[x] = x
229                 m1[y] = y
230                 m1[z] = z
231                 istrue(m1[x] == z)
232                 istrue(m1[y] == y)
233                 istrue(m1[z] == z)
234                 istrue(m1[x] == x)
235                 isfalse(m1[x] != z)
236                 isfalse(m1[y] != y)
237                 isfalse(m1[z] != z)
238                 isfalse(m1[x] != x)
239
240                 var ix, iy, iz interface{} = x, y, z
241
242                 isfalse(ix == iy)
243                 isfalse(iy == ix)
244                 isfalse(iy == iz)
245                 isfalse(iz == iy)
246                 istrue(ix == iz)
247                 istrue(iz == ix)
248
249                 isfalse(x == iy)
250                 isfalse(y == ix)
251                 isfalse(y == iz)
252                 isfalse(z == iy)
253                 istrue(x == iz)
254                 istrue(z == ix)
255
256                 isfalse(ix == y)
257                 isfalse(iy == x)
258                 isfalse(iy == z)
259                 isfalse(iz == y)
260                 istrue(ix == z)
261                 istrue(iz == x)
262
263                 istrue(ix != iy)
264                 istrue(iy != ix)
265                 istrue(iy != iz)
266                 istrue(iz != iy)
267                 isfalse(ix != iz)
268                 isfalse(iz != ix)
269
270                 istrue(x != iy)
271                 istrue(y != ix)
272                 istrue(y != iz)
273                 istrue(z != iy)
274                 isfalse(x != iz)
275                 isfalse(z != ix)
276
277                 istrue(ix != y)
278                 istrue(iy != x)
279                 istrue(iy != z)
280                 istrue(iz != y)
281                 isfalse(ix != z)
282                 isfalse(iz != x)
283         }
284
285         // arrays
286         {
287                 var x = [2]string{"1", "hi"}
288                 var y = [2]string{"2", "bye"}
289                 var z = [2]string{"1", "hi"}
290
291                 isfalse(x == y)
292                 isfalse(y == x)
293                 isfalse(y == z)
294                 isfalse(z == y)
295                 istrue(x == z)
296                 istrue(z == x)
297
298                 istrue(x != y)
299                 istrue(y != x)
300                 istrue(y != z)
301                 istrue(z != y)
302                 isfalse(x != z)
303                 isfalse(z != x)
304
305                 var m = make(map[[2]string]int)
306                 m[x] = 10
307                 m[y] = 20
308                 m[z] = 30
309                 istrue(m[x] == 30)
310                 istrue(m[y] == 20)
311                 istrue(m[z] == 30)
312                 isfalse(m[x] != 30)
313                 isfalse(m[y] != 20)
314                 isfalse(m[z] != 30)
315
316                 var ix, iy, iz interface{} = x, y, z
317
318                 isfalse(ix == iy)
319                 isfalse(iy == ix)
320                 isfalse(iy == iz)
321                 isfalse(iz == iy)
322                 istrue(ix == iz)
323                 istrue(iz == ix)
324
325                 isfalse(x == iy)
326                 isfalse(y == ix)
327                 isfalse(y == iz)
328                 isfalse(z == iy)
329                 istrue(x == iz)
330                 istrue(z == ix)
331
332                 isfalse(ix == y)
333                 isfalse(iy == x)
334                 isfalse(iy == z)
335                 isfalse(iz == y)
336                 istrue(ix == z)
337                 istrue(iz == x)
338
339                 istrue(ix != iy)
340                 istrue(iy != ix)
341                 istrue(iy != iz)
342                 istrue(iz != iy)
343                 isfalse(ix != iz)
344                 isfalse(iz != ix)
345
346                 istrue(x != iy)
347                 istrue(y != ix)
348                 istrue(y != iz)
349                 istrue(z != iy)
350                 isfalse(x != iz)
351                 isfalse(z != ix)
352
353                 istrue(ix != y)
354                 istrue(iy != x)
355                 istrue(iy != z)
356                 istrue(iz != y)
357                 isfalse(ix != z)
358                 isfalse(iz != x)
359         }
360
361         shouldPanic(p1)
362         shouldPanic(p2)
363         shouldPanic(p3)
364         shouldPanic(p4)
365 }
366
367 func p1() {
368         var a []int
369         var ia interface{} = a
370         use(ia == ia)
371 }
372
373 func p2() {
374         var b []int
375         var ib interface{} = b
376         use(ib == ib)
377 }
378
379 func p3() {
380         var a []int
381         var ia interface{} = a
382         var m = make(map[interface{}]int)
383         m[ia] = 1
384 }
385
386 func p4() {
387         var b []int
388         var ib interface{} = b
389         var m = make(map[interface{}]int)
390         m[ib] = 1
391 }
392
393 func shouldPanic(f func()) {
394         defer func() {
395                 if recover() == nil {
396                         panic("function should panic")
397                 }
398         }()
399         f()
400 }