#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 test import support
from collections import UserList from collections import UserList
# We do a bit of trickery here to be able to test both the C implementation py_bisect = support.import_fresh_module('bisect', blocked=['_bisect'])
# and the Python implementation of the module. c_bisect = support.import_fresh_module('bisect', fresh=['_bisect'])
# 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
class Range(object): class Range(object):
"""A trivial range()-like object without any integer width limitations.""" """A trivial range()-like object without any integer width limitations."""
@ -45,9 +28,7 @@ class Range(object):
self.last_insert = idx, item self.last_insert = idx, item
class TestBisect(unittest.TestCase): class TestBisect:
module = None
def setUp(self): def setUp(self):
self.precomputedCases = [ self.precomputedCases = [
(self.module.bisect_right, [], 1, 0), (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.module.insort(a=data, x=25, lo=1, hi=3)
self.assertEqual(data, [10, 20, 25, 25, 25, 30, 40, 50]) self.assertEqual(data, [10, 20, 25, 25, 25, 30, 40, 50])
class TestBisectPython(TestBisect): class TestBisectPython(TestBisect, unittest.TestCase):
module = py_bisect module = py_bisect
class TestBisectC(TestBisect): class TestBisectC(TestBisect, unittest.TestCase):
module = c_bisect module = c_bisect
#============================================================================== #==============================================================================
class TestInsort(unittest.TestCase): class TestInsort:
module = None
def test_vsBuiltinSort(self, n=500): def test_vsBuiltinSort(self, n=500):
from random import choice from random import choice
for insorted in (list(), UserList()): for insorted in (list(), UserList()):
@ -255,15 +234,14 @@ class TestInsort(unittest.TestCase):
self.module.insort_right(lst, 5) self.module.insort_right(lst, 5)
self.assertEqual([5, 10], lst.data) self.assertEqual([5, 10], lst.data)
class TestInsortPython(TestInsort): class TestInsortPython(TestInsort, unittest.TestCase):
module = py_bisect module = py_bisect
class TestInsortC(TestInsort): class TestInsortC(TestInsort, unittest.TestCase):
module = c_bisect module = c_bisect
#============================================================================== #==============================================================================
class LenOnly: class LenOnly:
"Dummy sequence class defining __len__ but not __getitem__." "Dummy sequence class defining __len__ but not __getitem__."
def __len__(self): def __len__(self):
@ -284,9 +262,7 @@ class CmpErr:
__eq__ = __lt__ __eq__ = __lt__
__ne__ = __lt__ __ne__ = __lt__
class TestErrorHandling(unittest.TestCase): class TestErrorHandling:
module = None
def test_non_sequence(self): def test_non_sequence(self):
for f in (self.module.bisect_left, self.module.bisect_right, for f in (self.module.bisect_left, self.module.bisect_right,
self.module.insort_left, self.module.insort_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.module.insort_left, self.module.insort_right):
self.assertRaises(TypeError, f, 10) self.assertRaises(TypeError, f, 10)
class TestErrorHandlingPython(TestErrorHandling): class TestErrorHandlingPython(TestErrorHandling, unittest.TestCase):
module = py_bisect module = py_bisect
class TestErrorHandlingC(TestErrorHandling): class TestErrorHandlingC(TestErrorHandling, unittest.TestCase):
module = c_bisect module = c_bisect
#============================================================================== #==============================================================================
libreftest = """ class TestDocExample:
Example from the Library Reference: Doc/library/bisect.rst 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. result = [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
This example uses bisect() to look up a letter grade for an exam total self.assertEqual(result, ['F', 'A', 'C', 'C', 'B', 'A', 'A'])
(say) based on a set of ordered numeric breakpoints: 85 and up is an `A',
75..84 is a `B', etc.
>>> grades = "FEDCBA" def test_colors(self):
>>> breakpoints = [30, 44, 66, 75, 85] data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)]
>>> from bisect import bisect data.sort(key=lambda r: r[1])
>>> def grade(total): keys = [r[1] for r in data]
... return grades[bisect(breakpoints, total)] bisect_left = self.module.bisect_left
... self.assertEqual(data[bisect_left(keys, 0)], ('black', 0))
>>> grade(66) self.assertEqual(data[bisect_left(keys, 1)], ('blue', 1))
'C' self.assertEqual(data[bisect_left(keys, 5)], ('red', 5))
>>> list(map(grade, [33, 99, 77, 44, 12, 88])) self.assertEqual(data[bisect_left(keys, 8)], ('yellow', 8))
['E', 'A', 'B', 'D', 'F', 'A']
""" 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__": 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 #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 - Issue #16852: test_genericpath, test_posixpath, test_ntpath, and test_macpath
now work with unittest test discovery. Patch by Zachary Ware. now work with unittest test discovery. Patch by Zachary Ware.