-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
102acc4
commit cc1a07e
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}, | ||
}); |