diff options
Diffstat (limited to 'js/src/tests/test262/built-ins/TypedArray/prototype/toSorted')
14 files changed, 374 insertions, 0 deletions
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/browser.js b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/browser.js diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/comparefn-not-a-function.js b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/comparefn-not-a-function.js new file mode 100644 index 0000000000..1ff4bbfe9c --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/comparefn-not-a-function.js @@ -0,0 +1,29 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.toSorted +description: > + %TypedArray%.prototype.toSorted verifies that the comparator is callable before reading the length. +info: | + %TypedArray%.prototype.toSorted ( comparefn ) + + 1. If comparefn is not undefined and IsCallable(comparefn) is false, throw a TypeError exception. + 2. ... + 3. Let len be ? LengthOfArrayLike(O). +includes: [testTypedArray.js] +features: [TypedArray, change-array-by-copy] +---*/ + +var invalidComparators = [null, true, false, "", /a/g, 42, 42n, [], {}, Symbol()]; + +testWithTypedArrayConstructors(TA => { + const ta = new TA([1]); + for (var i = 0; i < invalidComparators.length; i++) { + assert.throws(TypeError, function() { + ta.toSorted(invalidComparators[i]); + }, String(invalidComparators[i])); + } +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/comparefn-stop-after-error.js b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/comparefn-stop-after-error.js new file mode 100644 index 0000000000..61894ca9fe --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/comparefn-stop-after-error.js @@ -0,0 +1,35 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.toSorted +description: > + %TypedArray%.prototype.toSorted doesn't call copmareFn if there is an error +info: | + %TypedArray%.prototype.toSorted ( compareFn ) + + ... + 9. Sort items using an implementation-defined sequence of + calls to SortCompare. If any such call returns an abrupt + completion, stop before performing any further calls to + SortCompare or steps in this algorithm and return that completion. + ... +includes: [testTypedArray.js] +features: [TypedArray, change-array-by-copy] +---*/ + +testWithTypedArrayConstructors(TA => { + var calls = 0; + var ta = new TA([3, 1, 2]); + try { + ta.toSorted(() => { + ++calls; + if (calls === 1) { + throw new Test262Error(); + } + }); + } catch (e) {} + assert.sameValue(calls <= 1, true, "compareFn is not called after an error"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/ignores-species.js b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/ignores-species.js new file mode 100644 index 0000000000..bdd4fa92e8 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/ignores-species.js @@ -0,0 +1,43 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.toSorted +description: > + %TypedArray%.prototype.toSorted ignores @@species +info: | + %TypedArray%.prototype.toSorted ( comparefn ) + + ... + 6. Let A be ? TypedArrayCreateSameType(O, « 𝔽(len) »). + ... + + TypedArrayCreateSameType ( exemplar, argumentList ) + ... + 2. Let constructor be the intrinsic object listed in column one of Table 63 for exemplar.[[TypedArrayName]]. + ... +includes: [testTypedArray.js] +features: [TypedArray, change-array-by-copy] +---*/ + +testWithTypedArrayConstructors(TA => { + var ta = new TA(); + ta.constructor = TA === Uint8Array ? Int32Array : Uint8Array; + assert.sameValue(Object.getPrototypeOf(ta.toSorted()), TA.prototype); + + ta = new TA(); + ta.constructor = { + [Symbol.species]: TA === Uint8Array ? Int32Array : Uint8Array, + }; + assert.sameValue(Object.getPrototypeOf(ta.toSorted()), TA.prototype); + + ta = new TA(); + Object.defineProperty(ta, "constructor", { + get() { + throw new Test262Error("Should not get .constructor"); + } + }); + ta.toSorted(); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/immutable.js b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/immutable.js new file mode 100644 index 0000000000..7fb76e65f5 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/immutable.js @@ -0,0 +1,20 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.toSorted +description: > + %TypedArray%.prototype.toSorted does not mutate its this value +includes: [testTypedArray.js, compareArray.js] +features: [TypedArray, change-array-by-copy] +---*/ + +testWithTypedArrayConstructors(TA => { + var ta = new TA([3, 1, 2]); + ta.toSorted(); + + assert.compareArray(ta, [3, 1, 2]); + assert.notSameValue(ta.toSorted(), ta); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/length-property-ignored.js b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/length-property-ignored.js new file mode 100644 index 0000000000..977e74b9c5 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/length-property-ignored.js @@ -0,0 +1,53 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.toSorted +description: > + %TypedArray%.prototype.toSorted does not read a "length" property +info: | + %TypedArray%.prototype.toSorted ( comparefn ) + + ... + 4. Let len be O.[[ArrayLength]]. + ... +includes: [testTypedArray.js, compareArray.js] +features: [TypedArray, change-array-by-copy] +---*/ + +testWithTypedArrayConstructors(TA => { + var ta = new TA([3, 1, 2]); + Object.defineProperty(ta, "length", { value: 2 }) + var res = ta.toSorted() + assert.compareArray(res, [1, 2, 3]); + assert.sameValue(res.length, 3); + + ta = new TA([3, 1, 2]); + Object.defineProperty(ta, "length", { value: 5 }); + res = ta.toSorted(); + assert.compareArray(res, [1, 2, 3]); + assert.sameValue(res.length, 3); +}); + +function setLength(length) { + Object.defineProperty(TypedArray.prototype, "length", { + get: () => length, + }); +} + +testWithTypedArrayConstructors(TA => { + var ta = new TA([3, 1, 2]); + + setLength(2); + var res = ta.toSorted(); + setLength(3); + assert.compareArray(res, [1, 2, 3]); + + setLength(5); + res = ta.toSorted(); + setLength(3); + + assert.compareArray(res, [1, 2, 3]); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/metadata/browser.js b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/metadata/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/metadata/browser.js diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/metadata/length.js b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/metadata/length.js new file mode 100644 index 0000000000..20e9f3d35b --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/metadata/length.js @@ -0,0 +1,32 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.toSorted +description: > + The "length" property of %TypedArray%.prototype.toSorted +info: | + 17 ECMAScript Standard Built-in Objects + + Every built-in function object, including constructors, has a length property + whose value is an integer. Unless otherwise specified, this value is equal to + the largest number of named arguments shown in the subclause headings for the + function description. Optional parameters (which are indicated with brackets: + [ ]) or rest parameters (which are shown using the form «...name») are not + included in the default argument count. + + Unless otherwise specified, the length property of a built-in function object + has the attributes { [[Writable]]: false, [[Enumerable]]: false, + [[Configurable]]: true }. +includes: [propertyHelper.js, testTypedArray.js] +features: [TypedArray, change-array-by-copy] +---*/ + +verifyProperty(TypedArray.prototype.toSorted, "length", { + value: 1, + writable: false, + enumerable: false, + configurable: true, +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/metadata/name.js b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/metadata/name.js new file mode 100644 index 0000000000..644f27593f --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/metadata/name.js @@ -0,0 +1,30 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.toSorted +description: > + %TypedArray%.prototype.toSorted.name is "toSorted". +info: | + %TypedArray%.prototype.toSorted ( comparefn ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, that is not + identified as an anonymous function has a name property whose value + is a String. + + Unless otherwise specified, the name property of a built-in Function + object, if it exists, has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. +includes: [propertyHelper.js, testTypedArray.js] +features: [TypedArray, change-array-by-copy] +---*/ + +verifyProperty(TypedArray.prototype.toSorted, "name", { + value: "toSorted", + writable: false, + enumerable: false, + configurable: true, +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/metadata/property-descriptor.js b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/metadata/property-descriptor.js new file mode 100644 index 0000000000..54588af0d5 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/metadata/property-descriptor.js @@ -0,0 +1,27 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.toSorted +description: > + "toSorted" property of %TypedArray%.prototype +info: | + 17 ECMAScript Standard Built-in Objects + + Every other data property described in clauses 18 through 26 and in Annex B.2 + has the attributes { [[Writable]]: true, [[Enumerable]]: false, + [[Configurable]]: true } unless otherwise specified. +includes: [propertyHelper.js, testTypedArray.js] +features: [TypedArray, change-array-by-copy] +---*/ + +assert.sameValue(typeof TypedArray.prototype.toSorted, "function", "typeof"); + +verifyProperty(TypedArray.prototype, "toSorted", { + value: TypedArray.prototype.toSorted, + writable: true, + enumerable: false, + configurable: true, +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/metadata/shell.js b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/metadata/shell.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/metadata/shell.js diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/not-a-constructor.js b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/not-a-constructor.js new file mode 100644 index 0000000000..be40246fa1 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/not-a-constructor.js @@ -0,0 +1,35 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-ecmascript-standard-built-in-objects +description: > + %TypedArray%.prototype.toSorted 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, testTypedArray.js] +features: [TypedArray, change-array-by-copy, Reflect.construct] +---*/ + +assert.sameValue( + isConstructor(TypedArray.prototype.toSorted), + false, + 'isConstructor(TypedArray.prototype.toSorted) must return false' +); + +assert.throws(TypeError, () => { + new TypedArray.prototype.toSorted(); +}, '`new TypedArray.prototype.toSorted()` throws TypeError'); + + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/shell.js b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/shell.js new file mode 100644 index 0000000000..eda1477282 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/shell.js @@ -0,0 +1,24 @@ +// GENERATED, DO NOT EDIT +// file: isConstructor.js +// Copyright (C) 2017 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: | + Test if a given function is a constructor function. +defines: [isConstructor] +features: [Reflect.construct] +---*/ + +function isConstructor(f) { + if (typeof f !== "function") { + throw new Test262Error("isConstructor invoked with a non-function value"); + } + + try { + Reflect.construct(function(){}, [], f); + } catch (e) { + return false; + } + return true; +} diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/this-value-invalid.js b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/this-value-invalid.js new file mode 100644 index 0000000000..47cdf3627a --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/toSorted/this-value-invalid.js @@ -0,0 +1,46 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.toSorted +description: > + %TypedArray%.prototype.toSorted throws if the receiver is not a valid TypedArray +info: | + %TypedArray%.prototype.toSorted ( comparefn ) + + 2. Let O be the this value. + 3. Perform ? ValidateTypedArray(O). + ... +includes: [detachArrayBuffer.js, testTypedArray.js] +features: [TypedArray, change-array-by-copy] +---*/ + +var invalidValues = { + 'null': null, + 'undefined': undefined, + 'true': true, + '"abc"': "abc", + '12': 12, + 'Symbol()': Symbol(), + '[1, 2, 3]': [1, 2, 3], + '{ 0: 1, 1: 2, 2: 3, length: 3 }': { 0: 1, 1: 2, 2: 3, length: 3 }, + 'Uint8Array.prototype': Uint8Array.prototype, + '1n': 1n, +}; + +Object.entries(invalidValues).forEach(value => { + assert.throws(TypeError, () => { + TypedArray.prototype.toSorted.call(value[1]); + }, `${value[0]} is not a valid TypedArray`); +}); + +testWithTypedArrayConstructors(function(TA) { + let buffer = new ArrayBuffer(8); + let sample = new TA(buffer, 0, 1); + $DETACHBUFFER(sample.buffer); + assert.throws(TypeError, () => { + sample.toSorted(); + }, `array has a detached buffer`); +}); + +reportCompare(0, 0); |