diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /js/src/tests/test262/built-ins/Array/prototype/find | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/src/tests/test262/built-ins/Array/prototype/find')
22 files changed, 898 insertions, 0 deletions
diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/array-altered-during-loop.js b/js/src/tests/test262/built-ins/Array/prototype/find/array-altered-during-loop.js new file mode 100644 index 0000000000..4cedb36f08 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/array-altered-during-loop.js @@ -0,0 +1,49 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: > + The range of elements processed is set before the first call to `predicate`. +info: | + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 6. If thisArg was supplied, let T be thisArg; else let T be undefined. + 7. Let k be 0. + 8. Repeat, while k < len + ... + d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)). + ... +---*/ + +var arr = ['Shoes', 'Car', 'Bike']; +var results = []; + +arr.find(function(kValue) { + if (results.length === 0) { + arr.splice(1, 1); + } + results.push(kValue); +}); + +assert.sameValue(results.length, 3, 'predicate called three times'); +assert.sameValue(results[0], 'Shoes'); +assert.sameValue(results[1], 'Bike'); +assert.sameValue(results[2], undefined); + +results = []; +arr = ['Skateboard', 'Barefoot']; +arr.find(function(kValue) { + if (results.length === 0) { + arr.push('Motorcycle'); + arr[1] = 'Magic Carpet'; + } + + results.push(kValue); +}); + +assert.sameValue(results.length, 2, 'predicate called twice'); +assert.sameValue(results[0], 'Skateboard'); +assert.sameValue(results[1], 'Magic Carpet'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/browser.js b/js/src/tests/test262/built-ins/Array/prototype/find/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/browser.js diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/call-with-boolean.js b/js/src/tests/test262/built-ins/Array/prototype/find/call-with-boolean.js new file mode 100644 index 0000000000..4ac4e5fa42 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/call-with-boolean.js @@ -0,0 +1,20 @@ +// Copyright (c) 2020 Rick Waldron. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.find +description: Array.prototype.find applied to boolean primitive +---*/ + +assert.sameValue( + Array.prototype.find.call(true, () => {}), + undefined, + 'Array.prototype.find.call(true, () => {}) must return undefined' +); +assert.sameValue( + Array.prototype.find.call(false, () => {}), + undefined, + 'Array.prototype.find.call(false, () => {}) must return undefined' +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/callbackfn-resize-arraybuffer.js b/js/src/tests/test262/built-ins/Array/prototype/find/callbackfn-resize-arraybuffer.js new file mode 100644 index 0000000000..31a7b55750 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/callbackfn-resize-arraybuffer.js @@ -0,0 +1,76 @@ +// |reftest| shell-option(--enable-arraybuffer-resizable) skip-if(!ArrayBuffer.prototype.resize||!xulRuntime.shell) -- resizable-arraybuffer is not enabled unconditionally, requires shell-options +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: TypedArray instance buffer can be resized during iteration +includes: [testTypedArray.js, compareArray.js] +features: [TypedArray, resizable-arraybuffer] +---*/ + +// If the host chooses to throw as allowed by the specification, the observed +// behavior will be identical to the case where `ArrayBuffer.prototype.resize` +// has not been implemented. The following assertion prevents this test from +// passing in runtimes which have not implemented the method. +assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function'); + +testWithTypedArrayConstructors(function(TA) { + var BPE = TA.BYTES_PER_ELEMENT; + var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4}); + var sample = new TA(buffer); + var finalElement, expectedElements, expectedIndices, expectedArrays; + var elements, indices, arrays, result; + + elements = []; + indices = []; + arrays = []; + result = Array.prototype.find.call(sample, function(element, index, array) { + if (elements.length === 0) { + try { + buffer.resize(2 * BPE); + finalElement = undefined; + expectedElements = [0, 0]; + expectedIndices = [0, 1]; + expectedArrays = [sample, sample]; + } catch (_) { + finalElement = 0; + expectedElements = [0, 0, 0]; + expectedIndices = [0, 1, 2]; + expectedArrays = [sample, sample, sample]; + } + } + + elements.push(element); + indices.push(index); + arrays.push(array); + return false; + }); + + assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)'); + assert.compareArray(indices, [0, 1, 2], 'indices (shrink)'); + assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)'); + assert.sameValue(result, undefined, 'result (shrink)'); + + elements = []; + indices = []; + arrays = []; + result = Array.prototype.find.call(sample, function(element, index, array) { + if (elements.length === 0) { + try { + buffer.resize(4 * BPE); + } catch (_) {} + } + + elements.push(element); + indices.push(index); + arrays.push(array); + return false; + }); + + assert.compareArray(elements, expectedElements, 'elements (grow)'); + assert.compareArray(indices, expectedIndices, 'indices (grow)'); + assert.compareArray(arrays, expectedArrays, 'arrays (grow)'); + assert.sameValue(result, undefined, 'result (grow)'); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/length.js b/js/src/tests/test262/built-ins/Array/prototype/find/length.js new file mode 100644 index 0000000000..2522319483 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/length.js @@ -0,0 +1,20 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: Array.prototype.find.length value and descriptor. +info: | + 17 ECMAScript Standard Built-in Objects +includes: [propertyHelper.js] +---*/ + +assert.sameValue( + Array.prototype.find.length, 1, + 'The value of `Array.prototype.find.length` is `1`' +); + +verifyNotEnumerable(Array.prototype.find, 'length'); +verifyNotWritable(Array.prototype.find, 'length'); +verifyConfigurable(Array.prototype.find, 'length'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/name.js b/js/src/tests/test262/built-ins/Array/prototype/find/name.js new file mode 100644 index 0000000000..8a4c21fe67 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/name.js @@ -0,0 +1,24 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: > + Array.prototype.find.name value and descriptor. +info: | + 22.1.3.8 Array.prototype.find ( predicate [ , thisArg ] ) + + 17 ECMAScript Standard Built-in Objects + +includes: [propertyHelper.js] +---*/ + +assert.sameValue( + Array.prototype.find.name, 'find', + 'The value of `Array.prototype.find.name` is `"find"`' +); + +verifyNotEnumerable(Array.prototype.find, 'name'); +verifyNotWritable(Array.prototype.find, 'name'); +verifyConfigurable(Array.prototype.find, 'name'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/not-a-constructor.js b/js/src/tests/test262/built-ins/Array/prototype/find/not-a-constructor.js new file mode 100644 index 0000000000..8e2d17caff --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/not-a-constructor.js @@ -0,0 +1,31 @@ +// Copyright (C) 2020 Rick Waldron. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-ecmascript-standard-built-in-objects +description: > + Array.prototype.find does not implement [[Construct]], is not new-able +info: | + ECMAScript Function Objects + + Built-in function objects that are not identified as constructors do not + implement the [[Construct]] internal method unless otherwise specified in + the description of a particular function. + + sec-evaluatenew + + ... + 7. If IsConstructor(constructor) is false, throw a TypeError exception. + ... +includes: [isConstructor.js] +features: [Reflect.construct, arrow-function] +---*/ + +assert.sameValue(isConstructor(Array.prototype.find), false, 'isConstructor(Array.prototype.find) must return false'); + +assert.throws(TypeError, () => { + new Array.prototype.find(() => {}); +}, '`new Array.prototype.find(() => {})` throws TypeError'); + + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/predicate-call-parameters.js b/js/src/tests/test262/built-ins/Array/prototype/find/predicate-call-parameters.js new file mode 100644 index 0000000000..aa9483c656 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/predicate-call-parameters.js @@ -0,0 +1,48 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: > + Predicate called as F.call( thisArg, kValue, k, O ) for each array entry. +info: | + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 6. If thisArg was supplied, let T be thisArg; else let T be undefined. + 7. Let k be 0. + 8. Repeat, while k < len + ... + d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)). + e. ReturnIfAbrupt(testResult). + ... +---*/ + +var arr = ['Mike', 'Rick', 'Leo']; + +var results = []; + +arr.find(function(kValue, k, O) { + results.push(arguments); +}); + +assert.sameValue(results.length, 3); + +var result = results[0]; +assert.sameValue(result[0], 'Mike'); +assert.sameValue(result[1], 0); +assert.sameValue(result[2], arr); +assert.sameValue(result.length, 3); + +result = results[1]; +assert.sameValue(result[0], 'Rick'); +assert.sameValue(result[1], 1); +assert.sameValue(result[2], arr); +assert.sameValue(result.length, 3); + +result = results[2]; +assert.sameValue(result[0], 'Leo'); +assert.sameValue(result[1], 2); +assert.sameValue(result[2], arr); +assert.sameValue(result.length, 3); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/predicate-call-this-non-strict.js b/js/src/tests/test262/built-ins/Array/prototype/find/predicate-call-this-non-strict.js new file mode 100644 index 0000000000..b9b1827fb0 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/predicate-call-this-non-strict.js @@ -0,0 +1,34 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: > + Predicate thisArg as F.call( thisArg, kValue, k, O ) for each array entry. +info: | + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 8. Repeat, while k < len + ... + d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)). + e. ReturnIfAbrupt(testResult). + ... +flags: [noStrict] +---*/ + +var result; + +[1].find(function(kValue, k, O) { + result = this; +}); + +assert.sameValue(result, this); + +var o = {}; +[1].find(function() { + result = this; +}, o); + +assert.sameValue(result, o); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/predicate-call-this-strict-strict.js b/js/src/tests/test262/built-ins/Array/prototype/find/predicate-call-this-strict-strict.js new file mode 100644 index 0000000000..4ed3a65b04 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/predicate-call-this-strict-strict.js @@ -0,0 +1,35 @@ +'use strict'; +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: > + Predicate thisArg as F.call( thisArg, kValue, k, O ) for each array entry. +info: | + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 8. Repeat, while k < len + ... + d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)). + e. ReturnIfAbrupt(testResult). + ... +flags: [onlyStrict] +---*/ + +var result; + +[1].find(function(kValue, k, O) { + result = this; +}); + +assert.sameValue(result, undefined); + +var o = {}; +[1].find(function() { + result = this; +}, o); + +assert.sameValue(result, o); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/predicate-called-for-each-array-property.js b/js/src/tests/test262/built-ins/Array/prototype/find/predicate-called-for-each-array-property.js new file mode 100644 index 0000000000..6e42134880 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/predicate-called-for-each-array-property.js @@ -0,0 +1,28 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: > + Predicate is called for each array property. +info: | + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 6. If thisArg was supplied, let T be thisArg; else let T be undefined. + 7. Let k be 0. + 8. Repeat, while k < len + ... + d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)). + ... +---*/ + +var arr = [undefined, , , 'foo']; +var called = 0; + +arr.find(function() { + called++; +}); + +assert.sameValue(called, 4); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/predicate-is-not-callable-throws.js b/js/src/tests/test262/built-ins/Array/prototype/find/predicate-is-not-callable-throws.js new file mode 100644 index 0000000000..09266c14a7 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/predicate-is-not-callable-throws.js @@ -0,0 +1,51 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: > + Throws a TypeError exception if predicate is not callable. +info: | + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 5. If IsCallable(predicate) is false, throw a TypeError exception. + ... +---*/ + +assert.throws(TypeError, function() { + [].find({}); +}); + +assert.throws(TypeError, function() { + [].find(null); +}); + +assert.throws(TypeError, function() { + [].find(undefined); +}); + +assert.throws(TypeError, function() { + [].find(true); +}); + +assert.throws(TypeError, function() { + [].find(1); +}); + +assert.throws(TypeError, function() { + [].find(''); +}); + +assert.throws(TypeError, function() { + [].find(1); +}); + +assert.throws(TypeError, function() { + [].find([]); +}); + +assert.throws(TypeError, function() { + [].find(/./); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/predicate-not-called-on-empty-array.js b/js/src/tests/test262/built-ins/Array/prototype/find/predicate-not-called-on-empty-array.js new file mode 100644 index 0000000000..115e03774a --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/predicate-not-called-on-empty-array.js @@ -0,0 +1,31 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: > + Predicate is only called if this.length is > 0. +info: | + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 7. Let k be 0. + 8. Repeat, while k < len + ... + d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)). + ... + 9. Return undefined. +---*/ + +var called = false; + +var predicate = function() { + called = true; + return true; +}; + +var result = [].find(predicate); + +assert.sameValue(called, false, '[].find(predicate) does not call predicate'); +assert.sameValue(result, undefined, '[].find(predicate) returned undefined'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/prop-desc.js b/js/src/tests/test262/built-ins/Array/prototype/find/prop-desc.js new file mode 100644 index 0000000000..1e36c8a92f --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/prop-desc.js @@ -0,0 +1,21 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: Property type and descriptor. +info: | + 17 ECMAScript Standard Built-in Objects +includes: [propertyHelper.js] +---*/ + +assert.sameValue( + typeof Array.prototype.find, + 'function', + '`typeof Array.prototype.find` is `function`' +); + +verifyNotEnumerable(Array.prototype, 'find'); +verifyWritable(Array.prototype, 'find'); +verifyConfigurable(Array.prototype, 'find'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/return-abrupt-from-predicate-call.js b/js/src/tests/test262/built-ins/Array/prototype/find/return-abrupt-from-predicate-call.js new file mode 100644 index 0000000000..d12cc87369 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/return-abrupt-from-predicate-call.js @@ -0,0 +1,27 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: > + Return abrupt from predicate call. +info: | + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 7. Let k be 0. + 8. Repeat, while k < len + ... + d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)). + e. ReturnIfAbrupt(testResult). + ... +---*/ + +var predicate = function() { + throw new Test262Error(); +}; + +assert.throws(Test262Error, function() { + [1].find(predicate); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/return-abrupt-from-property.js b/js/src/tests/test262/built-ins/Array/prototype/find/return-abrupt-from-property.js new file mode 100644 index 0000000000..27d0bd6870 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/return-abrupt-from-property.js @@ -0,0 +1,33 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: > + Returns abrupt from getting property value from `this`. +info: | + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 7. Let k be 0. + 8. Repeat, while k < len + a. Let Pk be ToString(k). + b. Let kValue be Get(O, Pk). + c. ReturnIfAbrupt(kValue). + ... +---*/ + +var o = { + length: 1 +}; + +Object.defineProperty(o, 0, { + get: function() { + throw new Test262Error(); + } +}); + +assert.throws(Test262Error, function() { + [].find.call(o, function() {}); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/return-abrupt-from-this-length-as-symbol.js b/js/src/tests/test262/built-ins/Array/prototype/find/return-abrupt-from-this-length-as-symbol.js new file mode 100644 index 0000000000..25fc55d79f --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/return-abrupt-from-this-length-as-symbol.js @@ -0,0 +1,26 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: > + Return abrupt from ToLength(Get(O, "length")) where length is a Symbol. +info: | + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + 1. Let O be ToObject(this value). + 2. ReturnIfAbrupt(O). + 3. Let len be ToLength(Get(O, "length")). + 4. ReturnIfAbrupt(len). +features: [Symbol] +---*/ + +var o = {}; + +o.length = Symbol(1); + +// predicate fn is given to avoid false positives +assert.throws(TypeError, function() { + [].find.call(o, function() {}); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/return-abrupt-from-this-length.js b/js/src/tests/test262/built-ins/Array/prototype/find/return-abrupt-from-this-length.js new file mode 100644 index 0000000000..0063384230 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/return-abrupt-from-this-length.js @@ -0,0 +1,40 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: > + Return abrupt from ToLength(Get(O, "length")). +info: | + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + 1. Let O be ToObject(this value). + 2. ReturnIfAbrupt(O). + 3. Let len be ToLength(Get(O, "length")). + 4. ReturnIfAbrupt(len). +---*/ + +var o1 = {}; + +Object.defineProperty(o1, 'length', { + get: function() { + throw new Test262Error(); + }, + configurable: true +}); + +assert.throws(Test262Error, function() { + [].find.call(o1); +}); + +var o2 = { + length: { + valueOf: function() { + throw new Test262Error(); + } + } +}; +assert.throws(Test262Error, function() { + [].find.call(o2); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/return-abrupt-from-this.js b/js/src/tests/test262/built-ins/Array/prototype/find/return-abrupt-from-this.js new file mode 100644 index 0000000000..4be965cd8f --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/return-abrupt-from-this.js @@ -0,0 +1,23 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: > + Return abrupt from ToObject(this value). +info: | + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + 1. Let O be ToObject(this value). + 2. ReturnIfAbrupt(O). +---*/ + +// predicate fn is given to avoid false positives +assert.throws(TypeError, function() { + Array.prototype.find.call(undefined, function() {}); +}); + +assert.throws(TypeError, function() { + Array.prototype.find.call(null, function() {}); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/return-found-value-predicate-result-is-true.js b/js/src/tests/test262/built-ins/Array/prototype/find/return-found-value-predicate-result-is-true.js new file mode 100644 index 0000000000..bde5b7c13c --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/return-found-value-predicate-result-is-true.js @@ -0,0 +1,65 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: > + Return found value if predicate return a boolean true value. +info: | + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 8. Repeat, while k < len + ... + d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)). + e. ReturnIfAbrupt(testResult). + f. If testResult is true, return kValue. + ... +features: [Symbol] +---*/ + +var arr = ['Shoes', 'Car', 'Bike']; +var called = 0; + +var result = arr.find(function(val) { + called++; + return true; +}); + +assert.sameValue(result, 'Shoes'); +assert.sameValue(called, 1, 'predicate was called once'); + +called = 0; +result = arr.find(function(val) { + called++; + return val === 'Bike'; +}); + +assert.sameValue(called, 3, 'predicate was called three times'); +assert.sameValue(result, 'Bike'); + +result = arr.find(function(val) { + return 'string'; +}); +assert.sameValue(result, 'Shoes', 'coerced string'); + +result = arr.find(function(val) { + return {}; +}); +assert.sameValue(result, 'Shoes', 'coerced object'); + +result = arr.find(function(val) { + return Symbol(''); +}); +assert.sameValue(result, 'Shoes', 'coerced Symbol'); + +result = arr.find(function(val) { + return 1; +}); +assert.sameValue(result, 'Shoes', 'coerced number'); + +result = arr.find(function(val) { + return -1; +}); +assert.sameValue(result, 'Shoes', 'coerced negative number'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/return-undefined-if-predicate-returns-false-value.js b/js/src/tests/test262/built-ins/Array/prototype/find/return-undefined-if-predicate-returns-false-value.js new file mode 100644 index 0000000000..51a8d9aa20 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/return-undefined-if-predicate-returns-false-value.js @@ -0,0 +1,55 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-array.prototype.find +description: > + Return undefined if predicate always returns a boolean false value. +info: | + 22.1.3.8 Array.prototype.find ( predicate[ , thisArg ] ) + + ... + 8. Repeat, while k < len + ... + d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)). + ... + 9. Return undefined. +features: [Symbol] +---*/ + +var arr = ['Shoes', 'Car', 'Bike']; +var called = 0; + +var result = arr.find(function(val) { + called++; + return false; +}); + +assert.sameValue(called, 3, 'predicate was called three times'); +assert.sameValue(result, undefined); + +result = arr.find(function(val) { + return ''; +}); +assert.sameValue(result, undefined, 'coerced string'); + +result = arr.find(function(val) { + return undefined; +}); +assert.sameValue(result, undefined, 'coerced undefined'); + +result = arr.find(function(val) { + return null; +}); +assert.sameValue(result, undefined, 'coerced null'); + +result = arr.find(function(val) { + return 0; +}); +assert.sameValue(result, undefined, 'coerced 0'); + +result = arr.find(function(val) { + return NaN; +}); +assert.sameValue(result, undefined, 'coerced NaN'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/Array/prototype/find/shell.js b/js/src/tests/test262/built-ins/Array/prototype/find/shell.js new file mode 100644 index 0000000000..05fecd6ef1 --- /dev/null +++ b/js/src/tests/test262/built-ins/Array/prototype/find/shell.js @@ -0,0 +1,161 @@ +// GENERATED, DO NOT EDIT +// file: testTypedArray.js +// Copyright (C) 2015 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +description: | + Collection of functions used to assert the correctness of TypedArray objects. +defines: + - floatArrayConstructors + - nonClampedIntArrayConstructors + - intArrayConstructors + - typedArrayConstructors + - TypedArray + - testWithTypedArrayConstructors + - nonAtomicsFriendlyTypedArrayConstructors + - testWithAtomicsFriendlyTypedArrayConstructors + - testWithNonAtomicsFriendlyTypedArrayConstructors + - testTypedArrayConversions +---*/ + +var floatArrayConstructors = [ + Float64Array, + Float32Array +]; + +var nonClampedIntArrayConstructors = [ + Int32Array, + Int16Array, + Int8Array, + Uint32Array, + Uint16Array, + Uint8Array +]; + +var intArrayConstructors = nonClampedIntArrayConstructors.concat([Uint8ClampedArray]); + +// Float16Array is a newer feature +// adding it to this list unconditionally would cause implementations lacking it to fail every test which uses it +if (typeof Float16Array !== 'undefined') { + floatArrayConstructors.push(Float16Array); +} + +/** + * Array containing every non-bigint typed array constructor. + */ + +var typedArrayConstructors = floatArrayConstructors.concat(intArrayConstructors); + +/** + * The %TypedArray% intrinsic constructor function. + */ +var TypedArray = Object.getPrototypeOf(Int8Array); + +/** + * Callback for testing a typed array constructor. + * + * @callback typedArrayConstructorCallback + * @param {Function} Constructor the constructor object to test with. + */ + +/** + * Calls the provided function for every typed array constructor. + * + * @param {typedArrayConstructorCallback} f - the function to call for each typed array constructor. + * @param {Array} selected - An optional Array with filtered typed arrays + */ +function testWithTypedArrayConstructors(f, selected) { + var constructors = selected || typedArrayConstructors; + for (var i = 0; i < constructors.length; ++i) { + var constructor = constructors[i]; + try { + f(constructor); + } catch (e) { + e.message += " (Testing with " + constructor.name + ".)"; + throw e; + } + } +} + +var nonAtomicsFriendlyTypedArrayConstructors = floatArrayConstructors.concat([Uint8ClampedArray]); +/** + * Calls the provided function for every non-"Atomics Friendly" typed array constructor. + * + * @param {typedArrayConstructorCallback} f - the function to call for each typed array constructor. + * @param {Array} selected - An optional Array with filtered typed arrays + */ +function testWithNonAtomicsFriendlyTypedArrayConstructors(f) { + testWithTypedArrayConstructors(f, nonAtomicsFriendlyTypedArrayConstructors); +} + +/** + * Calls the provided function for every "Atomics Friendly" typed array constructor. + * + * @param {typedArrayConstructorCallback} f - the function to call for each typed array constructor. + * @param {Array} selected - An optional Array with filtered typed arrays + */ +function testWithAtomicsFriendlyTypedArrayConstructors(f) { + testWithTypedArrayConstructors(f, [ + Int32Array, + Int16Array, + Int8Array, + Uint32Array, + Uint16Array, + Uint8Array, + ]); +} + +/** + * Helper for conversion operations on TypedArrays, the expected values + * properties are indexed in order to match the respective value for each + * TypedArray constructor + * @param {Function} fn - the function to call for each constructor and value. + * will be called with the constructor, value, expected + * value, and a initial value that can be used to avoid + * a false positive with an equivalent expected value. + */ +function testTypedArrayConversions(byteConversionValues, fn) { + var values = byteConversionValues.values; + var expected = byteConversionValues.expected; + + testWithTypedArrayConstructors(function(TA) { + var name = TA.name.slice(0, -5); + + return values.forEach(function(value, index) { + var exp = expected[name][index]; + var initial = 0; + if (exp === 0) { + initial = 1; + } + fn(TA, value, exp, initial); + }); + }); +} + +/** + * Checks if the given argument is one of the float-based TypedArray constructors. + * + * @param {constructor} ctor - the value to check + * @returns {boolean} + */ +function isFloatTypedArrayConstructor(arg) { + return floatArrayConstructors.indexOf(arg) !== -1; +} + +/** + * Determines the precision of the given float-based TypedArray constructor. + * + * @param {constructor} ctor - the value to check + * @returns {string} "half", "single", or "double" for Float16Array, Float32Array, and Float64Array respectively. + */ +function floatTypedArrayConstructorPrecision(FA) { + if (typeof Float16Array !== "undefined" && FA === Float16Array) { + return "half"; + } else if (FA === Float32Array) { + return "single"; + } else if (FA === Float64Array) { + return "double"; + } else { + throw new Error("Malformed test - floatTypedArrayConstructorPrecision called with non-float TypedArray"); + } +} |