deno.land / std@0.157.0 / io / util_test.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { assertEquals } from "../testing/asserts.ts";import { copyN, readInt, readLong, readShort, sliceLongToBytes,} from "./util.ts";import { Buffer, BufReader } from "./buffer.ts";import { StringReader } from "./readers.ts";import type { Reader } from "./types.d.ts";
class BinaryReader implements Reader { index = 0;
constructor(private bytes: Uint8Array = new Uint8Array(0)) {}
read(p: Uint8Array): Promise<number | null> { p.set(this.bytes.subarray(this.index, p.byteLength)); this.index += p.byteLength; return Promise.resolve(p.byteLength); }}
Deno.test("testReadShort", async function () { const r = new BinaryReader(new Uint8Array([0x12, 0x34])); const short = await readShort(new BufReader(r)); assertEquals(short, 0x1234);});
Deno.test("testReadInt", async function () { const r = new BinaryReader(new Uint8Array([0x12, 0x34, 0x56, 0x78])); const int = await readInt(new BufReader(r)); assertEquals(int, 0x12345678);});
Deno.test("testReadLong", async function () { const r = new BinaryReader( new Uint8Array([0x00, 0x00, 0x00, 0x78, 0x12, 0x34, 0x56, 0x78]), ); const long = await readLong(new BufReader(r)); assertEquals(long, 0x7812345678);});
Deno.test("testReadLong2", async function () { const r = new BinaryReader( new Uint8Array([0, 0, 0, 0, 0x12, 0x34, 0x56, 0x78]), ); const long = await readLong(new BufReader(r)); assertEquals(long, 0x12345678);});
Deno.test("testSliceLongToBytes", function () { 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]), ), ), ); assertEquals(actual, expected);});
Deno.test("testSliceLongToBytes2", function () { const arr = sliceLongToBytes(0x12345678); assertEquals(arr, [0, 0, 0, 0, 0x12, 0x34, 0x56, 0x78]);});
Deno.test("testCopyN1", async function () { const w = new Buffer(); const r = new StringReader("abcdefghij"); const n = await copyN(r, w, 3); assertEquals(n, 3); assertEquals(new TextDecoder().decode(w.bytes()), "abc");});
Deno.test("testCopyN2", async function () { const w = new Buffer(); const r = new StringReader("abcdefghij"); const n = await copyN(r, w, 11); assertEquals(n, 10); assertEquals(new TextDecoder().decode(w.bytes()), "abcdefghij");});
Deno.test("copyNWriteAllData", async function () { const tmpDir = await Deno.makeTempDir(); const filepath = `${tmpDir}/data`; const file = await Deno.open(filepath, { create: true, write: true });
const size = 16 * 1024 + 1; const data = "a".repeat(32 * 1024); const r = new StringReader(data); const n = await copyN(r, file, size); // Over max file possible buffer file.close(); await Deno.remove(filepath);
assertEquals(n, size);});
Deno.test("testStringReaderEof", async function () { 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);});
std

Version Info

Tagged at
a year ago