| 
									
										
										
										
											2012-02-16 23:51:04 -05:00
										 |  |  | // run | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Copyright 2009 The Go Authors. All rights reserved. | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style | 
					
						
							|  |  |  | // license that can be found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-24 11:48:19 +11:00
										 |  |  | // Test simple type switches, including chans, maps etc. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-08 15:21:41 -07:00
										 |  |  | import "os" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | const ( | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	Bool = iota | 
					
						
							|  |  |  | 	Int | 
					
						
							|  |  |  | 	Float | 
					
						
							|  |  |  | 	String | 
					
						
							|  |  |  | 	Struct | 
					
						
							|  |  |  | 	Chan | 
					
						
							|  |  |  | 	Array | 
					
						
							|  |  |  | 	Map | 
					
						
							|  |  |  | 	Func | 
					
						
							|  |  |  | 	Last | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-19 23:09:00 -05:00
										 |  |  | type S struct { | 
					
						
							|  |  |  | 	a int | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | var s S = S{1234} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | var c = make(chan int) | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-19 23:09:00 -05:00
										 |  |  | var a = []int{0, 1, 2, 3} | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | var m = make(map[string]int) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func assert(b bool, s string) { | 
					
						
							|  |  |  | 	if !b { | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		println(s) | 
					
						
							|  |  |  | 		os.Exit(1) | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func f(i int) interface{} { | 
					
						
							|  |  |  | 	switch i { | 
					
						
							|  |  |  | 	case Bool: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		return true | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	case Int: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		return 7 | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	case Float: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		return 7.4 | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	case String: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		return "hello" | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	case Struct: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		return s | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	case Chan: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		return c | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	case Array: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		return a | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	case Map: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		return m | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	case Func: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		return f | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 	panic("bad type number") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func main() { | 
					
						
							|  |  |  | 	for i := Bool; i < Last; i++ { | 
					
						
							|  |  |  | 		switch x := f(i).(type) { | 
					
						
							|  |  |  | 		case bool: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 			assert(x == true && i == Bool, "bool") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 		case int: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 			assert(x == 7 && i == Int, "int") | 
					
						
							| 
									
										
										
										
											2011-01-19 23:09:00 -05:00
										 |  |  | 		case float64: | 
					
						
							|  |  |  | 			assert(x == 7.4 && i == Float, "float64") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 		case string: | 
					
						
							| 
									
										
										
										
											2011-01-19 23:09:00 -05:00
										 |  |  | 			assert(x == "hello" && i == String, "string") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 		case S: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 			assert(x.a == 1234 && i == Struct, "struct") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 		case chan int: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 			assert(x == c && i == Chan, "chan") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 		case []int: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 			assert(x[3] == 3 && i == Array, "array") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 		case map[string]int: | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 			assert(x != nil && i == Map, "map") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 		case func(i int) interface{}: | 
					
						
							| 
									
										
										
										
											2011-11-13 22:58:08 -05:00
										 |  |  | 			assert(x != nil && i == Func, "fun") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 			assert(false, "unknown") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-18 14:05:05 -07:00
										 |  |  | 	// boolean switch (has had bugs in past; worth writing down) | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	switch { | 
					
						
							|  |  |  | 	case true: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		assert(true, "switch 2 bool") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		assert(false, "switch 2 unknown") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch true { | 
					
						
							|  |  |  | 	case true: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		assert(true, "switch 3 bool") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		assert(false, "switch 3 unknown") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch false { | 
					
						
							|  |  |  | 	case false: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		assert(true, "switch 4 bool") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2010-09-04 10:36:13 +10:00
										 |  |  | 		assert(false, "switch 4 unknown") | 
					
						
							| 
									
										
										
										
											2009-03-17 20:55:42 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |