Update and expand tests for isNaN

This commit is contained in:
Leonardo Balter 2016-06-30 15:05:47 -03:00 committed by Mike Pennisi
parent 81f22800fa
commit 7b1eddf6b0
20 changed files with 411 additions and 214 deletions

View File

@ -1,70 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: >
isNaN applies ToNumber to its argument, then return true if the result is
NaN, and otherwise return false
es5id: 15.1.2.4_A1_T1
description: Checking all primitive
---*/
// CHECK#1
if (!(isNaN(NaN) === true)) {
$ERROR('#1: NaN === Not-a-Number. Actual: ' + (NaN));
}
// CHECK#2
if (!(isNaN(Number.NaN) === true)) {
$ERROR('#2: Number.NaN === Not-a-Number. Actual: ' + (Number.NaN));
}
// CHECK#3
if (!(isNaN(Number(void 0)) === true)) {
$ERROR('#3: Number(void 0) === Not-a-Number. Actual: ' + (Number(void 0)));
}
// CHECK#4
if (!(isNaN(void 0) === true)) {
$ERROR('#4: void 0 === Not-a-Number. Actual: ' + (void 0));
}
// CHECK#5
if (!(isNaN("string") === true)) {
$ERROR('#5: "string" === Not-a-Number. Actual: ' + ("string"));
}
// CHECK#6
if (isNaN(Number.POSITIVE_INFINITY) === true) {
$ERROR('#6: Number.POSITIVE_INFINITY !== Not-a-Number');
}
// CHECK#7
if (isNaN(Number.NEGATIVE_INFINITY) === true) {
$ERROR('#7: Number.NEGATIVE_INFINITY !== Not-a-Number');
}
// CHECK#8
if (isNaN(Number.MAX_VALUE) === true) {
$ERROR('#8: Number.MAX_VALUE !== Not-a-Number');
}
// CHECK#9
if (isNaN(Number.MIN_VALUE) === true) {
$ERROR('#9: Number.MIN_VALUE !== Not-a-Number');
}
// CHECK#10
if (isNaN(-0) === true) {
$ERROR('#10: -0 !== Not-a-Number');
}
// CHECK#11
if (isNaN(true) === true) {
$ERROR('#11: true !== Not-a-Number');
}
// CHECK#12
if (isNaN("1") === true) {
$ERROR('#12: "1" !== Not-a-Number');
}

View File

@ -1,40 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: >
isNaN applies ToNumber to its argument, then return true if the result is
NaN, and otherwise return false
es5id: 15.1.2.4_A1_T2
description: Checking all object
---*/
// CHECK#1
if (!(isNaN({}) === true)) {
$ERROR('#1: {} === Not-a-Number. Actual: ' + ({}));
}
// CHECK#2
if (!(isNaN(new String("string")) === true)) {
$ERROR('#2: new String("string") === Not-a-Number. Actual: ' + (new String("string")));
}
// CHECK#3
if (isNaN(new String("1")) === true) {
$ERROR('#3: new String("1") === Not-a-Number. Actual: ' + (new String("1")));
}
// CHECK#4
if (isNaN(new Number(1)) === true) {
$ERROR('#4: new Number(1) !== Not-a-Number');
}
// CHECK#5
if (!(isNaN(new Number(NaN)) === true)) {
$ERROR('#5: new Number(NaN) === Not-a-Number. Actual: ' + (new Number(NaN)));
}
// CHECK#6
if (isNaN(new Boolean(true)) === true) {
$ERROR('#6: new Boolean(true) !== Not-a-Number');
}

View File

@ -1,25 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: The length property of isNaN has the attribute DontEnum
es5id: 15.1.2.4_A2.1
description: Checking use propertyIsEnumerable, for-in
---*/
//CHECK#1
if (isNaN.propertyIsEnumerable('length') !== false) {
$ERROR('#1: isNaN.propertyIsEnumerable(\'length\') === false. Actual: ' + (isNaN.propertyIsEnumerable('length')));
}
//CHECK#2
var result = true;
for (var p in isNaN){
if (p === "length") {
result = false;
}
}
if (result !== true) {
$ERROR('#2: result = true; for (p in isNaN) { if (p === "length") result = false; } result === true;');
}

View File

