-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpriority_queue.test.js
83 lines (68 loc) · 2.13 KB
/
priority_queue.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
var { PriorityQueue } = require('../dist/js-data-structs.cjs');
describe('Check priority queue functions', () => {
it('should create an empty priority queue', () => {
var q = PriorityQueue();
expect(q.isEmpty()).toBe(true);
});
it('should return the element with highest priority', () => {
var q = PriorityQueue();
q.enqueue('this', 4);
q.enqueue('that', 1);
q.enqueue('here', 7);
expect(q.peek()).toBe('that');
q.enqueue('there', 3);
q.enqueue('where', 9);
q.enqueue('near', 0);
q.enqueue('hear', 2);
expect(q.peek()).toBe('near');
});
it('should dequeue the element with highest priority', () => {
var q = PriorityQueue();
q.enqueue('this', 4);
q.enqueue('that', 1);
q.enqueue('here', 7);
q.enqueue('there', 3);
q.enqueue('where', 9);
q.enqueue('near', 0);
q.enqueue('hear', 2);
q.dequeue();
expect(q.peek()).toBe('that');
q.dequeue();
expect(q.peek()).toBe('hear');
q.dequeue();
expect(q.peek()).toBe('there');
});
it('should return the dequeued element', () => {
var q = PriorityQueue();
q.enqueue('this', 4);
q.enqueue('that', 1);
q.enqueue('here', 7);
q.enqueue('there', 3);
q.enqueue('where', 9);
q.enqueue('near', 0);
q.enqueue('hear', 2);
var x = q.dequeue();
expect(x).toEqual({ data: 'near', key: 0 });
x = q.dequeue();
expect(x).toEqual({ data: 'that', key: 1 });
});
it('should be empty after all elements are dequeued', () => {
var q = PriorityQueue();
q.enqueue('this', 4);
q.enqueue('that', 1);
q.enqueue('here', 7);
q.enqueue('there', 3);
q.enqueue('where', 9);
q.enqueue('near', 0);
q.enqueue('hear', 2);
q.dequeue();
q.dequeue();
q.dequeue();
q.dequeue();
q.dequeue();
q.dequeue();
expect(q.isEmpty()).toBe(false);
q.dequeue();
expect(q.isEmpty()).toBe(true);
});
});