github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/go/constant/value_test.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package constant 6 7 import ( 8 "go/token" 9 "strings" 10 "testing" 11 ) 12 13 // TODO(gri) expand this test framework 14 15 var opTests = []string{ 16 // unary operations 17 `+ 0 = 0`, 18 `+ ? = ?`, 19 `- 1 = -1`, 20 `- ? = ?`, 21 `^ 0 = -1`, 22 `^ ? = ?`, 23 24 `! true = false`, 25 `! false = true`, 26 `! ? = ?`, 27 28 // etc. 29 30 // binary operations 31 `"" + "" = ""`, 32 `"foo" + "" = "foo"`, 33 `"" + "bar" = "bar"`, 34 `"foo" + "bar" = "foobar"`, 35 36 `0 + 0 = 0`, 37 `0 + 0.1 = 0.1`, 38 `0 + 0.1i = 0.1i`, 39 `0.1 + 0.9 = 1`, 40 `1e100 + 1e100 = 2e100`, 41 `? + 0 = ?`, 42 `0 + ? = ?`, 43 44 `0 - 0 = 0`, 45 `0 - 0.1 = -0.1`, 46 `0 - 0.1i = -0.1i`, 47 `1e100 - 1e100 = 0`, 48 `? - 0 = ?`, 49 `0 - ? = ?`, 50 51 `0 * 0 = 0`, 52 `1 * 0.1 = 0.1`, 53 `1 * 0.1i = 0.1i`, 54 `1i * 1i = -1`, 55 `? * 0 = ?`, 56 `0 * ? = ?`, 57 58 `0 / 0 = "division_by_zero"`, 59 `10 / 2 = 5`, 60 `5 / 3 = 5/3`, 61 `5i / 3i = 5/3`, 62 `? / 0 = ?`, 63 `0 / ? = ?`, 64 65 `0 % 0 = "runtime_error:_integer_divide_by_zero"`, // TODO(gri) should be the same as for / 66 `10 % 3 = 1`, 67 `? % 0 = ?`, 68 `0 % ? = ?`, 69 70 `0 & 0 = 0`, 71 `12345 & 0 = 0`, 72 `0xff & 0xf = 0xf`, 73 `? & 0 = ?`, 74 `0 & ? = ?`, 75 76 `0 | 0 = 0`, 77 `12345 | 0 = 12345`, 78 `0xb | 0xa0 = 0xab`, 79 `? | 0 = ?`, 80 `0 | ? = ?`, 81 82 `0 ^ 0 = 0`, 83 `1 ^ -1 = -2`, 84 `? ^ 0 = ?`, 85 `0 ^ ? = ?`, 86 87 `0 &^ 0 = 0`, 88 `0xf &^ 1 = 0xe`, 89 `1 &^ 0xf = 0`, 90 // etc. 91 92 // shifts 93 `0 << 0 = 0`, 94 `1 << 10 = 1024`, 95 `0 >> 0 = 0`, 96 `1024 >> 10 == 1`, 97 `? << 0 == ?`, 98 `? >> 10 == ?`, 99 // etc. 100 101 // comparisons 102 `false == false = true`, 103 `false == true = false`, 104 `true == false = false`, 105 `true == true = true`, 106 107 `false != false = false`, 108 `false != true = true`, 109 `true != false = true`, 110 `true != true = false`, 111 112 `"foo" == "bar" = false`, 113 `"foo" != "bar" = true`, 114 `"foo" < "bar" = false`, 115 `"foo" <= "bar" = false`, 116 `"foo" > "bar" = true`, 117 `"foo" >= "bar" = true`, 118 119 `0 == 0 = true`, 120 `0 != 0 = false`, 121 `0 < 10 = true`, 122 `10 <= 10 = true`, 123 `0 > 10 = false`, 124 `10 >= 10 = true`, 125 126 `1/123456789 == 1/123456789 == true`, 127 `1/123456789 != 1/123456789 == false`, 128 `1/123456789 < 1/123456788 == true`, 129 `1/123456788 <= 1/123456789 == false`, 130 `0.11 > 0.11 = false`, 131 `0.11 >= 0.11 = true`, 132 133 `? == 0 = false`, 134 `? != 0 = false`, 135 `? < 10 = false`, 136 `? <= 10 = false`, 137 `? > 10 = false`, 138 `? >= 10 = false`, 139 140 `0 == ? = false`, 141 `0 != ? = false`, 142 `0 < ? = false`, 143 `10 <= ? = false`, 144 `0 > ? = false`, 145 `10 >= ? = false`, 146 147 // etc. 148 } 149 150 func TestOps(t *testing.T) { 151 for _, test := range opTests { 152 a := strings.Split(test, " ") 153 i := 0 // operator index 154 155 var x, x0 Value 156 switch len(a) { 157 case 4: 158 // unary operation 159 case 5: 160 // binary operation 161 x, x0 = val(a[0]), val(a[0]) 162 i = 1 163 default: 164 t.Errorf("invalid test case: %s", test) 165 continue 166 } 167 168 op, ok := optab[a[i]] 169 if !ok { 170 panic("missing optab entry for " + a[i]) 171 } 172 173 y, y0 := val(a[i+1]), val(a[i+1]) 174 175 got := doOp(x, op, y) 176 want := val(a[i+3]) 177 if !eql(got, want) { 178 t.Errorf("%s: got %s; want %s", test, got, want) 179 continue 180 } 181 182 if x0 != nil && !eql(x, x0) { 183 t.Errorf("%s: x changed to %s", test, x) 184 continue 185 } 186 187 if !eql(y, y0) { 188 t.Errorf("%s: y changed to %s", test, y) 189 continue 190 } 191 } 192 } 193 194 func eql(x, y Value) bool { 195 _, ux := x.(unknownVal) 196 _, uy := y.(unknownVal) 197 if ux || uy { 198 return ux == uy 199 } 200 return Compare(x, token.EQL, y) 201 } 202 203 // ---------------------------------------------------------------------------- 204 // String tests 205 206 var xxx = strings.Repeat("x", 68) 207 208 var stringTests = []struct { 209 input, short, exact string 210 }{ 211 // Unknown 212 {"", "unknown", "unknown"}, 213 {"0x", "unknown", "unknown"}, 214 {"'", "unknown", "unknown"}, 215 {"1f0", "unknown", "unknown"}, 216 {"unknown", "unknown", "unknown"}, 217 218 // Bool 219 {"true", "true", "true"}, 220 {"false", "false", "false"}, 221 222 // String 223 {`""`, `""`, `""`}, 224 {`"foo"`, `"foo"`, `"foo"`}, 225 {`"` + xxx + `xx"`, `"` + xxx + `xx"`, `"` + xxx + `xx"`}, 226 {`"` + xxx + `xxx"`, `"` + xxx + `...`, `"` + xxx + `xxx"`}, 227 {`"` + xxx + xxx + `xxx"`, `"` + xxx + `...`, `"` + xxx + xxx + `xxx"`}, 228 229 // Int 230 {"0", "0", "0"}, 231 {"-1", "-1", "-1"}, 232 {"12345", "12345", "12345"}, 233 {"-12345678901234567890", "-12345678901234567890", "-12345678901234567890"}, 234 {"12345678901234567890", "12345678901234567890", "12345678901234567890"}, 235 236 // Float 237 {"0.", "0", "0"}, 238 {"-0.0", "0", "0"}, 239 {"10.0", "10", "10"}, 240 {"2.1", "2.1", "21/10"}, 241 {"-2.1", "-2.1", "-21/10"}, 242 {"1e9999", "1e+9999", "0x.f8d4a9da224650a8cb2959e10d985ad92adbd44c62917e608b1f24c0e1b76b6f61edffeb15c135a4b601637315f7662f325f82325422b244286a07663c9415d2p+33216"}, 243 {"1e-9999", "1e-9999", "0x.83b01ba6d8c0425eec1b21e96f7742d63c2653ed0a024cf8a2f9686df578d7b07d7a83d84df6a2ec70a921d1f6cd5574893a7eda4d28ee719e13a5dce2700759p-33215"}, 244 {"2.71828182845904523536028747135266249775724709369995957496696763", "2.71828", "271828182845904523536028747135266249775724709369995957496696763/100000000000000000000000000000000000000000000000000000000000000"}, 245 246 // Complex 247 {"0i", "(0 + 0i)", "(0 + 0i)"}, 248 {"-0i", "(0 + 0i)", "(0 + 0i)"}, 249 {"10i", "(0 + 10i)", "(0 + 10i)"}, 250 {"-10i", "(0 + -10i)", "(0 + -10i)"}, 251 {"1e9999i", "(0 + 1e+9999i)", "(0 + 0x.f8d4a9da224650a8cb2959e10d985ad92adbd44c62917e608b1f24c0e1b76b6f61edffeb15c135a4b601637315f7662f325f82325422b244286a07663c9415d2p+33216i)"}, 252 } 253 254 func TestString(t *testing.T) { 255 for _, test := range stringTests { 256 x := val(test.input) 257 if got := x.String(); got != test.short { 258 t.Errorf("%s: got %q; want %q as short string", test.input, got, test.short) 259 } 260 if got := x.ExactString(); got != test.exact { 261 t.Errorf("%s: got %q; want %q as exact string", test.input, got, test.exact) 262 } 263 } 264 } 265 266 // ---------------------------------------------------------------------------- 267 // Support functions 268 269 func val(lit string) Value { 270 if len(lit) == 0 { 271 return MakeUnknown() 272 } 273 274 switch lit { 275 case "?": 276 return MakeUnknown() 277 case "true": 278 return MakeBool(true) 279 case "false": 280 return MakeBool(false) 281 } 282 283 if i := strings.IndexByte(lit, '/'); i >= 0 { 284 // assume fraction 285 a := MakeFromLiteral(lit[:i], token.INT, 0) 286 b := MakeFromLiteral(lit[i+1:], token.INT, 0) 287 return BinaryOp(a, token.QUO, b) 288 } 289 290 tok := token.INT 291 switch first, last := lit[0], lit[len(lit)-1]; { 292 case first == '"' || first == '`': 293 tok = token.STRING 294 lit = strings.Replace(lit, "_", " ", -1) 295 case first == '\'': 296 tok = token.CHAR 297 case last == 'i': 298 tok = token.IMAG 299 default: 300 if !strings.HasPrefix(lit, "0x") && strings.ContainsAny(lit, "./Ee") { 301 tok = token.FLOAT 302 } 303 } 304 305 return MakeFromLiteral(lit, tok, 0) 306 } 307 308 var optab = map[string]token.Token{ 309 "!": token.NOT, 310 311 "+": token.ADD, 312 "-": token.SUB, 313 "*": token.MUL, 314 "/": token.QUO, 315 "%": token.REM, 316 317 "<<": token.SHL, 318 ">>": token.SHR, 319 320 "&": token.AND, 321 "|": token.OR, 322 "^": token.XOR, 323 "&^": token.AND_NOT, 324 325 "==": token.EQL, 326 "!=": token.NEQ, 327 "<": token.LSS, 328 "<=": token.LEQ, 329 ">": token.GTR, 330 ">=": token.GEQ, 331 } 332 333 func panicHandler(v *Value) { 334 switch p := recover().(type) { 335 case nil: 336 // nothing to do 337 case string: 338 *v = MakeString(p) 339 case error: 340 *v = MakeString(p.Error()) 341 default: 342 panic(p) 343 } 344 } 345 346 func doOp(x Value, op token.Token, y Value) (z Value) { 347 defer panicHandler(&z) 348 349 if x == nil { 350 return UnaryOp(op, y, 0) 351 } 352 353 switch op { 354 case token.EQL, token.NEQ, token.LSS, token.LEQ, token.GTR, token.GEQ: 355 return MakeBool(Compare(x, op, y)) 356 case token.SHL, token.SHR: 357 s, _ := Int64Val(y) 358 return Shift(x, op, uint(s)) 359 default: 360 return BinaryOp(x, op, y) 361 } 362 } 363 364 // ---------------------------------------------------------------------------- 365 // Other tests 366 367 var fracTests = []string{ 368 "0", 369 "1", 370 "-1", 371 "1.2", 372 "-0.991", 373 "2.718281828", 374 "3.14159265358979323e-10", 375 "1e100", 376 "1e1000", 377 } 378 379 func TestFractions(t *testing.T) { 380 for _, test := range fracTests { 381 x := val(test) 382 // We don't check the actual numerator and denominator because they 383 // are unlikely to be 100% correct due to floatVal rounding errors. 384 // Instead, we compute the fraction again and compare the rounded 385 // result. 386 q := BinaryOp(Num(x), token.QUO, Denom(x)) 387 got := q.String() 388 want := x.String() 389 if got != want { 390 t.Errorf("%s: got quotient %s, want %s", x, got, want) 391 } 392 } 393 } 394 395 var bytesTests = []string{ 396 "0", 397 "1", 398 "123456789", 399 "123456789012345678901234567890123456789012345678901234567890", 400 } 401 402 func TestBytes(t *testing.T) { 403 for _, test := range bytesTests { 404 x := val(test) 405 bytes := Bytes(x) 406 407 // special case 0 408 if Sign(x) == 0 && len(bytes) != 0 { 409 t.Errorf("%s: got %v; want empty byte slice", test, bytes) 410 } 411 412 if n := len(bytes); n > 0 && bytes[n-1] == 0 { 413 t.Errorf("%s: got %v; want no leading 0 byte", test, bytes) 414 } 415 416 if got := MakeFromBytes(bytes); !eql(got, x) { 417 t.Errorf("%s: got %s; want %s (bytes = %v)", test, got, x, bytes) 418 } 419 } 420 } 421 422 func TestUnknown(t *testing.T) { 423 u := MakeUnknown() 424 var values = []Value{ 425 u, 426 MakeBool(false), // token.ADD ok below, operation is never considered 427 MakeString(""), 428 MakeInt64(1), 429 MakeFromLiteral("-1234567890123456789012345678901234567890", token.INT, 0), 430 MakeFloat64(1.2), 431 MakeImag(MakeFloat64(1.2)), 432 } 433 for _, val := range values { 434 x, y := val, u 435 for i := range [2]int{} { 436 if i == 1 { 437 x, y = y, x 438 } 439 if got := BinaryOp(x, token.ADD, y); got.Kind() != Unknown { 440 t.Errorf("%s + %s: got %s; want %s", x, y, got, u) 441 } 442 if got := Compare(x, token.EQL, y); got { 443 t.Errorf("%s == %s: got true; want false", x, y) 444 } 445 } 446 } 447 }