Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 1 | import sys |
Raymond Hettinger | d230550 | 2003-01-16 12:02:35 +0000 | [diff] [blame] | 2 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support |
Raymond Hettinger | 53dbe39 | 2008-02-12 20:03:09 +0000 | [diff] [blame] | 4 | from collections import UserList |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 5 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 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 | |
| 25 | |
Raymond Hettinger | d230550 | 2003-01-16 12:02:35 +0000 | [diff] [blame] | 26 | class TestBisect(unittest.TestCase): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 27 | module = None |
Tim Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 28 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 29 | 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 Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 70 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 71 | (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 Peters | 36cdad1 | 2000-12-29 02:06:45 +0000 | [diff] [blame] | 111 | |
Raymond Hettinger | d230550 | 2003-01-16 12:02:35 +0000 | [diff] [blame] | 112 | def test_precomputed(self): |
Raymond Hettinger | 6aa1c3f | 2003-01-16 14:00:15 +0000 | [diff] [blame] | 113 | for func, data, elem, expected in self.precomputedCases: |
| 114 | self.assertEqual(func(data, elem), expected) |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 115 | self.assertEqual(func(UserList(data), elem), expected) |
Raymond Hettinger | d230550 | 2003-01-16 12:02:35 +0000 | [diff] [blame] | 116 | |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 117 | 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 | |
Raymond Hettinger | 6aa1c3f | 2003-01-16 14:00:15 +0000 | [diff] [blame] | 125 | def test_random(self, n=25): |
Raymond Hettinger | 4422375 | 2003-01-16 12:31:36 +0000 | [diff] [blame] | 126 | from random import randrange |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 127 | for i in range(n): |
| 128 | data = [randrange(0, n, 2) for j in range(i)] |
Raymond Hettinger | 4422375 | 2003-01-16 12:31:36 +0000 | [diff] [blame] | 129 | data.sort() |
Raymond Hettinger | 6aa1c3f | 2003-01-16 14:00:15 +0000 | [diff] [blame] | 130 | elem = randrange(-1, n+1) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 131 | ip = self.module.bisect_left(data, elem) |
Raymond Hettinger | 4422375 | 2003-01-16 12:31:36 +0000 | [diff] [blame] | 132 | if ip < len(data): |
| 133 | self.failUnless(elem <= data[ip]) |
| 134 | if ip > 0: |
| 135 | self.failUnless(data[ip-1] < elem) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 136 | ip = self.module.bisect_right(data, elem) |
Raymond Hettinger | 4422375 | 2003-01-16 12:31:36 +0000 | [diff] [blame] | 137 | if ip < len(data): |
| 138 | self.failUnless(elem < data[ip]) |
| 139 | if ip > 0: |
| 140 | self.failUnless(data[ip-1] <= elem) |
| 141 | |
Raymond Hettinger | a9f18dc | 2003-01-16 13:02:25 +0000 | [diff] [blame] | 142 | def test_optionalSlicing(self): |
Raymond Hettinger | 6aa1c3f | 2003-01-16 14:00:15 +0000 | [diff] [blame] | 143 | for func, data, elem, expected in self.precomputedCases: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 144 | for lo in range(4): |
Raymond Hettinger | 6aa1c3f | 2003-01-16 14:00:15 +0000 | [diff] [blame] | 145 | lo = min(len(data), lo) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 146 | for hi in range(3,8): |
Raymond Hettinger | 6aa1c3f | 2003-01-16 14:00:15 +0000 | [diff] [blame] | 147 | hi = min(len(data), hi) |
| 148 | ip = func(data, elem, lo, hi) |
| 149 | self.failUnless(lo <= ip <= hi) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 150 | if func is self.module.bisect_left and ip < hi: |
Raymond Hettinger | 6aa1c3f | 2003-01-16 14:00:15 +0000 | [diff] [blame] | 151 | self.failUnless(elem <= data[ip]) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 152 | if func is self.module.bisect_left and ip > lo: |
Raymond Hettinger | 6aa1c3f | 2003-01-16 14:00:15 +0000 | [diff] [blame] | 153 | self.failUnless(data[ip-1] < elem) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 154 | if func is self.module.bisect_right and ip < hi: |
Raymond Hettinger | 6aa1c3f | 2003-01-16 14:00:15 +0000 | [diff] [blame] | 155 | self.failUnless(elem < data[ip]) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 156 | if func is self.module.bisect_right and ip > lo: |
Raymond Hettinger | 6aa1c3f | 2003-01-16 14:00:15 +0000 | [diff] [blame] | 157 | self.failUnless(data[ip-1] <= elem) |
| 158 | self.assertEqual(ip, max(lo, min(hi, expected))) |
Raymond Hettinger | a9f18dc | 2003-01-16 13:02:25 +0000 | [diff] [blame] | 159 | |
| 160 | def test_backcompatibility(self): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 161 | self.assertEqual(self.module.bisect, self.module.bisect_right) |
Raymond Hettinger | a9f18dc | 2003-01-16 13:02:25 +0000 | [diff] [blame] | 162 | |
Raymond Hettinger | cc9a951 | 2005-10-05 11:39:12 +0000 | [diff] [blame] | 163 | def test_keyword_args(self): |
| 164 | data = [10, 20, 30, 40, 50] |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 165 | self.assertEqual(self.module.bisect_left(a=data, x=25, lo=1, hi=3), 2) |
| 166 | self.assertEqual(self.module.bisect_right(a=data, x=25, lo=1, hi=3), 2) |
| 167 | self.assertEqual(self.module.bisect(a=data, x=25, lo=1, hi=3), 2) |
| 168 | self.module.insort_left(a=data, x=25, lo=1, hi=3) |
| 169 | self.module.insort_right(a=data, x=25, lo=1, hi=3) |
| 170 | self.module.insort(a=data, x=25, lo=1, hi=3) |
Raymond Hettinger | cc9a951 | 2005-10-05 11:39:12 +0000 | [diff] [blame] | 171 | self.assertEqual(data, [10, 20, 25, 25, 25, 30, 40, 50]) |
| 172 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 173 | class TestBisectPython(TestBisect): |
| 174 | module = py_bisect |
| 175 | |
| 176 | class TestBisectC(TestBisect): |
| 177 | module = c_bisect |
| 178 | |
Raymond Hettinger | d230550 | 2003-01-16 12:02:35 +0000 | [diff] [blame] | 179 | #============================================================================== |
| 180 | |
| 181 | class TestInsort(unittest.TestCase): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 182 | module = None |
Raymond Hettinger | d230550 | 2003-01-16 12:02:35 +0000 | [diff] [blame] | 183 | |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 184 | def test_vsBuiltinSort(self, n=500): |
Raymond Hettinger | d230550 | 2003-01-16 12:02:35 +0000 | [diff] [blame] | 185 | from random import choice |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 186 | for insorted in (list(), UserList()): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 187 | for i in range(n): |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 188 | digit = choice("0123456789") |
| 189 | if digit in "02468": |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 190 | f = self.module.insort_left |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 191 | else: |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 192 | f = self.module.insort_right |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 193 | f(insorted, digit) |
| 194 | self.assertEqual(sorted(insorted), insorted) |
Raymond Hettinger | d230550 | 2003-01-16 12:02:35 +0000 | [diff] [blame] | 195 | |
Raymond Hettinger | a9f18dc | 2003-01-16 13:02:25 +0000 | [diff] [blame] | 196 | def test_backcompatibility(self): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 197 | self.assertEqual(self.module.insort, self.module.insort_right) |
| 198 | |
| 199 | class TestInsortPython(TestInsort): |
| 200 | module = py_bisect |
| 201 | |
| 202 | class TestInsortC(TestInsort): |
| 203 | module = c_bisect |
Raymond Hettinger | a9f18dc | 2003-01-16 13:02:25 +0000 | [diff] [blame] | 204 | |
Raymond Hettinger | d230550 | 2003-01-16 12:02:35 +0000 | [diff] [blame] | 205 | #============================================================================== |
| 206 | |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 207 | |
| 208 | class LenOnly: |
| 209 | "Dummy sequence class defining __len__ but not __getitem__." |
| 210 | def __len__(self): |
| 211 | return 10 |
| 212 | |
| 213 | class GetOnly: |
| 214 | "Dummy sequence class defining __getitem__ but not __len__." |
| 215 | def __getitem__(self, ndx): |
| 216 | return 10 |
| 217 | |
| 218 | class CmpErr: |
| 219 | "Dummy element that always raises an error during comparison" |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 220 | def __lt__(self, other): |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 221 | raise ZeroDivisionError |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 222 | __gt__ = __lt__ |
| 223 | __le__ = __lt__ |
| 224 | __ge__ = __lt__ |
| 225 | __eq__ = __lt__ |
| 226 | __ne__ = __lt__ |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 227 | |
| 228 | class TestErrorHandling(unittest.TestCase): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 229 | module = None |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 230 | |
| 231 | def test_non_sequence(self): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 232 | for f in (self.module.bisect_left, self.module.bisect_right, |
| 233 | self.module.insort_left, self.module.insort_right): |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 234 | self.assertRaises(TypeError, f, 10, 10) |
| 235 | |
| 236 | def test_len_only(self): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 237 | for f in (self.module.bisect_left, self.module.bisect_right, |
| 238 | self.module.insort_left, self.module.insort_right): |
Thomas Wouters | 1ae9afa | 2006-04-15 09:12:14 +0000 | [diff] [blame] | 239 | self.assertRaises(TypeError, f, LenOnly(), 10) |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 240 | |
| 241 | def test_get_only(self): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 242 | for f in (self.module.bisect_left, self.module.bisect_right, |
| 243 | self.module.insort_left, self.module.insort_right): |
Thomas Wouters | 1ae9afa | 2006-04-15 09:12:14 +0000 | [diff] [blame] | 244 | self.assertRaises(TypeError, f, GetOnly(), 10) |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 245 | |
Raymond Hettinger | 630e535 | 2004-09-27 23:11:35 +0000 | [diff] [blame] | 246 | def test_cmp_err(self): |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 247 | seq = [CmpErr(), CmpErr(), CmpErr()] |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 248 | for f in (self.module.bisect_left, self.module.bisect_right, |
| 249 | self.module.insort_left, self.module.insort_right): |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 250 | self.assertRaises(ZeroDivisionError, f, seq, 10) |
| 251 | |
| 252 | def test_arg_parsing(self): |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 253 | for f in (self.module.bisect_left, self.module.bisect_right, |
| 254 | self.module.insort_left, self.module.insort_right): |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 255 | self.assertRaises(TypeError, f, 10) |
| 256 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 257 | class TestErrorHandlingPython(TestErrorHandling): |
| 258 | module = py_bisect |
| 259 | |
| 260 | class TestErrorHandlingC(TestErrorHandling): |
| 261 | module = c_bisect |
| 262 | |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 263 | #============================================================================== |
| 264 | |
Raymond Hettinger | 4422375 | 2003-01-16 12:31:36 +0000 | [diff] [blame] | 265 | libreftest = """ |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 266 | Example from the Library Reference: Doc/library/bisect.rst |
Raymond Hettinger | 4422375 | 2003-01-16 12:31:36 +0000 | [diff] [blame] | 267 | |
| 268 | The bisect() function is generally useful for categorizing numeric data. |
| 269 | This example uses bisect() to look up a letter grade for an exam total |
| 270 | (say) based on a set of ordered numeric breakpoints: 85 and up is an `A', |
| 271 | 75..84 is a `B', etc. |
| 272 | |
| 273 | >>> grades = "FEDCBA" |
| 274 | >>> breakpoints = [30, 44, 66, 75, 85] |
| 275 | >>> from bisect import bisect |
| 276 | >>> def grade(total): |
| 277 | ... return grades[bisect(breakpoints, total)] |
| 278 | ... |
| 279 | >>> grade(66) |
| 280 | 'C' |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 281 | >>> list(map(grade, [33, 99, 77, 44, 12, 88])) |
Raymond Hettinger | 4422375 | 2003-01-16 12:31:36 +0000 | [diff] [blame] | 282 | ['E', 'A', 'B', 'D', 'F', 'A'] |
| 283 | |
Raymond Hettinger | 4422375 | 2003-01-16 12:31:36 +0000 | [diff] [blame] | 284 | """ |
| 285 | |
Raymond Hettinger | d230550 | 2003-01-16 12:02:35 +0000 | [diff] [blame] | 286 | #------------------------------------------------------------------------------ |
| 287 | |
Raymond Hettinger | 4422375 | 2003-01-16 12:31:36 +0000 | [diff] [blame] | 288 | __test__ = {'libreftest' : libreftest} |
| 289 | |
Raymond Hettinger | d230550 | 2003-01-16 12:02:35 +0000 | [diff] [blame] | 290 | def test_main(verbose=None): |
| 291 | from test import test_bisect |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 292 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 293 | test_classes = [TestBisectPython, TestBisectC, |
| 294 | TestInsortPython, TestInsortC, |
| 295 | TestErrorHandlingPython, TestErrorHandlingC] |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 296 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 297 | support.run_unittest(*test_classes) |
| 298 | support.run_doctest(test_bisect, verbose) |
Raymond Hettinger | d230550 | 2003-01-16 12:02:35 +0000 | [diff] [blame] | 299 | |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 300 | # verify reference counting |
| 301 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 302 | import gc |
| 303 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 304 | for i in range(len(counts)): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 305 | support.run_unittest(*test_classes) |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 306 | gc.collect() |
| 307 | counts[i] = sys.gettotalrefcount() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 308 | print(counts) |
Raymond Hettinger | 6325178 | 2004-09-27 22:48:40 +0000 | [diff] [blame] | 309 | |
Raymond Hettinger | d230550 | 2003-01-16 12:02:35 +0000 | [diff] [blame] | 310 | if __name__ == "__main__": |
| 311 | test_main(verbose=True) |