Add tests for %TypedArray%.prototype.includes

This commit is contained in:
Leonardo Balter 2016-05-17 16:16:53 -04:00 committed by Mike Pennisi
parent d3effa125f
commit 56b988883e
11 changed files with 441 additions and 0 deletions

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.
/*---
esid: sec-%typedarray%.prototype.includes
description: Return false if fromIndex >= ArrayLength
info: >
22.2.3.13 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )
%TypedArray%.prototype.includes is a distinct function that implements the
same algorithm as Array.prototype.includes as defined in 22.1.3.11 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] )
...
4. Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step
produces the value 0.)
5. If n 0, then
a. Let k be n.
...
7. Repeat, while k < len
...
8. Return false.
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample;
sample = new TA(42);
assert.sameValue(sample.includes(0, 42), false);
assert.sameValue(sample.includes(0, 43), false);
});

View File

@ -0,0 +1,43 @@
// 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-%typedarray%.prototype.includes
description: handle Infinity values for fromIndex
info: >
22.2.3.13 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )
%TypedArray%.prototype.includes is a distinct function that implements the
same algorithm as Array.prototype.includes as defined in 22.1.3.11 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] )
...
4. Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step
produces the value 0.)
5. If n 0, then
a. Let k be n.
6. Else n < 0,
a. Let k be len + n.
b. If k < 0, let k be 0.
7. Repeat, while k < len
...
8. Return false.
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([42, 43, 43, 41]);
assert.sameValue(
sample.includes(43, Infinity),
false,
"includes(43, Infinity)"
);
assert.sameValue(
sample.includes(43, -Infinity),
true,
"includes(43, -Infinity)");
});

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-%typedarray%.prototype.includes
description: -0 fromIndex becomes 0
info: >
22.2.3.13 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )
%TypedArray%.prototype.includes is a distinct function that implements the
same algorithm as Array.prototype.includes as defined in 22.1.3.11 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] )
...
5. If n 0, then
a. Let k be n.
...
7. Repeat, while k < len
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample;
sample = new TA([42, 43]);
assert.sameValue(sample.includes(42, -0), true, "-0 [0]");
assert.sameValue(sample.includes(43, -0), true, "-0 [1]");
assert.sameValue(sample.includes(44, -0), false, "-0 [2]");
});

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-%typedarray%.prototype.includes
description: Get "length" uses internal ArrayLength
info: >
22.2.3.13 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )
%TypedArray%.prototype.includes is a distinct function that implements the
same algorithm as Array.prototype.includes as defined in 22.1.3.11 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] )
...
2. Let len be ? ToLength(? Get(O, "length")).
...
includes: [testTypedArray.js]
---*/
Object.defineProperty(TypedArray.prototype, "length", {value: 0});
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([7]);
Object.defineProperty(TA.prototype, "length", {value: 0});
Object.defineProperty(sample, "length", {value: 0});
assert.sameValue(sample.includes(7), true);
});

View File

@ -0,0 +1,38 @@
// 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-%typedarray%.prototype.includes
description: Returns false if length is 0
info: >
22.2.3.13 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )
%TypedArray%.prototype.includes is a distinct function that implements the
same algorithm as Array.prototype.includes as defined in 22.1.3.11 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] )
...
2. Let len be ? ToLength(? Get(O, "length")).
3. If len is 0, return false.
...
includes: [testTypedArray.js]
---*/
var fromIndex = {
valueOf: function() {
throw new Test262Error();
}
};
testWithTypedArrayConstructors(function(TA) {
var sample = new TA();
assert.sameValue(sample.includes(0), false, "returns false");
assert.sameValue(sample.includes(), false, "returns false - no arg");
assert.sameValue(
sample.includes(0, fromIndex), false,
"length is checked before ToInteger(fromIndex)"
);
});

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-%typedarray%.prototype.includes
description: Return abrupt from ToInteger(fromIndex) - using symbol
info: >
22.2.3.13 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )
%TypedArray%.prototype.includes is a distinct function that implements the
same algorithm as Array.prototype.includes as defined in 22.1.3.11 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] )
...
4. Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step
produces the value 0.)
...
includes: [testTypedArray.js]
features: [Symbol]
---*/
var fromIndex = Symbol("1");
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([7]);
assert.throws(TypeError, function() {
sample.includes(7, fromIndex);
});
});

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-%typedarray%.prototype.includes
description: Return abrupt from ToInteger(fromIndex)
info: >
22.2.3.13 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )
%TypedArray%.prototype.includes is a distinct function that implements the
same algorithm as Array.prototype.includes as defined in 22.1.3.11 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] )
...
4. Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step
produces the value 0.)
...
includes: [testTypedArray.js]
---*/
var fromIndex = {
valueOf: function() {
throw new Test262Error();
}
};
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([7]);
assert.throws(Test262Error, function() {
sample.includes(7, fromIndex);
});
});

View File

