Skip to content

Commit ffdd09a

Browse files
feat: add tests for KeyPriorityQueue TheAlgorithms#1298
1 parent 8b5247a commit ffdd09a

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import { KeyPriorityQueue } from '../KeyPriorityQueue.js'
2+
3+
describe('KeyPriorityQueue', () => {
4+
5+
test('Check heap correctly ordered', () => {
6+
// create queue and fill it
7+
const priorities = [5, 2, 4, 1, 7, 6, 3, 8]
8+
const queue = new KeyPriorityQueue()
9+
for (let i = 0; i < priorities.length; i++) {
10+
queue.push(i, priorities[i])
11+
}
12+
13+
// result from popping all elements from the queue
14+
let res = []
15+
while (!queue.isEmpty()) {
16+
res.push(queue.pop())
17+
}
18+
19+
expect(res).toEqual([1, 2, 3, 5, 7, 6, 4, 8])
20+
})
21+
})
22+
23+
describe('Method isEmpty', () => {
24+
25+
test('Check heap is empty', () => {
26+
const queue = new KeyPriorityQueue()
27+
let res = queue.isEmpty()
28+
expect(res).toEqual(true)
29+
})
30+
31+
test('Check heap is not empty', () => {
32+
const queue = new KeyPriorityQueue()
33+
queue.push(0, 2)
34+
let res = queue.isEmpty()
35+
expect(res).toEqual(false)
36+
})
37+
})
38+
39+
describe('Methods push and pop', () => {
40+
41+
test('Test Case 1', () => {
42+
// create queue
43+
const queue = new KeyPriorityQueue()
44+
queue.push(0, 3)
45+
queue.push(1, 7)
46+
queue.push(2, 9)
47+
queue.push(3, 13)
48+
// create expected queue
49+
const expectedQueue = new KeyPriorityQueue()
50+
expectedQueue.push(1, 7)
51+
expectedQueue.push(3, 13)
52+
expectedQueue.push(2, 9)
53+
// result from popping element from the queue
54+
queue.pop()
55+
expect(queue).toEqual(expectedQueue)
56+
})
57+
58+
test('Test Case 2', () => {
59+
// create queue
60+
const queue = new KeyPriorityQueue()
61+
queue.push(0, 3)
62+
queue.push(1, 9)
63+
queue.push(2, 7)
64+
queue.push(3, 13)
65+
// create expected queue
66+
const expectedQueue = new KeyPriorityQueue()
67+
expectedQueue.push(2, 7)
68+
expectedQueue.push(1, 9)
69+
expectedQueue.push(3, 13)
70+
// result from popping element from the queue
71+
queue.pop()
72+
expect(queue).toEqual(expectedQueue)
73+
})
74+
75+
test('Test Case 3', () => {
76+
// create queue
77+
const queue = new KeyPriorityQueue()
78+
queue.push(0, 3)
79+
queue.push(1, 7)
80+
queue.push(2, 9)
81+
queue.push(3, 12)
82+
queue.push(4, 13)
83+
// create expected queue
84+
const expectedQueue = new KeyPriorityQueue()
85+
expectedQueue.push(1, 7)
86+
expectedQueue.push(3, 12)
87+
expectedQueue.push(2, 9)
88+
expectedQueue.push(4, 13)
89+
// result from popping element from the queue
90+
queue.pop()
91+
expect(queue).toEqual(expectedQueue)
92+
})
93+
})
94+
95+
describe('Method contains', () => {
96+
97+
test('Check heap does not contain element', () => {
98+
const queue = new KeyPriorityQueue()
99+
let res = queue.contains(0)
100+
expect(res).toEqual(false)
101+
})
102+
103+
test('Check heap contains element', () => {
104+
const queue = new KeyPriorityQueue()
105+
queue.push(0, 2)
106+
let res = queue.contains(0)
107+
expect(res).toEqual(true)
108+
})
109+
})
110+
111+
describe('Method update', () => {
112+
113+
test('Update without change in position', () => {
114+
// create queue
115+
const queue = new KeyPriorityQueue()
116+
queue.push(0, 3)
117+
queue.push(1, 5)
118+
queue.push(2, 7)
119+
queue.push(3, 11)
120+
// create expected queue
121+
const expectedQueue = new KeyPriorityQueue()
122+
expectedQueue.push(0, 3)
123+
expectedQueue.push(1, 5)
124+
expectedQueue.push(2, 7)
125+
expectedQueue.push(3, 11)
126+
// result from updating to similar priority
127+
queue.update(0, 2)
128+
expect(queue).toEqual(expectedQueue)
129+
})
130+
131+
test('Update with change in position', () => {
132+
// create queue
133+
const queue = new KeyPriorityQueue()
134+
queue.push(0, 3)
135+
queue.push(1, 5)
136+
queue.push(2, 7)
137+
queue.push(3, 11)
138+
// create expected queue
139+
const expectedQueue = new KeyPriorityQueue()
140+
expectedQueue.push(1, 5)
141+
expectedQueue.push(3, 11)
142+
expectedQueue.push(2, 7)
143+
expectedQueue.push(0, 9)
144+
// result from updating to similar priority
145+
queue.update(0, 9)
146+
expect(queue).toEqual(expectedQueue)
147+
})
148+
})

0 commit comments

Comments
 (0)