2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-02-26 00:35:50 -05:00
|
|
|
const { Buffer } = Deno;
|
2019-03-07 19:25:16 -05:00
|
|
|
type Reader = Deno.Reader;
|
2019-03-06 19:42:24 -05:00
|
|
|
import { assertEquals } from "../testing/asserts.ts";
|
2019-02-10 18:49:48 -05:00
|
|
|
import {
|
|
|
|
copyN,
|
|
|
|
readInt,
|
|
|
|
readLong,
|
|
|
|
readShort,
|
|
|
|
sliceLongToBytes
|
|
|
|
} from "./ioutil.ts";
|
2019-01-06 14:26:18 -05:00
|
|
|
import { BufReader } from "./bufio.ts";
|
2019-02-10 18:49:48 -05:00
|
|
|
import { stringsReader } from "./util.ts";
|
2019-01-06 14:26:18 -05:00
|
|
|
|
|
|
|
class BinaryReader implements Reader {
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
constructor(private bytes: Uint8Array = new Uint8Array(0)) {}
|
|
|
|
|
2019-07-07 15:20:41 -04:00
|
|
|
async read(p: Uint8Array): Promise<number | Deno.EOF> {
|
2019-01-06 14:26:18 -05:00
|
|
|
p.set(this.bytes.subarray(this.index, p.byteLength));
|
|
|
|
this.index += p.byteLength;
|
2019-07-07 15:20:41 -04:00
|
|
|
return p.byteLength;
|
2019-01-06 14:26:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function testReadShort(): Promise<void> {
|
2019-01-06 14:26:18 -05:00
|
|
|
const r = new BinaryReader(new Uint8Array([0x12, 0x34]));
|
|
|
|
const short = await readShort(new BufReader(r));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(short, 0x1234);
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function testReadInt(): Promise<void> {
|
2019-01-06 14:26:18 -05:00
|
|
|
const r = new BinaryReader(new Uint8Array([0x12, 0x34, 0x56, 0x78]));
|
|
|
|
const int = await readInt(new BufReader(r));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(int, 0x12345678);
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function testReadLong(): Promise<void> {
|
2019-01-06 14:26:18 -05:00
|
|
|
const r = new BinaryReader(
|
2019-05-30 21:19:28 -04:00
|
|
|
new Uint8Array([0x00, 0x00, 0x00, 0x78, 0x12, 0x34, 0x56, 0x78])
|
2019-01-06 14:26:18 -05:00
|
|
|
);
|
|
|
|
const long = await readLong(new BufReader(r));
|
2019-05-30 21:19:28 -04:00
|
|
|
assertEquals(long, 0x7812345678);
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function testReadLong2(): Promise<void> {
|
2019-01-06 14:26:18 -05:00
|
|
|
const r = new BinaryReader(
|
|
|
|
new Uint8Array([0, 0, 0, 0, 0x12, 0x34, 0x56, 0x78])
|
|
|
|
);
|
|
|
|
const long = await readLong(new BufReader(r));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(long, 0x12345678);
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function testSliceLongToBytes(): Promise<void> {
|
2019-01-06 14:26:18 -05:00
|
|
|
const arr = sliceLongToBytes(0x1234567890abcdef);
|
|
|
|
const actual = readLong(new BufReader(new BinaryReader(new Uint8Array(arr))));
|
|
|
|
const expected = readLong(
|
|
|
|
new BufReader(
|
|
|
|
new BinaryReader(
|
|
|
|
new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef])
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(actual, expected);
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function testSliceLongToBytes2(): Promise<void> {
|
2019-01-06 14:26:18 -05:00
|
|
|
const arr = sliceLongToBytes(0x12345678);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(arr, [0, 0, 0, 0, 0x12, 0x34, 0x56, 0x78]);
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
2019-02-10 18:49:48 -05:00
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function testCopyN1(): Promise<void> {
|
2019-02-10 18:49:48 -05:00
|
|
|
const w = new Buffer();
|
|
|
|
const r = stringsReader("abcdefghij");
|
|
|
|
const n = await copyN(w, r, 3);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(n, 3);
|
|
|
|
assertEquals(w.toString(), "abc");
|
2019-02-10 18:49:48 -05:00
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function testCopyN2(): Promise<void> {
|
2019-02-10 18:49:48 -05:00
|
|
|
const w = new Buffer();
|
|
|
|
const r = stringsReader("abcdefghij");
|
|
|
|
const n = await copyN(w, r, 11);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(n, 10);
|
|
|
|
assertEquals(w.toString(), "abcdefghij");
|
2019-02-10 18:49:48 -05:00
|
|
|
});
|