11"""Unittests for heapq."""
22
3+ import sys
34import random
4- import unittest
5+
56from test import support
6- import sys
7+ from unittest import TestCase , skipUnless
78
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- import heapq as c_heapq
119py_heapq = support .import_fresh_module ('heapq' , blocked = ['_heapq' ])
10+ c_heapq = 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' )
1226
13- class TestHeap (unittest .TestCase ):
27+
28+ class TestHeap (TestCase ):
1429 module = None
1530
1631 def test_push_pop (self ):
@@ -176,16 +191,12 @@ def test_nlargest(self):
176191 self .assertEqual (list (self .module .nlargest (n , data , key = f )),
177192 sorted (data , key = f , reverse = True )[:n ])
178193
194+
179195class TestHeapPython (TestHeap ):
180196 module = py_heapq
181197
182- # As an early adopter, we sanity check the
183- # test.support.import_fresh_module utility function
184- def test_pure_python (self ):
185- self .assertFalse (sys .modules ['heapq' ] is self .module )
186- self .assertTrue (hasattr (self .module .heapify , '__code__' ))
187-
188198
199+ @skipUnless (c_heapq , 'requires _heapq' )
189200class TestHeapC (TestHeap ):
190201 module = c_heapq
191202
@@ -211,12 +222,6 @@ def __le__(self, other):
211222 self .assertEqual (hsort (data , LT ), target )
212223 self .assertRaises (TypeError , data , LE )
213224
214- # As an early adopter, we sanity check the
215- # test.support.import_fresh_module utility function
216- def test_accelerated (self ):
217- self .assertTrue (sys .modules ['heapq' ] is self .module )
218- self .assertFalse (hasattr (self .module .heapify , '__code__' ))
219-
220225
221226#==============================================================================
222227
@@ -313,7 +318,9 @@ def L(seqn):
313318 'Test multiple tiers of iterators'
314319 return chain (map (lambda x :x , R (Ig (G (seqn )))))
315320
316- class TestErrorHandling (unittest .TestCase ):
321+
322+ @skipUnless (c_heapq , 'requires _heapq' )
323+ class TestErrorHandling (TestCase ):
317324 # only for C implementation
318325 module = c_heapq
319326
@@ -372,7 +379,7 @@ def test_iterable_args(self):
372379def test_main (verbose = None ):
373380 from types import BuiltinFunctionType
374381
375- test_classes = [TestHeapPython , TestHeapC , TestErrorHandling ]
382+ test_classes = [TestModules , TestHeapPython , TestHeapC , TestErrorHandling ]
376383 support .run_unittest (* test_classes )
377384
378385 # verify reference counting
0 commit comments