blob: 7848e4e98a06cb0a8ec3317606dd0e85b1bf8fee [file] [log] [blame]
Guido van Rossum0b191782002-08-02 18:29:53 +00001"""Unittests for heapq."""
2
Raymond Hettinger33ecffb2004-06-10 05:03:17 +00003from heapq import heappush, heappop, heapify, heapreplace, nlargest, nsmallest
Guido van Rossum0b191782002-08-02 18:29:53 +00004import random
Raymond Hettingerbce036b2004-06-10 05:07:18 +00005import unittest
6from test import test_support
Raymond Hettinger2e3dfaf2004-06-13 05:26:33 +00007import sys
Guido van Rossum0b191782002-08-02 18:29:53 +00008
Guido van Rossum0b191782002-08-02 18:29:53 +00009
Raymond Hettingerbce036b2004-06-10 05:07:18 +000010def heapiter(heap):
11 # An iterator returning a heap's elements, smallest-first.
12 try:
13 while 1:
14 yield heappop(heap)
15 except IndexError:
16 pass
Tim Petersaa7d2432002-08-03 02:11:26 +000017
Raymond Hettingerbce036b2004-06-10 05:07:18 +000018class TestHeap(unittest.TestCase):
Tim Petersaa7d2432002-08-03 02:11:26 +000019
Raymond Hettingerbce036b2004-06-10 05:07:18 +000020 def test_push_pop(self):
21 # 1) Push 256 random numbers and pop them off, verifying all's OK.
22 heap = []
23 data = []
24 self.check_invariant(heap)
25 for i in range(256):
26 item = random.random()
27 data.append(item)
28 heappush(heap, item)
29 self.check_invariant(heap)
30 results = []
31 while heap:
32 item = heappop(heap)
33 self.check_invariant(heap)
34 results.append(item)
35 data_sorted = data[:]
36 data_sorted.sort()
37 self.assertEqual(data_sorted, results)
38 # 2) Check that the invariant holds for a sorted array
39 self.check_invariant(results)
40
Neal Norwitzd7be1182004-07-08 01:56:46 +000041 self.assertRaises(TypeError, heappush, [])
42 self.assertRaises(TypeError, heappush, None, None)
43 self.assertRaises(TypeError, heappop, None)
44
Raymond Hettingerbce036b2004-06-10 05:07:18 +000045 def check_invariant(self, heap):
46 # Check the heap invariant.
47 for pos, item in enumerate(heap):
48 if pos: # pos 0 has no parent
49 parentpos = (pos-1) >> 1
50 self.assert_(heap[parentpos] <= item)
51
52 def test_heapify(self):
53 for size in range(30):
54 heap = [random.random() for dummy in range(size)]
55 heapify(heap)
56 self.check_invariant(heap)
57
Neal Norwitzd7be1182004-07-08 01:56:46 +000058 self.assertRaises(TypeError, heapify, None)
59
Raymond Hettingerbce036b2004-06-10 05:07:18 +000060 def test_naive_nbest(self):
61 data = [random.randrange(2000) for i in range(1000)]
62 heap = []
63 for item in data:
64 heappush(heap, item)
65 if len(heap) > 10:
66 heappop(heap)
67 heap.sort()
68 self.assertEqual(heap, sorted(data)[-10:])
69
70 def test_nbest(self):
71 # Less-naive "N-best" algorithm, much faster (if len(data) is big
72 # enough <wink>) than sorting all of data. However, if we had a max
73 # heap instead of a min heap, it could go faster still via
74 # heapify'ing all of data (linear time), then doing 10 heappops
75 # (10 log-time steps).
76 data = [random.randrange(2000) for i in range(1000)]
77 heap = data[:10]
78 heapify(heap)
79 for item in data[10:]:
80 if item > heap[0]: # this gets rarer the longer we run
81 heapreplace(heap, item)
82 self.assertEqual(list(heapiter(heap)), sorted(data)[-10:])
83
Neal Norwitzd7be1182004-07-08 01:56:46 +000084 self.assertRaises(TypeError, heapreplace, None)
85 self.assertRaises(TypeError, heapreplace, None, None)
86 self.assertRaises(IndexError, heapreplace, [], None)
87
Raymond Hettingerbce036b2004-06-10 05:07:18 +000088 def test_heapsort(self):
89 # Exercise everything with repeated heapsort checks
90 for trial in xrange(100):
91 size = random.randrange(50)
92 data = [random.randrange(25) for i in range(size)]
93 if trial & 1: # Half of the time, use heapify
94 heap = data[:]
95 heapify(heap)
96 else: # The rest of the time, use heappush
97 heap = []
98 for item in data:
99 heappush(heap, item)
100 heap_sorted = [heappop(heap) for i in range(size)]
101 self.assertEqual(heap_sorted, sorted(data))
102
103 def test_nsmallest(self):
104 data = [random.randrange(2000) for i in range(1000)]
Raymond Hettingeraefde432004-06-15 23:53:35 +0000105 for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
106 self.assertEqual(nsmallest(n, data), sorted(data)[:n])
Raymond Hettingerbce036b2004-06-10 05:07:18 +0000107
108 def test_largest(self):
109 data = [random.randrange(2000) for i in range(1000)]
Raymond Hettingeraefde432004-06-15 23:53:35 +0000110 for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
111 self.assertEqual(nlargest(n, data), sorted(data, reverse=True)[:n])
Tim Petersaa7d2432002-08-03 02:11:26 +0000112
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000113
114#==============================================================================
115
116class LenOnly:
117 "Dummy sequence class defining __len__ but not __getitem__."
118 def __len__(self):
119 return 10
120
121class GetOnly:
122 "Dummy sequence class defining __getitem__ but not __len__."
123 def __getitem__(self, ndx):
124 return 10
125
126class CmpErr:
127 "Dummy element that always raises an error during comparison"
128 def __cmp__(self, other):
129 raise ZeroDivisionError
130
131def R(seqn):
132 'Regular generator'
133 for i in seqn:
134 yield i
135
136class G:
137 'Sequence using __getitem__'
138 def __init__(self, seqn):
139 self.seqn = seqn
140 def __getitem__(self, i):
141 return self.seqn[i]
142
143class I:
144 'Sequence using iterator protocol'
145 def __init__(self, seqn):
146 self.seqn = seqn
147 self.i = 0
148 def __iter__(self):
149 return self
150 def next(self):
151 if self.i >= len(self.seqn): raise StopIteration
152 v = self.seqn[self.i]
153 self.i += 1
154 return v
155
156class Ig:
157 'Sequence using iterator protocol defined with a generator'
158 def __init__(self, seqn):
159 self.seqn = seqn
160 self.i = 0
161 def __iter__(self):
162 for val in self.seqn:
163 yield val
164
165class X:
166 'Missing __getitem__ and __iter__'
167 def __init__(self, seqn):
168 self.seqn = seqn
169 self.i = 0
170 def next(self):
171 if self.i >= len(self.seqn): raise StopIteration
172 v = self.seqn[self.i]
173 self.i += 1
174 return v
175
176class N:
177 'Iterator missing next()'
178 def __init__(self, seqn):
179 self.seqn = seqn
180 self.i = 0
181 def __iter__(self):
182 return self
183
184class E:
185 'Test propagation of exceptions'
186 def __init__(self, seqn):
187 self.seqn = seqn
188 self.i = 0
189 def __iter__(self):
190 return self
191 def next(self):
192 3 // 0
193
194class S:
195 'Test immediate stop'
196 def __init__(self, seqn):
197 pass
198 def __iter__(self):
199 return self
200 def next(self):
201 raise StopIteration
202
203from itertools import chain, imap
204def L(seqn):
205 'Test multiple tiers of iterators'
206 return chain(imap(lambda x:x, R(Ig(G(seqn)))))
207
208class TestErrorHandling(unittest.TestCase):
209
210 def test_non_sequence(self):
211 for f in (heapify, heappop):
212 self.assertRaises(TypeError, f, 10)
213 for f in (heappush, heapreplace, nlargest, nsmallest):
214 self.assertRaises(TypeError, f, 10, 10)
215
216 def test_len_only(self):
217 for f in (heapify, heappop):
218 self.assertRaises(TypeError, f, LenOnly())
219 for f in (heappush, heapreplace):
220 self.assertRaises(TypeError, f, LenOnly(), 10)
221 for f in (nlargest, nsmallest):
222 self.assertRaises(TypeError, f, 2, LenOnly())
223
224 def test_get_only(self):
225 for f in (heapify, heappop):
226 self.assertRaises(TypeError, f, GetOnly())
227 for f in (heappush, heapreplace):
228 self.assertRaises(TypeError, f, GetOnly(), 10)
229 for f in (nlargest, nsmallest):
230 self.assertRaises(TypeError, f, 2, GetOnly())
231
232 def test_get_only(self):
233 seq = [CmpErr(), CmpErr(), CmpErr()]
234 for f in (heapify, heappop):
235 self.assertRaises(ZeroDivisionError, f, seq)
236 for f in (heappush, heapreplace):
237 self.assertRaises(ZeroDivisionError, f, seq, 10)
238 for f in (nlargest, nsmallest):
239 self.assertRaises(ZeroDivisionError, f, 2, seq)
240
241 def test_arg_parsing(self):
242 for f in (heapify, heappop, heappush, heapreplace, nlargest, nsmallest):
243 self.assertRaises(TypeError, f, 10)
244
245 def test_iterable_args(self):
246 for f in (nlargest, nsmallest):
247 for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
248 for g in (G, I, Ig, L, R):
249 self.assertEqual(f(2, g(s)), f(2,s))
250 self.assertEqual(f(2, S(s)), [])
251 self.assertRaises(TypeError, f, 2, X(s))
252 self.assertRaises(TypeError, f, 2, N(s))
253 self.assertRaises(ZeroDivisionError, f, 2, E(s))
254
255#==============================================================================
256
257
Raymond Hettinger2e3dfaf2004-06-13 05:26:33 +0000258def test_main(verbose=None):
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000259 from types import BuiltinFunctionType
260
Raymond Hettinger2e3dfaf2004-06-13 05:26:33 +0000261 test_classes = [TestHeap]
Raymond Hettinger855d9a92004-09-28 00:03:54 +0000262 if isinstance(heapify, BuiltinFunctionType):
263 test_classes.append(TestErrorHandling)
Raymond Hettinger2e3dfaf2004-06-13 05:26:33 +0000264 test_support.run_unittest(*test_classes)
265
266 # verify reference counting
267 if verbose and hasattr(sys, "gettotalrefcount"):
268 import gc
269 counts = [None] * 5
270 for i in xrange(len(counts)):
271 test_support.run_unittest(*test_classes)
272 gc.collect()
273 counts[i] = sys.gettotalrefcount()
274 print counts
Guido van Rossum0b191782002-08-02 18:29:53 +0000275
276if __name__ == "__main__":
Raymond Hettinger2e3dfaf2004-06-13 05:26:33 +0000277 test_main(verbose=True)