Add tests for Number#toExponential (#664)

* Add tests for Number#toExponential
This commit is contained in:
Leo Balter 2016-07-21 14:57:05 -03:00 committed by Tom Care
parent 9ec41dfabd
commit 3275f17cd4
11 changed files with 397 additions and 0 deletions

View File

@ -0,0 +1,29 @@
// Copyright (C) 2016 The V8 Project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 20.1.3.2
esid: sec-number.prototype.toexponential
description: >
Return signed Infinity string if this is Infinity
info: |
Number.prototype.toExponential ( fractionDigits )
1. Let x be ? thisNumberValue(this value).
[...]
5. Let s be the empty String.
6. If x < 0, then
a. Let s be "-".
b. Let x be -x.
7. If x = +, then
a. Return the concatenation of the Strings s and "Infinity".
[...]
---*/
assert.sameValue((+Infinity).toExponential(1000), "Infinity", "Infinity value");
var n = new Number(+Infinity);
assert.sameValue(n.toExponential(1000), "Infinity", "Number Infinity");
assert.sameValue((-Infinity).toExponential(1000), "-Infinity", "-Infinity value");
var n = new Number(-Infinity);
assert.sameValue(n.toExponential(1000), "-Infinity", "Number -Infinity");

View File

@ -0,0 +1,22 @@
// Copyright (C) 2016 The V8 Project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 20.1.3.2
esid: sec-number.prototype.toexponential
description: >
Return "NaN" if this is NaN
info: |
Number.prototype.toExponential ( fractionDigits )
1. Let x be ? thisNumberValue(this value).
2. Let f be ? ToInteger(fractionDigits).
3. Assert: f is 0, when fractionDigits is undefined.
4. If x is NaN, return the String "NaN".
[...]
---*/
assert.sameValue(NaN.toExponential(Infinity), "NaN", "NaN value");
var n = new Number(NaN);
assert.sameValue(n.toExponential(NaN), "NaN", "NaN obj");

View File

@ -0,0 +1,18 @@
// Copyright (C) 2016 The V8 Project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 20.1.3.2
esid: sec-number.prototype.toexponential
description: >
"toExponential" property of Number.prototype
info: >
ES6 section 17: Every other data property described in clauses 18 through 26
and in Annex B.2 has the attributes { [[Writable]]: true,
[[Enumerable]]: false, [[Configurable]]: true } unless otherwise specified.
includes: [propertyHelper.js]
---*/
verifyNotEnumerable(Number.prototype, "toExponential");
verifyWritable(Number.prototype, "toExponential");
verifyConfigurable(Number.prototype, "toExponential");

View File

@ -0,0 +1,22 @@
// Copyright (C) 2016 The V8 Project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 20.1.3.2
esid: sec-number.prototype.toexponential
description: >
Return abrupt completion from ToInteger(symbol fractionDigits)
info: |
Number.prototype.toExponential ( fractionDigits )
1. Let x be ? thisNumberValue(this value).
2. Let f be ? ToInteger(fractionDigits).
[...]
features: [Symbol]
---*/
var fd = Symbol("1");
assert.throws(TypeError, function() {
NaN.toExponential(fd);
});

View File

@ -0,0 +1,35 @@
// Copyright (C) 2016 The V8 Project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 20.1.3.2
esid: sec-number.prototype.toexponential
description: >
Return abrupt completion from ToInteger(fractionDigits)
info: |
Number.prototype.toExponential ( fractionDigits )
1. Let x be ? thisNumberValue(this value).
2. Let f be ? ToInteger(fractionDigits).
[...]
---*/
var fd1 = {
valueOf: function() {
throw new Test262Error();
}
};
var fd2 = {
toString: function() {
throw new Test262Error();
}
};
assert.throws(Test262Error, function() {
NaN.toExponential(fd1);
}, "valueOf");
assert.throws(Test262Error, function() {
NaN.toExponential(fd2);
}, "toString");

View File

