]> Cypherpunks.ru repositories - gostls13.git/blob - test/cmp.go
test: invert incorrect condition.
[gostls13.git] / test / cmp.go
1 // run
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 // Test equality and inequality operations.
8
9 package main
10
11 import (
12         "os"
13         "unsafe"
14 )
15
16 var global bool
17
18 func use(b bool) { global = b }
19
20 func stringptr(s string) uintptr { return *(*uintptr)(unsafe.Pointer(&s)) }
21
22 func isfalse(b bool) {
23         if b {
24                 // stack will explain where
25                 panic("wanted false, got true")
26         }
27 }
28
29 func istrue(b bool) {
30         if !b {
31                 // stack will explain where
32                 panic("wanted true, got false")
33         }
34 }
35
36 type T *int
37
38 func main() {
39         var a []int
40         var b map[string]int
41
42         var c string = "hello"
43         var d string = "hel" // try to get different pointer
44         d = d + "lo"
45
46         // go.tools/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")
50                 }
51         }
52
53         var e = make(chan int)
54
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
60
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)
71
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)
79
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)
86
87         istrue(c == id)
88         istrue(e == ie)
89
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)
97
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)
105
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)
113
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)
121
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]")
130         }
131
132         // non-interface comparisons
133         {
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)
141
142                 isfalse(c != c1)
143                 isfalse(c != c2)
144                 isfalse(c1 != c)
145                 isfalse(c2 != c)
146
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)
154
155                 istrue(c != d)
156                 istrue(d != c)
157                 istrue(d != c1)
158                 istrue(d != c2)
159                 istrue(c1 != d)
160                 istrue(c2 != d)
161         }
162
163         // named types vs not
164         {
165                 var x = new(int)
166                 var y T
167                 var z T = x
168
169                 isfalse(x == y)
170                 istrue(x == z)
171                 isfalse(y == z)
172
173                 isfalse(y == x)
174                 istrue(z == x)
175                 isfalse(z == y)
176
177                 istrue(x != y)
178                 isfalse(x != z)
179                 istrue(y != z)
180
181                 istrue(y != x)
182                 isfalse(z != x)
183                 istrue(z != y)
184         }
185
186         // structs
187         {
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"}
200
201                 isfalse(x == y)
202                 isfalse(y == x)
203                 isfalse(y == z)
204                 isfalse(z == y)
205                 istrue(x == z)
206                 istrue(z == x)
207
208                 istrue(x != y)
209                 istrue(y != x)
210                 istrue(y != z)
211                 istrue(z != y)
212                 isfalse(x != z)
213                 isfalse(z != x)
214
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)
230
231                 var m1 = make(map[struct {
232                         x int
233                         y string
234                 }]struct {
235                         x int
236                         y string
237                 })
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)
249
250                 var ix, iy, iz interface{} = x, y, z
251
252                 isfalse(ix == iy)
253                 isfalse(iy == ix)
254                 isfalse(iy == iz)
255                 isfalse(iz == iy)
256                 istrue(ix == iz)
257                 istrue(iz == ix)
258
259                 isfalse(x == iy)
260                 isfalse(y == ix)
261                 isfalse(y == iz)
262                 isfalse(z == iy)
263                 istrue(x == iz)
264                 istrue(z == ix)
265
266                 isfalse(ix == y)
267                 isfalse(iy == x)
268                 isfalse(iy == z)
269                 isfalse(iz == y)
270                 istrue(ix == z)
271                 istrue(iz == x)
272
273                 istrue(ix != iy)
274                 istrue(iy != ix)
275                 istrue(iy != iz)
276                 istrue(iz != iy)
277                 isfalse(ix != iz)
278                 isfalse(iz != ix)
279
280                 istrue(x != iy)
281                 istrue(y != ix)
282                 istrue(y != iz)
283                 istrue(z != iy)
284                 isfalse(x != iz)
285                 isfalse(z != ix)
286
287                 istrue(ix != y)
288                 istrue(iy != x)
289                 istrue(iy != z)
290                 istrue(iz != y)
291                 isfalse(ix != z)
292                 isfalse(iz != x)
293         }
294
295         // structs with _ fields
296         {
297                 var x = struct {
298                         x int
299                         _ string
300                         y float64
301                         _ float64
302                         z int
303                 }{
304                         x: 1, y: 2, z: 3,
305                 }
306                 var ix interface{} = x
307
308                 istrue(x == x)
309                 istrue(x == ix)
310                 istrue(ix == x)
311                 istrue(ix == ix)
312         }
313
314         // arrays
315         {
316                 var x = [2]string{"1", "hi"}
317                 var y = [2]string{"2", "bye"}
318                 var z = [2]string{"1", "hi"}
319
320                 isfalse(x == y)
321                 isfalse(y == x)
322                 isfalse(y == z)
323                 isfalse(z == y)
324                 istrue(x == z)
325                 istrue(z == x)
326
327                 istrue(x != y)
328                 istrue(y != x)
329                 istrue(y != z)
330                 istrue(z != y)
331                 isfalse(x != z)
332                 isfalse(z != x)
333
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)
344
345                 var ix, iy, iz interface{} = x, y, z
346
347                 isfalse(ix == iy)
348                 isfalse(iy == ix)
349                 isfalse(iy == iz)
350                 isfalse(iz == iy)
351                 istrue(ix == iz)
352                 istrue(iz == ix)
353
354                 isfalse(x == iy)
355                 isfalse(y == ix)
356                 isfalse(y == iz)
357                 isfalse(z == iy)
358                 istrue(x == iz)
359                 istrue(z == ix)
360
361                 isfalse(ix == y)
362                 isfalse(iy == x)
363                 isfalse(iy == z)
364                 isfalse(iz == y)
365                 istrue(ix == z)
366                 istrue(iz == x)
367
368                 istrue(ix != iy)
369                 istrue(iy != ix)
370                 istrue(iy != iz)
371                 istrue(iz != iy)
372                 isfalse(ix != iz)
373                 isfalse(iz != ix)
374
375                 istrue(x != iy)
376                 istrue(y != ix)
377                 istrue(y != iz)
378                 istrue(z != iy)
379                 isfalse(x != iz)
380                 isfalse(z != ix)
381
382                 istrue(ix != y)
383                 istrue(iy != x)
384                 istrue(iy != z)
385                 istrue(iz != y)
386                 isfalse(ix != z)
387                 isfalse(iz != x)
388         }
389
390         shouldPanic(p1)
391         shouldPanic(p2)
392         shouldPanic(p3)
393         shouldPanic(p4)
394 }
395
396 func p1() {
397         var a []int
398         var ia interface{} = a
399         use(ia == ia)
400 }
401
402 func p2() {
403         var b []int
404         var ib interface{} = b
405         use(ib == ib)
406 }
407
408 func p3() {
409         var a []int
410         var ia interface{} = a
411         var m = make(map[interface{}]int)
412         m[ia] = 1
413 }
414
415 func p4() {
416         var b []int
417         var ib interface{} = b
418         var m = make(map[interface{}]int)
419         m[ib] = 1
420 }
421
422 func shouldPanic(f func()) {
423         defer func() {
424                 if recover() == nil {
425                         panic("function should panic")
426                 }
427         }()
428         f()
429 }