|
| 1 | +function shouldBe(actual, expected) { |
| 2 | + if (actual !== expected) |
| 3 | + throw new Error('bad value: ' + actual); |
| 4 | +} |
| 5 | + |
| 6 | +function shouldThrow(func, errorMessage) { |
| 7 | + var errorThrown = false; |
| 8 | + var error = null; |
| 9 | + try { |
| 10 | + func(); |
| 11 | + } catch (e) { |
| 12 | + errorThrown = true; |
| 13 | + error = e; |
| 14 | + } |
| 15 | + if (!errorThrown) |
| 16 | + throw new Error('not thrown'); |
| 17 | + if (String(error) !== errorMessage) |
| 18 | + throw new Error(`bad error: ${String(error)}`); |
| 19 | +} |
| 20 | + |
| 21 | +shouldThrow(() => { |
| 22 | + BigInt.asUintN(-1, 0n) |
| 23 | +}, `RangeError: number of bits cannot be negative`); |
| 24 | + |
| 25 | +{ |
| 26 | + let toIndex = false; |
| 27 | + let toBigInt = false; |
| 28 | + let index = { |
| 29 | + [Symbol.toPrimitive]() { |
| 30 | + shouldBe(toIndex, false); |
| 31 | + shouldBe(toBigInt, false); |
| 32 | + toIndex = true; |
| 33 | + return 32; |
| 34 | + } |
| 35 | + }; |
| 36 | + let bigint = { |
| 37 | + [Symbol.toPrimitive]() { |
| 38 | + shouldBe(toIndex, true); |
| 39 | + shouldBe(toBigInt, false); |
| 40 | + toBigInt = true; |
| 41 | + return 10n; |
| 42 | + } |
| 43 | + } |
| 44 | + shouldBe(BigInt.asUintN(index, bigint), 10n); |
| 45 | + shouldBe(toIndex, true); |
| 46 | + shouldBe(toBigInt, true); |
| 47 | +} |
| 48 | + |
| 49 | +shouldBe(BigInt.asUintN(-0, 0n), 0n); |
| 50 | +shouldBe(BigInt.asUintN(0, 0n), 0n); |
| 51 | +shouldBe(BigInt.asUintN(1, 0n), 0n); |
| 52 | +shouldBe(BigInt.asUintN(2, 0n), 0n); |
| 53 | +shouldBe(BigInt.asUintN(3, 0n), 0n); |
| 54 | + |
| 55 | +shouldBe(BigInt.asUintN(0, 1n), 0n); |
| 56 | +shouldBe(BigInt.asUintN(1, 1n), 1n); |
| 57 | +shouldBe(BigInt.asUintN(2, 1n), 1n); |
| 58 | +shouldBe(BigInt.asUintN(3, 1n), 1n); |
| 59 | + |
| 60 | +shouldBe(BigInt.asUintN(30, 0x7fffffffn), 1073741823n); |
| 61 | +shouldBe(BigInt.asUintN(31, 0x7fffffffn), 2147483647n); |
| 62 | +shouldBe(BigInt.asUintN(32, 0x7fffffffn), 2147483647n); |
| 63 | +shouldBe(BigInt.asUintN(33, 0x7fffffffn), 2147483647n); |
| 64 | +shouldBe(BigInt.asUintN(34, 0x7fffffffn), 2147483647n); |
| 65 | + |
| 66 | +shouldBe(BigInt.asUintN(30, 0x80000000n), 0n); |
| 67 | +shouldBe(BigInt.asUintN(31, 0x80000000n), 0n); |
| 68 | +shouldBe(BigInt.asUintN(32, 0x80000000n), 2147483648n); |
| 69 | +shouldBe(BigInt.asUintN(33, 0x80000000n), 2147483648n); |
| 70 | +shouldBe(BigInt.asUintN(34, 0x80000000n), 2147483648n); |
| 71 | + |
| 72 | +shouldBe(BigInt.asUintN(30, 0xffffffffn), 1073741823n); |
| 73 | +shouldBe(BigInt.asUintN(31, 0xffffffffn), 2147483647n); |
| 74 | +shouldBe(BigInt.asUintN(32, 0xffffffffn), 4294967295n); |
| 75 | +shouldBe(BigInt.asUintN(33, 0xffffffffn), 4294967295n); |
| 76 | +shouldBe(BigInt.asUintN(34, 0xffffffffn), 4294967295n); |
| 77 | + |
| 78 | +shouldBe(BigInt.asUintN(30, -0x80000000n), 0n); |
| 79 | +shouldBe(BigInt.asUintN(31, -0x80000000n), 0n); |
| 80 | +shouldBe(BigInt.asUintN(32, -0x80000000n), 2147483648n); |
| 81 | +shouldBe(BigInt.asUintN(33, -0x80000000n), 6442450944n); |
| 82 | +shouldBe(BigInt.asUintN(34, -0x80000000n), 15032385536n); |
| 83 | + |
| 84 | +shouldBe(BigInt.asUintN(30, -0x80000001n), 1073741823n); |
| 85 | +shouldBe(BigInt.asUintN(31, -0x80000001n), 2147483647n); |
| 86 | +shouldBe(BigInt.asUintN(32, -0x80000001n), 2147483647n); |
| 87 | +shouldBe(BigInt.asUintN(33, -0x80000001n), 6442450943n); |
| 88 | +shouldBe(BigInt.asUintN(34, -0x80000001n), 15032385535n); |
| 89 | + |
| 90 | +shouldBe(BigInt.asUintN(30, -0xffffffffn), 1n); |
| 91 | +shouldBe(BigInt.asUintN(31, -0xffffffffn), 1n); |
| 92 | +shouldBe(BigInt.asUintN(32, -0xffffffffn), 1n); |
| 93 | +shouldBe(BigInt.asUintN(33, -0xffffffffn), 4294967297n); |
| 94 | +shouldBe(BigInt.asUintN(34, -0xffffffffn), 12884901889n); |
| 95 | + |
| 96 | +shouldBe(BigInt.asUintN(62, 0x7fffffffffffffffn), 4611686018427387903n); |
| 97 | +shouldBe(BigInt.asUintN(63, 0x7fffffffffffffffn), 9223372036854775807n); |
| 98 | +shouldBe(BigInt.asUintN(64, 0x7fffffffffffffffn), 9223372036854775807n); |
| 99 | +shouldBe(BigInt.asUintN(65, 0x7fffffffffffffffn), 9223372036854775807n); |
| 100 | +shouldBe(BigInt.asUintN(66, 0x7fffffffffffffffn), 9223372036854775807n); |
| 101 | + |
| 102 | +shouldBe(BigInt.asUintN(62, 0x8000000000000000n), 0n); |
| 103 | +shouldBe(BigInt.asUintN(63, 0x8000000000000000n), 0n); |
| 104 | +shouldBe(BigInt.asUintN(64, 0x8000000000000000n), 9223372036854775808n); |
| 105 | +shouldBe(BigInt.asUintN(65, 0x8000000000000000n), 9223372036854775808n); |
| 106 | +shouldBe(BigInt.asUintN(66, 0x8000000000000000n), 9223372036854775808n); |
| 107 | + |
| 108 | +shouldBe(BigInt.asUintN(62, 0xffffffffffffffffn), 4611686018427387903n); |
| 109 | +shouldBe(BigInt.asUintN(63, 0xffffffffffffffffn), 9223372036854775807n); |
| 110 | +shouldBe(BigInt.asUintN(64, 0xffffffffffffffffn), 18446744073709551615n); |
| 111 | +shouldBe(BigInt.asUintN(65, 0xffffffffffffffffn), 18446744073709551615n); |
| 112 | +shouldBe(BigInt.asUintN(66, 0xffffffffffffffffn), 18446744073709551615n); |
| 113 | + |
| 114 | +shouldBe(BigInt.asUintN(62, -0x800000000000000n), 4035225266123964416n); |
| 115 | +shouldBe(BigInt.asUintN(63, -0x800000000000000n), 8646911284551352320n); |
| 116 | +shouldBe(BigInt.asUintN(64, -0x800000000000000n), 17870283321406128128n); |
| 117 | +shouldBe(BigInt.asUintN(65, -0x800000000000000n), 36317027395115679744n); |
| 118 | +shouldBe(BigInt.asUintN(66, -0x800000000000000n), 73210515542534782976n); |
| 119 | + |
| 120 | +shouldBe(BigInt.asUintN(62, -0x800000000000001n), 4035225266123964415n); |
| 121 | +shouldBe(BigInt.asUintN(63, -0x800000000000001n), 8646911284551352319n); |
| 122 | +shouldBe(BigInt.asUintN(64, -0x800000000000001n), 17870283321406128127n); |
| 123 | +shouldBe(BigInt.asUintN(65, -0x800000000000001n), 36317027395115679743n); |
| 124 | +shouldBe(BigInt.asUintN(66, -0x800000000000001n), 73210515542534782975n); |
| 125 | + |
| 126 | +shouldBe(BigInt.asUintN(62, -0xffffffffffffffffn), 1n); |
| 127 | +shouldBe(BigInt.asUintN(63, -0xffffffffffffffffn), 1n); |
| 128 | +shouldBe(BigInt.asUintN(64, -0xffffffffffffffffn), 1n); |
| 129 | +shouldBe(BigInt.asUintN(65, -0xffffffffffffffffn), 18446744073709551617n); |
| 130 | +shouldBe(BigInt.asUintN(66, -0xffffffffffffffffn), 55340232221128654849n); |
| 131 | +shouldBe(BigInt.asUintN(67, -0xffffffffffffffffn), 129127208515966861313n); |
0 commit comments