diff options
author | Nico Weber <thakis@chromium.org> | 2020-07-22 20:27:32 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-07-23 13:06:49 +0200 |
commit | 79a5ba58a5a318b913ec4ff9ba320f97128169e5 (patch) | |
tree | d3f15f90205e41f20a964b76483d3a87b1430558 /Libraries/LibJS | |
parent | bbc7e8429bda06534ba2685a9abc7bd2eac45374 (diff) | |
download | serenity-79a5ba58a5a318b913ec4ff9ba320f97128169e5.zip |
LibJS: Add tests for bitwise & and ^
And fix some edge case conversion bugs found by the tests.
Diffstat (limited to 'Libraries/LibJS')
-rw-r--r-- | Libraries/LibJS/Runtime/Value.cpp | 14 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/operators/binary-bitwise-and.js | 60 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/operators/binary-bitwise-xor.js | 60 |
3 files changed, 132 insertions, 2 deletions
diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp index a9fc4fb488..82eb484ad0 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -409,8 +409,11 @@ Value bitwise_and(Interpreter& interpreter, Value lhs, Value rhs) auto rhs_numeric = rhs.to_numeric(interpreter); if (interpreter.exception()) return {}; - if (both_number(lhs_numeric, rhs_numeric)) + if (both_number(lhs_numeric, rhs_numeric)) { + if (!lhs_numeric.is_finite_number() || !rhs_numeric.is_finite_number()) + return Value(0); return Value((i32)lhs_numeric.as_double() & (i32)rhs_numeric.as_double()); + } if (both_bigint(lhs_numeric, rhs_numeric)) return js_bigint(interpreter, lhs_numeric.as_bigint().big_integer().bitwise_and(rhs_numeric.as_bigint().big_integer())); interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise AND"); @@ -448,8 +451,15 @@ Value bitwise_xor(Interpreter& interpreter, Value lhs, Value rhs) auto rhs_numeric = rhs.to_numeric(interpreter); if (interpreter.exception()) return {}; - if (both_number(lhs_numeric, rhs_numeric)) + if (both_number(lhs_numeric, rhs_numeric)) { + if (!lhs_numeric.is_finite_number() && !rhs_numeric.is_finite_number()) + return Value(0); + if (!lhs_numeric.is_finite_number()) + return rhs_numeric; + if (!rhs_numeric.is_finite_number()) + return lhs_numeric; return Value((i32)lhs_numeric.as_double() ^ (i32)rhs_numeric.as_double()); + } if (both_bigint(lhs_numeric, rhs_numeric)) return js_bigint(interpreter, lhs_numeric.as_bigint().big_integer().bitwise_xor(rhs_numeric.as_bigint().big_integer())); interpreter.throw_exception<TypeError>(ErrorType::BigIntBadOperatorOtherType, "bitwise XOR"); diff --git a/Libraries/LibJS/Tests/operators/binary-bitwise-and.js b/Libraries/LibJS/Tests/operators/binary-bitwise-and.js new file mode 100644 index 0000000000..5eb4f19cc3 --- /dev/null +++ b/Libraries/LibJS/Tests/operators/binary-bitwise-and.js @@ -0,0 +1,60 @@ +test("basic numeric and", () => { + expect(0 & 0).toBe(0); + expect(0 & 1).toBe(0); + expect(0 & 2).toBe(0); + expect(0 & 3).toBe(0); + expect(0 & 4).toBe(0); + expect(0 & 5).toBe(0); + + expect(1 & 0).toBe(0); + expect(1 & 1).toBe(1); + expect(1 & 2).toBe(0); + expect(1 & 3).toBe(1); + expect(1 & 4).toBe(0); + expect(1 & 5).toBe(1); + + expect(2 & 0).toBe(0); + expect(2 & 1).toBe(0); + expect(2 & 2).toBe(2); + expect(2 & 3).toBe(2); + expect(2 & 4).toBe(0); + expect(2 & 5).toBe(0); + + expect(3 & 0).toBe(0); + expect(3 & 1).toBe(1); + expect(3 & 2).toBe(2); + expect(3 & 3).toBe(3); + expect(3 & 4).toBe(0); + expect(3 & 5).toBe(1); + + expect(4 & 0).toBe(0); + expect(4 & 1).toBe(0); + expect(4 & 2).toBe(0); + expect(4 & 3).toBe(0); + expect(4 & 4).toBe(4); + expect(4 & 5).toBe(4); + + expect(5 & 0).toBe(0); + expect(5 & 1).toBe(1); + expect(5 & 2).toBe(0); + expect(5 & 3).toBe(1); + expect(5 & 4).toBe(4); + expect(5 & 5).toBe(5); +}); + +test("and with non-numeric values", () => { + let x = 3; + let y = 7; + + expect("42" & 6).toBe(2); + expect(x & y).toBe(3); + expect(x & [[[[13]]]]).toBe(1); + expect(undefined & y).toBe(0); + expect("a" & "b").toBe(0); + expect(null & null).toBe(0); + expect(undefined & undefined).toBe(0); + expect(NaN & NaN).toBe(0); + expect(NaN & 6).toBe(0); + expect(Infinity & Infinity).toBe(0); + expect(-Infinity & Infinity).toBe(0); +}); diff --git a/Libraries/LibJS/Tests/operators/binary-bitwise-xor.js b/Libraries/LibJS/Tests/operators/binary-bitwise-xor.js new file mode 100644 index 0000000000..356ad01ca4 --- /dev/null +++ b/Libraries/LibJS/Tests/operators/binary-bitwise-xor.js @@ -0,0 +1,60 @@ +test("basic numeric xor", () => { + expect(0 ^ 0).toBe(0); + expect(0 ^ 1).toBe(1); + expect(0 ^ 2).toBe(2); + expect(0 ^ 3).toBe(3); + expect(0 ^ 4).toBe(4); + expect(0 ^ 5).toBe(5); + + expect(1 ^ 0).toBe(1); + expect(1 ^ 1).toBe(0); + expect(1 ^ 2).toBe(3); + expect(1 ^ 3).toBe(2); + expect(1 ^ 4).toBe(5); + expect(1 ^ 5).toBe(4); + + expect(2 ^ 0).toBe(2); + expect(2 ^ 1).toBe(3); + expect(2 ^ 2).toBe(0); + expect(2 ^ 3).toBe(1); + expect(2 ^ 4).toBe(6); + expect(2 ^ 5).toBe(7); + + expect(3 ^ 0).toBe(3); + expect(3 ^ 1).toBe(2); + expect(3 ^ 2).toBe(1); + expect(3 ^ 3).toBe(0); + expect(3 ^ 4).toBe(7); + expect(3 ^ 5).toBe(6); + + expect(4 ^ 0).toBe(4); + expect(4 ^ 1).toBe(5); + expect(4 ^ 2).toBe(6); + expect(4 ^ 3).toBe(7); + expect(4 ^ 4).toBe(0); + expect(4 ^ 5).toBe(1); + + expect(5 ^ 0).toBe(5); + expect(5 ^ 1).toBe(4); + expect(5 ^ 2).toBe(7); + expect(5 ^ 3).toBe(6); + expect(5 ^ 4).toBe(1); + expect(5 ^ 5).toBe(0); +}); + +test("xor with non-numeric values", () => { + let x = 3; + let y = 7; + + expect("42" ^ 6).toBe(44); + expect(x ^ y).toBe(4); + expect(x ^ [[[[12]]]]).toBe(15); + expect(undefined ^ y).toBe(7); + expect("a" ^ "b").toBe(0); + expect(null ^ null).toBe(0); + expect(undefined ^ undefined).toBe(0); + expect(NaN ^ NaN).toBe(0); + expect(NaN ^ 6).toBe(6); + expect(Infinity ^ Infinity).toBe(0); + expect(-Infinity ^ Infinity).toBe(0); +}); |