-
Notifications
You must be signed in to change notification settings - Fork 46
/
test.js
60 lines (42 loc) · 1.17 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 '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();
});