summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmjad Alsharafi <amjadsharafi10@gmail.com>2021-04-07 15:09:53 +0300
committerAndreas Kling <kling@serenityos.org>2021-04-09 09:05:19 +0200
commitb75865484073cabc05fd783d6e4c24383877a542 (patch)
treef6d11fea92473788b1cb892b01c8b2885bcb9fe5
parent5d445444014cd27ed9a57977aebee495e2e59646 (diff)
downloadserenity-b75865484073cabc05fd783d6e4c24383877a542.zip
LibJS: Added tests for constructing TypeArray from another
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js65
1 files changed, 65 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js
index d443d13cd6..efa7e49be5 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js
@@ -118,6 +118,71 @@ test("typed array from ArrayBuffer errors", () => {
);
});
+test("typed array from TypedArray", () => {
+ const u8Array = new Uint8Array(3);
+ u8Array[0] = 1;
+ u8Array[1] = 2;
+ u8Array[2] = 3;
+
+ TYPED_ARRAYS.forEach(T => {
+ const newTypedArray = new T(u8Array);
+ expect(newTypedArray[0]).toBe(1);
+ expect(newTypedArray[1]).toBe(2);
+ expect(newTypedArray[2]).toBe(3);
+ });
+});
+
+test("typed array from TypedArray element cast", () => {
+ const u32Array = new Uint32Array(2);
+ u32Array[0] = 0x100;
+ u32Array[1] = 0xff;
+ const u8Array = new Uint8Array(1);
+ u8Array[0] = 0xff;
+
+ const u32Expected = [
+ [0, 0xff],
+ [0x100, 0xff],
+ [0x100, 0xff],
+ [0, -1],
+ [0x100, 0xff],
+ [0x100, 0xff],
+ [0x100, 0xff],
+ [0x100, 0xff],
+ ];
+ const u8Expected = [0xff, 0xff, 0xff, -1, 0xff, 0xff, 0xff, 0xff];
+
+ TYPED_ARRAYS.forEach((T, i) => {
+ const newArrFromU32 = new T(u32Array);
+ expect(newArrFromU32[0]).toBe(u32Expected[i][0]);
+ expect(newArrFromU32[1]).toBe(u32Expected[i][1]);
+
+ const newArrFromU8 = new T(u8Array);
+ expect(newArrFromU8[0]).toBe(u8Expected[i]);
+ });
+});
+
+test("typed array created from TypedArray do not share buffer", () => {
+ const u8Array = new Uint8Array(2);
+ u8Array[0] = 1;
+ u8Array[1] = 2;
+
+ const u8Array2 = new Uint8Array(u8Array);
+ u8Array2[0] = 3;
+ u8Array2[1] = 4;
+
+ expect(u8Array[0]).toBe(1);
+ expect(u8Array[1]).toBe(2);
+
+ const i32Array = new Int32Array(u8Array);
+ expect(i32Array[0]).toBe(1);
+ expect(i32Array[1]).toBe(2);
+
+ i32Array[0] = 5;
+ i32Array[1] = 6;
+ expect(u8Array[0]).toBe(1);
+ expect(u8Array[1]).toBe(2);
+});
+
test("TypedArray is not exposed on the global object", () => {
expect(globalThis.TypedArray).toBeUndefined();
});