forked from mourner/tinyqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
80 lines (58 loc) · 1.62 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
import {test} from 'tape';
import TinyQueue 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', (t) => {
const queue = new TinyQueue();
for (let i = 0; i < data.length; i++) queue.push(data[i]);
t.equal(queue.peek(), sorted[0]);
const result = [];
while (queue.length) result.push(queue.pop());
t.same(result, sorted);
t.end();
});
test('accepts data in constructor', (t) => {
const queue = new TinyQueue(data.slice());
const result = [];
while (queue.length) result.push(queue.pop());
t.same(result, sorted);
t.end();
});
test('handles edge cases with few elements', (t) => {
const queue = new TinyQueue();
queue.push(2);
queue.push(1);
queue.pop();
queue.pop();
queue.pop();
queue.push(2);
queue.push(1);
t.equal(queue.pop(), 1);
t.equal(queue.pop(), 2);
t.equal(queue.pop(), undefined);
t.end();
});
test('handles init with empty array', (t) => {
const queue = new TinyQueue([]);
t.same(queue.data, []);
t.end();
});
test('updates few elements', (t) => {
const queue = new TinyQueue();
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(1);
let res = queue.update(4, (val) => { return val == 1 });
t.equal(res, true);
t.equal(queue.length, 4);
t.equal(queue.peek(), 1);
res = queue.update(4, (val) => { return val == 1 });
t.equal(res, true);
t.equal(queue.length, 4);
t.equal(queue.peek(), 2);
t.end();
});