Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add BigInt tests #1148

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions test/built-ins/BigInt/asIntN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt.asIntN
esid: pending
features: [BigInt]
---*/

assert.sameValue(BigInt.asIntN(1, 3n), -1n);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some more tests for:

  • Passing a non-Index argument for the first argument (e.g., -1, or "foo")
  • Passing a non-BigInt argument for the second argument
  • If both are objects with valueOf methods, then the index is first (sorry, just changed this Normative: Reverse the order of casts for asIntN/asUintN proposal-bigint#52)
  • Testing a wider range of values to get a little more confidence in the arithmetic
  • Superficial things like, what's the length of the function, check that it's a writable, non-enumerable and configurable property.

10 changes: 10 additions & 0 deletions test/built-ins/BigInt/asUintN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt.asUintN
esid: pending
features: [BigInt]
---*/

assert.sameValue(BigInt.asUintN(1, 3n), 1n);
10 changes: 10 additions & 0 deletions test/built-ins/BigInt/constructor-prototype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Prototype of BigInt constructor
esid: pending
features: [BigInt]
---*/

assert.sameValue(Object.getPrototypeOf(BigInt), Function.prototype);
16 changes: 16 additions & 0 deletions test/built-ins/BigInt/constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt constructor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could write something here like, "if the argument to the BigInt constructor is not a safe integer, throw a RangeError."

esid: pending
features: [BigInt]
---*/

assert.throws(TypeError, () => new BigInt(0));
for (let x of [NaN, Infinity, 0.5, 2**53]) {
assert.throws(RangeError, () => BigInt(x));
assert.throws(RangeError, () => BigInt(-x));
}
assert.sameValue(BigInt(9007199254740991), 9007199254740991n);
assert.sameValue(BigInt(-9007199254740991), -9007199254740991n);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more tests you can write for the constructor:

  • Taking a string as an argument (should parse similarly to the parseInt tests below, except there is no radix argument)
  • An object (should call ToPrimitive appropriately)
  • The length and property attributes of how it's installed on the global object, as described above.

18 changes: 18 additions & 0 deletions test/built-ins/BigInt/parseInt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt string parsing
esid: pending
features: [BigInt]
---*/

assert.throws(SyntaxError, () => BigInt.parseInt(""));
assert.throws(SyntaxError, () => BigInt.parseInt("@"));
assert.throws(SyntaxError, () => BigInt.parseInt("1", 1));
assert.throws(SyntaxError, () => BigInt.parseInt("1", 37));
assert.sameValue(BigInt.parseInt("0xf", 0), 0xfn);
assert.sameValue(BigInt.parseInt("-0"), 0n);
assert.sameValue(BigInt.parseInt(" 0@"), 0n);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this suffix allowed?

assert.sameValue(BigInt.parseInt("kf12oikf12oikf12oi", 36),
5849853453554480289462428370n);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding a test that one more than this parses as a distinct BigInt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some additional tests that you could add:

  • How are arguments coerced
  • A leading + is accepted and deleted
  • 0x and 0X both work, but binary, legacy octal and new-style octal don't work (should we take this feature out?)
  • Length and property attributes of parseInt
  • The default base is 10

Generally, it'd be a ideal to take all the parseInt tests from test/built-ins/parseInt and port them here, though I understand that that's a lot of boring work.

13 changes: 13 additions & 0 deletions test/built-ins/BigInt/prototype/attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt prototype object attributes
esid: pending
features: [BigInt]
includes: [propertyHelper.js]
---*/

verifyNotWritable(BigInt, "prototype");
verifyNotEnumerable(BigInt, "prototype");
verifyNotConfigurable(BigInt, "prototype");
10 changes: 10 additions & 0 deletions test/built-ins/BigInt/prototype/prototype-is-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Prototype of BigInt.prototype
esid: pending
features: [BigInt]
---*/

assert.sameValue(Object.getPrototypeOf(BigInt.prototype), Object.prototype);
61 changes: 61 additions & 0 deletions test/built-ins/BigInt/prototype/toString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt toString method
esid: pending
features: [BigInt]
---*/

const n = 1234567890n;
const strs = {
2: "1001001100101100000001011010010",
3: "10012001001112202200",
4: "1021211200023102",
5: "10012022133030",
6: "322301024030",
7: "42410440203",
8: "11145401322",
9: "3161045680",
10: "1234567890",
11: "583977146",
12: "2a5555016",
13: "168a0865a",
14: "b9d6b5aa",
15: "735b7d60",
16: "499602d2",
17: "30288g3a",
18: "20568ad0",
19: "174b57c7",
20: "j5g0jea",
21: "e8605e3",
22: "ajc3e26",
23: "87ifcgi",
24: "6b1230i",
25: "51ac8ff",
26: "3pnfhma",
27: "3511eki",
28: "2fkfbqa",
29: "225ep2g",
30: "1ko4m30",
31: "1c3ou0k",
32: "14pc0mi",
33: "vi0m56",
34: "r5spaa",
35: "nhokia",
36: "kf12oi"
};

assert.throws(TypeError, () => BigInt.prototype.toString.call(null));
assert.sameValue(n.toString(), n.toString(10));
assert.sameValue(n.toString(undefined), n.toString(10));
for (let radix of [1, 37, NaN, 0, Infinity]) {
assert.throws(RangeError, () => n.toString(radix));
assert.throws(RangeError, () => n.toString(-radix));
}
for (let i = 2; i < 37; i++) {
for (let x of [n, Object(n)]) {
assert.sameValue(x.toString(i), strs[i]);
assert.sameValue(x.toString(i + 0.5), strs[i]);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly to above, you can do more for edge cases about coercion, property details, etc, and port tests from test/built-ins/Number/prototype/toString

12 changes: 12 additions & 0 deletions test/built-ins/BigInt/prototype/valueOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt valueOf method and thisBigIntValue operation
esid: pending
features: [BigInt]
---*/

assert.sameValue(BigInt.prototype.valueOf.call(0n), 0n);
assert.sameValue(BigInt.prototype.valueOf.call(Object(0n)), 0n);
assert.throws(TypeError, () => BigInt.prototype.valueOf.call(null));
10 changes: 10 additions & 0 deletions test/built-ins/JSON/stringify/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: JSON serialization of BigInt values
esid: pending
features: [BigInt]
---*/

assert.throws(TypeError, () => JSON.stringify(0n));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Also test that it throws on Object(0n), and finding one of these values within an object.
  • Replacers can replace BigInts with other values to avoid throwing
  • The toJSON method is called when it's installed
  • When these cases occur together, first the replacer method is run, then the unwrapping happens, then .toJSON() is called on the primitive.

11 changes: 11 additions & 0 deletions test/built-ins/Number/bigint-conversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt to Number conversion
esid: pending
features: [BigInt]
---*/

assert.sameValue(Number(0n), 0);
assert.sameValue(+(new Number(0n)), +(new Number(0)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what the second test is getting at; it seems redundant.

It would be good to have a case here which shows losing precision, which the Number constructor currently does implicitly.

11 changes: 11 additions & 0 deletions test/built-ins/Object/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Conversion of BigInt values to Objects
esid: pending
features: [BigInt]
---*/

assert(Object(0n) instanceof BigInt);
assert.sameValue(Object(0n).valueOf(), 0n);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test another case where this comes up: if you make a sloppy-mode method on BigInt.prototype.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function wrap() { return this; }
assert.sameValue(typeof wrap.call(0n) === 'object');

16 changes: 16 additions & 0 deletions test/built-ins/Object/setPrototypeOf/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: RequireObjectCoercible for BigInt values
esid: pending
features: [BigInt]
---*/

try {
let {} = 0n;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do slightly more than this while you're at it--for example, let {valueOf} = 0n; assert.sameValue(valueOf, BigInt.prototype.valueOf).

} catch (e) {
$ERROR('Expected RequireObjectCoercible to succeed for BigInt values');
}

assert.sameValue(Object.setPrototypeOf(0n, null), 0n);
16 changes: 16 additions & 0 deletions test/language/expressions/addition/bigint-to-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: ToString for BigInt values
esid: pending
features: [BigInt]
---*/

const ToString = (x) => x + "";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test that BigInt + String works the other way around as well, and with non-empty strings, and that it also works with these things in object wrappers.


assert.sameValue(ToString(-1n), "-1");
assert.sameValue(ToString(0n), "0");
assert.sameValue(ToString(1n), "1");
assert.sameValue(ToString(1234567890n), "1234567890");
assert.sameValue(ToString(-1234567890n), "-1234567890");
10 changes: 10 additions & 0 deletions test/language/expressions/division/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt division
esid: pending
features: [BigInt]
---*/

assert.throws(RangeError, () => 1n / 0n);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0n/0n as well?

18 changes: 18 additions & 0 deletions test/language/expressions/does-not-equals/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt equality
esid: pending
features: [BigInt]
---*/

assert("x" != 0n);
assert(0n != "x");
assert(true != 0n);
assert(false != 1n);
assert(0n != true);
assert(1n != false);
assert(0n != NaN);
assert(0n != Infinity);
assert(0n != -Infinity);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Rather than just assert(), use assert.sameValue(true, ...) to be completely sure that this outputs a boolean.
  • Share the tests from the "equals" assertions, except asserting that the output is false
  • Include a test which requires some calculation to compare the BigInt to the Number

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a lot of observation parts here that I can't get an exact reference for all of these cases combined.

https://tc39.github.io/proposal-bigint/#sec-abstract-equality-comparison

19 changes: 19 additions & 0 deletions test/language/expressions/equals/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt equality
esid: pending
features: [BigInt]
---*/

assert("" == 0n);
assert("0" == 0n);
assert(0n == "");
assert(0n == "0");
assert(true == 1n);
assert(false == 0n);
assert(1n == true);
assert(0n == false);
assert(0n == 0);
assert(0 == 0n);
11 changes: 11 additions & 0 deletions test/language/expressions/exponentiation/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt exponentiation
esid: pending
features: [BigInt]
---*/

assert.throws(RangeError, () => 1n ** -1n);
assert.sameValue(0n ** 0n, 1n);
13 changes: 13 additions & 0 deletions test/language/expressions/greater-than-or-equal/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt comparison
esid: pending
features: [BigInt]
---*/

assert(1n >= 0);
assert(1 >= 0n);
assert(1n >= 1);
assert(1 >= 1n);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include some test which requires a little bit more calculation, and include a comparison between some non-numeric types and a BigInt.

11 changes: 11 additions & 0 deletions test/language/expressions/greater-than/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt comparison
esid: pending
features: [BigInt]
---*/

assert(1 > 0n);
assert(1n > 0);
13 changes: 13 additions & 0 deletions test/language/expressions/less-than-or-equal/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt comparison
esid: pending
features: [BigInt]
---*/

assert(0n <= 1);
assert(0 <= 1n);
assert(1n <= 1);
assert(1 <= 1n);
11 changes: 11 additions & 0 deletions test/language/expressions/less-than/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt comparison
esid: pending
features: [BigInt]
---*/

assert(0n < 1);
assert(0 < 1n);
11 changes: 11 additions & 0 deletions test/language/expressions/logical-not/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Conversion of BigInt values to booleans
esid: pending
features: [BigInt]
---*/

assert.sameValue(!!0n, false, "Expected ToBoolean(0n) to be false");
assert.sameValue(!!1n, true, "Expected ToBoolean(1n) to be true");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also test this through if, ||, &&, ?:.

10 changes: 10 additions & 0 deletions test/language/expressions/modulus/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: BigInt modulus
esid: pending
features: [BigInt]
---*/

assert.throws(RangeError, () => 1n % 0n);
10 changes: 10 additions & 0 deletions test/language/expressions/unary-plus/bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Conversion of BigInt values to numbers
esid: pending
features: [BigInt]
---*/

assert.throws(TypeError, () => +0n);
13 changes: 13 additions & 0 deletions test/language/literals/bigint/binary-invalid-digit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (C) 2017 Robin Templeton. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Binary BigInt literal containing an invalid digit
esid: pending
features: [BigInt]
negative:
phase: early
type: SyntaxError
---*/

0b2n;
Loading