Skip to content

Commit

Permalink
Add %TypedArray%.prototype.includes tests
Browse files Browse the repository at this point in the history
Includes new testTypedArrays.js helper.
  • Loading branch information
domenic committed Nov 17, 2015
1 parent 8d3ffdd commit 9467eaa
Show file tree
Hide file tree
Showing 17 changed files with 347 additions and 0 deletions.
21 changes: 21 additions & 0 deletions harness/testTypedArrays.js
@@ -0,0 +1,21 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

function testTypedArrays(callback) {
[
Uint8Array,
Int8Array,
Uint16Array,
Int16Array,
Uint32Array,
Int32Array,
Uint8ClampedArray,
Float32Array,
Float64Array
]
.forEach(callback);
}

testTypedArrays.floatOnly = function (callback) {
[Float32Array, Float64Array].forEach(callback);
};
26 changes: 26 additions & 0 deletions test/built-ins/TypedArray/prototype/includes/exception-bad-this.js
@@ -0,0 +1,26 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes throws a TypeError when used on non-typed arrays
author: Domenic Denicola
---*/

var taIncludes = Uint8Array.prototype.includes;

assert.throws(TypeError, function () {
taIncludes.call({ length: 2, 0: 1, 1: 2 }, 2);
});

assert.throws(TypeError, function () {
taIncludes.call([1, 2, 3], 2);
});

assert.throws(TypeError, function () {
taIncludes.call(null, 2);
});

assert.throws(TypeError, function () {
taIncludes.call(undefined, 2);
});
@@ -0,0 +1,17 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes should terminate if ToNumber ends up being called on a symbol fromIndex
author: Domenic Denicola
includes: [testTypedArrays.js]
---*/

testTypedArrays(function (TypedArrayConstructor) {
var ta = new TypedArrayConstructor([1, 2, 3]);

assert.throws(TypeError, function () {
ta.includes(2, Symbol());
});
});
@@ -0,0 +1,22 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes should terminate if an exception occurs converting the fromIndex to a number
includes: [Test262Error.js, testTypedArrays.js]
---*/

var fromIndex = {
valueOf: function () {
throw new Test262Error('This error should be re-thrown');
}
};

testTypedArrays(function (TypedArrayConstructor) {
var ta = new TypedArrayConstructor([1, 2, 3]);

assert.throws(Test262Error, function () {
ta.includes(2, fromIndex);
});
});
18 changes: 18 additions & 0 deletions test/built-ins/TypedArray/prototype/includes/from-index-default.js
@@ -0,0 +1,18 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes should search the whole array, as the optional second argument fromIndex defaults
to 0
author: Domenic Denicola
includes: [testTypedArrays.js]
---*/

testTypedArrays(function (TypedArrayConstructor) {
var ta = new TypedArrayConstructor([1, 2, 3]);

assert(ta.includes(1), 'Expected that the whole array was searched for 1 and it was found');
assert(ta.includes(2), 'Expected that the whole array was searched for 2 and it was found');
assert(ta.includes(3), 'Expected that the whole array was searched for 3 and it was found');
});
@@ -0,0 +1,16 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes returns false if fromIndex is greater or equal to the length of the array
author: Domenic Denicola
includes: [testTypedArrays.js]
---*/

testTypedArrays(function (TypedArrayConstructor) {
var ta = new TypedArrayConstructor([1, 2]);

assert.sameValue(ta.includes(2, 3), false, 'Expected that the array was not searched');
assert.sameValue(ta.includes(2, 2), false, 'Expected that the array was not searched');
});
@@ -0,0 +1,16 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes searches the whole array if the computed index from the given negative fromIndex
argument is less than 0
author: Domenic Denicola
---*/

testTypedArrays(function (TypedArrayConstructor) {
var ta = new TypedArrayConstructor([1, 3]);

assert(ta.includes(1, -4), 'Expected that the whole array was searched');
assert(ta.includes(1, -4), 'Expected that the whole array was searched');
});
@@ -0,0 +1,18 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes should use a negative value as the offset from the end of the array to compute
fromIndex
author: Domenic Denicola
includes: [testTypedArrays.js]
---*/

