Skip to content

Commit

Permalink
BigInt.parseInt tests (#1208)
Browse files Browse the repository at this point in the history
* BigInt.parseInt tests

* update for PR#1208

* fix copyright notices
  • Loading branch information
Robin Templeton authored and leobalter committed Oct 17, 2017
1 parent 8837686 commit 72fb638
Show file tree
Hide file tree
Showing 50 changed files with 2,106 additions and 1 deletion.
49 changes: 49 additions & 0 deletions test/built-ins/BigInt/parseInt/all-decimal-digits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (C) 2017 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-bigint-parseint-string-radix
description: 01234567890 parsed in different radices
info: >
BigInt.parseInt ( string, radix )
The parseInt function produces a BigInt value dictated by
interpretation of the contents of the string argument according to
the specified radix.
The algorithm is the same as 18.2.5 but with the following edits:
* For all cases which result in returning NaN, throw a SyntaxError
exception.
* For all cases which result in returning -0, return 0n.
* Replace the second to last step, which casts mathInt to a Number,
with casting mathInt to a BigInt.
18.2.5 parseInt ( string, radix )
The parseInt function produces an integer value dictated by
interpretation of the contents of the string argument according to the
specified radix. Leading white space in string is ignored. If radix is
undefined or 0, it is assumed to be 10 except when the number begins
with the code unit pairs 0x or 0X, in which case a radix of 16 is
assumed. If radix is 16, the number may also optionally begin with the
code unit pairs 0x or 0X.
[...]
NOTE: parseInt may interpret only a leading portion of string as an
integer value; it ignores any code units that cannot be interpreted as
part of the notation of an integer, and no indication is given that
any such code units were ignored.
features: [BigInt]
---*/

assert.sameValue(BigInt.parseInt("01234567890", 2), 1n);
assert.sameValue(BigInt.parseInt("01234567890", 3), 5n);
assert.sameValue(BigInt.parseInt("01234567890", 4), 27n);
assert.sameValue(BigInt.parseInt("01234567890", 5), 194n);
assert.sameValue(BigInt.parseInt("01234567890", 6), 1865n);
assert.sameValue(BigInt.parseInt("01234567890", 7), 22875n);
assert.sameValue(BigInt.parseInt("01234567890", 8), 342391n);
assert.sameValue(BigInt.parseInt("01234567890", 9), 6053444n);
assert.sameValue(BigInt.parseInt("01234567890", 10), 1234567890n);
41 changes: 41 additions & 0 deletions test/built-ins/BigInt/parseInt/arg-boolean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (C) 2017 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-bigint-parseint-string-radix
description: Boolean argument
info: >
BigInt.parseInt ( string, radix )
The parseInt function produces a BigInt value dictated by
interpretation of the contents of the string argument according to
the specified radix.
The algorithm is the same as 18.2.5 but with the following edits:
* For all cases which result in returning NaN, throw a SyntaxError
exception.
* For all cases which result in returning -0, return 0n.
* Replace the second to last step, which casts mathInt to a Number,
with casting mathInt to a BigInt.
18.2.5 parseInt ( string, radix )
1. Let inputString be ? ToString(string).
2. Let S be a newly created substring of inputString consisting of
the first code unit that is not a StrWhiteSpaceChar and all code
units following that code unit. (In other words, remove leading
white space.) If inputString does not contain any such code unit,
let S be the empty string.
[...]
11. If S contains a code unit that is not a radix-R digit, let Z be
the substring of S consisting of all code units before the first
such code unit; otherwise, let Z be S.
12. If Z is empty, return NaN.
features: [BigInt, arrow-function]
---*/

assert.throws(SyntaxError, () => BigInt.parseInt(true));
assert.throws(SyntaxError, () => BigInt.parseInt(false));
assert.throws(SyntaxError, () => BigInt.parseInt(new Boolean(true)));
assert.throws(SyntaxError, () => BigInt.parseInt(new Boolean(false)));
35 changes: 35 additions & 0 deletions test/built-ins/BigInt/parseInt/arg-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (C) 2017 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-bigint-parseint-string-radix
description: Number argument
info: >
BigInt.parseInt ( string, radix )
The parseInt function produces a BigInt value dictated by
interpretation of the contents of the string argument according to
the specified radix.
The algorithm is the same as 18.2.5 but with the following edits:
* For all cases which result in returning NaN, throw a SyntaxError
exception.
* For all cases which result in returning -0, return 0n.
* Replace the second to last step, which casts mathInt to a Number,
with casting mathInt to a BigInt.
18.2.5 parseInt ( string, radix )
1. Let inputString be ? ToString(string).
features: [BigInt, arrow-function]
---*/

assert.sameValue(BigInt.parseInt(-1), -1n);
assert.throws(SyntaxError, () => BigInt.parseInt(Infinity));
assert.throws(SyntaxError, () => BigInt.parseInt(NaN));
assert.sameValue(BigInt.parseInt(-0), 0n);
assert.sameValue(BigInt.parseInt(new Number(-1)), -1n);
assert.throws(SyntaxError, () => BigInt.parseInt(new Number(Infinity)));
assert.throws(SyntaxError, () => BigInt.parseInt(new Number(NaN)));
assert.sameValue(BigInt.parseInt(new Number(-0)), 0n);
50 changes: 50 additions & 0 deletions test/built-ins/BigInt/parseInt/arg-primitive-coercion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (C) 2017 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-bigint-parseint-string-radix
description: Argument converted using ToPrimitive with hint Number
info: >
BigInt.parseInt ( string, radix )
The parseInt function produces a BigInt value dictated by
interpretation of the contents of the string argument according to
the specified radix.
The algorithm is the same as 18.2.5 but with the following edits:
* For all cases which result in returning NaN, throw a SyntaxError
exception.
* For all cases which result in returning -0, return 0n.
* Replace the second to last step, which casts mathInt to a Number,
with casting mathInt to a BigInt.
18.2.5 parseInt ( string, radix )
1. Let inputString be ? ToString(string).
features: [BigInt, arrow-function]
---*/

var object = {valueOf() {return 1}};
assert.throws(SyntaxError, () => BigInt.parseInt(object));

var object = {valueOf() {return 1}, toString() {return 0}};
assert.sameValue(BigInt.parseInt(object), 0n);

var object = {valueOf() {return 1}, toString() {return {}}};
assert.sameValue(BigInt.parseInt(object), 1n);

var object = {valueOf() {throw new Test262Error()}, toString() {return 1}};
assert.sameValue(BigInt.parseInt(object), 1n);

var object = {toString() {return 1}};
assert.sameValue(BigInt.parseInt(object), 1n);

var object = {valueOf() {return {}}, toString() {return 1}};
assert.sameValue(BigInt.parseInt(object), 1n);

var object = {valueOf() {return 1}, toString() {throw new Test262Error()}};
assert.throws(Test262Error, () => BigInt.parseInt(object));

var object = {valueOf() {return {}}, toString() {return {}}};
assert.throws(TypeError, () => BigInt.parseInt(object));
32 changes: 32 additions & 0 deletions test/built-ins/BigInt/parseInt/arg-string-obj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (C) 2017 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-bigint-parseint-string-radix
description: String object argument
info: >
BigInt.parseInt ( string, radix )
The parseInt function produces a BigInt value dictated by
interpretation of the contents of the string argument according to
the specified radix.
The algorithm is the same as 18.2.5 but with the following edits:
* For all cases which result in returning NaN, throw a SyntaxError
exception.
* For all cases which result in returning -0, return 0n.
* Replace the second to last step, which casts mathInt to a Number,
with casting mathInt to a BigInt.
18.2.5 parseInt ( string, radix )
1. Let inputString be ? ToString(string).
features: [BigInt, arrow-function]
---*/

assert.sameValue(BigInt.parseInt(new String("-1")), -1n);
assert.throws(SyntaxError, () => BigInt.parseInt(new String("Infinity")));
assert.throws(SyntaxError, () => BigInt.parseInt(new String("NaN")));
assert.throws(SyntaxError, () => BigInt.parseInt(new String("true")));
assert.throws(SyntaxError, () => BigInt.parseInt(new String("false")));
39 changes: 39 additions & 0 deletions test/built-ins/BigInt/parseInt/arg-undefined-null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (C) 2017 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-bigint-parseint-string-radix
description: Undefined or null argument
info: >
BigInt.parseInt ( string, radix )
The parseInt function produces a BigInt value dictated by
interpretation of the contents of the string argument according to
the specified radix.
The algorithm is the same as 18.2.5 but with the following edits:
* For all cases which result in returning NaN, throw a SyntaxError
exception.
* For all cases which result in returning -0, return 0n.
* Replace the second to last step, which casts mathInt to a Number,
with casting mathInt to a BigInt.
18.2.5 parseInt ( string, radix )
[...]
2. Let S be a newly created substring of inputString consisting of
the first code unit that is not a StrWhiteSpaceChar and all code
units following that code unit. (In other words, remove leading
white space.) If inputString does not contain any such code unit,
let S be the empty string.
[...]
11. If S contains a code unit that is not a radix-R digit, let Z be
the substring of S consisting of all code units before the first
such code unit; otherwise, let Z be S.
12. If Z is empty, return NaN.
features: [BigInt, arrow-function]
---*/

assert.throws(SyntaxError, () => BigInt.parseInt(undefined));
assert.throws(SyntaxError, () => BigInt.parseInt(null));
62 changes: 62 additions & 0 deletions test/built-ins/BigInt/parseInt/binary-negative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (C) 2017 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-bigint-parseint-string-radix
description: Negative binary argument with radix 2
info: >
BigInt.parseInt ( string, radix )
The parseInt function produces a BigInt value dictated by
interpretation of the contents of the string argument according to
the specified radix.
The algorithm is the same as 18.2.5 but with the following edits:
* For all cases which result in returning NaN, throw a SyntaxError
exception.
* For all cases which result in returning -0, return 0n.
* Replace the second to last step, which casts mathInt to a Number,
with casting mathInt to a BigInt.
18.2.5 parseInt ( string, radix )
The parseInt function produces an integer value dictated by
interpretation of the contents of the string argument according to
the specified radix. Leading white space in string is ignored. If
radix is undefined or 0, it is assumed to be 10 except when the
number begins with the code unit pairs 0x or 0X, in which case a
radix of 16 is assumed. If radix is 16, the number may also
optionally begin with the code unit pairs 0x or 0X.
The parseInt function is the %parseInt% intrinsic object. When the
parseInt function is called, the following steps are taken:
[...]
4. If S is not empty and the first code unit of S is the code unit
0x002D (HYPHEN-MINUS), let sign be -1.
[...]
16. Return sign × number.
features: [BigInt]
---*/

assert.sameValue(BigInt.parseInt("-1", 2), -1n);
assert.sameValue(BigInt.parseInt("-11", 2), -3n);
assert.sameValue(BigInt.parseInt("-111", 2), -7n);
assert.sameValue(BigInt.parseInt("-1111", 2), -15n);
assert.sameValue(BigInt.parseInt("-11111", 2), -31n);
assert.sameValue(BigInt.parseInt("-111111", 2), -63n);
assert.sameValue(BigInt.parseInt("-1111111", 2), -127n);
assert.sameValue(BigInt.parseInt("-11111111", 2), -255n);
assert.sameValue(BigInt.parseInt("-111111111", 2), -511n);
assert.sameValue(BigInt.parseInt("-1111111111", 2), -1023n);
assert.sameValue(BigInt.parseInt("-11111111111", 2), -2047n);
assert.sameValue(BigInt.parseInt("-111111111111", 2), -4095n);
assert.sameValue(BigInt.parseInt("-1111111111111", 2), -8191n);
assert.sameValue(BigInt.parseInt("-11111111111111", 2), -16383n);
assert.sameValue(BigInt.parseInt("-111111111111111", 2), -32767n);
assert.sameValue(BigInt.parseInt("-1111111111111111", 2), -65535n);
assert.sameValue(BigInt.parseInt("-11111111111111111", 2), -131071n);
assert.sameValue(BigInt.parseInt("-111111111111111111", 2), -262143n);
assert.sameValue(BigInt.parseInt("-1111111111111111111", 2), -524287n);
assert.sameValue(BigInt.parseInt("-11111111111111111111", 2), -1048575n);
53 changes: 53 additions & 0 deletions test/built-ins/BigInt/parseInt/binary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (C) 2017 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-bigint-parseint-string-radix
description: Binary string argument with radix 2
info: >
BigInt.parseInt ( string, radix )
The parseInt function produces a BigInt value dictated by
interpretation of the contents of the string argument according to
the specified radix.
The algorithm is the same as 18.2.5 but with the following edits:
* For all cases which result in returning NaN, throw a SyntaxError
exception.
* For all cases which result in returning -0, return 0n.
* Replace the second to last step, which casts mathInt to a Number,
with casting mathInt to a BigInt.
18.2.5 parseInt ( string, radix )
The parseInt function produces an integer value dictated by
interpretation of the contents of the string argument according to
the specified radix. Leading white space in string is ignored. If
radix is undefined or 0, it is assumed to be 10 except when the
number begins with the code unit pairs 0x or 0X, in which case a
radix of 16 is assumed. If radix is 16, the number may also
optionally begin with the code unit pairs 0x or 0X.
features: [BigInt]
---*/

assert.sameValue(BigInt.parseInt("1", 2), 1n);
assert.sameValue(BigInt.parseInt("11", 2), 3n);
assert.sameValue(BigInt.parseInt("111", 2), 7n);
assert.sameValue(BigInt.parseInt("1111", 2), 15n);
assert.sameValue(BigInt.parseInt("11111", 2), 31n);
assert.sameValue(BigInt.parseInt("111111", 2), 63n);
assert.sameValue(BigInt.parseInt("1111111", 2), 127n);
assert.sameValue(BigInt.parseInt("11111111", 2), 255n);
assert.sameValue(BigInt.parseInt("111111111", 2), 511n);
assert.sameValue(BigInt.parseInt("1111111111", 2), 1023n);
assert.sameValue(BigInt.parseInt("11111111111", 2), 2047n);
assert.sameValue(BigInt.parseInt("111111111111", 2), 4095n);
assert.sameValue(BigInt.parseInt("1111111111111", 2), 8191n);
assert.sameValue(BigInt.parseInt("11111111111111", 2), 16383n);
assert.sameValue(BigInt.parseInt("111111111111111", 2), 32767n);
assert.sameValue(BigInt.parseInt("1111111111111111", 2), 65535n);
assert.sameValue(BigInt.parseInt("11111111111111111", 2), 131071n);
assert.sameValue(BigInt.parseInt("111111111111111111", 2), 262143n);
assert.sameValue(BigInt.parseInt("1111111111111111111", 2), 524287n);
assert.sameValue(BigInt.parseInt("11111111111111111111", 2), 1048575n);
Loading

0 comments on commit 72fb638

Please sign in to comment.