Reverting merge from trunk
[official-gcc.git] / gcc / testsuite / go.test / test / cmp.go
blob5be64561d597c5d2fcf012d1e57ae7b5134de953
1 // run
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.
7 // Test equality and inequality operations.
9 package main
11 import (
12 "os"
13 "unsafe"
16 var global bool
18 func use(b bool) { global = b }
20 func stringptr(s string) uintptr { return *(*uintptr)(unsafe.Pointer(&s)) }
22 func isfalse(b bool) {
23 if b {
24 // stack will explain where
25 panic("wanted false, got true")
29 func istrue(b bool) {
30 if !b {
31 // stack will explain where
32 panic("wanted true, got false")
36 type T *int
38 func main() {
39 var a []int
40 var b map[string]int
42 var c string = "hello"
43 var d string = "hel" // try to get different pointer
44 d = d + "lo"
46 // exp/ssa/interp can't handle unsafe.Pointer.
47 if os.Getenv("GOSSAINTERP") != "" {
48 if stringptr(c) == stringptr(d) {
49 panic("compiler too smart -- got same string")
53 var e = make(chan int)
55 var ia interface{} = a
56 var ib interface{} = b
57 var ic interface{} = c
58 var id interface{} = d
59 var ie interface{} = e
61 // these comparisons are okay because
62 // string compare is okay and the others
63 // are comparisons where the types differ.
64 isfalse(ia == ib)
65 isfalse(ia == ic)
66 isfalse(ia == id)
67 isfalse(ib == ic)
68 isfalse(ib == id)
69 istrue(ic == id)
70 istrue(ie == ie)
72 istrue(ia != ib)
73 istrue(ia != ic)
74 istrue(ia != id)
75 istrue(ib != ic)
76 istrue(ib != id)
77 isfalse(ic != id)
78 isfalse(ie != ie)
80 // these are not okay, because there is no comparison on slices or maps.
81 //isfalse(a == ib)
82 //isfalse(a == ic)
83 //isfalse(a == id)
84 //isfalse(b == ic)
85 //isfalse(b == id)
87 istrue(c == id)
88 istrue(e == ie)
90 //isfalse(ia == b)
91 isfalse(ia == c)
92 isfalse(ia == d)
93 isfalse(ib == c)
94 isfalse(ib == d)
95 istrue(ic == d)
96 istrue(ie == e)
98 //istrue(a != ib)
99 //istrue(a != ic)
100 //istrue(a != id)
101 //istrue(b != ic)
102 //istrue(b != id)
103 isfalse(c != id)
104 isfalse(e != ie)
106 //istrue(ia != b)
107 istrue(ia != c)
108 istrue(ia != d)
109 istrue(ib != c)
110 istrue(ib != d)
111 isfalse(ic != d)
112 isfalse(ie != e)
114 // 6g used to let this go through as true.
115 var g uint64 = 123
116 var h int64 = 123
117 var ig interface{} = g
118 var ih interface{} = h
119 isfalse(ig == ih)
120 istrue(ig != ih)
122 // map of interface should use == on interface values,
123 // not memory.
124 var m = make(map[interface{}]int)
125 m[ic] = 1
126 m[id] = 2
127 if m[c] != 2 {
128 println("m[c] = ", m[c])
129 panic("bad m[c]")
132 // non-interface comparisons
134 c := make(chan int)
135 c1 := (<-chan int)(c)
136 c2 := (chan<- int)(c)
137 istrue(c == c1)
138 istrue(c == c2)
139 istrue(c1 == c)
140 istrue(c2 == c)
142 isfalse(c != c1)
143 isfalse(c != c2)
144 isfalse(c1 != c)
145 isfalse(c2 != c)
147 d := make(chan int)
148 isfalse(c == d)
149 isfalse(d == c)
150 isfalse(d == c1)
151 isfalse(d == c2)
152 isfalse(c1 == d)
153 isfalse(c2 == d)
155 istrue(c != d)
156 istrue(d != c)
157 istrue(d != c1)
158 istrue(d != c2)
159 istrue(c1 != d)
160 istrue(c2 != d)
163 // named types vs not
165 var x = new(int)
166 var y T
167 var z T = x
169 isfalse(x == y)
170 istrue(x == z)
171 isfalse(y == z)
173 isfalse(y == x)
174 istrue(z == x)
175 isfalse(z == y)
177 istrue(x != y)
178 isfalse(x != z)
179 istrue(y != z)
181 istrue(y != x)
182 isfalse(z != x)
183 istrue(z != y)
186 // structs
188 var x = struct {
189 x int
190 y string
191 }{1, "hi"}
192 var y = struct {
193 x int
194 y string
195 }{2, "bye"}
196 var z = struct {
197 x int
198 y string
199 }{1, "hi"}
201 isfalse(x == y)
202 isfalse(y == x)
203 isfalse(y == z)
204 isfalse(z == y)
205 istrue(x == z)
206 istrue(z == x)
208 istrue(x != y)
209 istrue(y != x)
210 istrue(y != z)
211 istrue(z != y)
212 isfalse(x != z)
213 isfalse(z != x)
215 var m = make(map[struct {
216 x int
217 y string
218 }]int)
219 m[x] = 10
220 m[y] = 20
221 m[z] = 30
222 istrue(m[x] == 30)
223 istrue(m[y] == 20)
224 istrue(m[z] == 30)
225 istrue(m[x] != 10)
226 isfalse(m[x] != 30)
227 isfalse(m[y] != 20)
228 isfalse(m[z] != 30)
229 isfalse(m[x] == 10)
231 var m1 = make(map[struct {
232 x int
233 y string
234 }]struct {
235 x int
236 y string
238 m1[x] = x
239 m1[y] = y
240 m1[z] = z
241 istrue(m1[x] == z)
242 istrue(m1[y] == y)
243 istrue(m1[z] == z)
244 istrue(m1[x] == x)
245 isfalse(m1[x] != z)
246 isfalse(m1[y] != y)
247 isfalse(m1[z] != z)
248 isfalse(m1[x] != x)
250 var ix, iy, iz interface{} = x, y, z
252 isfalse(ix == iy)
253 isfalse(iy == ix)
254 isfalse(iy == iz)
255 isfalse(iz == iy)
256 istrue(ix == iz)
257 istrue(iz == ix)
259 isfalse(x == iy)
260 isfalse(y == ix)
261 isfalse(y == iz)
262 isfalse(z == iy)
263 istrue(x == iz)
264 istrue(z == ix)
266 isfalse(ix == y)
267 isfalse(iy == x)
268 isfalse(iy == z)
269 isfalse(iz == y)
270 istrue(ix == z)
271 istrue(iz == x)
273 istrue(ix != iy)
274 istrue(iy != ix)
275 istrue(iy != iz)
276 istrue(iz != iy)
277 isfalse(ix != iz)
278 isfalse(iz != ix)
280 istrue(x != iy)
281 istrue(y != ix)
282 istrue(y != iz)
283 istrue(z != iy)
284 isfalse(x != iz)
285 isfalse(z != ix)
287 istrue(ix != y)
288 istrue(iy != x)
289 istrue(iy != z)
290 istrue(iz != y)
291 isfalse(ix != z)
292 isfalse(iz != x)
295 // structs with _ fields
297 var x = struct {
298 x int
299 _ []int
300 y float64
301 _ float64
302 z int
304 x: 1, y: 2, z: 3,
306 var ix interface{} = x
308 istrue(x == x)
309 istrue(x == ix)
310 istrue(ix == x)
311 istrue(ix == ix)
314 // arrays
316 var x = [2]string{"1", "hi"}
317 var y = [2]string{"2", "bye"}
318 var z = [2]string{"1", "hi"}
320 isfalse(x == y)
321 isfalse(y == x)
322 isfalse(y == z)
323 isfalse(z == y)
324 istrue(x == z)
325 istrue(z == x)
327 istrue(x != y)
328 istrue(y != x)
329 istrue(y != z)
330 istrue(z != y)
331 isfalse(x != z)
332 isfalse(z != x)
334 var m = make(map[[2]string]int)
335 m[x] = 10
336 m[y] = 20
337 m[z] = 30
338 istrue(m[x] == 30)
339 istrue(m[y] == 20)
340 istrue(m[z] == 30)
341 isfalse(m[x] != 30)
342 isfalse(m[y] != 20)
343 isfalse(m[z] != 30)
345 var ix, iy, iz interface{} = x, y, z
347 isfalse(ix == iy)
348 isfalse(iy == ix)
349 isfalse(iy == iz)
350 isfalse(iz == iy)
351 istrue(ix == iz)
352 istrue(iz == ix)
354 isfalse(x == iy)
355 isfalse(y == ix)
356 isfalse(y == iz)
357 isfalse(z == iy)
358 istrue(x == iz)
359 istrue(z == ix)
361 isfalse(ix == y)
362 isfalse(iy == x)
363 isfalse(iy == z)
364 isfalse(iz == y)
365 istrue(ix == z)
366 istrue(iz == x)
368 istrue(ix != iy)
369 istrue(iy != ix)
370 istrue(iy != iz)
371 istrue(iz != iy)
372 isfalse(ix != iz)
373 isfalse(iz != ix)
375 istrue(x != iy)
376 istrue(y != ix)
377 istrue(y != iz)
378 istrue(z != iy)
379 isfalse(x != iz)
380 isfalse(z != ix)
382 istrue(ix != y)
383 istrue(iy != x)
384 istrue(iy != z)
385 istrue(iz != y)
386 isfalse(ix != z)
387 isfalse(iz != x)
390 shouldPanic(p1)
391 shouldPanic(p2)
392 shouldPanic(p3)
393 shouldPanic(p4)
396 func p1() {
397 var a []int
398 var ia interface{} = a
399 use(ia == ia)
402 func p2() {
403 var b []int
404 var ib interface{} = b
405 use(ib == ib)
408 func p3() {
409 var a []int
410 var ia interface{} = a
411 var m = make(map[interface{}]int)
412 m[ia] = 1
415 func p4() {
416 var b []int
417 var ib interface{} = b
418 var m = make(map[interface{}]int)
419 m[ib] = 1
422 func shouldPanic(f func()) {
423 defer func() {
424 if recover() == nil {
425 panic("function should panic")