Source code
Revision control
Copy as Markdown
Other Tools
const tests = [
[-0x80000000n, 0n, 1n],
[-0x7fffffffn, 0n, 1n],
[-0x7ffffffen, 0n, 1n],
[-9n, 0n, 1n],
[-8n, 0n, 1n],
[-7n, 0n, 1n],
[-6n, 0n, 1n],
[-5n, 0n, 1n],
[-4n, 0n, 1n],
[-3n, 0n, 1n],
[-2n, 0n, 1n],
[-1n, 0n, 1n],
[0n, 0n, 1n],
[1n, 0n, 1n],
[2n, 0n, 1n],
[3n, 0n, 1n],
[4n, 0n, 1n],
[5n, 0n, 1n],
[6n, 0n, 1n],
[7n, 0n, 1n],
[8n, 0n, 1n],
[9n, 0n, 1n],
[0x7ffffffen, 0n, 1n],
[0x7fffffffn, 0n, 1n],
[-0x80000000n, 1n, -0x80000000n],
[-0x7fffffffn, 1n, -0x7fffffffn],
[-0x7ffffffen, 1n, -0x7ffffffen],
[-9n, 1n, -9n],
[-8n, 1n, -8n],
[-7n, 1n, -7n],
[-6n, 1n, -6n],
[-5n, 1n, -5n],
[-4n, 1n, -4n],
[-3n, 1n, -3n],
[-2n, 1n, -2n],
[-1n, 1n, -1n],
[0n, 1n, 0n],
[1n, 1n, 1n],
[2n, 1n, 2n],
[3n, 1n, 3n],
[4n, 1n, 4n],
[5n, 1n, 5n],
[6n, 1n, 6n],
[7n, 1n, 7n],
[8n, 1n, 8n],
[9n, 1n, 9n],
[0x7ffffffen, 1n, 0x7ffffffen],
[0x7fffffffn, 1n, 0x7fffffffn],
[-9n, 2n, 0x51n],
[-8n, 2n, 0x40n],
[-7n, 2n, 0x31n],
[-6n, 2n, 0x24n],
[-5n, 2n, 0x19n],
[-4n, 2n, 0x10n],
[-3n, 2n, 9n],
[-2n, 2n, 4n],
[-1n, 2n, 1n],
[0n, 2n, 0n],
[1n, 2n, 1n],
[2n, 2n, 4n],
[3n, 2n, 9n],
[4n, 2n, 0x10n],
[5n, 2n, 0x19n],
[6n, 2n, 0x24n],
[7n, 2n, 0x31n],
[8n, 2n, 0x40n],
[9n, 2n, 0x51n],
[-9n, 3n, -0x2d9n],
[-8n, 3n, -0x200n],
[-7n, 3n, -0x157n],
[-6n, 3n, -0xd8n],
[-5n, 3n, -0x7dn],
[-4n, 3n, -0x40n],
[-3n, 3n, -0x1bn],
[-2n, 3n, -8n],
[-1n, 3n, -1n],
[0n, 3n, 0n],
[1n, 3n, 1n],
[2n, 3n, 8n],
[3n, 3n, 0x1bn],
[4n, 3n, 0x40n],
[5n, 3n, 0x7dn],
[6n, 3n, 0xd8n],
[7n, 3n, 0x157n],
[8n, 3n, 0x200n],
[9n, 3n, 0x2d9n],
[-9n, 4n, 0x19a1n],
[-8n, 4n, 0x1000n],
[-7n, 4n, 0x961n],
[-6n, 4n, 0x510n],
[-5n, 4n, 0x271n],
[-4n, 4n, 0x100n],
[-3n, 4n, 0x51n],
[-2n, 4n, 0x10n],
[-1n, 4n, 1n],
[0n, 4n, 0n],
[1n, 4n, 1n],
[2n, 4n, 0x10n],
[3n, 4n, 0x51n],
[4n, 4n, 0x100n],
[5n, 4n, 0x271n],
[6n, 4n, 0x510n],
[7n, 4n, 0x961n],
[8n, 4n, 0x1000n],
[9n, 4n, 0x19a1n],
[-2n, 0x1fn, -0x80000000n],
[-1n, 0x1fn, -1n],
[0n, 0x1fn, 0n],
[1n, 0x1fn, 1n],
[-1n, 0x20n, 1n],
[0n, 0x20n, 0n],
[1n, 0x20n, 1n],
[-1n, 0x3fn, -1n],
[0n, 0x3fn, 0n],
[1n, 0x3fn, 1n],
[-1n, 0x40n, 1n],
[0n, 0x40n, 0n],
[1n, 0x40n, 1n],
];
// Cases not covered above.
tests.push(
// 0 ** (2**31)
[0n, 0x7fffffffn, 0n],
// 1 ** (2**31)
[1n, 0x7fffffffn, 1n],
// -1 ** (2**31)
[-1n, 0x7fffffffn, -1n],
);
function f(tests) {
for (let test of tests) {
let lhs = test[0], rhs = test[1], expected = test[2];
assertEq(BigInt.asIntN(32, lhs), lhs);
assertEq(BigInt.asIntN(32, rhs), rhs);
assertEq(BigInt.asIntN(32, expected), expected);
assertEq(lhs ** rhs, expected);
}
}
for (let i = 0; i < 10; ++i) {
f(tests);
}