summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authormjz19910 <matthias291999@gmail.com>2022-03-12 20:22:21 -0700
committerLinus Groh <mail@linusgroh.de>2022-03-13 16:49:25 +0100
commitfd8a56cdded113702735818e23b27f40ab4795e4 (patch)
treeabe9792aff7f2bd56195b3f42ccbe165f0e61ea8 /Userland/Libraries/LibJS
parent13c253d2ee35444d33300c4883746784a03ff124 (diff)
downloadserenity-fd8a56cdded113702735818e23b27f40ab4795e4.zip
LibJS: Add some tests for TypedArray.prototype.set
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.set.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.set.js b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.set.js
index 57495cf666..85e19c1e69 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.set.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.set.js
@@ -47,4 +47,56 @@ describe("normal behavior", () => {
expect(secondTypedArray[1]).toBe(maxUnsignedInteger);
});
});
+
+ test("set works when source is TypedArray", () => {
+ function argumentTests({ array, maxUnsignedInteger }) {
+ const firstTypedArray = new array(1);
+ const secondTypedArray = new array([maxUnsignedInteger]);
+ firstTypedArray.set(secondTypedArray, 0);
+ expect(firstTypedArray[0]).toBe(maxUnsignedInteger);
+ }
+
+ TYPED_ARRAYS.forEach(T => argumentTests(T));
+ BIGINT_TYPED_ARRAYS.forEach(T => argumentTests(T));
+ });
+
+ test("set works when source is Array", () => {
+ function argumentTests({ array, maxUnsignedInteger }) {
+ const firstTypedArray = new array(1);
+ firstTypedArray.set([maxUnsignedInteger], 0);
+ expect(firstTypedArray[0]).toBe(maxUnsignedInteger);
+ }
+
+ TYPED_ARRAYS.forEach(T => argumentTests(T));
+ BIGINT_TYPED_ARRAYS.forEach(T => argumentTests(T));
+ });
+});
+
+test("length is 1", () => {
+ TYPED_ARRAYS.forEach(({ array: T }) => {
+ expect(T.prototype.set).toHaveLength(1);
+ });
+
+ BIGINT_TYPED_ARRAYS.forEach(({ array: T }) => {
+ expect(T.prototype.set).toHaveLength(1);
+ });
+});
+
+describe("errors", () => {
+ function argumentErrorTests(T) {
+ test(`requires at least one argument (${T.name})`, () => {
+ expect(() => {
+ new T().set();
+ }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
+ });
+
+ test(`source array in bounds (${T.name})`, () => {
+ expect(() => {
+ new T().set([0]);
+ }).toThrowWithMessage(RangeError, "Overflow or out of bounds in target length");
+ });
+ }
+
+ TYPED_ARRAYS.forEach(({ array: T }) => argumentErrorTests(T));
+ BIGINT_TYPED_ARRAYS.forEach(({ array: T }) => argumentErrorTests(T));
});