summaryrefslogtreecommitdiff
path: root/Lib/test/test_heapq.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_heapq.py')
-rw-r--r--Lib/test/test_heapq.py68
1 files changed, 44 insertions, 24 deletions
diff --git a/Lib/test/test_heapq.py b/Lib/test/test_heapq.py
index d5d8c1a179..5932a40c28 100644
--- a/Lib/test/test_heapq.py
+++ b/Lib/test/test_heapq.py
@@ -1,16 +1,31 @@
"""Unittests for heapq."""
+import sys
import random
-import unittest
+
from test import test_support
-import sys
+from unittest import TestCase, skipUnless
-# 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'])
+c_heapq = test_support.import_fresh_module('heapq', fresh=['_heapq'])
+
+# _heapq.nlargest/nsmallest are saved in heapq._nlargest/_smallest when
+# _heapq is imported, so check them there
+func_names = ['heapify', 'heappop', 'heappush', 'heappushpop',
+ 'heapreplace', '_nlargest', '_nsmallest']
-class TestHeap(unittest.TestCase):
+class TestModules(TestCase):
+ def test_py_functions(self):
+ for fname in func_names:
+ self.assertEqual(getattr(py_heapq, fname).__module__, 'heapq')
+
+ @skipUnless(c_heapq, 'requires _heapq')
+ def test_c_functions(self):
+ for fname in func_names:
+ self.assertEqual(getattr(c_heapq, fname).__module__, '_heapq')
+
+
+class TestHeap(TestCase):
module = None
def test_push_pop(self):
@@ -175,21 +190,8 @@ class TestHeap(unittest.TestCase):
self.assertEqual(self.module.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
-
def test_comparison_operator(self):
- # Issue 3501: Make sure heapq works with both __lt__ and __le__
+ # Issue 3051: Make sure heapq works with both __lt__ and __le__
def hsort(data, comp):
data = map(comp, data)
self.module.heapify(data)
@@ -210,6 +212,15 @@ class TestHeapC(TestHeap):
self.assertEqual(hsort(data, LE), target)
+class TestHeapPython(TestHeap):
+ module = py_heapq
+
+
+@skipUnless(c_heapq, 'requires _heapq')
+class TestHeapC(TestHeap):
+ module = c_heapq
+
+
#==============================================================================
class LenOnly:
@@ -304,9 +315,8 @@ def L(seqn):
'Test multiple tiers of iterators'
return chain(imap(lambda x:x, R(Ig(G(seqn)))))
-class TestErrorHandling(unittest.TestCase):
- # only for C implementation
- module = c_heapq
+class TestErrorHandling(TestCase):
+ module = None
def test_non_sequence(self):
for f in (self.module.heapify, self.module.heappop):
@@ -352,11 +362,21 @@ class TestErrorHandling(unittest.TestCase):
self.assertRaises(ZeroDivisionError, f, 2, E(s))
+class TestErrorHandlingPython(TestErrorHandling):
+ module = py_heapq
+
+
+@skipUnless(c_heapq, 'requires _heapq')
+class TestErrorHandlingC(TestErrorHandling):
+ module = c_heapq
+
+
#==============================================================================
def test_main(verbose=None):
- test_classes = [TestHeapPython, TestHeapC, TestErrorHandling]
+ test_classes = [TestModules, TestHeapPython, TestHeapC,
+ TestErrorHandlingPython, TestErrorHandlingC]
test_support.run_unittest(*test_classes)
# verify reference counting