Changeset 391 for python/trunk/Lib/test/test_heapq.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_heapq.py
r2 r391 1 1 """Unittests for heapq.""" 2 2 3 import sys 3 4 import random 4 import unittest 5 5 6 from test import test_support 6 import sys 7 8 # We do a bit of trickery here to be able to test both the C implementation 9 # and the Python implementation of the module. 10 11 # Make it impossible to import the C implementation anymore. 12 sys.modules['_heapq'] = 0 13 # We must also handle the case that heapq was imported before. 14 if 'heapq' in sys.modules: 15 del sys.modules['heapq'] 16 17 # Now we can import the module and get the pure Python implementation. 18 import heapq as py_heapq 19 20 # Restore everything to normal. 21 del sys.modules['_heapq'] 22 del sys.modules['heapq'] 23 24 # This is now the module with the C implementation. 25 import heapq as c_heapq 26 27 28 class TestHeap(unittest.TestCase): 7 from unittest import TestCase, skipUnless 8 9 py_heapq = test_support.import_fresh_module('heapq', blocked=['_heapq']) 10 c_heapq = test_support.import_fresh_module('heapq', fresh=['_heapq']) 11 12 # _heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when 13 # _heapq is imported, so check them there 14 func_names = ['heapify', 'heappop', 'heappush', 'heappushpop', 15 'heapreplace', '_nlargest', '_nsmallest'] 16 17 class 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 28 class TestHeap(TestCase): 29 29 module = None 30 30 … … 62 62 if pos: # pos 0 has no parent 63 63 parentpos = (pos-1) >> 1 64 self.assert _(heap[parentpos] <= item)64 self.assertTrue(heap[parentpos] <= item) 65 65 66 66 def test_heapify(self): … … 159 159 self.assertEqual(list(self.module.merge()), []) 160 160 161 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 161 170 def test_merge_stability(self): 162 171 class Int(int): … … 191 200 sorted(data, key=f, reverse=True)[:n]) 192 201 193 class TestHeapPython(TestHeap):194 module = py_heapq195 196 class TestHeapC(TestHeap):197 module = c_heapq198 199 202 def test_comparison_operator(self): 200 # Issue 3 501: Make sure heapq works with both __lt__ and __le__203 # Issue 3051: Make sure heapq works with both __lt__ and __le__ 201 204 def hsort(data, comp): 202 205 data = map(comp, data) … … 219 222 220 223 224 class TestHeapPython(TestHeap): 225 module = py_heapq 226 227 228 @skipUnless(c_heapq, 'requires _heapq') 229 class TestHeapC(TestHeap): 230 module = c_heapq 231 232 221 233 #============================================================================== 222 234 … … 313 325 return chain(imap(lambda x:x, R(Ig(G(seqn))))) 314 326 315 class TestErrorHandling(unittest.TestCase): 316 # only for C implementation 317 module = c_heapq 327 class 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 337 class TestErrorHandling(TestCase): 338 module = None 318 339 319 340 def test_non_sequence(self): 320 341 for f in (self.module.heapify, self.module.heappop): 321 self.assertRaises( TypeError, f, 10)342 self.assertRaises((TypeError, AttributeError), f, 10) 322 343 for f in (self.module.heappush, self.module.heapreplace, 323 344 self.module.nlargest, self.module.nsmallest): 324 self.assertRaises( TypeError, f, 10, 10)345 self.assertRaises((TypeError, AttributeError), f, 10, 10) 325 346 326 347 def test_len_only(self): 327 348 for f in (self.module.heapify, self.module.heappop): 328 self.assertRaises( TypeError, f, LenOnly())349 self.assertRaises((TypeError, AttributeError), f, LenOnly()) 329 350 for f in (self.module.heappush, self.module.heapreplace): 330 self.assertRaises( TypeError, f, LenOnly(), 10)351 self.assertRaises((TypeError, AttributeError), f, LenOnly(), 10) 331 352 for f in (self.module.nlargest, self.module.nsmallest): 332 353 self.assertRaises(TypeError, f, 2, LenOnly()) 333 334 def test_get_only(self):335 for f in (self.module.heapify, self.module.heappop):336 self.assertRaises(TypeError, f, GetOnly())337 for f in (self.module.heappush, self.module.heapreplace):338 self.assertRaises(TypeError, f, GetOnly(), 10)339 for f in (self.module.nlargest, self.module.nsmallest):340 self.assertRaises(TypeError, f, 2, GetOnly())341 354 342 355 def test_get_only(self): … … 353 366 self.module.heappush, self.module.heapreplace, 354 367 self.module.nlargest, self.module.nsmallest): 355 self.assertRaises( TypeError, f, 10)368 self.assertRaises((TypeError, AttributeError), f, 10) 356 369 357 370 def test_iterable_args(self): … … 359 372 for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): 360 373 for g in (G, I, Ig, L, R): 361 self.assertEqual(f(2, g(s)), f(2,s)) 374 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)) 362 378 self.assertEqual(f(2, S(s)), []) 363 379 self.assertRaises(TypeError, f, 2, X(s)) … … 365 381 self.assertRaises(ZeroDivisionError, f, 2, E(s)) 366 382 383 # 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 399 400 class TestErrorHandlingPython(TestErrorHandling): 401 module = py_heapq 402 403 404 @skipUnless(c_heapq, 'requires _heapq') 405 class TestErrorHandlingC(TestErrorHandling): 406 module = c_heapq 407 367 408 368 409 #============================================================================== … … 370 411 371 412 def test_main(verbose=None): 372 from types import BuiltinFunctionType 373 374 test_classes = [TestHeapPython, TestHeapC, TestErrorHandling] 413 test_classes = [TestModules, TestHeapPython, TestHeapC, 414 TestErrorHandlingPython, TestErrorHandlingC] 375 415 test_support.run_unittest(*test_classes) 376 416
Note:
See TracChangeset
for help on using the changeset viewer.