mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 01:57:45 +00:00
Libraries: Move to Userland/Libraries/
This commit is contained in:
parent
dc28c07fa5
commit
13d7c09125
1857 changed files with 266 additions and 274 deletions
68
Userland/Libraries/LibJS/Tests/builtins/BigInt/BigInt.js
Normal file
68
Userland/Libraries/LibJS/Tests/builtins/BigInt/BigInt.js
Normal file
|
@ -0,0 +1,68 @@
|
|||
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);
|
||||
});
|
||||
});
|
||||
|
||||
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, "BigInt argument must be an integer");
|
||||
});
|
||||
});
|
||||
});
|
|
@ -0,0 +1,3 @@
|
|||
test("basic functionality", () => {
|
||||
expect(BigInt.prototype[Symbol.toStringTag]).toBe("BigInt");
|
||||
});
|
|
@ -0,0 +1,10 @@
|
|||
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 a BigInt object");
|
||||
});
|
|
@ -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 a BigInt object");
|
||||
});
|
|
@ -0,0 +1,12 @@
|
|||
test("basic functionality", () => {
|
||||
expect(BigInt.prototype.valueOf).toHaveLength(0);
|
||||
expect(typeof BigInt(123).valueOf()).toBe("bigint");
|
||||
// FIXME: Uncomment once we support Object() with argument
|
||||
// expect(typeof Object(123n).valueOf()).toBe("bigint");
|
||||
});
|
||||
|
||||
test("calling with non-BigInt |this|", () => {
|
||||
expect(() => {
|
||||
BigInt.prototype.valueOf.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not a BigInt object");
|
||||
});
|
|
@ -0,0 +1,80 @@
|
|||
describe("correct behavior", () => {
|
||||
test("typeof bigint", () => {
|
||||
expect(typeof 1n).toBe("bigint");
|
||||
});
|
||||
|
||||
test("bigint string coercion", () => {
|
||||
expect("" + 123n).toBe("123");
|
||||
});
|
||||
|
||||
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(1n | 2n).toBe(3n);
|
||||
expect(5n ^ 3n).toBe(6n);
|
||||
expect(~1n).toBe(-2n);
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("conversion to number", () => {
|
||||
expect(() => {
|
||||
+123n;
|
||||
}).toThrowWithMessage(TypeError, "Cannot convert BigInt to number");
|
||||
});
|
||||
});
|
|
@ -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