Commit cbc36e43 authored by Simon Zünd's avatar Simon Zünd Committed by Commit Bot

Extend TypedArray.p.sort test with numerical edge cases.

This CL adds a test for each typed element kind where the array to sort
consists of some max/min/zero elements.

When providing a custom compare function, the upcoming torque version
of TypedArray.p.sort needs to convert array elements to Number/BigInt
and back. The tests check the edge cases for that conversion.

R=jgruber@chromium.org

Bug: v8:7382
Change-Id: Ia85ca343f62ece208acdeb1595e94f17ce12b713
Reviewed-on: https://chromium-review.googlesource.com/1021080
Commit-Queue: Simon Zünd <szuend@google.com>
Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#52710}
parent 8f55ec89
......@@ -68,3 +68,58 @@ for (var constructor of typedArrayConstructors) {
%ArrayBufferNeuter(array.buffer);
assertThrows(() => array.sort(), TypeError);
}
// The following creates a test for each typed element kind, where the array
// to sort consists of some max/min/zero elements.
//
// When providing a custom compare function, the torque version of
// TypedArray.p.sort needs to convert array elements to "Number"/"BigInt"
// and back. The following test checks the edge cases for that conversion.
let constructorsWithArrays = [
{ctor: Uint8Array, array: [255, 254, 4, 3, 2, 1, 0]},
{ctor: Int8Array, array: [127, 126, 1, 0, -1, -127, -128]},
{ctor: Uint16Array, array: [2 ** 16 - 1, 2 ** 16 - 2, 4, 3, 2, 1, 0]},
{
ctor: Int16Array,
array: [2 ** 15 - 1, 2 ** 15 - 2, 1, 0, -1, -(2 ** 15 - 1), -(2 ** 15)]
},
{ctor: Uint32Array, array: [2 ** 32 - 1, 2 ** 32 - 2, 4, 3, 2, 1, 0]},
{
ctor: Int32Array,
array: [2 ** 31 - 1, 2 ** 31 - 2, 1, 0, -1, -(2 ** 31 - 1), -(2 ** 31)]
},
{
ctor: Float32Array,
array: [2 ** 24, 2 ** 24 - 1, 1, 0,-1, -(2 ** 24 - 1), -(2 ** 24)]
},
{
ctor: Float64Array,
array: [2 ** 53, 2 ** 53 - 1, 1, 0, -1, -(2 ** 53 - 1), -(2 ** 53)]
},
{
ctor: BigUint64Array,
array: [2n ** 64n - 1n, 2n ** 64n - 2n, 4n, 3n, 2n, 1n, 0n]
},
{
ctor: BigInt64Array,
array: [2n ** 63n - 1n, 2n ** 63n - 2n, 1n, 0n,
-1n, -(2n ** 63n - 1n), -(2n ** 63n)]
}
];
// Compare function needs to return a Number in all cases, and not a BigInt.
// Hence we can not simply return "a - b".
function cmpfn(a, b) {
if (a < b) return -1;
if (b < a) return 1;
return 0;
}
for (let constructor of constructorsWithArrays) {
let array = new constructor.ctor(constructor.array);
assertEquals(array.sort(cmpfn), array);
assertArrayLikeEquals(array, constructor.array.reverse(), constructor.ctor);
assertEquals(array.length, constructor.array.length);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment