From 443b15e43e8c497b2edcf8c064b11fd7c1ad67fc Mon Sep 17 00:00:00 2001 From: Josh Wolfe Date: Wed, 6 Sep 2017 13:52:08 -0400 Subject: [PATCH] Add tests for BitInt.asIntN (#1191) --- test/built-ins/BigInt/asIntN/arithmetic.js | 70 +++++++++++++++++++ test/built-ins/BigInt/asIntN/asIntN.js | 21 ++++++ .../BigInt/asIntN/bigint-tobigint.js | 31 ++++++++ test/built-ins/BigInt/asIntN/bits-toindex.js | 37 ++++++++++ test/built-ins/BigInt/asIntN/length.js | 20 ++++++ test/built-ins/BigInt/asIntN/name.js | 20 ++++++ .../built-ins/BigInt/asIntN/order-of-steps.js | 32 +++++++++ 7 files changed, 231 insertions(+) create mode 100644 test/built-ins/BigInt/asIntN/arithmetic.js create mode 100644 test/built-ins/BigInt/asIntN/asIntN.js create mode 100644 test/built-ins/BigInt/asIntN/bigint-tobigint.js create mode 100644 test/built-ins/BigInt/asIntN/bits-toindex.js create mode 100644 test/built-ins/BigInt/asIntN/length.js create mode 100644 test/built-ins/BigInt/asIntN/name.js create mode 100644 test/built-ins/BigInt/asIntN/order-of-steps.js diff --git a/test/built-ins/BigInt/asIntN/arithmetic.js b/test/built-ins/BigInt/asIntN/arithmetic.js new file mode 100644 index 0000000000..06ca025478 --- /dev/null +++ b/test/built-ins/BigInt/asIntN/arithmetic.js @@ -0,0 +1,70 @@ +// Copyright (C) 2017 Josh Wolfe. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: pending +description: BigInt.asIntN arithmetic test cases +info: > + BigInt.asIntN ( bits, bigint ) + + 3. Let mod be a BigInt representing bigint modulo 2**bits. + 4. If mod ≥ 2**bits - 1, return mod - 2**bits; otherwise, return mod. + +features: [BigInt] +---*/ + +assert.sameValue(BigInt.asIntN(0, -2n), 0n); +assert.sameValue(BigInt.asIntN(0, -1n), 0n); +assert.sameValue(BigInt.asIntN(0, 0n), 0n); +assert.sameValue(BigInt.asIntN(0, 1n), 0n); +assert.sameValue(BigInt.asIntN(0, 2n), 0n); + +assert.sameValue(BigInt.asIntN(1, -3n), -1n); +assert.sameValue(BigInt.asIntN(1, -2n), 0n); +assert.sameValue(BigInt.asIntN(1, -1n), -1n); +assert.sameValue(BigInt.asIntN(1, 0n), 0n); +assert.sameValue(BigInt.asIntN(1, 1n), -1n); +assert.sameValue(BigInt.asIntN(1, 2n), 0n); +assert.sameValue(BigInt.asIntN(1, 3n), -1n); +assert.sameValue(BigInt.asIntN(1, -123456789012345678901n), -1n); +assert.sameValue(BigInt.asIntN(1, -123456789012345678900n), 0n); +assert.sameValue(BigInt.asIntN(1, 123456789012345678900n), 0n); +assert.sameValue(BigInt.asIntN(1, 123456789012345678901n), -1n); + +assert.sameValue(BigInt.asIntN(2, -3n), 1n); +assert.sameValue(BigInt.asIntN(2, -2n), -2n); +assert.sameValue(BigInt.asIntN(2, -1n), -1n); +assert.sameValue(BigInt.asIntN(2, 0n), 0n); +assert.sameValue(BigInt.asIntN(2, 1n), 1n); +assert.sameValue(BigInt.asIntN(2, 2n), -2n); +assert.sameValue(BigInt.asIntN(2, 3n), -1n); +assert.sameValue(BigInt.asIntN(2, -123456789012345678901n), -1n); +assert.sameValue(BigInt.asIntN(2, -123456789012345678900n), 0n); +assert.sameValue(BigInt.asIntN(2, 123456789012345678900n), 0n); +assert.sameValue(BigInt.asIntN(2, 123456789012345678901n), 1n); + +assert.sameValue(BigInt.asIntN(8, 0xabn), -0x55n); +assert.sameValue(BigInt.asIntN(8, 0xabcdn), -0x33n); +assert.sameValue(BigInt.asIntN(8, 0xabcdef01n), 0x01n); +assert.sameValue(BigInt.asIntN(8, 0xabcdef0123456789abcdef0123n), 0x23n); +assert.sameValue(BigInt.asIntN(8, 0xabcdef0123456789abcdef0183n), -0x7dn); + +assert.sameValue(BigInt.asIntN(64, 0xabcdef0123456789abcdefn), 0x0123456789abcdefn); +assert.sameValue(BigInt.asIntN(65, 0xabcdef0123456789abcdefn), -0xfedcba9876543211n); + +assert.sameValue(BigInt.asIntN(200, + 0xcffffffffffffffffffffffffffffffffffffffffffffffffffn), + -0x00000000000000000000000000000000000000000000000001n +); +assert.sameValue(BigInt.asIntN(201, + 0xcffffffffffffffffffffffffffffffffffffffffffffffffffn), + 0xffffffffffffffffffffffffffffffffffffffffffffffffffn +); + +assert.sameValue(BigInt.asIntN(200, + 0xc89e081df68b65fedb32cffea660e55df9605650a603ad5fc54n), + -0x761f7e209749a0124cd3001599f1aa2069fa9af59fc52a03acn +); +assert.sameValue(BigInt.asIntN(201, + 0xc89e081df68b65fedb32cffea660e55df9605650a603ad5fc54n), + 0x89e081df68b65fedb32cffea660e55df9605650a603ad5fc54n +); diff --git a/test/built-ins/BigInt/asIntN/asIntN.js b/test/built-ins/BigInt/asIntN/asIntN.js new file mode 100644 index 0000000000..382ca4a752 --- /dev/null +++ b/test/built-ins/BigInt/asIntN/asIntN.js @@ -0,0 +1,21 @@ +// Copyright (C) 2017 Josh Wolfe. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: pending +description: BigInt.asIntN property descriptor +info: > + BigInt.asIntN ( bits, bigint ) + + 17 ECMAScript Standard Built-in Objects + +includes: [propertyHelper.js] +features: [BigInt] +---*/ + +assert.sameValue(typeof BigInt.asIntN, 'function'); + +verifyProperty(BigInt, "asIntN", { + enumerable: false, + writable: true, + configurable: true +}); diff --git a/test/built-ins/BigInt/asIntN/bigint-tobigint.js b/test/built-ins/BigInt/asIntN/bigint-tobigint.js new file mode 100644 index 0000000000..44a919c7a4 --- /dev/null +++ b/test/built-ins/BigInt/asIntN/bigint-tobigint.js @@ -0,0 +1,31 @@ +// Copyright (C) 2017 Josh Wolfe. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: pending +description: BigInt.asIntN type coercion for bigint parameter +info: > + BigInt.asIntN ( bits, bigint ) + + 2. Let bigint ? ToBigInt(bigint). + +features: [BigInt, Symbol, Symbol.toPrimitive, arrow-function] +---*/ + +function MyError() {} + +assert.sameValue(BigInt.asIntN(3, Object(10n)), 2n); +assert.sameValue(BigInt.asIntN(3, {[Symbol.toPrimitive]:()=>10n, valueOf(){throw new MyError();}, toString(){throw new MyError();}}), 2n); +assert.sameValue(BigInt.asIntN(3, {valueOf:()=>10n, toString(){throw new MyError();}}), 2n); +assert.sameValue(BigInt.asIntN(3, {toString:()=>"10"}), 2n); +assert.throws(MyError, () => BigInt.asIntN(0, {[Symbol.toPrimitive](){throw new MyError();}, valueOf:()=>10n, toString:()=>"10"})); +assert.throws(MyError, () => BigInt.asIntN(0, {valueOf(){throw new MyError();}, toString:()=>"10"})); +assert.throws(MyError, () => BigInt.asIntN(0, {toString(){throw new MyError();}})); +assert.throws(TypeError, () => BigInt.asIntN(0, undefined)); +assert.throws(TypeError, () => BigInt.asIntN(0, null)); +assert.sameValue(BigInt.asIntN(2, true), 1n); +assert.sameValue(BigInt.asIntN(2, false), 0n); +assert.throws(TypeError, () => BigInt.asIntN(0, 0)); +assert.throws(SyntaxError, () => BigInt.asIntN(0, "foo")); +assert.sameValue(BigInt.asIntN(3, "10"), 2n); +assert.sameValue(BigInt.asIntN(4, "12345678901234567890003"), 3n); +assert.throws(TypeError, () => BigInt.asIntN(0, Symbol("1"))); diff --git a/test/built-ins/BigInt/asIntN/bits-toindex.js b/test/built-ins/BigInt/asIntN/bits-toindex.js new file mode 100644 index 0000000000..76a61564d4 --- /dev/null +++ b/test/built-ins/BigInt/asIntN/bits-toindex.js @@ -0,0 +1,37 @@ +// Copyright (C) 2017 Josh Wolfe. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: pending +description: BigInt.asIntN type coercion for bits parameter +info: > + BigInt.asIntN ( bits, bigint ) + + 1. Let bits be ? ToIndex(bits). + +features: [BigInt, Symbol, Symbol.toPrimitive, arrow-function] +---*/ + +function MyError() {} + +assert.sameValue(BigInt.asIntN(undefined, 1n), 0n); +assert.sameValue(BigInt.asIntN(null, 1n), 0n); +assert.sameValue(BigInt.asIntN(true, 1n), -1n); +assert.sameValue(BigInt.asIntN(false, 1n), 0n); +assert.sameValue(BigInt.asIntN("foo", 1n), 0n); +assert.sameValue(BigInt.asIntN("3", 10n), 2n); +assert.throws(TypeError, () => BigInt.asIntN(Symbol(0), 0n)); +assert.throws(TypeError, () => BigInt.asIntN(1n, 0n)); +assert.sameValue(BigInt.asIntN(Object(3), 10n), 2n); +assert.sameValue(BigInt.asIntN({[Symbol.toPrimitive]:()=>3, valueOf(){throw new MyError();}, toString(){throw new MyError();}}, 10n), 2n); +assert.sameValue(BigInt.asIntN({valueOf:()=>3, toString(){throw new MyError();}}, 10n), 2n); +assert.sameValue(BigInt.asIntN({toString:()=>"3"}, 10n), 2n); +assert.throws(MyError, () => BigInt.asIntN({[Symbol.toPrimitive](){throw new MyError();}, valueOf:()=>3, toString:()=>"3"}, 0n)); +assert.throws(MyError, () => BigInt.asIntN({valueOf(){throw new MyError();}, toString:()=>"3"}, 0n)); +assert.throws(MyError, () => BigInt.asIntN({toString(){throw new MyError();}}, 0n)); +assert.sameValue(BigInt.asIntN(NaN, 1n), 0n); +assert.sameValue(BigInt.asIntN(3.9, 10n), 2n); +assert.sameValue(BigInt.asIntN(-0.9, 1n), 0n); +assert.throws(RangeError, () => BigInt.asIntN(-1, 0n)); +assert.throws(RangeError, () => BigInt.asIntN("-2.5", 0n)); +assert.throws(RangeError, () => BigInt.asIntN(2 ** 53, 0n)); +assert.throws(RangeError, () => BigInt.asIntN(Infinity, 0n)); diff --git a/test/built-ins/BigInt/asIntN/length.js b/test/built-ins/BigInt/asIntN/length.js new file mode 100644 index 0000000000..6568aa9e90 --- /dev/null +++ b/test/built-ins/BigInt/asIntN/length.js @@ -0,0 +1,20 @@ +// Copyright (C) 2017 Josh Wolfe. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: pending +description: BigInt.asIntN.length descriptor +info: > + BigInt.asIntN ( bits, bigint ) + + 17 ECMAScript Standard Built-in Objects + +includes: [propertyHelper.js] +features: [BigInt] +---*/ + +verifyProperty(BigInt.asIntN, "length", { + value: 2, + enumerable: false, + writable: false, + configurable: true +}); diff --git a/test/built-ins/BigInt/asIntN/name.js b/test/built-ins/BigInt/asIntN/name.js new file mode 100644 index 0000000000..0f6f759447 --- /dev/null +++ b/test/built-ins/BigInt/asIntN/name.js @@ -0,0 +1,20 @@ +// Copyright (C) 2017 Josh Wolfe. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: pending +description: BigInt.asIntN.name descriptor +info: > + BigInt.asIntN ( bits, bigint ) + + 17 ECMAScript Standard Built-in Objects + +includes: [propertyHelper.js] +features: [BigInt] +---*/ + +verifyProperty(BigInt.asIntN, "name", { + value: "asIntN", + enumerable: false, + writable: false, + configurable: true +}); diff --git a/test/built-ins/BigInt/asIntN/order-of-steps.js b/test/built-ins/BigInt/asIntN/order-of-steps.js new file mode 100644 index 0000000000..80783f0531 --- /dev/null +++ b/test/built-ins/BigInt/asIntN/order-of-steps.js @@ -0,0 +1,32 @@ +// Copyright (C) 2017 Josh Wolfe. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: pending +description: BigInt.asIntN order of parameter type coercion +info: > + BigInt.asIntN ( bits, bigint ) + + 1. Let bits be ? ToIndex(bits). + 2. Let bigint ? ToBigInt(bigint). + +features: [BigInt] +---*/ + +var i = 0; +var bits = { + valueOf() { + assert.sameValue(i, 0); + i++; + return 0; + } +}; +var bigint = { + valueOf() { + assert.sameValue(i, 1); + i++; + return 0n; + } +}; + +BigInt.asIntN(bits, bigint); +assert.sameValue(i, 2);