-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityqueue_test.py
More file actions
74 lines (63 loc) · 2.54 KB
/
priorityqueue_test.py
File metadata and controls
74 lines (63 loc) · 2.54 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
"""
This class contains the possible test cases
for adding and removing nodes from the
priority queue
"""
import unittest
from priorityqueue import PriorityQueue
# SAMPLE COMMANDS
COMMANDS = [
{'command': 'echo "HelloWorld"', 'priority': 1},
{'command': 'cp /bin/images/* .', 'priority': 0},
{'command': 'ls media', 'priority': 10},
{'command': 'cd bin', 'priority': 5},
{'command': 'open *.exr', 'priority': 4},
{'command': 'open ref_pic.jpg', 'priority': 7},
{'command': 'htop', 'priority': 9}]
SAME_PRIO_COMMANDS = [
{'command': 'cd bin', 'priority': 5},
{'command': 'cd lib', 'priority': 5},
{'command': 'echo "HelloWorld"', 'priority': 1},
{'command': 'cd out', 'priority': 5},
{'command': 'cp /bin/images/* .', 'priority': 0}]
EMPTY_COMMAND = {'command': '', 'priority': 3}
INVALID_PRIO_COMMAND1 = {'command': 'ls', 'priority': 11}
INVALID_PRIO_COMMAND2 = {'command': 'ls', 'priority': -1}
class PriorityQueueTest(unittest.TestCase):
def test_enqueue(self):
p_queue = PriorityQueue()
for cmd in COMMANDS:
p_queue.enQueue(cmd)
self.assertEqual(len(COMMANDS), p_queue.getQueueSize())
def test_enqueue_empty_command(self):
p_queue = PriorityQueue()
self.assertRaises(ValueError, p_queue.enQueue, EMPTY_COMMAND)
def test_enqueue_invalid_priority(self):
p_queue = PriorityQueue()
self.assertRaises(ValueError, p_queue.enQueue, INVALID_PRIO_COMMAND1)
self.assertRaises(ValueError, p_queue.enQueue, INVALID_PRIO_COMMAND2)
def test_dequeue(self):
p_queue = PriorityQueue()
for cmd in COMMANDS:
p_queue.enQueue(cmd)
highest_prio = p_queue.deQueue()
self.assertEqual(10, highest_prio.priority())
self.assertEqual('ls media', highest_prio.command())
def test_dequeue_same_prio(self):
p_queue = PriorityQueue()
for cmd in SAME_PRIO_COMMANDS:
p_queue.enQueue(cmd)
next_prio = p_queue.deQueue()
self.assertEqual(5, next_prio.priority())
self.assertEqual('cd bin', next_prio.command())
next_prio = p_queue.deQueue()
self.assertEqual(5, next_prio.priority())
self.assertEqual('cd lib', next_prio.command())
next_prio = p_queue.deQueue()
self.assertEqual(5, next_prio.priority())
self.assertEqual('cd out', next_prio.command())
def test_dequeue_empty_queue(self):
p_queue = PriorityQueue()
self.assertRaises(IndexError, p_queue.deQueue)
if __name__ == '__main__':
unittest.main()