summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Tuple/prototype/toSorted
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/Tuple/prototype/toSorted
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/Tuple/prototype/toSorted/comparefn-call-throws.js16
-rw-r--r--js/src/tests/non262/Tuple/prototype/toSorted/comparefn-calls.js25
-rw-r--r--js/src/tests/non262/Tuple/prototype/toSorted/comparefn-nonfunction-call-throws.js25
-rw-r--r--js/src/tests/non262/Tuple/prototype/toSorted/invoked-as-func.js10
-rw-r--r--js/src/tests/non262/Tuple/prototype/toSorted/invoked-as-method.js10
-rw-r--r--js/src/tests/non262/Tuple/prototype/toSorted/length.js27
-rw-r--r--js/src/tests/non262/Tuple/prototype/toSorted/sorted-values.js29
-rw-r--r--js/src/tests/non262/Tuple/prototype/toSorted/stability.js32
-rw-r--r--js/src/tests/non262/Tuple/prototype/toSorted/this-is-not-tuple.js21
-rw-r--r--js/src/tests/non262/Tuple/prototype/toSorted/toSorted.js36
-rw-r--r--js/src/tests/non262/Tuple/prototype/toSorted/toSortedcompare-with-no-tostring.js14
-rw-r--r--js/src/tests/non262/Tuple/prototype/toSorted/tuplelength-internal.js28
12 files changed, 273 insertions, 0 deletions
diff --git a/js/src/tests/non262/Tuple/prototype/toSorted/comparefn-call-throws.js b/js/src/tests/non262/Tuple/prototype/toSorted/comparefn-call-throws.js
new file mode 100644
index 0000000000..4442b0d3d5
--- /dev/null
+++ b/js/src/tests/non262/Tuple/prototype/toSorted/comparefn-call-throws.js
@@ -0,0 +1,16 @@
+// |reftest| skip-if(!this.hasOwnProperty("Tuple"))
+let sample = #[42, 43, 44, 45, 46];
+var calls = 0;
+
+let comparefn = function() {
+ calls += 1;
+ throw new TypeError();
+};
+
+assertThrowsInstanceOf(function() {
+ sample.toSorted(comparefn);
+}, TypeError, "comparefn should throw");
+
+assertEq(calls, 1);
+
+reportCompare(0, 0);
diff --git a/js/src/tests/non262/Tuple/prototype/toSorted/comparefn-calls.js b/js/src/tests/non262/Tuple/prototype/toSorted/comparefn-calls.js
new file mode 100644
index 0000000000..e8d65b9445
--- /dev/null
+++ b/js/src/tests/non262/Tuple/prototype/toSorted/comparefn-calls.js
@@ -0,0 +1,25 @@
+// |reftest| skip-if(!this.hasOwnProperty("Tuple"))
+var expectedThis = (function() {
+ return this;
+})();
+
+
+var sample = #[42, 42, 42, 42, 42];
+var calls = [];
+
+var comparefn = function() {
+ calls.push([this, arguments]);
+};
+
+let result = sample.toSorted(comparefn);
+
+assertEq(calls.length > 0, true);
+
+calls.forEach(function(args) {
+ assertEq(args[0], expectedThis);
+ assertEq(args[1].length, 2);
+ assertEq(args[1][0], 42);
+ assertEq(args[1][0], 42);
+});
+
+reportCompare(0, 0);
diff --git a/js/src/tests/non262/Tuple/prototype/toSorted/comparefn-nonfunction-call-throws.js b/js/src/tests/non262/Tuple/prototype/toSorted/comparefn-nonfunction-call-throws.js
new file mode 100644
index 0000000000..5a68e1d343
--- /dev/null
+++ b/js/src/tests/non262/Tuple/prototype/toSorted/comparefn-nonfunction-call-throws.js
@@ -0,0 +1,25 @@
+// |reftest| skip-if(!this.hasOwnProperty("Tuple"))
+/*
+ 1. If _comparefn_ is not *undefined* and IsCallable(_comparefn_) is *false*, throw a *TypeError* exception.
+ ...
+*/
+
+let sample = #[42, 43, 44, 45, 46];
+
+let compareFnVals = [[null, "null"],
+ [true, "true"],
+ [false, "false"],
+ ['', "\'\'"],
+ [/a/g, "/a/g"],
+ [42, "42"],
+ [[], "[]"],
+ [{}, "{}"]];
+
+for (pair of compareFnVals) {
+ f = pair[0];
+ errorMsg = "comparefn is: " + pair[1];
+ assertThrowsInstanceOf(() => sample.withSorted(f),
+ TypeError, errorMsg);
+}
+
+reportCompare(0, 0);
diff --git a/js/src/tests/non262/Tuple/prototype/toSorted/invoked-as-func.js b/js/src/tests/non262/Tuple/prototype/toSorted/invoked-as-func.js
new file mode 100644
index 0000000000..da68b8b8fc
--- /dev/null
+++ b/js/src/tests/non262/Tuple/prototype/toSorted/invoked-as-func.js
@@ -0,0 +1,10 @@
+// |reftest| skip-if(!this.hasOwnProperty("Tuple"))
+var toSorted = Tuple.prototype.toSorted;
+
+assertEq(typeof toSorted, 'function');
+
+assertThrowsInstanceOf(function() {
+ toSorted();
+}, TypeError, "toSorted invoked as function");
+
+reportCompare(0, 0);
diff --git a/js/src/tests/non262/Tuple/prototype/toSorted/invoked-as-method.js b/js/src/tests/non262/Tuple/prototype/toSorted/invoked-as-method.js
new file mode 100644
index 0000000000..86fc2e713b
--- /dev/null
+++ b/js/src/tests/non262/Tuple/prototype/toSorted/invoked-as-method.js
@@ -0,0 +1,10 @@
+// |reftest| skip-if(!this.hasOwnProperty("Tuple"))
+var TuplePrototype = Tuple.prototype;
+
+assertEq(typeof TuplePrototype.toSorted, 'function');
+
+assertThrowsInstanceOf(function() {
+ TuplePrototype.toSorted();
+}, TypeError, "toSorted() invoked as method");
+
+reportCompare(0, 0);
diff --git a/js/src/tests/non262/Tuple/prototype/toSorted/length.js b/js/src/tests/non262/Tuple/prototype/toSorted/length.js
new file mode 100644
index 0000000000..7a94575ce6
--- /dev/null
+++ b/js/src/tests/non262/Tuple/prototype/toSorted/length.js
@@ -0,0 +1,27 @@
+// |reftest| skip-if(!this.hasOwnProperty("Tuple"))
+var desc = Object.getOwnPropertyDescriptor(Tuple.prototype.toSorted, "length");
+assertEq(desc.value, 1);
+assertEq(desc.writable, false);
+assertEq(desc.enumerable, false);
+assertEq(desc.configurable, true);
+
+
+desc = Object.getOwnPropertyDescriptor(Tuple.prototype.toSorted, "name");
+assertEq(desc.value, "toSorted");
+assertEq(desc.writable, false);
+assertEq(desc.enumerable, false);
+assertEq(desc.configurable, true);
+
+desc = Object.getOwnPropertyDescriptor(Tuple.prototype, "toSorted");
+assertEq(desc.writable, true);
+assertEq(desc.enumerable, false);
+assertEq(desc.configurable, true);
+
+assertEq(isConstructor(Tuple.prototype.toSorted), false);
+
+assertThrowsInstanceOf(() => {
+ let t = #[1];
+ new t.toSorted();
+}, TypeError, '`let t = #[1]; new t.toSorted()` throws TypeError');
+
+reportCompare(0, 0);
diff --git a/js/src/tests/non262/Tuple/prototype/toSorted/sorted-values.js b/js/src/tests/non262/Tuple/prototype/toSorted/sorted-values.js
new file mode 100644
index 0000000000..ecdd349145
--- /dev/null
+++ b/js/src/tests/non262/Tuple/prototype/toSorted/sorted-values.js
@@ -0,0 +1,29 @@
+// |reftest| skip-if(!this.hasOwnProperty("Tuple"))
+var sample;
+
+sample = #[4,3,2,1].toSorted();
+assertEq(sample, #[1,2,3,4]);
+
+sample = #[3, 4, 1, 2].toSorted();
+assertEq(sample, #[1, 2, 3, 4]);
+
+sample = #[3,4,3,1,0,1,2].toSorted();
+assertEq(sample, #[0,1,1,2,3,3,4]);
+
+sample = #[1,0,-0,2].toSorted();
+// This matches the behavior of Array.sort()
+assertEq(sample, #[0,-0,1,2]);
+
+sample = #[-4,3,4,-3,2,-1,1,0].toSorted();
+assertEq(sample, #[-1,-3,-4,0,1,2,3,4]);
+
+sample = #[0.5,0,1.5,1].toSorted();
+assertEq(sample, #[0,0.5,1,1.5]);
+
+sample = #[0.5,0,1.5,-0.5,-1,-1.5,1].toSorted();
+assertEq(sample, #[-0.5,-1,-1.5,0,0.5,1,1.5]);
+
+sample = #[3,4,Infinity,-Infinity,1,2].toSorted();
+assertEq(sample, #[-Infinity,1,2,3,4,Infinity]);
+
+reportCompare(0, 0);
diff --git a/js/src/tests/non262/Tuple/prototype/toSorted/stability.js b/js/src/tests/non262/Tuple/prototype/toSorted/stability.js
new file mode 100644
index 0000000000..1b2d0eb193
--- /dev/null
+++ b/js/src/tests/non262/Tuple/prototype/toSorted/stability.js
@@ -0,0 +1,32 @@
+// |reftest| skip-if(!this.hasOwnProperty("Tuple"))
+// Treat 0..3, 4..7, etc. as equal.
+const compare = (a, b) => (a / 4 | 0) - (b / 4 | 0);
+
+// Create a tuple of the form `[0, 1, …, 126, 127]`.
+var tup = Tuple.from({ length: 128 }, (_, i) => i);
+
+const tuple1 = tup;
+assertEq(tuple1.toSorted(compare), tup);
+
+
+// Reverse `tup` so it becomes `[127, 126, …, 1, 0]`.
+tup = tup.toReversed();
+
+const tuple2 = tup;
+assertEq(tuple2.toSorted(compare),
+ #[
+ 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8,
+ 15, 14, 13, 12, 19, 18, 17, 16, 23, 22, 21, 20,
+ 27, 26, 25, 24, 31, 30, 29, 28, 35, 34, 33, 32,
+ 39, 38, 37, 36, 43, 42, 41, 40, 47, 46, 45, 44,
+ 51, 50, 49, 48, 55, 54, 53, 52, 59, 58, 57, 56,
+ 63, 62, 61, 60, 67, 66, 65, 64, 71, 70, 69, 68,
+ 75, 74, 73, 72, 79, 78, 77, 76, 83, 82, 81, 80,
+ 87, 86, 85, 84, 91, 90, 89, 88, 95, 94, 93, 92,
+ 99, 98, 97, 96, 103, 102, 101, 100, 107, 106, 105, 104,
+ 111, 110, 109, 108, 115, 114, 113, 112, 119, 118, 117, 116,
+ 123, 122, 121, 120, 127, 126, 125, 124,
+ ]);
+
+reportCompare(0, 0);
+
diff --git a/js/src/tests/non262/Tuple/prototype/toSorted/this-is-not-tuple.js b/js/src/tests/non262/Tuple/prototype/toSorted/this-is-not-tuple.js
new file mode 100644
index 0000000000..3d1761f08f
--- /dev/null
+++ b/js/src/tests/non262/Tuple/prototype/toSorted/this-is-not-tuple.js
@@ -0,0 +1,21 @@
+// |reftest| skip-if(!this.hasOwnProperty("Tuple"))
+var withSorted = Tuple.prototype.withSorted;
+
+var thisVals = [[undefined, "undefined"],
+ [null, "null"],
+ [42, "42"],
+ ["1", "1"],
+ [true, "true"],
+ [false, "false"],
+ [Symbol("s"), "Symbol(\"s\")"],
+ [[], "[]"],
+ [{}, "{}"]];
+
+for (pair of thisVals) {
+ thisVal = pair[0];
+ errorMsg = "this is: " + pair[1];
+ assertThrowsInstanceOf(() => withSorted.call(thisVal),
+ TypeError, errorMsg);
+}
+
+reportCompare(0, 0);
diff --git a/js/src/tests/non262/Tuple/prototype/toSorted/toSorted.js b/js/src/tests/non262/Tuple/prototype/toSorted/toSorted.js
new file mode 100644
index 0000000000..c2d90f06f6
--- /dev/null
+++ b/js/src/tests/non262/Tuple/prototype/toSorted/toSorted.js
@@ -0,0 +1,36 @@
+// |reftest| skip-if(!this.hasOwnProperty("Tuple"))
+/*
+8.2.3.29 Tuple.prototype.toSorted ( compareFn )
+Except as described below, implements the same requirements as those of Array.prototype.sort as defined in 22.1.3.27. The implementation may be optimized with the knowledge that the this value is an object that has a fixed length and whose integer-indexed properties are not sparse.
+
+Upon entry, the following steps are performed to initialize evaluation of the sorted function. These steps are used instead of the entry steps in 22.1.3.27:
+
+1. If comparefn is not undefined and IsCallable(comparefn) is false, throw a TypeError exception.
+2. Let T be ? thisTupleValue(this value).
+3. Let list be a new List containing the elements of T.[[Sequence]].
+4. Let len be the length of list.
+A new Tuple containing the elements from the original Tuple is returned, where the elements are sorted. The sort order is the ordering, after completion of this function, of the elements of list. The sort order of the result of sorted is the same order as if the elements of list were sorted in an Array via %Array.prototype.sort% with comparefn as the first argument.
+
+*/
+/* Step 2 */
+/* toSorted() should throw on a non-Tuple */
+let method = Tuple.prototype.toSorted;
+assertEq(method.call(#[6,5,4,3,2,1]), #[1,2,3,4,5,6]);
+assertEq(method.call(Object(#[6,5,4,3,2,1])), #[1,2,3,4,5,6]);
+assertThrowsInstanceOf(() => method.call("monkeys"), TypeError,
+ "value of TupleObject must be a Tuple");
+
+/* Test that length is still handled correctly if it's overridden */
+Object.defineProperty(Tuple.prototype, "length", { get() { return 0 } })
+assertEq(#[5,4,3,2,1].toSorted(), #[1,2,3,4,5]);
+
+/* Step 1. If comparefn is not undefined... */
+assertEq(#[5,4,3,2,1].toSorted(), #[1,2,3,4,5]);
+assertEq(#[5,4,3,2,1].toSorted(undefined), #[1,2,3,4,5]);
+assertThrowsInstanceOf(() => #[1].toSorted("monkeys"), TypeError,
+ "bad comparefn passed to toSorted");
+let comparefn = (x, y) => x < y;
+assertEq(#[1,2,3,4,5].toSorted(comparefn), #[5,4,3,2,1]);
+
+reportCompare(0, 0);
+
diff --git a/js/src/tests/non262/Tuple/prototype/toSorted/toSortedcompare-with-no-tostring.js b/js/src/tests/non262/Tuple/prototype/toSorted/toSortedcompare-with-no-tostring.js
new file mode 100644
index 0000000000..68b5774d61
--- /dev/null
+++ b/js/src/tests/non262/Tuple/prototype/toSorted/toSortedcompare-with-no-tostring.js
@@ -0,0 +1,14 @@
+// |reftest| skip-if(!this.hasOwnProperty("Tuple"))
+/* description: Tuple toSorted does cast values to String */
+
+var toStringCalled = false;
+Number.prototype.toString = function() {
+ toStringCalled = true;
+}
+
+let sample = #[20, 100, 3];
+let result = sample.toSorted();
+assertEq(toStringCalled, false);
+assertEq(result, #[100, 20, 3]);
+
+reportCompare(0, 0);
diff --git a/js/src/tests/non262/Tuple/prototype/toSorted/tuplelength-internal.js b/js/src/tests/non262/Tuple/prototype/toSorted/tuplelength-internal.js
new file mode 100644
index 0000000000..8353eaea11
--- /dev/null
+++ b/js/src/tests/non262/Tuple/prototype/toSorted/tuplelength-internal.js
@@ -0,0 +1,28 @@
+// |reftest| skip-if(!this.hasOwnProperty("Tuple"))
+/* Use internal length rather than getting a length property */
+var getCalls = 0;
+var desc = {
+ get: function getLen() {
+ getCalls++;
+ return 0;
+ }
+};
+
+var internalLength = Object.getOwnPropertyDescriptor(Tuple.prototype, "length").get;
+Object.defineProperty(Tuple.prototype, "length", desc);
+
+let sample = #[42,42,42];
+
+getCalls = 0;
+
+var result = sample.toSorted();
+
+assertEq(getCalls, 0)
+assertEq(result, sample);
+assertEq(result[0], sample[0]);
+assertEq(result[1], sample[1]);
+assertEq(result[2], sample[2]);
+assertEq(result.length, 0);
+assertEq(internalLength.call(result), 3);
+
+reportCompare(0, 0);