mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-03 14:50:18 +00:00
Everywhere: Hoist the Libraries folder to the top-level
This commit is contained in:
parent
950e819ee7
commit
93712b24bf
Notes:
github-actions[bot]
2024-11-10 11:51:52 +00:00
Author: https://github.com/trflynn89
Commit: 93712b24bf
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2256
Reviewed-by: https://github.com/sideshowbarker
4547 changed files with 104 additions and 113 deletions
79
Libraries/LibJS/Tests/builtins/BigInt/BigInt.asIntN.js
Normal file
79
Libraries/LibJS/Tests/builtins/BigInt/BigInt.asIntN.js
Normal 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);
|
||||
});
|
||||
});
|
77
Libraries/LibJS/Tests/builtins/BigInt/BigInt.asUintN.js
Normal file
77
Libraries/LibJS/Tests/builtins/BigInt/BigInt.asUintN.js
Normal file
|
@ -0,0 +1,77 @@
|
|||
describe("errors", () => {
|
||||
test("invalid index", () => {
|
||||
expect(() => {
|
||||
BigInt.asUintN(-1, 0n);
|
||||
}).toThrowWithMessage(RangeError, "Index must be a positive integer");
|
||||
|
||||
expect(() => {
|
||||
BigInt.asUintN(Symbol(), 0n);
|
||||
}).toThrowWithMessage(TypeError, "Cannot convert symbol to number");
|
||||
});
|
||||
|
||||
test("invalid BigInt", () => {
|
||||
expect(() => {
|
||||
BigInt.asUintN(1, 1);
|
||||
}).toThrowWithMessage(TypeError, "Cannot convert number to BigInt");
|
||||
|
||||
expect(() => {
|
||||
BigInt.asUintN(1, Symbol());
|
||||
}).toThrowWithMessage(TypeError, "Cannot convert symbol to BigInt");
|
||||
|
||||
expect(() => {
|
||||
BigInt.asUintN(1, "foo");
|
||||
}).toThrowWithMessage(SyntaxError, "Invalid value for BigInt: foo");
|
||||
});
|
||||
});
|
||||
|
||||
describe("correct behavior", () => {
|
||||
test("basic functionality", () => {
|
||||
expect(BigInt.asUintN(0, -2n)).toBe(0n);
|
||||
expect(BigInt.asUintN(0, -1n)).toBe(0n);
|
||||
expect(BigInt.asUintN(0, 0n)).toBe(0n);
|
||||
expect(BigInt.asUintN(0, 1n)).toBe(0n);
|
||||
expect(BigInt.asUintN(0, 2n)).toBe(0n);
|
||||
|
||||
expect(BigInt.asUintN(1, -3n)).toBe(1n);
|
||||
expect(BigInt.asUintN(1, -2n)).toBe(0n);
|
||||
expect(BigInt.asUintN(1, -1n)).toBe(1n);
|
||||
expect(BigInt.asUintN(1, 0n)).toBe(0n);
|
||||
expect(BigInt.asUintN(1, 1n)).toBe(1n);
|
||||
expect(BigInt.asUintN(1, 2n)).toBe(0n);
|
||||
expect(BigInt.asUintN(1, 3n)).toBe(1n);
|
||||
|
||||
expect(BigInt.asUintN(2, -3n)).toBe(1n);
|
||||
expect(BigInt.asUintN(2, -2n)).toBe(2n);
|
||||
expect(BigInt.asUintN(2, -1n)).toBe(3n);
|
||||
expect(BigInt.asUintN(2, 0n)).toBe(0n);
|
||||
expect(BigInt.asUintN(2, 1n)).toBe(1n);
|
||||
expect(BigInt.asUintN(2, 2n)).toBe(2n);
|
||||
expect(BigInt.asUintN(2, 3n)).toBe(3n);
|
||||
|
||||
expect(BigInt.asUintN(4, -3n)).toBe(13n);
|
||||
expect(BigInt.asUintN(4, -2n)).toBe(14n);
|
||||
expect(BigInt.asUintN(4, -1n)).toBe(15n);
|
||||
expect(BigInt.asUintN(4, 0n)).toBe(0n);
|
||||
expect(BigInt.asUintN(4, 1n)).toBe(1n);
|
||||
expect(BigInt.asUintN(4, 2n)).toBe(2n);
|
||||
expect(BigInt.asUintN(4, 3n)).toBe(3n);
|
||||
|
||||
const extremelyBigInt = 123456789123456789123456789123456789123456789123456789n;
|
||||
|
||||
expect(BigInt.asUintN(0, extremelyBigInt)).toBe(0n);
|
||||
expect(BigInt.asUintN(1, extremelyBigInt)).toBe(1n);
|
||||
expect(BigInt.asUintN(2, extremelyBigInt)).toBe(1n);
|
||||
expect(BigInt.asUintN(4, extremelyBigInt)).toBe(5n);
|
||||
expect(BigInt.asUintN(128, extremelyBigInt)).toBe(241220992521549204068304577237384191765n);
|
||||
expect(BigInt.asUintN(256, extremelyBigInt)).toBe(extremelyBigInt);
|
||||
|
||||
expect(BigInt.asUintN(0, -extremelyBigInt)).toBe(0n);
|
||||
expect(BigInt.asUintN(1, -extremelyBigInt)).toBe(1n);
|
||||
expect(BigInt.asUintN(2, -extremelyBigInt)).toBe(3n);
|
||||
expect(BigInt.asUintN(4, -extremelyBigInt)).toBe(11n);
|
||||
expect(BigInt.asUintN(128, -extremelyBigInt)).toBe(99061374399389259395070030194384019691n);
|
||||
expect(BigInt.asUintN(256, -extremelyBigInt)).toBe(
|
||||
115792089237316195423570861551898784396480861208851440582668460551124006183147n
|
||||
);
|
||||
});
|
||||
});
|
142
Libraries/LibJS/Tests/builtins/BigInt/BigInt.js
Normal file
142
Libraries/LibJS/Tests/builtins/BigInt/BigInt.js
Normal file
|
@ -0,0 +1,142 @@
|
|||
describe("correct behavior", () => {
|
||||
test("basic functionality", () => {
|
||||
expect(BigInt).toHaveLength(1);
|
||||
expect(BigInt.name).toBe("BigInt");
|
||||
});
|
||||
|
||||
test("constructor with numbers", () => {
|
||||
expect(BigInt(0)).toBe(0n);
|
||||
expect(BigInt(1)).toBe(1n);
|
||||
expect(BigInt(+1)).toBe(1n);
|
||||
expect(BigInt(-1)).toBe(-1n);
|
||||
expect(BigInt(123n)).toBe(123n);
|
||||
});
|
||||
|
||||
test("constructor with strings", () => {
|
||||
expect(BigInt("")).toBe(0n);
|
||||
expect(BigInt("0")).toBe(0n);
|
||||
expect(BigInt("1")).toBe(1n);
|
||||
expect(BigInt("+1")).toBe(1n);
|
||||
expect(BigInt("-1")).toBe(-1n);
|
||||
expect(BigInt("-1")).toBe(-1n);
|
||||
expect(BigInt("42")).toBe(42n);
|
||||
expect(BigInt(" \n 00100 \n ")).toBe(100n);
|
||||
expect(BigInt("3323214327642987348732109829832143298746432437532197321")).toBe(
|
||||
3323214327642987348732109829832143298746432437532197321n
|
||||
);
|
||||
});
|
||||
|
||||
test("constructor with objects", () => {
|
||||
expect(BigInt([])).toBe(0n);
|
||||
});
|
||||
|
||||
test("base-2 strings", () => {
|
||||
expect(BigInt("0b0")).toBe(0n);
|
||||
expect(BigInt("0B0")).toBe(0n);
|
||||
expect(BigInt("0b1")).toBe(1n);
|
||||
expect(BigInt("0B1")).toBe(1n);
|
||||
expect(BigInt("0b10")).toBe(2n);
|
||||
expect(BigInt("0B10")).toBe(2n);
|
||||
expect(BigInt(`0b${"1".repeat(100)}`)).toBe(1267650600228229401496703205375n);
|
||||
});
|
||||
|
||||
test("base-8 strings", () => {
|
||||
expect(BigInt("0o0")).toBe(0n);
|
||||
expect(BigInt("0O0")).toBe(0n);
|
||||
expect(BigInt("0o1")).toBe(1n);
|
||||
expect(BigInt("0O1")).toBe(1n);
|
||||
expect(BigInt("0o7")).toBe(7n);
|
||||
expect(BigInt("0O7")).toBe(7n);
|
||||
expect(BigInt("0o10")).toBe(8n);
|
||||
expect(BigInt("0O10")).toBe(8n);
|
||||
expect(BigInt(`0o1${"7".repeat(33)}`)).toBe(1267650600228229401496703205375n);
|
||||
});
|
||||
|
||||
test("base-16 strings", () => {
|
||||
expect(BigInt("0x0")).toBe(0n);
|
||||
expect(BigInt("0X0")).toBe(0n);
|
||||
expect(BigInt("0x1")).toBe(1n);
|
||||
expect(BigInt("0X1")).toBe(1n);
|
||||
expect(BigInt("0xf")).toBe(15n);
|
||||
expect(BigInt("0Xf")).toBe(15n);
|
||||
expect(BigInt("0x10")).toBe(16n);
|
||||
expect(BigInt("0X10")).toBe(16n);
|
||||
expect(BigInt(`0x${"f".repeat(25)}`)).toBe(1267650600228229401496703205375n);
|
||||
});
|
||||
|
||||
test("only coerces value once", () => {
|
||||
let calls = 0;
|
||||
const value = {
|
||||
[Symbol.toPrimitive]() {
|
||||
expect(calls).toBe(0);
|
||||
++calls;
|
||||
return "123";
|
||||
},
|
||||
};
|
||||
|
||||
expect(BigInt(value)).toEqual(123n);
|
||||
expect(calls).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test('cannot be constructed with "new"', () => {
|
||||
expect(() => {
|
||||
new BigInt();
|
||||
}).toThrowWithMessage(TypeError, "BigInt is not a constructor");
|
||||
});
|
||||
|
||||
test("invalid arguments", () => {
|
||||
expect(() => {
|
||||
BigInt(null);
|
||||
}).toThrowWithMessage(TypeError, "Cannot convert null to BigInt");
|
||||
|
||||
expect(() => {
|
||||
BigInt(undefined);
|
||||
}).toThrowWithMessage(TypeError, "Cannot convert undefined to BigInt");
|
||||
|
||||
expect(() => {
|
||||
BigInt(Symbol());
|
||||
}).toThrowWithMessage(TypeError, "Cannot convert symbol to BigInt");
|
||||
|
||||
["foo", "123n", "1+1", {}, function () {}].forEach(value => {
|
||||
expect(() => {
|
||||
BigInt(value);
|
||||
}).toThrowWithMessage(SyntaxError, `Invalid value for BigInt: ${value}`);
|
||||
});
|
||||
});
|
||||
|
||||
test("invalid numeric arguments", () => {
|
||||
[1.23, Infinity, -Infinity, NaN].forEach(value => {
|
||||
expect(() => {
|
||||
BigInt(value);
|
||||
}).toThrowWithMessage(RangeError, "Cannot convert non-integral number to BigInt");
|
||||
});
|
||||
});
|
||||
|
||||
test("invalid string for base", () => {
|
||||
["0b", "0b2", "0B02", "-0b1", "-0B1"].forEach(value => {
|
||||
expect(() => {
|
||||
BigInt(value);
|
||||
}).toThrowWithMessage(SyntaxError, `Invalid value for BigInt: ${value}`);
|
||||
});
|
||||
|
||||
["0o", "0o8", "0O08", "-0o1", "-0O1"].forEach(value => {
|
||||
expect(() => {
|
||||
BigInt(value);
|
||||
}).toThrowWithMessage(SyntaxError, `Invalid value for BigInt: ${value}`);
|
||||
});
|
||||
|
||||
["0x", "0xg", "0X0g", "-0x1", "-0X1"].forEach(value => {
|
||||
expect(() => {
|
||||
BigInt(value);
|
||||
}).toThrowWithMessage(SyntaxError, `Invalid value for BigInt: ${value}`);
|
||||
});
|
||||
|
||||
["a", "-1a"].forEach(value => {
|
||||
expect(() => {
|
||||
BigInt(value);
|
||||
}).toThrowWithMessage(SyntaxError, `Invalid value for BigInt: ${value}`);
|
||||
});
|
||||
});
|
||||
});
|
|
@ -0,0 +1,3 @@
|
|||
test("basic functionality", () => {
|
||||
expect(BigInt.prototype[Symbol.toStringTag]).toBe("BigInt");
|
||||
});
|
|
@ -0,0 +1,89 @@
|
|||
test("basic functionality", () => {
|
||||
expect(BigInt.prototype.toLocaleString).toHaveLength(0);
|
||||
expect(BigInt(123).toLocaleString()).toBe("123");
|
||||
});
|
||||
|
||||
test("calling with non-BigInt |this|", () => {
|
||||
expect(() => {
|
||||
BigInt.prototype.toLocaleString.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type BigInt");
|
||||
});
|
||||
|
||||
test("default", () => {
|
||||
expect(1n.toLocaleString("en")).toBe("1");
|
||||
expect(12n.toLocaleString("en")).toBe("12");
|
||||
expect(123n.toLocaleString("en")).toBe("123");
|
||||
expect(123456789123456789123456789123456789n.toLocaleString("en")).toBe(
|
||||
"123,456,789,123,456,789,123,456,789,123,456,789"
|
||||
);
|
||||
|
||||
const ar = new Intl.NumberFormat("ar");
|
||||
expect(1n.toLocaleString("ar")).toBe("\u0661");
|
||||
expect(12n.toLocaleString("ar")).toBe("\u0661\u0662");
|
||||
expect(123n.toLocaleString("ar")).toBe("\u0661\u0662\u0663");
|
||||
expect(123456789123456789123456789123456789n.toLocaleString("ar")).toBe(
|
||||
"\u0661\u0662\u0663\u066c\u0664\u0665\u0666\u066c\u0667\u0668\u0669\u066c\u0661\u0662\u0663\u066c\u0664\u0665\u0666\u066c\u0667\u0668\u0669\u066c\u0661\u0662\u0663\u066c\u0664\u0665\u0666\u066c\u0667\u0668\u0669\u066c\u0661\u0662\u0663\u066c\u0664\u0665\u0666\u066c\u0667\u0668\u0669"
|
||||
);
|
||||
});
|
||||
|
||||
test("integer digits", () => {
|
||||
expect(1n.toLocaleString("en", { minimumIntegerDigits: 2 })).toBe("01");
|
||||
expect(12n.toLocaleString("en", { minimumIntegerDigits: 2 })).toBe("12");
|
||||
expect(123n.toLocaleString("en", { minimumIntegerDigits: 2 })).toBe("123");
|
||||
|
||||
expect(1n.toLocaleString("ar", { minimumIntegerDigits: 2 })).toBe("\u0660\u0661");
|
||||
expect(12n.toLocaleString("ar", { minimumIntegerDigits: 2 })).toBe("\u0661\u0662");
|
||||
expect(123n.toLocaleString("ar", { minimumIntegerDigits: 2 })).toBe("\u0661\u0662\u0663");
|
||||
});
|
||||
|
||||
test("significant digits", () => {
|
||||
expect(
|
||||
1n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("1.000");
|
||||
expect(
|
||||
12n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("12.00");
|
||||
expect(
|
||||
123n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("123.0");
|
||||
expect(
|
||||
1234n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("1,234");
|
||||
expect(
|
||||
12345n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("12,345");
|
||||
expect(
|
||||
123456n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("123,456");
|
||||
expect(
|
||||
1234567n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("1,234,570");
|
||||
expect(
|
||||
1234561n.toLocaleString("en", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("1,234,560");
|
||||
|
||||
expect(
|
||||
1n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("\u0661\u066b\u0660\u0660\u0660");
|
||||
expect(
|
||||
12n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("\u0661\u0662\u066b\u0660\u0660");
|
||||
expect(
|
||||
123n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("\u0661\u0662\u0663\u066b\u0660");
|
||||
expect(
|
||||
1234n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("\u0661\u066c\u0662\u0663\u0664");
|
||||
expect(
|
||||
12345n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("\u0661\u0662\u066c\u0663\u0664\u0665");
|
||||
expect(
|
||||
123456n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("\u0661\u0662\u0663\u066c\u0664\u0665\u0666");
|
||||
expect(
|
||||
1234567n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("\u0661\u066c\u0662\u0663\u0664\u066c\u0665\u0667\u0660");
|
||||
expect(
|
||||
1234561n.toLocaleString("ar", { minimumSignificantDigits: 4, maximumSignificantDigits: 6 })
|
||||
).toBe("\u0661\u066c\u0662\u0663\u0664\u066c\u0665\u0666\u0660");
|
||||
});
|
|
@ -0,0 +1,10 @@
|
|||
test("basic functionality", () => {
|
||||
expect(BigInt.prototype.toString).toHaveLength(0);
|
||||
expect(BigInt(123).toString()).toBe("123");
|
||||
});
|
||||
|
||||
test("calling with non-BigInt |this|", () => {
|
||||
expect(() => {
|
||||
BigInt.prototype.toString.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type BigInt");
|
||||
});
|
|
@ -0,0 +1,11 @@
|
|||
test("basic functionality", () => {
|
||||
expect(BigInt.prototype.valueOf).toHaveLength(0);
|
||||
expect(typeof BigInt(123).valueOf()).toBe("bigint");
|
||||
expect(typeof Object(123n).valueOf()).toBe("bigint");
|
||||
});
|
||||
|
||||
test("calling with non-BigInt |this|", () => {
|
||||
expect(() => {
|
||||
BigInt.prototype.valueOf.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type BigInt");
|
||||
});
|
219
Libraries/LibJS/Tests/builtins/BigInt/bigint-basic.js
Normal file
219
Libraries/LibJS/Tests/builtins/BigInt/bigint-basic.js
Normal file
|
@ -0,0 +1,219 @@
|
|||
describe("correct behavior", () => {
|
||||
test("typeof bigint", () => {
|
||||
expect(typeof 1n).toBe("bigint");
|
||||
});
|
||||
|
||||
test("bigint string coercion", () => {
|
||||
expect("" + 123n).toBe("123");
|
||||
});
|
||||
|
||||
test("hex literals", () => {
|
||||
expect(0xffn).toBe(255n);
|
||||
});
|
||||
|
||||
test("octal literals", () => {
|
||||
expect(0o10n).toBe(8n);
|
||||
});
|
||||
|
||||
test("binary literals", () => {
|
||||
expect(0b10n).toBe(2n);
|
||||
});
|
||||
|
||||
test("arithmetic operators", () => {
|
||||
let bigint = 123n;
|
||||
expect(-bigint).toBe(-123n);
|
||||
|
||||
expect(12n + 34n).toBe(46n);
|
||||
expect(12n - 34n).toBe(-22n);
|
||||
expect(8n * 12n).toBe(96n);
|
||||
expect(123n / 10n).toBe(12n);
|
||||
expect(2n ** 3n).toBe(8n);
|
||||
expect(5n % 3n).toBe(2n);
|
||||
expect(
|
||||
45977665298704210987n +
|
||||
(714320987142450987412098743217984576n / 4598741987421098765327980n) * 987498743n
|
||||
).toBe(199365500239020623962n);
|
||||
});
|
||||
|
||||
test("bitwise operators", () => {
|
||||
expect(12n & 5n).toBe(4n);
|
||||
expect(3n & -2n).toBe(2n);
|
||||
expect(-3n & -2n).toBe(-4n);
|
||||
expect(-3n & 2n).toBe(0n);
|
||||
expect(0xffff_ffffn & -1n).toBe(0xffff_ffffn);
|
||||
|
||||
expect(1n | 2n).toBe(3n);
|
||||
expect(0n | -1n).toBe(-1n);
|
||||
expect(0n | -2n).toBe(-2n);
|
||||
|
||||
expect(5n ^ 3n).toBe(6n);
|
||||
|
||||
expect(~1n).toBe(-2n);
|
||||
expect(~-1n).toBe(0n);
|
||||
|
||||
expect(5n << 2n).toBe(20n);
|
||||
expect(7n >> 1n).toBe(3n);
|
||||
});
|
||||
|
||||
test("increment operators", () => {
|
||||
let bigint = 1n;
|
||||
expect(bigint++).toBe(1n);
|
||||
expect(bigint).toBe(2n);
|
||||
expect(bigint--).toBe(2n);
|
||||
expect(bigint).toBe(1n);
|
||||
expect(++bigint).toBe(2n);
|
||||
expect(bigint).toBe(2n);
|
||||
expect(--bigint).toBe(1n);
|
||||
expect(bigint).toBe(1n);
|
||||
});
|
||||
|
||||
test("weak equality operators", () => {
|
||||
expect(1n == 1n).toBeTrue();
|
||||
expect(1n == 1).toBeTrue();
|
||||
expect(1 == 1n).toBeTrue();
|
||||
expect(1n == 1.23).toBeFalse();
|
||||
expect(1.23 == 1n).toBeFalse();
|
||||
|
||||
expect(1n != 1n).toBeFalse();
|
||||
expect(1n != 1).toBeFalse();
|
||||
expect(1 != 1n).toBeFalse();
|
||||
expect(1n != 1.23).toBeTrue();
|
||||
expect(1.23 != 1n).toBeTrue();
|
||||
|
||||
const a = 552141064586571465517761649840658n;
|
||||
const b = 704179908449526267977309288010258n;
|
||||
expect(a == a).toBeTrue();
|
||||
expect(a == b).toBeFalse();
|
||||
|
||||
expect(0n == Object(0n)).toBeTrue();
|
||||
expect(Object(0n) == 0n).toBeTrue();
|
||||
expect(0n != Object(0n)).toBeFalse();
|
||||
expect(Object(0n) != 0n).toBeFalse();
|
||||
|
||||
expect(1n == Object(1n)).toBeTrue();
|
||||
expect(Object(1n) == 1n).toBeTrue();
|
||||
expect(1n != Object(1n)).toBeFalse();
|
||||
expect(Object(1n) != 1n).toBeFalse();
|
||||
|
||||
expect(1n == Object(2n)).toBeFalse();
|
||||
expect(Object(2n) == 1n).toBeFalse();
|
||||
expect(1n != Object(2n)).toBeTrue();
|
||||
expect(Object(2n) != 1n).toBeTrue();
|
||||
|
||||
expect(2n == "2").toBeTrue();
|
||||
expect(2n == "0b10").toBeTrue();
|
||||
expect(2n == "0o2").toBeTrue();
|
||||
expect(2n == "0x2").toBeTrue();
|
||||
|
||||
expect(1n == "2").toBeFalse();
|
||||
expect(1n == "0b10").toBeFalse();
|
||||
expect(1n == "0o2").toBeFalse();
|
||||
expect(1n == "0x2").toBeFalse();
|
||||
});
|
||||
|
||||
test("strong equality operators", () => {
|
||||
expect(1n === 1n).toBeTrue();
|
||||
expect(1n === 1).toBeFalse();
|
||||
expect(1 === 1n).toBeFalse();
|
||||
expect(1n === 1.23).toBeFalse();
|
||||
expect(1.23 === 1n).toBeFalse();
|
||||
|
||||
expect(1n !== 1n).toBeFalse();
|
||||
expect(1n !== 1).toBeTrue();
|
||||
expect(1 !== 1n).toBeTrue();
|
||||
expect(1n !== 1.23).toBeTrue();
|
||||
expect(1.23 !== 1n).toBeTrue();
|
||||
|
||||
const a = 552141064586571465517761649840658n;
|
||||
const b = 704179908449526267977309288010258n;
|
||||
expect(a === a).toBeTrue();
|
||||
expect(a === b).toBeFalse();
|
||||
});
|
||||
|
||||
test("less-than operators", () => {
|
||||
expect(1n < 1n).toBeFalse();
|
||||
expect(1n < 1).toBeFalse();
|
||||
expect(1 < 1n).toBeFalse();
|
||||
expect(1n < 2n).toBeTrue();
|
||||
expect(1n < 2).toBeTrue();
|
||||
expect(1 < 2n).toBeTrue();
|
||||
expect(1n < 1.23).toBeTrue();
|
||||
expect(1.23 < 1n).toBeFalse();
|
||||
|
||||
expect(1n <= 1n).toBeTrue();
|
||||
expect(1n <= 1).toBeTrue();
|
||||
expect(1 <= 1n).toBeTrue();
|
||||
expect(1n <= 2n).toBeTrue();
|
||||
expect(1n <= 2).toBeTrue();
|
||||
expect(1 <= 2n).toBeTrue();
|
||||
expect(1n <= 1.23).toBeTrue();
|
||||
expect(1.23 <= 1n).toBeFalse();
|
||||
|
||||
expect(1n < "1").toBeFalse();
|
||||
expect(1n < "2").toBeTrue();
|
||||
expect(1n < "1.23").toBeFalse();
|
||||
|
||||
expect(1n <= "1").toBeTrue();
|
||||
expect(1n <= "2").toBeTrue();
|
||||
expect(1n <= "1.23").toBeFalse();
|
||||
|
||||
expect(1n < "0b1").toBeFalse();
|
||||
expect(1n < "0B10").toBeTrue();
|
||||
expect(1n < "0o1").toBeFalse();
|
||||
expect(1n < "0O2").toBeTrue();
|
||||
expect(1n < "0x1").toBeFalse();
|
||||
expect(1n < "0X2").toBeTrue();
|
||||
|
||||
expect(1n <= "0b1").toBeTrue();
|
||||
expect(1n <= "0B10").toBeTrue();
|
||||
expect(1n <= "0o1").toBeTrue();
|
||||
expect(1n <= "0O2").toBeTrue();
|
||||
expect(1n <= "0x1").toBeTrue();
|
||||
expect(1n <= "0X2").toBeTrue();
|
||||
|
||||
expect("1" < 1n).toBeFalse();
|
||||
expect("1" < 2n).toBeTrue();
|
||||
expect("1.23" < 1n).toBeFalse();
|
||||
|
||||
expect("1" <= 1n).toBeTrue();
|
||||
expect("1" <= 2n).toBeTrue();
|
||||
expect("1.23" <= 1n).toBeFalse();
|
||||
|
||||
expect("0b1" < 1n).toBeFalse();
|
||||
expect("0B1" < 2n).toBeTrue();
|
||||
expect("0o1" < 1n).toBeFalse();
|
||||
expect("0O1" < 2n).toBeTrue();
|
||||
expect("0x1" < 1n).toBeFalse();
|
||||
expect("0X1" < 2n).toBeTrue();
|
||||
|
||||
expect("0b1" <= 1n).toBeTrue();
|
||||
expect("0B10" <= 2n).toBeTrue();
|
||||
expect("0o1" <= 1n).toBeTrue();
|
||||
expect("0O2" <= 2n).toBeTrue();
|
||||
expect("0x1" <= 1n).toBeTrue();
|
||||
expect("0X2" <= 2n).toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("conversion to number", () => {
|
||||
expect(() => {
|
||||
+123n;
|
||||
}).toThrowWithMessage(TypeError, "Cannot convert BigInt to number");
|
||||
});
|
||||
|
||||
test("division by zero", () => {
|
||||
expect(() => {
|
||||
1n / 0n;
|
||||
}).toThrowWithMessage(RangeError, "Division by zero");
|
||||
expect(() => {
|
||||
1n % 0n;
|
||||
}).toThrowWithMessage(RangeError, "Division by zero");
|
||||
});
|
||||
|
||||
test("negative exponent", () => {
|
||||
expect(() => {
|
||||
1n ** -1n;
|
||||
}).toThrowWithMessage(RangeError, "Exponent must be positive");
|
||||
});
|
||||
});
|
8
Libraries/LibJS/Tests/builtins/BigInt/bigint-minus.js
Normal file
8
Libraries/LibJS/Tests/builtins/BigInt/bigint-minus.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
describe("minus behavior", () => {
|
||||
test("the basics", () => {
|
||||
expect(3n - 4n).toBe(-1n);
|
||||
expect(3n - -4n).toBe(7n);
|
||||
expect(-3n - -4n).toBe(1n);
|
||||
expect(-3n - 4n).toBe(-7n);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,31 @@
|
|||
const doTest = (operatorName, executeOperation) => {
|
||||
[1, null, undefined].forEach(value => {
|
||||
const messageSuffix = operatorName === "unsigned right-shift" ? "" : " and other type";
|
||||
|
||||
expect(() => {
|
||||
executeOperation(1n, value);
|
||||
}).toThrowWithMessage(
|
||||
TypeError,
|
||||
`Cannot use ${operatorName} operator with BigInt${messageSuffix}`
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
[
|
||||
["addition", (a, b) => a + b],
|
||||
["subtraction", (a, b) => a - b],
|
||||
["multiplication", (a, b) => a * b],
|
||||
["division", (a, b) => a / b],
|
||||
["modulo", (a, b) => a % b],
|
||||
["exponentiation", (a, b) => a ** b],
|
||||
["bitwise OR", (a, b) => a | b],
|
||||
["bitwise AND", (a, b) => a & b],
|
||||
["bitwise XOR", (a, b) => a ^ b],
|
||||
["left-shift", (a, b) => a << b],
|
||||
["right-shift", (a, b) => a >> b],
|
||||
["unsigned right-shift", (a, b) => a >>> b],
|
||||
].forEach(testCase => {
|
||||
test(`using ${testCase[0]} operator with BigInt and other type`, () => {
|
||||
doTest(testCase[0], testCase[1]);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue