Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(95)

Side by Side Diff: Lib/test/test_bisect.py

Issue 16897: Fix test discovery for test_bisect.py
Patch Set: Created 7 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import sys 1 import sys
2 import unittest 2 import unittest
3 from test import support 3 from test import support
4 from collections import UserList 4 from collections import UserList
5 5
6 # We do a bit of trickery here to be able to test both the C implementation 6 py_bisect = support.import_fresh_module('bisect', blocked=['_bisect'])
7 # and the Python implementation of the module. 7 c_bisect = support.import_fresh_module('bisect', fresh=['_bisect'])
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 8
26 class Range(object): 9 class Range(object):
27 """A trivial range()-like object without any integer width limitations.""" 10 """A trivial range()-like object without any integer width limitations."""
28 def __init__(self, start, stop): 11 def __init__(self, start, stop):
29 self.start = start 12 self.start = start
30 self.stop = stop 13 self.stop = stop
31 self.last_insert = None 14 self.last_insert = None
32 15
33 def __len__(self): 16 def __len__(self):
34 return self.stop - self.start 17 return self.stop - self.start
35 18
36 def __getitem__(self, idx): 19 def __getitem__(self, idx):
37 n = self.stop - self.start 20 n = self.stop - self.start
38 if idx < 0: 21 if idx < 0:
39 idx += n 22 idx += n
40 if idx >= n: 23 if idx >= n:
41 raise IndexError(idx) 24 raise IndexError(idx)
42 return self.start + idx 25 return self.start + idx
43 26
44 def insert(self, idx, item): 27 def insert(self, idx, item):
45 self.last_insert = idx, item 28 self.last_insert = idx, item
46 29
47 30
48 class TestBisect(unittest.TestCase): 31 class TestBisect:
49 module = None
50
51 def setUp(self): 32 def setUp(self):
52 self.precomputedCases = [ 33 self.precomputedCases = [
53 (self.module.bisect_right, [], 1, 0), 34 (self.module.bisect_right, [], 1, 0),
54 (self.module.bisect_right, [1], 0, 0), 35 (self.module.bisect_right, [1], 0, 0),
55 (self.module.bisect_right, [1], 1, 1), 36 (self.module.bisect_right, [1], 1, 1),
56 (self.module.bisect_right, [1], 2, 1), 37 (self.module.bisect_right, [1], 2, 1),
57 (self.module.bisect_right, [1, 1], 0, 0), 38 (self.module.bisect_right, [1, 1], 0, 0),
58 (self.module.bisect_right, [1, 1], 1, 2), 39 (self.module.bisect_right, [1, 1], 1, 2),
59 (self.module.bisect_right, [1, 1], 2, 2), 40 (self.module.bisect_right, [1, 1], 2, 2),
60 (self.module.bisect_right, [1, 1, 1], 0, 0), 41 (self.module.bisect_right, [1, 1, 1], 0, 0),
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 def test_keyword_args(self): 192 def test_keyword_args(self):
212 data = [10, 20, 30, 40, 50] 193 data = [10, 20, 30, 40, 50]
213 self.assertEqual(self.module.bisect_left(a=data, x=25, lo=1, hi=3), 2) 194 self.assertEqual(self.module.bisect_left(a=data, x=25, lo=1, hi=3), 2)
214 self.assertEqual(self.module.bisect_right(a=data, x=25, lo=1, hi=3), 2) 195 self.assertEqual(self.module.bisect_right(a=data, x=25, lo=1, hi=3), 2)
215 self.assertEqual(self.module.bisect(a=data, x=25, lo=1, hi=3), 2) 196 self.assertEqual(self.module.bisect(a=data, x=25, lo=1, hi=3), 2)
216 self.module.insort_left(a=data, x=25, lo=1, hi=3) 197 self.module.insort_left(a=data, x=25, lo=1, hi=3)
217 self.module.insort_right(a=data, x=25, lo=1, hi=3) 198 self.module.insort_right(a=data, x=25, lo=1, hi=3)
218 self.module.insort(a=data, x=25, lo=1, hi=3) 199 self.module.insort(a=data, x=25, lo=1, hi=3)
219 self.assertEqual(data, [10, 20, 25, 25, 25, 30, 40, 50]) 200 self.assertEqual(data, [10, 20, 25, 25, 25, 30, 40, 50])
220 201
221 class TestBisectPython(TestBisect): 202 class TestBisectPython(TestBisect, unittest.TestCase):
222 module = py_bisect 203 module = py_bisect
223 204
224 class TestBisectC(TestBisect): 205 class TestBisectC(TestBisect, unittest.TestCase):
225 module = c_bisect 206 module = c_bisect
226 207
227 #============================================================================== 208 #==============================================================================
228 209
229 class TestInsort(unittest.TestCase): 210 class TestInsort:
230 module = None
231
232 def test_vsBuiltinSort(self, n=500): 211 def test_vsBuiltinSort(self, n=500):
233 from random import choice 212 from random import choice
234 for insorted in (list(), UserList()): 213 for insorted in (list(), UserList()):
235 for i in range(n): 214 for i in range(n):
236 digit = choice("0123456789") 215 digit = choice("0123456789")
237 if digit in "02468": 216 if digit in "02468":
238 f = self.module.insort_left 217 f = self.module.insort_left
239 else: 218 else:
240 f = self.module.insort_right 219 f = self.module.insort_right
241 f(insorted, digit) 220 f(insorted, digit)
242 self.assertEqual(sorted(insorted), insorted) 221 self.assertEqual(sorted(insorted), insorted)
243 222
244 def test_backcompatibility(self): 223 def test_backcompatibility(self):
245 self.assertEqual(self.module.insort, self.module.insort_right) 224 self.assertEqual(self.module.insort, self.module.insort_right)
246 225
247 def test_listDerived(self): 226 def test_listDerived(self):
248 class List(list): 227 class List(list):
249 data = [] 228 data = []
250 def insert(self, index, item): 229 def insert(self, index, item):
251 self.data.insert(index, item) 230 self.data.insert(index, item)
252 231
253 lst = List() 232 lst = List()
254 self.module.insort_left(lst, 10) 233 self.module.insort_left(lst, 10)
255 self.module.insort_right(lst, 5) 234 self.module.insort_right(lst, 5)
256 self.assertEqual([5, 10], lst.data) 235 self.assertEqual([5, 10], lst.data)
257 236
258 class TestInsortPython(TestInsort): 237 class TestInsortPython(TestInsort, unittest.TestCase):
259 module = py_bisect 238 module = py_bisect
260 239
261 class TestInsortC(TestInsort): 240 class TestInsortC(TestInsort, unittest.TestCase):
262 module = c_bisect 241 module = c_bisect
263 242
264 #============================================================================== 243 #==============================================================================
265 244
266 245
267 class LenOnly: 246 class LenOnly:
268 "Dummy sequence class defining __len__ but not __getitem__." 247 "Dummy sequence class defining __len__ but not __getitem__."
269 def __len__(self): 248 def __len__(self):
270 return 10 249 return 10
271 250
272 class GetOnly: 251 class GetOnly:
273 "Dummy sequence class defining __getitem__ but not __len__." 252 "Dummy sequence class defining __getitem__ but not __len__."
274 def __getitem__(self, ndx): 253 def __getitem__(self, ndx):
275 return 10 254 return 10
276 255
277 class CmpErr: 256 class CmpErr:
278 "Dummy element that always raises an error during comparison" 257 "Dummy element that always raises an error during comparison"
279 def __lt__(self, other): 258 def __lt__(self, other):
280 raise ZeroDivisionError 259 raise ZeroDivisionError
281 __gt__ = __lt__ 260 __gt__ = __lt__
282 __le__ = __lt__ 261 __le__ = __lt__
283 __ge__ = __lt__ 262 __ge__ = __lt__
284 __eq__ = __lt__ 263 __eq__ = __lt__
285 __ne__ = __lt__ 264 __ne__ = __lt__
286 265
287 class TestErrorHandling(unittest.TestCase): 266 class TestErrorHandling:
288 module = None
289
290 def test_non_sequence(self): 267 def test_non_sequence(self):
291 for f in (self.module.bisect_left, self.module.bisect_right, 268 for f in (self.module.bisect_left, self.module.bisect_right,
292 self.module.insort_left, self.module.insort_right): 269 self.module.insort_left, self.module.insort_right):
293 self.assertRaises(TypeError, f, 10, 10) 270 self.assertRaises(TypeError, f, 10, 10)
294 271
295 def test_len_only(self): 272 def test_len_only(self):
296 for f in (self.module.bisect_left, self.module.bisect_right, 273 for f in (self.module.bisect_left, self.module.bisect_right,
297 self.module.insort_left, self.module.insort_right): 274 self.module.insort_left, self.module.insort_right):
298 self.assertRaises(TypeError, f, LenOnly(), 10) 275 self.assertRaises(TypeError, f, LenOnly(), 10)
299 276
300 def test_get_only(self): 277 def test_get_only(self):
301 for f in (self.module.bisect_left, self.module.bisect_right, 278 for f in (self.module.bisect_left, self.module.bisect_right,
302 self.module.insort_left, self.module.insort_right): 279 self.module.insort_left, self.module.insort_right):
303 self.assertRaises(TypeError, f, GetOnly(), 10) 280 self.assertRaises(TypeError, f, GetOnly(), 10)
304 281
305 def test_cmp_err(self): 282 def test_cmp_err(self):
306 seq = [CmpErr(), CmpErr(), CmpErr()] 283 seq = [CmpErr(), CmpErr(), CmpErr()]
307 for f in (self.module.bisect_left, self.module.bisect_right, 284 for f in (self.module.bisect_left, self.module.bisect_right,
308 self.module.insort_left, self.module.insort_right): 285 self.module.insort_left, self.module.insort_right):
309 self.assertRaises(ZeroDivisionError, f, seq, 10) 286 self.assertRaises(ZeroDivisionError, f, seq, 10)
310 287
311 def test_arg_parsing(self): 288 def test_arg_parsing(self):
312 for f in (self.module.bisect_left, self.module.bisect_right, 289 for f in (self.module.bisect_left, self.module.bisect_right,
313 self.module.insort_left, self.module.insort_right): 290 self.module.insort_left, self.module.insort_right):
314 self.assertRaises(TypeError, f, 10) 291 self.assertRaises(TypeError, f, 10)
315 292
316 class TestErrorHandlingPython(TestErrorHandling): 293 class TestErrorHandlingPython(TestErrorHandling, unittest.TestCase):
317 module = py_bisect 294 module = py_bisect
318 295
319 class TestErrorHandlingC(TestErrorHandling): 296 class TestErrorHandlingC(TestErrorHandling, unittest.TestCase):
320 module = c_bisect 297 module = c_bisect
321 298
322 #============================================================================== 299 #==============================================================================
323 300
324 libreftest = """ 301 libreftest = """
325 Example from the Library Reference: Doc/library/bisect.rst 302 Example from the Library Reference: Doc/library/bisect.rst
326 303
327 The bisect() function is generally useful for categorizing numeric data. 304 The bisect() function is generally useful for categorizing numeric data.
328 This example uses bisect() to look up a letter grade for an exam total 305 This example uses bisect() to look up a letter grade for an exam total
329 (say) based on a set of ordered numeric breakpoints: 85 and up is an `A', 306 (say) based on a set of ordered numeric breakpoints: 85 and up is an `A',
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 import gc 338 import gc
362 counts = [None] * 5 339 counts = [None] * 5
363 for i in range(len(counts)): 340 for i in range(len(counts)):
364 support.run_unittest(*test_classes) 341 support.run_unittest(*test_classes)
365 gc.collect() 342 gc.collect()
366 counts[i] = sys.gettotalrefcount() 343 counts[i] = sys.gettotalrefcount()
367 print(counts) 344 print(counts)
368 345
369 if __name__ == "__main__": 346 if __name__ == "__main__":
370 test_main(verbose=True) 347 test_main(verbose=True)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+