@ -1,25 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: The length property of isNaN does not have the attribute DontDelete
es5id: 15.1.2.4_A2.2
description: Checking use hasOwnProperty, delete
---*/
//CHECK#1
if (isNaN.hasOwnProperty('length') !== true) {
$ERROR('#1: isNaN.hasOwnProperty(\'length\') === true. Actual: ' + (isNaN.hasOwnProperty('length')));
}
delete isNaN.length;
//CHECK#2
if (isNaN.hasOwnProperty('length') !== false) {
$ERROR('#2: delete isNaN.length; isNaN.hasOwnProperty(\'length\') === false. Actual: ' + (isNaN.hasOwnProperty('length')));
}
//CHECK#3
if (isNaN.length === undefined) {
$ERROR('#3: delete isNaN.length; isNaN.length !== undefined');
}

View File

@ -1,16 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: The length property of isNaN has the attribute ReadOnly
es5id: 15.1.2.4_A2.3
description: Checking if varying the length property fails
includes: [propertyHelper.js]
---*/
//CHECK#1
var x = isNaN.length;
verifyNotWritable(isNaN, "length", null, Infinity);
if (isNaN.length !== x) {
$ERROR('#1: x = isNaN.length; isNaN.length = Infinity; isNaN.length === x. Actual: ' + (isNaN.length));
}

View File

@ -1,13 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: The length property of isNaN is 1
es5id: 15.1.2.4_A2.4
description: isNaN.length === 1
---*/
//CHECK#1
if (isNaN.length !== 1) {
$ERROR('#1: isNaN.length === 1. Actual: ' + (isNaN.length));
}

View File

@ -1,25 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: The isNaN property has the attribute DontEnum
es5id: 15.1.2.4_A2.5
description: Checking use propertyIsEnumerable, for-in
---*/
//CHECK#1
if (this.propertyIsEnumerable('isNaN') !== false) {
$ERROR('#1: this.propertyIsEnumerable(\'isNaN\') === false. Actual: ' + (this.propertyIsEnumerable('isNaN')));
}
//CHECK#2
var result = true;
for (var p in this){
if (p === "isNaN") {
result = false;
}
}
if (result !== true) {
$ERROR('#2: result = true; for (p in this) { if (p === "isNaN") result = false; } result === true;');
}

View File

@ -0,0 +1,16 @@
// 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-isnan-number
es6id: 18.2.3
description: >
The length property of isNaN is 1
includes: [propertyHelper.js]
---*/
assert.sameValue(isNaN.length, 1, "The value of `isNaN.length` is `1`");
verifyNotEnumerable(isNaN, "length");
verifyNotWritable(isNaN, "length");
verifyConfigurable(isNaN, "length");

View File

@ -0,0 +1,14 @@
// 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-isnan-number
es6id: 18.2.3
description: >
Property descriptor for isNaN
includes: [propertyHelper.js]
---*/
verifyNotEnumerable(this, "isNaN");
verifyWritable(this, "isNaN");
verifyConfigurable(this, "isNaN");

View File

@ -0,0 +1,20 @@
// 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-isnan-number
es6id: 18.2.3
description: >
Throws a TypeError if number is a Symbol
info: |
isNaN (number)
1. Let num be ? ToNumber(number).
features: [Symbol]
---*/
var s = Symbol(1);
assert.throws(TypeError, function() {
isNaN(s);
});

View File

@ -0,0 +1,33 @@
// 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-isnan-number
es6id: 18.2.3
description: >
Return abrupt completion from ToNumber(number)
info: |
isNaN (number)
1. Let num be ? ToNumber(number).
---*/
var obj1 = {
valueOf: function() {
throw new Test262Error();
}
};
var obj2 = {
toString: function() {
throw new Test262Error();
}
};
assert.throws(Test262Error, function() {
isNaN(obj1);
}, "valueOf");
assert.throws(Test262Error, function() {
isNaN(obj2);
}, "toString");

View File

