blob: 4f004d2213f34b3e84cd685027ca9444ba02513d [file] [log] [blame]
Georg Brandl0bb85672008-02-23 22:35:33 +00001import sys
Raymond Hettingerd2305502003-01-16 12:02:35 +00002import unittest
3from test import test_support
Raymond Hettinger0c410272004-01-05 10:13:35 +00004from UserList import UserList
Tim Peters36cdad12000-12-29 02:06:45 +00005
Georg Brandl0bb85672008-02-23 22:35:33 +00006# 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.
10sys.modules['_bisect'] = 0
11# We must also handle the case that bisect was imported before.
12if 'bisect' in sys.modules:
13 del sys.modules['bisect']
14
15# Now we can import the module and get the pure Python implementation.
16import bisect as py_bisect
17
18# Restore everything to normal.
19del sys.modules['_bisect']
20del sys.modules['bisect']
21
22# This is now the module with the C implementation.
23import bisect as c_bisect
24
25
Raymond Hettingerd2305502003-01-16 12:02:35 +000026class TestBisect(unittest.TestCase):
Georg Brandl0bb85672008-02-23 22:35:33 +000027 module = None
Tim Peters36cdad12000-12-29 02:06:45 +000028
Georg Brandl0bb85672008-02-23 22:35:33 +000029 def setUp(self):
30 self.precomputedCases = [
31 (self.module.bisect_right, [], 1, 0),
32 (self.module.bisect_right, [1], 0, 0),
33 (self.module.bisect_right, [1], 1, 1),
34 (self.module.bisect_right, [1], 2, 1),
35 (self.module.bisect_right, [1, 1], 0, 0),
36 (self.module.bisect_right, [1, 1], 1, 2),
37 (self.module.bisect_right, [1, 1], 2, 2),
38 (self.module.bisect_right, [1, 1, 1], 0, 0),
39 (self.module.bisect_right, [1, 1, 1], 1, 3),
40 (self.module.bisect_right, [1, 1, 1], 2, 3),
41 (self.module.bisect_right, [1, 1, 1, 1], 0, 0),
42 (self.module.bisect_right, [1, 1, 1, 1], 1, 4),
43 (self.module.bisect_right, [1, 1, 1, 1], 2, 4),
44 (self.module.bisect_right, [1, 2], 0, 0),
45 (self.module.bisect_right, [1, 2], 1, 1),
46 (self.module.bisect_right, [1, 2], 1.5, 1),
47 (self.module.bisect_right, [1, 2], 2, 2),
48 (self.module.bisect_right, [1, 2], 3, 2),
49 (self.module.bisect_right, [1, 1, 2, 2], 0, 0),
50 (self.module.bisect_right, [1, 1, 2, 2], 1, 2),
51 (self.module.bisect_right, [1, 1, 2, 2], 1.5, 2),
52 (self.module.bisect_right, [1, 1, 2, 2], 2, 4),
53 (self.module.bisect_right, [1, 1, 2, 2], 3, 4),
54 (self.module.bisect_right, [1, 2, 3], 0, 0),
55 (self.module.bisect_right, [1, 2, 3], 1, 1),
56 (self.module.bisect_right, [1, 2, 3], 1.5, 1),
57 (self.module.bisect_right, [1, 2, 3], 2, 2),
58 (self.module.bisect_right, [1, 2, 3], 2.5, 2),
59 (self.module.bisect_right, [1, 2, 3], 3, 3),
60 (self.module.bisect_right, [1, 2, 3], 4, 3),
61 (self.module.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 0, 0),
62 (self.module.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1, 1),
63 (self.module.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1.5, 1),
64 (self.module.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2, 3),
65 (self.module.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2.5, 3),
66 (self.module.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3, 6),
67 (self.module.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3.5, 6),
68 (self.module.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 4, 10),
69 (self.module.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 5, 10),
Tim Peters36cdad12000-12-29 02:06:45 +000070
Georg Brandl0bb85672008-02-23 22:35:33 +000071 (self.module.bisect_left, [], 1, 0),
72 (self.module.bisect_left, [1], 0, 0),
73 (self.module.bisect_left, [1], 1, 0),
74 (self.module.bisect_left, [1], 2, 1),
75 (self.module.bisect_left, [1, 1], 0, 0),
76 (self.module.bisect_left, [1, 1], 1, 0),
77 (self.module.bisect_left, [1, 1], 2, 2),
78 (self.module.bisect_left, [1, 1, 1], 0, 0),
79 (self.module.bisect_left, [1, 1, 1], 1, 0),
80 (self.module.bisect_left, [1, 1, 1], 2, 3),
81 (self.module.bisect_left, [1, 1, 1, 1], 0, 0),
82 (self.module.bisect_left, [1, 1, 1, 1], 1, 0),
83 (self.module.bisect_left, [1, 1, 1, 1], 2, 4),
84 (self.module.bisect_left, [1, 2], 0, 0),
85 (self.module.bisect_left, [1, 2], 1, 0),
86 (self.module.bisect_left, [1, 2], 1.5, 1),
87 (self.module.bisect_left, [1, 2], 2, 1),
88 (self.module.bisect_left, [1, 2], 3, 2),
89 (self.module.bisect_left, [1, 1, 2, 2], 0, 0),
90 (self.module.bisect_left, [1, 1, 2, 2], 1, 0),
91 (self.module.bisect_left, [1, 1, 2, 2], 1.5, 2),
92 (self.module.bisect_left, [1, 1, 2, 2], 2, 2),
93 (self.module.bisect_left, [1, 1, 2, 2], 3, 4),
94 (self.module.bisect_left, [1, 2, 3], 0, 0),
95 (self.module.bisect_left, [1, 2, 3], 1, 0),
96 (self.module.bisect_left, [1, 2, 3], 1.5, 1),
97 (self.module.bisect_left, [1, 2, 3], 2, 1),
98 (self.module.bisect_left, [1, 2, 3], 2.5, 2),
99 (self.module.bisect_left, [1, 2, 3], 3, 2),
100 (self.module.bisect_left, [1, 2, 3], 4, 3),
101 (self.module.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 0, 0),
102 (self.module.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1, 0),
103 (self.module.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1.5, 1),
104 (self.module.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2, 1),
105 (self.module.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2.5, 3),
106 (self.module.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3, 3),
107 (self.module.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3.5, 6),
108 (self.module.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 4, 6),
109 (self.module.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 5, 10)
110 ]
Tim Peters36cdad12000-12-29 02:06:45 +0000111
Raymond Hettingerd2305502003-01-16 12:02:35 +0000112 def test_precomputed(self):
Raymond Hettinger6aa1c3f2003-01-16 14:00:15 +0000113 for func, data, elem, expected in self.precomputedCases:
114 self.assertEqual(func(data, elem), expected)
Raymond Hettinger0c410272004-01-05 10:13:35 +0000115 self.assertEqual(func(UserList(data), elem), expected)
Raymond Hettingerd2305502003-01-16 12:02:35 +0000116
Raymond Hettinger3cd1e422008-07-10 14:03:19 +0000117 def test_negative_lo(self):
118 # Issue 3301
119 mod = self.module
120 self.assertRaises(ValueError, mod.bisect_left, [1, 2, 3], 5, -1, 3),
121 self.assertRaises(ValueError, mod.bisect_right, [1, 2, 3], 5, -1, 3),
122 self.assertRaises(ValueError, mod.insort_left, [1, 2, 3], 5, -1, 3),
123 self.assertRaises(ValueError, mod.insort_right, [1, 2, 3], 5, -1, 3),
124
Mark Dickinson0407e962012-04-15 16:43:19 +0100125 def test_large_range(self):
126 # Issue 13496
127 mod = self.module
128 data = xrange(sys.maxsize-1)
129 self.assertEqual(mod.bisect_left(data, sys.maxsize-3), sys.maxsize-3)
130 self.assertEqual(mod.bisect_right(data, sys.maxsize-3), sys.maxsize-2)
131
Raymond Hettinger6aa1c3f2003-01-16 14:00:15 +0000132 def test_random(self, n=25):
Raymond Hettinger44223752003-01-16 12:31:36 +0000133 from random import randrange
134 for i in xrange(n):
135 data = [randrange(0, n, 2) for j in xrange(i)]
136 data.sort()
Raymond Hettinger6aa1c3f2003-01-16 14:00:15 +0000137 elem = randrange(-1, n+1)
Georg Brandl0bb85672008-02-23 22:35:33 +0000138 ip = self.module.bisect_left(data, elem)
Raymond Hettinger44223752003-01-16 12:31:36 +0000139 if ip < len(data):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000140 self.assertTrue(elem <= data[ip])
Raymond Hettinger44223752003-01-16 12:31:36 +0000141 if ip > 0:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000142 self.assertTrue(data[ip-1] < elem)
Georg Brandl0bb85672008-02-23 22:35:33 +0000143 ip = self.module.bisect_right(data, elem)
Raymond Hettinger44223752003-01-16 12:31:36 +0000144 if ip < len(data):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000145 self.assertTrue(elem < data[ip])
Raymond Hettinger44223752003-01-16 12:31:36 +0000146 if ip > 0:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000147 self.assertTrue(data[ip-1] <= elem)
Raymond Hettinger44223752003-01-16 12:31:36 +0000148
Raymond Hettingera9f18dc2003-01-16 13:02:25 +0000149 def test_optionalSlicing(self):
Raymond Hettinger6aa1c3f2003-01-16 14:00:15 +0000150 for func, data, elem, expected in self.precomputedCases:
151 for lo in xrange(4):
152 lo = min(len(data), lo)
153 for hi in xrange(3,8):
154 hi = min(len(data), hi)
155 ip = func(data, elem, lo, hi)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000156 self.assertTrue(lo <= ip <= hi)
Georg Brandl0bb85672008-02-23 22:35:33 +0000157 if func is self.module.bisect_left and ip < hi:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000158 self.assertTrue(elem <= data[ip])
Georg Brandl0bb85672008-02-23 22:35:33 +0000159 if func is self.module.bisect_left and ip > lo:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000160 self.assertTrue(data[ip-1] < elem)
Georg Brandl0bb85672008-02-23 22:35:33 +0000161 if func is self.module.bisect_right and ip < hi:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000162 self.assertTrue(elem < data[ip])
Georg Brandl0bb85672008-02-23 22:35:33 +0000163 if func is self.module.bisect_right and ip > lo:
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000164 self.assertTrue(data[ip-1] <= elem)
Raymond Hettinger6aa1c3f2003-01-16 14:00:15 +0000165 self.assertEqual(ip, max(lo, min(hi, expected)))
Raymond Hettingera9f18dc2003-01-16 13:02:25 +0000166
167 def test_backcompatibility(self):
Georg Brandl0bb85672008-02-23 22:35:33 +0000168 self.assertEqual(self.module.bisect, self.module.bisect_right)
Raymond Hettingera9f18dc2003-01-16 13:02:25 +0000169
Raymond Hettingercc9a9512005-10-05 11:39:12 +0000170 def test_keyword_args(self):
171 data = [10, 20, 30, 40, 50]
Georg Brandl0bb85672008-02-23 22:35:33 +0000172 self.assertEqual(self.module.bisect_left(a=data, x=25, lo=1, hi=3), 2)
173 self.assertEqual(self.module.bisect_right(a=data, x=25, lo=1, hi=3), 2)
174 self.assertEqual(self.module.bisect(a=data, x=25, lo=1, hi=3), 2)
175 self.module.insort_left(a=data, x=25, lo=1, hi=3)
176 self.module.insort_right(a=data, x=25, lo=1, hi=3)
177 self.module.insort(a=data, x=25, lo=1, hi=3)
Raymond Hettingercc9a9512005-10-05 11:39:12 +0000178 self.assertEqual(data, [10, 20, 25, 25, 25, 30, 40, 50])
179
Georg Brandl0bb85672008-02-23 22:35:33 +0000180class TestBisectPython(TestBisect):
181 module = py_bisect
182
183class TestBisectC(TestBisect):
184 module = c_bisect
185
Raymond Hettingerd2305502003-01-16 12:02:35 +0000186#==============================================================================
187
188class TestInsort(unittest.TestCase):
Georg Brandl0bb85672008-02-23 22:35:33 +0000189 module = None
Raymond Hettingerd2305502003-01-16 12:02:35 +0000190
Raymond Hettinger0c410272004-01-05 10:13:35 +0000191 def test_vsBuiltinSort(self, n=500):
Raymond Hettingerd2305502003-01-16 12:02:35 +0000192 from random import choice
Raymond Hettinger0c410272004-01-05 10:13:35 +0000193 for insorted in (list(), UserList()):
194 for i in xrange(n):
195 digit = choice("0123456789")
196 if digit in "02468":
Georg Brandl0bb85672008-02-23 22:35:33 +0000197 f = self.module.insort_left
Raymond Hettinger0c410272004-01-05 10:13:35 +0000198 else:
Georg Brandl0bb85672008-02-23 22:35:33 +0000199 f = self.module.insort_right
Raymond Hettinger0c410272004-01-05 10:13:35 +0000200 f(insorted, digit)
201 self.assertEqual(sorted(insorted), insorted)
Raymond Hettingerd2305502003-01-16 12:02:35 +0000202
Raymond Hettingera9f18dc2003-01-16 13:02:25 +0000203 def test_backcompatibility(self):
Georg Brandl0bb85672008-02-23 22:35:33 +0000204 self.assertEqual(self.module.insort, self.module.insort_right)
205
Georg Brandlf3776a12008-10-08 18:47:17 +0000206 def test_listDerived(self):
207 class List(list):
208 data = []
209 def insert(self, index, item):
210 self.data.insert(index, item)
211
212 lst = List()
213 self.module.insort_left(lst, 10)
214 self.module.insort_right(lst, 5)
215 self.assertEqual([5, 10], lst.data)
216
Georg Brandl0bb85672008-02-23 22:35:33 +0000217class TestInsortPython(TestInsort):
218 module = py_bisect
219
220class TestInsortC(TestInsort):
221 module = c_bisect
Raymond Hettingera9f18dc2003-01-16 13:02:25 +0000222
Raymond Hettingerd2305502003-01-16 12:02:35 +0000223#==============================================================================
224
Raymond Hettinger63251782004-09-27 22:48:40 +0000225
226class LenOnly:
227 "Dummy sequence class defining __len__ but not __getitem__."
228 def __len__(self):
229 return 10
230
231class GetOnly:
232 "Dummy sequence class defining __getitem__ but not __len__."
233 def __getitem__(self, ndx):
234 return 10
235
236class CmpErr:
237 "Dummy element that always raises an error during comparison"
238 def __cmp__(self, other):
239 raise ZeroDivisionError
240
241class TestErrorHandling(unittest.TestCase):
Georg Brandl0bb85672008-02-23 22:35:33 +0000242 module = None
Raymond Hettinger63251782004-09-27 22:48:40 +0000243
244 def test_non_sequence(self):
Georg Brandl0bb85672008-02-23 22:35:33 +0000245 for f in (self.module.bisect_left, self.module.bisect_right,
246 self.module.insort_left, self.module.insort_right):
Raymond Hettinger63251782004-09-27 22:48:40 +0000247 self.assertRaises(TypeError, f, 10, 10)
248
249 def test_len_only(self):
Georg Brandl0bb85672008-02-23 22:35:33 +0000250 for f in (self.module.bisect_left, self.module.bisect_right,
251 self.module.insort_left, self.module.insort_right):
Raymond Hettinger63251782004-09-27 22:48:40 +0000252 self.assertRaises(AttributeError, f, LenOnly(), 10)
253
254 def test_get_only(self):
Georg Brandl0bb85672008-02-23 22:35:33 +0000255 for f in (self.module.bisect_left, self.module.bisect_right,
256 self.module.insort_left, self.module.insort_right):
Raymond Hettinger63251782004-09-27 22:48:40 +0000257 self.assertRaises(AttributeError, f, GetOnly(), 10)
258
Raymond Hettinger630e5352004-09-27 23:11:35 +0000259 def test_cmp_err(self):
Raymond Hettinger63251782004-09-27 22:48:40 +0000260 seq = [CmpErr(), CmpErr(), CmpErr()]
Georg Brandl0bb85672008-02-23 22:35:33 +0000261 for f in (self.module.bisect_left, self.module.bisect_right,
262 self.module.insort_left, self.module.insort_right):
Raymond Hettinger63251782004-09-27 22:48:40 +0000263 self.assertRaises(ZeroDivisionError, f, seq, 10)
264
265 def test_arg_parsing(self):
Georg Brandl0bb85672008-02-23 22:35:33 +0000266 for f in (self.module.bisect_left, self.module.bisect_right,
267 self.module.insort_left, self.module.insort_right):
Raymond Hettinger63251782004-09-27 22:48:40 +0000268 self.assertRaises(TypeError, f, 10)
269
Georg Brandl0bb85672008-02-23 22:35:33 +0000270class TestErrorHandlingPython(TestErrorHandling):
271 module = py_bisect
272
273class TestErrorHandlingC(TestErrorHandling):
274 module = c_bisect
275
Raymond Hettinger63251782004-09-27 22:48:40 +0000276#==============================================================================
277
Raymond Hettinger44223752003-01-16 12:31:36 +0000278libreftest = """
Georg Brandl0bb85672008-02-23 22:35:33 +0000279Example from the Library Reference: Doc/library/bisect.rst
Raymond Hettinger44223752003-01-16 12:31:36 +0000280
281The bisect() function is generally useful for categorizing numeric data.
282This example uses bisect() to look up a letter grade for an exam total
283(say) based on a set of ordered numeric breakpoints: 85 and up is an `A',
28475..84 is a `B', etc.
285
286 >>> grades = "FEDCBA"
287 >>> breakpoints = [30, 44, 66, 75, 85]
288 >>> from bisect import bisect
289 >>> def grade(total):
290 ... return grades[bisect(breakpoints, total)]
291 ...
292 >>> grade(66)
293 'C'
294 >>> map(grade, [33, 99, 77, 44, 12, 88])
295 ['E', 'A', 'B', 'D', 'F', 'A']
296
Raymond Hettinger44223752003-01-16 12:31:36 +0000297"""
298
Raymond Hettingerd2305502003-01-16 12:02:35 +0000299#------------------------------------------------------------------------------
300
Raymond Hettinger44223752003-01-16 12:31:36 +0000301__test__ = {'libreftest' : libreftest}
302
Raymond Hettingerd2305502003-01-16 12:02:35 +0000303def test_main(verbose=None):
304 from test import test_bisect
Raymond Hettinger63251782004-09-27 22:48:40 +0000305
Georg Brandl0bb85672008-02-23 22:35:33 +0000306 test_classes = [TestBisectPython, TestBisectC,
307 TestInsortPython, TestInsortC,
308 TestErrorHandlingPython, TestErrorHandlingC]
Raymond Hettinger63251782004-09-27 22:48:40 +0000309
310 test_support.run_unittest(*test_classes)
Raymond Hettinger44223752003-01-16 12:31:36 +0000311 test_support.run_doctest(test_bisect, verbose)
Raymond Hettingerd2305502003-01-16 12:02:35 +0000312
Raymond Hettinger63251782004-09-27 22:48:40 +0000313 # verify reference counting
314 if verbose and hasattr(sys, "gettotalrefcount"):
315 import gc
316 counts = [None] * 5
317 for i in xrange(len(counts)):
318 test_support.run_unittest(*test_classes)
319 gc.collect()
320 counts[i] = sys.gettotalrefcount()
321 print counts
322
Raymond Hettingerd2305502003-01-16 12:02:35 +0000323if __name__ == "__main__":
324 test_main(verbose=True)