testTypedArrays(function (TypedArrayConstructor) {
var ta = new TypedArrayConstructor([12, 13]);

assert.sameValue(ta.includes(13, -1), true, 'Expected to find 13');
assert.sameValue(ta.includes(12, -1), false, 'Should not find 12');
assert.sameValue(ta.includes(12, -2), true, 'Should find 12');
});
@@ -0,0 +1,32 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes converts its fromIndex parameter to an integer
author: Domenic Denicola
includes: [testTypedArrays.js]
---*/

testTypedArrays(function (TypedArrayConstructor) {
var ta = new TypedArrayConstructor([1, 2, 3]);

assert.sameValue(ta.includes(1, 3.3), false, 'Expected that the array was not searched');

assert.sameValue(ta.includes(1, -Infinity), true,
'Expected the array to be searched for a fromIndex of -Infinity');
assert.sameValue(ta.includes(3, 2.9), true,
'Expected the fromIndex to be rounded down and thus the element to be found');
assert.sameValue(ta.includes(3, NaN), true, 'Expected a fromIndex of NaN to be treated as 0 for an array');

var numberLike = { valueOf: function () { return 2; } };

assert.sameValue(ta.includes(1, numberLike), false,
'Expected the element not to be found with the given number-like fromIndex');
assert.sameValue(ta.includes(1, '2'), false,
'Expected the element not to be found with the given string fromIndex');
assert.sameValue(ta.includes(3, numberLike), true,
'Expected the element to be found with the given number-like fromIndex');
assert.sameValue(ta.includes(3, '2'), true,
'Expected the element to be found with the given string fromIndex');
});
22 changes: 22 additions & 0 deletions test/built-ins/TypedArray/prototype/includes/function-identity.js
@@ -0,0 +1,22 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes lives on the shared prototype and is not equal to Array.prototype.includes
author: Domenic Denicola
includes: [testTypedArrays.js]
---*/

var taProto = Object.getPrototypeOf(Uint8Array.prototype);

assert.notSameValue(taProto, Object.prototype, 'Expected %TypedArray%.prototype to not be Object.prototype');
assert.sameValue(typeof taProto.includes, 'function', 'Expected %TypedArray%.prototype to have an includes method');
assert.notSameValue(taProto.includes, Array.prototype.includes,
'Expected %TypedArray%.prototype.includes to be different from Array.prototype.includes');

testTypedArrays(function (TypedArrayConstructor) {
assert.sameValue(TypedArrayConstructor.prototype.includes, taProto.includes,
'Expected the method retrieved from the ' + TypedArrayConstructor.name + ' prototype to equal the one from ' +
'the shared %TypedArray% prototype');
});
10 changes: 10 additions & 0 deletions test/built-ins/TypedArray/prototype/includes/function-length.js
@@ -0,0 +1,10 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes should have length 1
author: Domenic Denicola
---*/

assert.sameValue(Uint8Array.prototype.includes.length, 1, 'Expected %TypedArray%.prototype.includes.length to be 1');
11 changes: 11 additions & 0 deletions test/built-ins/TypedArray/prototype/includes/function-name.js
@@ -0,0 +1,11 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes should have name property with value 'includes'
author: Domenic Denicola
---*/

assert.sameValue(Uint8Array.prototype.includes.name, 'includes',
'Expected %TypedArray%.prototype.includes.name to be \'includes\'');
@@ -0,0 +1,21 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes should be writable, non-enumerable, and configurable
author: Domenic Denicola
includes: [dataPropertyAttributesAreCorrect.js]
---*/

var typedArrayProto = Object.getPrototypeOf(Uint8Array.prototype);
var propertyDescriptor = Object.getOwnPropertyDescriptor(typedArrayProto, 'includes');

assert.sameValue(propertyDescriptor.writable, true, 'Expected %TypedArray%.prototype.includes to be writable');
assert.sameValue(propertyDescriptor.enumerable, false, 'Expected %TypedArray%.prototype.includes to be non-enumerable');
assert.sameValue(propertyDescriptor.configurable, true, 'Expected %TypedArray%.prototype.includes to be configurable');

