LEFT | RIGHT |
(no file at all) | |
1 // run | 1 // run |
2 | 2 |
3 // Copyright 2009 The Go Authors. All rights reserved. | 3 // Copyright 2009 The Go Authors. All rights reserved. |
4 // Use of this source code is governed by a BSD-style | 4 // Use of this source code is governed by a BSD-style |
5 // license that can be found in the LICENSE file. | 5 // license that can be found in the LICENSE file. |
6 | 6 |
7 // Test equality and inequality operations. | 7 // Test equality and inequality operations. |
8 | 8 |
9 package main | 9 package main |
10 | 10 |
11 import "unsafe" | 11 import ( |
| 12 » "os" |
| 13 » "unsafe" |
| 14 ) |
12 | 15 |
13 var global bool | 16 var global bool |
| 17 |
14 func use(b bool) { global = b } | 18 func use(b bool) { global = b } |
15 | 19 |
16 func stringptr(s string) uintptr { return *(*uintptr)(unsafe.Pointer(&s)) } | 20 func stringptr(s string) uintptr { return *(*uintptr)(unsafe.Pointer(&s)) } |
17 | 21 |
18 func isfalse(b bool) { | 22 func isfalse(b bool) { |
19 if b { | 23 if b { |
20 // stack will explain where | 24 // stack will explain where |
21 panic("wanted false, got true") | 25 panic("wanted false, got true") |
22 } | 26 } |
23 } | 27 } |
24 | 28 |
25 func istrue(b bool) { | 29 func istrue(b bool) { |
26 if !b { | 30 if !b { |
27 // stack will explain where | 31 // stack will explain where |
28 panic("wanted true, got false") | 32 panic("wanted true, got false") |
29 } | 33 } |
30 } | 34 } |
31 | 35 |
32 type T *int | 36 type T *int |
33 | 37 |
34 func main() { | 38 func main() { |
35 var a []int | 39 var a []int |
36 var b map[string]int | 40 var b map[string]int |
37 | 41 |
38 var c string = "hello" | 42 var c string = "hello" |
39 var d string = "hel" // try to get different pointer | 43 var d string = "hel" // try to get different pointer |
40 d = d + "lo" | 44 d = d + "lo" |
41 » if stringptr(c) == stringptr(d) { | 45 |
42 » » panic("compiler too smart -- got same string") | 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") |
| 50 » » } |
43 } | 51 } |
44 | 52 |
45 var e = make(chan int) | 53 var e = make(chan int) |
46 | 54 |
47 var ia interface{} = a | 55 var ia interface{} = a |
48 var ib interface{} = b | 56 var ib interface{} = b |
49 var ic interface{} = c | 57 var ic interface{} = c |
50 var id interface{} = d | 58 var id interface{} = d |
51 var ie interface{} = e | 59 var ie interface{} = e |
52 | 60 |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 isfalse(x != iz) | 284 isfalse(x != iz) |
277 isfalse(z != ix) | 285 isfalse(z != ix) |
278 | 286 |
279 istrue(ix != y) | 287 istrue(ix != y) |
280 istrue(iy != x) | 288 istrue(iy != x) |
281 istrue(iy != z) | 289 istrue(iy != z) |
282 istrue(iz != y) | 290 istrue(iz != y) |
283 isfalse(ix != z) | 291 isfalse(ix != z) |
284 isfalse(iz != x) | 292 isfalse(iz != x) |
285 } | 293 } |
286 » | 294 |
287 // structs with _ fields | 295 // structs with _ fields |
288 { | 296 { |
289 var x = struct { | 297 var x = struct { |
290 x int | 298 x int |
291 _ []int | 299 _ []int |
292 y float64 | 300 y float64 |
293 _ float64 | 301 _ float64 |
294 z int | 302 z int |
295 }{ | 303 }{ |
296 x: 1, y: 2, z: 3, | 304 x: 1, y: 2, z: 3, |
297 } | 305 } |
298 var ix interface{} = x | 306 var ix interface{} = x |
299 » » | 307 |
300 istrue(x == x) | 308 istrue(x == x) |
301 istrue(x == ix) | 309 istrue(x == ix) |
302 istrue(ix == x) | 310 istrue(ix == x) |
303 istrue(ix == ix) | 311 istrue(ix == ix) |
304 } | 312 } |
305 | 313 |
306 // arrays | 314 // arrays |
307 { | 315 { |
308 var x = [2]string{"1", "hi"} | 316 var x = [2]string{"1", "hi"} |
309 var y = [2]string{"2", "bye"} | 317 var y = [2]string{"2", "bye"} |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 } | 420 } |
413 | 421 |
414 func shouldPanic(f func()) { | 422 func shouldPanic(f func()) { |
415 defer func() { | 423 defer func() { |
416 if recover() == nil { | 424 if recover() == nil { |
417 panic("function should panic") | 425 panic("function should panic") |
418 } | 426 } |
419 }() | 427 }() |
420 f() | 428 f() |
421 } | 429 } |
LEFT | RIGHT |