[typedarray] Improve test for TA#sort stableness

This CL makes the TypedArray#sort unit test more robust, that checks
for the stable property.

R=jgruber@chromium.org

Bug: v8:8567
Change-Id: I8e6a729d0a8b54901535d49eb578b8b34bd6f2eb
Reviewed-on: https://chromium-review.googlesource.com/c/1449695
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Reviewed-by: Mathias Bynens <mathias@chromium.org>
Commit-Queue: Simon Zünd <szuend@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59316}
This commit is contained in:
Simon Zünd 2019-02-04 07:33:28 +01:00 committed by Commit Bot
parent f65a638ec1
commit b79e661957

View File

@ -71,14 +71,27 @@ for (var constructor of typedArrayConstructors) {
// Check that TypedArray.p.sort is stable. // Check that TypedArray.p.sort is stable.
for (let constructor of typedArrayConstructors) { for (let constructor of typedArrayConstructors) {
const template = [2, 1, 0, 4, 4, 4, 4, 4, 4, 4, 4]; // Sort an array [0..kSize-1] modulo 4. If the sort is stable, the array
// will be partitioned into 4 parts, where each part has only increasing
// elements.
const kSize = 128;
const kModulo = 4;
const kRunSize = kSize / kModulo;
const template = Array.from({ length: kSize }, (_, i) => i);
const array = new constructor(template); const array = new constructor(template);
// Treat 0..3, 4..7, etc. as equal.
const compare = (a, b) => (a / 4 | 0) - (b / 4 | 0); const compare = (a, b) => (b % kModulo) - (a % kModulo);
array.sort(compare); array.sort(compare);
assertArrayLikeEquals(array.slice(0, 3), [2, 1, 0], constructor); function assertIncreasing(from) {
for (let i = from + 1; i < from + kRunSize; ++i) {
assertTrue(array[i - 1] < array[i]);
assertEquals(array[i - 1] % kModulo, array[i] % kModulo);
}
}
for (let i = 0; i < kModulo; ++i) assertIncreasing(i * kRunSize);
} }
// The following creates a test for each typed element kind, where the array // The following creates a test for each typed element kind, where the array