| 
									
										
										
										
											2003-01-16 12:02:35 +00:00
										 |  |  | import unittest | 
					
						
							|  |  |  | from test import test_support | 
					
						
							| 
									
										
										
										
											2003-01-16 13:02:25 +00:00
										 |  |  | from bisect import bisect_right, bisect_left, insort_left, insort_right, insort, bisect | 
					
						
							| 
									
										
										
										
											2000-12-29 02:06:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-16 12:02:35 +00:00
										 |  |  | class TestBisect(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2000-12-29 02:06:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-16 12:02:35 +00: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 14:00:15 +00:00
										 |  |  |         for func, data, elem, expected in self.precomputedCases: | 
					
						
							|  |  |  |             self.assertEqual(func(data, elem), expected) | 
					
						
							| 
									
										
										
										
											2003-01-16 12:02:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-16 14:00:15 +00:00
										 |  |  |     def test_random(self, n=25): | 
					
						
							| 
									
										
										
										
											2003-01-16 12:31:36 +00:00
										 |  |  |         from random import randrange | 
					
						
							|  |  |  |         for i in xrange(n): | 
					
						
							|  |  |  |             data = [randrange(0, n, 2) for j in xrange(i)] | 
					
						
							|  |  |  |             data.sort() | 
					
						
							| 
									
										
										
										
											2003-01-16 14:00:15 +00:00
										 |  |  |             elem = randrange(-1, n+1) | 
					
						
							| 
									
										
										
										
											2003-01-16 12:31:36 +00: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 13:02:25 +00:00
										 |  |  |     def test_optionalSlicing(self): | 
					
						
							| 
									
										
										
										
											2003-01-16 14:00:15 +00:00
										 |  |  |         for func, data, elem, expected in self.precomputedCases: | 
					
						
							|  |  |  |             for lo in xrange(4): | 
					
						
							|  |  |  |                 lo = min(len(data), lo) | 
					
						
							|  |  |  |                 for hi in xrange(3,8): | 
					
						
							|  |  |  |                     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 13:02:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_backcompatibility(self): | 
					
						
							|  |  |  |         self.assertEqual(bisect, bisect_right) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-16 12:02:35 +00:00
										 |  |  | #============================================================================== | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TestInsort(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_vsListSort(self, n=500): | 
					
						
							|  |  |  |         from random import choice | 
					
						
							|  |  |  |         digits = "0123456789" | 
					
						
							|  |  |  |         raw = [] | 
					
						
							|  |  |  |         insorted = [] | 
					
						
							|  |  |  |         for i in range(n): | 
					
						
							|  |  |  |             digit = choice(digits) | 
					
						
							|  |  |  |             raw.append(digit) | 
					
						
							|  |  |  |             if digit in "02468": | 
					
						
							|  |  |  |                 f = insort_left | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 f = insort_right | 
					
						
							|  |  |  |             f(insorted, digit) | 
					
						
							|  |  |  |         sorted = raw[:] | 
					
						
							|  |  |  |         sorted.sort() | 
					
						
							|  |  |  |         self.assertEqual(sorted, insorted) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-16 13:02:25 +00:00
										 |  |  |     def test_backcompatibility(self): | 
					
						
							|  |  |  |         self.assertEqual(insort, insort_right) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-16 12:02:35 +00:00
										 |  |  | #============================================================================== | 
					
						
							| 
									
										
										
										
											2000-12-29 02:06:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-16 12:31:36 +00: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' | 
					
						
							|  |  |  |     >>> 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') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #============================================================================== | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-16 12:02:35 +00:00
										 |  |  | def makeAllTests(): | 
					
						
							|  |  |  |     suite = unittest.TestSuite() | 
					
						
							|  |  |  |     for klass in (TestBisect, | 
					
						
							|  |  |  |                   TestInsort | 
					
						
							|  |  |  |                   ): | 
					
						
							|  |  |  |         suite.addTest(unittest.makeSuite(klass)) | 
					
						
							|  |  |  |     return suite | 
					
						
							| 
									
										
										
										
											2000-12-29 02:06:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-16 12:02:35 +00:00
										 |  |  | #------------------------------------------------------------------------------ | 
					
						
							| 
									
										
										
										
											2000-12-29 02:06:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-16 12:31:36 +00:00
										 |  |  | __test__ = {'libreftest' : libreftest} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-16 12:02:35 +00:00
										 |  |  | def test_main(verbose=None): | 
					
						
							|  |  |  |     from test import test_bisect | 
					
						
							|  |  |  |     suite = makeAllTests() | 
					
						
							|  |  |  |     test_support.run_suite(suite) | 
					
						
							| 
									
										
										
										
											2003-01-16 12:31:36 +00:00
										 |  |  |     test_support.run_doctest(test_bisect, verbose) | 
					
						
							| 
									
										
										
										
											2000-12-29 02:06:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-01-16 12:02:35 +00:00
										 |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     test_main(verbose=True) | 
					
						
							| 
									
										
										
										
											2000-12-29 02:06:45 +00:00
										 |  |  | 
 |