diff --git a/Lib/test/test_heapq.py b/Lib/test/test_heapq.py --- a/Lib/test/test_heapq.py +++ b/Lib/test/test_heapq.py @@ -4,14 +4,10 @@ import unittest from test import test_support import sys - -# We do a bit of trickery here to be able to test both the C implementation -# and the Python implementation of the module. -import heapq as c_heapq -py_heapq = test_support.import_fresh_module('heapq', blocked=['_heapq']) +from heapq import (heapify, heappop, heappush, heappushpop, heapreplace, + merge, nsmallest, nlargest) class TestHeap(unittest.TestCase): - module = None def test_push_pop(self): # 1) Push 256 random numbers and pop them off, verifying all's OK. @@ -21,11 +17,11 @@ for i in range(256): item = random.random() data.append(item) - self.module.heappush(heap, item) + heappush(heap, item) self.check_invariant(heap) results = [] while heap: - item = self.module.heappop(heap) + item = heappop(heap) self.check_invariant(heap) results.append(item) data_sorted = data[:] @@ -34,10 +30,10 @@ # 2) Check that the invariant holds for a sorted array self.check_invariant(results) - self.assertRaises(TypeError, self.module.heappush, []) + self.assertRaises(TypeError, heappush, []) try: - self.assertRaises(TypeError, self.module.heappush, None, None) - self.assertRaises(TypeError, self.module.heappop, None) + self.assertRaises(TypeError, heappush, None, None) + self.assertRaises(TypeError, heappop, None) except AttributeError: pass @@ -51,18 +47,18 @@ def test_heapify(self): for size in range(30): heap = [random.random() for dummy in range(size)] - self.module.heapify(heap) + heapify(heap) self.check_invariant(heap) - self.assertRaises(TypeError, self.module.heapify, None) + self.assertRaises(TypeError, heapify, None) def test_naive_nbest(self): data = [random.randrange(2000) for i in range(1000)] heap = [] for item in data: - self.module.heappush(heap, item) + heappush(heap, item) if len(heap) > 10: - self.module.heappop(heap) + heappop(heap) heap.sort() self.assertEqual(heap, sorted(data)[-10:]) @@ -70,7 +66,7 @@ # An iterator returning a heap's elements, smallest-first. try: while 1: - yield self.module.heappop(heap) + yield heappop(heap) except IndexError: pass @@ -82,42 +78,42 @@ # (10 log-time steps). data = [random.randrange(2000) for i in range(1000)] heap = data[:10] - self.module.heapify(heap) + heapify(heap) for item in data[10:]: if item > heap[0]: # this gets rarer the longer we run - self.module.heapreplace(heap, item) + heapreplace(heap, item) self.assertEqual(list(self.heapiter(heap)), sorted(data)[-10:]) - self.assertRaises(TypeError, self.module.heapreplace, None) - self.assertRaises(TypeError, self.module.heapreplace, None, None) - self.assertRaises(IndexError, self.module.heapreplace, [], None) + self.assertRaises(TypeError, heapreplace, None) + self.assertRaises(TypeError, heapreplace, None, None) + self.assertRaises(IndexError, heapreplace, [], None) def test_nbest_with_pushpop(self): data = [random.randrange(2000) for i in range(1000)] heap = data[:10] - self.module.heapify(heap) + heapify(heap) for item in data[10:]: - self.module.heappushpop(heap, item) + heappushpop(heap, item) self.assertEqual(list(self.heapiter(heap)), sorted(data)[-10:]) - self.assertEqual(self.module.heappushpop([], 'x'), 'x') + self.assertEqual(heappushpop([], 'x'), 'x') def test_heappushpop(self): h = [] - x = self.module.heappushpop(h, 10) + x = heappushpop(h, 10) self.assertEqual((h, x), ([], 10)) h = [10] - x = self.module.heappushpop(h, 10.0) + x = heappushpop(h, 10.0) self.assertEqual((h, x), ([10], 10.0)) self.assertEqual(type(h[0]), int) self.assertEqual(type(x), float) h = [10]; - x = self.module.heappushpop(h, 9) + x = heappushpop(h, 9) self.assertEqual((h, x), ([10], 9)) h = [10]; - x = self.module.heappushpop(h, 11) + x = heappushpop(h, 11) self.assertEqual((h, x), ([11], 10)) def test_heapsort(self): @@ -127,12 +123,12 @@ data = [random.randrange(25) for i in range(size)] if trial & 1: # Half of the time, use heapify heap = data[:] - self.module.heapify(heap) + heapify(heap) else: # The rest of the time, use heappush heap = [] for item in data: - self.module.heappush(heap, item) - heap_sorted = [self.module.heappop(heap) for i in range(size)] + heappush(heap, item) + heap_sorted = [heappop(heap) for i in range(size)] self.assertEqual(heap_sorted, sorted(data)) def test_merge(self): @@ -140,8 +136,8 @@ for i in xrange(random.randrange(5)): row = sorted(random.randrange(1000) for j in range(random.randrange(10))) inputs.append(row) - self.assertEqual(sorted(chain(*inputs)), list(self.module.merge(*inputs))) - self.assertEqual(list(self.module.merge()), []) + self.assertEqual(sorted(chain(*inputs)), list(merge(*inputs))) + self.assertEqual(list(merge()), []) def test_merge_stability(self): class Int(int): @@ -155,45 +151,35 @@ inputs[stream].append(obj) for stream in inputs: stream.sort() - result = [i.pair for i in self.module.merge(*inputs)] + result = [i.pair for i in merge(*inputs)] self.assertEqual(result, sorted(result)) def test_nsmallest(self): data = [(random.randrange(2000), i) for i in range(1000)] for f in (None, lambda x: x[0] * 547 % 2000): for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100): - self.assertEqual(self.module.nsmallest(n, data), sorted(data)[:n]) - self.assertEqual(self.module.nsmallest(n, data, key=f), + self.assertEqual(nsmallest(n, data), sorted(data)[:n]) + self.assertEqual(nsmallest(n, data, key=f), sorted(data, key=f)[:n]) def test_nlargest(self): data = [(random.randrange(2000), i) for i in range(1000)] for f in (None, lambda x: x[0] * 547 % 2000): for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100): - self.assertEqual(self.module.nlargest(n, data), + self.assertEqual(nlargest(n, data), sorted(data, reverse=True)[:n]) - self.assertEqual(self.module.nlargest(n, data, key=f), + self.assertEqual(nlargest(n, data, key=f), sorted(data, key=f, reverse=True)[:n]) -class TestHeapPython(TestHeap): - module = py_heapq - - # As an early adopter, we sanity check the - # test_support.import_fresh_module utility function - def test_pure_python(self): - self.assertFalse(sys.modules['heapq'] is self.module) - self.assertTrue(hasattr(self.module.heapify, 'func_code')) - - -class TestHeapC(TestHeap): - module = c_heapq - + # XXX @unittest.skipUnless(type(heapify)==type(oct), '_heapq specific') def test_comparison_operator(self): + if type(heapify) != type(oct): # only run for _heapq version + return # Issue 3501: Make sure heapq works with both __lt__ and __le__ def hsort(data, comp): data = map(comp, data) - self.module.heapify(data) - return [self.module.heappop(data).x for i in range(len(data))] + heapify(data) + return [heappop(data).x for i in range(len(data))] class LT: def __init__(self, x): self.x = x @@ -307,37 +293,37 @@ class TestErrorHandling(unittest.TestCase): def test_non_sequence(self): - for f in (self.module.heapify, self.module.heappop): + for f in (heapify, heappop): self.assertRaises((TypeError, AttributeError), f, 10) - for f in (self.module.heappush, self.module.heapreplace, - self.module.nlargest, self.module.nsmallest): + for f in (heappush, heapreplace, + nlargest, nsmallest): self.assertRaises((TypeError, AttributeError), f, 10, 10) def test_len_only(self): - for f in (self.module.heapify, self.module.heappop): + for f in (heapify, heappop): self.assertRaises((TypeError, AttributeError), f, LenOnly()) - for f in (self.module.heappush, self.module.heapreplace): + for f in (heappush, heapreplace): self.assertRaises((TypeError, AttributeError), f, LenOnly(), 10) - for f in (self.module.nlargest, self.module.nsmallest): + for f in (nlargest, nsmallest): self.assertRaises(TypeError, f, 2, LenOnly()) def test_get_only(self): seq = [CmpErr(), CmpErr(), CmpErr()] - for f in (self.module.heapify, self.module.heappop): + for f in (heapify, heappop): self.assertRaises(ZeroDivisionError, f, seq) - for f in (self.module.heappush, self.module.heapreplace): + for f in (heappush, heapreplace): self.assertRaises(ZeroDivisionError, f, seq, 10) - for f in (self.module.nlargest, self.module.nsmallest): + for f in (nlargest, nsmallest): self.assertRaises(ZeroDivisionError, f, 2, seq) def test_arg_parsing(self): - for f in (self.module.heapify, self.module.heappop, - self.module.heappush, self.module.heapreplace, - self.module.nlargest, self.module.nsmallest): + for f in (heapify, heappop, + heappush, heapreplace, + nlargest, nsmallest): self.assertRaises((TypeError, AttributeError), f, 10) def test_iterable_args(self): - for f in (self.module.nlargest, self.module.nsmallest): + for f in (nlargest, nsmallest): for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): for g in (G, I, Ig, L, R): with test_support.check_py3k_warnings( @@ -349,19 +335,12 @@ self.assertRaises(TypeError, f, 2, N(s)) self.assertRaises(ZeroDivisionError, f, 2, E(s)) -class TestErrorHandling_Python(TestErrorHandling): - module = py_heapq - -class TestErrorHandling_C(TestErrorHandling): - module = c_heapq - #============================================================================== def test_main(verbose=None): - test_classes = [TestHeapPython, TestHeapC, TestErrorHandling_Python, - TestErrorHandling_C] + test_classes = [TestHeap, TestErrorHandling] test_support.run_unittest(*test_classes) # verify reference counting @@ -374,5 +353,16 @@ counts[i] = sys.gettotalrefcount() print counts + # We do a bit of trickery here to be able to test + # the module in the absence of the C accelerations + assert type(heapify) == type(oct) # sanity check + py_heapq = test_support.import_fresh_module('heapq', blocked=['_heapq']) + for name in py_heapq.__all__: + globals()[name] = getattr(py_heapq, name) + assert type(heapify) == type(lambda :0) # sanity check + test_support.run_unittest(*test_classes) + + if __name__ == "__main__": test_main(verbose=True) +