blob: 8ae1637aa9bc5a14bcf6aecbae6c70ccef9cb2e7 [file] [log] [blame]
Guido van Rossum0b191782002-08-02 18:29:53 +00001"""Unittests for heapq."""
2
Guido van Rossum0b191782002-08-02 18:29:53 +00003import random
Raymond Hettingerbce036b2004-06-10 05:07:18 +00004import unittest
5from test import test_support
Raymond Hettinger2e3dfaf2004-06-13 05:26:33 +00006import sys
Guido van Rossum0b191782002-08-02 18:29:53 +00007
Christian Heimesd3eb5a152008-02-24 00:38:49 +00008# We do a bit of trickery here to be able to test both the C implementation
9# and the Python implementation of the module.
Guido van Rossum0b191782002-08-02 18:29:53 +000010
Christian Heimesd3eb5a152008-02-24 00:38:49 +000011# Make it impossible to import the C implementation anymore.
12sys.modules['_heapq'] = 0
13# We must also handle the case that heapq was imported before.
14if 'heapq' in sys.modules:
15 del sys.modules['heapq']
16
17# Now we can import the module and get the pure Python implementation.
18import heapq as py_heapq
19
20# Restore everything to normal.
21del sys.modules['_heapq']
22del sys.modules['heapq']
23
24# This is now the module with the C implementation.
25import heapq as c_heapq
26
Tim Petersaa7d2432002-08-03 02:11:26 +000027
Raymond Hettingerbce036b2004-06-10 05:07:18 +000028class TestHeap(unittest.TestCase):
Christian Heimesd3eb5a152008-02-24 00:38:49 +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)
Christian Heimesd3eb5a152008-02-24 00:38:49 +000039 self.module.heappush(heap, item)
Raymond Hettingerbce036b2004-06-10 05:07:18 +000040 self.check_invariant(heap)
41 results = []
42 while heap:
Christian Heimesd3eb5a152008-02-24 00:38:49 +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
Christian Heimesd3eb5a152008-02-24 00:38:49 +000052 self.assertRaises(TypeError, self.module.heappush, [])
Raymond Hettingere1defa42004-11-29 05:54:48 +000053 try:
Christian Heimesd3eb5a152008-02-24 00:38:49 +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
64 self.assert_(heap[parentpos] <= item)
65
66 def test_heapify(self):
67 for size in range(30):
68 heap = [random.random() for dummy in range(size)]
Christian Heimesd3eb5a152008-02-24 00:38:49 +000069 self.module.heapify(heap)
Raymond Hettingerbce036b2004-06-10 05:07:18 +000070 self.check_invariant(heap)
71
Christian Heimesd3eb5a152008-02-24 00:38:49 +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:
Christian Heimesd3eb5a152008-02-24 00:38:49 +000078 self.module.heappush(heap, item)
Raymond Hettingerbce036b2004-06-10 05:07:18 +000079 if len(heap) > 10:
Christian Heimesd3eb5a152008-02-24 00:38:49 +000080 self.module.heappop(heap)
Raymond Hettingerbce036b2004-06-10 05:07:18 +000081 heap.sort()
82 self.assertEqual(heap, sorted(data)[-10:])
83
Christian Heimesd3eb5a152008-02-24 00:38:49 +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]
Christian Heimesd3eb5a152008-02-24 00:38:49 +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
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000103 self.module.heapreplace(heap, item)
104 self.assertEqual(list(self.heapiter(heap)), sorted(data)[-10:])
Raymond Hettingerbce036b2004-06-10 05:07:18 +0000105
Christian Heimesd3eb5a152008-02-24 00:38:49 +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 Hettingerbce036b2004-06-10 05:07:18 +0000110 def test_heapsort(self):
111 # Exercise everything with repeated heapsort checks
Guido van Rossum805365e2007-05-07 22:24:25 +0000112 for trial in range(100):
Raymond Hettingerbce036b2004-06-10 05:07:18 +0000113 size = random.randrange(50)
114 data = [random.randrange(25) for i in range(size)]
115 if trial & 1: # Half of the time, use heapify
116 heap = data[:]
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000117 self.module.heapify(heap)
Raymond Hettingerbce036b2004-06-10 05:07:18 +0000118 else: # The rest of the time, use heappush
119 heap = []
120 for item in data:
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000121 self.module.heappush(heap, item)
122 heap_sorted = [self.module.heappop(heap) for i in range(size)]
Raymond Hettingerbce036b2004-06-10 05:07:18 +0000123 self.assertEqual(heap_sorted, sorted(data))
124
Thomas Wouterscf297e42007-02-23 15:07:44 +0000125 def test_merge(self):
126 inputs = []
Guido van Rossum805365e2007-05-07 22:24:25 +0000127 for i in range(random.randrange(5)):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000128 row = sorted(random.randrange(1000) for j in range(random.randrange(10)))
129 inputs.append(row)
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000130 self.assertEqual(sorted(chain(*inputs)), list(self.module.merge(*inputs)))
131 self.assertEqual(list(self.module.merge()), [])
Thomas Wouterscf297e42007-02-23 15:07:44 +0000132
133 def test_merge_stability(self):
134 class Int(int):
135 pass
136 inputs = [[], [], [], []]
137 for i in range(20000):
138 stream = random.randrange(4)
139 x = random.randrange(500)
140 obj = Int(x)
141 obj.pair = (x, stream)
142 inputs[stream].append(obj)
143 for stream in inputs:
144 stream.sort()
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000145 result = [i.pair for i in self.module.merge(*inputs)]
Thomas Wouterscf297e42007-02-23 15:07:44 +0000146 self.assertEqual(result, sorted(result))
147
Raymond Hettingerbce036b2004-06-10 05:07:18 +0000148 def test_nsmallest(self):
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000149 data = [(random.randrange(2000), i) for i in range(1000)]
150 for f in (None, lambda x: x[0] * 547 % 2000):
151 for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000152 self.assertEqual(list(self.module.nsmallest(n, data)),
153 sorted(data)[:n])
154 self.assertEqual(list(self.module.nsmallest(n, data, key=f)),
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000155 sorted(data, key=f)[:n])
Raymond Hettingerbce036b2004-06-10 05:07:18 +0000156
Raymond Hettinger4901a1f2004-12-02 08:59:14 +0000157 def test_nlargest(self):
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000158 data = [(random.randrange(2000), i) for i in range(1000)]
159 for f in (None, lambda x: x[0] * 547 % 2000):
160 for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000161 self.assertEqual(list(self.module.nlargest(n, data)),
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000162 sorted(data, reverse=True)[:n])
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000163 self.assertEqual(list(self.module.nlargest(n, data, key=f)),
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000164 sorted(data, key=f, reverse=True)[:n])
Tim Petersaa7d2432002-08-03 02:11:26 +0000165
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000166class TestHeapPython(TestHeap):
167 module = py_heapq
168
169class TestHeapC(TestHeap):
170 module = c_heapq
171
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000172
173#==============================================================================
174
175class LenOnly:
176 "Dummy sequence class defining __len__ but not __getitem__."
177 def __len__(self):
178 return 10
179
180class GetOnly:
181 "Dummy sequence class defining __getitem__ but not __len__."
182 def __getitem__(self, ndx):
183 return 10
184
185class CmpErr:
186 "Dummy element that always raises an error during comparison"
187 def __cmp__(self, other):
188 raise ZeroDivisionError
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000189 __eq__ = __ne__ = __lt__ = __le__ = __gt__ = __ge__ = __cmp__
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000190
191def R(seqn):
192 'Regular generator'
193 for i in seqn:
194 yield i
195
196class G:
197 'Sequence using __getitem__'
198 def __init__(self, seqn):
199 self.seqn = seqn
200 def __getitem__(self, i):
201 return self.seqn[i]
202
203class I:
204 'Sequence using iterator protocol'
205 def __init__(self, seqn):
206 self.seqn = seqn
207 self.i = 0
208 def __iter__(self):
209 return self
Georg Brandla18af4e2007-04-21 15:47:16 +0000210 def __next__(self):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000211 if self.i >= len(self.seqn): raise StopIteration
212 v = self.seqn[self.i]
213 self.i += 1
214 return v
215
216class Ig:
217 'Sequence using iterator protocol defined with a generator'
218 def __init__(self, seqn):
219 self.seqn = seqn
220 self.i = 0
221 def __iter__(self):
222 for val in self.seqn:
223 yield val
224
225class X:
226 'Missing __getitem__ and __iter__'
227 def __init__(self, seqn):
228 self.seqn = seqn
229 self.i = 0
Georg Brandla18af4e2007-04-21 15:47:16 +0000230 def __next__(self):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000231 if self.i >= len(self.seqn): raise StopIteration
232 v = self.seqn[self.i]
233 self.i += 1
234 return v
235
236class N:
Georg Brandla18af4e2007-04-21 15:47:16 +0000237 'Iterator missing __next__()'
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000238 def __init__(self, seqn):
239 self.seqn = seqn
240 self.i = 0
241 def __iter__(self):
242 return self
243
244class E:
245 'Test propagation of exceptions'
246 def __init__(self, seqn):
247 self.seqn = seqn
248 self.i = 0
249 def __iter__(self):
250 return self
Georg Brandla18af4e2007-04-21 15:47:16 +0000251 def __next__(self):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000252 3 // 0
253
254class S:
255 'Test immediate stop'
256 def __init__(self, seqn):
257 pass
258 def __iter__(self):
259 return self
Georg Brandla18af4e2007-04-21 15:47:16 +0000260 def __next__(self):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000261 raise StopIteration
262
263from itertools import chain, imap
264def L(seqn):
265 'Test multiple tiers of iterators'
266 return chain(imap(lambda x:x, R(Ig(G(seqn)))))
267
268class TestErrorHandling(unittest.TestCase):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000269 # only for C implementation
270 module = c_heapq
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000271
272 def test_non_sequence(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000273 for f in (self.module.heapify, self.module.heappop):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000274 self.assertRaises(TypeError, f, 10)
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000275 for f in (self.module.heappush, self.module.heapreplace,
276 self.module.nlargest, self.module.nsmallest):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000277 self.assertRaises(TypeError, f, 10, 10)
278
279 def test_len_only(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000280 for f in (self.module.heapify, self.module.heappop):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000281 self.assertRaises(TypeError, f, LenOnly())
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000282 for f in (self.module.heappush, self.module.heapreplace):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000283 self.assertRaises(TypeError, f, LenOnly(), 10)
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000284 for f in (self.module.nlargest, self.module.nsmallest):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000285 self.assertRaises(TypeError, f, 2, LenOnly())
286
287 def test_get_only(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000288 for f in (self.module.heapify, self.module.heappop):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000289 self.assertRaises(TypeError, f, GetOnly())
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000290 for f in (self.module.heappush, self.module.heapreplace):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000291 self.assertRaises(TypeError, f, GetOnly(), 10)
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000292 for f in (self.module.nlargest, self.module.nsmallest):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000293 self.assertRaises(TypeError, f, 2, GetOnly())
294
295 def test_get_only(self):
296 seq = [CmpErr(), CmpErr(), CmpErr()]
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000297 for f in (self.module.heapify, self.module.heappop):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000298 self.assertRaises(ZeroDivisionError, f, seq)
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000299 for f in (self.module.heappush, self.module.heapreplace):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000300 self.assertRaises(ZeroDivisionError, f, seq, 10)
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000301 for f in (self.module.nlargest, self.module.nsmallest):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000302 self.assertRaises(ZeroDivisionError, f, 2, seq)
303
304 def test_arg_parsing(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000305 for f in (self.module.heapify, self.module.heappop,
306 self.module.heappush, self.module.heapreplace,
307 self.module.nlargest, self.module.nsmallest):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000308 self.assertRaises(TypeError, f, 10)
309
310 def test_iterable_args(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000311 for f in (self.module.nlargest, self.module.nsmallest):
Guido van Rossum805365e2007-05-07 22:24:25 +0000312 for s in ("123", "", range(1000), (1, 1.2), range(2000,2200,5)):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000313 for g in (G, I, Ig, L, R):
Guido van Rossumc1f779c2007-07-03 08:25:58 +0000314 self.assertEqual(list(f(2, g(s))), list(f(2,s)))
315 self.assertEqual(list(f(2, S(s))), [])
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000316 self.assertRaises(TypeError, f, 2, X(s))
317 self.assertRaises(TypeError, f, 2, N(s))
318 self.assertRaises(ZeroDivisionError, f, 2, E(s))
319
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000320
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000321#==============================================================================
322
323
Raymond Hettinger2e3dfaf2004-06-13 05:26:33 +0000324def test_main(verbose=None):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000325 from types import BuiltinFunctionType
326
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000327 test_classes = [TestHeapPython, TestHeapC, TestErrorHandling]
Raymond Hettinger2e3dfaf2004-06-13 05:26:33 +0000328 test_support.run_unittest(*test_classes)
329
330 # verify reference counting
331 if verbose and hasattr(sys, "gettotalrefcount"):
332 import gc
333 counts = [None] * 5
Guido van Rossum805365e2007-05-07 22:24:25 +0000334 for i in range(len(counts)):
Raymond Hettinger2e3dfaf2004-06-13 05:26:33 +0000335 test_support.run_unittest(*test_classes)
336 gc.collect()
337 counts[i] = sys.gettotalrefcount()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000338 print(counts)
Guido van Rossum0b191782002-08-02 18:29:53 +0000339
340if __name__ == "__main__":
Raymond Hettinger2e3dfaf2004-06-13 05:26:33 +0000341 test_main(verbose=True)