Add tests for TypedArrays filter

This commit is contained in:
Leonardo Balter 2016-04-27 14:03:44 -04:00 committed by Mike Pennisi
parent 4980fd264e
commit e6f4b20834
32 changed files with 1396 additions and 0 deletions

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.filter
description: Uses internal ArrayLength instead of length property
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
3. Let len be the value of O's [[ArrayLength]] internal slot.
...
includes: [testTypedArray.js]
---*/
var getCalls = 0;
var desc = {
get: function getLen() {
getCalls++;
return 0;
}
};
Object.defineProperty(TypedArray.prototype, "length", desc);
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(4);
var calls = 0;
Object.defineProperty(TA.prototype, "length", desc);
Object.defineProperty(sample, "length", desc);
sample.filter(function() {
calls++;
});
assert.sameValue(getCalls, 0, "ignores length properties");
assert.sameValue(calls, 4, "interactions are not affected by custom length");
});

View File

@ -0,0 +1,45 @@
// 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.filter
description: >
thisArg does not affect callbackfn arguments
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
9. Repeat, while k < len
...
c. Let selected be ToBoolean(? Call(callbackfn, T, « kValue, k, O »)).
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([42, 43, 44]);
var results = [];
var thisArg = ["test262", 0, "ecma262", 0];
sample.filter(function() {
results.push(arguments);
}, thisArg);
assert.sameValue(results.length, 3, "results.length");
assert.sameValue(thisArg.length, 4, "thisArg.length");
assert.sameValue(results[0].length, 3, "results[0].length");
assert.sameValue(results[0][0], 42, "results[0][0] - kValue");
assert.sameValue(results[0][1], 0, "results[0][1] - k");
assert.sameValue(results[0][2], sample, "results[0][2] - this");
assert.sameValue(results[1].length, 3, "results[1].length");
assert.sameValue(results[1][0], 43, "results[1][0] - kValue");
assert.sameValue(results[1][1], 1, "results[1][1] - k");
assert.sameValue(results[1][2], sample, "results[1][2] - this");
assert.sameValue(results[2].length, 3, "results[2].length");
assert.sameValue(results[2][0], 44, "results[2][0] - kValue");
assert.sameValue(results[2][1], 2, "results[2][1] - k");
assert.sameValue(results[2][2], sample, "results[2][2] - this");
});

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.filter
description: >
callbackfn arguments
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
9. Repeat, while k < len
...
c. Let selected be ToBoolean(? Call(callbackfn, T, « kValue, k, O »)).
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([42, 43, 44]);
var results = [];
sample.filter(function() {
results.push(arguments);
});
assert.sameValue(results.length, 3, "results.length");
assert.sameValue(results[0].length, 3, "results[0].length");
assert.sameValue(results[0][0], 42, "results[0][0] - kValue");
assert.sameValue(results[0][1], 0, "results[0][1] - k");
assert.sameValue(results[0][2], sample, "results[0][2] - this");
assert.sameValue(results[1].length, 3, "results[1].length");
assert.sameValue(results[1][0], 43, "results[1][0] - kValue");
assert.sameValue(results[1][1], 1, "results[1][1] - k");
assert.sameValue(results[1][2], sample, "results[1][2] - this");
assert.sameValue(results[2].length, 3, "results[2].length");
assert.sameValue(results[2][0], 44, "results[2][0] - kValue");
assert.sameValue(results[2][1], 2, "results[2][1] - k");
assert.sameValue(results[2][2], sample, "results[2][2] - this");
});

View File

@ -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-%typedarray%.prototype.filter
description: callbackfn is called for each item before TypedArraySpeciesCreate
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
9. Repeat, while k < len
...
c. Let selected be ToBoolean(? Call(callbackfn, T, « kValue, k, O »)).
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var length = 42;
var sample = new TA(length);
var calls = 0;
var before = false;
sample.constructor = {};
Object.defineProperty(sample, "constructor", {
get: function() {
before = calls === length;
}
});
sample.filter(function() {
calls++;
});
assert.sameValue(calls, 42, "callbackfn called for each item");
assert.sameValue(before, true, "all callbackfn called before");
});

