From e6f4b2083439dc1b1917a95be30400de2639250d Mon Sep 17 00:00:00 2001 From: Leonardo Balter Date: Wed, 27 Apr 2016 14:03:44 -0400 Subject: [PATCH] Add tests for TypedArrays filter --- .../prototype/filter/arraylength-internal.js | 38 +++++++++++ .../callbackfn-arguments-with-thisarg.js | 45 +++++++++++++ .../callbackfn-arguments-without-thisarg.js | 43 ++++++++++++ .../filter/callbackfn-called-before-ctor.js | 39 +++++++++++ .../callbackfn-called-before-species.js | 39 +++++++++++ .../filter/callbackfn-detachbuffer.js | 34 ++++++++++ ...allbackfn-no-iteration-over-non-integer.js | 38 +++++++++++ .../filter/callbackfn-not-callable-throws.js | 58 ++++++++++++++++ .../filter/callbackfn-not-called-on-empty.js | 26 ++++++++ ...lbackfn-return-does-not-change-instance.js | 22 +++++++ .../filter/callbackfn-returns-abrupt.js | 26 ++++++++ .../callbackfn-set-value-during-iteration.js | 47 +++++++++++++ .../prototype/filter/callbackfn-this.js | 47 +++++++++++++ .../filter/result-does-not-share-buffer.js | 26 ++++++++ .../result-empty-callbackfn-returns-false.js | 35 ++++++++++ .../result-full-callbackfn-returns-true.js | 38 +++++++++++ .../filter/speciesctor-get-ctor-abrupt.js | 42 ++++++++++++ .../filter/speciesctor-get-ctor-inherited.js | 63 ++++++++++++++++++ .../speciesctor-get-ctor-returns-throws.js | 65 ++++++++++++++++++ .../prototype/filter/speciesctor-get-ctor.js | 53 +++++++++++++++ .../filter/speciesctor-get-species-abrupt.js | 45 +++++++++++++ ...ctor-get-species-custom-ctor-invocation.js | 59 +++++++++++++++++ ...r-get-species-custom-ctor-length-throws.js | 41 ++++++++++++ ...ciesctor-get-species-custom-ctor-length.js | 46 +++++++++++++ ...es-custom-ctor-returns-another-instance.js | 54 +++++++++++++++ ...ciesctor-get-species-custom-ctor-throws.js | 46 +++++++++++++ .../speciesctor-get-species-custom-ctor.js | 56 ++++++++++++++++ .../speciesctor-get-species-returns-throws.js | 66 +++++++++++++++++++ ...peciesctor-get-species-use-default-ctor.js | 54 +++++++++++++++ .../filter/speciesctor-get-species.js | 46 +++++++++++++ .../prototype/filter/values-are-not-cached.js | 30 +++++++++ .../prototype/filter/values-are-set.js | 29 ++++++++ 32 files changed, 1396 insertions(+) create mode 100644 test/built-ins/TypedArray/prototype/filter/arraylength-internal.js create mode 100644 test/built-ins/TypedArray/prototype/filter/callbackfn-arguments-with-thisarg.js create mode 100644 test/built-ins/TypedArray/prototype/filter/callbackfn-arguments-without-thisarg.js create mode 100644 test/built-ins/TypedArray/prototype/filter/callbackfn-called-before-ctor.js create mode 100644 test/built-ins/TypedArray/prototype/filter/callbackfn-called-before-species.js create mode 100644 test/built-ins/TypedArray/prototype/filter/callbackfn-detachbuffer.js create mode 100644 test/built-ins/TypedArray/prototype/filter/callbackfn-no-iteration-over-non-integer.js create mode 100644 test/built-ins/TypedArray/prototype/filter/callbackfn-not-callable-throws.js create mode 100644 test/built-ins/TypedArray/prototype/filter/callbackfn-not-called-on-empty.js create mode 100644 test/built-ins/TypedArray/prototype/filter/callbackfn-return-does-not-change-instance.js create mode 100644 test/built-ins/TypedArray/prototype/filter/callbackfn-returns-abrupt.js create mode 100644 test/built-ins/TypedArray/prototype/filter/callbackfn-set-value-during-iteration.js create mode 100644 test/built-ins/TypedArray/prototype/filter/callbackfn-this.js create mode 100644 test/built-ins/TypedArray/prototype/filter/result-does-not-share-buffer.js create mode 100644 test/built-ins/TypedArray/prototype/filter/result-empty-callbackfn-returns-false.js create mode 100644 test/built-ins/TypedArray/prototype/filter/result-full-callbackfn-returns-true.js create mode 100644 test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor-abrupt.js create mode 100644 test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor-inherited.js create mode 100644 test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor-returns-throws.js create mode 100644 test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor.js create mode 100644 test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-abrupt.js create mode 100644 test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-invocation.js create mode 100644 test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-length-throws.js create mode 100644 test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-length.js create mode 100644 test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-returns-another-instance.js create mode 100644 test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-throws.js create mode 100644 test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor.js create mode 100644 test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-returns-throws.js create mode 100644 test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-use-default-ctor.js create mode 100644 test/built-ins/TypedArray/prototype/filter/speciesctor-get-species.js create mode 100644 test/built-ins/TypedArray/prototype/filter/values-are-not-cached.js create mode 100644 test/built-ins/TypedArray/prototype/filter/values-are-set.js diff --git a/test/built-ins/TypedArray/prototype/filter/arraylength-internal.js b/test/built-ins/TypedArray/prototype/filter/arraylength-internal.js new file mode 100644 index 0000000000..9dc8a2109c --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/arraylength-internal.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/callbackfn-arguments-with-thisarg.js b/test/built-ins/TypedArray/prototype/filter/callbackfn-arguments-with-thisarg.js new file mode 100644 index 0000000000..983d30712f --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/callbackfn-arguments-with-thisarg.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/callbackfn-arguments-without-thisarg.js b/test/built-ins/TypedArray/prototype/filter/callbackfn-arguments-without-thisarg.js new file mode 100644 index 0000000000..7e2f9692b8 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/callbackfn-arguments-without-thisarg.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/callbackfn-called-before-ctor.js b/test/built-ins/TypedArray/prototype/filter/callbackfn-called-before-ctor.js new file mode 100644 index 0000000000..a6cf03d3ab --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/callbackfn-called-before-ctor.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/callbackfn-called-before-species.js b/test/built-ins/TypedArray/prototype/filter/callbackfn-called-before-species.js new file mode 100644 index 0000000000..b79dc2d097 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/callbackfn-called-before-species.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/callbackfn-detachbuffer.js b/test/built-ins/TypedArray/prototype/filter/callbackfn-detachbuffer.js new file mode 100644 index 0000000000..e719c8a946 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/callbackfn-detachbuffer.js @@ -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); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/callbackfn-no-iteration-over-non-integer.js b/test/built-ins/TypedArray/prototype/filter/callbackfn-no-iteration-over-non-integer.js new file mode 100644 index 0000000000..ccc6e64b26 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/callbackfn-no-iteration-over-non-integer.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/callbackfn-not-callable-throws.js b/test/built-ins/TypedArray/prototype/filter/callbackfn-not-callable-throws.js new file mode 100644 index 0000000000..3b6e8773a9 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/callbackfn-not-callable-throws.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/callbackfn-not-called-on-empty.js b/test/built-ins/TypedArray/prototype/filter/callbackfn-not-called-on-empty.js new file mode 100644 index 0000000000..d5224368b2 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/callbackfn-not-called-on-empty.js @@ -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); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/callbackfn-return-does-not-change-instance.js b/test/built-ins/TypedArray/prototype/filter/callbackfn-return-does-not-change-instance.js new file mode 100644 index 0000000000..4a46d8f4ee --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/callbackfn-return-does-not-change-instance.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/callbackfn-returns-abrupt.js b/test/built-ins/TypedArray/prototype/filter/callbackfn-returns-abrupt.js new file mode 100644 index 0000000000..574afea76c --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/callbackfn-returns-abrupt.js @@ -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(); + }); + }); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/callbackfn-set-value-during-iteration.js b/test/built-ins/TypedArray/prototype/filter/callbackfn-set-value-during-iteration.js new file mode 100644 index 0000000000..ed355fbba5 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/callbackfn-set-value-during-iteration.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/callbackfn-this.js b/test/built-ins/TypedArray/prototype/filter/callbackfn-this.js new file mode 100644 index 0000000000..169dce30f7 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/callbackfn-this.js @@ -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]"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/result-does-not-share-buffer.js b/test/built-ins/TypedArray/prototype/filter/result-does-not-share-buffer.js new file mode 100644 index 0000000000..8ee1898429 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/result-does-not-share-buffer.js @@ -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); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/result-empty-callbackfn-returns-false.js b/test/built-ins/TypedArray/prototype/filter/result-empty-callbackfn-returns-false.js new file mode 100644 index 0000000000..2d9e9ea0c4 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/result-empty-callbackfn-returns-false.js @@ -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); + }); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/result-full-callbackfn-returns-true.js b/test/built-ins/TypedArray/prototype/filter/result-full-callbackfn-returns-true.js new file mode 100644 index 0000000000..ae6b89a4fd --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/result-full-callbackfn-returns-true.js @@ -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); + }); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor-abrupt.js b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor-abrupt.js new file mode 100644 index 0000000000..da99e55b10 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor-abrupt.js @@ -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; + }); + }); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor-inherited.js b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor-inherited.js new file mode 100644 index 0000000000..fb69d6af31 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor-inherited.js @@ -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" + ); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor-returns-throws.js b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor-returns-throws.js new file mode 100644 index 0000000000..5414f38420 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor-returns-throws.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor.js b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor.js new file mode 100644 index 0000000000..87d96ea985 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-ctor.js @@ -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" + ); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-abrupt.js b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-abrupt.js new file mode 100644 index 0000000000..8795742cc8 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-abrupt.js @@ -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; }); + }); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-invocation.js b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-invocation.js new file mode 100644 index 0000000000..394d350422 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-invocation.js @@ -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" + ); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-length-throws.js b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-length-throws.js new file mode 100644 index 0000000000..db9b5963d1 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-length-throws.js @@ -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; }); + }); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-length.js b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-length.js new file mode 100644 index 0000000000..1d3d719c9c --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-length.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-returns-another-instance.js b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-returns-another-instance.js new file mode 100644 index 0000000000..425026d370 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-returns-another-instance.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-throws.js b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-throws.js new file mode 100644 index 0000000000..f3e4327bfa --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor-throws.js @@ -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() {}); + }); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor.js b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor.js new file mode 100644 index 0000000000..03ed29263a --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-returns-throws.js b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-returns-throws.js new file mode 100644 index 0000000000..1eb770fc89 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-returns-throws.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-use-default-ctor.js b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-use-default-ctor.js new file mode 100644 index 0000000000..a30f8da956 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species-use-default-ctor.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species.js b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species.js new file mode 100644 index 0000000000..9d9b4535a8 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/speciesctor-get-species.js @@ -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); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/values-are-not-cached.js b/test/built-ins/TypedArray/prototype/filter/values-are-not-cached.js new file mode 100644 index 0000000000..1310e4d731 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/values-are-not-cached.js @@ -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" + ); + }); +}); diff --git a/test/built-ins/TypedArray/prototype/filter/values-are-set.js b/test/built-ins/TypedArray/prototype/filter/values-are-set.js new file mode 100644 index 0000000000..65defb955a --- /dev/null +++ b/test/built-ins/TypedArray/prototype/filter/values-are-set.js @@ -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"); +});