]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/go.test/test/cmp1.go
Add Go frontend, libgo library, and Go testsuite.
[thirdparty/gcc.git] / gcc / testsuite / go.test / test / cmp1.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 func main() {
30 var a []int
31 var b map[string]int
32
33 var c string = "hello"
34 var d string = "hel" // try to get different pointer
35 d = d + "lo"
36 if stringptr(c) == stringptr(d) {
37 panic("compiler too smart -- got same string")
38 }
39
40 var e = make(chan int)
41
42 var ia interface{} = a
43 var ib interface{} = b
44 var ic interface{} = c
45 var id interface{} = d
46 var ie interface{} = e
47
48 // these comparisons are okay because
49 // string compare is okay and the others
50 // are comparisons where the types differ.
51 isfalse(ia == ib)
52 isfalse(ia == ic)
53 isfalse(ia == id)
54 isfalse(ib == ic)
55 isfalse(ib == id)
56 istrue(ic == id)
57 istrue(ie == ie)
58
59 // 6g used to let this go through as true.
60 var g uint64 = 123
61 var h int64 = 123
62 var ig interface{} = g
63 var ih interface{} = h
64 isfalse(ig == ih)
65
66 // map of interface should use == on interface values,
67 // not memory.
68 // TODO: should m[c], m[d] be valid here?
69 var m = make(map[interface{}]int)
70 m[ic] = 1
71 m[id] = 2
72 if m[ic] != 2 {
73 println("m[ic] = ", m[ic])
74 panic("bad m[ic]")
75 }
76 }