deno.land / std@0.224.0 / io / read_range_test.ts

read_range_test.ts
View Documentation
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { copy } from "../bytes/copy.ts";import { assert, assertEquals, assertRejects, assertThrows,} from "../assert/mod.ts";import { readRange, readRangeSync } from "./read_range.ts";import type { Closer, Reader, ReaderSync } from "./types.ts";
// N controls how many iterations of certain checks are performed.const N = 100;let testBytes: Uint8Array | undefined;
export function init() { if (testBytes === undefined) { testBytes = new Uint8Array(N); for (let i = 0; i < N; i++) { testBytes[i] = "a".charCodeAt(0) + (i % 26); } }}
class MockFile implements Deno.Seeker, Deno.SeekerSync, Reader, ReaderSync, Closer { #buf: Uint8Array; #closed = false; #offset = 0;
get closed() { return this.#closed; }
constructor(buf: Uint8Array) { this.#buf = buf; }
close() { this.#closed = true; }
read(p: Uint8Array): Promise<number | null> { if (this.#offset >= this.#buf.length) { return Promise.resolve(null); } const nread = Math.min(p.length, 16_384, this.#buf.length - this.#offset); if (nread === 0) { return Promise.resolve(0); } copy(this.#buf.subarray(this.#offset, this.#offset + nread), p); this.#offset += nread; return Promise.resolve(nread); }
readSync(p: Uint8Array): number | null { if (this.#offset >= this.#buf.length) { return null; } const nread = Math.min(p.length, 16_384, this.#buf.length - this.#offset); if (nread === 0) { return 0; } copy(this.#buf.subarray(this.#offset, this.#offset + nread), p); this.#offset += nread; return nread; }
seek(offset: number, whence: Deno.SeekMode): Promise<number> { assert(whence === Deno.SeekMode.Start); if (offset >= this.#buf.length) { return Promise.reject(new RangeError("attempted to seek past end")); } this.#offset = offset; return Promise.resolve(this.#offset); }
seekSync(offset: number, whence: Deno.SeekMode): number { assert(whence === Deno.SeekMode.Start); if (offset >= this.#buf.length) { throw new RangeError("attempted to seek past end"); } this.#offset = offset; return this.#offset; }}
Deno.test({ name: "readRange", async fn() { init(); assert(testBytes); const file = new MockFile(testBytes); const actual = await readRange(file, { start: 0, end: 9 }); assertEquals(actual, testBytes.subarray(0, 10)); },});
Deno.test({ name: "readRange - invalid range", async fn() { init(); assert(testBytes); const file = new MockFile(testBytes); await assertRejects( async () => { await readRange(file, { start: 100, end: 0 }); }, Error, "Invalid byte range was passed.", ); },});
Deno.test({ name: "readRange - read past EOF", async fn() { init(); assert(testBytes); const file = new MockFile(testBytes); await assertRejects( async () => { await readRange(file, { start: 99, end: 100 }); }, Error, "Unexpected EOF reach while reading a range.", ); },});
Deno.test({ name: "readRangeSync", fn() { init(); assert(testBytes); const file = new MockFile(testBytes); const actual = readRangeSync(file, { start: 0, end: 9 }); assertEquals(actual, testBytes.subarray(0, 10)); },});
Deno.test({ name: "readRangeSync - invalid range", fn() { init(); assert(testBytes); const file = new MockFile(testBytes); assertThrows( () => { readRangeSync(file, { start: 100, end: 0 }); }, Error, "Invalid byte range was passed.", ); },});
Deno.test({ name: "readRangeSync - read past EOF", fn() { init(); assert(testBytes); const file = new MockFile(testBytes); assertThrows( () => { readRangeSync(file, { start: 99, end: 100 }); }, Error, "Unexpected EOF reach while reading a range.", ); },});
std

Version Info

Tagged at
6 months ago