if (!dataPropertyAttributesAreCorrect(typedArrayProto, 'includes', Uint8Array.prototype.includes, true, false, true)) {
$ERROR('Expected %TypedArray%.prototype.includes to be writable, non-enumerable, and configurable (based on ' +
'behavior)');
}
19 changes: 19 additions & 0 deletions test/built-ins/TypedArray/prototype/includes/length-zero.js
@@ -0,0 +1,19 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes should always return false on zero-length typed arrays
author: Domenic Denicola
includes: [testTypedArrays.js]
---*/

testTypedArrays(function (TypedArrayConstructor) {
var ta = new TypedArrayConstructor([]);

assert.sameValue(ta.includes(2), false, 'Expected empty typed array to not contain 2');
assert.sameValue(ta.includes(), false, 'Expected empty typed array to not contain (no argument passed)');
assert.sameValue(ta.includes(undefined), false, 'Expected empty typed array to not contain undefined');
assert.sameValue(ta.includes(NaN), false, 'Expected empty typed array to not contain NaN');
});

@@ -0,0 +1,24 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes should not have its behavior impacted by modifications to the global property
Number
author: Domenic Denicola
includes: [testTypedArrays.js]
---*/

function fakeNumber() {
$ERROR('The overriden version of fakeNumber was called!');
}

var global = (new Function("return this;"))();
global.Number = fakeNumber;
assert.sameValue(Number, fakeNumber, 'Sanity check failed: could not modify the global Number');

testTypedArrays(function (TypedArrayConstructor) {
var ta = new TypedArrayConstructor([]);

assert.sameValue(ta.includes(1), false, 'Expected the empty typed array not to contain anything');
});
@@ -0,0 +1,24 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes should not have its behavior impacted by modifications to the global property
Object
author: Domenic Denicola
includes: [testTypedArrays.js]
---*/

function fakeObject() {
$ERROR('The overriden version of Object was called!');
}

var global = (new Function("return this;"))();
global.Object = fakeObject;
assert.sameValue(Object, fakeObject, 'Sanity check failed: could not modify the global Object');

testTypedArrays(function (TypedArrayConstructor) {
var ta = new TypedArrayConstructor([]);

assert.sameValue(ta.includes(1), false, 'Expected the empty typed array not to contain anything');
});
30 changes: 30 additions & 0 deletions test/built-ins/TypedArray/prototype/includes/same-value-zero.js
@@ -0,0 +1,30 @@
// Copyright (C) 2014 Domenic Denicola. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
%TypedArray%.prototype.includes should use the SameValueZero algorithm to compare
author: Domenic Denicola
includes: [testTypedArrays.js]
---*/

testTypedArrays.floatOnly(function (FloatArrayConstructor) {
assert.sameValue(new FloatArrayConstructor([1, 2, NaN]).includes(NaN), true,
'Expected float array for [1, 2, NaN] to contain NaN');
assert.sameValue(new FloatArrayConstructor([1, 2, -0]).includes(+0), true,
'Expected float array for [1, 2, -0] to contain +0');
assert.sameValue(new FloatArrayConstructor([1, 2, -0]).includes(-0), true,
'Expected float array for [1, 2, -0] to contain -0');
assert.sameValue(new FloatArrayConstructor([1, 2, +0]).includes(-0), true,
'Expected float array for [1, 2, +0] to contain -0');
assert.sameValue(new FloatArrayConstructor([1, 2, +0]).includes(+0), true,
'Expected float array for [1, 2, +0] to contain +0');
assert.sameValue(new FloatArrayConstructor([1, 2, -Infinity]).includes(+Infinity), false,
'Expected float array for [1, 2, -Infinity] to not contain +Infinity');
assert.sameValue(new FloatArrayConstructor([1, 2, -Infinity]).includes(-Infinity), true,
'Expected float array for [1, 2, -Infinity] to contain -Infinity');
assert.sameValue(new FloatArrayConstructor([1, 2, +Infinity]).includes(-Infinity), false,
'Expected float array for [1, 2, +Infinity] to not contain -Infinity');
assert.sameValue(new FloatArrayConstructor([1, 2, +Infinity]).includes(+Infinity), true,
'Expected float array for [1, 2, +Infinity] to contain +Infinity');
});

0 comments on commit 9467eaa

Please sign in to comment.