summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/TypedArray/sort_modifications.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/src/tests/non262/TypedArray/sort_modifications.js
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--js/src/tests/non262/TypedArray/sort_modifications.js73
1 files changed, 73 insertions, 0 deletions
diff --git a/js/src/tests/non262/TypedArray/sort_modifications.js b/js/src/tests/non262/TypedArray/sort_modifications.js
new file mode 100644
index 0000000000..2a6ed49eb7
--- /dev/null
+++ b/js/src/tests/non262/TypedArray/sort_modifications.js
@@ -0,0 +1,73 @@
+const TAConstructors = [
+ Int8Array,
+ Uint8Array,
+ Int16Array,
+ Uint16Array,
+ Int32Array,
+ Uint32Array,
+ Uint8ClampedArray,
+ Float32Array,
+ Float64Array,
+ BigInt64Array,
+ BigUint64Array,
+];
+
+// Use different size classes to catch any implementation-specific
+// optimisations.
+const sizes = [
+ 4, 8, 64, 128, 1024
+];
+
+function ToNumeric(TA) {
+ if (TA === BigInt64Array || TA === BigUint64Array) {
+ return BigInt;
+ }
+ return Number;
+}
+
+function ascending(a, b) {
+ return a < b ? -1 : a > b ? 1 : 0;
+}
+
+function descending(a, b) {
+ return -ascending(a, b);
+}
+
+for (let TA of TAConstructors) {
+ let toNumeric = ToNumeric(TA);
+ for (let size of sizes) {
+ let sorted = new TA(size);
+
+ // Fill with |1..size| and then sort to account for wrap-arounds.
+ for (let i = 0; i < size; ++i) {
+ sorted[i] = toNumeric(i + 1);
+ }
+ sorted.sort();
+
+ // Create a copy in descending order.
+ let ta = new TA(sorted);
+ ta.sort(descending);
+
+ // Sort the copy in ascending order and on the first call reset all
+ // elements to zero.
+ let called = false;
+ ta.sort(function(a, b) {
+ if (!called) {
+ called = true;
+ ta.fill(toNumeric(0));
+ }
+ return ascending(a, b);
+ });
+
+ // Ensure the comparator function was called.
+ assertEq(called, true);
+
+ // All elements should be sorted correctly. No elements should be zero.
+ for (let i = 0; i < size; ++i) {
+ assertEq(ta[i], sorted[i], `${TA.name} at index ${i} for size ${size}`);
+ }
+ }
+}
+
+if (typeof reportCompare === "function")
+ reportCompare(true, true);