@ -0,0 +1,53 @@
// Copyright (C) 2016 The V8 Project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 20.1.3.2
esid: sec-number.prototype.toexponential
description: >
Return regular string values
---*/
assert.sameValue((123.456).toExponential(0), "1e+2");
assert.sameValue((123.456).toExponential(1), "1.2e+2");
assert.sameValue((123.456).toExponential(2), "1.23e+2");
assert.sameValue((123.456).toExponential(3), "1.235e+2");
assert.sameValue((123.456).toExponential(4), "1.2346e+2");
assert.sameValue((123.456).toExponential(5), "1.23456e+2");
assert.sameValue((123.456).toExponential(6), "1.234560e+2");
assert.sameValue((123.456).toExponential(7), "1.2345600e+2");
assert.sameValue((123.456).toExponential(17), "1.23456000000000003e+2");
assert.sameValue((123.456).toExponential(20), "1.23456000000000003070e+2");
assert.sameValue((-123.456).toExponential(0), "-1e+2");
assert.sameValue((-123.456).toExponential(1), "-1.2e+2");
assert.sameValue((-123.456).toExponential(2), "-1.23e+2");
assert.sameValue((-123.456).toExponential(3), "-1.235e+2");
assert.sameValue((-123.456).toExponential(4), "-1.2346e+2");
assert.sameValue((-123.456).toExponential(5), "-1.23456e+2");
assert.sameValue((-123.456).toExponential(6), "-1.234560e+2");
assert.sameValue((-123.456).toExponential(7), "-1.2345600e+2");
assert.sameValue((-123.456).toExponential(17), "-1.23456000000000003e+2");
assert.sameValue((-123.456).toExponential(20), "-1.23456000000000003070e+2");
assert.sameValue((0.0001).toExponential(0), "1e-4");
assert.sameValue((0.0001).toExponential(1), "1.0e-4");
assert.sameValue((0.0001).toExponential(2), "1.00e-4");
assert.sameValue((0.0001).toExponential(3), "1.000e-4");
assert.sameValue((0.0001).toExponential(4), "1.0000e-4");
assert.sameValue((0.0001).toExponential(16), "1.0000000000000000e-4");
assert.sameValue((0.0001).toExponential(17), "1.00000000000000005e-4");
assert.sameValue((0.0001).toExponential(18), "1.000000000000000048e-4");
assert.sameValue((0.0001).toExponential(19), "1.0000000000000000479e-4");
assert.sameValue((0.0001).toExponential(20), "1.00000000000000004792e-4");
assert.sameValue((0.9999).toExponential(0), "1e+0");
assert.sameValue((0.9999).toExponential(1), "1.0e+0");
assert.sameValue((0.9999).toExponential(2), "1.00e+0");
assert.sameValue((0.9999).toExponential(3), "9.999e-1");
assert.sameValue((0.9999).toExponential(4), "9.9990e-1");
assert.sameValue((0.9999).toExponential(16), "9.9990000000000001e-1");
assert.sameValue((0.9999).toExponential(17), "9.99900000000000011e-1");
assert.sameValue((0.9999).toExponential(18), "9.999000000000000110e-1");
assert.sameValue((0.9999).toExponential(19), "9.9990000000000001101e-1");
assert.sameValue((0.9999).toExponential(20), "9.99900000000000011013e-1");

View File

@ -0,0 +1,35 @@
// Copyright (C) 2016 The V8 Project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 20.1.3.2
esid: sec-number.prototype.toexponential
description: >
Return "0" if this value is 0 and ToInteger(fractionDigits) is 0
info: |
Number.prototype.toExponential ( fractionDigits )
1. Let x be ? thisNumberValue(this value).
[...]
9. If x = 0, then
a. Let m be the String consisting of f+1 occurrences of the code unit 0x0030
(DIGIT ZERO).
b. Let e be 0.
[...]
11. If f 0, then
[...]
12. If e = 0, then
a. Let c be "+".
b. Let d be "0".
[...]
14. Let m be the concatenation of the four Strings m, "e", c, and d.
15. Return the concatenation of the Strings s and m.
---*/
assert.sameValue(Number.prototype.toExponential(0), "0e+0", "Number.prototype");
assert.sameValue((0).toExponential(0), "0e+0", "(0).toExponential(0)");
assert.sameValue((-0).toExponential(0), "0e+0", "(-0).toExponential(0)");
assert.sameValue((0).toExponential(-0), "0e+0", "(0).toExponential(-0)");
assert.sameValue((-0).toExponential(-0), "0e+0", "(-0).toExponential(-0)");

View File

@ -0,0 +1,41 @@
// Copyright (C) 2016 The V8 Project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 20.1.3.2
esid: sec-number.prototype.toexponential
description: >
Return string value for this value = 0 and fractionDigits != 0
info: |
Number.prototype.toExponential ( fractionDigits )
1. Let x be ? thisNumberValue(this value).
[...]
9. If x = 0, then
a. Let m be the String consisting of f+1 occurrences of the code unit 0x0030
(DIGIT ZERO).
b. Let e be 0.
[...]
11. If f 0, then
a. Let a be the first element of m, and let b be the remaining f elements of m.
b. Let m be the concatenation of the three Strings a, ".", and b.
12. If e = 0, then
a. Let c be "+".
b. Let d be "0".
[...]
14. Let m be the concatenation of the four Strings m, "e", c, and d.
15. Return the concatenation of the Strings s and m.
---*/
assert.sameValue((0).toExponential(1), "0.0e+0", "0 and 1");
assert.sameValue((0).toExponential(2), "0.00e+0", "0 and 2");
assert.sameValue((0).toExponential(7), "0.0000000e+0", "0 and 7");
assert.sameValue((0).toExponential(20), "0.00000000000000000000e+0", "0 and 20");
assert.sameValue((-0).toExponential(1), "0.0e+0", "-0 and 1");
assert.sameValue((-0).toExponential(2), "0.00e+0", "-0 and 2");
assert.sameValue((-0).toExponential(7), "0.0000000e+0", "-0 and 7");
assert.sameValue((-0).toExponential(20), "0.00000000000000000000e+0", "-0 and 20");
assert.sameValue((0.0).toExponential(4), "0.0000e+0", "0.0 and 4");
assert.sameValue((-0.0).toExponential(4), "0.0000e+0", "-0.0 and 4");

