This repository was archived by the owner on May 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_priorityqueue.py
More file actions
90 lines (70 loc) · 1.93 KB
/
Copy pathtest_priorityqueue.py
File metadata and controls
90 lines (70 loc) · 1.93 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
import unittest
from priorityqueue import *
class TestPriorityQueue(unittest.TestCase):
def test_insert(self):
pq = PriorityQueue(lambda x: x) # Value is the same as the object.
pq.insert(1)
pq.insert(2)
pq.insert(3)
self.assertEqual(pq.length, 3)
def test_getMin(self):
pq = PriorityQueue(lambda x: x) # Value is the same as the object.
pq.insert(3)
pq.insert(2)
pq.insert(1)
self.assertEqual(pq.getMin(), 1)
self.assertEqual(pq.length, 3)
pq = PriorityQueue(lambda x: -x) # Returns max this time.
pq.insert(3)
pq.insert(2)
pq.insert(1)
self.assertEqual(pq.getMin(), 3)
self.assertEqual(pq.length, 3)
pq = PriorityQueue(lambda x: -(x % 3))
pq.insert(3)
pq.insert(2)
pq.insert(1)
self.assertEqual(pq.getMin(), 2)
self.assertEqual(pq.length, 3)
def test_removeMin(self):
pq = PriorityQueue(lambda x: x) # Value is the same as the object.
pq.insert(3)
pq.insert(2)
pq.insert(1)
self.assertEqual(pq.removeMin(), 1)
self.assertEqual(pq.length, 2)
pq = PriorityQueue(lambda x: -x) # Returns max this time.
pq.insert(3)
pq.insert(2)
pq.insert(1)
self.assertEqual(pq.removeMin(), 3)
self.assertEqual(pq.length, 2)
pq = PriorityQueue(lambda x: -(x % 3))
pq.insert(3)
pq.insert(2)
pq.insert(1)
self.assertEqual(pq.removeMin(), 2)
self.assertEqual(pq.length, 2)
def test_isEmpty(self):
pq = PriorityQueue(lambda x: x) # Value is the same as the object.
pq.insert(3)
pq.insert(2)
pq.insert(1)
self.assertFalse(pq.isEmpty())
pq.removeMin()
self.assertFalse(pq.isEmpty())
pq.removeMin()
self.assertFalse(pq.isEmpty())
pq.removeMin()
self.assertTrue(pq.isEmpty())
def test_getValue(self):
pq = PriorityQueue(lambda x: x)
pq.insert(3)
pq.insert(2)
pq.insert(1)
self.assertEqual(pq.getValue(1), 1)
self.assertEqual(pq.getValue(2), 2)
self.assertEqual(pq.getValue(3), 3)
self.assertIsNone(pq.getValue(5))
if __name__ == '__main__':
unittest.main()