Implement relational operators for BigInts (#4087)
Improve BigInt constructor as well JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
function check_result(bigint, expected)
|
||||
{
|
||||
assert(bigint.toString() === expected)
|
||||
}
|
||||
|
||||
function check_error (code, error_type)
|
||||
{
|
||||
try {
|
||||
eval(code)
|
||||
assert(false)
|
||||
} catch (e) {
|
||||
assert(e instanceof error_type)
|
||||
}
|
||||
}
|
||||
|
||||
check_error("BigInt(undefined)", TypeError)
|
||||
check_error("BigInt(null)", TypeError)
|
||||
check_error("BigInt(Symbol())", TypeError)
|
||||
|
||||
check_error("BigInt(0.25)", RangeError)
|
||||
check_error("BigInt(-0.25)", RangeError)
|
||||
check_error("BigInt(-10000000.25)", RangeError)
|
||||
check_error("BigInt(4503599627370495.5)", RangeError)
|
||||
check_error("BigInt(NaN)", RangeError)
|
||||
check_error("BigInt(Infinity)", RangeError)
|
||||
|
||||
check_result(BigInt(true), "1")
|
||||
check_result(BigInt(false), "0")
|
||||
check_result(BigInt({ valueOf() { return "0x100" } }), "256")
|
||||
|
||||
check_result(BigInt(0), "0")
|
||||
check_result(BigInt(-0), "0")
|
||||
check_result(BigInt(8192), "8192")
|
||||
check_result(BigInt(-0xffffffffff), "-1099511627775")
|
||||
check_result(BigInt(0x1fffffffffffff), "9007199254740991")
|
||||
check_result(BigInt(-4503599627370496), "-4503599627370496")
|
||||
check_result(BigInt(4503599627370496.5), "4503599627370496")
|
||||
check_result(BigInt(9007199254740991.5), "9007199254740992")
|
||||
check_result(BigInt(0x1fffffffffffff * (2 ** 70)), "10633823966279325802638835764831453184")
|
||||
check_result(BigInt(-0x1fffffffffffff * (2 ** 128)), "-3064991081731777376434327133362154903862870812598992896")
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
/* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/* Strict equal. */
|
||||
|
||||
assert(BigInt("0") === BigInt("0"))
|
||||
assert(BigInt("-77") === BigInt("-77"))
|
||||
assert(!(BigInt("-77") !== BigInt("-77")))
|
||||
|
||||
assert(!(Object(BigInt("-77")) === BigInt("-77")))
|
||||
assert(BigInt("-77") !== Object(BigInt("-77")))
|
||||
|
||||
assert(BigInt("0xffffffffffffffffffffffffffffffff") === BigInt("0xffffffffffffffffffffffffffffffff"))
|
||||
assert(!(BigInt("0xfffffffffffffffffffffffffffffffe") === BigInt("0xffffffffffffffffffffffffffffffff")))
|
||||
assert(BigInt("0x100000000000000000000000000000000") !== BigInt("0xffffffffffffffffffffffffffffffff"))
|
||||
assert(!(BigInt("1234") === 1234))
|
||||
assert(-4567 !== BigInt("-4567"))
|
||||
|
||||
/* Equal. */
|
||||
|
||||
assert(BigInt("0x100") == BigInt("256"))
|
||||
assert(BigInt("-77") == "-77")
|
||||
assert("168" == BigInt("168"))
|
||||
assert(!("0xffffffffffffffffffffffffffffffff" != BigInt("0xffffffffffffffffffffffffffffffff")))
|
||||
|
||||
assert(BigInt("0x1000") == 0x1000)
|
||||
assert(468123 == BigInt("468123"))
|
||||
|
||||
assert(!(BigInt("100000000") == 100000000.5))
|
||||
assert(-0.125 != BigInt("0"))
|
||||
assert(!("InvalidBigIntString" == BigInt("100000000")))
|
||||
assert(BigInt("100000000") != "10000 0000")
|
||||
|
||||
assert(BigInt("0") == 0)
|
||||
assert(!(-0 != BigInt("0")))
|
||||
assert(!(BigInt("0") == 0.0000152587890625))
|
||||
assert(0.0000152587890625 != BigInt("0"))
|
||||
|
||||
assert(!(BigInt("100000000") == NaN))
|
||||
assert(NaN != BigInt("-100000000"))
|
||||
assert(!(BigInt("100000000000000000000000000") == Infinity))
|
||||
assert(Infinity != BigInt("100000000000000000000000000"))
|
||||
|
||||
/* Relational. */
|
||||
|
||||
assert(!(BigInt("1234") < BigInt("1234")))
|
||||
assert(BigInt("1234") >= BigInt("1234"))
|
||||
assert(BigInt("1234") <= BigInt("1234"))
|
||||
assert(!(BigInt("1234") > BigInt("1234")))
|
||||
|
||||
assert(BigInt("1234") < BigInt("1235"))
|
||||
assert(!(BigInt("1234") >= BigInt("1235")))
|
||||
assert(BigInt("1234") <= BigInt("1235"))
|
||||
assert(!(BigInt("1234") > BigInt("1235")))
|
||||
|
||||
assert(!(BigInt("123456789012345678901234567890") < "123456789012345678901234567890"))
|
||||
assert(BigInt("123456789012345678901234567890") >= "123456789012345678901234567890")
|
||||
assert(BigInt("123456789012345678901234567890") <= "123456789012345678901234567890")
|
||||
assert(!(BigInt("123456789012345678901234567890") > "123456789012345678901234567890"))
|
||||
|
||||
assert(!("0x1234567890abcdef1234567890abcdef" < BigInt("0x1234567890abcdef1234567890abcdef")))
|
||||
assert("0x1234567890abcdef1234567890abcdef" >= BigInt("0x1234567890abcdef1234567890abcdef"))
|
||||
assert("0x1234567890abcdef1234567890abcdef" <= BigInt("0x1234567890abcdef1234567890abcdef"))
|
||||
assert(!("0x1234567890abcdef1234567890abcdef" > BigInt("0x1234567890abcdef1234567890abcdef")))
|
||||
|
||||
assert(!("Invalid" < BigInt("100")))
|
||||
assert(!("Invalid" >= BigInt("100")))
|
||||
assert(!("Invalid" <= BigInt("100")))
|
||||
assert(!("Invalid" > BigInt("100")))
|
||||
|
||||
assert(!(BigInt("0") < "NotABigInt"))
|
||||
assert(!(BigInt("0") >= "NotABigInt"))
|
||||
assert(!(BigInt("0") <= "NotABigInt"))
|
||||
assert(!(BigInt("0") > "NotABigInt"))
|
||||
|
||||
assert(!(BigInt("0") < 0))
|
||||
assert(BigInt("0") >= 0)
|
||||
assert(BigInt("0") <= 0)
|
||||
assert(!(BigInt("0") > 0))
|
||||
|
||||
assert(!(-0 < BigInt("0")))
|
||||
assert(-0 >= BigInt("0"))
|
||||
assert(-0 <= BigInt("0"))
|
||||
assert(!(-0 > BigInt("0")))
|
||||
|
||||
assert(BigInt("0") < 67)
|
||||
assert(!(BigInt("0") > 67))
|
||||
assert(!(BigInt("0") < -0.125))
|
||||
assert(BigInt("0") > -0.125)
|
||||
|
||||
assert(!(BigInt("7") < NaN))
|
||||
assert(!(BigInt("7") >= NaN))
|
||||
assert(!(BigInt("7") <= NaN))
|
||||
assert(!(BigInt("7") > NaN))
|
||||
|
||||
assert(!(Infinity < BigInt("1000000000000000000000000000000")))
|
||||
assert(!(Infinity <= BigInt("1000000000000000000000000000000")))
|
||||
assert(Infinity >= BigInt("1000000000000000000000000000000"))
|
||||
assert(Infinity > BigInt("1000000000000000000000000000000"))
|
||||
|
||||
assert(-Infinity < BigInt("1000000000000000000000000000000"))
|
||||
assert(-Infinity <= BigInt("1000000000000000000000000000000"))
|
||||
assert(!(-Infinity >= BigInt("1000000000000000000000000000000")))
|
||||
assert(!(-Infinity > BigInt("1000000000000000000000000000000")))
|
||||
|
||||
assert(BigInt("-10000") < 1)
|
||||
assert(BigInt("-10000") <= 1)
|
||||
assert(!(BigInt("-10000") >= 1))
|
||||
assert(!(BigInt("-10000") > 1))
|
||||
|
||||
assert(!(1 < BigInt("-12345678")))
|
||||
assert(!(1 <= BigInt("-12345678")))
|
||||
assert(1 >= BigInt("-12345678"))
|
||||
assert(1 > BigInt("-12345678"))
|
||||
|
||||
assert(!(BigInt("1") < 0.5))
|
||||
assert(!(BigInt("1") <= 0.5))
|
||||
assert(BigInt("1") >= 0.5)
|
||||
assert(BigInt("1") > 0.5)
|
||||
|
||||
assert(!(-0.5 < BigInt("-1")))
|
||||
assert(!(-0.5 <= BigInt("-1")))
|
||||
assert(-0.5 >= BigInt("-1"))
|
||||
assert(-0.5 > BigInt("-1"))
|
||||
|
||||
assert(!(BigInt("0x1000000000000000000000000000000") < 0x100000))
|
||||
assert(!(BigInt("0x1000000000000000000000000000000") <= 0x100000))
|
||||
assert(BigInt("0x1000000000000000000000000000000") >= 0x100000)
|
||||
assert(BigInt("0x1000000000000000000000000000000") > 0x100000)
|
||||
|
||||
assert(-0x1000000000000000000000000000000 < BigInt("-1234"))
|
||||
assert(-0x1000000000000000000000000000000 <= BigInt("-1234"))
|
||||
assert(!(-0x1000000000000000000000000000000 >= BigInt("-1234")))
|
||||
assert(!(-0x1000000000000000000000000000000 > BigInt("-1234")))
|
||||
|
||||
assert(0x1234567880000000000000000000000 < BigInt("0x1234567890000000000000000000000"))
|
||||
assert(0x1234567880000000000000000000000 <= BigInt("0x1234567890000000000000000000000"))
|
||||
assert(!(0x1234567880000000000000000000000 > BigInt("0x1234567890000000000000000000000")))
|
||||
assert(!(0x1234567880000000000000000000000 >= BigInt("0x1234567890000000000000000000000")))
|
||||
|
||||
assert(-BigInt("0x1234567890000000000000000000000") < -0x1234567880000000000000000000000)
|
||||
assert(-BigInt("0x1234567890000000000000000000000") <= -0x1234567880000000000000000000000)
|
||||
assert(!(-BigInt("0x1234567890000000000000000000000") >= -0x1234567880000000000000000000000))
|
||||
assert(!(-BigInt("0x1234567890000000000000000000000") > -0x1234567880000000000000000000000))
|
||||
|
||||
// True because of rounding
|
||||
assert(0x1234567890000000000000000000001 < BigInt("0x1234567890000000000000000000001"))
|
||||
assert(0x1234567890000000000000000000001 <= BigInt("0x1234567890000000000000000000001"))
|
||||
assert(!(0x1234567890000000000000000000001 >= BigInt("0x1234567890000000000000000000001")))
|
||||
assert(!(0x1234567890000000000000000000001 > BigInt("0x1234567890000000000000000000001")))
|
||||
|
||||
assert(-BigInt("0x1234567890000000000000000000001") < -0x1234567890000000000000000000001)
|
||||
assert(-BigInt("0x1234567890000000000000000000001") <= -0x1234567890000000000000000000001)
|
||||
assert(!(-BigInt("0x1234567890000000000000000000001") >= -0x1234567890000000000000000000001))
|
||||
assert(!(-BigInt("0x1234567890000000000000000000001") > -0x1234567890000000000000000000001))
|
||||
|
||||
assert(!(1.0000152587890625 < BigInt("1")))
|
||||
assert(!(1.0000152587890625 <= BigInt("1")))
|
||||
assert(1.0000152587890625 >= BigInt("1"))
|
||||
assert(1.0000152587890625 > BigInt("1"))
|
||||
|
||||
assert(!(BigInt("-1") < -1.0000152587890625))
|
||||
assert(!(BigInt("-1") <= -1.0000152587890625))
|
||||
assert(BigInt("-1") >= -1.0000152587890625)
|
||||
assert(BigInt("-1") > -1.0000152587890625)
|
||||
@@ -707,9 +707,6 @@
|
||||
<test id="built-ins/BigInt/constructor-from-octal-string.js"><reason></reason></test>
|
||||
<test id="built-ins/BigInt/constructor-integer.js"><reason></reason></test>
|
||||
<test id="built-ins/BigInt/constructor-trailing-leading-spaces.js"><reason></reason></test>
|
||||
<test id="built-ins/BigInt/infinity-throws-rangeerror.js"><reason></reason></test>
|
||||
<test id="built-ins/BigInt/nan-throws-rangeerror.js"><reason></reason></test>
|
||||
<test id="built-ins/BigInt/negative-infinity-throws.rangeerror.js"><reason></reason></test>
|
||||
<test id="built-ins/BigInt/non-integer-rangeerror.js"><reason></reason></test>
|
||||
<test id="built-ins/BigInt/prototype/toString/a-z.js"><reason></reason></test>
|
||||
<test id="built-ins/BigInt/prototype/toString/default-radix.js"><reason></reason></test>
|
||||
@@ -720,8 +717,6 @@
|
||||
<test id="built-ins/BigInt/prototype/toString/thisbigintvalue-not-valid-throws.js"><reason></reason></test>
|
||||
<test id="built-ins/BigInt/prototype/valueOf/cross-realm.js"><reason></reason></test>
|
||||
<test id="built-ins/BigInt/prototype/valueOf/return.js"><reason></reason></test>
|
||||
<test id="built-ins/BigInt/tostring-throws.js"><reason></reason></test>
|
||||
<test id="built-ins/BigInt/valueof-throws.js"><reason></reason></test>
|
||||
<test id="built-ins/Boolean/proto-from-ctor-realm.js"><reason></reason></test>
|
||||
<test id="built-ins/DataView/buffer-does-not-have-arraybuffer-data-throws-sab.js"><reason></reason></test>
|
||||
<test id="built-ins/DataView/buffer-reference-sab.js"><reason></reason></test>
|
||||
@@ -1167,7 +1162,6 @@
|
||||
<test id="built-ins/Object/prototype/toString/symbol-tag-non-str-bigint.js"><reason></reason></test>
|
||||
<test id="built-ins/Object/prototype/toString/symbol-tag-non-str-builtin.js"><reason></reason></test>
|
||||
<test id="built-ins/Object/prototype/toString/symbol-tag-non-str-proxy-function.js"><reason></reason></test>
|
||||
<test id="built-ins/Object/prototype/toString/symbol-tag-override-bigint.js"><reason></reason></test>
|
||||
<test id="built-ins/Object/setPrototypeOf/bigint.js"><reason></reason></test>
|
||||
<test id="built-ins/Object/subclass-object-arg.js"><reason></reason></test>
|
||||
<test id="built-ins/Object/values/order-after-define-property.js"><reason></reason></test>
|
||||
|
||||
Reference in New Issue
Block a user