diff --git a/deno.lock b/deno.lock index 55b8a59..61f4b55 100644 --- a/deno.lock +++ b/deno.lock @@ -8,7 +8,8 @@ }, "npm": { "specifiers": { - "wabt": "wabt@1.0.32" + "wabt": "wabt@1.0.32", + "wabt@1.0.32": "wabt@1.0.32" }, "packages": { "wabt@1.0.32": { diff --git a/types/array/array_buffer_test.ts b/types/array/array_buffer_test.ts new file mode 100644 index 0000000..aa74c30 --- /dev/null +++ b/types/array/array_buffer_test.ts @@ -0,0 +1,22 @@ +import { ArrayBufferType } from "./array_buffer.ts"; +import { assertEquals } from "std/testing/asserts.ts"; + +Deno.test("Array Buffer", async ({ step }) => { + const ab = new ArrayBuffer(4); + const dt = new DataView(ab); + const byteView = new Uint8Array(ab); + const abType = new ArrayBufferType(4); + + await step("read", () => { + byteView.set([1, 2, 3, 4]); + const newAB = abType.read(dt); + assertEquals(new Uint8Array(newAB), byteView); + }); + + await step("write", () => { + byteView.set([0, 0, 0, 0]); + abType.write(Uint8Array.of(1, 2, 3, 4).buffer, dt); + + assertEquals(byteView, Uint8Array.of(1, 2, 3, 4)); + }); +}); diff --git a/types/array/array_test.ts b/types/array/array_test.ts new file mode 100644 index 0000000..5595173 --- /dev/null +++ b/types/array/array_test.ts @@ -0,0 +1,20 @@ +import { assertEquals } from "https://deno.land/std@0.184.0/testing/asserts.ts"; +import { u8 } from "../primitive/u8.ts"; +import { ArrayType } from "./array.ts"; + +Deno.test("Array Type", async ({ step }) => { + const ab = new ArrayBuffer(2); + const dt = new DataView(ab); + const byteView = new Uint8Array(ab); + const byteArray = new ArrayType(u8, 2); + + await step("read", () => { + byteView.set([1, 2]); + assertEquals(byteArray.read(dt), [1, 2]); + }); + + await step("write", () => { + byteArray.write([3, 4], dt); + assertEquals(byteView, Uint8Array.of(3, 4)); + }); +}); diff --git a/types/array/typed_array.ts b/types/array/typed_array.ts index b526dd3..b05c6a7 100644 --- a/types/array/typed_array.ts +++ b/types/array/typed_array.ts @@ -44,7 +44,7 @@ export class TypedArrayType write(value: T, dataView: DataView, byteOffset = 0) { // @ts-ignore Sets the dataView buffer to the value - new this.Constructor(dataView, byteOffset).set(value); + new this.Constructor(dataView.buffer, byteOffset).set(value); } view(dataView: DataView, byteOffset = 0): T { diff --git a/types/array/typed_array_test.ts b/types/array/typed_array_test.ts new file mode 100644 index 0000000..48ba6cd --- /dev/null +++ b/types/array/typed_array_test.ts @@ -0,0 +1,18 @@ +import { assertEquals } from "std/testing/asserts.ts"; +import { TypedArrayType } from "./typed_array.ts"; + +Deno.test("TypedArray", async ({ step }) => { + const ab = new ArrayBuffer(2); + const byteView = new Uint8Array(ab); + const dt = new DataView(ab); + const typedArray = new TypedArrayType(Uint8Array, 2); + await step("read", () => { + byteView.set([1, 2]); + assertEquals(typedArray.read(dt), Uint8Array.of(1, 2)); + }); + + await step("write", () => { + typedArray.write(Uint8Array.of(3, 4), dt); + assertEquals(byteView, Uint8Array.of(3, 4)); + }); +}); diff --git a/types/bitflags/bitflags16_test.ts b/types/bitflags/bitflags16_test.ts new file mode 100644 index 0000000..c84dc7a --- /dev/null +++ b/types/bitflags/bitflags16_test.ts @@ -0,0 +1,69 @@ +import { BitFlags16 } from "./bitflags16.ts"; +import { assertEquals } from "std/testing/asserts.ts"; + +Deno.test("bitflags16", async ({ step }) => { + const buff = new ArrayBuffer(2); + const dt = new DataView(buff); + const bitflags16 = new BitFlags16({ + 1: 1 << 8, + 2: 1 << 9, + 3: 1 << 10, + 4: 1 << 11, + 5: 1 << 12, + 6: 1 << 13, + 7: 1 << 14, + 8: 1 << 15, + }); + + await step("read", () => { + dt.setUint16(0, 0b1111111100000000); + assertEquals(bitflags16.read(dt), { + 1: true, + 2: true, + 3: true, + 4: true, + 5: true, + 6: true, + 7: true, + 8: true, + }); + + dt.setUint16(0, 0b0000000100000000); + assertEquals(bitflags16.read(dt), { + 1: true, + 2: false, + 3: false, + 4: false, + 5: false, + 6: false, + 7: false, + 8: false, + }); + }); + + await step("write", () => { + bitflags16.write({ + 1: true, + 2: true, + 3: true, + 4: true, + 5: true, + 6: true, + 7: true, + 8: true, + }, dt); + assertEquals(dt.getUint16(0), 0b1111111100000000); + + bitflags16.write({ + 1: true, + 2: false, + 3: false, + 4: false, + 5: false, + 6: false, + 7: false, + 8: false, + }, dt); + assertEquals(dt.getUint16(0), 0b0000000100000000); + }); +}); diff --git a/types/bitflags/bitflags32_test.ts b/types/bitflags/bitflags32_test.ts new file mode 100644 index 0000000..5cb5265 --- /dev/null +++ b/types/bitflags/bitflags32_test.ts @@ -0,0 +1,109 @@ +import { BitFlags32 } from "./bitflags32.ts"; +import { assertEquals } from "std/testing/asserts.ts"; + +Deno.test("bitflags32", async ({ step }) => { + const buff = new ArrayBuffer(4); + const dt = new DataView(buff); + const bitflags32 = new BitFlags32({ + 16: 0x10000, + 17: 0x20000, + 18: 0x40000, + 19: 0x80000, + 20: 0x100000, + 21: 0x200000, + 22: 0x400000, + 23: 0x800000, + 24: 0x1000000, + 25: 0x2000000, + 26: 0x4000000, + 27: 0x8000000, + 28: 0x10000000, + 29: 0x20000000, + 30: 0x40000000, + 31: 0x80000000, + }); + + await step("read", () => { + dt.setUint32(0, 0xFFFF0000); + assertEquals(bitflags32.read(dt), { + 16: true, + 17: true, + 18: true, + 19: true, + 20: true, + 21: true, + 22: true, + 23: true, + 24: true, + 25: true, + 26: true, + 27: true, + 28: true, + 29: true, + 30: true, + 31: true, + }); + + dt.setUint32(0, 0x10000); + assertEquals(bitflags32.read(dt), { + 16: true, + 17: false, + 18: false, + 19: false, + 20: false, + 21: false, + 22: false, + 23: false, + 24: false, + 25: false, + 26: false, + 27: false, + 28: false, + 29: false, + 30: false, + 31: false, + }); + }); + + await step("write", () => { + bitflags32.write({ + 16: true, + 17: true, + 18: true, + 19: true, + 20: true, + 21: true, + 22: true, + 23: true, + 24: true, + 25: true, + 26: true, + 27: true, + 28: true, + 29: true, + 30: true, + 31: true, + }, dt); + assertEquals(dt.getUint32(0), 0xFFFF0000); + + bitflags32.write({ + 16: true, + 17: false, + 18: false, + 19: false, + 20: false, + 21: false, + 22: false, + 23: false, + 24: false, + 25: false, + 26: false, + 27: false, + 28: false, + 29: false, + 30: false, + 31: false, + }, dt); + assertEquals(dt.getUint32(0), 0x10000); + }); +}); diff --git a/types/bitflags/bitflags64_test.ts b/types/bitflags/bitflags64_test.ts new file mode 100644 index 0000000..729aaa0 --- /dev/null +++ b/types/bitflags/bitflags64_test.ts @@ -0,0 +1,69 @@ +import { BitFlags64 } from "./bitflags64.ts"; +import { assertEquals } from "std/testing/asserts.ts"; + +Deno.test("bitflags64", async ({ step }) => { + const buff = new ArrayBuffer(8); + const dt = new DataView(buff); + const bitflags64 = new BitFlags64({ + 1: 1n, + 2: BigInt(1 << 1), + 3: BigInt(1 << 2), + 4: BigInt(1 << 3), + 5: BigInt(1 << 4), + 6: BigInt(1 << 5), + 7: BigInt(1 << 6), + 8: BigInt(1 << 7), + }); + + await step("read", () => { + dt.setUint8(7, 0b11111111); + assertEquals(bitflags64.read(dt), { + 1: true, + 2: true, + 3: true, + 4: true, + 5: true, + 6: true, + 7: true, + 8: true, + }); + + dt.setUint8(7, 1); + assertEquals(bitflags64.read(dt), { + 1: true, + 2: false, + 3: false, + 4: false, + 5: false, + 6: false, + 7: false, + 8: false, + }); + }); + + await step("write", () => { + bitflags64.write({ + 1: true, + 2: true, + 3: true, + 4: true, + 5: true, + 6: true, + 7: true, + 8: true, + }, dt); + assertEquals(dt.getUint8(7), 0b11111111); + + bitflags64.write({ + 1: true, + 2: false, + 3: false, + 4: false, + 5: false, + 6: false, + 7: false, + 8: false, + }, dt); + assertEquals(dt.getUint8(7), 0b00000001); + }); +}); diff --git a/types/bitflags/bitflags8_test.ts b/types/bitflags/bitflags8_test.ts new file mode 100644 index 0000000..a58e942 --- /dev/null +++ b/types/bitflags/bitflags8_test.ts @@ -0,0 +1,69 @@ +import { BitFlags8 } from "./bitflags8.ts"; +import { assertEquals } from "std/testing/asserts.ts"; + +Deno.test("bitflags8", async ({ step }) => { + const buff = new ArrayBuffer(1); + const dt = new DataView(buff); + const bitflags8 = new BitFlags8({ + 1: 1, + 2: 1 << 1, + 3: 1 << 2, + 4: 1 << 3, + 5: 1 << 4, + 6: 1 << 5, + 7: 1 << 6, + 8: 1 << 7, + }); + + await step("read", () => { + dt.setUint8(0, 0b11111111); + assertEquals(bitflags8.read(dt), { + 1: true, + 2: true, + 3: true, + 4: true, + 5: true, + 6: true, + 7: true, + 8: true, + }); + + dt.setUint8(0, 1); + assertEquals(bitflags8.read(dt), { + 1: true, + 2: false, + 3: false, + 4: false, + 5: false, + 6: false, + 7: false, + 8: false, + }); + }); + + await step("write", () => { + bitflags8.write({ + 1: true, + 2: true, + 3: true, + 4: true, + 5: true, + 6: true, + 7: true, + 8: true, + }, dt); + assertEquals(dt.getUint8(0), 0b11111111); + + bitflags8.write({ + 1: true, + 2: false, + 3: false, + 4: false, + 5: false, + 6: false, + 7: false, + 8: false, + }, dt); + assertEquals(dt.getUint8(0), 0b00000001); + }); +}); diff --git a/types/primitive/bool_test.ts b/types/primitive/bool_test.ts new file mode 100644 index 0000000..d1a720a --- /dev/null +++ b/types/primitive/bool_test.ts @@ -0,0 +1,21 @@ +import { bool } from "./bool.ts"; +import { assertEquals } from "std/testing/asserts.ts"; + +Deno.test("Boolean", async ({ step }) => { + const buff = new ArrayBuffer(1); + const dt = new DataView(buff); + + await step("read", () => { + dt.setInt8(0, 0); + assertEquals(bool.read(dt), false); + dt.setInt8(0, 1); + assertEquals(bool.read(dt), true); + }); + + await step("write", () => { + bool.write(true, dt); + assertEquals(dt.getInt8(0), 1); + bool.write(false, dt); + assertEquals(dt.getInt8(0), 0); + }); +}); diff --git a/types/primitive/f32_test.ts b/types/primitive/f32_test.ts new file mode 100644 index 0000000..7e723eb --- /dev/null +++ b/types/primitive/f32_test.ts @@ -0,0 +1,26 @@ +import { assertAlmostEquals } from "std/testing/asserts.ts"; +import { f32be, f32le } from "./f32.ts"; + +Deno.test("f32", async ({ step }) => { + const buff = new ArrayBuffer(4); + const dt = new DataView(buff); + const value = 12.69; + + await step("read", () => { + // Little endian + dt.setFloat32(0, value, true); + assertAlmostEquals(value, f32le.read(dt), 0.01); + // Big endian + dt.setFloat32(0, value, false); + assertAlmostEquals(value, f32be.read(dt), 0.01); + }); + + await step("write", () => { + // Little endian + f32le.write(value, dt); + assertAlmostEquals(value, dt.getFloat32(0, true), 0.01); + // Big endian + f32be.write(value, dt); + assertAlmostEquals(value, dt.getFloat32(0, false), 0.01); + }); +}); diff --git a/types/primitive/f64_test.ts b/types/primitive/f64_test.ts new file mode 100644 index 0000000..30c64e3 --- /dev/null +++ b/types/primitive/f64_test.ts @@ -0,0 +1,26 @@ +import { assertEquals } from "std/testing/asserts.ts"; +import { f64be, f64le } from "./f64.ts"; + +Deno.test("f64", async ({ step }) => { + const buff = new ArrayBuffer(8); + const dt = new DataView(buff); + const value = 12.69; + + await step("read", () => { + // Little endian + dt.setFloat64(0, value, true); + assertEquals(value, f64le.read(dt)); + // Big endian + dt.setFloat64(0, value, false); + assertEquals(value, f64be.read(dt)); + }); + + await step("write", () => { + // Little endian + f64le.write(value, dt); + assertEquals(value, dt.getFloat64(0, true)); + // Big endian + f64be.write(value, dt); + assertEquals(value, dt.getFloat64(0, false)); + }); +}); diff --git a/types/primitive/i16_test.ts b/types/primitive/i16_test.ts new file mode 100644 index 0000000..da0de74 --- /dev/null +++ b/types/primitive/i16_test.ts @@ -0,0 +1,26 @@ +import { i16be, i16le } from "./i16.ts"; +import { assertEquals } from "std/testing/asserts.ts"; + +Deno.test("i16", async ({ step }) => { + const buff = new ArrayBuffer(2); + const dt = new DataView(buff); + const value = 12; + + await step("read", () => { + // Little endian + dt.setInt16(0, value, true); + assertEquals(value, i16le.read(dt)); + // Big endian + dt.setInt16(0, value, false); + assertEquals(value, i16be.read(dt)); + }); + + await step("write", () => { + // Little endian + i16le.write(value, dt); + assertEquals(value, dt.getInt16(0, true)); + // Big endian + i16be.write(value, dt); + assertEquals(value, dt.getInt16(0, false)); + }); +}); diff --git a/types/primitive/i32_test.ts b/types/primitive/i32_test.ts new file mode 100644 index 0000000..b3b703e --- /dev/null +++ b/types/primitive/i32_test.ts @@ -0,0 +1,26 @@ +import { i32be, i32le } from "./i32.ts"; +import { assertEquals } from "std/testing/asserts.ts"; + +Deno.test("i32", async ({ step }) => { + const buff = new ArrayBuffer(4); + const dt = new DataView(buff); + const value = 12; + + await step("read", () => { + // Little endian + dt.setInt32(0, value, true); + assertEquals(value, i32le.read(dt)); + // Big endian + dt.setInt32(0, value, false); + assertEquals(value, i32be.read(dt)); + }); + + await step("write", () => { + // Little endian + i32le.write(value, dt); + assertEquals(value, dt.getInt32(0, true)); + // Big endian + i32be.write(value, dt); + assertEquals(value, dt.getInt32(0, false)); + }); +}); diff --git a/types/primitive/i64_test.ts b/types/primitive/i64_test.ts new file mode 100644 index 0000000..c9df343 --- /dev/null +++ b/types/primitive/i64_test.ts @@ -0,0 +1,26 @@ +import { i64be, i64le } from "./i64.ts"; +import { assertEquals } from "std/testing/asserts.ts"; + +Deno.test("i64", async ({ step }) => { + const buff = new ArrayBuffer(8); + const dt = new DataView(buff); + const value = 12n; + + await step("read", () => { + // Little endian + dt.setBigInt64(0, value, true); + assertEquals(value, i64le.read(dt)); + // Big endian + dt.setBigInt64(0, value, false); + assertEquals(value, i64be.read(dt)); + }); + + await step("write", () => { + // Little endian + i64le.write(value, dt); + assertEquals(value, dt.getBigInt64(0, true)); + // Big endian + i64be.write(value, dt); + assertEquals(value, dt.getBigInt64(0, false)); + }); +}); diff --git a/types/primitive/i8_test.ts b/types/primitive/i8_test.ts new file mode 100644 index 0000000..dd3d5fd --- /dev/null +++ b/types/primitive/i8_test.ts @@ -0,0 +1,21 @@ +import { i8 } from "./i8.ts"; +import { assertEquals } from "std/testing/asserts.ts"; + +Deno.test("i8", async ({ step }) => { + const buff = new ArrayBuffer(1); + const dt = new DataView(buff); + + await step("read", () => { + dt.setInt8(0, 0); + assertEquals(i8.read(dt), 0); + dt.setInt8(0, 1); + assertEquals(i8.read(dt), 1); + }); + + await step("write", () => { + i8.write(1, dt); + assertEquals(dt.getInt8(0), 1); + i8.write(0, dt); + assertEquals(dt.getInt8(0), 0); + }); +}); diff --git a/types/primitive/u16_test.ts b/types/primitive/u16_test.ts new file mode 100644 index 0000000..f75aa0d --- /dev/null +++ b/types/primitive/u16_test.ts @@ -0,0 +1,26 @@ +import { u16be, u16le } from "./u16.ts"; +import { assertEquals } from "std/testing/asserts.ts"; + +Deno.test("u16", async ({ step }) => { + const buff = new ArrayBuffer(2); + const dt = new DataView(buff); + const value = 12; + + await step("read", () => { + // Little endian + dt.setUint16(0, value, true); + assertEquals(value, u16le.read(dt)); + // Big endian + dt.setUint16(0, value, false); + assertEquals(value, u16be.read(dt)); + }); + + await step("write", () => { + // Little endian + u16le.write(value, dt); + assertEquals(value, dt.getUint16(0, true)); + // Big endian + u16be.write(value, dt); + assertEquals(value, dt.getUint16(0, false)); + }); +}); diff --git a/types/primitive/u32_test.ts b/types/primitive/u32_test.ts new file mode 100644 index 0000000..f3dd825 --- /dev/null +++ b/types/primitive/u32_test.ts @@ -0,0 +1,26 @@ +import { u32be, u32le } from "./u32.ts"; +import { assertEquals } from "std/testing/asserts.ts"; + +Deno.test("u32", async ({ step }) => { + const buff = new ArrayBuffer(4); + const dt = new DataView(buff); + const value = 12; + + await step("read", () => { + // Little endian + dt.setUint32(0, value, true); + assertEquals(value, u32le.read(dt)); + // Big endian + dt.setUint32(0, value, false); + assertEquals(value, u32be.read(dt)); + }); + + await step("write", () => { + // Little endian + u32le.write(value, dt); + assertEquals(value, dt.getUint32(0, true)); + // Big endian + u32be.write(value, dt); + assertEquals(value, dt.getUint32(0, false)); + }); +}); diff --git a/types/primitive/u64_test.ts b/types/primitive/u64_test.ts new file mode 100644 index 0000000..e429d69 --- /dev/null +++ b/types/primitive/u64_test.ts @@ -0,0 +1,26 @@ +import { u64be, u64le } from "./u64.ts"; +import { assertEquals } from "std/testing/asserts.ts"; + +Deno.test("u64", async ({ step }) => { + const buff = new ArrayBuffer(8); + const dt = new DataView(buff); + const value = 12n; + + await step("read", () => { + // Little endian + dt.setBigUint64(0, value, true); + assertEquals(value, u64le.read(dt)); + // Big endian + dt.setBigUint64(0, value, false); + assertEquals(value, u64be.read(dt)); + }); + + await step("write", () => { + // Little endian + u64le.write(value, dt); + assertEquals(value, dt.getBigUint64(0, true)); + // Big endian + u64be.write(value, dt); + assertEquals(value, dt.getBigUint64(0, false)); + }); +}); diff --git a/types/primitive/u8_test.ts b/types/primitive/u8_test.ts new file mode 100644 index 0000000..f2ca35a --- /dev/null +++ b/types/primitive/u8_test.ts @@ -0,0 +1,21 @@ +import { u8 } from "./u8.ts"; +import { assertEquals } from "std/testing/asserts.ts"; + +Deno.test("u8", async ({ step }) => { + const buff = new ArrayBuffer(1); + const dt = new DataView(buff); + + await step("read", () => { + dt.setUint8(0, 0); + assertEquals(u8.read(dt), 0); + dt.setUint8(0, 1); + assertEquals(u8.read(dt), 1); + }); + + await step("write", () => { + u8.write(1, dt); + assertEquals(dt.getUint8(0), 1); + u8.write(0, dt); + assertEquals(dt.getUint8(0), 0); + }); +}); diff --git a/types/string/fixed_length_test.ts b/types/string/fixed_length_test.ts new file mode 100644 index 0000000..3d545ab --- /dev/null +++ b/types/string/fixed_length_test.ts @@ -0,0 +1,21 @@ +import { assertEquals } from "https://deno.land/std@0.184.0/testing/asserts.ts"; +import { FixedLengthString } from "./fixed_length.ts"; + +Deno.test("Fixed Length String", async ({ step }) => { + const value = "Lorem Ipsum"; + const buffer = new ArrayBuffer(value.length); + const dt = new DataView(buffer); + const fixedLengthStringType = new FixedLengthString(value.length); + + await step("read", () => { + const byteView = new Uint8Array(buffer); + byteView.set(value.split("").map((x) => x.charCodeAt(0))); + assertEquals(fixedLengthStringType.read(dt), value); + }); + + await step("write", () => { + const byteView = new Uint8Array(buffer); + fixedLengthStringType.write("LOREM IPSUM", dt); + assertEquals(new TextDecoder().decode(byteView), value.toUpperCase()); + }); +});