View File

@ -0,0 +1,68 @@
// Copyright (C) 2016 The V8 Project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 20.1.3.2
esid: sec-number.prototype.toexponential
description: >
Throws a TypeError if this value is not a number object or value
info: |
20.1.3 Properties of the Number Prototype Object
The Number prototype object is the intrinsic object %NumberPrototype%. The
Number prototype object is an ordinary object. The Number prototype is itself
a Number object; it has a [[NumberData]] internal slot with the value +0.
[...]
The abstract operation thisNumberValue(value) performs the following steps:
1. If Type(value) is Number, return value.
2. If Type(value) is Object and value has a [[NumberData]] internal slot, then
a. Assert: value's [[NumberData]] internal slot is a Number value.
b. Return the value of value's [[NumberData]] internal slot.
3. Throw a TypeError exception.
Number.prototype.toExponential ( fractionDigits )
1. Let x be ? thisNumberValue(this value).
[...]
features: [Symbol]
---*/
var toExponential = Number.prototype.toExponential;
assert.throws(TypeError, function() {
toExponential.call({}, 1);
}, "{}");
assert.throws(TypeError, function() {
toExponential.call("1", 1);
}, "string");
assert.throws(TypeError, function() {
toExponential.call(Number, 1);
}, "Number");
assert.throws(TypeError, function() {
toExponential.call(true, 1);
}, "true");
assert.throws(TypeError, function() {
toExponential.call(false, 1);
}, "false");
assert.throws(TypeError, function() {
toExponential.call(null, 1);
}, "null");
assert.throws(TypeError, function() {
toExponential.call(undefined, 1);
}, "undefined");
assert.throws(TypeError, function() {
toExponential.call(Symbol("1"), 1);
}, "symbol");
assert.throws(TypeError, function() {
toExponential.call([], 1);
}, "[]");

View File

@ -0,0 +1,35 @@
// Copyright (C) 2016 The V8 Project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 20.1.3.2
esid: sec-number.prototype.toexponential
description: >
ToInteger(fractionDigits operations)
info: |
Number.prototype.toExponential ( fractionDigits )
1. Let x be ? thisNumberValue(this value).
2. Let f be ? ToInteger(fractionDigits).
[...]
---*/
assert.sameValue((123.456).toExponential(0.1), "1e+2", "0.1");
assert.sameValue((123.456).toExponential(-0.1), "1e+2", "-0.1");
assert.sameValue((123.456).toExponential(0.9), "1e+2", "0.9");
assert.sameValue((123.456).toExponential(-0.9), "1e+2", "-0.9");
assert.sameValue((123.456).toExponential(false), "1e+2", "false");
assert.sameValue((123.456).toExponential(true), "1.2e+2", "true");
assert.sameValue((123.456).toExponential(NaN), "1e+2", "NaN");
assert.sameValue((123.456).toExponential(null), "1e+2", "null");
assert.sameValue((123.456).toExponential("2"), "1.23e+2", "string");
assert.sameValue((123.456).toExponential(""), "1e+2", "the empty string");
assert.sameValue((123.456).toExponential([]), "1e+2", "[]");
assert.sameValue((123.456).toExponential([2]), "1.23e+2", "[2]");
assert.sameValue((0).toExponential(undefined), "0e+0", "undefined");
assert.sameValue((0).toExponential(), "0e+0", "no arg");

View File

@ -0,0 +1,39 @@
// Copyright (C) 2016 The V8 Project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 20.1.3.2
esid: sec-number.prototype.toexponential
description: >
Handle undefined fractionDigits, not only casting it to 0
info: |
Number.prototype.toExponential ( fractionDigits )
1. Let x be ? thisNumberValue(this value).
2. Let f be ? ToInteger(fractionDigits).
[...]
10. Else x 0,
a. If fractionDigits is not undefined, then
i. Let e and n be integers such that 10f n < 10f+1 and for which the
exact mathematical value of n × 10e-f - x is as close to zero as
possible. If there are two such sets of e and n, pick the e and n for
which n × 10e-f is larger.
b. Else fractionDigits is undefined,
i. Let e, n, and f be integers such that f 0, 10f n < 10f+1, the
Number value for n × 10e-f is x, and f is as small as possible. Note
that the decimal representation of n has f+1 digits, n is not divisible
by 10, and the least significant digit of n is not necessarily uniquely
determined by these criteria.
---*/
assert.sameValue((123.456).toExponential(undefined), "1.23456e+2", "undefined");
assert.sameValue((123.456).toExponential(), "1.23456e+2", "no arg");
assert.sameValue((123.456).toExponential(0), "1e+2", "0");
assert.sameValue((1.1e-32).toExponential(undefined), "1.1e-32", "undefined");
assert.sameValue((1.1e-32).toExponential(), "1.1e-32", "no arg");
assert.sameValue((1.1e-32).toExponential(0), "1e-32", "0");
assert.sameValue((100).toExponential(undefined), "1e+2", "undefined");
assert.sameValue((100).toExponential(), "1e+2", "no arg");
assert.sameValue((100).toExponential(0), "1e+2", "0");