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