-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest.js
455 lines (429 loc) · 13.4 KB
/
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
'use strict';
var HashTable = require('./index.js');
var Node = { crypto: require('crypto') };
function Assert(description, a, b) {
var x = a;
var y = b;
if (x === y) return;
throw new Error(description + ': ' + x + ' !== ' + y);
}
// Log result and value of each operation (slow):
var Debug = false;
function Hash(buffer, offset, size) {
if (size === 0) return '(0)';
if (size <= 16) {
var hash = buffer.toString('hex', offset, offset + size);
} else {
var hash = Node.crypto.createHash('SHA256');
hash.update(buffer.slice(offset, offset + size));
hash = hash.digest('hex').slice(0, 32);
}
return hash + ' (' + size + ')';
}
function Log(string) { console.log(string); }
function Random() { return Math.random(); }
var BAR_DOUBLE = new Array(80 + 1).join('=');
var BAR_SINGLE = new Array(80 + 1).join('-');
// For keySizes 20 and above:
var KEY_RND = Node.crypto.randomBytes(8 * 1024 * 1024);
// For keySizes 4, 8, 12 and 16:
var KEY_SEQ = (function() {
var buffer = Buffer.alloc(8 * 1024 * 1024);
var index = 0;
var offset = 0;
while (offset < buffer.length) {
buffer.writeUInt32BE(index++, offset);
offset += 4;
}
return buffer;
})();
// Tracks canonical value state for each element:
var VALUE = Node.crypto.randomBytes(8 * 1024 * 1024);
// Source of entropy for setting new values:
var ENTROPY = Node.crypto.randomBytes(VALUE.length);
var ENTROPY_OFFSET = 0;
// Receives value for get():
var TARGET = Buffer.alloc(65536 * 4);
function Test(keySize, valueSize, many, cache) {
Assert('Number.isInteger(keySize)', Number.isInteger(keySize), true);
Assert('Number.isInteger(valueSize)', Number.isInteger(valueSize), true);
Assert('many', many === true || many === false, true);
Assert('cache', cache === true || cache === false, true);
var key = keySize <= 16 ? KEY_SEQ : KEY_RND;
var value = VALUE;
var bucket = HashTable.bucket(keySize, valueSize);
var max = Math.floor(Math.min(
key.length / keySize,
value.length / valueSize,
many ? (Math.random() < 0.5 ? 1048576 : 65536) : 64
));
var elements = Math.floor(Math.random() * max) || max;
Assert('Number.isInteger(elements)', Number.isInteger(elements), true);
Assert('elements > 0', elements > 0, true);
if (Math.random() < 0.3) {
var elementsMin = 0;
} else if (Math.random() < 0.2) {
var elementsMin = Math.round(elements * 1.2);
} else {
var elementsMin = Math.floor(Math.random() * elements);
}
if (Math.random() < 0.3) {
var elementsMax = 0;
} else if (Math.random() < 0.05) {
var elementsMax = HashTable.ELEMENTS_MAX;
} else {
var elementsMax = elementsMin + Math.floor(Math.random() * elements);
}
var table = new HashTable(keySize, valueSize, elementsMin, elementsMax);
Assert('table.capacity >= elementsMin', table.capacity >= elementsMin, true);
var tableLength = 0;
var state = Buffer.alloc(elements);
var leader = true;
if (Debug) Log('');
if (Debug) Log(BAR_DOUBLE);
Log(
'HashTable:' +
' keySize=' + keySize.toString().padEnd(3, ' ') +
' valueSize=' + valueSize.toString().padEnd(6, ' ') +
' buffers=' + table.tables.length.toString().padEnd(5, ' ') +
' elements=' + elements
);
if (Debug) Log(BAR_DOUBLE);
function Mutate(id, reset) {
var keyOffset = id * keySize;
var valueOffset = id * valueSize;
function Cache() {
if (!cache) return;
var source = ENTROPY;
var sourceOffset = (ENTROPY_OFFSET += valueSize);
if (sourceOffset + valueSize > ENTROPY.length) {
sourceOffset = ENTROPY_OFFSET = 0;
}
var sourceHash1 = Hash(source, sourceOffset, valueSize);
var result = table.cache(key, keyOffset, source, sourceOffset);
var sourceHash2 = Hash(source, sourceOffset, valueSize);
Assert('sourceHash', sourceHash2, sourceHash1);
if (Debug) {
var sourceHash = Hash(source, sourceOffset, valueSize);
Log(' cache() result=' + result + ' value=' + sourceHash);
}
if (result === 1) {
// Updated
Assert('result', result, state[id]);
} else if (result === 2) {
// Evicted
state[id] = 1;
} else {
// Inserted
Assert('result', result, 0);
state[id] = 1;
tableLength++;
}
Assert(
'source.copy()',
source.copy(value, valueOffset, sourceOffset, sourceOffset + valueSize),
valueSize
);
Assert('table.length', table.length, tableLength);
}
function Exist(cached) {
// `cached` indicates whether element is certain to exist:
Assert('cached', cached === 0 || cached === 1, true);
var result = table.exist(key, keyOffset);
if (Debug) Log(' exist() result=' + result);
Assert('result', result, (cache && result === 0) ? cached : state[id]);
Assert('table.length', table.length, tableLength);
}
function Get(cached) {
Assert('cached', cached === 0 || cached === 1, true);
var target = TARGET;
var targetOffset = Math.floor(
Math.random() * (TARGET.length - valueSize)
);
Assert('targetOffset', targetOffset + valueSize <= TARGET.length, true);
if (!state[id] || cache) {
var targetHex = target.toString(
'hex',
targetOffset,
targetOffset + valueSize
);
}
var result = table.get(key, keyOffset, target, targetOffset);
if (Debug) {
if (result === 1) {
var targetHash = Hash(target, targetOffset, valueSize);
Log(' get() result=' + result + ' value=' + targetHash);
} else {
Log(' get() result=' + result);
}
}
Assert('result', result, (cache && result === 0) ? cached : state[id]);
Assert('table.length', table.length, tableLength);
if (result === 1) {
var targetSlice = target.slice(targetOffset, targetOffset + valueSize);
var valueSlice = value.slice(valueOffset, valueOffset + valueSize);
Assert('target', targetSlice.equals(valueSlice), true);
} else {
Assert(
'target',
target.toString('hex', targetOffset, targetOffset + valueSize),
targetHex
);
}
}
function Set() {
if (cache) return;
var source = ENTROPY;
var sourceOffset = (ENTROPY_OFFSET += valueSize);
if (sourceOffset + valueSize > ENTROPY.length) {
sourceOffset = ENTROPY_OFFSET = 0;
}
var result = table.set(key, keyOffset, source, sourceOffset);
if (Debug) {
var sourceHash = Hash(source, sourceOffset, valueSize);
Log(' set() result=' + result + ' value=' + sourceHash);
}
Assert('result', result, state[id]);
Assert(
'source.copy()',
source.copy(value, valueOffset, sourceOffset, sourceOffset + valueSize),
valueSize
);
if (!state[id]) {
state[id] = 1;
tableLength++;
}
Assert('table.length', table.length, tableLength);
}
function Unset() {
var result = table.unset(key, keyOffset);
if (Debug) Log(' unset() result=' + result);
if (cache) {
Assert('result', result, result === 1 ? state[id] : 0);
if (result === 1) {
state[id] = 0;
tableLength--;
}
} else {
Assert('result', result, state[id]);
if (state[id]) {
state[id] = 0;
tableLength--;
}
}
Assert('table.length', table.length, tableLength);
}
if (leader) {
leader = false;
} else if (Debug) {
Log('');
Log(BAR_SINGLE);
}
if (Debug) {
Log('');
Log(' key=' + Hash(key, keyOffset, keySize));
Log('');
}
if (reset) {
Get(0);
Exist(0);
Unset();
Get(0);
Exist(0);
} else {
Get(0);
Exist(0);
if (Random() < 0.50) {
Unset();
Get(0);
Exist(0);
}
if (Random() < 0.50) {
Cache();
Set();
Get(1);
Exist(1);
}
if (Random() < 0.25) {
Unset();
Get(0);
Exist(0);
}
if (Random() < 0.25) {
Cache();
Set();
Get(1);
Exist(1);
}
}
var stats = { capacity: 0, size: 0 };
for (var index = 0, length = table.tables.length; index < length; index++) {
var tableSize = table.tables[index].buffer.length;
stats.capacity += (tableSize / table.bucket) * 8;
stats.size += tableSize;
}
Assert('table.capacity', table.capacity, stats.capacity);
Assert('table.length', table.length, tableLength);
Assert('table.load', table.load, tableLength / stats.capacity);
Assert('table.size', table.size, stats.size);
}
function Iterate() {
for (var id = 0; id < elements; id++) Mutate(id, false);
}
function Reset() {
for (var id = 0; id < elements; id++) Mutate(id, true);
}
function Skip() {
var length = Math.round(Random() * elements / 4);
while (length--) Mutate(Math.floor(Random() * elements), false);
}
Iterate();
Skip();
Iterate();
Reset();
}
// Exception message constants must be strings:
[ 'ERROR_MAXIMUM_CAPACITY_EXCEEDED', 'ERROR_MODE', 'ERROR_SET'].forEach(
function(key) {
Assert('HashTable.' + key, typeof HashTable[key], 'string');
}
);
// Constants must be integers:
[
'KEY_MIN',
'KEY_MAX',
'VALUE_MIN',
'VALUE_MAX',
'BUFFERS_MIN',
'BUFFERS_MAX',
'ELEMENTS_MIN',
'ELEMENTS_MAX',
'BUCKETS_MIN',
'BUCKETS_MAX',
'BUFFER_MAX'
].forEach(
function(key) {
Assert('HashTable.' + key, typeof HashTable[key], 'number');
Assert('HashTable.' + key, Number.isSafeInteger(HashTable[key]), true);
Assert('HashTable.' + key, HashTable[key] >= 0, true);
}
);
// HashTable must throw exceptions:
[
[
HashTable.KEY_MIN - 1, 0, 1, 0,
'keySize must be at least ' + HashTable.KEY_MIN
],
[
HashTable.KEY_MAX + 1, 0, 1, 0,
'keySize must be at most ' + HashTable.KEY_MAX
],
[
4, HashTable.VALUE_MIN - 1, 1, 0,
'valueSize must be at least ' + HashTable.VALUE_MIN
],
[
4, HashTable.VALUE_MAX + 1, 1, 0,
'valueSize must be at most ' + HashTable.VALUE_MAX
],
[
4, 0, HashTable.ELEMENTS_MIN - 1, 0,
'elementsMin must be at least ' + HashTable.ELEMENTS_MIN
],
[
4, 0, HashTable.ELEMENTS_MAX + 1, 0,
'elementsMin must be at most ' + HashTable.ELEMENTS_MAX
],
[
4, 0, 123, 122,
'elementsMax must be at least 123'
],
[
4, 0, 1, HashTable.ELEMENTS_MAX + 1,
'elementsMax must be at most ' + HashTable.ELEMENTS_MAX
],
[
HashTable.KEY_MIN + 1, 0, 1, 0,
'keySize must be a multiple of 4'
],
[
4, 65536, 1024 * 1024 * 1024, 0,
HashTable.ERROR_MAXIMUM_CAPACITY_EXCEEDED
]
].forEach(
function(args) {
Assert('args.length === 5', args.length === 5, true);
var error;
try {
var keySize = args[0];
var valueSize = args[1];
var elementsMin = args[2];
var elementsMax = args[3];
new HashTable(keySize, valueSize, elementsMin, elementsMax);
} catch (exception) {
error = exception.message;
}
Assert('error', error, args[4]);
}
);
// Values used by README must match constants:
Assert('README a maximum of 64 bytes', 64, HashTable.KEY_MAX);
Assert('README a maximum of 1 MB', 1024 * 1024, HashTable.VALUE_MAX);
Assert('README 4,294,967,296 elements', 4294967296, HashTable.ELEMENTS_MAX);
Assert('README 16 TB', 17592186044416, HashTable.BUFFERS_MAX * 2147483648);
// Maximum load factor must not be artificially restricted by too few buckets:
(function() {
var elements = HashTable.BUCKETS_MAX * 8;
var keySize = HashTable.KEY_MIN;
while (keySize <= HashTable.KEY_MAX) {
var valueSize = HashTable.VALUE_MIN;
while (valueSize <= HashTable.VALUE_MAX) {
var elements = HashTable.BUCKETS_MAX * 8;
while (elements < HashTable.ELEMENTS_MAX) {
var buffers = HashTable.buffers(keySize, valueSize, elements);
var buckets = HashTable.buckets(elements, buffers);
Assert('buckets >= 64', buckets >= 64, true);
elements = elements * 2;
}
valueSize = valueSize === 0 ? 1 : valueSize * 2;
}
keySize += 4;
}
})();
var KEY_SIZES = [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64];
Assert('KEY_SIZES[0]', KEY_SIZES[0], HashTable.KEY_MIN);
Assert('KEY_SIZES[N]', KEY_SIZES[KEY_SIZES.length - 1], HashTable.KEY_MAX);
var VALUE_SIZES = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 16, 18, 20, 22, 23,
24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 61,
64, 128, 256, 1024, 4096, 65536, 65537
];
Assert(
'TARGET.length > VALUE_SIZES[N]',
TARGET.length > VALUE_SIZES[VALUE_SIZES.length - 1],
true
);
var KEY_RND_HASH = Hash(KEY_RND, 0, KEY_RND.length);
var KEY_SEQ_HASH = Hash(KEY_SEQ, 0, KEY_SEQ.length);
var ENTROPY_HASH = Hash(ENTROPY, 0, ENTROPY.length);
KEY_SIZES.forEach(
function(keySize) {
var manyIndex = Math.floor(Math.random() * VALUE_SIZES.length);
var cacheIndex = Math.floor(Math.random() * VALUE_SIZES.length);
VALUE_SIZES.forEach(
function(valueSize, valueSizeIndex) {
var many = valueSizeIndex === manyIndex && Math.random() < 0.5;
var cache = valueSizeIndex === cacheIndex;
Test(keySize, valueSize, many, cache);
}
);
}
);
// Read-only buffers must not be modified by any HashTable methods:
Assert('KEY_RND_HASH', Hash(KEY_RND, 0, KEY_RND.length), KEY_RND_HASH);
Assert('KEY_SEQ_HASH', Hash(KEY_SEQ, 0, KEY_SEQ.length), KEY_SEQ_HASH);
Assert('ENTROPY_HASH', Hash(ENTROPY, 0, ENTROPY.length), ENTROPY_HASH);
Log(BAR_DOUBLE);
Log('PASSED ALL TESTS');
Log(BAR_DOUBLE);