Commit 2b2ce1d8 authored by Marja Hölttä's avatar Marja Hölttä Committed by V8 LUCI CQ

[rab/gsab] Tests for Array.p methods, part 7

In this part: sort

Bug: v8:11111
Change-Id: Idd1e7552b4cdda0cdec610189391dbb729c94cdf
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3742703Reviewed-by: 's avatarShu-yu Guo <syg@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/main@{#81599}
parent 3483b970
......@@ -3422,6 +3422,8 @@ Reverse(ArrayReverseHelper);
}
})();
// This function cannot be reused between TypedArray.protoype.sort and
// Array.prototype.sort, since the default sorting functions differ.
(function SortWithDefaultComparison() {
for (let ctor of ctors) {
const gsab = CreateGrowableSharedArrayBuffer(4 * ctor.BYTES_PER_ELEMENT,
......@@ -3487,7 +3489,73 @@ Reverse(ArrayReverseHelper);
}
})();
(function SortWithCustomComparison() {
// The default comparison function for Array.prototype.sort is the string sort.
(function ArraySortWithDefaultComparison() {
for (let ctor of ctors) {
const gsab = CreateGrowableSharedArrayBuffer(4 * ctor.BYTES_PER_ELEMENT,
8 * ctor.BYTES_PER_ELEMENT);
const fixedLength = new ctor(gsab, 0, 4);
const fixedLengthWithOffset = new ctor(gsab, 2 * ctor.BYTES_PER_ELEMENT, 2);
const lengthTracking = new ctor(gsab, 0);
const lengthTrackingWithOffset = new ctor(gsab, 2 * ctor.BYTES_PER_ELEMENT);
const taFull = new ctor(gsab, 0);
function WriteUnsortedData() {
// Write some data into the array.
for (let i = 0; i < taFull.length; ++i) {
WriteToTypedArray(taFull, i, 10 - 2 * i);
}
}
// Orig. array: [10, 8, 6, 4]
// [10, 8, 6, 4] << fixedLength
// [6, 4] << fixedLengthWithOffset
// [10, 8, 6, 4, ...] << lengthTracking
// [6, 4, ...] << lengthTrackingWithOffset
WriteUnsortedData();
ArraySortHelper(fixedLength);
assertEquals([10, 4, 6, 8], ToNumbers(taFull));
WriteUnsortedData();
ArraySortHelper(fixedLengthWithOffset);
assertEquals([10, 8, 4, 6], ToNumbers(taFull));
WriteUnsortedData();
ArraySortHelper(lengthTracking);
assertEquals([10, 4, 6, 8], ToNumbers(taFull));
WriteUnsortedData();
ArraySortHelper(lengthTrackingWithOffset);
assertEquals([10, 8, 4, 6], ToNumbers(taFull));
// Grow.
gsab.grow(6 * ctor.BYTES_PER_ELEMENT);
// Orig. array: [10, 8, 6, 4, 2, 0]
// [10, 8, 6, 4] << fixedLength
// [6, 4] << fixedLengthWithOffset
// [10, 8, 6, 4, 2, 0, ...] << lengthTracking
// [6, 4, 2, 0, ...] << lengthTrackingWithOffset
WriteUnsortedData();
ArraySortHelper(fixedLength);
assertEquals([10, 4, 6, 8, 2, 0], ToNumbers(taFull));
WriteUnsortedData();
ArraySortHelper(fixedLengthWithOffset);
assertEquals([10, 8, 4, 6, 2, 0], ToNumbers(taFull));
WriteUnsortedData();
ArraySortHelper(lengthTracking);
assertEquals([0, 10, 2, 4, 6, 8], ToNumbers(taFull));
WriteUnsortedData();
ArraySortHelper(lengthTrackingWithOffset);
assertEquals([10, 8, 0, 2, 4, 6], ToNumbers(taFull));
}
})();
function SortWithCustomComparison(sortHelper) {
for (let ctor of ctors) {
const gsab = CreateGrowableSharedArrayBuffer(4 * ctor.BYTES_PER_ELEMENT,
8 * ctor.BYTES_PER_ELEMENT);
......@@ -3529,19 +3597,19 @@ Reverse(ArrayReverseHelper);
// [8, 7, ...] << lengthTrackingWithOffset
WriteUnsortedData();
fixedLength.sort(CustomComparison);
sortHelper(fixedLength, CustomComparison);
assertEquals([7, 9, 8, 10], ToNumbers(taFull));
WriteUnsortedData();
fixedLengthWithOffset.sort(CustomComparison);
sortHelper(fixedLengthWithOffset, CustomComparison);
assertEquals([10, 9, 7, 8], ToNumbers(taFull));
WriteUnsortedData();
lengthTracking.sort(CustomComparison);
sortHelper(lengthTracking, CustomComparison);
assertEquals([7, 9, 8, 10], ToNumbers(taFull));
WriteUnsortedData();
lengthTrackingWithOffset.sort(CustomComparison);
sortHelper(lengthTrackingWithOffset, CustomComparison);
assertEquals([10, 9, 7, 8], ToNumbers(taFull));
// Grow.
......@@ -3554,24 +3622,26 @@ Reverse(ArrayReverseHelper);
// [8, 7, 6, 5, ...] << lengthTrackingWithOffset
WriteUnsortedData();
fixedLength.sort(CustomComparison);
sortHelper(fixedLength, CustomComparison);
assertEquals([7, 9, 8, 10, 6, 5], ToNumbers(taFull));
WriteUnsortedData();
fixedLengthWithOffset.sort(CustomComparison);
sortHelper(fixedLengthWithOffset, CustomComparison);
assertEquals([10, 9, 7, 8, 6, 5], ToNumbers(taFull));
WriteUnsortedData();
lengthTracking.sort(CustomComparison);
sortHelper(lengthTracking, CustomComparison);
assertEquals([5, 7, 9, 6, 8, 10], ToNumbers(taFull));
WriteUnsortedData();
lengthTrackingWithOffset.sort(CustomComparison);
sortHelper(lengthTrackingWithOffset, CustomComparison);
assertEquals([10, 9, 5, 7, 6, 8], ToNumbers(taFull));
}
})();
}
SortWithCustomComparison(TypedArraySortHelper);
SortWithCustomComparison(ArraySortHelper);
(function SortCallbackGrows() {
function SortCallbackGrows(sortHelper) {
function WriteUnsortedData(taFull) {
for (let i = 0; i < taFull.length; ++i) {
WriteToTypedArray(taFull, i, 10 - i);
......@@ -3600,7 +3670,7 @@ Reverse(ArrayReverseHelper);
const taFull = new ctor(gsab, 0);
WriteUnsortedData(taFull);
fixedLength.sort(CustomComparison);
sortHelper(fixedLength, CustomComparison);
// Growing doesn't affect the sorting.
assertEquals([7, 8, 9, 10, 0, 0], ToNumbers(taFull));
......@@ -3615,13 +3685,15 @@ Reverse(ArrayReverseHelper);
const taFull = new ctor(gsab, 0);
WriteUnsortedData(taFull);
lengthTracking.sort(CustomComparison);
sortHelper(lengthTracking, CustomComparison);
// Growing doesn't affect the sorting. Only the elements that were part of
// the original TA are sorted.
assertEquals([7, 8, 9, 10, 0, 0], ToNumbers(taFull));
}
})();
}
SortCallbackGrows(TypedArraySortHelper);
SortCallbackGrows(ArraySortHelper);
(function ObjectDefinePropertyDefineProperties() {
for (let helper of
......
......@@ -439,3 +439,7 @@ const ArrayCopyWithinHelper = (ta, ...rest) => {
const TypedArrayReverseHelper = (ta) => { ta.reverse(); }
const ArrayReverseHelper = (ta) => { Array.prototype.reverse.call(ta); };
const TypedArraySortHelper = (ta, ...rest) => { ta.sort(...rest); }
const ArraySortHelper = (ta, ...rest) => {
Array.prototype.sort.call(ta, ...rest); };
......@@ -1495,7 +1495,7 @@ LastIndexOfParameterConversionDetaches(ArrayLastIndexOfHelper);
}
})();
(function SortCallbackDetaches() {
function SortCallbackDetaches(sortHelper) {
function WriteUnsortedData(taFull) {
for (let i = 0; i < taFull.length; ++i) {
WriteToTypedArray(taFull, i, 10 - i);
......@@ -1522,7 +1522,7 @@ LastIndexOfParameterConversionDetaches(ArrayLastIndexOfHelper);
const taFull = new ctor(rab, 0);
WriteUnsortedData(taFull);
fixedLength.sort(CustomComparison);
sortHelper(fixedLength, CustomComparison);
AssertIsDetached(fixedLength);
}
......@@ -1534,10 +1534,12 @@ LastIndexOfParameterConversionDetaches(ArrayLastIndexOfHelper);
const taFull = new ctor(rab, 0);
WriteUnsortedData(taFull);
lengthTracking.sort(CustomComparison);
sortHelper(lengthTracking, CustomComparison);
AssertIsDetached(lengthTracking);
}
})();
}
SortCallbackDetaches(TypedArraySortHelper);
SortCallbackDetaches(ArraySortHelper);
(function ObjectDefineProperty() {
for (let helper of
......
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