-
Notifications
You must be signed in to change notification settings - Fork 0
/
memoryfile.test.ts
295 lines (234 loc) · 7.17 KB
/
memoryfile.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// import { open } from 'node:fs/promises';
import { assertEquals } from '@std/assert';
import { MemoryFile } from './memoryfile.ts';
// import type { FileLike } from './type.ts';
const BS = 4096;
Deno.test('stat two blocks', async () => {
const size = BS * 2;
const m = new MemoryFile(size);
const r = await m.stat();
assertEquals(r.size, size);
assertEquals(r.blocks, 2);
assertEquals(r.blksize, BS);
});
Deno.test('stat two blocks plus one', async () => {
const size = BS * 2;
const m = new MemoryFile(size + 1);
const r = await m.stat();
assertEquals(r.size, size + 1);
assertEquals(r.blocks, 3);
assertEquals(r.blksize, BS);
});
Deno.test('stat custom blksize', async () => {
const CBS = 42;
const size = Math.round(CBS * 2.5);
const m = new MemoryFile(size, CBS);
const r = await m.stat();
assertEquals(r.size, size);
assertEquals(r.blocks, 3);
assertEquals(r.blksize, CBS);
});
Deno.test('write expand', async () => {
let r;
const d = new Uint8Array(1);
const m = new MemoryFile();
r = await m.write(d, 0, 1, 10);
assertEquals(r.bytesWritten, 1);
assertEquals((await m.stat()).size, 11);
r = await m.write(d, 0, 1, 99);
assertEquals(r.bytesWritten, 1);
assertEquals((await m.stat()).size, 100);
r = await m.write(d, 0, 1, 100);
assertEquals(r.bytesWritten, 1);
assertEquals((await m.stat()).size, 101);
r = await m.write(d, 0, 1, 99);
assertEquals(r.bytesWritten, 1);
assertEquals((await m.stat()).size, 101);
r = await m.write(d, 0, 1, BS - 1);
assertEquals(r.bytesWritten, 1);
assertEquals((await m.stat()).size, BS);
r = await m.write(d, 0, 1, BS);
assertEquals(r.bytesWritten, 1);
assertEquals((await m.stat()).size, BS + 1);
r = await m.write(d, 0, 1, BS * 2);
assertEquals(r.bytesWritten, 1);
assertEquals((await m.stat()).size, BS * 2 + 1);
});
Deno.test('sparse', async () => {
let r;
const d = new Uint8Array(1);
const m = new MemoryFile();
r = await m.write(d, 0, 1, 10000);
assertEquals(r.bytesWritten, 1);
assertEquals((await m.stat()).size, 10001);
d[0] = 123;
r = await m.read(d, 0, 1, 0);
assertEquals(r.bytesRead, 1);
assertEquals((await m.stat()).size, 10001);
assertEquals(d[0], 0);
r = await m.write(d, 0, 1, 0);
assertEquals(r.bytesWritten, 1);
assertEquals((await m.stat()).size, 10001);
});
Deno.test('write many pieces overlapping', async () => {
const m = new MemoryFile();
let offset = 0;
for (let l = 0; l < 10000; l++) {
const sub = offset ? 1 : 0;
const d = new Uint8Array(l);
// deno-lint-ignore no-await-in-loop
const r = await m.write(d, 0, l, offset - sub);
assertEquals(r.bytesWritten, l);
offset += l - sub;
// deno-lint-ignore no-await-in-loop
assertEquals((await m.stat()).size, offset);
}
});
Deno.test('read simple', async () => {
let r;
let dr;
const m = new MemoryFile();
const dw = new Uint8Array([1, 2, 3, 4, 5]);
r = await m.write(dw, 0, dw.length, 5);
assertEquals(r.bytesWritten, dw.length);
dr = new Uint8Array(dw.length);
r = await m.read(dr, 0, dw.length, 5);
assertEquals(r.bytesRead, dw.length);
assertEquals(dr, dw);
dr = new Uint8Array([9, 9, 9, 9, 9]);
r = await m.read(dr, 0, 0, 5);
assertEquals(r.bytesRead, 0);
assertEquals(dr, new Uint8Array([9, 9, 9, 9, 9]));
dr = new Uint8Array([9, 9, 9, 9, 9]);
r = await m.read(dr, 2, 1, 5);
assertEquals(r.bytesRead, 1);
assertEquals(dr, new Uint8Array([9, 9, 1, 9, 9]));
dr = new Uint8Array([9, 9, 9, 9, 9]);
r = await m.read(dr, 1, 3, 5);
assertEquals(r.bytesRead, 3);
assertEquals(dr, new Uint8Array([9, 1, 2, 3, 9]));
});
Deno.test('read over', async () => {
let r;
const m = new MemoryFile();
const dw = new Uint8Array([1, 2, 3, 4, 5]);
r = await m.write(dw, 0, dw.length, 5);
assertEquals(r.bytesWritten, dw.length);
const dr = new Uint8Array(dw.length);
r = await m.read(dr, 0, dw.length, 6);
assertEquals(r.bytesRead, 4);
assertEquals(dr, new Uint8Array([2, 3, 4, 5, 0]));
});
Deno.test('read multiple blocks', async () => {
let r;
const m = new MemoryFile();
const dw = new Uint8Array(Math.round(BS * 5.5));
for (let i = 0; i < dw.length; i++) {
dw[i] = i % 256;
}
r = await m.write(dw, 0, dw.length, 5);
assertEquals(r.bytesWritten, dw.length);
const dr = new Uint8Array(dw.length);
r = await m.read(dr, 0, dw.length, 5);
assertEquals(r.bytesRead, dw.length);
assertEquals(dr, dw);
});
Deno.test('truncate bigger', async () => {
for (const size of [0, BS - 1, BS, BS + 1, BS * 2, BS * 3.5]) {
const m = new MemoryFile(size);
const dw = new Uint8Array(size);
for (let i = dw.length; i--;) {
dw[i] = (i + 1) % 256;
}
// deno-lint-ignore no-await-in-loop
await m.write(dw, 0, dw.length, 0);
for (const more of [1, 22, 333, 4444, 55555, 666666]) {
const bigger = size + more;
// deno-lint-ignore no-await-in-loop
await m.truncate(bigger);
// deno-lint-ignore no-await-in-loop
const r = await m.stat();
assertEquals(r.size, bigger);
assertEquals(r.blocks, Math.ceil(bigger / BS));
const dr = new Uint8Array(size);
// deno-lint-ignore no-await-in-loop
await m.read(dr, 0, size, 0);
assertEquals(dr, dw);
const dm = new Uint8Array(more);
dm.fill(1);
// deno-lint-ignore no-await-in-loop
await m.read(dm, 0, more, size);
for (let i = 0; i < more; i++) {
if (dm[i] !== 0) {
throw new Error(`[${i}] = ${dm[i]}`);
}
}
}
}
});
Deno.test('truncate smaller', async () => {
for (const size of [0, BS - 1, BS, BS + 1, BS * 2, BS * 3.5]) {
for (const more of [1, 22, 333, 4444, 55555, 666666]) {
const bigger = size + more;
const m = new MemoryFile(bigger);
const dw = new Uint8Array(bigger);
for (let i = dw.length; i--;) {
dw[i] = (i + 1) % 256;
}
// deno-lint-ignore no-await-in-loop
await m.write(dw, 0, dw.length, 0);
// deno-lint-ignore no-await-in-loop
await m.truncate(size);
// deno-lint-ignore no-await-in-loop
const r = await m.stat();
assertEquals(r.size, size);
assertEquals(r.blocks, Math.ceil(size / BS));
// deno-lint-ignore no-await-in-loop
await m.truncate(bigger);
const drKeep = new Uint8Array(size);
// deno-lint-ignore no-await-in-loop
await m.read(drKeep, 0, size, 0);
assertEquals(drKeep, dw.subarray(0, size));
const drLost = new Uint8Array(more);
// deno-lint-ignore no-await-in-loop
await m.read(drLost, 0, more, size);
for (let i = 0; i < more; i++) {
if (drLost[i] !== 0) {
throw new Error(`[${i}] = ${drLost[i]}`);
}
}
}
}
});
/*
Deno.test('like node', async () => {
const m = new MemoryFile();
const f = await open('LICENSE.txt', 'r');
try {
const n: FileLike = f;
let d;
{
const { size } = await n.stat();
d = new Uint8Array(size);
await n.read(d, 0, d.length, 0);
await m.write(d, 0, d.length, 0);
}
for (let i = 0, o = 0, l = 0; o < d.length; i++) {
const md = new Uint8Array(l + 2);
const nd = new Uint8Array(l + 2);
nd[0] = md[0] = (i % 10) + 1;
nd[l + 1] = md[l + 1] = ((i + 1) % 10) + 1;
// deno-lint-ignore no-await-in-loop
const mr = await m.read(md, 1, l, o);
// deno-lint-ignore no-await-in-loop
const nr = await n.read(nd, 1, l, o);
assertEquals(mr.bytesRead, nr.bytesRead);
assertEquals(md, nd);
l = Math.round((l || 1) * 1.5);
o += l;
}
} finally {
await f.close();
}
});
*/