Russ Cox | 57eb06f | 2012-02-16 23:51:04 -0500 | [diff] [blame] | 1 | // run |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 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 | |
Rob Pike | 80a9783 | 2012-02-24 11:48:19 +1100 | [diff] [blame] | 7 | // Test simple type switches on basic types. |
| 8 | |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 9 | package main |
| 10 | |
| 11 | import "fmt" |
| 12 | |
| 13 | const ( |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 14 | a = iota |
| 15 | b |
| 16 | c |
| 17 | d |
| 18 | e |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 21 | var x = []int{1, 2, 3} |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 22 | |
| 23 | func f(x int, len *byte) { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 24 | *len = byte(x) |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | func whatis(x interface{}) string { |
| 28 | switch xx := x.(type) { |
| 29 | default: |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 30 | return fmt.Sprint("default ", xx) |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 31 | case int, int8, int16, int32: |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 32 | return fmt.Sprint("signed ", xx) |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 33 | case int64: |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 34 | return fmt.Sprint("signed64 ", int64(xx)) |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 35 | case uint, uint8, uint16, uint32: |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 36 | return fmt.Sprint("unsigned ", xx) |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 37 | case uint64: |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 38 | return fmt.Sprint("unsigned64 ", uint64(xx)) |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 39 | case nil: |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 40 | return fmt.Sprint("nil ", xx) |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 41 | } |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 42 | panic("not reached") |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | func whatis1(x interface{}) string { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 46 | xx := x |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 47 | switch xx.(type) { |
| 48 | default: |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 49 | return fmt.Sprint("default ", xx) |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 50 | case int, int8, int16, int32: |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 51 | return fmt.Sprint("signed ", xx) |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 52 | case int64: |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 53 | return fmt.Sprint("signed64 ", xx.(int64)) |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 54 | case uint, uint8, uint16, uint32: |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 55 | return fmt.Sprint("unsigned ", xx) |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 56 | case uint64: |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 57 | return fmt.Sprint("unsigned64 ", xx.(uint64)) |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 58 | case nil: |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 59 | return fmt.Sprint("nil ", xx) |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 60 | } |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 61 | panic("not reached") |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | func check(x interface{}, s string) { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 65 | w := whatis(x) |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 66 | if w != s { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 67 | fmt.Println("whatis", x, "=>", w, "!=", s) |
| 68 | panic("fail") |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 71 | w = whatis1(x) |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 72 | if w != s { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 73 | fmt.Println("whatis1", x, "=>", w, "!=", s) |
| 74 | panic("fail") |
Russ Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | func main() { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 79 | 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 Cox | 5d16d23 | 2009-09-09 00:18:16 -0700 | [diff] [blame] | 85 | } |