blob: c4de593bb820a847a5f55703741c15df03324c0e [file] [log] [blame]
Guido van Rossum0b191782002-08-02 18:29:53 +00001"""Unittests for heapq."""
2
Raymond Hettinger2e3dfaf2004-06-13 05:26:33 +00003import sys
Ezio Melotti5c5d7e22011-05-09 06:54:53 +03004import random
Guido van Rossum0b191782002-08-02 18:29:53 +00005
Ezio Melotti5c5d7e22011-05-09 06:54:53 +03006from test import test_support
7from unittest import TestCase, skipUnless
8
Nick Coghlan5533ff62009-04-22 15:26:04 +00009py_heapq = test_support.import_fresh_module('heapq', blocked=['_heapq'])
Ezio Melotti5c5d7e22011-05-09 06:54:53 +030010c_heapq = test_support.import_fresh_module('heapq', fresh=['_heapq'])
Tim Petersaa7d2432002-08-03 02:11:26 +000011
Ezio Melotti5c5d7e22011-05-09 06:54:53 +030012# _heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when
13# _heapq is imported, so check them there
14func_names = ['heapify', 'heappop', 'heappush', 'heappushpop',
15 'heapreplace', '_nlargest', '_nsmallest']
16
17class TestModules(TestCase):
18 def test_py_functions(self):
19 for fname in func_names:
20 self.assertEqual(getattr(py_heapq, fname).__module__, 'heapq')
21
22 @skipUnless(c_heapq, 'requires _heapq')
23 def test_c_functions(self):
24 for fname in func_names:
25 self.assertEqual(getattr(c_heapq, fname).__module__, '_heapq')
26
27
28class TestHeap(TestCase):
Georg Brandl0bb85672008-02-23 22:35:33 +000029 module = None
Tim Petersaa7d2432002-08-03 02:11:26 +000030
Raymond Hettingerbce036b2004-06-10 05:07:18 +000031 def test_push_pop(self):
32 # 1) Push 256 random numbers and pop them off, verifying all's OK.
33 heap = []
34 data = []
35 self.check_invariant(heap)
36 for i in range(256):
37 item = random.random()
38 data.append(item)
Georg Brandl0bb85672008-02-23 22:35:33 +000039 self.module.heappush(heap, item)
Raymond Hettingerbce036b2004-06-10 05:07:18 +000040 self.check_invariant(heap)
41 results = []
42 while heap:
Georg Brandl0bb85672008-02-23 22:35:33 +000043 item = self.module.heappop(heap)
Raymond Hettingerbce036b2004-06-10 05:07:18 +000044 self.check_invariant(heap)
45 results.append(item)
46 data_sorted = data[:]
47 data_sorted.sort()
48 self.assertEqual(data_sorted, results)
49 # 2) Check that the invariant holds for a sorted array
50 self.check_invariant(results)
51
Georg Brandl0bb85672008-02-23 22:35:33 +000052 self.assertRaises(TypeError, self.module.heappush, [])
Raymond Hettingere1defa42004-11-29 05:54:48 +000053 try:
Georg Brandl0bb85672008-02-23 22:35:33 +000054 self.assertRaises(TypeError, self.module.heappush, None, None)
55 self.assertRaises(TypeError, self.module.heappop, None)
Raymond Hettingere1defa42004-11-29 05:54:48 +000056 except AttributeError:
57 pass
Neal Norwitzd7be1182004-07-08 01:56:46 +000058
Raymond Hettingerbce036b2004-06-10 05:07:18 +000059 def check_invariant(self, heap):
60 # Check the heap invariant.
61 for pos, item in enumerate(heap):
62 if pos: # pos 0 has no parent
63 parentpos = (pos-1) >> 1
Benjamin Peterson5c8da862009-06-30 22:57:08 +000064 self.assertTrue(heap[parentpos] <= item)
Raymond Hettingerbce036b2004-06-10 05:07:18 +000065
66 def test_heapify(self):
67 for size in range(30):
68 heap = [random.random() for dummy in range(size)]
Georg Brandl0bb85672008-02-23 22:35:33 +000069 self.module.heapify(heap)
Raymond Hettingerbce036b2004-06-10 05:07:18 +000070 self.check_invariant(heap)
71
Georg Brandl0bb85672008-02-23 22:35:33 +000072 self.assertRaises(TypeError, self.module.heapify, None)
Neal Norwitzd7be1182004-07-08 01:56:46 +000073
Raymond Hettingerbce036b2004-06-10 05:07:18 +000074 def test_naive_nbest(self):
75 data = [random.randrange(2000) for i in range(1000)]
76 heap = []
77 for item in data:
Georg Brandl0bb85672008-02-23 22:35:33 +000078 self.module.heappush(heap, item)
Raymond Hettingerbce036b2004-06-10 05:07:18 +000079 if len(heap) > 10:
Georg Brandl0bb85672008-02-23 22:35:33 +000080 self.module.heappop(heap)
Raymond Hettingerbce036b2004-06-10 05:07:18 +000081 heap.sort()
82 self.assertEqual(heap, sorted(data)[-10:])
83
Georg Brandl0bb85672008-02-23 22:35:33 +000084 def heapiter(self, heap):
85 # An iterator returning a heap's elements, smallest-first.
86 try:
87 while 1:
88 yield self.module.heappop(heap)
89 except IndexError:
90 pass
91
Raymond Hettingerbce036b2004-06-10 05:07:18 +000092 def test_nbest(self):
93 # Less-naive "N-best" algorithm, much faster (if len(data) is big
94 # enough <wink>) than sorting all of data. However, if we had a max
95 # heap instead of a min heap, it could go faster still via
96 # heapify'ing all of data (linear time), then doing 10 heappops
97 # (10 log-time steps).
98 data = [random.randrange(2000) for i in range(1000)]
99 heap = data[:10]
Georg Brandl0bb85672008-02-23 22:35:33 +0000100 self.module.heapify(heap)
Raymond Hettingerbce036b2004-06-10 05:07:18 +0000101 for item in data[10:]:
102 if item > heap[0]: # this gets rarer the longer we run
Georg Brandl0bb85672008-02-23 22:35:33 +0000103 self.module.heapreplace(heap, item)
104 self.assertEqual(list(self.heapiter(heap)), sorted(data)[-10:])
Raymond Hettingerbce036b2004-06-10 05:07:18 +0000105
Georg Brandl0bb85672008-02-23 22:35:33 +0000106 self.assertRaises(TypeError, self.module.heapreplace, None)
107 self.assertRaises(TypeError, self.module.heapreplace, None, None)
108 self.assertRaises(IndexError, self.module.heapreplace, [], None)
Neal Norwitzd7be1182004-07-08 01:56:46 +0000109
Raymond Hettinger53bdf092008-03-13 19:03:51 +0000110 def test_nbest_with_pushpop(self):
111 data = [random.randrange(2000) for i in range(1000)]
112 heap = data[:10]
113 self.module.heapify(heap)
114 for item in data[10:]:
115 self.module.heappushpop(heap, item)
116 self.assertEqual(list(self.heapiter(heap)), sorted(data)[-10:])
117 self.assertEqual(self.module.heappushpop([], 'x'), 'x')
118
119 def test_heappushpop(self):
120 h = []
121 x = self.module.heappushpop(h, 10)
122 self.assertEqual((h, x), ([], 10))
123
124 h = [10]
125 x = self.module.heappushpop(h, 10.0)
126 self.assertEqual((h, x), ([10], 10.0))
127 self.assertEqual(type(h[0]), int)
128 self.assertEqual(type(x), float)
129
130 h = [10];
131 x = self.module.heappushpop(h, 9)
132 self.assertEqual((h, x), ([10], 9))
133
134 h = [10];
135 x = self.module.heappushpop(h, 11)
136 self.assertEqual((h, x), ([11], 10))
137
Raymond Hettingerbce036b2004-06-10 05:07:18 +0000138 def test_heapsort(self):
139 # Exercise everything with repeated heapsort checks
140 for trial in xrange(100):
141 size = random.randrange(50)
142 data = [random.randrange(25) for i in range(size)]
143 if trial & 1: # Half of the time, use heapify
144 heap = data[:]
Georg Brandl0bb85672008-02-23 22:35:33 +0000145 self.module.heapify(heap)
Raymond Hettingerbce036b2004-06-10 05:07:18 +0000146 else: # The rest of the time, use heappush
147 heap = []
148 for item in data:
Georg Brandl0bb85672008-02-23 22:35:33 +0000149 self.module.heappush(heap, item)
150 heap_sorted = [self.module.heappop(heap) for i in range(size)]
Raymond Hettingerbce036b2004-06-10 05:07:18 +0000151 self.assertEqual(heap_sorted, sorted(data))
152
Raymond Hettinger00166c52007-02-19 04:08:43 +0000153 def test_merge(self):
154 inputs = []
155 for i in xrange(random.randrange(5)):
156 row = sorted(random.randrange(1000) for j in range(random.randrange(10)))
157 inputs.append(row)
Georg Brandl0bb85672008-02-23 22:35:33 +0000158 self.assertEqual(sorted(chain(*inputs)), list(self.module.merge(*inputs)))
159 self.assertEqual(list(self.module.merge()), [])
Raymond Hettinger00166c52007-02-19 04:08:43 +0000160
Raymond Hettinger39659f22013-09-14 22:17:39 -0700161 def test_merge_does_not_suppress_index_error(self):
162 # Issue 19018: Heapq.merge suppresses IndexError from user generator
163 def iterable():
164 s = list(range(10))
165 for i in range(20):
166 yield s[i] # IndexError when i > 10
167 with self.assertRaises(IndexError):
168 list(self.module.merge(iterable(), iterable()))
169
Raymond Hettinger01b98812007-02-19 07:30:21 +0000170 def test_merge_stability(self):
171 class Int(int):
172 pass
173 inputs = [[], [], [], []]
174 for i in range(20000):
175 stream = random.randrange(4)
176 x = random.randrange(500)
177 obj = Int(x)
178 obj.pair = (x, stream)
179 inputs[stream].append(obj)
180 for stream in inputs:
181 stream.sort()
Georg Brandl0bb85672008-02-23 22:35:33 +0000182 result = [i.pair for i in self.module.merge(*inputs)]
Raymond Hettinger01b98812007-02-19 07:30:21 +0000183 self.assertEqual(result, sorted(result))
184
Raymond Hettingerbce036b2004-06-10 05:07:18 +0000185 def test_nsmallest(self):
Raymond Hettinger769a40a2007-01-04 17:53:34 +0000186 data = [(random.randrange(2000), i) for i in range(1000)]
187 for f in (None, lambda x: x[0] * 547 % 2000):
188 for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
Georg Brandl0bb85672008-02-23 22:35:33 +0000189 self.assertEqual(self.module.nsmallest(n, data), sorted(data)[:n])
190 self.assertEqual(self.module.nsmallest(n, data, key=f),
Raymond Hettinger769a40a2007-01-04 17:53:34 +0000191 sorted(data, key=f)[:n])
Raymond Hettingerbce036b2004-06-10 05:07:18 +0000192
Raymond Hettinger4901a1f2004-12-02 08:59:14 +0000193 def test_nlargest(self):
Raymond Hettinger769a40a2007-01-04 17:53:34 +0000194 data = [(random.randrange(2000), i) for i in range(1000)]
195 for f in (None, lambda x: x[0] * 547 % 2000):
196 for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
Georg Brandl0bb85672008-02-23 22:35:33 +0000197 self.assertEqual(self.module.nlargest(n, data),
198 sorted(data, reverse=True)[:n])
199 self.assertEqual(self.module.nlargest(n, data, key=f),
Raymond Hettinger769a40a2007-01-04 17:53:34 +0000200 sorted(data, key=f, reverse=True)[:n])
Tim Petersaa7d2432002-08-03 02:11:26 +0000201
Raymond Hettingere29a1032008-06-11 13:14:50 +0000202 def test_comparison_operator(self):
Ezio Melottib47553a2011-05-09 18:32:18 +0300203 # Issue 3051: Make sure heapq works with both __lt__ and __le__
Raymond Hettingere29a1032008-06-11 13:14:50 +0000204 def hsort(data, comp):
205 data = map(comp, data)
206 self.module.heapify(data)
207 return [self.module.heappop(data).x for i in range(len(data))]
208 class LT:
209 def __init__(self, x):
210 self.x = x
211 def __lt__(self, other):
212 return self.x > other.x
213 class LE:
214 def __init__(self, x):
215 self.x = x
Neal Norwitz04097a62008-06-13 06:03:25 +0000216 def __le__(self, other):
Raymond Hettingere29a1032008-06-11 13:14:50 +0000217 return self.x >= other.x
218 data = [random.random() for i in range(100)]
219 target = sorted(data, reverse=True)
220 self.assertEqual(hsort(data, LT), target)
221 self.assertEqual(hsort(data, LE), target)
222
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000223
Ezio Melottib47553a2011-05-09 18:32:18 +0300224class TestHeapPython(TestHeap):
225 module = py_heapq
226
227
228@skipUnless(c_heapq, 'requires _heapq')
229class TestHeapC(TestHeap):
230 module = c_heapq
231
232
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000233#==============================================================================
234
235class LenOnly:
236 "Dummy sequence class defining __len__ but not __getitem__."
237 def __len__(self):
238 return 10
239
240class GetOnly:
241 "Dummy sequence class defining __getitem__ but not __len__."
242 def __getitem__(self, ndx):
243 return 10
244
245class CmpErr:
246 "Dummy element that always raises an error during comparison"
247 def __cmp__(self, other):
248 raise ZeroDivisionError
249
250def R(seqn):
251 'Regular generator'
252 for i in seqn:
253 yield i
254
255class G:
256 'Sequence using __getitem__'
257 def __init__(self, seqn):
258 self.seqn = seqn
259 def __getitem__(self, i):
260 return self.seqn[i]
261
262class I:
263 'Sequence using iterator protocol'
264 def __init__(self, seqn):
265 self.seqn = seqn
266 self.i = 0
267 def __iter__(self):
268 return self
269 def next(self):
270 if self.i >= len(self.seqn): raise StopIteration
271 v = self.seqn[self.i]
272 self.i += 1
273 return v
274
275class Ig:
276 'Sequence using iterator protocol defined with a generator'
277 def __init__(self, seqn):
278 self.seqn = seqn
279 self.i = 0
280 def __iter__(self):
281 for val in self.seqn:
282 yield val
283
284class X:
285 'Missing __getitem__ and __iter__'
286 def __init__(self, seqn):
287 self.seqn = seqn
288 self.i = 0
289 def next(self):
290 if self.i >= len(self.seqn): raise StopIteration
291 v = self.seqn[self.i]
292 self.i += 1
293 return v
294
295class N:
296 'Iterator missing next()'
297 def __init__(self, seqn):
298 self.seqn = seqn
299 self.i = 0
300 def __iter__(self):
301 return self
302
303class E:
304 'Test propagation of exceptions'
305 def __init__(self, seqn):
306 self.seqn = seqn
307 self.i = 0
308 def __iter__(self):
309 return self
310 def next(self):
311 3 // 0
312
313class S:
314 'Test immediate stop'
315 def __init__(self, seqn):
316 pass
317 def __iter__(self):
318 return self
319 def next(self):
320 raise StopIteration
321
322from itertools import chain, imap
323def L(seqn):
324 'Test multiple tiers of iterators'
325 return chain(imap(lambda x:x, R(Ig(G(seqn)))))
326
Antoine Pitrou49e4dfe2013-03-04 20:30:01 +0100327class SideEffectLT:
328 def __init__(self, value, heap):
329 self.value = value
330 self.heap = heap
331
332 def __lt__(self, other):
333 self.heap[:] = []
334 return self.value < other.value
335
336
Ezio Melotti5c5d7e22011-05-09 06:54:53 +0300337class TestErrorHandling(TestCase):
Ezio Melottib47553a2011-05-09 18:32:18 +0300338 module = None
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000339
340 def test_non_sequence(self):
Georg Brandl0bb85672008-02-23 22:35:33 +0000341 for f in (self.module.heapify, self.module.heappop):
Raymond Hettinger9b342c62011-04-13 11:15:58 -0700342 self.assertRaises((TypeError, AttributeError), f, 10)
Georg Brandl0bb85672008-02-23 22:35:33 +0000343 for f in (self.module.heappush, self.module.heapreplace,
344 self.module.nlargest, self.module.nsmallest):
Raymond Hettinger9b342c62011-04-13 11:15:58 -0700345 self.assertRaises((TypeError, AttributeError), f, 10, 10)
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000346
347 def test_len_only(self):
Georg Brandl0bb85672008-02-23 22:35:33 +0000348 for f in (self.module.heapify, self.module.heappop):
Raymond Hettinger9b342c62011-04-13 11:15:58 -0700349 self.assertRaises((TypeError, AttributeError), f, LenOnly())
Georg Brandl0bb85672008-02-23 22:35:33 +0000350 for f in (self.module.heappush, self.module.heapreplace):
Raymond Hettinger9b342c62011-04-13 11:15:58 -0700351 self.assertRaises((TypeError, AttributeError), f, LenOnly(), 10)
Georg Brandl0bb85672008-02-23 22:35:33 +0000352 for f in (self.module.nlargest, self.module.nsmallest):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000353 self.assertRaises(TypeError, f, 2, LenOnly())
354
355 def test_get_only(self):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000356 seq = [CmpErr(), CmpErr(), CmpErr()]
Georg Brandl0bb85672008-02-23 22:35:33 +0000357 for f in (self.module.heapify, self.module.heappop):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000358 self.assertRaises(ZeroDivisionError, f, seq)
Georg Brandl0bb85672008-02-23 22:35:33 +0000359 for f in (self.module.heappush, self.module.heapreplace):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000360 self.assertRaises(ZeroDivisionError, f, seq, 10)
Georg Brandl0bb85672008-02-23 22:35:33 +0000361 for f in (self.module.nlargest, self.module.nsmallest):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000362 self.assertRaises(ZeroDivisionError, f, 2, seq)
363
364 def test_arg_parsing(self):
Georg Brandl0bb85672008-02-23 22:35:33 +0000365 for f in (self.module.heapify, self.module.heappop,
366 self.module.heappush, self.module.heapreplace,
367 self.module.nlargest, self.module.nsmallest):
Raymond Hettinger9b342c62011-04-13 11:15:58 -0700368 self.assertRaises((TypeError, AttributeError), f, 10)
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000369
370 def test_iterable_args(self):
Georg Brandl0bb85672008-02-23 22:35:33 +0000371 for f in (self.module.nlargest, self.module.nsmallest):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000372 for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
373 for g in (G, I, Ig, L, R):
Florent Xicluna07627882010-03-21 01:14:24 +0000374 with test_support.check_py3k_warnings(
375 ("comparing unequal types not supported",
376 DeprecationWarning), quiet=True):
377 self.assertEqual(f(2, g(s)), f(2,s))
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000378 self.assertEqual(f(2, S(s)), [])
379 self.assertRaises(TypeError, f, 2, X(s))
380 self.assertRaises(TypeError, f, 2, N(s))
381 self.assertRaises(ZeroDivisionError, f, 2, E(s))
382
Antoine Pitrou49e4dfe2013-03-04 20:30:01 +0100383 # Issue #17278: the heap may change size while it's being walked.
384
385 def test_heappush_mutating_heap(self):
386 heap = []
387 heap.extend(SideEffectLT(i, heap) for i in range(200))
388 # Python version raises IndexError, C version RuntimeError
389 with self.assertRaises((IndexError, RuntimeError)):
390 self.module.heappush(heap, SideEffectLT(5, heap))
391
392 def test_heappop_mutating_heap(self):
393 heap = []
394 heap.extend(SideEffectLT(i, heap) for i in range(200))
395 # Python version raises IndexError, C version RuntimeError
396 with self.assertRaises((IndexError, RuntimeError)):
397 self.module.heappop(heap)
398
Ezio Melotti5c5d7e22011-05-09 06:54:53 +0300399
Ezio Melottib47553a2011-05-09 18:32:18 +0300400class TestErrorHandlingPython(TestErrorHandling):
Raymond Hettinger8dd06242011-05-07 14:16:42 -0700401 module = py_heapq
402
Ezio Melotti5c5d7e22011-05-09 06:54:53 +0300403
404@skipUnless(c_heapq, 'requires _heapq')
Ezio Melottib47553a2011-05-09 18:32:18 +0300405class TestErrorHandlingC(TestErrorHandling):
Raymond Hettinger8dd06242011-05-07 14:16:42 -0700406 module = c_heapq
407
Georg Brandl0bb85672008-02-23 22:35:33 +0000408
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000409#==============================================================================
410
411
Raymond Hettinger2e3dfaf2004-06-13 05:26:33 +0000412def test_main(verbose=None):
Ezio Melotti5c5d7e22011-05-09 06:54:53 +0300413 test_classes = [TestModules, TestHeapPython, TestHeapC,
Ezio Melottib47553a2011-05-09 18:32:18 +0300414 TestErrorHandlingPython, TestErrorHandlingC]
Raymond Hettinger2e3dfaf2004-06-13 05:26:33 +0000415 test_support.run_unittest(*test_classes)
416
417 # verify reference counting
418 if verbose and hasattr(sys, "gettotalrefcount"):
419 import gc
420 counts = [None] * 5
421 for i in xrange(len(counts)):
422 test_support.run_unittest(*test_classes)
423 gc.collect()
424 counts[i] = sys.gettotalrefcount()
425 print counts
Guido van Rossum0b191782002-08-02 18:29:53 +0000426
427if __name__ == "__main__":
Raymond Hettinger2e3dfaf2004-06-13 05:26:33 +0000428 test_main(verbose=True)