Skip to content

Commit

Permalink
Add tests for BitInt.asIntN (#1191)
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoshwolfe authored and leobalter committed Sep 6, 2017
1 parent 457c97f commit 443b15e
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 0 deletions.
70 changes: 70 additions & 0 deletions test/built-ins/BigInt/asIntN/arithmetic.js
Original file line number Diff line number Diff line change
@@ -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
);
21 changes: 21 additions & 0 deletions test/built-ins/BigInt/asIntN/asIntN.js
Original file line number Diff line number Diff line change
@@ -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
});
31 changes: 31 additions & 0 deletions test/built-ins/BigInt/asIntN/bigint-tobigint.js
Original file line number Diff line number Diff line change
@@ -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")));
37 changes: 37 additions & 0 deletions test/built-ins/BigInt/asIntN/bits-toindex.js
Original file line number Diff line number Diff line change
@@ -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));
20 changes: 20 additions & 0 deletions test/built-ins/BigInt/asIntN/length.js
Original file line number Diff line number Diff line change
@@ -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
});
20 changes: 20 additions & 0 deletions test/built-ins/BigInt/asIntN/name.js
Original file line number Diff line number Diff line change
@@ -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
});
32 changes: 32 additions & 0 deletions test/built-ins/BigInt/asIntN/order-of-steps.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 443b15e

Please sign in to comment.