View File

@ -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-%typedarray%.prototype.filter
description: callbackfn is called for each item before TypedArraySpeciesCreate
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
9. Repeat, while k < len
...
c. Let selected be ToBoolean(? Call(callbackfn, T, « kValue, k, O »)).
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var length = 42;
var sample = new TA(length);
var calls = 0;
var before = false;
sample.constructor = {};
Object.defineProperty(sample.constructor, Symbol.species, {
get: function() {
before = calls === length;
}
});
sample.filter(function() {
calls++;
});
assert.sameValue(calls, 42, "callbackfn called for each item");
assert.sameValue(before, true, "all callbackfn called before");
});

View File

@ -0,0 +1,34 @@
// 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.filter
description: >
Instance buffer can be detached during loop
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
9. Repeat, while k < len
...
b. Let kValue be ? Get(O, Pk).
c. Let selected be ToBoolean(? Call(callbackfn, T, « kValue, k, O »)).
...
includes: [detachArrayBuffer.js, testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var loops = 0;
var sample = new TA(2);
assert.throws(TypeError, function() {
sample.filter(function() {
if (loops === 1) {
throw new Test262Error("callbackfn called twice");
}
$DETACHBUFFER(sample.buffer);
loops++;
});
});
assert.sameValue(loops, 1);
});

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.filter
description: >
Does not iterate over non-integer properties
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
9. Repeat, while k < len
...
c. Let selected be ToBoolean(? Call(callbackfn, T, « kValue, k, O »)).
...
includes: [testTypedArray.js]
features: [Symbol]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([7, 8]);
var results = [];
sample.foo = 42;
sample[Symbol("1")] = 43;
sample.filter(function() {
results.push(arguments);
});
assert.sameValue(results.length, 2, "results.length");
assert.sameValue(results[0][1], 0, "results[0][1] - k");
assert.sameValue(results[1][1], 1, "results[1][1] - k");
assert.sameValue(results[0][0], 7, "results[0][0] - kValue");
assert.sameValue(results[1][0], 8, "results[1][0] - kValue");
});

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.filter
description: Throws TypeError if callbackfn is not callable
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
4. If IsCallable(callbackfn) is false, throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Symbol]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(4);
assert.throws(TypeError, function() {
sample.filter();
}, "no arg");
assert.throws(TypeError, function() {
sample.filter(undefined);
}, "undefined");
assert.throws(TypeError, function() {
sample.filter(null);
}, "null");
assert.throws(TypeError, function() {
sample.filter(true);
}, "true");
assert.throws(TypeError, function() {
sample.filter(false);
}, "false");
assert.throws(TypeError, function() {
sample.filter({});
}, "{}");
assert.throws(TypeError, function() {
sample.filter([]);
}, "[]");
assert.throws(TypeError, function() {
sample.filter(1);
}, "Number");
assert.throws(TypeError, function() {
sample.filter(Symbol(""));
}, "symbol");
assert.throws(TypeError, function() {
sample.filter("");
}, "string");
});

View File

@ -0,0 +1,26 @@
// 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.filter
description: >
callbackfn is not called on empty instances
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
9. Repeat, while k < len
...
c. Let selected be ToBoolean(? Call(callbackfn, T, « kValue, k, O »)).
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var called = 0;
new TA().filter(function() {
called++;
});
assert.sameValue(called, 0);
});

View File

@ -0,0 +1,22 @@
// 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.filter
description: >
The callbackfn return does not change the instance
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample1 = new TA(3);
sample1[1] = 1;
sample1.filter(function() {
return 42;
});
assert.sameValue(sample1[0], 0, "[0] == 0");
assert.sameValue(sample1[1], 1, "[1] == 1");
assert.sameValue(sample1[2], 0, "[2] == 0");
});

View File

@ -0,0 +1,26 @@
// 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.filter
description: >
Returns abrupt from callbackfn
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
9. Repeat, while k < len
...
c. Let selected be ToBoolean(? Call(callbackfn, T, « kValue, k, O »)).
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(3);
assert.throws(Test262Error, function() {
sample.filter(function() {
throw new Test262Error();
});
});
});

