summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_heapq.py
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2011-05-09 04:15:04 (GMT)
committerEzio Melotti <ezio.melotti@gmail.com>2011-05-09 04:15:04 (GMT)
commit8269a44d5095cbd0c351aedc41272e9a6d91c269 (patch)
treebb8d4ae16e197ddc2103e8f169199fa6e346c0cc /Lib/test/test_heapq.py
parent199e0857f9040c56d7a37fac62fdc5b0df7a08a7 (diff)
downloadcpython-8269a44d5095cbd0c351aedc41272e9a6d91c269.zip
cpython-8269a44d5095cbd0c351aedc41272e9a6d91c269.tar.gz
cpython-8269a44d5095cbd0c351aedc41272e9a6d91c269.tar.bz2
#11910: Fix test_heapq to skip the C tests when _heapq is missing.
Diffstat (limited to 'Lib/test/test_heapq.py')
-rw-r--r--Lib/test/test_heapq.py47
1 files changed, 27 insertions, 20 deletions
diff --git a/Lib/test/test_heapq.py b/Lib/test/test_heapq.py
index 5e3b0ee..3016c30 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 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 = support.import_fresh_module('heapq', blocked=['_heapq'])
+c_heapq = 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 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(unittest.TestCase):
+
+class TestHeap(TestCase):
module = None
def test_push_pop(self):
@@ -176,16 +191,12 @@ class TestHeap(unittest.TestCase):
self.assertEqual(list(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, '__code__'))
-
+@skipUnless(c_heapq, 'requires _heapq')
class TestHeapC(TestHeap):
module = c_heapq
@@ -211,12 +222,6 @@ class TestHeapC(TestHeap):
self.assertEqual(hsort(data, LT), target)
self.assertRaises(TypeError, data, LE)
- # As an early adopter, we sanity check the
- # test.support.import_fresh_module utility function
- def test_accelerated(self):
- self.assertTrue(sys.modules['heapq'] is self.module)
- self.assertFalse(hasattr(self.module.heapify, '__code__'))
-
#==============================================================================
@@ -313,7 +318,9 @@ def L(seqn):
'Test multiple tiers of iterators'
return chain(map(lambda x:x, R(Ig(G(seqn)))))
-class TestErrorHandling(unittest.TestCase):
+
+@skipUnless(c_heapq, 'requires _heapq')
+class TestErrorHandling(TestCase):
# only for C implementation
module = c_heapq
@@ -372,7 +379,7 @@ class TestErrorHandling(unittest.TestCase):
def test_main(verbose=None):
from types import BuiltinFunctionType
- test_classes = [TestHeapPython, TestHeapC, TestErrorHandling]
+ test_classes = [TestModules, TestHeapPython, TestHeapC, TestErrorHandling]
support.run_unittest(*test_classes)
# verify reference counting