@ -0,0 +1,43 @@
// 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-%typedarray%.prototype.includes
description: search element is compared using SameValueZero
info: >
22.2.3.13 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )
%TypedArray%.prototype.includes is a distinct function that implements the
same algorithm as Array.prototype.includes as defined in 22.1.3.11 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] )
...
7. Repeat, while k < len
a. Let elementK be the result of ? Get(O, ! ToString(k)).
b. If SameValueZero(searchElement, elementK) is true, return true.
c. Increase k by 1.
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([42, 0, 1, undefined]);
assert.sameValue(sample.includes(), false, "no arg");
assert.sameValue(sample.includes(undefined), false, "undefined");
assert.sameValue(sample.includes("42"), false, "'42'");
assert.sameValue(sample.includes([42]), false, "[42]");
assert.sameValue(sample.includes(42.0), true, "42.0");
assert.sameValue(sample.includes(-0), true, "-0");
assert.sameValue(sample.includes(true), false, "true");
assert.sameValue(sample.includes(false), false, "false");
assert.sameValue(sample.includes(null), false, "null");
assert.sameValue(sample.includes(""), false, "empty string");
});
testWithTypedArrayConstructors(function(FloatArray) {
var sample = new FloatArray([42, 0, 1, undefined, NaN]);
assert.sameValue(sample.includes(NaN), true, "NaN");
}, [Float32Array, Float64Array]);

View File

@ -0,0 +1,43 @@
// 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-%typedarray%.prototype.includes
description: returns true for found index
info: >
22.2.3.13 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )
%TypedArray%.prototype.includes is a distinct function that implements the
same algorithm as Array.prototype.includes as defined in 22.1.3.11 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] )
...
5. If n 0, then
a. Let k be n.
6. Else n < 0,
a. Let k be len + n.
b. If k < 0, let k be 0.
7. Repeat, while k < len
a. Let elementK be the result of ? Get(O, ! ToString(k)).
b. If SameValueZero(searchElement, elementK) is true, return true.
c. Increase k by 1.
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([42, 43, 42, 41]);
assert.sameValue(sample.includes(42), true, "includes(42)");
assert.sameValue(sample.includes(43), true, "includes(43)");
assert.sameValue(sample.includes(43, 1), true, "includes(43, 1)");
assert.sameValue(sample.includes(42, 1), true, "includes(42, 1)");
assert.sameValue(sample.includes(42, 2), true, "includes(42, 2)");
assert.sameValue(sample.includes(42, -4), true, "includes(42, -4)");
assert.sameValue(sample.includes(42, -3), true, "includes(42, -3)");
assert.sameValue(sample.includes(42, -2), true, "includes(42, -2)");
assert.sameValue(sample.includes(42, -5), true, "includes(42, -5)");
});

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.
/*---
esid: sec-%typedarray%.prototype.includes
description: returns false if the element is not found
info: >
22.2.3.13 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )
%TypedArray%.prototype.includes is a distinct function that implements the
same algorithm as Array.prototype.includes as defined in 22.1.3.11 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] )
...
5. If n 0, then
a. Let k be n.
6. Else n < 0,
a. Let k be len + n.
b. If k < 0, let k be 0.
7. Repeat, while k < len
a. Let elementK be the result of ? Get(O, ! ToString(k)).
b. If SameValueZero(searchElement, elementK) is true, return true.
c. Increase k by 1.
8. Return false.
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample;
sample = new TA([42, 43, 42, 41]);
assert.sameValue(sample.includes(44), false, "includes(44)");
assert.sameValue(sample.includes(43, 2), false, "includes(43, 2)");
assert.sameValue(sample.includes(42, 3), false, "includes(42, 3)");
assert.sameValue(sample.includes(44, -4), false, "includes(44, -4)");
assert.sameValue(sample.includes(44, -5), false, "includes(44, -5)");
assert.sameValue(sample.includes(42, -1), false, "includes(42, -1)");
});

View File

@ -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-%typedarray%.prototype.includes
description: get the integer value from fromIndex
info: >
22.2.3.13 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )
%TypedArray%.prototype.includes is a distinct function that implements the
same algorithm as Array.prototype.includes as defined in 22.1.3.11 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
22.1.3.11 Array.prototype.includes ( searchElement [ , fromIndex ] )
...
4. Let n be ? ToInteger(fromIndex). (If fromIndex is undefined, this step
produces the value 0.)
5. If n 0, then
a. Let k be n.
...
7. Repeat, while k < len
a. Let elementK be the result of ? Get(O, ! ToString(k)).
b. If SameValueZero(searchElement, elementK) is true, return true.
c. Increase k by 1.
8. Return false.
includes: [testTypedArray.js]
---*/
var obj = {
valueOf: function() {
return 1;
}
};
testWithTypedArrayConstructors(function(TA) {
var sample;
sample = new TA([42, 43]);
assert.sameValue(sample.includes(42, "1"), false, "string [0]");
assert.sameValue(sample.includes(43, "1"), true, "string [1]");
assert.sameValue(sample.includes(42, true), false, "true [0]");
assert.sameValue(sample.includes(43, true), true, "true [1]");
assert.sameValue(sample.includes(42, false), true, "false [0]");
assert.sameValue(sample.includes(43, false), true, "false [1]");
assert.sameValue(sample.includes(42, NaN), true, "NaN [0]");
assert.sameValue(sample.includes(43, NaN), true, "NaN [1]");
assert.sameValue(sample.includes(42, null), true, "null [0]");
assert.sameValue(sample.includes(43, null), true, "null [1]");
assert.sameValue(sample.includes(42, undefined), true, "undefined [0]");
assert.sameValue(sample.includes(43, undefined), true, "undefined [1]");
assert.sameValue(sample.includes(42, null), true, "null [0]");
assert.sameValue(sample.includes(43, null), true, "null [1]");
assert.sameValue(sample.includes(42, obj), false, "object [0]");
assert.sameValue(sample.includes(43, obj), true, "object [1]");
});