Guido van Rossum | 0b19178 | 2002-08-02 18:29:53 +0000 | [diff] [blame] | 1 | """Unittests for heapq.""" |
| 2 | |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 3 | import sys |
Ezio Melotti | 8269a44 | 2011-05-09 07:15:04 +0300 | [diff] [blame] | 4 | import random |
Ezio Melotti | 22ebb2d | 2013-01-02 21:19:37 +0200 | [diff] [blame] | 5 | import unittest |
Guido van Rossum | 0b19178 | 2002-08-02 18:29:53 +0000 | [diff] [blame] | 6 | |
Ezio Melotti | 8269a44 | 2011-05-09 07:15:04 +0300 | [diff] [blame] | 7 | from test import support |
| 8 | from unittest import TestCase, skipUnless |
| 9 | |
Nick Coghlan | 4738470 | 2009-04-22 16:13:36 +0000 | [diff] [blame] | 10 | py_heapq = support.import_fresh_module('heapq', blocked=['_heapq']) |
Ezio Melotti | 8269a44 | 2011-05-09 07:15:04 +0300 | [diff] [blame] | 11 | c_heapq = support.import_fresh_module('heapq', fresh=['_heapq']) |
Tim Peters | aa7d243 | 2002-08-03 02:11:26 +0000 | [diff] [blame] | 12 | |
Ezio Melotti | 8269a44 | 2011-05-09 07:15:04 +0300 | [diff] [blame] | 13 | # _heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when |
| 14 | # _heapq is imported, so check them there |
| 15 | func_names = ['heapify', 'heappop', 'heappush', 'heappushpop', |
| 16 | 'heapreplace', '_nlargest', '_nsmallest'] |
| 17 | |
| 18 | class TestModules(TestCase): |
| 19 | def test_py_functions(self): |
| 20 | for fname in func_names: |
| 21 | self.assertEqual(getattr(py_heapq, fname).__module__, 'heapq') |
| 22 | |
| 23 | @skipUnless(c_heapq, 'requires _heapq') |
| 24 | def test_c_functions(self): |
| 25 | for fname in func_names: |
| 26 | self.assertEqual(getattr(c_heapq, fname).__module__, '_heapq') |
| 27 | |
| 28 | |
Ezio Melotti | 22ebb2d | 2013-01-02 21:19:37 +0200 | [diff] [blame] | 29 | class TestHeap: |
Tim Peters | aa7d243 | 2002-08-03 02:11:26 +0000 | [diff] [blame] | 30 | |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 31 | 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 Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 39 | self.module.heappush(heap, item) |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 40 | self.check_invariant(heap) |
| 41 | results = [] |
| 42 | while heap: |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 43 | item = self.module.heappop(heap) |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 44 | 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 Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 52 | self.assertRaises(TypeError, self.module.heappush, []) |
Raymond Hettinger | e1defa4 | 2004-11-29 05:54:48 +0000 | [diff] [blame] | 53 | try: |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 54 | self.assertRaises(TypeError, self.module.heappush, None, None) |
| 55 | self.assertRaises(TypeError, self.module.heappop, None) |
Raymond Hettinger | e1defa4 | 2004-11-29 05:54:48 +0000 | [diff] [blame] | 56 | except AttributeError: |
| 57 | pass |
Neal Norwitz | d7be118 | 2004-07-08 01:56:46 +0000 | [diff] [blame] | 58 | |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 59 | 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 Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 64 | self.assertTrue(heap[parentpos] <= item) |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 65 | |
| 66 | def test_heapify(self): |
| 67 | for size in range(30): |
| 68 | heap = [random.random() for dummy in range(size)] |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 69 | self.module.heapify(heap) |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 70 | self.check_invariant(heap) |
| 71 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 72 | self.assertRaises(TypeError, self.module.heapify, None) |
Neal Norwitz | d7be118 | 2004-07-08 01:56:46 +0000 | [diff] [blame] | 73 | |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 74 | def test_naive_nbest(self): |
| 75 | data = [random.randrange(2000) for i in range(1000)] |
| 76 | heap = [] |
| 77 | for item in data: |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 78 | self.module.heappush(heap, item) |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 79 | if len(heap) > 10: |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 80 | self.module.heappop(heap) |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 81 | heap.sort() |
| 82 | self.assertEqual(heap, sorted(data)[-10:]) |
| 83 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 84 | 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 Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 92 | 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 Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 100 | self.module.heapify(heap) |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 101 | for item in data[10:]: |
| 102 | if item > heap[0]: # this gets rarer the longer we run |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 103 | self.module.heapreplace(heap, item) |
| 104 | self.assertEqual(list(self.heapiter(heap)), sorted(data)[-10:]) |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 105 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 106 | 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 Norwitz | d7be118 | 2004-07-08 01:56:46 +0000 | [diff] [blame] | 109 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 110 | 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 Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 138 | def test_heapsort(self): |
| 139 | # Exercise everything with repeated heapsort checks |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 140 | for trial in range(100): |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 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[:] |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 145 | self.module.heapify(heap) |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 146 | else: # The rest of the time, use heappush |
| 147 | heap = [] |
| 148 | for item in data: |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 149 | self.module.heappush(heap, item) |
| 150 | heap_sorted = [self.module.heappop(heap) for i in range(size)] |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 151 | self.assertEqual(heap_sorted, sorted(data)) |
| 152 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 153 | def test_merge(self): |
| 154 | inputs = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 155 | for i in range(random.randrange(5)): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 156 | row = sorted(random.randrange(1000) for j in range(random.randrange(10))) |
| 157 | inputs.append(row) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 158 | self.assertEqual(sorted(chain(*inputs)), list(self.module.merge(*inputs))) |
| 159 | self.assertEqual(list(self.module.merge()), []) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 160 | |
| 161 | def test_merge_stability(self): |
| 162 | class Int(int): |
| 163 | pass |
| 164 | inputs = [[], [], [], []] |
| 165 | for i in range(20000): |
| 166 | stream = random.randrange(4) |
| 167 | x = random.randrange(500) |
| 168 | obj = Int(x) |
| 169 | obj.pair = (x, stream) |
| 170 | inputs[stream].append(obj) |
| 171 | for stream in inputs: |
| 172 | stream.sort() |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 173 | result = [i.pair for i in self.module.merge(*inputs)] |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 174 | self.assertEqual(result, sorted(result)) |
| 175 | |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 176 | def test_nsmallest(self): |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 177 | data = [(random.randrange(2000), i) for i in range(1000)] |
| 178 | for f in (None, lambda x: x[0] * 547 % 2000): |
| 179 | for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 180 | self.assertEqual(list(self.module.nsmallest(n, data)), |
| 181 | sorted(data)[:n]) |
| 182 | self.assertEqual(list(self.module.nsmallest(n, data, key=f)), |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 183 | sorted(data, key=f)[:n]) |
Raymond Hettinger | bce036b | 2004-06-10 05:07:18 +0000 | [diff] [blame] | 184 | |
Raymond Hettinger | 4901a1f | 2004-12-02 08:59:14 +0000 | [diff] [blame] | 185 | def test_nlargest(self): |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 186 | 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): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 189 | self.assertEqual(list(self.module.nlargest(n, data)), |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 190 | sorted(data, reverse=True)[:n]) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 191 | self.assertEqual(list(self.module.nlargest(n, data, key=f)), |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 192 | sorted(data, key=f, reverse=True)[:n]) |
Tim Peters | aa7d243 | 2002-08-03 02:11:26 +0000 | [diff] [blame] | 193 | |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 194 | def test_comparison_operator(self): |
Ezio Melotti | f9756c2 | 2011-05-09 18:36:53 +0300 | [diff] [blame] | 195 | # Issue 3051: Make sure heapq works with both __lt__ |
Amaury Forgeot d'Arc | 2ba198d | 2008-06-17 21:25:35 +0000 | [diff] [blame] | 196 | # For python 3.0, __le__ alone is not enough |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 197 | def hsort(data, comp): |
| 198 | data = [comp(x) for x in data] |
| 199 | self.module.heapify(data) |
| 200 | return [self.module.heappop(data).x for i in range(len(data))] |
| 201 | class LT: |
| 202 | def __init__(self, x): |
| 203 | self.x = x |
| 204 | def __lt__(self, other): |
| 205 | return self.x > other.x |
| 206 | class LE: |
| 207 | def __init__(self, x): |
| 208 | self.x = x |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 209 | def __le__(self, other): |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 210 | return self.x >= other.x |
| 211 | data = [random.random() for i in range(100)] |
| 212 | target = sorted(data, reverse=True) |
| 213 | self.assertEqual(hsort(data, LT), target) |
Amaury Forgeot d'Arc | 2ba198d | 2008-06-17 21:25:35 +0000 | [diff] [blame] | 214 | self.assertRaises(TypeError, data, LE) |
Amaury Forgeot d'Arc | fdfe62d | 2008-06-17 20:36:03 +0000 | [diff] [blame] | 215 | |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 216 | |
Ezio Melotti | 22ebb2d | 2013-01-02 21:19:37 +0200 | [diff] [blame] | 217 | class TestHeapPython(TestHeap, TestCase): |
Ezio Melotti | f9756c2 | 2011-05-09 18:36:53 +0300 | [diff] [blame] | 218 | module = py_heapq |
| 219 | |
| 220 | |
| 221 | @skipUnless(c_heapq, 'requires _heapq') |
Ezio Melotti | 22ebb2d | 2013-01-02 21:19:37 +0200 | [diff] [blame] | 222 | class TestHeapC(TestHeap, TestCase): |
Ezio Melotti | f9756c2 | 2011-05-09 18:36:53 +0300 | [diff] [blame] | 223 | module = c_heapq |
| 224 | |
| 225 | |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 226 | #============================================================================== |
| 227 | |
| 228 | class LenOnly: |
| 229 | "Dummy sequence class defining __len__ but not __getitem__." |
| 230 | def __len__(self): |
| 231 | return 10 |
| 232 | |
| 233 | class GetOnly: |
| 234 | "Dummy sequence class defining __getitem__ but not __len__." |
| 235 | def __getitem__(self, ndx): |
| 236 | return 10 |
| 237 | |
| 238 | class CmpErr: |
| 239 | "Dummy element that always raises an error during comparison" |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 240 | def __eq__(self, other): |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 241 | raise ZeroDivisionError |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 242 | __ne__ = __lt__ = __le__ = __gt__ = __ge__ = __eq__ |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 243 | |
| 244 | def R(seqn): |
| 245 | 'Regular generator' |
| 246 | for i in seqn: |
| 247 | yield i |
| 248 | |
| 249 | class G: |
| 250 | 'Sequence using __getitem__' |
| 251 | def __init__(self, seqn): |
| 252 | self.seqn = seqn |
| 253 | def __getitem__(self, i): |
| 254 | return self.seqn[i] |
| 255 | |
| 256 | class I: |
| 257 | 'Sequence using iterator protocol' |
| 258 | def __init__(self, seqn): |
| 259 | self.seqn = seqn |
| 260 | self.i = 0 |
| 261 | def __iter__(self): |
| 262 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 263 | def __next__(self): |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 264 | if self.i >= len(self.seqn): raise StopIteration |
| 265 | v = self.seqn[self.i] |
| 266 | self.i += 1 |
| 267 | return v |
| 268 | |
| 269 | class Ig: |
| 270 | 'Sequence using iterator protocol defined with a generator' |
| 271 | def __init__(self, seqn): |
| 272 | self.seqn = seqn |
| 273 | self.i = 0 |
| 274 | def __iter__(self): |
| 275 | for val in self.seqn: |
| 276 | yield val |
| 277 | |
| 278 | class X: |
| 279 | 'Missing __getitem__ and __iter__' |
| 280 | def __init__(self, seqn): |
| 281 | self.seqn = seqn |
| 282 | self.i = 0 |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 283 | def __next__(self): |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 284 | if self.i >= len(self.seqn): raise StopIteration |
| 285 | v = self.seqn[self.i] |
| 286 | self.i += 1 |
| 287 | return v |
| 288 | |
| 289 | class N: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 290 | 'Iterator missing __next__()' |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 291 | def __init__(self, seqn): |
| 292 | self.seqn = seqn |
| 293 | self.i = 0 |
| 294 | def __iter__(self): |
| 295 | return self |
| 296 | |
| 297 | class E: |
| 298 | 'Test propagation of exceptions' |
| 299 | def __init__(self, seqn): |
| 300 | self.seqn = seqn |
| 301 | self.i = 0 |
| 302 | def __iter__(self): |
| 303 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 304 | def __next__(self): |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 305 | 3 // 0 |
| 306 | |
| 307 | class S: |
| 308 | 'Test immediate stop' |
| 309 | def __init__(self, seqn): |
| 310 | pass |
| 311 | def __iter__(self): |
| 312 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 313 | def __next__(self): |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 314 | raise StopIteration |
| 315 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 316 | from itertools import chain |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 317 | def L(seqn): |
| 318 | 'Test multiple tiers of iterators' |
Raymond Hettinger | a6c6037 | 2008-03-13 01:26:19 +0000 | [diff] [blame] | 319 | return chain(map(lambda x:x, R(Ig(G(seqn))))) |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 320 | |
Ezio Melotti | 8269a44 | 2011-05-09 07:15:04 +0300 | [diff] [blame] | 321 | |
Ezio Melotti | 22ebb2d | 2013-01-02 21:19:37 +0200 | [diff] [blame] | 322 | class TestErrorHandling: |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 323 | |
| 324 | def test_non_sequence(self): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 325 | for f in (self.module.heapify, self.module.heappop): |
Raymond Hettinger | 8a9c4d9 | 2011-04-13 11:49:57 -0700 | [diff] [blame] | 326 | self.assertRaises((TypeError, AttributeError), f, 10) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 327 | for f in (self.module.heappush, self.module.heapreplace, |
| 328 | self.module.nlargest, self.module.nsmallest): |
Raymond Hettinger | 8a9c4d9 | 2011-04-13 11:49:57 -0700 | [diff] [blame] | 329 | self.assertRaises((TypeError, AttributeError), f, 10, 10) |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 330 | |
| 331 | def test_len_only(self): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 332 | for f in (self.module.heapify, self.module.heappop): |
Raymond Hettinger | 8a9c4d9 | 2011-04-13 11:49:57 -0700 | [diff] [blame] | 333 | self.assertRaises((TypeError, AttributeError), f, LenOnly()) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 334 | for f in (self.module.heappush, self.module.heapreplace): |
Raymond Hettinger | 8a9c4d9 | 2011-04-13 11:49:57 -0700 | [diff] [blame] | 335 | self.assertRaises((TypeError, AttributeError), f, LenOnly(), 10) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 336 | for f in (self.module.nlargest, self.module.nsmallest): |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 337 | self.assertRaises(TypeError, f, 2, LenOnly()) |
| 338 | |
| 339 | def test_get_only(self): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 340 | for f in (self.module.heapify, self.module.heappop): |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 341 | self.assertRaises(TypeError, f, GetOnly()) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 342 | for f in (self.module.heappush, self.module.heapreplace): |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 343 | self.assertRaises(TypeError, f, GetOnly(), 10) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 344 | for f in (self.module.nlargest, self.module.nsmallest): |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 345 | self.assertRaises(TypeError, f, 2, GetOnly()) |
| 346 | |
| 347 | def test_get_only(self): |
| 348 | seq = [CmpErr(), CmpErr(), CmpErr()] |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 349 | for f in (self.module.heapify, self.module.heappop): |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 350 | self.assertRaises(ZeroDivisionError, f, seq) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 351 | for f in (self.module.heappush, self.module.heapreplace): |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 352 | self.assertRaises(ZeroDivisionError, f, seq, 10) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 353 | for f in (self.module.nlargest, self.module.nsmallest): |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 354 | self.assertRaises(ZeroDivisionError, f, 2, seq) |
| 355 | |
| 356 | def test_arg_parsing(self): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 357 | for f in (self.module.heapify, self.module.heappop, |
| 358 | self.module.heappush, self.module.heapreplace, |
| 359 | self.module.nlargest, self.module.nsmallest): |
Raymond Hettinger | 8a9c4d9 | 2011-04-13 11:49:57 -0700 | [diff] [blame] | 360 | self.assertRaises((TypeError, AttributeError), f, 10) |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 361 | |
| 362 | def test_iterable_args(self): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 363 | for f in (self.module.nlargest, self.module.nsmallest): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 364 | for s in ("123", "", range(1000), (1, 1.2), range(2000,2200,5)): |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 365 | for g in (G, I, Ig, L, R): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 366 | self.assertEqual(list(f(2, g(s))), list(f(2,s))) |
| 367 | self.assertEqual(list(f(2, S(s))), []) |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 368 | self.assertRaises(TypeError, f, 2, X(s)) |
| 369 | self.assertRaises(TypeError, f, 2, N(s)) |
| 370 | self.assertRaises(ZeroDivisionError, f, 2, E(s)) |
| 371 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 372 | |
Ezio Melotti | 22ebb2d | 2013-01-02 21:19:37 +0200 | [diff] [blame] | 373 | class TestErrorHandlingPython(TestErrorHandling, TestCase): |
Ezio Melotti | fd69abb | 2011-05-09 07:20:47 +0300 | [diff] [blame] | 374 | module = py_heapq |
| 375 | |
Ezio Melotti | 19f7ca2 | 2011-05-09 07:27:20 +0300 | [diff] [blame] | 376 | @skipUnless(c_heapq, 'requires _heapq') |
Ezio Melotti | 22ebb2d | 2013-01-02 21:19:37 +0200 | [diff] [blame] | 377 | class TestErrorHandlingC(TestErrorHandling, TestCase): |
Ezio Melotti | fd69abb | 2011-05-09 07:20:47 +0300 | [diff] [blame] | 378 | module = c_heapq |
| 379 | |
Raymond Hettinger | 855d9a9 | 2004-09-28 00:03:54 +0000 | [diff] [blame] | 380 | |
Guido van Rossum | 0b19178 | 2002-08-02 18:29:53 +0000 | [diff] [blame] | 381 | if __name__ == "__main__": |
Ezio Melotti | 22ebb2d | 2013-01-02 21:19:37 +0200 | [diff] [blame] | 382 | unittest.main() |