forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.test.js
More file actions
124 lines (92 loc) · 3.46 KB
/
PriorityQueue.test.js
File metadata and controls
124 lines (92 loc) · 3.46 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
124
import PriorityQueue from '../PriorityQueue';
describe('PriorityQueue', () => {
it('should create default priority queue', () => {
const priorityQueue = new PriorityQueue();
expect(priorityQueue).toBeDefined();
});
it('should insert items to the queue and respect priorities', () => {
const priorityQueue = new PriorityQueue();
priorityQueue.add(10, 1);
expect(priorityQueue.peek()).toBe(10);
priorityQueue.add(5, 2);
expect(priorityQueue.peek()).toBe(10);
priorityQueue.add(100, 0);
expect(priorityQueue.peek()).toBe(100);
});
it('should be possible to use objects in priority queue', () => {
const priorityQueue = new PriorityQueue();
const user1 = { name: 'Mike' };
const user2 = { name: 'Bill' };
const user3 = { name: 'Jane' };
priorityQueue.add(user1, 1);
expect(priorityQueue.peek()).toBe(user1);
priorityQueue.add(user2, 2);
expect(priorityQueue.peek()).toBe(user1);
priorityQueue.add(user3, 0);
expect(priorityQueue.peek()).toBe(user3);
});
it('should poll from queue with respect to priorities', () => {
const priorityQueue = new PriorityQueue();
priorityQueue.add(10, 1);
priorityQueue.add(5, 2);
priorityQueue.add(100, 0);
priorityQueue.add(200, 0);
expect(priorityQueue.poll()).toBe(100);
expect(priorityQueue.poll()).toBe(200);
expect(priorityQueue.poll()).toBe(10);
expect(priorityQueue.poll()).toBe(5);
});
it('should be possible to change priority of head node', () => {
const priorityQueue = new PriorityQueue();
priorityQueue.add(10, 1);
priorityQueue.add(5, 2);
priorityQueue.add(100, 0);
priorityQueue.add(200, 0);
expect(priorityQueue.peek()).toBe(100);
priorityQueue.changePriority(100, 10);
priorityQueue.changePriority(10, 20);
expect(priorityQueue.poll()).toBe(200);
expect(priorityQueue.poll()).toBe(5);
expect(priorityQueue.poll()).toBe(100);
expect(priorityQueue.poll()).toBe(10);
});
it('should be possible to change priority of internal nodes', () => {
const priorityQueue = new PriorityQueue();
priorityQueue.add(10, 1);
priorityQueue.add(5, 2);
priorityQueue.add(100, 0);
priorityQueue.add(200, 0);
expect(priorityQueue.peek()).toBe(100);
priorityQueue.changePriority(200, 10);
priorityQueue.changePriority(10, 20);
expect(priorityQueue.poll()).toBe(100);
expect(priorityQueue.poll()).toBe(5);
expect(priorityQueue.poll()).toBe(200);
expect(priorityQueue.poll()).toBe(10);
});
it('should be possible to change priority along with node addition', () => {
const priorityQueue = new PriorityQueue();
priorityQueue.add(10, 1);
priorityQueue.add(5, 2);
priorityQueue.add(100, 0);
priorityQueue.add(200, 0);
priorityQueue.changePriority(200, 10);
priorityQueue.changePriority(10, 20);
priorityQueue.add(15, 15);
expect(priorityQueue.poll()).toBe(100);
expect(priorityQueue.poll()).toBe(5);
expect(priorityQueue.poll()).toBe(200);
expect(priorityQueue.poll()).toBe(15);
expect(priorityQueue.poll()).toBe(10);
});
it('should be possible to search in priority queue by value', () => {
const priorityQueue = new PriorityQueue();
priorityQueue.add(10, 1);
priorityQueue.add(5, 2);
priorityQueue.add(100, 0);
priorityQueue.add(200, 0);
priorityQueue.add(15, 15);
expect(priorityQueue.hasValue(70)).toBe(false);
expect(priorityQueue.hasValue(15)).toBe(true);
});
});