View File

@ -0,0 +1,47 @@
// 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.filter
description: >
Integer indexed values changed during iteration
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
9. Repeat, while k < len
...
c. Let selected be ToBoolean(? Call(callbackfn, T, « kValue, k, O »)).
...
includes: [testTypedArray.js]
features: [Reflect.set]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([42, 43, 44]);
var newVal = 0;
sample.filter(function(val, i) {
if (i > 0) {
assert.sameValue(
sample[i - 1], newVal - 1,
"get the changed value during the loop"
);
assert.sameValue(
Reflect.set(sample, 0, 7),
true,
"re-set a value for sample[0]"
);
}
assert.sameValue(
Reflect.set(sample, i, newVal),
true,
"set value during interaction"
);
newVal++;
});
assert.sameValue(sample[0], 7, "changed values after interaction [0] == 7");
assert.sameValue(sample[1], 1, "changed values after interaction [1] == 1");
assert.sameValue(sample[2], 2, "changed values after interaction [2] == 2");
});

View File

@ -0,0 +1,47 @@
// 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.filter
description: >
callbackfn `this` value
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
5. If thisArg was supplied, let T be thisArg; else let T be undefined.
...
9. Repeat, while k < len
...
c. Let selected be ToBoolean(? Call(callbackfn, T, « kValue, k, O »)).
...
includes: [testTypedArray.js]
---*/
var expected = (function() { return this; })();
var thisArg = {};
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(3);
var results1 = [];
sample.filter(function() {
results1.push(this);
});
assert.sameValue(results1.length, 3, "results1");
assert.sameValue(results1[0], expected, "without thisArg - [0]");
assert.sameValue(results1[1], expected, "without thisArg - [1]");
assert.sameValue(results1[2], expected, "without thisArg - [2]");
var results2 = [];
sample.filter(function() {
results2.push(this);
}, thisArg);
assert.sameValue(results2.length, 3, "results2");
assert.sameValue(results2[0], thisArg, "using thisArg - [0]");
assert.sameValue(results2[1], thisArg, "using thisArg - [1]");
assert.sameValue(results2[2], thisArg, "using thisArg - [2]");
});

View File

@ -0,0 +1,26 @@
// 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.filter
description: >
Return does not share buffer
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
13. Return A.
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42]);
var result;
result = sample.filter(function() { return true; });
assert.notSameValue(result.buffer, sample.buffer);
result = sample.filter(function() { return false; });
assert.notSameValue(result.buffer, sample.buffer);
});

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.filter
description: >
Returns empty if every callbackfn returns boolean false
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
12. For each element e of kept
a. Perform ! Set(A, ! ToString(n), e, true).
b. Increment n by 1.
13. Return A.
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(3);
[
false,
"",
0,
-0,
NaN,
undefined,
null
].forEach(function(val) {
var result = sample.filter(function() {
return val;
});
assert.sameValue(result.length, 0, val);
});
});

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.filter
description: >
Returns full length result if every callbackfn returns boolean false
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
12. For each element e of kept
a. Perform ! Set(A, ! ToString(n), e, true).
b. Increment n by 1.
13. Return A.
includes: [testTypedArray.js, compareArray.js]
features: [Symbol]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42]);
[
true,
1,
"test262",
Symbol("1"),
{},
[],
-1,
Infinity,
-Infinity,
0.1,
-0.1
].forEach(function(val) {
var result = sample.filter(function() { return val; });
assert(compareArray(result, sample), val);
});
});

View File

@ -0,0 +1,42 @@
// 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.filter
description: Return abrupt from SpeciesConstructor's get Constructor
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
1. Assert: Type(O) is Object.
2. Let C be ? Get(O, "constructor").
3. If C is undefined, return defaultConstructor.
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42, 43]);
Object.defineProperty(sample, "constructor", {
get: function() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
sample.filter(function() {
return true;
});
});
});

View File

