2003-01-16 08:02:35 -04:00
|
|
|
import unittest
|
|
|
|
from test import test_support
|
2003-01-16 09:02:25 -04:00
|
|
|
from bisect import bisect_right, bisect_left, insort_left, insort_right, insort, bisect
|
2004-01-05 06:13:35 -04:00
|
|
|
from UserList import UserList
|
2000-12-28 22:06:45 -04:00
|
|
|
|
2003-01-16 08:02:35 -04:00
|
|
|
class TestBisect(unittest.TestCase):
|
2000-12-28 22:06:45 -04:00
|
|
|
|
2003-01-16 08:02:35 -04:00
|
|
|
precomputedCases = [
|
|
|
|
(bisect_right, [], 1, 0),
|
|
|
|
(bisect_right, [1], 0, 0),
|
|
|
|
(bisect_right, [1], 1, 1),
|
|
|
|
(bisect_right, [1], 2, 1),
|
|
|
|
(bisect_right, [1, 1], 0, 0),
|
|
|
|
(bisect_right, [1, 1], 1, 2),
|
|
|
|
(bisect_right, [1, 1], 2, 2),
|
|
|
|
(bisect_right, [1, 1, 1], 0, 0),
|
|
|
|
(bisect_right, [1, 1, 1], 1, 3),
|
|
|
|
(bisect_right, [1, 1, 1], 2, 3),
|
|
|
|
(bisect_right, [1, 1, 1, 1], 0, 0),
|
|
|
|
(bisect_right, [1, 1, 1, 1], 1, 4),
|
|
|
|
(bisect_right, [1, 1, 1, 1], 2, 4),
|
|
|
|
(bisect_right, [1, 2], 0, 0),
|
|
|
|
(bisect_right, [1, 2], 1, 1),
|
|
|
|
(bisect_right, [1, 2], 1.5, 1),
|
|
|
|
(bisect_right, [1, 2], 2, 2),
|
|
|
|
(bisect_right, [1, 2], 3, 2),
|
|
|
|
(bisect_right, [1, 1, 2, 2], 0, 0),
|
|
|
|
(bisect_right, [1, 1, 2, 2], 1, 2),
|
|
|
|
(bisect_right, [1, 1, 2, 2], 1.5, 2),
|
|
|
|
(bisect_right, [1, 1, 2, 2], 2, 4),
|
|
|
|
(bisect_right, [1, 1, 2, 2], 3, 4),
|
|
|
|
(bisect_right, [1, 2, 3], 0, 0),
|
|
|
|
(bisect_right, [1, 2, 3], 1, 1),
|
|
|
|
(bisect_right, [1, 2, 3], 1.5, 1),
|
|
|
|
(bisect_right, [1, 2, 3], 2, 2),
|
|
|
|
(bisect_right, [1, 2, 3], 2.5, 2),
|
|
|
|
(bisect_right, [1, 2, 3], 3, 3),
|
|
|
|
(bisect_right, [1, 2, 3], 4, 3),
|
|
|
|
(bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 0, 0),
|
|
|
|
(bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1, 1),
|
|
|
|
(bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1.5, 1),
|
|
|
|
(bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2, 3),
|
|
|
|
(bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2.5, 3),
|
|
|
|
(bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3, 6),
|
|
|
|
(bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3.5, 6),
|
|
|
|
(bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 4, 10),
|
|
|
|
(bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 5, 10),
|
|
|
|
|
|
|
|
(bisect_left, [], 1, 0),
|
|
|
|
(bisect_left, [1], 0, 0),
|
|
|
|
(bisect_left, [1], 1, 0),
|
|
|
|
(bisect_left, [1], 2, 1),
|
|
|
|
(bisect_left, [1, 1], 0, 0),
|
|
|
|
(bisect_left, [1, 1], 1, 0),
|
|
|
|
(bisect_left, [1, 1], 2, 2),
|
|
|
|
(bisect_left, [1, 1, 1], 0, 0),
|
|
|
|
(bisect_left, [1, 1, 1], 1, 0),
|
|
|
|
(bisect_left, [1, 1, 1], 2, 3),
|
|
|
|
(bisect_left, [1, 1, 1, 1], 0, 0),
|
|
|
|
(bisect_left, [1, 1, 1, 1], 1, 0),
|
|
|
|
(bisect_left, [1, 1, 1, 1], 2, 4),
|
|
|
|
(bisect_left, [1, 2], 0, 0),
|
|
|
|
(bisect_left, [1, 2], 1, 0),
|
|
|
|
(bisect_left, [1, 2], 1.5, 1),
|
|
|
|
(bisect_left, [1, 2], 2, 1),
|
|
|
|
(bisect_left, [1, 2], 3, 2),
|
|
|
|
(bisect_left, [1, 1, 2, 2], 0, 0),
|
|
|
|
(bisect_left, [1, 1, 2, 2], 1, 0),
|
|
|
|
(bisect_left, [1, 1, 2, 2], 1.5, 2),
|
|
|
|
(bisect_left, [1, 1, 2, 2], 2, 2),
|
|
|
|
(bisect_left, [1, 1, 2, 2], 3, 4),
|
|
|
|
(bisect_left, [1, 2, 3], 0, 0),
|
|
|
|
(bisect_left, [1, 2, 3], 1, 0),
|
|
|
|
(bisect_left, [1, 2, 3], 1.5, 1),
|
|
|
|
(bisect_left, [1, 2, 3], 2, 1),
|
|
|
|
(bisect_left, [1, 2, 3], 2.5, 2),
|
|
|
|
(bisect_left, [1, 2, 3], 3, 2),
|
|
|
|
(bisect_left, [1, 2, 3], 4, 3),
|
|
|
|
(bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 0, 0),
|
|
|
|
(bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1, 0),
|
|
|
|
(bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1.5, 1),
|
|
|
|
(bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2, 1),
|
|
|
|
(bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2.5, 3),
|
|
|
|
(bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3, 3),
|
|
|
|
(bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3.5, 6),
|
|
|
|
(bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 4, 6),
|
|
|
|
(bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 5, 10)
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_precomputed(self):
|
2003-01-16 10:00:15 -04:00
|
|
|
for func, data, elem, expected in self.precomputedCases:
|
|
|
|
self.assertEqual(func(data, elem), expected)
|
2004-01-05 06:13:35 -04:00
|
|
|
self.assertEqual(func(UserList(data), elem), expected)
|
2003-01-16 08:02:35 -04:00
|
|
|
|
2003-01-16 10:00:15 -04:00
|
|
|
def test_random(self, n=25):
|
2003-01-16 08:31:36 -04:00
|
|
|
from random import randrange
|
2007-05-07 19:24:25 -03:00
|
|
|
for i in range(n):
|
|
|
|
data = [randrange(0, n, 2) for j in range(i)]
|
2003-01-16 08:31:36 -04:00
|
|
|
data.sort()
|
2003-01-16 10:00:15 -04:00
|
|
|
elem = randrange(-1, n+1)
|
2003-01-16 08:31:36 -04:00
|
|
|
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)
|
|
|
|
|
2003-01-16 09:02:25 -04:00
|
|
|
def test_optionalSlicing(self):
|
2003-01-16 10:00:15 -04:00
|
|
|
for func, data, elem, expected in self.precomputedCases:
|
2007-05-07 19:24:25 -03:00
|
|
|
for lo in range(4):
|
2003-01-16 10:00:15 -04:00
|
|
|
lo = min(len(data), lo)
|
2007-05-07 19:24:25 -03:00
|
|
|
for hi in range(3,8):
|
2003-01-16 10:00:15 -04:00
|
|
|
hi = min(len(data), hi)
|
|
|
|
ip = func(data, elem, lo, hi)
|
|
|
|
self.failUnless(lo <= ip <= hi)
|
|
|
|
if func is bisect_left and ip < hi:
|
|
|
|
self.failUnless(elem <= data[ip])
|
|
|
|
if func is bisect_left and ip > lo:
|
|
|
|
self.failUnless(data[ip-1] < elem)
|
|
|
|
if func is bisect_right and ip < hi:
|
|
|
|
self.failUnless(elem < data[ip])
|
|
|
|
if func is bisect_right and ip > lo:
|
|
|
|
self.failUnless(data[ip-1] <= elem)
|
|
|
|
self.assertEqual(ip, max(lo, min(hi, expected)))
|
2003-01-16 09:02:25 -04:00
|
|
|
|
|
|
|
def test_backcompatibility(self):
|
|
|
|
self.assertEqual(bisect, bisect_right)
|
|
|
|
|
2005-10-05 08:39:12 -03:00
|
|
|
def test_keyword_args(self):
|
|
|
|
data = [10, 20, 30, 40, 50]
|
|
|
|
self.assertEqual(bisect_left(a=data, x=25, lo=1, hi=3), 2)
|
|
|
|
self.assertEqual(bisect_right(a=data, x=25, lo=1, hi=3), 2)
|
|
|
|
self.assertEqual(bisect(a=data, x=25, lo=1, hi=3), 2)
|
|
|
|
insort_left(a=data, x=25, lo=1, hi=3)
|
|
|
|
insort_right(a=data, x=25, lo=1, hi=3)
|
|
|
|
insort(a=data, x=25, lo=1, hi=3)
|
|
|
|
self.assertEqual(data, [10, 20, 25, 25, 25, 30, 40, 50])
|
|
|
|
|
2003-01-16 08:02:35 -04:00
|
|
|
#==============================================================================
|
|
|
|
|
|
|
|
class TestInsort(unittest.TestCase):
|
|
|
|
|
2004-01-05 06:13:35 -04:00
|
|
|
def test_vsBuiltinSort(self, n=500):
|
2003-01-16 08:02:35 -04:00
|
|
|
from random import choice
|
2004-01-05 06:13:35 -04:00
|
|
|
for insorted in (list(), UserList()):
|
2007-05-07 19:24:25 -03:00
|
|
|
for i in range(n):
|
2004-01-05 06:13:35 -04:00
|
|
|
digit = choice("0123456789")
|
|
|
|
if digit in "02468":
|
|
|
|
f = insort_left
|
|
|
|
else:
|
|
|
|
f = insort_right
|
|
|
|
f(insorted, digit)
|
|
|
|
self.assertEqual(sorted(insorted), insorted)
|
2003-01-16 08:02:35 -04:00
|
|
|
|
2003-01-16 09:02:25 -04:00
|
|
|
def test_backcompatibility(self):
|
|
|
|
self.assertEqual(insort, insort_right)
|
|
|
|
|
2003-01-16 08:02:35 -04:00
|
|
|
#==============================================================================
|
2000-12-28 22:06:45 -04:00
|
|
|
|
2004-09-27 19:48:40 -03:00
|
|
|
|
|
|
|
class LenOnly:
|
|
|
|
"Dummy sequence class defining __len__ but not __getitem__."
|
|
|
|
def __len__(self):
|
|
|
|
return 10
|
|
|
|
|
|
|
|
class GetOnly:
|
|
|
|
"Dummy sequence class defining __getitem__ but not __len__."
|
|
|
|
def __getitem__(self, ndx):
|
|
|
|
return 10
|
|
|
|
|
|
|
|
class CmpErr:
|
|
|
|
"Dummy element that always raises an error during comparison"
|
2006-08-23 21:41:19 -03:00
|
|
|
def __lt__(self, other):
|
2004-09-27 19:48:40 -03:00
|
|
|
raise ZeroDivisionError
|
2006-08-23 21:41:19 -03:00
|
|
|
__gt__ = __lt__
|
|
|
|
__le__ = __lt__
|
|
|
|
__ge__ = __lt__
|
|
|
|
__eq__ = __lt__
|
|
|
|
__ne__ = __lt__
|
2004-09-27 19:48:40 -03:00
|
|
|
|
|
|
|
class TestErrorHandling(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_non_sequence(self):
|
|
|
|
for f in (bisect_left, bisect_right, insort_left, insort_right):
|
|
|
|
self.assertRaises(TypeError, f, 10, 10)
|
|
|
|
|
|
|
|
def test_len_only(self):
|
|
|
|
for f in (bisect_left, bisect_right, insort_left, insort_right):
|
2006-04-15 06:12:14 -03:00
|
|
|
self.assertRaises(TypeError, f, LenOnly(), 10)
|
2004-09-27 19:48:40 -03:00
|
|
|
|
|
|
|
def test_get_only(self):
|
|
|
|
for f in (bisect_left, bisect_right, insort_left, insort_right):
|
2006-04-15 06:12:14 -03:00
|
|
|
self.assertRaises(TypeError, f, GetOnly(), 10)
|
2004-09-27 19:48:40 -03:00
|
|
|
|
2004-09-27 20:11:35 -03:00
|
|
|
def test_cmp_err(self):
|
2004-09-27 19:48:40 -03:00
|
|
|
seq = [CmpErr(), CmpErr(), CmpErr()]
|
|
|
|
for f in (bisect_left, bisect_right, insort_left, insort_right):
|
|
|
|
self.assertRaises(ZeroDivisionError, f, seq, 10)
|
|
|
|
|
|
|
|
def test_arg_parsing(self):
|
|
|
|
for f in (bisect_left, bisect_right, insort_left, insort_right):
|
|
|
|
self.assertRaises(TypeError, f, 10)
|
|
|
|
|
|
|
|
#==============================================================================
|
|
|
|
|
2003-01-16 08:31:36 -04:00
|
|
|
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'
|
Merged revisions 56125-56153 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
........
r56127 | georg.brandl | 2007-06-30 09:32:49 +0200 (Sat, 30 Jun 2007) | 2 lines
Fix a place where floor division would be in order.
........
r56135 | guido.van.rossum | 2007-07-01 06:13:54 +0200 (Sun, 01 Jul 2007) | 28 lines
Make map() and filter() identical to itertools.imap() and .ifilter(),
respectively.
I fixed two bootstrap issues, due to the dynamic import of itertools:
1. Starting python requires that map() and filter() are not used until
site.py has added build/lib.<arch> to sys.path.
2. Building python requires that setup.py and distutils and everything
they use is free of map() and filter() calls.
Beyond this, I only fixed the tests in test_builtin.py.
Others, please help fixing the remaining tests that are now broken!
The fixes are usually simple:
a. map(None, X) -> list(X)
b. map(F, X) -> list(map(F, X))
c. map(lambda x: F(x), X) -> [F(x) for x in X]
d. filter(F, X) -> list(filter(F, X))
e. filter(lambda x: P(x), X) -> [x for x in X if P(x)]
Someone, please also contribute a fixer for 2to3 to do this.
It can leave map()/filter() calls alone that are already
inside a list() or sorted() call or for-loop.
Only in rare cases have I seen code that depends on map() of lists
of different lengths going to the end of the longest, or on filter()
of a string or tuple returning an object of the same type; these
will need more thought to fix.
........
r56136 | guido.van.rossum | 2007-07-01 06:22:01 +0200 (Sun, 01 Jul 2007) | 3 lines
Make it so that test_decimal fails instead of hangs, to help automated
test runners.
........
r56139 | georg.brandl | 2007-07-01 18:20:58 +0200 (Sun, 01 Jul 2007) | 2 lines
Fix a few test cases after the map->imap change.
........
r56142 | neal.norwitz | 2007-07-02 06:38:12 +0200 (Mon, 02 Jul 2007) | 1 line
Get a bunch more tests passing after converting map/filter to return iterators.
........
r56147 | guido.van.rossum | 2007-07-02 15:32:02 +0200 (Mon, 02 Jul 2007) | 4 lines
Fix the remaining failing unit tests (at least on OSX).
Also tweaked urllib2 so it doesn't raise socket.gaierror when
all network interfaces are turned off.
........
2007-07-03 05:25:58 -03:00
|
|
|
>>> list(map(grade, [33, 99, 77, 44, 12, 88]))
|
2003-01-16 08:31:36 -04:00
|
|
|
['E', 'A', 'B', 'D', 'F', 'A']
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2003-01-16 08:02:35 -04:00
|
|
|
#------------------------------------------------------------------------------
|
2000-12-28 22:06:45 -04:00
|
|
|
|
2003-01-16 08:31:36 -04:00
|
|
|
__test__ = {'libreftest' : libreftest}
|
|
|
|
|
2003-01-16 08:02:35 -04:00
|
|
|
def test_main(verbose=None):
|
|
|
|
from test import test_bisect
|
2004-09-27 19:48:40 -03:00
|
|
|
from types import BuiltinFunctionType
|
|
|
|
import sys
|
|
|
|
|
|
|
|
test_classes = [TestBisect, TestInsort]
|
|
|
|
if isinstance(bisect_left, BuiltinFunctionType):
|
|
|
|
test_classes.append(TestErrorHandling)
|
|
|
|
|
|
|
|
test_support.run_unittest(*test_classes)
|
2003-01-16 08:31:36 -04:00
|
|
|
test_support.run_doctest(test_bisect, verbose)
|
2000-12-28 22:06:45 -04:00
|
|
|
|
2004-09-27 19:48:40 -03:00
|
|
|
# verify reference counting
|
|
|
|
if verbose and hasattr(sys, "gettotalrefcount"):
|
|
|
|
import gc
|
|
|
|
counts = [None] * 5
|
2007-05-07 19:24:25 -03:00
|
|
|
for i in range(len(counts)):
|
2004-09-27 19:48:40 -03:00
|
|
|
test_support.run_unittest(*test_classes)
|
|
|
|
gc.collect()
|
|
|
|
counts[i] = sys.gettotalrefcount()
|
2007-02-09 01:37:30 -04:00
|
|
|
print(counts)
|
2004-09-27 19:48:40 -03:00
|
|
|
|
2003-01-16 08:02:35 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main(verbose=True)
|