blob: a980ce4c070d4f2c08d698bb846406d39f4d18c7 [file] [log] [blame]
Russ Cox57eb06f2012-02-16 23:51:04 -05001// run
Russ Cox5d16d232009-09-09 00:18:16 -07002
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
Rob Pike80a97832012-02-24 11:48:19 +11007// Test simple type switches on basic types.
8
Russ Cox5d16d232009-09-09 00:18:16 -07009package main
10
11import "fmt"
12
13const (
Russ Cox00f9f0c2010-03-30 10:34:57 -070014 a = iota
15 b
16 c
17 d
18 e
Russ Cox5d16d232009-09-09 00:18:16 -070019)
20
Russ Cox00f9f0c2010-03-30 10:34:57 -070021var x = []int{1, 2, 3}
Russ Cox5d16d232009-09-09 00:18:16 -070022
23func f(x int, len *byte) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070024 *len = byte(x)
Russ Cox5d16d232009-09-09 00:18:16 -070025}
26
27func whatis(x interface{}) string {
28 switch xx := x.(type) {
29 default:
Russ Cox00f9f0c2010-03-30 10:34:57 -070030 return fmt.Sprint("default ", xx)
Russ Cox5d16d232009-09-09 00:18:16 -070031 case int, int8, int16, int32:
Russ Cox00f9f0c2010-03-30 10:34:57 -070032 return fmt.Sprint("signed ", xx)
Russ Cox5d16d232009-09-09 00:18:16 -070033 case int64:
Russ Cox00f9f0c2010-03-30 10:34:57 -070034 return fmt.Sprint("signed64 ", int64(xx))
Russ Cox5d16d232009-09-09 00:18:16 -070035 case uint, uint8, uint16, uint32:
Russ Cox00f9f0c2010-03-30 10:34:57 -070036 return fmt.Sprint("unsigned ", xx)
Russ Cox5d16d232009-09-09 00:18:16 -070037 case uint64:
Russ Cox00f9f0c2010-03-30 10:34:57 -070038 return fmt.Sprint("unsigned64 ", uint64(xx))
Russ Cox5d16d232009-09-09 00:18:16 -070039 case nil:
Russ Cox00f9f0c2010-03-30 10:34:57 -070040 return fmt.Sprint("nil ", xx)
Russ Cox5d16d232009-09-09 00:18:16 -070041 }
Russ Cox00f9f0c2010-03-30 10:34:57 -070042 panic("not reached")
Russ Cox5d16d232009-09-09 00:18:16 -070043}
44
45func whatis1(x interface{}) string {
Russ Cox00f9f0c2010-03-30 10:34:57 -070046 xx := x
Russ Cox5d16d232009-09-09 00:18:16 -070047 switch xx.(type) {
48 default:
Russ Cox00f9f0c2010-03-30 10:34:57 -070049 return fmt.Sprint("default ", xx)
Russ Cox5d16d232009-09-09 00:18:16 -070050 case int, int8, int16, int32:
Russ Cox00f9f0c2010-03-30 10:34:57 -070051 return fmt.Sprint("signed ", xx)
Russ Cox5d16d232009-09-09 00:18:16 -070052 case int64:
Russ Cox00f9f0c2010-03-30 10:34:57 -070053 return fmt.Sprint("signed64 ", xx.(int64))
Russ Cox5d16d232009-09-09 00:18:16 -070054 case uint, uint8, uint16, uint32:
Russ Cox00f9f0c2010-03-30 10:34:57 -070055 return fmt.Sprint("unsigned ", xx)
Russ Cox5d16d232009-09-09 00:18:16 -070056 case uint64:
Russ Cox00f9f0c2010-03-30 10:34:57 -070057 return fmt.Sprint("unsigned64 ", xx.(uint64))
Russ Cox5d16d232009-09-09 00:18:16 -070058 case nil:
Russ Cox00f9f0c2010-03-30 10:34:57 -070059 return fmt.Sprint("nil ", xx)
Russ Cox5d16d232009-09-09 00:18:16 -070060 }
Russ Cox00f9f0c2010-03-30 10:34:57 -070061 panic("not reached")
Russ Cox5d16d232009-09-09 00:18:16 -070062}
63
64func check(x interface{}, s string) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070065 w := whatis(x)
Russ Cox5d16d232009-09-09 00:18:16 -070066 if w != s {
Russ Cox00f9f0c2010-03-30 10:34:57 -070067 fmt.Println("whatis", x, "=>", w, "!=", s)
68 panic("fail")
Russ Cox5d16d232009-09-09 00:18:16 -070069 }
70
Russ Cox00f9f0c2010-03-30 10:34:57 -070071 w = whatis1(x)
Russ Cox5d16d232009-09-09 00:18:16 -070072 if w != s {
Russ Cox00f9f0c2010-03-30 10:34:57 -070073 fmt.Println("whatis1", x, "=>", w, "!=", s)
74 panic("fail")
Russ Cox5d16d232009-09-09 00:18:16 -070075 }
76}
77
78func main() {
Russ Cox00f9f0c2010-03-30 10:34:57 -070079 check(1, "signed 1")
80 check(uint(1), "unsigned 1")
81 check(int64(1), "signed64 1")
82 check(uint64(1), "unsigned64 1")
83 check(1.5, "default 1.5")
84 check(nil, "nil <nil>")
Russ Cox5d16d232009-09-09 00:18:16 -070085}