diff options
Diffstat (limited to 'js/src/tests/test262/built-ins/Number/prototype/toPrecision')
19 files changed, 742 insertions, 0 deletions
diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/browser.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/browser.js diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/exponential.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/exponential.js new file mode 100644 index 0000000000..172abfb596 --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/exponential.js @@ -0,0 +1,110 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-number.prototype.toprecision +description: > + Return string values using exponential character +info: | + Number.prototype.toPrecision ( precision ) + + 1. Let x be ? thisNumberValue(this value). + [...] + 5. Let s be the empty String. + [...] + 9. If x = 0, then + [...] + 10. Else x ≠ 0, + [...] + c. If e < -6 or e ≥ p, then + [...] + vii. Return the concatenation of s, m, code unit 0x0065 (LATIN SMALL + LETTER E), c, and d. + [...] +---*/ + +assert.sameValue((10).toPrecision(1), "1e+1"); +assert.sameValue((11).toPrecision(1), "1e+1"); +assert.sameValue((17).toPrecision(1), "2e+1"); +assert.sameValue((19).toPrecision(1), "2e+1"); +assert.sameValue((20).toPrecision(1), "2e+1"); + +assert.sameValue((100).toPrecision(1), "1e+2"); +assert.sameValue((1000).toPrecision(1), "1e+3"); +assert.sameValue((10000).toPrecision(1), "1e+4"); +assert.sameValue((100000).toPrecision(1), "1e+5"); + +assert.sameValue((100).toPrecision(2), "1.0e+2"); +assert.sameValue((1000).toPrecision(2), "1.0e+3"); +assert.sameValue((10000).toPrecision(2), "1.0e+4"); +assert.sameValue((100000).toPrecision(2), "1.0e+5"); + +assert.sameValue((1000).toPrecision(3), "1.00e+3"); +assert.sameValue((10000).toPrecision(3), "1.00e+4"); +assert.sameValue((100000).toPrecision(3), "1.00e+5"); + +assert.sameValue((42).toPrecision(1), "4e+1"); +assert.sameValue((-42).toPrecision(1), "-4e+1"); + +assert.sameValue((1.2345e+27).toPrecision(1), "1e+27"); +assert.sameValue((1.2345e+27).toPrecision(2), "1.2e+27"); +assert.sameValue((1.2345e+27).toPrecision(3), "1.23e+27"); +assert.sameValue((1.2345e+27).toPrecision(4), "1.234e+27"); +assert.sameValue((1.2345e+27).toPrecision(5), "1.2345e+27"); +assert.sameValue((1.2345e+27).toPrecision(6), "1.23450e+27"); +assert.sameValue((1.2345e+27).toPrecision(7), "1.234500e+27"); +assert.sameValue((1.2345e+27).toPrecision(16), "1.234500000000000e+27"); +assert.sameValue((1.2345e+27).toPrecision(17), "1.2345000000000000e+27"); +assert.sameValue((1.2345e+27).toPrecision(18), "1.23449999999999996e+27"); +assert.sameValue((1.2345e+27).toPrecision(19), "1.234499999999999962e+27"); +assert.sameValue((1.2345e+27).toPrecision(20), "1.2344999999999999618e+27"); +assert.sameValue((1.2345e+27).toPrecision(21), "1.23449999999999996184e+27"); + +assert.sameValue((-1.2345e+27).toPrecision(1), "-1e+27"); +assert.sameValue((-1.2345e+27).toPrecision(2), "-1.2e+27"); +assert.sameValue((-1.2345e+27).toPrecision(3), "-1.23e+27"); +assert.sameValue((-1.2345e+27).toPrecision(4), "-1.234e+27"); +assert.sameValue((-1.2345e+27).toPrecision(5), "-1.2345e+27"); +assert.sameValue((-1.2345e+27).toPrecision(6), "-1.23450e+27"); +assert.sameValue((-1.2345e+27).toPrecision(7), "-1.234500e+27"); +assert.sameValue((-1.2345e+27).toPrecision(16), "-1.234500000000000e+27"); +assert.sameValue((-1.2345e+27).toPrecision(17), "-1.2345000000000000e+27"); +assert.sameValue((-1.2345e+27).toPrecision(18), "-1.23449999999999996e+27"); +assert.sameValue((-1.2345e+27).toPrecision(19), "-1.234499999999999962e+27"); +assert.sameValue((-1.2345e+27).toPrecision(20), "-1.2344999999999999618e+27"); +assert.sameValue((-1.2345e+27).toPrecision(21), "-1.23449999999999996184e+27"); + +var n = new Number("1000000000000000000000"); // 1e+21 +assert.sameValue((n).toPrecision(1), "1e+21"); +assert.sameValue((n).toPrecision(2), "1.0e+21"); +assert.sameValue((n).toPrecision(3), "1.00e+21"); +assert.sameValue((n).toPrecision(4), "1.000e+21"); +assert.sameValue((n).toPrecision(5), "1.0000e+21"); +assert.sameValue((n).toPrecision(6), "1.00000e+21"); +assert.sameValue((n).toPrecision(7), "1.000000e+21"); +assert.sameValue((n).toPrecision(16), "1.000000000000000e+21"); +assert.sameValue((n).toPrecision(17), "1.0000000000000000e+21"); +assert.sameValue((n).toPrecision(18), "1.00000000000000000e+21"); +assert.sameValue((n).toPrecision(19), "1.000000000000000000e+21"); +assert.sameValue((n).toPrecision(20), "1.0000000000000000000e+21"); +assert.sameValue((n).toPrecision(21), "1.00000000000000000000e+21"); + +var n = new Number("0.000000000000000000001"); // 1e-21 +assert.sameValue((n).toPrecision(1), "1e-21"); +assert.sameValue((n).toPrecision(2), "1.0e-21"); +assert.sameValue((n).toPrecision(3), "1.00e-21"); +assert.sameValue((n).toPrecision(4), "1.000e-21"); +assert.sameValue((n).toPrecision(5), "1.0000e-21"); +assert.sameValue((n).toPrecision(6), "1.00000e-21"); +assert.sameValue((n).toPrecision(7), "1.000000e-21"); +assert.sameValue((n).toPrecision(16), "9.999999999999999e-22"); +assert.sameValue((n).toPrecision(17), "9.9999999999999991e-22"); +assert.sameValue((n).toPrecision(18), "9.99999999999999908e-22"); +assert.sameValue((n).toPrecision(19), "9.999999999999999075e-22"); +assert.sameValue((n).toPrecision(20), "9.9999999999999990754e-22"); +assert.sameValue((n).toPrecision(21), "9.99999999999999907537e-22"); + +assert.sameValue((0.00000001).toPrecision(1), "1e-8"); +assert.sameValue((-0.00000001).toPrecision(1), "-1e-8"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/infinity.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/infinity.js new file mode 100644 index 0000000000..6a0c519708 --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/infinity.js @@ -0,0 +1,30 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-number.prototype.toprecision +description: > + Return "NaN" if this is NaN +info: | + Number.prototype.toPrecision ( precision ) + + 1. Let x be ? thisNumberValue(this value). + [...] + 5. Let s be the empty String. + 6. If x < 0, then + a. Let s be code unit 0x002D (HYPHEN-MINUS). + b. Let x be -x. + 7. If x = +∞, then + a. Return the String that is the concatenation of s and "Infinity". + [...] +---*/ + +assert.sameValue((+Infinity).toPrecision(1000), "Infinity", "Infinity value"); +var n = new Number(+Infinity); +assert.sameValue(n.toPrecision(1000), "Infinity", "Number Infinity"); + +assert.sameValue((-Infinity).toPrecision(1000), "-Infinity", "-Infinity value"); +var n = new Number(-Infinity); +assert.sameValue(n.toPrecision(1000), "-Infinity", "Number -Infinity"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/length.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/length.js new file mode 100644 index 0000000000..a5cecd54d3 --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/length.js @@ -0,0 +1,31 @@ +// Copyright (C) 2015 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +es6id: 20.1.3.5 +description: > + Number.prototype.toPrecision.length is 1. +info: | + Number.prototype.toPrecision ( precision ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, has a length + property whose value is an integer. Unless otherwise specified, this + value is equal to the largest number of named arguments shown in the + subclause headings for the function description, including optional + parameters. However, rest parameters shown using the form “...name” + are not included in the default argument count. + + Unless otherwise specified, the length property of a built-in Function + object has the attributes { [[Writable]]: false, [[Enumerable]]: false, + [[Configurable]]: true }. +includes: [propertyHelper.js] +---*/ + +assert.sameValue(Number.prototype.toPrecision.length, 1); + +verifyNotEnumerable(Number.prototype.toPrecision, "length"); +verifyNotWritable(Number.prototype.toPrecision, "length"); +verifyConfigurable(Number.prototype.toPrecision, "length"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/name.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/name.js new file mode 100644 index 0000000000..946c5ad308 --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/name.js @@ -0,0 +1,28 @@ +// Copyright (C) 2015 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +es6id: 20.1.3.5 +description: > + Number.prototype.toPrecision.name is "toPrecision". +info: | + Number.prototype.toPrecision ( precision ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, that is not + identified as an anonymous function has a name property whose value + is a String. + + Unless otherwise specified, the name property of a built-in Function + object, if it exists, has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. +includes: [propertyHelper.js] +---*/ + +assert.sameValue(Number.prototype.toPrecision.name, "toPrecision"); + +verifyNotEnumerable(Number.prototype.toPrecision, "name"); +verifyNotWritable(Number.prototype.toPrecision, "name"); +verifyConfigurable(Number.prototype.toPrecision, "name"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/nan.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/nan.js new file mode 100644 index 0000000000..41bc08e922 --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/nan.js @@ -0,0 +1,56 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-number.prototype.toprecision +description: > + Return "NaN" if this is NaN +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.toPrecision ( precision ) + + 1. Let x be ? thisNumberValue(this value). + 2. If precision is undefined, return ! ToString(x). + 3. Let p be ? ToInteger(precision). + 4. If x is NaN, return the String "NaN". + [...] +---*/ + +assert.sameValue( + NaN.toPrecision(undefined), + "NaN", + "step 2: If precision is undefined, return ! ToString(x)" +); + +var calls = 0; + +var p = { + valueOf: function() { + calls++; + return Infinity; + } +}; + +assert.sameValue(NaN.toPrecision(p), "NaN", "value"); +assert.sameValue(calls, 1, "NaN is checked after ToInteger(precision)"); + +var n = new Number(NaN); +calls = 0; +assert.sameValue(n.toPrecision(p), "NaN", "object"); +assert.sameValue(calls, 1, "Number NaN is checked after ToInteger(precision)"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/not-a-constructor.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/not-a-constructor.js new file mode 100644 index 0000000000..5424e12b7e --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/not-a-constructor.js @@ -0,0 +1,35 @@ +// Copyright (C) 2020 Rick Waldron. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-ecmascript-standard-built-in-objects +description: > + Number.prototype.toPrecision does not implement [[Construct]], is not new-able +info: | + ECMAScript Function Objects + + Built-in function objects that are not identified as constructors do not + implement the [[Construct]] internal method unless otherwise specified in + the description of a particular function. + + sec-evaluatenew + + ... + 7. If IsConstructor(constructor) is false, throw a TypeError exception. + ... +includes: [isConstructor.js] +features: [Reflect.construct, arrow-function] +---*/ + +assert.sameValue( + isConstructor(Number.prototype.toPrecision), + false, + 'isConstructor(Number.prototype.toPrecision) must return false' +); + +assert.throws(TypeError, () => { + new Number.prototype.toPrecision(); +}, '`new Number.prototype.toPrecision()` throws TypeError'); + + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/precision-cannot-be-coerced-to-a-number-in-range.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/precision-cannot-be-coerced-to-a-number-in-range.js new file mode 100644 index 0000000000..e3aa62e7cd --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/precision-cannot-be-coerced-to-a-number-in-range.js @@ -0,0 +1,30 @@ +// Copyright (C) 2020 Rick Waldron. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-number.prototype.toprecision +description: > + Throws a RangeError if precision cannot be coerced to a number in range. +info: | + Let p be ? ToInteger(precision). + If x is not finite, return ! Number::toString(x). + If p < 1 or p > 100, throw a RangeError exception. + +features: [Symbol] +---*/ + +var toPrecision = Number.prototype.toPrecision; + +assert.throws(RangeError, function() { + toPrecision.call(1, function() {}); +}, "`function() {}` doesn't coerce into a number in range (1-100)"); + +assert.throws(RangeError, function() { + toPrecision.call(1, NaN); +}, "NaN doesn't coerce into a number in range (1-100)"); + +assert.throws(RangeError, function() { + toPrecision.call(1, {}); +}, "{} doesn't coerce into a number in range (1-100)"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/prop-desc.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/prop-desc.js new file mode 100644 index 0000000000..f46b260076 --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/prop-desc.js @@ -0,0 +1,19 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-number.prototype.toprecision +description: > + "toPrecision" 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, "toPrecision"); +verifyWritable(Number.prototype, "toPrecision"); +verifyConfigurable(Number.prototype, "toPrecision"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/range.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/range.js new file mode 100644 index 0000000000..7c1043736f --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/range.js @@ -0,0 +1,22 @@ +// Copyright (C) 2017 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-number.prototype.toprecision +description: Number.prototype.toPrecision permits fractionDigits from 1 to 100 +info: | + Number.prototype.toPrecision ( fractionDigits ) + + ... + 8. If _p_ < 1 or _p_ > 100, throw a *RangeError* exception. + ... +---*/ + +assert.sameValue((3).toPrecision(1), "3"); +assert.throws(RangeError, () => (3).toPrecision(0)); +assert.throws(RangeError, () => (3).toPrecision(-10)); + +assert.sameValue((3).toPrecision(100), "3.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); +assert.throws(RangeError, () => (3).toPrecision(101)); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/return-abrupt-tointeger-precision-symbol.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/return-abrupt-tointeger-precision-symbol.js new file mode 100644 index 0000000000..7a9374bb85 --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/return-abrupt-tointeger-precision-symbol.js @@ -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. + +/*--- +esid: sec-number.prototype.toprecision +description: > + Return abrupt completion from ToInteger(symbol precision) +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.toPrecision ( precision ) + + 1. Let x be ? thisNumberValue(this value). + 2. If precision is undefined, return ! ToString(x). + 3. Let p be ? ToInteger(precision). + [...] +features: [Symbol] +---*/ + +var p = Symbol("1"); + +assert.throws(TypeError, function() { + Number.prototype.toPrecision(p); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/return-abrupt-tointeger-precision.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/return-abrupt-tointeger-precision.js new file mode 100644 index 0000000000..4d972449be --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/return-abrupt-tointeger-precision.js @@ -0,0 +1,52 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-number.prototype.toprecision +description: > + Return abrupt completion from ToInteger(precision) +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.toPrecision ( precision ) + + 1. Let x be ? thisNumberValue(this value). + 2. If precision is undefined, return ! ToString(x). + 3. Let p be ? ToInteger(precision). + [...] +---*/ + +var p1 = { + valueOf: function() { + throw new Test262Error(); + } +}; + +var p2 = { + toString: function() { + throw new Test262Error(); + } +}; + +assert.throws(Test262Error, function() { + Number.prototype.toPrecision(p1); +}, "valueOf"); + +assert.throws(Test262Error, function() { + Number.prototype.toPrecision(p2); +}, "toString"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/return-values.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/return-values.js new file mode 100644 index 0000000000..b75141141a --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/return-values.js @@ -0,0 +1,65 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-number.prototype.toprecision +description: > + Return regular string values +info: | + Number.prototype.toPrecision ( precision ) + + 1. Let x be ? thisNumberValue(this value). + [...] + 5. Let s be the empty String. + [...] + 11. If e = p-1, return the concatenation of the Strings s and m. + 12. If e ≥ 0, then + a. Let m be the concatenation of the first e+1 elements of m, the code unit + 0x002E (FULL STOP), and the remaining p- (e+1) elements of m. + 13. Else e < 0, + a. Let m be the String formed by the concatenation of code unit 0x0030 + (DIGIT ZERO), code unit 0x002E (FULL STOP), -(e+1) occurrences of code unit + 0x0030 (DIGIT ZERO), and the String m. + 14. Return the String that is the concatenation of s and m. +---*/ + +assert.sameValue((7).toPrecision(1), "7"); +assert.sameValue((7).toPrecision(2), "7.0"); +assert.sameValue((7).toPrecision(3), "7.00"); +assert.sameValue((7).toPrecision(19), "7.000000000000000000"); +assert.sameValue((7).toPrecision(20), "7.0000000000000000000"); +assert.sameValue((7).toPrecision(21), "7.00000000000000000000"); + +assert.sameValue((-7).toPrecision(1), "-7"); +assert.sameValue((-7).toPrecision(2), "-7.0"); +assert.sameValue((-7).toPrecision(3), "-7.00"); +assert.sameValue((-7).toPrecision(19), "-7.000000000000000000"); +assert.sameValue((-7).toPrecision(20), "-7.0000000000000000000"); +assert.sameValue((-7).toPrecision(21), "-7.00000000000000000000"); + +assert.sameValue((10).toPrecision(2), "10"); +assert.sameValue((11).toPrecision(2), "11"); +assert.sameValue((17).toPrecision(2), "17"); +assert.sameValue((19).toPrecision(2), "19"); +assert.sameValue((20).toPrecision(2), "20"); + +assert.sameValue((-10).toPrecision(2), "-10"); +assert.sameValue((-11).toPrecision(2), "-11"); +assert.sameValue((-17).toPrecision(2), "-17"); +assert.sameValue((-19).toPrecision(2), "-19"); +assert.sameValue((-20).toPrecision(2), "-20"); + +assert.sameValue((42).toPrecision(2), "42"); +assert.sameValue((-42).toPrecision(2), "-42"); + +assert.sameValue((100).toPrecision(3), "100"); +assert.sameValue((100).toPrecision(7), "100.0000"); +assert.sameValue((1000).toPrecision(7), "1000.000"); +assert.sameValue((10000).toPrecision(7), "10000.00"); +assert.sameValue((100000).toPrecision(7), "100000.0"); + +assert.sameValue((0.000001).toPrecision(1), "0.000001"); +assert.sameValue((0.000001).toPrecision(2), "0.0000010"); +assert.sameValue((0.000001).toPrecision(3), "0.00000100"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/shell.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/shell.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/shell.js diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/this-is-0-precision-is-1.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/this-is-0-precision-is-1.js new file mode 100644 index 0000000000..eefebd7099 --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/this-is-0-precision-is-1.js @@ -0,0 +1,27 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-number.prototype.toprecision +description: > + Return "0" if this value is 0 and precision is 1 +info: | + Number.prototype.toPrecision ( precision ) + + 1. Let x be ? thisNumberValue(this value). + [...] + 5. Let s be the empty String. + [...] + 9. If x = 0, then + a. Let m be the String consisting of p occurrences of the code unit 0x0030 + (DIGIT ZERO). + b. Let e be 0. + [...] + 11. If e = p-1, return the concatenation of the Strings s and m. +---*/ + +assert.sameValue(Number.prototype.toPrecision(1), "0", "Number.prototype is 0"); + +assert.sameValue((-0).toPrecision(1), "0", "-0"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/this-is-0-precision-is-gter-than-1.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/this-is-0-precision-is-gter-than-1.js new file mode 100644 index 0000000000..ba6c6f3ad4 --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/this-is-0-precision-is-gter-than-1.js @@ -0,0 +1,64 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-number.prototype.toprecision +description: > + Return string value for this value = 0 and precision is > 1 +info: | + Number.prototype.toPrecision ( precision ) + + 1. Let x be ? thisNumberValue(this value). + [...] + 5. Let s be the empty String. + [...] + 9. If x = 0, then + a. Let m be the String consisting of p occurrences of the code unit 0x0030 + (DIGIT ZERO). + b. Let e be 0. + [...] + 11. If e = p-1, return the concatenation of the Strings s and m. + 12. If e ≥ 0, then + a. Let m be the concatenation of the first e+1 elements of m, the code unit + 0x002E (FULL STOP), and the remaining p- (e+1) elements of m. + [...] + 14. Return the String that is the concatenation of s and m. +---*/ + +assert.sameValue( + (0).toPrecision(2), + "0.0", + "(0).toPrecision(2)" +); + +assert.sameValue( + (0).toPrecision(7), + "0.000000", + "(0).toPrecision(7)" +); + +assert.sameValue( + (0).toPrecision(21), + "0.00000000000000000000", + "(0).toPrecision(21)" +); + +assert.sameValue( + (-0).toPrecision(2), + "0.0", + "(-0).toPrecision(2)" +); + +assert.sameValue( + (-0).toPrecision(7), + "0.000000", + "(-0).toPrecision(7)" +); + +assert.sameValue( + (-0).toPrecision(21), + "0.00000000000000000000", + "(-0).toPrecision(21)" +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/this-type-not-number-or-number-object.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/this-type-not-number-or-number-object.js new file mode 100644 index 0000000000..27d8f1eba6 --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/this-type-not-number-or-number-object.js @@ -0,0 +1,69 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-number.prototype.toprecision +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.toPrecision ( precision ) + + 1. Let x be ? thisNumberValue(this value). + [...] +features: [Symbol] +---*/ + +var toPrecision = Number.prototype.toPrecision; + +assert.throws(TypeError, function() { + toPrecision.call({}, 1); +}, "{}"); + +assert.throws(TypeError, function() { + toPrecision.call("1", 1); +}, "string"); + +assert.throws(TypeError, function() { + toPrecision.call(Number, 1); +}, "Number"); + +assert.throws(TypeError, function() { + toPrecision.call(true, 1); +}, "true"); + +assert.throws(TypeError, function() { + toPrecision.call(false, 1); +}, "false"); + +assert.throws(TypeError, function() { + toPrecision.call(null, 1); +}, "null"); + +assert.throws(TypeError, function() { + toPrecision.call(undefined, 1); +}, "undefined"); + +assert.throws(TypeError, function() { + toPrecision.call(Symbol("1"), 1); +}, "symbol"); + +assert.throws(TypeError, function() { + toPrecision.call([], 1); +}, "[]"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/tointeger-precision.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/tointeger-precision.js new file mode 100644 index 0000000000..90599f6589 --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/tointeger-precision.js @@ -0,0 +1,25 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-number.prototype.toprecision +description: > + ToInteger(precision) operations +info: | + Number.prototype.toPrecision ( precision ) + + [...] + 3. Let p be ? ToInteger(precision). + [...] +---*/ + +assert.sameValue((123.456).toPrecision(1.1), "1e+2", "1.1"); +assert.sameValue((123.456).toPrecision(1.9), "1e+2", "1.9"); + +assert.sameValue((123.456).toPrecision(true), "1e+2", "true"); + +assert.sameValue((123.456).toPrecision("2"), "1.2e+2", "string"); + +assert.sameValue((123.456).toPrecision([2]), "1.2e+2", "[2]"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Number/prototype/toPrecision/undefined-precision-arg.js b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/undefined-precision-arg.js new file mode 100644 index 0000000000..54ebcc14e4 --- /dev/null +++ b/js/src/tests/test262/built-ins/Number/prototype/toPrecision/undefined-precision-arg.js @@ -0,0 +1,40 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-number.prototype.toprecision +description: > + Return a string containing the the number value of this if precision is + undefined +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.toPrecision ( precision ) + + 1. Let x be ? thisNumberValue(this value). + 2. If precision is undefined, return ! ToString(x). + [...] +---*/ + +var n = new Number(7); + +assert.sameValue(n.toPrecision(undefined), "7"); +assert.sameValue((39).toPrecision(undefined), "39"); + +assert.sameValue(Number.prototype.toPrecision(), "0"); +assert.sameValue((42).toPrecision(), "42"); + +reportCompare(0, 0); |