diff --git a/test/built-ins/Array/prototype/toSorted/comparefn-controls-sort.js b/test/built-ins/Array/prototype/toSorted/comparefn-controls-sort.js new file mode 100644 index 0000000000..d58c78b625 --- /dev/null +++ b/test/built-ins/Array/prototype/toSorted/comparefn-controls-sort.js @@ -0,0 +1,39 @@ +// Copyright (C) 2025 Google. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.tosorted +description: > + Array.prototype.toSorted performs CompareArrayElements that sorts by the + provided comparator function +info: | + Array.prototype.toSorted ( compareFn ) + + ... + 5. Let SortCompare be a new Abstract Closure with parameters (x, y) that + captures comparator and performs the following steps when called: + a. Return ? CompareArrayElements(x, y, comparator). + ... +features: [change-array-by-copy] +includes: [compareArray.js] +---*/ + +function numericCompare(a, b) { + return a - b; +} + +assert.compareArray([1, 2, 3, 4].toSorted(numericCompare), [1, 2, 3, 4]); +assert.compareArray([4, 3, 2, 1].toSorted(numericCompare), [1, 2, 3, 4]); +assert.compareArray( + [333, 33, 3, 222, 22, 2, 111, 11, 1].toSorted(numericCompare), + [1, 2, 3, 11, 22, 33, 111, 222, 333]); + +function reverseNumericCompare(a, b) { + return b - a; +} + +assert.compareArray([1, 2, 3, 4].toSorted(reverseNumericCompare), [4, 3, 2, 1]); +assert.compareArray([4, 3, 2, 1].toSorted(reverseNumericCompare), [4, 3, 2, 1]); +assert.compareArray( + [333, 33, 3, 222, 22, 2, 111, 11, 1].toSorted(reverseNumericCompare), + [333, 222, 111, 33, 22, 11, 3, 2, 1]); diff --git a/test/built-ins/Array/prototype/toSorted/comparefn-default.js b/test/built-ins/Array/prototype/toSorted/comparefn-default.js new file mode 100644 index 0000000000..e16997c57c --- /dev/null +++ b/test/built-ins/Array/prototype/toSorted/comparefn-default.js @@ -0,0 +1,27 @@ +// Copyright (C) 2025 Google. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.tosorted +description: > + Array.prototype.toSorted performs CompareArrayElements that sorts by string + by default +info: | + Array.prototype.toSorted ( compareFn ) + + ... + 5. Let SortCompare be a new Abstract Closure with parameters (x, y) that + captures comparator and performs the following steps when called: + a. Return ? CompareArrayElements(x, y, comparator). + ... +features: [change-array-by-copy] +includes: [compareArray.js] +---*/ + +assert.compareArray([1, 2, 3, 4].toSorted(), [1, 2, 3, 4]); +assert.compareArray([4, 3, 2, 1].toSorted(), [1, 2, 3, 4]); +assert.compareArray(['a', 2, 1, 'z'].toSorted(), [1, 2, 'a', 'z']); + +assert.compareArray( + [333, 33, 3, 222, 22, 2, 111, 11, 1].toSorted(), + [1, 11, 111, 2, 22, 222, 3, 33, 333]); diff --git a/test/built-ins/TypedArray/prototype/toReversed/reverses.js b/test/built-ins/TypedArray/prototype/toReversed/reverses.js new file mode 100644 index 0000000000..a3033232e3 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/toReversed/reverses.js @@ -0,0 +1,27 @@ +// Copyright (C) 2025 Google. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-%typedarray%.prototype.toreversed +description: > + %TypedArray%.prototype.toReversed outputs a reversed copy +info: | + %TypedArray%.prototype.toReversed ( ) + + ... + 6. Repeat, while k < len, + a. Let from be ! ToString(𝔽(len - k - 1)). + b. Let Pk be ! ToString(𝔽(k)). + c. Let fromValue be ! Get(O, from). + d. Perform ! Set(A, Pk, fromValue, true). + e. Set k to k + 1. + ... +includes: [testTypedArray.js, compareArray.js] +features: [TypedArray, change-array-by-copy] +---*/ + +testWithTypedArrayConstructors(TA => { + assert.compareArray(new TA([]).toReversed(), []); + assert.compareArray(new TA([1]).toReversed(), [1]); + assert.compareArray(new TA([1, 2, 3, 4]).toReversed(), [4, 3, 2, 1]); +}); diff --git a/test/built-ins/TypedArray/prototype/toSorted/comparefn-controls-sort.js b/test/built-ins/TypedArray/prototype/toSorted/comparefn-controls-sort.js new file mode 100644 index 0000000000..2f88b02459 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/toSorted/comparefn-controls-sort.js @@ -0,0 +1,35 @@ +// Copyright (C) 2025 Google. 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 performs CompareTypedArrayElements that sorts + by the provided comparator function +info: | + %TypedArray%.prototype.toSorted ( compareFn ) + + ... + 7. Let SortCompare be a new Abstract Closure with parameters (x, y) that + captures comparator and performs the following steps when called: + a. Return ? CompareTypedArrayElements(x, y, comparator). + ... +includes: [testTypedArray.js, compareArray.js] +features: [TypedArray, change-array-by-copy] +---*/ + +function reverseNumericCompare(a, b) { + return b - a; +} + +testWithTypedArrayConstructors(TA => { + assert.compareArray( + new TA([1, 2, 3, 4]).toSorted(reverseNumericCompare), + [4, 3, 2, 1]); + assert.compareArray( + new TA([4, 3, 2, 1]).toSorted(reverseNumericCompare), + [4, 3, 2, 1]); + assert.compareArray( + new TA([33, 3, 22, 2, 111, 11, 1]).toSorted(reverseNumericCompare), + [111, 33, 22, 11, 3, 2, 1]); +}); diff --git a/test/built-ins/TypedArray/prototype/toSorted/comparefn-default.js b/test/built-ins/TypedArray/prototype/toSorted/comparefn-default.js new file mode 100644 index 0000000000..387d312494 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/toSorted/comparefn-default.js @@ -0,0 +1,26 @@ +// Copyright (C) 2025 Google. 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 performs CompareTypedArrayElements that sorts + numerically by default +info: | + %TypedArray%.prototype.toSorted ( compareFn ) + + ... + 7. Let SortCompare be a new Abstract Closure with parameters (x, y) that + captures comparator and performs the following steps when called: + a. Return ? CompareTypedArrayElements(x, y, comparator). + ... +includes: [testTypedArray.js, compareArray.js] +features: [TypedArray, change-array-by-copy] +---*/ + +testWithTypedArrayConstructors(TA => { + assert.compareArray(new TA([4, 2, 1, 3]).toSorted(), [1, 2, 3, 4]); + assert.compareArray( + new TA([111, 33, 22, 11, 3, 2, 1]).toSorted(), + [1, 2, 3, 11, 22, 33, 111]); +});