2020-03-10 15:16:08 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
import { assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
|
|
|
|
import {
|
|
|
|
getNBytes,
|
|
|
|
putVarbig,
|
|
|
|
putVarnum,
|
|
|
|
readVarbig,
|
|
|
|
readVarnum,
|
|
|
|
sizeof,
|
|
|
|
varbig,
|
|
|
|
varnum,
|
|
|
|
writeVarbig,
|
2020-03-28 13:03:49 -04:00
|
|
|
writeVarnum,
|
2020-06-08 18:58:55 -04:00
|
|
|
varbigBytes,
|
|
|
|
varnumBytes,
|
2020-03-10 15:16:08 -04:00
|
|
|
} from "./binary.ts";
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testGetNBytes", async function (): Promise<void> {
|
2020-03-10 15:16:08 -04:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testGetNBytesThrows", async function (): Promise<void> {
|
2020-03-10 15:16:08 -04:00
|
|
|
const data = new Uint8Array([1, 2, 3, 4]);
|
|
|
|
const buff = new Deno.Buffer(data.buffer);
|
2020-03-20 09:38:34 -04:00
|
|
|
await assertThrowsAsync(async () => {
|
2020-03-10 15:16:08 -04:00
|
|
|
await getNBytes(buff, 8);
|
2020-03-13 21:40:13 -04:00
|
|
|
}, Deno.errors.UnexpectedEof);
|
2020-03-10 15:16:08 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testPutVarbig", function (): void {
|
2020-03-10 15:16:08 -04:00
|
|
|
const buff = new Uint8Array(8);
|
|
|
|
putVarbig(buff, 0xffeeddccbbaa9988n);
|
|
|
|
assertEquals(
|
|
|
|
buff,
|
2020-07-14 15:24:17 -04:00
|
|
|
new Uint8Array([0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88]),
|
2020-03-10 15:16:08 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testPutVarbigLittleEndian", function (): void {
|
2020-03-10 15:16:08 -04:00
|
|
|
const buff = new Uint8Array(8);
|
|
|
|
putVarbig(buff, 0x8899aabbccddeeffn, { endian: "little" });
|
|
|
|
assertEquals(
|
|
|
|
buff,
|
2020-07-14 15:24:17 -04:00
|
|
|
new Uint8Array([0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88]),
|
2020-03-10 15:16:08 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testPutVarnum", function (): void {
|
2020-03-10 15:16:08 -04:00
|
|
|
const buff = new Uint8Array(4);
|
|
|
|
putVarnum(buff, 0xffeeddcc);
|
|
|
|
assertEquals(buff, new Uint8Array([0xff, 0xee, 0xdd, 0xcc]));
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testPutVarnumLittleEndian", function (): void {
|
2020-03-10 15:16:08 -04:00
|
|
|
const buff = new Uint8Array(4);
|
|
|
|
putVarnum(buff, 0xccddeeff, { endian: "little" });
|
|
|
|
assertEquals(buff, new Uint8Array([0xff, 0xee, 0xdd, 0xcc]));
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testReadVarbig", async function (): Promise<void> {
|
2020-03-10 15:16:08 -04:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testReadVarbigLittleEndian", async function (): Promise<void> {
|
2020-03-10 15:16:08 -04:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testReadVarnum", async function (): Promise<void> {
|
2020-03-10 15:16:08 -04:00
|
|
|
const data = new Uint8Array([1, 2, 3, 4]);
|
|
|
|
const buff = new Deno.Buffer(data.buffer);
|
|
|
|
const rslt = await readVarnum(buff);
|
|
|
|
assertEquals(rslt, 0x01020304);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testReadVarnumLittleEndian", async function (): Promise<void> {
|
2020-03-10 15:16:08 -04:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testSizeof", function (): void {
|
2020-03-10 15:16:08 -04:00
|
|
|
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"));
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testVarbig", function (): void {
|
2020-03-10 15:16:08 -04:00
|
|
|
const data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
|
|
|
|
const rslt = varbig(data);
|
|
|
|
assertEquals(rslt, 0x0102030405060708n);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testVarbigLittleEndian", function (): void {
|
2020-03-10 15:16:08 -04:00
|
|
|
const data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
|
|
|
|
const rslt = varbig(data, { endian: "little" });
|
|
|
|
assertEquals(rslt, 0x0807060504030201n);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testVarnum", function (): void {
|
2020-03-10 15:16:08 -04:00
|
|
|
const data = new Uint8Array([1, 2, 3, 4]);
|
|
|
|
const rslt = varnum(data);
|
|
|
|
assertEquals(rslt, 0x01020304);
|
|
|
|
});
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testVarnumLittleEndian", function (): void {
|
2020-03-10 15:16:08 -04:00
|
|
|
const data = new Uint8Array([1, 2, 3, 4]);
|
|
|
|
const rslt = varnum(data, { endian: "little" });
|
|
|
|
assertEquals(rslt, 0x04030201);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testWriteVarbig", async function (): Promise<void> {
|
2020-03-10 15:16:08 -04:00
|
|
|
const data = new Uint8Array(8);
|
|
|
|
const buff = new Deno.Buffer();
|
|
|
|
await writeVarbig(buff, 0x0102030405060708n);
|
|
|
|
await buff.read(data);
|
|
|
|
assertEquals(
|
|
|
|
data,
|
2020-07-14 15:24:17 -04:00
|
|
|
new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]),
|
2020-03-10 15:16:08 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testWriteVarbigLittleEndian", async function (): Promise<void> {
|
2020-03-10 15:16:08 -04:00
|
|
|
const data = new Uint8Array(8);
|
|
|
|
const buff = new Deno.Buffer();
|
|
|
|
await writeVarbig(buff, 0x0807060504030201n, { endian: "little" });
|
|
|
|
await buff.read(data);
|
|
|
|
assertEquals(
|
|
|
|
data,
|
2020-07-14 15:24:17 -04:00
|
|
|
new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]),
|
2020-03-10 15:16:08 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testWriteVarnum", async function (): Promise<void> {
|
2020-03-10 15:16:08 -04:00
|
|
|
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]));
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testWriteVarnumLittleEndian", async function (): Promise<void> {
|
2020-03-10 15:16:08 -04:00
|
|
|
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]));
|
|
|
|
});
|
2020-06-08 18:58:55 -04:00
|
|
|
|
|
|
|
Deno.test("testVarbigBytes", function (): void {
|
|
|
|
const rslt = varbigBytes(0x0102030405060708n);
|
|
|
|
assertEquals(
|
|
|
|
rslt,
|
2020-07-14 15:24:17 -04:00
|
|
|
new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]),
|
2020-06-08 18:58:55 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("testVarbigBytesLittleEndian", function (): void {
|
|
|
|
const rslt = varbigBytes(0x0807060504030201n, { endian: "little" });
|
|
|
|
assertEquals(
|
|
|
|
rslt,
|
2020-07-14 15:24:17 -04:00
|
|
|
new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]),
|
2020-06-08 18:58:55 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("testVarnumBytes", function (): void {
|
|
|
|
const rslt = varnumBytes(0x01020304);
|
|
|
|
assertEquals(rslt, new Uint8Array([0x01, 0x02, 0x03, 0x04]));
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("testVarnumBytesLittleEndian", function (): void {
|
|
|
|
const rslt = varnumBytes(0x04030201, { endian: "little" });
|
|
|
|
assertEquals(rslt, new Uint8Array([0x01, 0x02, 0x03, 0x04]));
|
|
|
|
});
|