diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/src/tests/non262/TypedArray/indexOf-and-lastIndexOf.js | |
parent | Initial commit. (diff) | |
download | firefox-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/indexOf-and-lastIndexOf.js | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/js/src/tests/non262/TypedArray/indexOf-and-lastIndexOf.js b/js/src/tests/non262/TypedArray/indexOf-and-lastIndexOf.js new file mode 100644 index 0000000000..c179f78502 --- /dev/null +++ b/js/src/tests/non262/TypedArray/indexOf-and-lastIndexOf.js @@ -0,0 +1,123 @@ +// Tests for TypedArray#indexOf. +for (var constructor of anyTypedArrayConstructors) { + assertEq(constructor.prototype.indexOf.length, 1); + + // Works with one argument. + assertEq(new constructor([1, 2, 3, 4, 5]).indexOf(0), -1); + assertEq(new constructor([1, 2, 3, 4, 5]).indexOf(1), 0); + assertEq(new constructor([1, 2, 3, 4, 5]).indexOf(5), 4); + assertEq(new constructor([1, 2, 3, 4, 5]).indexOf(6), -1); + assertEq(new constructor([1, 2, 1, 2, 1]).indexOf(1), 0); + + if (isFloatConstructor(constructor)) { + assertEq(new constructor([NaN, 0, -0]).indexOf(NaN), -1); + assertEq(new constructor([NaN, 0, -0]).indexOf(0), 1); + assertEq(new constructor([NaN, 0, -0]).indexOf(-0), 1); + } else { + // [NaN, 0, -0] will be coerced to [0, 0, 0] + assertEq(new constructor([NaN, 0, -0]).indexOf(NaN), -1); + assertEq(new constructor([NaN, 0, -0]).indexOf(0), 0); + assertEq(new constructor([NaN, 0, -0]).indexOf(-0), 0); + } + + // Works with two arguments. + assertEq(new constructor([1, 2, 3, 4, 5]).indexOf(1, 1), -1); + assertEq(new constructor([1, 2, 3, 4, 5]).indexOf(1, -100), 0); + assertEq(new constructor([1, 2, 3, 4, 5]).indexOf(3, 100), -1); + assertEq(new constructor([1, 2, 3, 4, 5]).indexOf(5, -1), 4); + assertEq(new constructor([1, 2, 1, 2, 1]).indexOf(1, 2), 2); + assertEq(new constructor([1, 2, 1, 2, 1]).indexOf(1, -2), 4); + + // Throws if `this` isn't a TypedArray. + var invalidReceivers = [undefined, null, 1, false, "", Symbol(), [], {}, /./, + new Proxy(new constructor(), {})]; + invalidReceivers.forEach(invalidReceiver => { + assertThrowsInstanceOf(() => { + constructor.prototype.indexOf.call(invalidReceiver); + }, TypeError, "Assert that indexOf fails if this value is not a TypedArray"); + }); + + // test that this.length is never called + assertEq(Object.defineProperty(new constructor([0, 1, 2, 3, 5]), "length", { + get() { + throw new Error("length accessor called"); + } + }).indexOf(1), 1); +} + +for (let constructor of anyTypedArrayConstructors.filter(isFloatConstructor)) { + if (constructor.BYTES_PER_ELEMENT === 4) { + assertEq(new constructor([.1, .2, .3]).indexOf(.2), -1); + assertEq(new constructor([.1, .2, .3]).indexOf(Math.fround(.2)), 1); + } else { + assertEq(constructor.BYTES_PER_ELEMENT, 8); + assertEq(new constructor([.1, .2, .3]).indexOf(.2), 1); + } +} + +// Tests for TypedArray#lastIndexOf. +for (var constructor of anyTypedArrayConstructors) { + + assertEq(constructor.prototype.lastIndexOf.length, 1); + + // Works with one arguments. + assertEq(new constructor([1, 2, 3, 4, 5]).lastIndexOf(0), -1); + assertEq(new constructor([1, 2, 3, 4, 5]).lastIndexOf(1), 0); + assertEq(new constructor([1, 2, 3, 4, 5]).lastIndexOf(5), 4); + assertEq(new constructor([1, 2, 3, 4, 5]).lastIndexOf(6), -1); + assertEq(new constructor([1, 2, 1, 2, 1]).lastIndexOf(1), 4); + + if (isFloatConstructor(constructor)) { + assertEq(new constructor([NaN, 0, -0]).lastIndexOf(NaN), -1); + assertEq(new constructor([NaN, 0, -0]).lastIndexOf(0), 2); + assertEq(new constructor([NaN, 0, -0]).lastIndexOf(-0), 2); + } else { + // [NaN, 0, -0] will be coerced to [0, 0, 0]. + assertEq(new constructor([NaN, 0, -0]).lastIndexOf(NaN), -1); + assertEq(new constructor([NaN, 0, -0]).lastIndexOf(0), 2); + assertEq(new constructor([NaN, 0, -0]).lastIndexOf(-0), 2); + } + + // Works with two arguments. + assertEq(new constructor([1, 2, 3, 4, 5]).lastIndexOf(1, 1), 0); + assertEq(new constructor([1, 2, 3, 4, 5]).lastIndexOf(1, -100), -1); + assertEq(new constructor([1, 2, 3, 4, 5]).lastIndexOf(3, 100), 2); + assertEq(new constructor([1, 2, 3, 4, 5]).lastIndexOf(5, -1), 4); + assertEq(new constructor([1, 2, 1, 2, 1]).lastIndexOf(1, 2), 2); + assertEq(new constructor([1, 2, 1, 2, 1]).lastIndexOf(1, -2), 2); + + // Throws if `this` isn't a TypedArray. + var invalidReceivers = [undefined, null, 1, false, "", Symbol(), [], {}, /./, + new Proxy(new constructor(), {})]; + invalidReceivers.forEach(invalidReceiver => { + assertThrowsInstanceOf(() => { + constructor.prototype.lastIndexOf.call(invalidReceiver); + }, TypeError, "Assert that lastIndexOf fails if this value is not a TypedArray"); + }); + + // Test that the length getter is never called. + assertEq(Object.defineProperty(new constructor([0, 1, 2, 3, 5]), "length", { + get() { + throw new Error("length accessor called"); + } + }).lastIndexOf(1), 1); + + // Starts search at last index when fromIndex parameter is absent. + assertEq(new constructor([10, 20, 10]).lastIndexOf(10), 2); + + // Starts search at first index when fromIndex parameter is undefined. + assertEq(new constructor([10, 20, 10]).lastIndexOf(10, undefined), 0); +} + +for (let constructor of anyTypedArrayConstructors.filter(isFloatConstructor)) { + if (constructor.BYTES_PER_ELEMENT === 4) { + assertEq(new constructor([.1, .2, .3]).lastIndexOf(.2), -1); + assertEq(new constructor([.1, .2, .3]).lastIndexOf(Math.fround(.2)), 1); + } else { + assertEq(constructor.BYTES_PER_ELEMENT, 8); + assertEq(new constructor([.1, .2, .3]).lastIndexOf(.2), 1); + } +} + +if (typeof reportCompare === "function") + reportCompare(true, true); |