Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_heapq.py

    r2 r391  
    11"""Unittests for heapq."""
    22
     3import sys
    34import random
    4 import unittest
     5
    56from 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):
     7from unittest import TestCase, skipUnless
     8
     9py_heapq = test_support.import_fresh_module('heapq', blocked=['_heapq'])
     10c_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
     14func_names = ['heapify', 'heappop', 'heappush', 'heappushpop',
     15              'heapreplace', '_nlargest', '_nsmallest']
     16
     17class 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
     28class TestHeap(TestCase):
    2929    module = None
    3030
     
    6262            if pos: # pos 0 has no parent
    6363                parentpos = (pos-1) >> 1
    64                 self.assert_(heap[parentpos] <= item)
     64                self.assertTrue(heap[parentpos] <= item)
    6565
    6666    def test_heapify(self):
     
    159159        self.assertEqual(list(self.module.merge()), [])
    160160
     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
    161170    def test_merge_stability(self):
    162171        class Int(int):
     
    191200                                 sorted(data, key=f, reverse=True)[:n])
    192201
    193 class TestHeapPython(TestHeap):
    194     module = py_heapq
    195 
    196 class TestHeapC(TestHeap):
    197     module = c_heapq
    198 
    199202    def test_comparison_operator(self):
    200         # Issue 3501: Make sure heapq works with both __lt__ and __le__
     203        # Issue 3051: Make sure heapq works with both __lt__ and __le__
    201204        def hsort(data, comp):
    202205            data = map(comp, data)
     
    219222
    220223
     224class TestHeapPython(TestHeap):
     225    module = py_heapq
     226
     227
     228@skipUnless(c_heapq, 'requires _heapq')
     229class TestHeapC(TestHeap):
     230    module = c_heapq
     231
     232
    221233#==============================================================================
    222234
     
    313325    return chain(imap(lambda x:x, R(Ig(G(seqn)))))
    314326
    315 class TestErrorHandling(unittest.TestCase):
    316     # only for C implementation
    317     module = c_heapq
     327class 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
     337class TestErrorHandling(TestCase):
     338    module = None
    318339
    319340    def test_non_sequence(self):
    320341        for f in (self.module.heapify, self.module.heappop):
    321             self.assertRaises(TypeError, f, 10)
     342            self.assertRaises((TypeError, AttributeError), f, 10)
    322343        for f in (self.module.heappush, self.module.heapreplace,
    323344                  self.module.nlargest, self.module.nsmallest):
    324             self.assertRaises(TypeError, f, 10, 10)
     345            self.assertRaises((TypeError, AttributeError), f, 10, 10)
    325346
    326347    def test_len_only(self):
    327348        for f in (self.module.heapify, self.module.heappop):
    328             self.assertRaises(TypeError, f, LenOnly())
     349            self.assertRaises((TypeError, AttributeError), f, LenOnly())
    329350        for f in (self.module.heappush, self.module.heapreplace):
    330             self.assertRaises(TypeError, f, LenOnly(), 10)
     351            self.assertRaises((TypeError, AttributeError), f, LenOnly(), 10)
    331352        for f in (self.module.nlargest, self.module.nsmallest):
    332353            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())
    341354
    342355    def test_get_only(self):
     
    353366                  self.module.heappush, self.module.heapreplace,
    354367                  self.module.nlargest, self.module.nsmallest):
    355             self.assertRaises(TypeError, f, 10)
     368            self.assertRaises((TypeError, AttributeError), f, 10)
    356369
    357370    def test_iterable_args(self):
     
    359372            for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)):
    360373                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))
    362378                self.assertEqual(f(2, S(s)), [])
    363379                self.assertRaises(TypeError, f, 2, X(s))
     
    365381                self.assertRaises(ZeroDivisionError, f, 2, E(s))
    366382
     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
     400class TestErrorHandlingPython(TestErrorHandling):
     401    module = py_heapq
     402
     403
     404@skipUnless(c_heapq, 'requires _heapq')
     405class TestErrorHandlingC(TestErrorHandling):
     406    module = c_heapq
     407
    367408
    368409#==============================================================================
     
    370411
    371412def 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]
    375415    test_support.run_unittest(*test_classes)
    376416
Note: See TracChangeset for help on using the changeset viewer.