Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MierenManz committed Jul 9, 2024
1 parent 102acc4 commit cc1a07e
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/compound/union_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { u32le, u8 } from "../mod.ts";
import { assertEquals, assertThrows } from "../../test_deps.ts";
import { Union } from "./union.ts";

Deno.test({
name: "Union",
fn: async (t) => {
const ab = new ArrayBuffer(8);
const dt = new DataView(ab);
const type = new Union({
0: u32le,
1: u8,
2: u8,
}, (a) => a === 32 ? 0 : 1);

await t.step("Read", () => {
dt.setUint8(0, 1);
dt.setUint8(1, 11);
dt.setUint8(2, 22);
dt.setUint8(4, 33);
const result = type.read(dt);
assertEquals(result, 33);
});

await t.step("Read Packed", () => {
dt.setUint8(0, 1);
dt.setUint8(1, 11);
dt.setUint8(2, 22);
dt.setUint8(4, 33);
const result = type.readPacked(dt);
assertEquals(result, 11);
});

dt.setBigUint64(0, 0n);

await t.step("Write", () => {
type.write(32, dt);
assertEquals(new Uint32Array(ab), Uint32Array.of(0, 32));
});

dt.setBigUint64(0, 0n);

await t.step("Write Packed", () => {
type.write(32, dt);
assertEquals(
new Uint8Array(ab).subarray(0, 5),
Uint8Array.of(0, 0, 0, 0, 32),
);
});

await t.step("OOB Read", () => {
assertThrows(() => {
type.read(dt, { byteOffset: 9 });
}, RangeError);
});
},
});

0 comments on commit cc1a07e

Please sign in to comment.