summaryrefslogtreecommitdiff
path: root/gcc/testsuite/go.test/test/typeswitch1.go
diff options
context:
space:
mode:
authorupstream source tree <ports@midipix.org>2015-03-15 20:14:05 -0400
committerupstream source tree <ports@midipix.org>2015-03-15 20:14:05 -0400
commit554fd8c5195424bdbcabf5de30fdc183aba391bd (patch)
tree976dc5ab7fddf506dadce60ae936f43f58787092 /gcc/testsuite/go.test/test/typeswitch1.go
downloadcbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.bz2
cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.xz
obtained gcc-4.6.4.tar.bz2 from upstream website;upstream
verified gcc-4.6.4.tar.bz2.sig; imported gcc-4.6.4 source tree from verified upstream tarball. downloading a git-generated archive based on the 'upstream' tag should provide you with a source tree that is binary identical to the one extracted from the above tarball. if you have obtained the source via the command 'git clone', however, do note that line-endings of files in your working directory might differ from line-endings of the respective files in the upstream repository.
Diffstat (limited to 'gcc/testsuite/go.test/test/typeswitch1.go')
-rw-r--r--gcc/testsuite/go.test/test/typeswitch1.go83
1 files changed, 83 insertions, 0 deletions
diff --git a/gcc/testsuite/go.test/test/typeswitch1.go b/gcc/testsuite/go.test/test/typeswitch1.go
new file mode 100644
index 000000000..9613b166f
--- /dev/null
+++ b/gcc/testsuite/go.test/test/typeswitch1.go
@@ -0,0 +1,83 @@
+// $G $D/$F.go && $L $F.$A && ./$A.out
+
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import "fmt"
+
+const (
+ a = iota
+ b
+ c
+ d
+ e
+)
+
+var x = []int{1, 2, 3}
+
+func f(x int, len *byte) {
+ *len = byte(x)
+}
+
+func whatis(x interface{}) string {
+ switch xx := x.(type) {
+ default:
+ return fmt.Sprint("default ", xx)
+ case int, int8, int16, int32:
+ return fmt.Sprint("signed ", xx)
+ case int64:
+ return fmt.Sprint("signed64 ", int64(xx))
+ case uint, uint8, uint16, uint32:
+ return fmt.Sprint("unsigned ", xx)
+ case uint64:
+ return fmt.Sprint("unsigned64 ", uint64(xx))
+ case nil:
+ return fmt.Sprint("nil ", xx)
+ }
+ panic("not reached")
+}
+
+func whatis1(x interface{}) string {
+ xx := x
+ switch xx.(type) {
+ default:
+ return fmt.Sprint("default ", xx)
+ case int, int8, int16, int32:
+ return fmt.Sprint("signed ", xx)
+ case int64:
+ return fmt.Sprint("signed64 ", xx.(int64))
+ case uint, uint8, uint16, uint32:
+ return fmt.Sprint("unsigned ", xx)
+ case uint64:
+ return fmt.Sprint("unsigned64 ", xx.(uint64))
+ case nil:
+ return fmt.Sprint("nil ", xx)
+ }
+ panic("not reached")
+}
+
+func check(x interface{}, s string) {
+ w := whatis(x)
+ if w != s {
+ fmt.Println("whatis", x, "=>", w, "!=", s)
+ panic("fail")
+ }
+
+ w = whatis1(x)
+ if w != s {
+ fmt.Println("whatis1", x, "=>", w, "!=", s)
+ panic("fail")
+ }
+}
+
+func main() {
+ check(1, "signed 1")
+ check(uint(1), "unsigned 1")
+ check(int64(1), "signed64 1")
+ check(uint64(1), "unsigned64 1")
+ check(1.5, "default 1.5")
+ check(nil, "nil <nil>")
+}