Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f472a90

Browse files
committed
#16897: test_bisect now works with unittest test discovery. Initial patch by Zachary Ware.
1 parent e212370 commit f472a90

2 files changed

Lines changed: 38 additions & 77 deletions

File tree

Lib/test/test_bisect.py

Lines changed: 35 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,8 @@
33
from test import support
44
from collections import UserList
55

6-
# We do a bit of trickery here to be able to test both the C implementation
7-
# and the Python implementation of the module.
8-
9-
# Make it impossible to import the C implementation anymore.
10-
sys.modules['_bisect'] = 0
11-
# We must also handle the case that bisect was imported before.
12-
if 'bisect' in sys.modules:
13-
del sys.modules['bisect']
14-
15-
# Now we can import the module and get the pure Python implementation.
16-
import bisect as py_bisect
17-
18-
# Restore everything to normal.
19-
del sys.modules['_bisect']
20-
del sys.modules['bisect']
21-
22-
# This is now the module with the C implementation.
23-
import bisect as c_bisect
24-
6+
py_bisect = support.import_fresh_module('bisect', blocked=['_bisect'])
7+
c_bisect = support.import_fresh_module('bisect', fresh=['_bisect'])
258

269
class Range(object):
2710
"""A trivial range()-like object without any integer width limitations."""
@@ -45,9 +28,7 @@ def insert(self, idx, item):
4528
self.last_insert = idx, item
4629

4730

48-
class TestBisect(unittest.TestCase):
49-
module = None
50-
31+
class TestBisect:
5132
def setUp(self):
5233
self.precomputedCases = [
5334
(self.module.bisect_right, [], 1, 0),
@@ -218,17 +199,15 @@ def test_keyword_args(self):
218199
self.module.insort(a=data, x=25, lo=1, hi=3)
219200
self.assertEqual(data, [10, 20, 25, 25, 25, 30, 40, 50])
220201

221-
class TestBisectPython(TestBisect):
202+
class TestBisectPython(TestBisect, unittest.TestCase):
222203
module = py_bisect
223204

224-
class TestBisectC(TestBisect):
205+
class TestBisectC(TestBisect, unittest.TestCase):
225206
module = c_bisect
226207

227208
#==============================================================================
228209

229-
class TestInsort(unittest.TestCase):
230-
module = None
231-
210+
class TestInsort:
232211
def test_vsBuiltinSort(self, n=500):
233212
from random import choice
234213
for insorted in (list(), UserList()):
@@ -255,15 +234,14 @@ def insert(self, index, item):
255234
self.module.insort_right(lst, 5)
256235
self.assertEqual([5, 10], lst.data)
257236

258-
class TestInsortPython(TestInsort):
237+
class TestInsortPython(TestInsort, unittest.TestCase):
259238
module = py_bisect
260239

261-
class TestInsortC(TestInsort):
240+
class TestInsortC(TestInsort, unittest.TestCase):
262241
module = c_bisect
263242

264243
#==============================================================================
265244

266-
267245
class LenOnly:
268246
"Dummy sequence class defining __len__ but not __getitem__."
269247
def __len__(self):
@@ -284,9 +262,7 @@ def __lt__(self, other):
284262
__eq__ = __lt__
285263
__ne__ = __lt__
286264

287-
class TestErrorHandling(unittest.TestCase):
288-
module = None
289-
265+
class TestErrorHandling:
290266
def test_non_sequence(self):
291267
for f in (self.module.bisect_left, self.module.bisect_right,
292268
self.module.insort_left, self.module.insort_right):
@@ -313,58 +289,40 @@ def test_arg_parsing(self):
313289
self.module.insort_left, self.module.insort_right):
314290
self.assertRaises(TypeError, f, 10)
315291

316-
class TestErrorHandlingPython(TestErrorHandling):
292+
class TestErrorHandlingPython(TestErrorHandling, unittest.TestCase):
317293
module = py_bisect
318294

319-
class TestErrorHandlingC(TestErrorHandling):
295+
class TestErrorHandlingC(TestErrorHandling, unittest.TestCase):
320296
module = c_bisect
321297

322298
#==============================================================================
323299

324-
libreftest = """
325-
Example from the Library Reference: Doc/library/bisect.rst
326-
327-
The bisect() function is generally useful for categorizing numeric data.
328-
This example uses bisect() to look up a letter grade for an exam total
329-
(say) based on a set of ordered numeric breakpoints: 85 and up is an `A',
330-
75..84 is a `B', etc.
331-
332-
>>> grades = "FEDCBA"
333-
>>> breakpoints = [30, 44, 66, 75, 85]
334-
>>> from bisect import bisect
335-
>>> def grade(total):
336-
... return grades[bisect(breakpoints, total)]
337-
...
338-
>>> grade(66)
339-
'C'
340-
>>> list(map(grade, [33, 99, 77, 44, 12, 88]))
341-
['E', 'A', 'B', 'D', 'F', 'A']
300+
class TestDocExample:
301+
def test_grades(self):
302+
def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
303+
i = self.module.bisect(breakpoints, score)
304+
return grades[i]
305+
306+
result = [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
307+
self.assertEqual(result, ['F', 'A', 'C', 'C', 'B', 'A', 'A'])
308+
309+
def test_colors(self):
310+
data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)]
311+
data.sort(key=lambda r: r[1])
312+
keys = [r[1] for r in data]
313+
bisect_left = self.module.bisect_left
314+
self.assertEqual(data[bisect_left(keys, 0)], ('black', 0))
315+
self.assertEqual(data[bisect_left(keys, 1)], ('blue', 1))
316+
self.assertEqual(data[bisect_left(keys, 5)], ('red', 5))
317+
self.assertEqual(data[bisect_left(keys, 8)], ('yellow', 8))
318+
319+
class TestDocExamplePython(TestDocExample, unittest.TestCase):
320+
module = py_bisect
342321

343-
"""
322+
class TestDocExampleC(TestDocExample, unittest.TestCase):
323+
module = c_bisect
344324

345325
#------------------------------------------------------------------------------
346326

347-
__test__ = {'libreftest' : libreftest}
348-
349-
def test_main(verbose=None):
350-
from test import test_bisect
351-
352-
test_classes = [TestBisectPython, TestBisectC,
353-
TestInsortPython, TestInsortC,
354-
TestErrorHandlingPython, TestErrorHandlingC]
355-
356-
support.run_unittest(*test_classes)
357-
support.run_doctest(test_bisect, verbose)
358-
359-
# verify reference counting
360-
if verbose and hasattr(sys, "gettotalrefcount"):
361-
import gc
362-
counts = [None] * 5
363-
for i in range(len(counts)):
364-
support.run_unittest(*test_classes)
365-
gc.collect()
366-
counts[i] = sys.gettotalrefcount()
367-
print(counts)
368-
369327
if __name__ == "__main__":
370-
test_main(verbose=True)
328+
unittest.main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ Library
408408
Tests
409409
-----
410410

411+
- Issue #16897: test_bisect now works with unittest test discovery.
412+
Initial patch by Zachary Ware.
413+
411414
- Issue #16852: test_genericpath, test_posixpath, test_ntpath, and test_macpath
412415
now work with unittest test discovery. Patch by Zachary Ware.
413416

0 commit comments

Comments
 (0)