Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
123 lines (95 loc) · 3.54 KB
/
Copy pathtest.js
File metadata and controls
123 lines (95 loc) · 3.54 KB
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
import test from 'node:test';
import assert from 'node:assert/strict';
import FlatQueue from './index.js';
test('does not push beyond the storage allocated for a fractional capacity', () => {
for (const capacity of [1, 1.5, 1.9]) {
const queue = new FlatQueue(capacity);
queue.push(42, 1);
assert.throws(() => queue.push(43, 0), RangeError);
assert.equal(queue.capacity, 1);
assert.equal(queue.length, 1);
assert.equal(queue.pop(), 42);
assert.equal(queue.pop(), undefined);
}
});
test('does not push when the allocated capacity is zero', () => {
for (const capacity of [0, 0.5]) {
const queue = new FlatQueue(capacity);
assert.throws(() => queue.push(42, 1), RangeError);
assert.equal(queue.capacity, 0);
assert.equal(queue.length, 0);
}
});
const data = [];
for (let i = 0; i < 100; i++) {
data.push(Math.floor(100 * Math.random()));
}
const sorted = data.slice().sort((a, b) => a - b);
test('maintains a priority queue', () => {
const queue = new FlatQueue();
for (let i = 0; i < data.length; i++) queue.push(i, data[i]);
assert.equal(queue.peekValue(), sorted[0]);
assert.equal(data[queue.peek()], sorted[0]);
const result = [];
while (queue.length) result.push(data[queue.pop()]);
assert.deepEqual(result, sorted);
});
test('handles edge cases with few elements', () => {
const queue = new FlatQueue();
queue.push(0, 2);
queue.push(1, 1);
queue.pop();
queue.pop();
queue.pop();
queue.push(2, 2);
queue.push(3, 1);
assert.equal(queue.pop(), 3);
assert.equal(queue.pop(), 2);
assert.equal(queue.pop(), undefined);
assert.equal(queue.peek(), undefined);
assert.equal(queue.peekValue(), undefined);
});
test('shrinks internal arrays when calling shrink', () => {
const queue = new FlatQueue();
for (let i = 0; i < 10; i++) queue.push(i, i);
while (queue.length) queue.pop();
assert.equal(queue.ids.length, 10);
assert.equal(queue.values.length, 10);
queue.shrink();
assert.equal(queue.ids.length, 0);
assert.equal(queue.values.length, 0);
});
test('uses typed arrays when given a capacity', () => {
const def = new FlatQueue(100);
assert.ok(def.ids instanceof Uint32Array);
assert.ok(def.values instanceof Float64Array);
assert.equal(def.ids.length, 100);
// values and ids constructors can be specialized
const narrow = new FlatQueue(100, Uint32Array, Uint16Array);
assert.ok(narrow.ids instanceof Uint16Array);
assert.ok(narrow.values instanceof Uint32Array);
const queue = new FlatQueue(data.length, Uint32Array);
for (let i = 0; i < data.length; i++) queue.push(i, data[i]);
const result = [];
while (queue.length) result.push(data[queue.pop()]);
assert.deepEqual(result, sorted);
// shrink is a no-op for typed-array-backed queues
queue.shrink();
assert.equal(queue.ids.length, data.length);
});
test('throws when pushing past a fixed capacity', () => {
const queue = new FlatQueue(3, Uint32Array, Uint32Array);
assert.equal(queue.capacity, 3);
for (let i = 0; i < 3; i++) queue.push(i, i);
assert.throws(() => queue.push(3, 3), RangeError);
// popping frees a slot again
queue.pop();
assert.doesNotThrow(() => queue.push(3, 3));
// regular-array queues have no capacity limit
const dynamic = new FlatQueue();
assert.equal(dynamic.capacity, Infinity);
assert.doesNotThrow(() => {
for (let i = 0; i < 1000; i++) dynamic.push(i, i);
});
});
test.run();