1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 13:07:46 +00:00

LibJS: Improve correctness of rounding and bitwise operations

Patch from Anonymous
This commit is contained in:
Andreas Kling 2021-02-05 09:11:58 +01:00
parent 6622ad8895
commit 16a0e7a66d
9 changed files with 106 additions and 19 deletions

View file

@ -0,0 +1,28 @@
test("basic rounding", () => {
expect(Math.round(1.25)).toBe(1);
expect(Math.round(-1.25)).toBe(-1);
expect(Math.round(1.5)).toBe(2);
expect(Math.round(-1.5)).toBe(-1);
expect(Math.round(1.75)).toBe(2);
expect(Math.round(-1.75)).toBe(-2);
expect(Math.round(1)).toBe(1);
expect(Math.round(-1)).toBe(-1);
expect(Math.round(4294967296.5)).toBe(4294967297);
expect(Math.round(-4294967296.5)).toBe(-4294967296);
expect(Math.round(4294967297)).toBe(4294967297);
expect(Math.round(-4294967297)).toBe(-4294967297);
});
test("basic floor", () => {
expect(Math.floor(1.25)).toBe(1);
expect(Math.floor(-1.25)).toBe(-2);
expect(Math.floor(1.5)).toBe(1);
expect(Math.floor(-1.5)).toBe(-2);
expect(Math.floor(1.75)).toBe(1);
expect(Math.floor(-1.75)).toBe(-2);
expect(Math.floor(1)).toBe(1);
expect(Math.floor(-1)).toBe(-1);
expect(Math.floor(4294967296.5)).toBe(4294967296);
expect(Math.floor(-4294967296.5)).toBe(-4294967297);
expect(Math.floor(4294967297)).toBe(4294967297);
expect(Math.floor(-4294967297)).toBe(-4294967297);
});

View file

@ -40,6 +40,9 @@ test("basic numeric and", () => {
expect(5 & 3).toBe(1);
expect(5 & 4).toBe(4);
expect(5 & 5).toBe(5);
expect(0xffffffff & 0).toBe(0);
expect(0xffffffff & 0xffffffff).toBe(-1);
});
test("and with non-numeric values", () => {

View file

@ -40,6 +40,11 @@ test("basic numeric shifting", () => {
expect(5 << 3).toBe(40);
expect(5 << 4).toBe(80);
expect(5 << 5).toBe(160);
expect(0xffffffff << 0).toBe(-1);
expect(0xffffffff << 16).toBe(-65536);
expect(0xffff0000 << 16).toBe(0);
expect(0xffff0000 << 15).toBe(-2147483648);
});
test("shifting with non-numeric values", () => {

View file

@ -40,6 +40,10 @@ test("basic numeric or", () => {
expect(5 | 3).toBe(7);
expect(5 | 4).toBe(5);
expect(5 | 5).toBe(5);
expect(0xffffffff | 0).toBe(-1);
expect(0xffffffff | 0xffffffff).toBe(-1);
expect(2147483648 | 0).toBe(-2147483648);
});
test("or with non-numeric values", () => {

View file

@ -26,6 +26,10 @@ test("basic numeric shifting", () => {
expect(42 >> 3).toBe(5);
expect(42 >> 4).toBe(2);
expect(42 >> 5).toBe(1);
expect(0xffffffff >> 0).toBe(-1);
expect(0xffffffff >> 16).toBe(-1);
expect((0xf0000000 * 2) >> 16).toBe(-8192);
});
test("numeric shifting with negative lhs values", () => {

View file

@ -26,6 +26,10 @@ test("basic numeric shifting", () => {
expect(42 >>> 3).toBe(5);
expect(42 >>> 4).toBe(2);
expect(42 >>> 5).toBe(1);
expect(0xffffffff >>> 0).toBe(4294967295);
expect(0xffffffff >>> 16).toBe(65535);
expect((0xf0000000 * 2) >>> 16).toBe(57344);
});
test("numeric shifting with negative lhs values", () => {

View file

@ -40,6 +40,9 @@ test("basic numeric xor", () => {
expect(5 ^ 3).toBe(6);
expect(5 ^ 4).toBe(1);
expect(5 ^ 5).toBe(0);
expect(0xffffffff ^ 0).toBe(-1);
expect(0xffffffff ^ 0xffffffff).toBe(0);
});
test("xor with non-numeric values", () => {