Add tests for TypedArrays findIndex

This commit is contained in:
Leonardo Balter 2016-04-14 15:24:17 -04:00 committed by Mike Pennisi
parent 9aa4dced8d
commit bc0a40c51c
11 changed files with 612 additions and 0 deletions

View File

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

View File

@ -0,0 +1,67 @@
// 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.findindex
es6id: 22.2.3.11
description: >
Change values during predicate call
info: >
22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )
%TypedArray%.prototype.findIndex is a distinct function that implements the
same algorithm as Array.prototype.findIndex as defined in 22.1.3.9 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
...
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
6. Repeat, while k < len
...
c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
...
includes: [compareArray.js, testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var arr = [10, 20, 30];
var sample;
var result;
sample = new TA(3);
sample.findIndex(function(val, i) {
sample[i] = arr[i];
assert.sameValue(val, 0, "value is not mapped to instance");
});
assert(compareArray(sample, arr), "values set during each predicate call");
sample = new TA(arr);
result = sample.findIndex(function(val, i) {
if ( i === 0 ) {
sample[2] = 7;
}
return val === 7;
});
assert.sameValue(result, 2, "value found");
sample = new TA(arr);
result = sample.findIndex(function(val, i) {
if ( i === 0 ) {
sample[2] = 7;
}
return val === 30;
});
assert.sameValue(result, -1, "value not found");
sample = new TA(arr);
result = sample.findIndex(function(val, i) {
if ( i > 0 ) {
sample[0] = 7;
}
return val === 7;
});
assert.sameValue(result, -1, "value not found - changed after call");
});

View File

@ -0,0 +1,60 @@
// 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.findindex
es6id: 22.2.3.11
description: >
Predicate called as F.call( thisArg, kValue, k, O ) for each array entry.
info: >
22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )
%TypedArray%.prototype.findIndex is a distinct function that implements the
same algorithm as Array.prototype.findIndex as defined in 22.1.3.9 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
...
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
4. If thisArg was supplied, let T be thisArg; else let T be undefined.
5. Let k be 0.
6. Repeat, while k < len
...
c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([39, 2, 62]);
var results = [];
var result;
sample.foo = "bar"; // Ignores non integer index properties
sample.findIndex(function() {
results.push(arguments);
});
assert.sameValue(results.length, 3, "predicate is called for each index");
result = results[0];
assert.sameValue(result[0], 39, "results[0][0] === 39, value");
assert.sameValue(result[1], 0, "results[0][1] === 0, index");
assert.sameValue(result[2], sample, "results[0][2] === sample, instance");
assert.sameValue(result.length, 3, "results[0].length === 3, arguments");
result = results[1];
assert.sameValue(result[0], 2, "results[1][0] === 2, value");
assert.sameValue(result[1], 1, "results[1][1] === 1, index");
assert.sameValue(result[2], sample, "results[1][2] === sample, instance");
assert.sameValue(result.length, 3, "results[1].length === 3, arguments");
result = results[2];
assert.sameValue(result[0], 62, "results[2][0] === 62, value");
assert.sameValue(result[1], 2, "results[2][1] === 2, index");
assert.sameValue(result[2], sample, "results[2][2] === sample, instance");
assert.sameValue(result.length, 3, "results[2].length === 3, arguments");
});

View File

@ -0,0 +1,57 @@
// 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.findindex
es6id: 22.2.3.11
description: >
Verify predicate this on non-strict mode
info: >
22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )
%TypedArray%.prototype.findIndex is a distinct function that implements the
same algorithm as Array.prototype.findIndex as defined in 22.1.3.9 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
...
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
4. If thisArg was supplied, let T be thisArg; else let T be undefined.
5. Let k be 0.
6. Repeat, while k < len
...
c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
...
flags: [noStrict]
includes: [testTypedArray.js]
---*/
var T = this;
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(1);
var result;
sample.findIndex(function() {
result = this;
});
assert.sameValue(result, T, "without thisArg, predicate this is the global");
result = null;
sample.findIndex(function() {
result = this;
}, undefined);
assert.sameValue(result, T, "predicate this is the global when thisArg is undefined");
var o = {};
result = null;
sample.findIndex(function() {
result = this;
}, o);
assert.sameValue(result, o, "thisArg becomes the predicate this");
});

View File

@ -0,0 +1,51 @@
// 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.findindex
es6id: 22.2.3.11
description: >
Predicate thisArg as F.call( thisArg, kValue, k, O ) for each array entry.
info: >
22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )
%TypedArray%.prototype.findIndex is a distinct function that implements the
same algorithm as Array.prototype.findIndex as defined in 22.1.3.9 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
...
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
4. If thisArg was supplied, let T be thisArg; else let T be undefined.
5. Let k be 0.
6. Repeat, while k < len
...
c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
...
flags: [onlyStrict]
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(1);
var result;
sample.findIndex(function() {
result = this;
});
assert.sameValue(
result,
undefined,
"without thisArg, predicate this is undefined"
);
var o = {};
sample.findIndex(function() {
result = this;
}, o);
assert.sameValue(result, o, "thisArg becomes the predicate this");
});

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.findindex
es6id: 22.2.3.11
description: >
Throws a TypeError exception if predicate is not callable.
info: >
22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )
%TypedArray%.prototype.findIndex is a distinct function that implements the
same algorithm as Array.prototype.findIndex as defined in 22.1.3.9 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
...
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
3. If IsCallable(predicate) is false, throw a TypeError exception.
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA();
assert.throws(TypeError, function() {
sample.findIndex({});
}, "{}");
assert.throws(TypeError, function() {
sample.findIndex(null);
}, "null");
assert.throws(TypeError, function() {
sample.findIndex(undefined);
}, "undefined");
assert.throws(TypeError, function() {
sample.findIndex(false);
}, "false");
assert.throws(TypeError, function() {
sample.findIndex(true);
}, "true");
assert.throws(TypeError, function() {
sample.findIndex(1);
}, "1");
assert.throws(TypeError, function() {
sample.findIndex("");
}, "string");
assert.throws(TypeError, function() {
sample.findIndex([]);
}, "[]");
assert.throws(TypeError, function() {
sample.findIndex(/./);
}, "/./");
});

