forked from jlongster/absurd-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtual-file.test.js
304 lines (266 loc) · 7.18 KB
/
virtual-file.test.js
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
296
297
298
299
300
301
302
303
304
import {
readChunks,
writeChunks,
File,
getBoundaryIndexes
} from './virtual-file';
import MemoryBackend from './backend-memory';
import * as fc from 'fast-check';
function toArray(buffer) {
return Array.from(new Uint8Array(buffer));
}
function makeChunks(chunkSize, data) {
let arr = [];
for (let i = 0; i < data.length; i += chunkSize) {
arr.push({
pos: i,
data: Int8Array.from(data.slice(i, i + chunkSize)).buffer
});
}
return arr;
}
function zeroBuffer(size) {
let buffer = new ArrayBuffer(size);
let view = new Uint8Array(buffer);
for (let i = 0; i < size; i++) {
view[i] = 0;
}
return buffer;
}
describe('chunks', () => {
test('reading', () => {
let chunks = makeChunks(3, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
expect(toArray(readChunks(chunks, 1, 7))).toEqual([1, 2, 3, 4, 5, 6]);
expect(toArray(readChunks(chunks, 0, 7))).toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(toArray(readChunks(chunks, 0, 10))).toEqual([
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
]);
expect(toArray(readChunks(chunks, 5, 12))).toEqual([5, 6, 7, 8, 9, 0, 0]);
});
test('writing', () => {
let buffer = new ArrayBuffer(20);
let view = new Uint8Array(buffer);
for (let i = 0; i < buffer.byteLength; i++) {
view[i] = i;
}
let pos = 2;
let chunkSize = 8;
let offset = 4;
let length = 10;
let res = writeChunks(
new Uint8Array(
buffer,
offset,
Math.max(Math.min(length, buffer.byteLength - length), 0)
),
chunkSize,
pos,
pos + length
);
expect(res.map(res => ({ ...res, data: toArray(res.data) }))).toEqual([
{ pos: 0, offset: 2, length: 6, data: [0, 0, 4, 5, 6, 7, 8, 9] },
{ pos: 8, offset: 0, length: 4, data: [10, 11, 12, 13, 0, 0, 0, 0] }
]);
});
});
describe('reading file', () => {
function readPropTest(bufferView, chunkSize, pos, length) {
if (bufferView.buffer.byteLength < length) {
return;
}
let files = { 'file.db': bufferView.buffer };
let file = new File(
'file.db',
chunkSize,
new MemoryBackend(files, chunkSize)
);
file.open();
let offset = 0;
let len = Math.max(
Math.min(length, bufferView.length - Math.max(pos, 0)),
0
);
let buffer = new ArrayBuffer(len);
let bytesRead = file.read(buffer, offset, length, pos);
if (buffer.byteLength === 0 || pos < 0) {
expect(bytesRead).toBe(0);
expect(toArray(buffer)).toEqual(toArray(zeroBuffer(len)));
} else {
expect(bytesRead).toBe(len);
expect(toArray(buffer)).toEqual(
toArray(new Uint8Array(bufferView.buffer, pos, len))
);
}
}
test('read-counter', () => {
let counter = [Int8Array.from([0]), 1, 0, 1];
readPropTest(...counter);
});
test('read-prop', () => {
fc.assert(
fc.property(
// buffer
fc.int8Array({ maxLength: 1000 }),
// chunk size
fc.integer(1, 1000),
// position
fc.integer(-1, 100),
// length
fc.integer(-1, 100),
readPropTest
),
{ numRuns: 1000 }
);
});
});
describe('writing file', () => {
function applyWrite(
file,
original,
chunkSize,
writeDataView,
offset,
length,
pos
) {
let bytesWritten = file.write(writeDataView.buffer, offset, length, pos);
// Check bytes written
let len = Math.min(length, writeDataView.length);
if (pos < 0 || len <= 0) {
expect(bytesWritten).toBe(0);
} else {
expect(bytesWritten).toBe(len);
// Manually make the writes into our test buffer
let fullLength = pos + len;
// The length of the file must be aligned to the boundary
let [index] = getBoundaryIndexes(chunkSize, fullLength - 1, fullLength);
fullLength = index + chunkSize;
if (fullLength > original.byteLength) {
// Resize
let buffer = new ArrayBuffer(fullLength);
new Uint8Array(buffer).set(new Uint8Array(original));
original = buffer;
}
// Set
new Uint8Array(original).set(
new Uint8Array(writeDataView.buffer, offset, len),
pos,
len
);
}
return { original, bytesWritten };
}
function writePropTest(bufferView, chunkSize, writeDataView, length, pos) {
let files = { 'file.db': bufferView.buffer };
let backend = new MemoryBackend(files, chunkSize);
let file = new File('file.db', chunkSize, backend);
file.open();
let original = backend.getFile('file.db').data.slice(0);
let maxPos = file.getattr().size;
let offset = 0;
let result = applyWrite(
file,
original,
chunkSize,
writeDataView,
offset,
length,
pos
);
original = result.original;
if (result.bytesWritten > 0) {
maxPos = Math.max(maxPos, pos + result.bytesWritten);
}
file.fsync();
let fileInfo = backend.getFile('file.db');
expect(toArray(fileInfo.data)).toEqual(toArray(original));
expect(fileInfo.size).toBe(maxPos);
}
function writePropTest2(bufferView, chunkSize, arr) {
let files = { 'file.db': bufferView.buffer };
let backend = new MemoryBackend(files, chunkSize);
let file = new File('file.db', chunkSize, backend);
file.open();
let original2 = backend.getFile('file.db').data.slice(0);
let original = backend.getFile('file.db').data.slice(0);
let maxPos = file.getattr().size;
for (let writeInfo of arr) {
let offset = 0;
let [writeDataView, pos, length] = writeInfo;
let result = applyWrite(
file,
original,
chunkSize,
writeDataView,
offset,
length,
pos
);
original = result.original;
if (result.bytesWritten > 0) {
maxPos = Math.max(maxPos, pos + result.bytesWritten);
}
}
expect(toArray(backend.getFile('file.db').data)).toEqual(
toArray(original2)
);
file.fsync();
expect(toArray(backend.getFile('file.db').data)).toEqual(toArray(original));
let fileInfo = backend.getFile('file.db');
expect(toArray(fileInfo.data)).toEqual(toArray(original));
expect(fileInfo.size).toBe(maxPos);
}
test('write-counter', () => {
let counter = [Uint8Array.from([]), 1, Uint8Array.from([]), 0, 1];
writePropTest(...counter);
});
test('write-prop1', () => {
fc.assert(
fc.property(
// buffer
fc.uint8Array({ maxLength: 1000 }),
// chunk size
fc.integer(1, 1000),
// writeData
fc.uint8Array({ maxLength: 1000 }),
// position
fc.integer(-1, 1000),
// length
fc.integer(-1, 1000),
writePropTest
)
);
});
test('write-prop2', () => {
fc.assert(
fc.property(
// buffer
fc.uint8Array({ maxLength: 1000 }),
// chunk size
fc.integer(1, 1000),
// many writes!
fc.array(
fc.tuple(
// writeData
fc.uint8Array({ maxLength: 1000 }),
// position
fc.integer(-1, 1000),
// length
fc.integer(-1, 1000)
)
),
writePropTest2
),
{ numRuns: 1000 }
);
});
});