Add additional tests for copy by array features (#4576)

Addresses testing gaps identified in #4575
This commit is contained in:
Trevor Florence 2025-09-15 11:55:13 -06:00 committed by GitHub
parent daa4186be4
commit 0b02980322
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 154 additions and 0 deletions

View File

@ -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]);

View File

@ -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]);

View File

@ -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]);
});

View File

@ -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]);
});

View File

@ -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]);
});