View File

@ -0,0 +1,51 @@
// 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.findindex
es6id: 22.2.3.11
description: >
Predicate may detach the buffer
info: >
22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )
%TypedArray%.prototype.findIndex is a distinct function that implements the
same algorithm as Array.prototype.findIndex as defined in 22.1.3.9 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
...
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
6. Repeat, while k < len
...
b. Let kValue be ? Get(O, Pk).
c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
...
9.4.5.8 IntegerIndexedElementGet ( O, index )
...
3. Let buffer be the value of O's [[ViewedArrayBuffer]] internal slot.
4. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
...
includes: [testTypedArray.js, detachArrayBuffer.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(2);
var loops = 0;
var completion = false;
assert.throws(TypeError, function() {
sample.findIndex(function() {
loops++;
$DETACHBUFFER(sample.buffer);
completion = true;
});
}, "throws a TypeError getting a value from the detached buffer");
assert.sameValue(loops, 1, "predicated is called once");
assert(completion, "abrupt completion does not come from DETACHBUFFER");
});

View File

@ -0,0 +1,48 @@
// 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.findindex
es6id: 22.2.3.11
description: >
Predicate is not called on an empty instance
info: >
22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )
%TypedArray%.prototype.findIndex is a distinct function that implements the
same algorithm as Array.prototype.findIndex as defined in 22.1.3.9 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
...
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
6. Repeat, while k < len
...
c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
...
7. Return -1.
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA();
var called = false;
var predicate = function() {
called = true;
return true;
};
var result = sample.findIndex(predicate);
assert.sameValue(
called, false,
"does not call predicate"
);
assert.sameValue(
result, -1,
"returns -1 on an empty instance"
);
});

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.findindex
es6id: 22.2.3.11
description: >
Return abrupt from predicate call.
info: >
22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )
%TypedArray%.prototype.findIndex is a distinct function that implements the
same algorithm as Array.prototype.findIndex as defined in 22.1.3.9 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
...
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
5. Let k be 0.
6. Repeat, while k < len
...
c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
...
includes: [testTypedArray.js]
---*/
var predicate = function() {
throw new Test262Error();
};
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(1);
assert.throws(Test262Error, function() {
sample.findIndex(predicate);
});
});

View File

@ -0,0 +1,66 @@
// 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.findindex
es6id: 22.2.3.11
description: >
Return index if predicate return a boolean true value.
info: >
22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )
%TypedArray%.prototype.findIndex is a distinct function that implements the
same algorithm as Array.prototype.findIndex as defined in 22.1.3.9 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
...
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
5. Let k be 0.
6. Repeat, while k < len
...
c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
d. If testResult is true, return k.
...
features: [Symbol]
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([39, 3, 9]);
var called = 0;
var result = sample.findIndex(function() {
called++;
return true;
});
assert.sameValue(result, 0, "returned true on sample[0]");
assert.sameValue(called, 1, "predicate was called once");
called = 0;
result = sample.findIndex(function(val) {
called++;
return val === 9;
});
assert.sameValue(called, 3, "predicate was called three times");
assert.sameValue(result, 2, "returned true on sample[3]");
result = sample.findIndex(function() { return "string"; });
assert.sameValue(result, 0, "ToBoolean(string)");
result = sample.findIndex(function() { return {}; });
assert.sameValue(result, 0, "ToBoolean(object)");
result = sample.findIndex(function() { return Symbol(""); });
assert.sameValue(result, 0, "ToBoolean(symbol)");
result = sample.findIndex(function() { return 1; });
assert.sameValue(result, 0, "ToBoolean(number)");
result = sample.findIndex(function() { return -1; });
assert.sameValue(result, 0, "ToBoolean(negative number)");
});

View File

@ -0,0 +1,58 @@
// 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.findindex
es6id: 22.2.3.11
description: >
Return -1 if predicate always returns a boolean false value.
info: >
22.2.3.11 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )
%TypedArray%.prototype.findIndex is a distinct function that implements the
same algorithm as Array.prototype.findIndex as defined in 22.1.3.9 except that
the this object's [[ArrayLength]] internal slot is accessed in place of
performing a [[Get]] of "length".
...
22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )
...
6. Repeat, while k < len
...
c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
...
7. Return -1.
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([1, 2, 3]);
var called = 0;
var result = sample.findIndex(function(val) {
called++;
return false;
});
assert.sameValue(called, 3, "predicate was called three times");
assert.sameValue(result, -1, "result is -1 when predicate returns are false");
result = sample.findIndex(function(val) { return ""; });
assert.sameValue(result, -1, "ToBoolean(string)");
result = sample.findIndex(function(val) { return undefined; });
assert.sameValue(result, -1, "ToBoolean(undefined)");
result = sample.findIndex(function(val) { return null; });
assert.sameValue(result, -1, "ToBoolean(null)");
result = sample.findIndex(function(val) { return 0; });
assert.sameValue(result, -1, "ToBoolean(0)");
result = sample.findIndex(function(val) { return -0; });
assert.sameValue(result, -1, "ToBoolean(-0)");
result = sample.findIndex(function(val) { return NaN; });
assert.sameValue(result, -1, "ToBoolean(NaN)");
});