utatane.tea@gmail.com | d2de85f | 2017-02-04 13:46:19 +0000 | [diff] [blame] | 1 | function shouldBe(actual, expected) { |
| 2 | if (actual !== expected) |
| 3 | throw new Error('bad value: ' + actual); |
| 4 | } |
| 5 | |
| 6 | // ValueToInt32(DoubleRep) |
| 7 | function toInt32(number) |
| 8 | { |
| 9 | return (number * 0.5) >> 0; |
| 10 | } |
| 11 | noInline(toInt32); |
| 12 | for (var i = 0; i < 1e5; ++i) |
| 13 | toInt32(i * 1.0); |
| 14 | |
| 15 | function test(number) |
| 16 | { |
| 17 | return toInt32(number * 2); |
| 18 | } |
| 19 | |
| 20 | const INT32_MAX = 2147483647; |
| 21 | const INT32_MIN = -2147483648; |
| 22 | |
| 23 | shouldBe(test(INT32_MAX - 1), INT32_MAX - 1); |
| 24 | shouldBe(test(INT32_MAX - 0.5), INT32_MAX - 1); |
| 25 | shouldBe(test(INT32_MAX), INT32_MAX); |
| 26 | shouldBe(test(INT32_MAX + 0.5), INT32_MAX); |
| 27 | shouldBe(test(INT32_MAX + 1), INT32_MIN); |
| 28 | |
| 29 | shouldBe(test(INT32_MIN - 1), INT32_MAX); |
| 30 | shouldBe(test(INT32_MIN - 0.5), INT32_MIN); |
| 31 | shouldBe(test(INT32_MIN), INT32_MIN); |
| 32 | shouldBe(test(INT32_MIN + 0.5), INT32_MIN + 1); |
| 33 | shouldBe(test(INT32_MIN + 1), INT32_MIN + 1); |
| 34 | |
| 35 | shouldBe(test(Number.EPSILON), 0); |
| 36 | shouldBe(test(Number.NaN), 0); |
| 37 | shouldBe(test(Number.POSITIVE_INFINITY), 0); |
| 38 | shouldBe(test(Number.NEGATIVE_INFINITY), 0); |
| 39 | shouldBe(test(Number.MAX_SAFE_INTEGER), -1); |
| 40 | shouldBe(test(Number.MIN_SAFE_INTEGER), 1); |