OSDN Git Service

PR c++/51318
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / go.test / test / typeswitch.go
1 // $G $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 "os"
10
11 const (
12         Bool = iota
13         Int
14         Float
15         String
16         Struct
17         Chan
18         Array
19         Map
20         Func
21         Last
22 )
23
24 type S struct {
25         a int
26 }
27
28 var s S = S{1234}
29
30 var c = make(chan int)
31
32 var a = []int{0, 1, 2, 3}
33
34 var m = make(map[string]int)
35
36 func assert(b bool, s string) {
37         if !b {
38                 println(s)
39                 os.Exit(1)
40         }
41 }
42
43 func f(i int) interface{} {
44         switch i {
45         case Bool:
46                 return true
47         case Int:
48                 return 7
49         case Float:
50                 return 7.4
51         case String:
52                 return "hello"
53         case Struct:
54                 return s
55         case Chan:
56                 return c
57         case Array:
58                 return a
59         case Map:
60                 return m
61         case Func:
62                 return f
63         }
64         panic("bad type number")
65 }
66
67 func main() {
68         for i := Bool; i < Last; i++ {
69                 switch x := f(i).(type) {
70                 case bool:
71                         assert(x == true && i == Bool, "bool")
72                 case int:
73                         assert(x == 7 && i == Int, "int")
74                 case float64:
75                         assert(x == 7.4 && i == Float, "float64")
76                 case string:
77                         assert(x == "hello" && i == String, "string")
78                 case S:
79                         assert(x.a == 1234 && i == Struct, "struct")
80                 case chan int:
81                         assert(x == c && i == Chan, "chan")
82                 case []int:
83                         assert(x[3] == 3 && i == Array, "array")
84                 case map[string]int:
85                         assert(x == m && i == Map, "map")
86                 case func(i int) interface{}:
87                         assert(x == f && i == Func, "fun")
88                 default:
89                         assert(false, "unknown")
90                 }
91         }
92
93         // boolean switch (has had bugs in past; worth writing down)
94         switch {
95         case true:
96                 assert(true, "switch 2 bool")
97         default:
98                 assert(false, "switch 2 unknown")
99         }
100
101         switch true {
102         case true:
103                 assert(true, "switch 3 bool")
104         default:
105                 assert(false, "switch 3 unknown")
106         }
107
108         switch false {
109         case false:
110                 assert(true, "switch 4 bool")
111         default:
112                 assert(false, "switch 4 unknown")
113         }
114
115 }