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