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 // Test simple type switches, including chans, maps etc.
32 var c = make(chan int)
34 var a = []int{0, 1, 2, 3}
36 var m = make(map[string]int)
38 func assert(b bool, s string) {
45 func f(i int) interface{} {
66 panic("bad type number")
70 for i := Bool; i < Last; i++ {
71 switch x := f(i).(type) {
73 assert(x == true && i == Bool, "bool")
75 assert(x == 7 && i == Int, "int")
77 assert(x == 7.4 && i == Float, "float64")
79 assert(x == "hello" && i == String, "string")
81 assert(x.a == 1234 && i == Struct, "struct")
83 assert(x == c && i == Chan, "chan")
85 assert(x[3] == 3 && i == Array, "array")
87 assert(x != nil && i == Map, "map")
88 case func(i int) interface{}:
89 assert(x != nil && i == Func, "fun")
91 assert(false, "unknown")
95 // boolean switch (has had bugs in past; worth writing down)
98 assert(true, "switch 2 bool")
100 assert(false, "switch 2 unknown")
105 assert(true, "switch 3 bool")
107 assert(false, "switch 3 unknown")
112 assert(true, "switch 4 bool")
114 assert(false, "switch 4 unknown")