2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
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,
|
2020-03-28 13:03:49 -04:00
|
|
|
sliceLongToBytes,
|
2019-02-10 18:49:48 -05:00
|
|
|
} from "./ioutil.ts";
|
2020-06-06 10:37:52 -04:00
|
|
|
import { StringReader } from "./readers.ts";
|
2019-01-06 14:26:18 -05:00
|
|
|
import { BufReader } from "./bufio.ts";
|
2020-06-06 10:37:52 -04:00
|
|
|
import { tempFile } from "./util.ts";
|
2020-04-29 06:25:40 -04:00
|
|
|
import * as path from "../path/mod.ts";
|
2019-01-06 14:26:18 -05:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
class BinaryReader implements Deno.Reader {
|
2019-01-06 14:26:18 -05:00
|
|
|
index = 0;
|
|
|
|
|
|
|
|
constructor(private bytes: Uint8Array = new Uint8Array(0)) {}
|
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
read(p: Uint8Array): Promise<number | null> {
|
2019-01-06 14:26:18 -05:00
|
|
|
p.set(this.bytes.subarray(this.index, p.byteLength));
|
|
|
|
this.index += p.byteLength;
|
2020-03-20 09:38:34 -04:00
|
|
|
return Promise.resolve(p.byteLength);
|
2019-01-06 14:26:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testReadShort", async function (): 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-04-28 06:33:09 -04:00
|
|
|
Deno.test("testReadInt", async function (): 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-04-28 06:33:09 -04:00
|
|
|
Deno.test("testReadLong", async function (): Promise<void> {
|
2019-01-06 14:26:18 -05:00
|
|
|
const r = new BinaryReader(
|
2020-07-14 15:24:17 -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-04-28 06:33:09 -04:00
|
|
|
Deno.test("testReadLong2", async function (): Promise<void> {
|
2019-01-06 14:26:18 -05:00
|
|
|
const r = new BinaryReader(
|
2020-07-14 15:24:17 -04:00
|
|
|
new Uint8Array([0, 0, 0, 0, 0x12, 0x34, 0x56, 0x78]),
|
2019-01-06 14:26:18 -05:00
|
|
|
);
|
|
|
|
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-04-28 06:33:09 -04:00
|
|
|
Deno.test("testSliceLongToBytes", function (): 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(
|
2020-07-14 15:24:17 -04:00
|
|
|
new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef]),
|
|
|
|
),
|
|
|
|
),
|
2019-01-06 14:26:18 -05:00
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(actual, expected);
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testSliceLongToBytes2", function (): 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-04-28 06:33:09 -04:00
|
|
|
Deno.test("testCopyN1", async function (): Promise<void> {
|
2020-06-12 15:23:38 -04:00
|
|
|
const w = new Deno.Buffer();
|
2020-06-06 10:37:52 -04:00
|
|
|
const r = new StringReader("abcdefghij");
|
2020-04-26 16:26:02 -04:00
|
|
|
const n = await copyN(r, w, 3);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(n, 3);
|
2020-04-29 16:38:10 -04:00
|
|
|
assertEquals(new TextDecoder().decode(w.bytes()), "abc");
|
2019-02-10 18:49:48 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("testCopyN2", async function (): Promise<void> {
|
2020-06-12 15:23:38 -04:00
|
|
|
const w = new Deno.Buffer();
|
2020-06-06 10:37:52 -04:00
|
|
|
const r = new StringReader("abcdefghij");
|
2020-04-26 16:26:02 -04:00
|
|
|
const n = await copyN(r, w, 11);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(n, 10);
|
2020-04-29 16:38:10 -04:00
|
|
|
assertEquals(new TextDecoder().decode(w.bytes()), "abcdefghij");
|
2019-02-10 18:49:48 -05:00
|
|
|
});
|
2020-04-29 06:25:40 -04:00
|
|
|
|
|
|
|
Deno.test("copyNWriteAllData", async function (): Promise<void> {
|
|
|
|
const { filepath, file } = await tempFile(path.resolve("io"));
|
|
|
|
const size = 16 * 1024 + 1;
|
|
|
|
const data = "a".repeat(32 * 1024);
|
2020-06-06 10:37:52 -04:00
|
|
|
const r = new StringReader(data);
|
2020-04-29 06:25:40 -04:00
|
|
|
const n = await copyN(r, file, size); // Over max file possible buffer
|
|
|
|
file.close();
|
|
|
|
await Deno.remove(filepath);
|
|
|
|
|
|
|
|
assertEquals(n, size);
|
|
|
|
});
|
2020-06-06 10:37:52 -04:00
|
|
|
|
|
|
|
Deno.test("testStringReaderEof", async function (): Promise<void> {
|
|
|
|
const r = new StringReader("abc");
|
|
|
|
assertEquals(await r.read(new Uint8Array()), 0);
|
|
|
|
assertEquals(await r.read(new Uint8Array(4)), 3);
|
|
|
|
assertEquals(await r.read(new Uint8Array(1)), null);
|
|
|
|
});
|