@ -0,0 +1,63 @@
// 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.filter
description: get inherited constructor on SpeciesConstructor
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
1. Assert: Type(O) is Object.
2. Let C be ? Get(O, "constructor").
3. If C is undefined, return defaultConstructor.
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42, 43]);
var calls = 0;
var result;
Object.defineProperty(TA.prototype, "constructor", {
get: function() {
calls++;
}
});
result = sample.filter(function() {
return true;
});
assert.sameValue(calls, 1, "called custom ctor get accessor once");
assert.sameValue(
Object.getPrototypeOf(result),
Object.getPrototypeOf(sample),
"use defaultCtor on an undefined return - getPrototypeOf check"
);
assert.sameValue(
result.constructor,
undefined,
"used defaultCtor but still checks the inherited .constructor"
);
calls = 6;
result.constructor;
assert.sameValue(
calls,
7,
"result.constructor triggers the inherited accessor property"
);
});

View File

@ -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-%typedarray%.prototype.filter
description: >
Throws if O.constructor returns a non-Object and non-undefined value
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
1. Assert: Type(O) is Object.
2. Let C be ? Get(O, "constructor").
3. If C is undefined, return defaultConstructor.
4. If Type(C) is not Object, throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Symbol]
---*/
var callbackfn = function() { return true; };
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42, 43]);
sample.constructor = 42;
assert.throws(TypeError, function() {
sample.filter(callbackfn);
}, "42");
sample.constructor = "1";
assert.throws(TypeError, function() {
sample.filter(callbackfn);
}, "string");
sample.constructor = null;
assert.throws(TypeError, function() {
sample.filter(callbackfn);
}, "null");
sample.constructor = NaN;
assert.throws(TypeError, function() {
sample.filter(callbackfn);
}, "NaN");
sample.constructor = false;
assert.throws(TypeError, function() {
sample.filter(callbackfn);
}, "false");
sample.constructor = Symbol("1");
assert.throws(TypeError, function() {
sample.filter(callbackfn);
}, "symbol");
});

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-%typedarray%.prototype.filter
description: get constructor on SpeciesConstructor
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
1. Assert: Type(O) is Object.
2. Let C be ? Get(O, "constructor").
3. If C is undefined, return defaultConstructor.
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42, 43]);
var calls = 0;
var result;
Object.defineProperty(sample, "constructor", {
get: function() {
calls++;
}
});
result = sample.filter(function() { return true; });
assert.sameValue(calls, 1, "called custom ctor get accessor once");
assert.sameValue(
Object.getPrototypeOf(result),
Object.getPrototypeOf(sample),
"use defaultCtor on an undefined return - getPrototypeOf check"
);
assert.sameValue(
result.constructor,
TA,
"use defaultCtor on an undefined return - .constructor check"
);
});

View File

@ -0,0 +1,45 @@
// 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.filter
description: >
Returns abrupt from get @@species on found constructor
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
1. Assert: Type(O) is Object.
2. Let C be ? Get(O, "constructor").
...
5. Let S be ? Get(C, @@species).
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(2);
sample.constructor = {};
Object.defineProperty(sample.constructor, Symbol.species, {
get: function() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
sample.filter(function() { return true; });
});
});

View File

@ -0,0 +1,59 @@
// 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.filter
description: >
Verify arguments on custom @@species construct call
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
4. Return ? TypedArrayCreate(constructor, argumentList).
7.3.20 SpeciesConstructor ( O, defaultConstructor )
...
5. Let S be ? Get(C, @@species).
...
7. If IsConstructor(S) is true, return S.
...
22.2.4.6 TypedArrayCreate ( constructor, argumentList )
1. Let newTypedArray be ? Construct(constructor, argumentList).
2. Perform ? ValidateTypedArray(newTypedArray).
3. If argumentList is a List of a single Number, then
...
4. Return newTypedArray.
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 42, 42]);
var result, ctorThis;
sample.constructor = {};
sample.constructor[Symbol.species] = function(count) {
result = arguments;
ctorThis = this;
return new TA(count);
};
sample.filter(function(v) { return v === 42; });
assert.sameValue(result.length, 1, "called with 1 argument");
assert.sameValue(result[0], 2, "[0] is the new captured length");
assert(
ctorThis instanceof sample.constructor[Symbol.species],
"`this` value in the @@species fn is an instance of the function itself"
);
});

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.filter
description: >
Throws a TypeError if new typedArray's length < captured
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
4. Return ? TypedArrayCreate(constructor, argumentList).
22.2.4.6 TypedArrayCreate ( constructor, argumentList )
...
3. If argumentList is a List of a single Number, then
a. If the value of newTypedArray's [[ArrayLength]] internal slot <
argumentList[0], throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(2);
sample.constructor = {};
sample.constructor[Symbol.species] = function() {
return new TA();
};
assert.throws(TypeError, function() {
sample.filter(function() { return true; });
});
});

