-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.js
60 lines (43 loc) · 1.38 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
import {test} from 'uvu';
import * as assert from 'uvu/assert';
import FlatQueue from './index.js';
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.is(queue.peekValue(), sorted[0]);
assert.is(data[queue.peek()], sorted[0]);
const result = [];
while (queue.length) result.push(data[queue.pop()]);
assert.equal(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.is(queue.pop(), 3);
assert.is(queue.pop(), 2);
assert.is(queue.pop(), undefined);
assert.is(queue.peek(), undefined);
assert.is(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.is(queue.ids.length, 10);
assert.is(queue.values.length, 10);
queue.shrink();
assert.is(queue.ids.length, 0);
assert.is(queue.values.length, 0);
});
test.run();