diff options
author | Obinna Ikeh <hikenike6@gmail.com> | 2022-06-29 07:52:06 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-06-29 13:36:54 +0100 |
commit | 104330d1a006829206ce9201084efc7dba346eaf (patch) | |
tree | 9e0ed075ed4b87fb91e3c1381fb11821d7370529 | |
parent | 4dbb2c2d44d26983ee7a08d9fe22df5f7188e794 (diff) | |
download | serenity-104330d1a006829206ce9201084efc7dba346eaf.zip |
LibJS: Add tests cases for %TypedArray%.prototype.toSorted function
-rw-r--r-- | Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.toSorted.js | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.toSorted.js b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.toSorted.js new file mode 100644 index 0000000000..461acb1ca7 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.toSorted.js @@ -0,0 +1,84 @@ +const TYPED_ARRAYS = [ + Uint8Array, + Uint8ClampedArray, + Uint16Array, + Uint32Array, + Int8Array, + Int16Array, + Int32Array, + Float32Array, + Float64Array, +]; + +const BIGINT_TYPED_ARRAYS = [BigUint64Array, BigInt64Array]; + +test("basic functionality", () => { + TYPED_ARRAYS.forEach(T => { + expect(T.prototype.toSorted).toHaveLength(1); + + const typedArray = new T([3, 1, 2]); + let sortedtypedArray = typedArray.toSorted(); + expect(sortedtypedArray).not.toBe(typedArray); + expect(sortedtypedArray[0]).toBe(1); + expect(sortedtypedArray[1]).toBe(2); + expect(sortedtypedArray[2]).toBe(3); + + sortedtypedArray = typedArray.toSorted(() => 0); + expect(sortedtypedArray).not.toBe(typedArray); + expect(sortedtypedArray[0]).toBe(3); + expect(sortedtypedArray[1]).toBe(1); + expect(sortedtypedArray[2]).toBe(2); + }); + + BIGINT_TYPED_ARRAYS.forEach(T => { + expect(T.prototype.toSorted).toHaveLength(1); + + const typedArray = new T([3n, 1n, 2n]); + + let sortedtypedArray = typedArray.toSorted(); + expect(sortedtypedArray).not.toBe(typedArray); + expect(sortedtypedArray[0]).toBe(1n); + expect(sortedtypedArray[1]).toBe(2n); + expect(sortedtypedArray[2]).toBe(3n); + + sortedtypedArray = typedArray.toSorted(() => 0); + expect(sortedtypedArray).not.toBe(typedArray); + expect(sortedtypedArray[0]).toBe(3n); + expect(sortedtypedArray[1]).toBe(1n); + expect(sortedtypedArray[2]).toBe(2n); + }); +}); + +describe("errors", () => { + test("null or undefined this value", () => { + TYPED_ARRAYS.forEach(T => { + expect(() => { + T.prototype.toSorted.call(); + }).toThrowWithMessage(TypeError, "ToObject on null or undefined"); + + expect(() => { + T.prototype.toSorted.call(undefined); + }).toThrowWithMessage(TypeError, "ToObject on null or undefined"); + + expect(() => { + T.prototype.toSorted.call(null); + }).toThrowWithMessage(TypeError, "ToObject on null or undefined"); + }); + + BIGINT_TYPED_ARRAYS.forEach(T => {}); + }); + + test("invalid compare function", () => { + TYPED_ARRAYS.forEach(T => { + expect(() => { + new T([]).toSorted("foo"); + }).toThrowWithMessage(TypeError, "foo is not a function"); + }); + + BIGINT_TYPED_ARRAYS.forEach(T => { + expect(() => { + new T([]).toSorted("foo"); + }).toThrowWithMessage(TypeError, "foo is not a function"); + }); + }); +}); |