1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:47:45 +00:00

LibJS: Implement BigInt.asIntN

This commit is contained in:
Timothy Flynn 2022-02-05 20:10:52 -05:00 committed by Linus Groh
parent 72b3ea49d6
commit 460c2caaf7
2 changed files with 105 additions and 4 deletions

View file

@ -0,0 +1,79 @@
describe("errors", () => {
test("invalid index", () => {
expect(() => {
BigInt.asIntN(-1, 0n);
}).toThrowWithMessage(RangeError, "Index must be a positive integer");
expect(() => {
BigInt.asIntN(Symbol(), 0n);
}).toThrowWithMessage(TypeError, "Cannot convert symbol to number");
});
test("invalid BigInt", () => {
expect(() => {
BigInt.asIntN(1, 1);
}).toThrowWithMessage(TypeError, "Cannot convert number to BigInt");
expect(() => {
BigInt.asIntN(1, Symbol());
}).toThrowWithMessage(TypeError, "Cannot convert symbol to BigInt");
expect(() => {
BigInt.asIntN(1, "foo");
}).toThrowWithMessage(SyntaxError, "Invalid value for BigInt: foo");
});
});
describe("correct behavior", () => {
test("length is 2", () => {
expect(BigInt.asIntN).toHaveLength(2);
});
test("basic functionality", () => {
expect(BigInt.asIntN(0, -2n)).toBe(0n);
expect(BigInt.asIntN(0, -1n)).toBe(0n);
expect(BigInt.asIntN(0, 0n)).toBe(0n);
expect(BigInt.asIntN(0, 1n)).toBe(0n);
expect(BigInt.asIntN(0, 2n)).toBe(0n);
expect(BigInt.asIntN(1, -3n)).toBe(-1n);
expect(BigInt.asIntN(1, -2n)).toBe(0n);
expect(BigInt.asIntN(1, -1n)).toBe(-1n);
expect(BigInt.asIntN(1, 0n)).toBe(0n);
expect(BigInt.asIntN(1, 1n)).toBe(-1n);
expect(BigInt.asIntN(1, 2n)).toBe(0n);
expect(BigInt.asIntN(1, 3n)).toBe(-1n);
expect(BigInt.asIntN(2, -3n)).toBe(1n);
expect(BigInt.asIntN(2, -2n)).toBe(-2n);
expect(BigInt.asIntN(2, -1n)).toBe(-1n);
expect(BigInt.asIntN(2, 0n)).toBe(0n);
expect(BigInt.asIntN(2, 1n)).toBe(1n);
expect(BigInt.asIntN(2, 2n)).toBe(-2n);
expect(BigInt.asIntN(2, 3n)).toBe(-1n);
expect(BigInt.asIntN(4, -3n)).toBe(-3n);
expect(BigInt.asIntN(4, -2n)).toBe(-2n);
expect(BigInt.asIntN(4, -1n)).toBe(-1n);
expect(BigInt.asIntN(4, 0n)).toBe(0n);
expect(BigInt.asIntN(4, 1n)).toBe(1n);
expect(BigInt.asIntN(4, 2n)).toBe(2n);
expect(BigInt.asIntN(4, 3n)).toBe(3n);
const extremelyBigInt = 123456789123456789123456789123456789123456789123456789n;
expect(BigInt.asIntN(0, extremelyBigInt)).toBe(0n);
expect(BigInt.asIntN(1, extremelyBigInt)).toBe(-1n);
expect(BigInt.asIntN(2, extremelyBigInt)).toBe(1n);
expect(BigInt.asIntN(4, extremelyBigInt)).toBe(5n);
expect(BigInt.asIntN(128, extremelyBigInt)).toBe(-99061374399389259395070030194384019691n);
expect(BigInt.asIntN(256, extremelyBigInt)).toBe(extremelyBigInt);
expect(BigInt.asIntN(0, -extremelyBigInt)).toBe(0n);
expect(BigInt.asIntN(1, -extremelyBigInt)).toBe(-1n);
expect(BigInt.asIntN(2, -extremelyBigInt)).toBe(-1n);
expect(BigInt.asIntN(4, -extremelyBigInt)).toBe(-5n);
expect(BigInt.asIntN(128, -extremelyBigInt)).toBe(99061374399389259395070030194384019691n);
expect(BigInt.asIntN(256, -extremelyBigInt)).toBe(-extremelyBigInt);
});
});