View File

@ -0,0 +1,46 @@
// 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.filter
description: >
Does not throw a TypeError if new typedArray's length >= captured
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
4. Return ? TypedArrayCreate(constructor, argumentList).
22.2.4.6 TypedArrayCreate ( constructor, argumentList )
...
3. If argumentList is a List of a single Number, then
a. If the value of newTypedArray's [[ArrayLength]] internal slot <
argumentList[0], throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(2);
var customCount, result;
sample.constructor = {};
sample.constructor[Symbol.species] = function() {
return new TA(customCount);
};
customCount = 2;
result = sample.filter(function() { return true; });
assert.sameValue(result.length, customCount, "length == count");
customCount = 5;
result = sample.filter(function() { return true; });
assert.sameValue(result.length, customCount, "length > count");
});

View File

@ -0,0 +1,54 @@
// 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.filter
description: >
Custom @@species constructor may return a different TypedArray
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
4. Return ? TypedArrayCreate(constructor, argumentList).
7.3.20 SpeciesConstructor ( O, defaultConstructor )
...
5. Let S be ? Get(C, @@species).
...
7. If IsConstructor(S) is true, return S.
...
22.2.4.6 TypedArrayCreate ( constructor, argumentList )
1. Let newTypedArray be ? Construct(constructor, argumentList).
2. Perform ? ValidateTypedArray(newTypedArray).
3. If argumentList is a List of a single Number, then
...
4. Return newTypedArray.
includes: [testTypedArray.js, compareArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40]);
var otherTA = TA === Int8Array ? Int16Array : Int8Array;
var other = new otherTA([1, 0, 1]);
var result;
sample.constructor = {};
sample.constructor[Symbol.species] = function() {
return other;
};
result = sample.filter(function() {});
assert.sameValue(result, other, "returned another typedarray");
assert(compareArray(result, [1, 0, 1]), "the returned object is preserved");
});

View File

@ -0,0 +1,46 @@
// 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.filter
description: >
Custom @@species constructor throws if it does not return a compatible object
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
4. Return ? TypedArrayCreate(constructor, argumentList).
7.3.20 SpeciesConstructor ( O, defaultConstructor )
...
5. Let S be ? Get(C, @@species).
...
7. If IsConstructor(S) is true, return S.
...
22.2.4.6 TypedArrayCreate ( constructor, argumentList )
1. Let newTypedArray be ? Construct(constructor, argumentList).
2. Perform ? ValidateTypedArray(newTypedArray).
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(2);
sample.constructor = {};
sample.constructor[Symbol.species] = Array;
assert.throws(TypeError, function() {
sample.filter(function() {});
});
});

View File

