/
test_priorityq.py
239 lines (176 loc) · 5.76 KB
/
test_priorityq.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from __future__ import unicode_literals
import pytest
from priorityq import PriorityQ, QNode
@pytest.fixture()
def QNode_list():
QNode_list = [
QNode(10),
QNode(5, priority=2),
QNode(100, priority=1)
]
return QNode_list
@pytest.fixture()
def base_pqueue(QNode_list):
return PriorityQ(QNode_list)
valid_instantiation_args = [
[QNode(1, 0), QNode(1, 1), QNode(2, 2)],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)],
[QNode(1, 0), QNode(1, 1), 1, 2, 3, 4, 5, 6],
[QNode(1, 0), QNode(1, 1), (1, 2), (2, 3), (3, 4)],
[1, 2, 3, 4, 5, 6, 7, (1, 2), (2, 3), (3, 4), (4, 5)]
]
invalid_instantiation_args = [
[(1, 2, 3), (2, 3), (3, 4), (4, 5), (5, 6)],
[(1, 2), (2, 3), (3, 4), (4, 5, 1), (5, 6)]
]
@pytest.mark.parametrize("input", valid_instantiation_args)
def test_valid_instantiation_args(input):
tpq = PriorityQ(input)
assert tpq.pop() is not None
def test_empty_instantiation_args():
tpq = PriorityQ()
with pytest.raises(IndexError):
tpq.pop()
@pytest.mark.parametrize("input", invalid_instantiation_args)
def test_invalid_instantiation_args(input):
with pytest.raises(TypeError):
tpq = PriorityQ(input)
def test_invalid_number_args_priorityq():
with pytest.raises(TypeError):
tpq = PriorityQ(1, 2)
def test_invalid_number_args_qnode():
with pytest.raises(TypeError):
tpq = QNode(1, 2, 3, 4)
def test_QNode_init_no_priority():
node1 = QNode(10)
assert node1.val == 10
assert node1.priority is None
def test_QNode_init_with_priority():
node1 = QNode(10, priority=0)
assert node1.val == 10
assert node1.priority == 0
def test_QNode_order_comparison():
node1 = QNode(10, order=1)
node2 = QNode(5, order=2)
assert node1 < node2
def test_QNode_priority_comparison():
node1 = QNode(10, priority=0)
node2 = QNode(10)
assert node1 < node2
def test_QNode_equal_priority_comparison():
node1 = QNode(10, priority=1, order=1)
node2 = QNode(5, priority=1, order=2)
assert node1 < node2
def test_QNode_equality_comparison():
node1 = QNode(10, priority=10)
node2 = QNode(10, priority=10)
assert node1 == node2
def test_PriorityQ_init_empty():
pqueue = PriorityQ()
assert isinstance(pqueue, PriorityQ)
assert len(pqueue) == 0
def test_PriorityQ_init_iterable_no_QNodes():
pqueue = PriorityQ([10, 9, 8, 7, 6, 5])
assert len(pqueue) == 6
assert pqueue[0].val == 10
assert pqueue[0].priority is None
def test_PriorityQ_init_iterable_with_QNodes(QNode_list):
pqueue = PriorityQ(QNode_list)
assert len(pqueue) == 3
assert pqueue[0].val == 100
assert pqueue[0].priority == 1
def test_insert_item_not_QNode_to_empty():
queue = PriorityQ()
queue.insert(50)
assert len(queue) == 1
assert queue[0].val == 50
assert queue[0].priority is None
def test_insert_item_QNode_to_empty():
node1 = QNode(10, priority=0)
pqueue = PriorityQ()
pqueue.insert(node1)
assert len(pqueue) == 1
assert pqueue[0].val == 10
assert pqueue[0].priority == 0
def test_insert_item_not_QNode_to_filled(base_pqueue):
base_pqueue.insert(500, priority=0)
assert len(base_pqueue) == 4
assert base_pqueue[0].val == 500
assert base_pqueue[0].order == 3
assert base_pqueue[0].priority == 0
def test_insert_QNode_to_filled(base_pqueue):
node1 = QNode(10, priority=0)
base_pqueue.insert(node1)
assert len(base_pqueue) == 4
assert base_pqueue[0].val == 10
assert base_pqueue[0].order == 3
assert base_pqueue[0].priority == 0
def test_pop1(base_pqueue):
top_priority = QNode(9000, priority=0)
length = len(base_pqueue)
base_pqueue.insert(top_priority)
assert base_pqueue.pop() == top_priority.val
assert len(base_pqueue) == length
def test_pop2(base_pqueue):
top_priority = QNode(9000, priority=0)
length = len(base_pqueue)
base_pqueue.insert(top_priority)
base_pqueue.pop()
assert base_pqueue.pop() == 100
def test_pop2(base_pqueue):
top_priority = QNode(9000, priority=0)
length = len(base_pqueue)
base_pqueue.insert(top_priority)
base_pqueue.pop()
base_pqueue.pop()
assert base_pqueue.pop() == 5
def test_pop3(base_pqueue):
top_priority = QNode(9000, priority=0)
length = len(base_pqueue)
base_pqueue.insert(top_priority)
base_pqueue.pop()
base_pqueue.pop()
base_pqueue.pop()
assert base_pqueue.pop() == 10
def test_pop4(base_pqueue):
top_priority = QNode(9000, priority=0)
length = len(base_pqueue)
base_pqueue.insert(top_priority)
base_pqueue.pop()
base_pqueue.pop()
base_pqueue.pop()
base_pqueue.pop()
with pytest.raises(IndexError):
base_pqueue.pop()
def test_peek1(base_pqueue):
top_priority = QNode(9000, priority=0)
base_pqueue.insert(top_priority)
assert base_pqueue.peek() == top_priority.val
assert base_pqueue[0] is top_priority
def test_peek2(base_pqueue):
top_priority = QNode(9000, priority=0)
base_pqueue.insert(top_priority)
base_pqueue.pop()
assert base_pqueue.peek() == base_pqueue.pop()
def test_peek3(base_pqueue):
top_priority = QNode(9000, priority=0)
base_pqueue.insert(top_priority)
base_pqueue.pop()
base_pqueue.pop()
assert base_pqueue.peek() == base_pqueue.pop()
def test_peek4(base_pqueue):
top_priority = QNode(9000, priority=0)
base_pqueue.insert(top_priority)
base_pqueue.pop()
base_pqueue.pop()
assert base_pqueue.peek() == base_pqueue.pop()
def test_peek5(base_pqueue):
top_priority = QNode(9000, priority=0)
base_pqueue.insert(top_priority)
base_pqueue.pop()
base_pqueue.pop()
base_pqueue.pop()
base_pqueue.pop()
with pytest.raises(IndexError):
base_pqueue.peek()