#16897: merge with 3.3.

This commit is contained in:
Ezio Melotti 2013-01-10 04:33:17 +02:00
commit 9e97071fe0
2 changed files with 36 additions and 75 deletions

View File

@ -3,25 +3,8 @@ import unittest
from test import support
from collections import UserList
# We do a bit of trickery here to be able to test both the C implementation
# and the Python implementation of the module.
# Make it impossible to import the C implementation anymore.
sys.modules['_bisect'] = 0
# We must also handle the case that bisect was imported before.
if 'bisect' in sys.modules:
del sys.modules['bisect']
# Now we can import the module and get the pure Python implementation.
import bisect as py_bisect
# Restore everything to normal.
del sys.modules['_bisect']
del sys.modules['bisect']
# This is now the module with the C implementation.
import bisect as c_bisect
py_bisect = support.import_fresh_module('bisect', blocked=['_bisect'])
c_bisect = support.import_fresh_module('bisect', fresh=['_bisect'])
class Range(object):
"""A trivial range()-like object without any integer width limitations."""
@ -45,9 +28,7 @@ class Range(object):
self.last_insert = idx, item
class TestBisect(unittest.TestCase):
module = None
class TestBisect:
def setUp(self):
self.precomputedCases = [
(self.module.bisect_right, [], 1, 0),
@ -218,17 +199,15 @@ class TestBisect(unittest.TestCase):
self.module.insort(a=data, x=25, lo=1, hi=3)
self.assertEqual(data, [10, 20, 25, 25, 25, 30, 40, 50])
class TestBisectPython(TestBisect):
class TestBisectPython(TestBisect, unittest.TestCase):
module = py_bisect
class TestBisectC(TestBisect):
class TestBisectC(TestBisect, unittest.TestCase):
module = c_bisect
#==============================================================================
class TestInsort(unittest.TestCase):
module = None
class TestInsort:
def test_vsBuiltinSort(self, n=500):
from random import choice
for insorted in (list(), UserList()):
@ -255,15 +234,14 @@ class TestInsort(unittest.TestCase):
self.module.insort_right(lst, 5)
self.assertEqual([5, 10], lst.data)
class TestInsortPython(TestInsort):
class TestInsortPython(TestInsort, unittest.TestCase):
module = py_bisect
class TestInsortC(TestInsort):
class TestInsortC(TestInsort, unittest.TestCase):
module = c_bisect
#==============================================================================
class LenOnly:
"Dummy sequence class defining __len__ but not __getitem__."
def __len__(self):
@ -284,9 +262,7 @@ class CmpErr:
__eq__ = __lt__
__ne__ = __lt__
class TestErrorHandling(unittest.TestCase):
module = None
class TestErrorHandling:
def test_non_sequence(self):
for f in (self.module.bisect_left, self.module.bisect_right,
self.module.insort_left, self.module.insort_right):
@ -313,58 +289,40 @@ class TestErrorHandling(unittest.TestCase):
self.module.insort_left, self.module.insort_right):
self.assertRaises(TypeError, f, 10)
class TestErrorHandlingPython(TestErrorHandling):
class TestErrorHandlingPython(TestErrorHandling, unittest.TestCase):
module = py_bisect
class TestErrorHandlingC(TestErrorHandling):
class TestErrorHandlingC(TestErrorHandling, unittest.TestCase):
module = c_bisect
#==============================================================================
libreftest = """
Example from the Library Reference: Doc/library/bisect.rst
class TestDocExample:
def test_grades(self):
def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
i = self.module.bisect(breakpoints, score)
return grades[i]
The bisect() function is generally useful for categorizing numeric data.
This example uses bisect() to look up a letter grade for an exam total
(say) based on a set of ordered numeric breakpoints: 85 and up is an `A',
75..84 is a `B', etc.
result = [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
self.assertEqual(result, ['F', 'A', 'C', 'C', 'B', 'A', 'A'])
>>> grades = "FEDCBA"
>>> breakpoints = [30, 44, 66, 75, 85]
>>> from bisect import bisect
>>> def grade(total):
... return grades[bisect(breakpoints, total)]
...
>>> grade(66)
'C'
>>> list(map(grade, [33, 99, 77, 44, 12, 88]))
['E', 'A', 'B', 'D', 'F', 'A']
def test_colors(self):
data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)]
data.sort(key=lambda r: r[1])
keys = [r[1] for r in data]
bisect_left = self.module.bisect_left
self.assertEqual(data[bisect_left(keys, 0)], ('black', 0))
self.assertEqual(data[bisect_left(keys, 1)], ('blue', 1))
self.assertEqual(data[bisect_left(keys, 5)], ('red', 5))
self.assertEqual(data[bisect_left(keys, 8)], ('yellow', 8))
"""
class TestDocExamplePython(TestDocExample, unittest.TestCase):
module = py_bisect
class TestDocExampleC(TestDocExample, unittest.TestCase):
module = c_bisect
#------------------------------------------------------------------------------
__test__ = {'libreftest' : libreftest}
def test_main(verbose=None):
from test import test_bisect
test_classes = [TestBisectPython, TestBisectC,
TestInsortPython, TestInsortC,
TestErrorHandlingPython, TestErrorHandlingC]
support.run_unittest(*test_classes)
support.run_doctest(test_bisect, verbose)
# verify reference counting
if verbose and hasattr(sys, "gettotalrefcount"):
import gc
counts = [None] * 5
for i in range(len(counts)):
support.run_unittest(*test_classes)
gc.collect()
counts[i] = sys.gettotalrefcount()
print(counts)
if __name__ == "__main__":
test_main(verbose=True)
unittest.main()

View File

@ -614,6 +614,9 @@ Tests
- Issue #16836: Enable IPv6 support even if IPv6 is disabled on the build host.
- Issue #16897: test_bisect now works with unittest test discovery.
Initial patch by Zachary Ware.
- Issue #16852: test_genericpath, test_posixpath, test_ntpath, and test_macpath
now work with unittest test discovery. Patch by Zachary Ware.