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