Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / gcc / testsuite / go.test / test / typeswitch1.go
blob9613b166f18bb15cd1491f468775f2fa869cb180
1 // $G $D/$F.go && $L $F.$A && ./$A.out
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.
7 package main
9 import "fmt"
11 const (
12 a = iota
19 var x = []int{1, 2, 3}
21 func f(x int, len *byte) {
22 *len = byte(x)
25 func whatis(x interface{}) string {
26 switch xx := x.(type) {
27 default:
28 return fmt.Sprint("default ", xx)
29 case int, int8, int16, int32:
30 return fmt.Sprint("signed ", xx)
31 case int64:
32 return fmt.Sprint("signed64 ", int64(xx))
33 case uint, uint8, uint16, uint32:
34 return fmt.Sprint("unsigned ", xx)
35 case uint64:
36 return fmt.Sprint("unsigned64 ", uint64(xx))
37 case nil:
38 return fmt.Sprint("nil ", xx)
40 panic("not reached")
43 func whatis1(x interface{}) string {
44 xx := x
45 switch xx.(type) {
46 default:
47 return fmt.Sprint("default ", xx)
48 case int, int8, int16, int32:
49 return fmt.Sprint("signed ", xx)
50 case int64:
51 return fmt.Sprint("signed64 ", xx.(int64))
52 case uint, uint8, uint16, uint32:
53 return fmt.Sprint("unsigned ", xx)
54 case uint64:
55 return fmt.Sprint("unsigned64 ", xx.(uint64))
56 case nil:
57 return fmt.Sprint("nil ", xx)
59 panic("not reached")
62 func check(x interface{}, s string) {
63 w := whatis(x)
64 if w != s {
65 fmt.Println("whatis", x, "=>", w, "!=", s)
66 panic("fail")
69 w = whatis1(x)
70 if w != s {
71 fmt.Println("whatis1", x, "=>", w, "!=", s)
72 panic("fail")
76 func main() {
77 check(1, "signed 1")
78 check(uint(1), "unsigned 1")
79 check(int64(1), "signed64 1")
80 check(uint64(1), "unsigned64 1")
81 check(1.5, "default 1.5")
82 check(nil, "nil <nil>")