summaryrefslogtreecommitdiff
path: root/std/encoding/binary_test.ts
diff options
context:
space:
mode:
authorOliver Lenehan <sunsetkookaburra+github@outlook.com.au>2020-03-11 06:16:08 +1100
committerGitHub <noreply@github.com>2020-03-10 15:16:08 -0400
commita309dcdd0f5bc93849d720328b887931a4810e2f (patch)
treee6f684df4607ada000635a2acdfee85576e3d774 /std/encoding/binary_test.ts
parent55119aaee2e5fec8074373ef51b56d5095da1faf (diff)
feat (std/encoding): add binary module (#4274)
Diffstat (limited to 'std/encoding/binary_test.ts')
-rw-r--r--std/encoding/binary_test.ts165
1 files changed, 165 insertions, 0 deletions
diff --git a/std/encoding/binary_test.ts b/std/encoding/binary_test.ts
new file mode 100644
index 000000000..9b541746b
--- /dev/null
+++ b/std/encoding/binary_test.ts
@@ -0,0 +1,165 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+
+import { assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
+import { UnexpectedEOFError } from "../io/bufio.ts";
+import {
+ getNBytes,
+ putVarbig,
+ putVarnum,
+ readVarbig,
+ readVarnum,
+ sizeof,
+ varbig,
+ varnum,
+ writeVarbig,
+ writeVarnum
+} from "./binary.ts";
+
+Deno.test(async function testGetNBytes(): Promise<void> {
+ const data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
+ const buff = new Deno.Buffer(data.buffer);
+ const rslt = await getNBytes(buff, 8);
+ assertEquals(rslt, data);
+});
+
+Deno.test(async function testGetNBytesThrows(): Promise<void> {
+ const data = new Uint8Array([1, 2, 3, 4]);
+ const buff = new Deno.Buffer(data.buffer);
+ assertThrowsAsync(async () => {
+ await getNBytes(buff, 8);
+ }, UnexpectedEOFError);
+});
+
+Deno.test(async function testPutVarbig(): Promise<void> {
+ const buff = new Uint8Array(8);
+ putVarbig(buff, 0xffeeddccbbaa9988n);
+ assertEquals(
+ buff,
+ new Uint8Array([0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88])
+ );
+});
+
+Deno.test(async function testPutVarbigLittleEndian(): Promise<void> {
+ const buff = new Uint8Array(8);
+ putVarbig(buff, 0x8899aabbccddeeffn, { endian: "little" });
+ assertEquals(
+ buff,
+ new Uint8Array([0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88])
+ );
+});
+
+Deno.test(async function testPutVarnum(): Promise<void> {
+ const buff = new Uint8Array(4);
+ putVarnum(buff, 0xffeeddcc);
+ assertEquals(buff, new Uint8Array([0xff, 0xee, 0xdd, 0xcc]));
+});
+
+Deno.test(async function testPutVarnumLittleEndian(): Promise<void> {
+ const buff = new Uint8Array(4);
+ putVarnum(buff, 0xccddeeff, { endian: "little" });
+ assertEquals(buff, new Uint8Array([0xff, 0xee, 0xdd, 0xcc]));
+});
+
+Deno.test(async function testReadVarbig(): Promise<void> {
+ const data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
+ const buff = new Deno.Buffer(data.buffer);
+ const rslt = await readVarbig(buff);
+ assertEquals(rslt, 0x0102030405060708n);
+});
+
+Deno.test(async function testReadVarbigLittleEndian(): Promise<void> {
+ const data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
+ const buff = new Deno.Buffer(data.buffer);
+ const rslt = await readVarbig(buff, { endian: "little" });
+ assertEquals(rslt, 0x0807060504030201n);
+});
+
+Deno.test(async function testReadVarnum(): Promise<void> {
+ const data = new Uint8Array([1, 2, 3, 4]);
+ const buff = new Deno.Buffer(data.buffer);
+ const rslt = await readVarnum(buff);
+ assertEquals(rslt, 0x01020304);
+});
+
+Deno.test(async function testReadVarnumLittleEndian(): Promise<void> {
+ const data = new Uint8Array([1, 2, 3, 4]);
+ const buff = new Deno.Buffer(data.buffer);
+ const rslt = await readVarnum(buff, { endian: "little" });
+ assertEquals(rslt, 0x04030201);
+});
+
+Deno.test(function testSizeof(): void {
+ assertEquals(1, sizeof("int8"));
+ assertEquals(1, sizeof("uint8"));
+ assertEquals(2, sizeof("int16"));
+ assertEquals(2, sizeof("uint16"));
+ assertEquals(4, sizeof("int32"));
+ assertEquals(4, sizeof("uint32"));
+ assertEquals(8, sizeof("int64"));
+ assertEquals(8, sizeof("uint64"));
+ assertEquals(4, sizeof("float32"));
+ assertEquals(8, sizeof("float64"));
+});
+
+Deno.test(function testVarbig(): void {
+ const data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
+ const rslt = varbig(data);
+ assertEquals(rslt, 0x0102030405060708n);
+});
+
+Deno.test(function testVarbigLittleEndian(): void {
+ const data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
+ const rslt = varbig(data, { endian: "little" });
+ assertEquals(rslt, 0x0807060504030201n);
+});
+
+Deno.test(function testVarnum(): void {
+ const data = new Uint8Array([1, 2, 3, 4]);
+ const rslt = varnum(data);
+ assertEquals(rslt, 0x01020304);
+});
+Deno.test(function testVarnumLittleEndian(): void {
+ const data = new Uint8Array([1, 2, 3, 4]);
+ const rslt = varnum(data, { endian: "little" });
+ assertEquals(rslt, 0x04030201);
+});
+
+Deno.test(async function testWriteVarbig(): Promise<void> {
+ const data = new Uint8Array(8);
+ const buff = new Deno.Buffer();
+ await writeVarbig(buff, 0x0102030405060708n);
+ await buff.read(data);
+ assertEquals(
+ data,
+ new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])
+ );
+});
+
+Deno.test(async function testWriteVarbigLittleEndian(): Promise<void> {
+ const data = new Uint8Array(8);
+ const buff = new Deno.Buffer();
+ await writeVarbig(buff, 0x0807060504030201n, { endian: "little" });
+ await buff.read(data);
+ assertEquals(
+ data,
+ new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])
+ );
+});
+
+Deno.test(async function testWriteVarnum(): Promise<void> {
+ const data = new Uint8Array(4);
+ const buff = new Deno.Buffer();
+ await writeVarnum(buff, 0x01020304);
+ await buff.read(data);
+ assertEquals(data, new Uint8Array([0x01, 0x02, 0x03, 0x04]));
+});
+
+Deno.test(async function testWriteVarnumLittleEndian(): Promise<void> {
+ const data = new Uint8Array(4);
+ const buff = new Deno.Buffer();
+ await writeVarnum(buff, 0x04030201, { endian: "little" });
+ await buff.read(data);
+ assertEquals(data, new Uint8Array([0x01, 0x02, 0x03, 0x04]));
+});
+
+Deno.runTests();