@ -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-%typedarray%.prototype.filter
description: >
Use custom @@species constructor if available
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
4. Return ? TypedArrayCreate(constructor, argumentList).
7.3.20 SpeciesConstructor ( O, defaultConstructor )
...
5. Let S be ? Get(C, @@species).
...
7. If IsConstructor(S) is true, return S.
...
22.2.4.6 TypedArrayCreate ( constructor, argumentList )
1. Let newTypedArray be ? Construct(constructor, argumentList).
2. Perform ? ValidateTypedArray(newTypedArray).
3. If argumentList is a List of a single Number, then
...
4. Return newTypedArray.
includes: [testTypedArray.js, compareArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([40, 41, 42]);
var calls = 0;
var other, result;
sample.constructor = {};
sample.constructor[Symbol.species] = function(captured) {
calls++;
other = new TA(captured);
return other;
};
result = sample.filter(function() { return true; });
assert.sameValue(calls, 1, "ctor called once");
assert.sameValue(result, other, "return is instance of custom constructor");
assert(compareArray(result, [40, 41, 42]), "values are set on the new obj");
});

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.filter
description: >
Throws if returned @@species is not a constructor, null or undefined.
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
...
5. Let S be ? Get(C, @@species).
6. If S is either undefined or null, return defaultConstructor.
7. If IsConstructor(S) is true, return S.
8. Throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(2);
sample.constructor = {};
sample.constructor[Symbol.species] = 0;
assert.throws(TypeError, function() {
sample.filter(function() {});
}, "0");
sample.constructor[Symbol.species] = "string";
assert.throws(TypeError, function() {
sample.filter(function() {});
}, "string");
sample.constructor[Symbol.species] = {};
assert.throws(TypeError, function() {
sample.filter(function() {});
}, "{}");
sample.constructor[Symbol.species] = NaN;
assert.throws(TypeError, function() {
sample.filter(function() {});
}, "NaN");
sample.constructor[Symbol.species] = false;
assert.throws(TypeError, function() {
sample.filter(function() {});
}, "false");
sample.constructor[Symbol.species] = true;
assert.throws(TypeError, function() {
sample.filter(function() {});
}, "true");
});

View File

@ -0,0 +1,54 @@
// 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.filter
description: >
Use defaultConstructor if @@species is either undefined or null
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
...
5. Let S be ? Get(C, @@species).
6. If S is either undefined or null, return defaultConstructor.
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(2);
var result;
sample.constructor = {};
result = sample.filter(function() {});
assert.sameValue(
Object.getPrototypeOf(result),
Object.getPrototypeOf(sample),
"undefined @@species - prototype check "
);
assert.sameValue(result.constructor, TA, "undefined @@species - ctor check");
sample.constructor[Symbol.species] = null;
result = sample.filter(function() {});
assert.sameValue(
Object.getPrototypeOf(result),
Object.getPrototypeOf(sample),
"null @@species - prototype check "
);
assert.sameValue(result.constructor, TA, "null @@species - ctor check");
});

View File

@ -0,0 +1,46 @@
// 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.filter
description: >
get @@species from found constructor
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
10. Let A be ? TypedArraySpeciesCreate(O, « captured »).
...
22.2.4.7 TypedArraySpeciesCreate ( exemplar, argumentList )
...
3. Let constructor be ? SpeciesConstructor(exemplar, defaultConstructor).
...
7.3.20 SpeciesConstructor ( O, defaultConstructor )
1. Assert: Type(O) is Object.
2. Let C be ? Get(O, "constructor").
...
5. Let S be ? Get(C, @@species).
...
includes: [testTypedArray.js]
features: [Symbol.species]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(2);
var calls = 0;
sample.constructor = {};
Object.defineProperty(sample.constructor, Symbol.species, {
get: function() {
calls++;
}
});
sample.filter(function() {});
assert.sameValue(calls, 1);
});

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-%typedarray%.prototype.filter
description: >
Integer indexed values are not cached before interaction
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
9. Repeat, while k < len
...
c. Let selected be ToBoolean(? Call(callbackfn, T, « kValue, k, O »)).
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([42, 43, 44]);
sample.filter(function(v, i) {
if (i < sample.length - 1) {
sample[i+1] = 42;
}
assert.sameValue(
v, 42, "method does not cache values before callbackfn calls"
);
});
});

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-%typedarray%.prototype.filter
description: >
Returned instance with filtered values set on it
info: >
22.2.3.9 %TypedArray%.prototype.filter ( callbackfn [ , thisArg ] )
...
12. For each element e of kept
a. Perform ! Set(A, ! ToString(n), e, true).
b. Increment n by 1.
13. Return A.
includes: [testTypedArray.js, compareArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([41, 1, 42, 7]);
var result;
result = sample.filter(function() { return true; });
assert(compareArray(result, [41, 1, 42, 7]), "values are set #1");
result = sample.filter(function(v) {
return v > 40;
});
assert(compareArray(result, [41, 42]), "values are set #2");
});