@ -0,0 +1,28 @@
// 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-isnan-number
es6id: 18.2.3
description: >
Return false if number is not NaN
info: |
isNaN (number)
1. Let num be ? ToNumber(number).
2. If num is NaN, return true.
3. Otherwise, return false.
---*/
assert.sameValue(isNaN(0), false, "0");
assert.sameValue(isNaN(-0), false, "-0");
assert.sameValue(isNaN(Math.pow(2, 53)), false, "Math.pow(2, 53)");
assert.sameValue(isNaN(-Math.pow(2, 53)), false, "-Math.pow(2, 53)");
assert.sameValue(isNaN(1), false, "1");
assert.sameValue(isNaN(-1), false, "-1");
assert.sameValue(isNaN(0.000001), false, "0.000001");
assert.sameValue(isNaN(-0.000001), false, "-0.000001");
assert.sameValue(isNaN(1e42), false, "1e42");
assert.sameValue(isNaN(-1e42), false, "-1e42");
assert.sameValue(isNaN(Infinity), false, "Infinity");
assert.sameValue(isNaN(-Infinity), false, "-Infinity");

View File

@ -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-isnan-number
es6id: 18.2.3
description: >
Return true if number is NaN
info: |
isNaN (number)
1. Let num be ? ToNumber(number).
2. If num is NaN, return true.
includes: [nans.js]
---*/
distinctNaNs.forEach(function(v, i) {
assert.sameValue(isNaN(v), true, "value on position: " + i);
});

View File

@ -0,0 +1,28 @@
// 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-isnan-number
es6id: 18.2.3
description: >
number argument is converted by ToNumber
info: |
isNaN (number)
1. Let num be ? ToNumber(number).
2. If num is NaN, return true.
3. Otherwise, return false.
---*/
assert.sameValue(isNaN("0"), false, "'0'");
assert.sameValue(isNaN(""), false, "the empty string");
assert.sameValue(isNaN("Infinity"), false, "'Infinity'");
assert.sameValue(isNaN("this is not a number"), true, "string");
assert.sameValue(isNaN(true), false, "true");
assert.sameValue(isNaN(false), false, "false");
assert.sameValue(isNaN([1]), false, "Object [1]");
assert.sameValue(isNaN([Infinity]), false, "Object [Infinity]");
assert.sameValue(isNaN([NaN]), true, "Object [NaN]");
assert.sameValue(isNaN(null), false, "null");
assert.sameValue(isNaN(undefined), true, "undefined");
assert.sameValue(isNaN(), true, "no arg");

View File

@ -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-isnan-number
es6id: 18.2.3
description: >
Return abrupt completion calling number.@@toPrimitive
info: |
isNaN (number)
1. Let num be ? ToNumber(number).
ToPrimitive ( input [ , PreferredType ] )
[...]
4. Let exoticToPrim be ? GetMethod(input, @@toPrimitive).
5. If exoticToPrim is not undefined, then
a. Let result be ? Call(exoticToPrim, input, « hint »).
features: [Symbol.toPrimitive]
---*/
var obj = {};
obj[Symbol.toPrimitive] = function() {
throw new Test262Error();
};
assert.throws(Test262Error, function() {
isNaN(obj);
});

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.
/*---
esid: sec-isnan-number
es6id: 18.2.3
description: >
Return abrupt completion getting number.@@toPrimitive
info: |
isNaN (number)
1. Let num be ? ToNumber(number).
ToPrimitive ( input [ , PreferredType ] )
[...]
4. Let exoticToPrim be ? GetMethod(input, @@toPrimitive).
features: [Symbol.toPrimitive]
---*/
var obj = Object.defineProperty({}, Symbol.toPrimitive, {
get: function() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
isNaN(obj);
});

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.
/*---
esid: sec-isnan-number
es6id: 18.2.3
description: >
Throws a TypeError if number.@@toPrimitive is not null, undefined, or callable
info: |
isNaN (number)
1. Let num be ? ToNumber(number).
ToPrimitive ( input [ , PreferredType ] )
[...]
4. Let exoticToPrim be ? GetMethod(input, @@toPrimitive).
GetMethod (V, P)
[...]
2. Let func be ? GetV(V, P).
3. If func is either undefined or null, return undefined.
4. If IsCallable(func) is false, throw a TypeError exception.
features: [Symbol.toPrimitive]
---*/
var obj = {};
obj[Symbol.toPrimitive] = 42;
assert.throws(TypeError, function() {
isNaN(obj);
}, "number");
obj[Symbol.toPrimitive] = "";
assert.throws(TypeError, function() {
isNaN(obj);
}, "string");
obj[Symbol.toPrimitive] = true;
assert.throws(TypeError, function() {
isNaN(obj);
}, "boolean");
obj[Symbol.toPrimitive] = Symbol.toPrimitive;
assert.throws(TypeError, function() {
isNaN(obj);
}, "symbol");
obj[Symbol.toPrimitive] = {};
assert.throws(TypeError, function() {
isNaN(obj);
}, "object");

