Added doctest for examples in the library reference.
Added random test from bisect to augment the finite precomputed checks.
This commit is contained in:
parent
d230550a6b
commit
4422375c72
|
@ -93,6 +93,23 @@ class TestBisect(unittest.TestCase):
|
||||||
for func, list, elt, expected in self.precomputedCases:
|
for func, list, elt, expected in self.precomputedCases:
|
||||||
self.assertEqual(func(list, elt), expected)
|
self.assertEqual(func(list, elt), expected)
|
||||||
|
|
||||||
|
def test_random(self, n=20):
|
||||||
|
from random import randrange
|
||||||
|
for i in xrange(n):
|
||||||
|
data = [randrange(0, n, 2) for j in xrange(i)]
|
||||||
|
data.sort()
|
||||||
|
elem = randrange(n)
|
||||||
|
ip = bisect_left(data, elem)
|
||||||
|
if ip < len(data):
|
||||||
|
self.failUnless(elem <= data[ip])
|
||||||
|
if ip > 0:
|
||||||
|
self.failUnless(data[ip-1] < elem)
|
||||||
|
ip = bisect_right(data, elem)
|
||||||
|
if ip < len(data):
|
||||||
|
self.failUnless(elem < data[ip])
|
||||||
|
if ip > 0:
|
||||||
|
self.failUnless(data[ip-1] <= elem)
|
||||||
|
|
||||||
#==============================================================================
|
#==============================================================================
|
||||||
|
|
||||||
class TestInsort(unittest.TestCase):
|
class TestInsort(unittest.TestCase):
|
||||||
|
@ -116,6 +133,46 @@ class TestInsort(unittest.TestCase):
|
||||||
|
|
||||||
#==============================================================================
|
#==============================================================================
|
||||||
|
|
||||||
|
libreftest = """
|
||||||
|
Example from the Library Reference: Doc/lib/libbisect.tex
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
>>> grades = "FEDCBA"
|
||||||
|
>>> breakpoints = [30, 44, 66, 75, 85]
|
||||||
|
>>> from bisect import bisect
|
||||||
|
>>> def grade(total):
|
||||||
|
... return grades[bisect(breakpoints, total)]
|
||||||
|
...
|
||||||
|
>>> grade(66)
|
||||||
|
'C'
|
||||||
|
>>> map(grade, [33, 99, 77, 44, 12, 88])
|
||||||
|
['E', 'A', 'B', 'D', 'F', 'A']
|
||||||
|
|
||||||
|
The bisect module can be used with the Queue module to implement
|
||||||
|
a priority queue (example courtesy of Fredrik Lundh):
|
||||||
|
|
||||||
|
>>> import Queue, bisect
|
||||||
|
>>> class PriorityQueue(Queue.Queue):
|
||||||
|
... def _put(self, item):
|
||||||
|
... bisect.insort(self.queue, item)
|
||||||
|
...
|
||||||
|
>>> queue = PriorityQueue(0)
|
||||||
|
>>> queue.put((2, "second"))
|
||||||
|
>>> queue.put((1, "first"))
|
||||||
|
>>> queue.put((3, "third"))
|
||||||
|
>>> queue.get()
|
||||||
|
(1, 'first')
|
||||||
|
>>> queue.get()
|
||||||
|
(2, 'second')
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
#==============================================================================
|
||||||
|
|
||||||
def makeAllTests():
|
def makeAllTests():
|
||||||
suite = unittest.TestSuite()
|
suite = unittest.TestSuite()
|
||||||
for klass in (TestBisect,
|
for klass in (TestBisect,
|
||||||
|
@ -126,10 +183,13 @@ def makeAllTests():
|
||||||
|
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
__test__ = {'libreftest' : libreftest}
|
||||||
|
|
||||||
def test_main(verbose=None):
|
def test_main(verbose=None):
|
||||||
from test import test_bisect
|
from test import test_bisect
|
||||||
suite = makeAllTests()
|
suite = makeAllTests()
|
||||||
test_support.run_suite(suite)
|
test_support.run_suite(suite)
|
||||||
|
test_support.run_doctest(test_bisect, verbose)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
test_main(verbose=True)
|
test_main(verbose=True)
|
||||||
|
|
Loading…
Reference in New Issue