-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_priority_queue.py
More file actions
146 lines (125 loc) · 4.19 KB
/
test_priority_queue.py
File metadata and controls
146 lines (125 loc) · 4.19 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# DO NOT MODIFY THIS FILE
# Run me via: python3 -m unittest test_priority_queue
import unittest
import time
import random
from priority_queue import PriorityQueue
from max_heap import MaxHeap
from job import Job
class TestPriorityQueue(unittest.TestCase):
"""
Test 1: Initialization
"""
def test_instantiation(self):
"""
A PriorityQueue exists.
"""
try:
PriorityQueue()
except NameError:
self.fail("Could not instantiate PriorityQueue.")
# def test_internal(self):
# """
# Test 2: A PriorityQueue uses a list to store its data.
# """
# pq = PriorityQueue()
# self.assertEqual(MaxHeap, type(pq.heap))
# """
# Enqueuing and dequeuing. A PriorityQueue is simple if you've got a binary heap.
# """
# def test_enqueue_dequeue_one(self):
# """
# Test 3: Enqueueing a single value is immediately dequeable.
# """
# pq = PriorityQueue()
# j = Job(5, 'The')
# pq.enqueue(j)
# self.assertEqual(j, pq.dequeue())
# def test_enqueue_dequeue_two(self):
# """
# Test 4: Dequeuing from a two-element queue returns the one with highest priority.
# """
# pq = PriorityQueue()
# lower_priority = Job(1, 'of')
# higher_priority = Job(3, 'the')
# pq.enqueue(higher_priority)
# pq.enqueue(lower_priority)
# self.assertEqual(higher_priority, pq.dequeue())
# pq = PriorityQueue()
# pq.enqueue(lower_priority)
# pq.enqueue(higher_priority)
# self.assertEqual(higher_priority, pq.dequeue())
# def test_enqueue_dequeue_three(self):
# """
# Test 5: Dequeuing from a three-element queue returns the jobs with the highest
# priority.
# """
# pq = PriorityQueue()
# lower_priority = Job(1, 'like')
# middle_priority = Job(3, 'who')
# higher_priority = Job(5, 'on')
# pq.enqueue(higher_priority)
# pq.enqueue(lower_priority)
# pq.enqueue(middle_priority)
# self.assertEqual(higher_priority, pq.dequeue())
# self.assertEqual(middle_priority, pq.dequeue())
# self.assertEqual(lower_priority, pq.dequeue())
# """
# Emptiness
# """
# def test_empty(self):
# """
# Test 6: A queue is initially empty.
# """
# pq = PriorityQueue()
# self.assertTrue(pq.is_empty())
# def test_not_empty(self):
# """
# Test 7: A queue with one enqueued value is not empty.
# """
# pq = PriorityQueue()
# pq.enqueue(Job(1, 'People'))
# self.assertFalse(pq.is_empty())
# def test_empty_after_dequeue(self):
# """
# Test 8: A queue with one enqueued value is empty after dequeuing.
# """
# pq = PriorityQueue()
# pq.enqueue(Job(1, 'was'))
# _ = pq.dequeue()
# self.assertTrue(pq.is_empty())
# def test_not_empty_multiple(self):
# """
# Test 9: A queue with two enqueued values is not empty after dequeuing only one.
# """
# pq = PriorityQueue()
# pq.enqueue(Job(1, 'hustling'))
# pq.enqueue(Job(3, 'arguing and bustling'))
# _ = pq.dequeue()
# self.assertFalse(pq.is_empty())
# def test_initial_dequeue(self):
# """
# Test 10: Dequeuing from an empty queue returns None.
# """
# pq = PriorityQueue()
# self.assertIsNone(pq.dequeue())
# """
# Final test.
# """
# def test_enqueue_dequeue_omg(self):
# """
# Test 11: Dequeing from a big priority queue always returns the highest priority
# item that was in the queue.
# """
# pq = PriorityQueue()
# for _ in range(1000): # Add some zeroes for fun.
# pq.enqueue(Job(random.randint(1, 1000), "Escuchela, la ciudad respirando"))
# job = pq.dequeue()
# while not pq.is_empty():
# latest_job = pq.dequeue()
# self.assertTrue(job >= latest_job)
# job = latest_job
def fake_value():
return f"FAKE {time.time()}"
if __name__ == '__main__':
unittest.main()