-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_test-queue.py
More file actions
295 lines (246 loc) · 10.4 KB
/
_test-queue.py
File metadata and controls
295 lines (246 loc) · 10.4 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
from __future__ import print_function
import unittest
import pqueue
from pqueue import EmptyQueue
# This test suite consists of 5 tests, which test the semantic
# behaviour of the implementation. These tests are switched on by
# default. A further 3 tests (conributed by Jan Iven) exist
# for testing the speed of the implementation. The performance tests
# are switched off by default; to enable the performance tests, pass a
# time limit (in seconds) as a command-line argument
# Main body of the test suite starts here
class queue_test(unittest.TestCase):
def setUp(self):
self.q = pqueue.priority_queue()
def testEmptyQueue(self):
"Popping from empty queue should raise EmptyQueue"
self.assertRaises(EmptyQueue, self.q.pop)
def testDefaultPriority(self):
"Default priority should be 2"
self.q.add('first in')
self.q.add('second in', 1)
self.q.add('third in')
self.assertEqual(self.q.pop(), 'second in')
self.assertEqual(self.q.pop(), 'first in')
self.assertEqual(self.q.pop(), 'third in')
self.q.add('fourth in')
self.q.add('fifth in', 3)
self.q.add('sixth in')
self.assertEqual(self.q.pop(), 'fourth in')
self.assertEqual(self.q.pop(), 'sixth in')
self.assertEqual(self.q.pop(), 'fifth in')
def testPriorityOutOfRange(self):
"Submitting an out-of-range priority should raise ValueError"
self.assertRaises(ValueError, self.q.add, 'dummy', 5)
self.assertRaises(ValueError, self.q.add, 'dummy', -1)
def testPriorityWrongType(self):
"Submitting a non-int priority should raise TypeError"
self.assertRaises(TypeError, self.q.add, 'dummy', 1.1)
def testPriority(self):
"The priorities should be respected"
self.q.add(10,2)
self.q.add(9,1)
self.q.add(8,1)
self.q.add(7,2)
self.q.add(6,4)
self.q.add(1,3)
self.q.add(2,4)
self.q.add(3,0)
self.q.add(4,0)
self.q.add(5,3)
out = (self.q.pop(), self.q.pop(), self.q.pop(), self.q.pop(), self.q.pop(),
self.q.pop(), self.q.pop(), self.q.pop(), self.q.pop(), self.q.pop())
self.assertEqual(out, (3,4,9,8,10,7,1,5,6,2))
# This is the same test as above, refactored to be a bit less
# verbose. Note that the names of both methods are identical,
# thererfore the second one will displace the first one.
def testPriority(self):
"The priorities should be respected"
for object, priority in ((10,2),
(9,1),
(8,1),
(7,2),
(6,4),
(1,3),
(2,4),
(3,0),
(4,0),
(5,3)):
self.q.add(object, priority)
out = [self.q.pop() for _ in range(10)]
self.assertEqual(out, [3,4,9,8,10,7,1,5,6,2])
def testLen(self):
"len(queue) should return the number of items in the queue."
self.assertEqual(len(self.q), 0)
self.q.add(1)
self.assertEqual(len(self.q), 1)
self.q.add(2)
self.assertEqual(len(self.q), 2)
self.q.add(3,1)
self.assertEqual(len(self.q), 3)
self.q.add(4,2)
self.assertEqual(len(self.q), 4)
self.q.add(5,0)
self.assertEqual(len(self.q), 5)
self.q.pop()
self.assertEqual(len(self.q), 4)
self.q.pop()
self.assertEqual(len(self.q), 3)
self.q.add(6)
self.assertEqual(len(self.q), 4)
self.q.add(7,3)
self.assertEqual(len(self.q), 5)
self.q.add(8,1)
self.assertEqual(len(self.q), 6)
self.q.pop()
self.assertEqual(len(self.q), 5)
# This is the same test as above, refactored to be a bit more
# legible. Note that the names of both methods are identical,
# thererfore the second one will displace the first one.
def testLen(self):
"len(queue) should return the number of items in the queue."
len_should_now_be = lambda n:self.assertEqual(len(self.q),n)
add = self.q.add
pop = self.q.pop
pass; len_should_now_be(0)
add(1); len_should_now_be(1)
add(2); len_should_now_be(2)
add(3,1); len_should_now_be(3)
add(4,2); len_should_now_be(4)
add(5,0); len_should_now_be(5)
pop(); len_should_now_be(4)
pop(); len_should_now_be(3)
add(6); len_should_now_be(4)
add(7,3); len_should_now_be(5)
add(8,1); len_should_now_be(6)
pop(); len_should_now_be(5)
########################################################################
# End of semantic tests. Ignore the rest of this file until you pass
# the five tests above.
########################################################################
prefix = 'priority_queue_'
implementations = [name.replace(prefix,'') for name in dir(pqueue)
if name.startswith(prefix)]
import sys
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-a", "--alt", choices=implementations,
help="Test alternative implementation priority_queue_<ALT>")
parser.add_option("-t", "--time", type="float",
help="Switch on timing tests. Test fails after TIME")
parser.add_option('-n', type="int",
help="Number of elements to process in the speed test",
default=50000)
parser.add_option( "--teeth", type="int", default=100,
help="Number of teeth to use in the sawtooth speed test")
options, args = parser.parse_args()
# ----------------------------------------------------------------------
if options.alt:
name = "priority_queue_" + options.alt
pqueue.priority_queue = getattr(pqueue, name)
# ----------------------------------------------------------------------
# Performance testing prelude ...
# These tests rely on Unix-only functions in the signal module (so
# they will not work on Windows). These functions do not accept
# fractions of seconds, but on fast machines the tests complete in
# under a second, so I added the ability to specify fractions of
# seconds as the time limit.
print("\nImplementation used:", pqueue.priority_queue.__name__)
if options.time:
fine_time_limit = options.time
print("Speed tests switched on.")
print("Time limit = %.1f s" % fine_time_limit)
print("Processing %d elements, in %d teeth." % (options.n, options.teeth))
import math
time_limit = int(math.ceil(fine_time_limit)) # round up; signal takes integers
fine_time_limit *= 1000 # Convert to milliseconds
import signal
import time
else:
print("Speed tests switched off.")
print("To enable speed tests, use the '-t TIME' option, where TIME")
print("is the time limit per test, in seconds.")
time_limit = False
# NB: The performance tests are off by default.
def mytimer(sig, stack):
raise TimerExpired
class TimerExpired(Exception):
pass
time_reports = []
# End of performance-testing prelude.
# ----------------------------------------------------------------------
if time_limit:
# Thanks to Jan Iven for contributing the following timing tests.
class queue_test_speed(unittest.TestCase):
def setUp(self):
self.q = pqueue.priority_queue()
def makeTest(action):
def test(self):
oldsignal=signal.signal(signal.SIGALRM, mytimer)
signal.alarm(time_limit)
try:
start = time.time()
action(self)
time_taken = time.time() -start
except TimerExpired:
self.fail("timeout after %.1f seconds" % time_limit)
signal.signal(signal.SIGALRM, oldsignal)
self.assertTrue(time_taken < fine_time_limit)
time_reports.append("%-8s test completed in %.3f seconds" %
(action.__name__[4:].capitalize(), time_taken))
test.__doc__ = action.__doc__
return test
@makeTest
def testTriangle(self):
"""check performance of the implementation. Triangle pattern
50000 add(), 50000 pop(), equally distributed over priorities"""
for i in range(options.n):
self.q.add(i,i%5)
for i in range(options.n):
self.q.pop()
@makeTest
def testSawtooth(self):
"""check performance of the implementation. Sawtooth pattern
500x(100 add(), 100 pop()), equally distributed over priorities"""
per_tooth = options.n // options.teeth
for i in range(options.teeth):
for j in range(per_tooth):
self.q.add(i,i%5)
for j in range(per_tooth):
self.q.pop()
@makeTest
def testFlat(self):
"""check performance of the implementation. Flat pattern
50000 add()/pop(), equally distributed over priorities"""
for i in range(options.n):
self.q.add(i,i%5)
self.q.pop()
@makeTest
def testPlateau(self):
# This test was inspired by a discussion with Markus Elsing
"""check performance of the implementation. Plateau pattern
N x add(), N x (add(), pop()), N x pop()"""
for i in range(options.n//2):
self.q.add(i,i%5)
for i in range(options.n//2):
self.q.add(i,i%5)
self.q.pop()
for i in range(options.n//2):
self.q.pop()
# ----------------------------------------------------------------------
# Unittest does not expect to find the extra parameters we pass (time
# limit, implementation). Remove them before running unittest.
sys.argv = sys.argv[:1]
try:
# unittest shuts down the interpreter when it finishes the
# tests. We want to delay the exit, in order to display some
# timing information, so we handle the SystemExit exception.
unittest.main()
except SystemExit:
if time_reports:
print
for r in time_reports:
print(r)
# Changelog
# replaced use of deprecated (and removed in Python 2.5) timing
# module, with time module (jacek 17/10/2006)