-
Notifications
You must be signed in to change notification settings - Fork 480
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
add BigInt tests #1148
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
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); |
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); |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some more tests you can write for the constructor:
|
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this suffix allowed? |
||
assert.sameValue(BigInt.parseInt("kf12oikf12oikf12oi", 36), | ||
5849853453554480289462428370n); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some additional tests that you could add:
Generally, it'd be a ideal to take all the parseInt tests from |
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"); |
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); |
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]); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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)); |
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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
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))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. function wrap() { return this; }
assert.sameValue(typeof wrap.call(0n) === 'object'); |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, |
||
} catch (e) { | ||
$ERROR('Expected RequireObjectCoercible to succeed for BigInt values'); | ||
} | ||
|
||
assert.sameValue(Object.setPrototypeOf(0n, null), 0n); |
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 + ""; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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); |
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); |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
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); |
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); |
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); |
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also test this through |
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); |
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); |
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; |
There was a problem hiding this comment.
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:
valueOf
methods, then the index is first (sorry, just changed this Normative: Reverse the order of casts for asIntN/asUintN proposal-bigint#52)