View File

@ -0,0 +1,32 @@
// 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-isnan-number
es6id: 18.2.3
description: >
Throws a TypeError if the result of calling number.@@toPrimitive is an Object
info: |
isNaN (number)
1. Let num be ? ToNumber(number).
ToPrimitive ( input [ , PreferredType ] )
[...]
4. Let exoticToPrim be ? GetMethod(input, @@toPrimitive).
5. If exoticToPrim is not undefined, then
a. Let result be ? Call(exoticToPrim, input, « hint »).
b. If Type(result) is not Object, return result.
c. Throw a TypeError exception.
features: [Symbol.toPrimitive]
---*/
var obj = {};
obj[Symbol.toPrimitive] = function() {
return [42];
};
assert.throws(TypeError, function() {
isNaN(obj);
});

View File

@ -0,0 +1,36 @@
// 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-isnan-number
es6id: 18.2.3
description: >
Throws a TypeError if the result of calling number.@@toPrimitive is a symbol
info: |
isNaN (number)
1. Let num be ? ToNumber(number).
ToNumber ( argument )
1. Let primValue be ? ToPrimitive(argument, hint Number).
2. Return ? ToNumber(primValue).
ToPrimitive ( input [ , PreferredType ] )
[...]
4. Let exoticToPrim be ? GetMethod(input, @@toPrimitive).
5. If exoticToPrim is not undefined, then
a. Let result be ? Call(exoticToPrim, input, « hint »).
b. If Type(result) is not Object, return result.
features: [Symbol.toPrimitive]
---*/
var obj = {};
obj[Symbol.toPrimitive] = function() {
return Symbol.toPrimitive;
};
assert.throws(TypeError, function() {
isNaN(obj);
});

View File

@ -0,0 +1,73 @@
// 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-isnan-number
es6id: 18.2.3
description: >
Use non-object value returned from @@toPrimitive method
info: |
isNaN (number)
1. Let num be ? ToNumber(number).
ToPrimitive ( input [ , PreferredType ] )
[...]
4. Let exoticToPrim be ? GetMethod(input, @@toPrimitive).
5. If exoticToPrim is not undefined, then
a. Let result be ? Call(exoticToPrim, input, « hint »).
b. If Type(result) is not Object, return result.
features: [Symbol.toPrimitive]
---*/
var called = 0;
var obj = {
valueOf: function() {
called = NaN;
return Infinity;
},
toString: function() {
called = NaN;
return Infinity;
}
};
obj[Symbol.toPrimitive] = function() {
called += 1;
return 42;
};
assert.sameValue(isNaN(obj), false, "use returned value - non-NaN number");
assert.sameValue(called, 1, "toPrimitive is called - non-NaN number");
called = 0;
obj[Symbol.toPrimitive] = function() {
called += 1;
return "this is not a number";
};
assert.sameValue(isNaN(obj), true, "use returned value - string to NaN");
assert.sameValue(called, 1, "toPrimitive is called - string to NaN");
called = 0;
obj[Symbol.toPrimitive] = function() {
called += 1;
return true;
};
assert.sameValue(isNaN(obj), false, "use returned value - boolean true");
assert.sameValue(called, 1, "toPrimitive is called - boolean true");
called = 0;
obj[Symbol.toPrimitive] = function() {
called += 1;
return false;
};
assert.sameValue(isNaN(obj), false, "use returned value - boolean false");
assert.sameValue(called, 1, "toPrimitive is called - boolean false");
called = 0;
obj[Symbol.toPrimitive] = function() {
called += 1;
return NaN;
};
assert.sameValue(isNaN(obj), true, "use returned value - NaN");
assert.sameValue(called, 1, "toPrimitive is called - NaN");