Russ Cox | 57eb06f | 2012-02-16 23:51:04 -0500 | [diff] [blame] | 1 | // run |
Russ Cox | ce46cbe | 2009-09-21 22:45:19 -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 | |
| 7 | // Test that basic operations on named types are valid |
| 8 | // and preserve the type. |
| 9 | |
| 10 | package main |
| 11 | |
| 12 | type Array [10]byte |
| 13 | type Bool bool |
| 14 | type Chan chan int |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 15 | type Float float32 |
Russ Cox | ce46cbe | 2009-09-21 22:45:19 -0700 | [diff] [blame] | 16 | type Int int |
| 17 | type Map map[int]byte |
| 18 | type Slice []byte |
| 19 | type String string |
| 20 | |
| 21 | // Calling these functions checks at compile time that the argument |
| 22 | // can be converted implicitly to (used as) the given type. |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 23 | func asArray(Array) {} |
| 24 | func asBool(Bool) {} |
| 25 | func asChan(Chan) {} |
| 26 | func asFloat(Float) {} |
| 27 | func asInt(Int) {} |
| 28 | func asMap(Map) {} |
| 29 | func asSlice(Slice) {} |
Russ Cox | ce46cbe | 2009-09-21 22:45:19 -0700 | [diff] [blame] | 30 | func asString(String) {} |
| 31 | |
| 32 | func (Map) M() {} |
| 33 | |
Russ Cox | a457fa5 | 2012-02-21 22:54:07 -0500 | [diff] [blame] | 34 | |
Russ Cox | ce46cbe | 2009-09-21 22:45:19 -0700 | [diff] [blame] | 35 | // These functions check at run time that the default type |
| 36 | // (in the absence of any implicit conversion hints) |
| 37 | // is the given type. |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 38 | func isArray(x interface{}) { _ = x.(Array) } |
| 39 | func isBool(x interface{}) { _ = x.(Bool) } |
| 40 | func isChan(x interface{}) { _ = x.(Chan) } |
| 41 | func isFloat(x interface{}) { _ = x.(Float) } |
| 42 | func isInt(x interface{}) { _ = x.(Int) } |
| 43 | func isMap(x interface{}) { _ = x.(Map) } |
| 44 | func isSlice(x interface{}) { _ = x.(Slice) } |
Russ Cox | ce46cbe | 2009-09-21 22:45:19 -0700 | [diff] [blame] | 45 | func isString(x interface{}) { _ = x.(String) } |
| 46 | |
| 47 | func main() { |
| 48 | var ( |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 49 | a Array |
Russ Cox | a457fa5 | 2012-02-21 22:54:07 -0500 | [diff] [blame] | 50 | b Bool = true |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 51 | c Chan = make(Chan) |
| 52 | f Float = 1 |
| 53 | i Int = 1 |
| 54 | m Map = make(Map) |
| 55 | slice Slice = make(Slice, 10) |
| 56 | str String = "hello" |
Russ Cox | ce46cbe | 2009-09-21 22:45:19 -0700 | [diff] [blame] | 57 | ) |
| 58 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 59 | asArray(a) |
| 60 | isArray(a) |
| 61 | asArray(*&a) |
| 62 | isArray(*&a) |
| 63 | asArray(Array{}) |
| 64 | isArray(Array{}) |
Russ Cox | ce46cbe | 2009-09-21 22:45:19 -0700 | [diff] [blame] | 65 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 66 | asBool(b) |
| 67 | isBool(b) |
| 68 | asBool(!b) |
| 69 | isBool(!b) |
Russ Cox | a457fa5 | 2012-02-21 22:54:07 -0500 | [diff] [blame] | 70 | asBool(true) |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 71 | asBool(*&b) |
| 72 | isBool(*&b) |
| 73 | asBool(Bool(true)) |
| 74 | isBool(Bool(true)) |
Russ Cox | ce46cbe | 2009-09-21 22:45:19 -0700 | [diff] [blame] | 75 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 76 | asChan(c) |
| 77 | isChan(c) |
| 78 | asChan(make(Chan)) |
| 79 | isChan(make(Chan)) |
| 80 | asChan(*&c) |
| 81 | isChan(*&c) |
| 82 | asChan(Chan(nil)) |
| 83 | isChan(Chan(nil)) |
Russ Cox | ce46cbe | 2009-09-21 22:45:19 -0700 | [diff] [blame] | 84 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 85 | asFloat(f) |
| 86 | isFloat(f) |
| 87 | asFloat(-f) |
| 88 | isFloat(-f) |
| 89 | asFloat(+f) |
| 90 | isFloat(+f) |
| 91 | asFloat(f + 1) |
| 92 | isFloat(f + 1) |
| 93 | asFloat(1 + f) |
| 94 | isFloat(1 + f) |
| 95 | asFloat(f + f) |
| 96 | isFloat(f + f) |
| 97 | f++ |
| 98 | f += 2 |
| 99 | asFloat(f - 1) |
| 100 | isFloat(f - 1) |
| 101 | asFloat(1 - f) |
| 102 | isFloat(1 - f) |
| 103 | asFloat(f - f) |
| 104 | isFloat(f - f) |
| 105 | f-- |
| 106 | f -= 2 |
| 107 | asFloat(f * 2.5) |
| 108 | isFloat(f * 2.5) |
| 109 | asFloat(2.5 * f) |
| 110 | isFloat(2.5 * f) |
| 111 | asFloat(f * f) |
| 112 | isFloat(f * f) |
| 113 | f *= 4 |
| 114 | asFloat(f / 2.5) |
| 115 | isFloat(f / 2.5) |
| 116 | asFloat(2.5 / f) |
| 117 | isFloat(2.5 / f) |
| 118 | asFloat(f / f) |
| 119 | isFloat(f / f) |
| 120 | f /= 4 |
| 121 | asFloat(f) |
| 122 | isFloat(f) |
| 123 | f = 5 |
| 124 | asFloat(*&f) |
| 125 | isFloat(*&f) |
| 126 | asFloat(234) |
| 127 | asFloat(Float(234)) |
| 128 | isFloat(Float(234)) |
| 129 | asFloat(1.2) |
| 130 | asFloat(Float(i)) |
| 131 | isFloat(Float(i)) |
Russ Cox | ce46cbe | 2009-09-21 22:45:19 -0700 | [diff] [blame] | 132 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 133 | asInt(i) |
| 134 | isInt(i) |
| 135 | asInt(-i) |
| 136 | isInt(-i) |
| 137 | asInt(^i) |
| 138 | isInt(^i) |
| 139 | asInt(+i) |
| 140 | isInt(+i) |
| 141 | asInt(i + 1) |
| 142 | isInt(i + 1) |
| 143 | asInt(1 + i) |
| 144 | isInt(1 + i) |
| 145 | asInt(i + i) |
| 146 | isInt(i + i) |
| 147 | i++ |
| 148 | i += 1 |
| 149 | asInt(i - 1) |
| 150 | isInt(i - 1) |
| 151 | asInt(1 - i) |
| 152 | isInt(1 - i) |
| 153 | asInt(i - i) |
| 154 | isInt(i - i) |
| 155 | i-- |
| 156 | i -= 1 |
| 157 | asInt(i * 2) |
| 158 | isInt(i * 2) |
| 159 | asInt(2 * i) |
| 160 | isInt(2 * i) |
| 161 | asInt(i * i) |
| 162 | isInt(i * i) |
| 163 | i *= 2 |
| 164 | asInt(i / 5) |
| 165 | isInt(i / 5) |
| 166 | asInt(5 / i) |
| 167 | isInt(5 / i) |
| 168 | asInt(i / i) |
| 169 | isInt(i / i) |
| 170 | i /= 2 |
| 171 | asInt(i % 5) |
| 172 | isInt(i % 5) |
| 173 | asInt(5 % i) |
| 174 | isInt(5 % i) |
| 175 | asInt(i % i) |
| 176 | isInt(i % i) |
| 177 | i %= 2 |
| 178 | asInt(i & 5) |
| 179 | isInt(i & 5) |
| 180 | asInt(5 & i) |
| 181 | isInt(5 & i) |
| 182 | asInt(i & i) |
| 183 | isInt(i & i) |
| 184 | i &= 2 |
| 185 | asInt(i &^ 5) |
| 186 | isInt(i &^ 5) |
| 187 | asInt(5 &^ i) |
| 188 | isInt(5 &^ i) |
| 189 | asInt(i &^ i) |
| 190 | isInt(i &^ i) |
| 191 | i &^= 2 |
| 192 | asInt(i | 5) |
| 193 | isInt(i | 5) |
| 194 | asInt(5 | i) |
| 195 | isInt(5 | i) |
| 196 | asInt(i | i) |
| 197 | isInt(i | i) |
| 198 | i |= 2 |
| 199 | asInt(i ^ 5) |
| 200 | isInt(i ^ 5) |
| 201 | asInt(5 ^ i) |
| 202 | isInt(5 ^ i) |
| 203 | asInt(i ^ i) |
| 204 | isInt(i ^ i) |
| 205 | i ^= 2 |
| 206 | asInt(i << 4) |
| 207 | isInt(i << 4) |
| 208 | i <<= 2 |
| 209 | asInt(i >> 4) |
| 210 | isInt(i >> 4) |
| 211 | i >>= 2 |
| 212 | asInt(i) |
| 213 | isInt(i) |
| 214 | asInt(0) |
| 215 | asInt(Int(0)) |
| 216 | isInt(Int(0)) |
| 217 | i = 10 |
| 218 | asInt(*&i) |
| 219 | isInt(*&i) |
| 220 | asInt(23) |
| 221 | asInt(Int(f)) |
| 222 | isInt(Int(f)) |
Russ Cox | ce46cbe | 2009-09-21 22:45:19 -0700 | [diff] [blame] | 223 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 224 | asMap(m) |
| 225 | isMap(m) |
| 226 | asMap(nil) |
| 227 | m = nil |
| 228 | asMap(make(Map)) |
| 229 | isMap(make(Map)) |
| 230 | asMap(*&m) |
| 231 | isMap(*&m) |
| 232 | asMap(Map(nil)) |
| 233 | isMap(Map(nil)) |
| 234 | asMap(Map{}) |
| 235 | isMap(Map{}) |
Russ Cox | ce46cbe | 2009-09-21 22:45:19 -0700 | [diff] [blame] | 236 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 237 | asSlice(slice) |
| 238 | isSlice(slice) |
| 239 | asSlice(make(Slice, 5)) |
| 240 | isSlice(make(Slice, 5)) |
| 241 | asSlice([]byte{1, 2, 3}) |
| 242 | asSlice([]byte{1, 2, 3}[0:2]) |
| 243 | asSlice(slice[0:4]) |
| 244 | isSlice(slice[0:4]) |
| 245 | asSlice(slice[3:8]) |
| 246 | isSlice(slice[3:8]) |
| 247 | asSlice(nil) |
| 248 | asSlice(Slice(nil)) |
| 249 | isSlice(Slice(nil)) |
| 250 | slice = nil |
| 251 | asSlice(Slice{1, 2, 3}) |
| 252 | isSlice(Slice{1, 2, 3}) |
| 253 | asSlice(Slice{}) |
| 254 | isSlice(Slice{}) |
| 255 | asSlice(*&slice) |
| 256 | isSlice(*&slice) |
Russ Cox | ce46cbe | 2009-09-21 22:45:19 -0700 | [diff] [blame] | 257 | |
Russ Cox | 565b5dc | 2010-06-08 18:50:02 -0700 | [diff] [blame] | 258 | asString(str) |
| 259 | isString(str) |
| 260 | asString(str + "a") |
| 261 | isString(str + "a") |
| 262 | asString("a" + str) |
| 263 | isString("a" + str) |
| 264 | asString(str + str) |
| 265 | isString(str + str) |
| 266 | str += "a" |
| 267 | str += str |
| 268 | asString(String('a')) |
| 269 | isString(String('a')) |
| 270 | asString(String([]byte(slice))) |
| 271 | isString(String([]byte(slice))) |
| 272 | asString(String([]byte(nil))) |
| 273 | isString(String([]byte(nil))) |
| 274 | asString("hello") |
| 275 | asString(String("hello")) |
| 276 | isString(String("hello")) |
| 277 | str = "hello" |
| 278 | isString(str) |
| 279 | asString(*&str) |
| 280 | isString(*&str) |
Russ Cox | ce46cbe | 2009-09-21 22:45:19 -0700 | [diff] [blame] | 281 | } |