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

Delta Between Two Patch Sets: Lib/test/test_weakset.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 6 years, 9 months ago
Right Patch Set: Created 5 years, 6 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_uuid.py ('k') | Lib/test/test_winreg.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 import unittest 1 import unittest
2 from test import support 2 from test import support
3 from weakref import proxy, ref, WeakSet 3 from weakref import proxy, ref, WeakSet
4 import operator 4 import operator
5 import copy 5 import copy
6 import string 6 import string
7 import os 7 import os
8 from random import randrange, shuffle 8 from random import randrange, shuffle
9 import sys 9 import sys
10 import warnings 10 import warnings
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 def test_eq(self): 333 def test_eq(self):
334 # issue 5964 334 # issue 5964
335 self.assertTrue(self.s == self.s) 335 self.assertTrue(self.s == self.s)
336 self.assertTrue(self.s == WeakSet(self.items)) 336 self.assertTrue(self.s == WeakSet(self.items))
337 self.assertFalse(self.s == set(self.items)) 337 self.assertFalse(self.s == set(self.items))
338 self.assertFalse(self.s == list(self.items)) 338 self.assertFalse(self.s == list(self.items))
339 self.assertFalse(self.s == tuple(self.items)) 339 self.assertFalse(self.s == tuple(self.items))
340 self.assertFalse(self.s == WeakSet([Foo])) 340 self.assertFalse(self.s == WeakSet([Foo]))
341 self.assertFalse(self.s == 1) 341 self.assertFalse(self.s == 1)
342 342
343 def test_ne(self):
344 self.assertTrue(self.s != set(self.items))
345 s1 = WeakSet()
346 s2 = WeakSet()
347 self.assertFalse(s1 != s2)
348
343 def test_weak_destroy_while_iterating(self): 349 def test_weak_destroy_while_iterating(self):
344 # Issue #7105: iterators shouldn't crash when a key is implicitly remove d 350 # Issue #7105: iterators shouldn't crash when a key is implicitly remove d
345 # Create new items to be sure no-one else holds a reference 351 # Create new items to be sure no-one else holds a reference
346 items = [ustr(c) for c in ('a', 'b', 'c')] 352 items = [ustr(c) for c in ('a', 'b', 'c')]
347 s = WeakSet(items) 353 s = WeakSet(items)
348 it = iter(s) 354 it = iter(s)
349 next(it) # Trigger internal iteration 355 next(it) # Trigger internal iteration
350 # Destroy an item 356 # Destroy an item
351 del items[-1] 357 del items[-1]
352 gc.collect() # just in case 358 gc.collect() # just in case
353 # We have removed either the first consumed items, or another one 359 # We have removed either the first consumed items, or another one
354 self.assertIn(len(list(it)), [len(items), len(items) - 1]) 360 self.assertIn(len(list(it)), [len(items), len(items) - 1])
355 del it 361 del it
356 # The removal has been committed 362 # The removal has been committed
357 self.assertEqual(len(s), len(items)) 363 self.assertEqual(len(s), len(items))
358 364
359 def test_weak_destroy_and_mutate_while_iterating(self): 365 def test_weak_destroy_and_mutate_while_iterating(self):
360 # Issue #7105: iterators shouldn't crash when a key is implicitly remove d 366 # Issue #7105: iterators shouldn't crash when a key is implicitly remove d
361 items = [ustr(c) for c in string.ascii_letters] 367 items = [ustr(c) for c in string.ascii_letters]
362 s = WeakSet(items) 368 s = WeakSet(items)
363 @contextlib.contextmanager 369 @contextlib.contextmanager
364 def testcontext(): 370 def testcontext():
365 try: 371 try:
366 it = iter(s) 372 it = iter(s)
367 next(it) 373 # Start iterator
368 del it 374 yielded = ustr(str(next(it)))
369 # Schedule an item for removal and recreate it 375 # Schedule an item for removal and recreate it
370 u = ustr(str(items.pop())) 376 u = ustr(str(items.pop()))
377 if yielded == u:
378 # The iterator still has a reference to the removed item,
379 # advance it (issue #20006).
380 next(it)
371 gc.collect() # just in case 381 gc.collect() # just in case
372 yield u 382 yield u
373 finally: 383 finally:
374 it = None # should commit all removals 384 it = None # should commit all removals
375 385
376 with testcontext() as u: 386 with testcontext() as u:
377 self.assertNotIn(u, s) 387 self.assertNotIn(u, s)
378 with testcontext() as u: 388 with testcontext() as u:
379 self.assertRaises(KeyError, s.remove, u) 389 self.assertRaises(KeyError, s.remove, u)
380 self.assertNotIn(u, s) 390 self.assertNotIn(u, s)
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 self.assertLessEqual(n1, N) 441 self.assertLessEqual(n1, N)
432 self.assertGreaterEqual(n2, 0) 442 self.assertGreaterEqual(n2, 0)
433 self.assertLessEqual(n2, n1) 443 self.assertLessEqual(n2, n1)
434 444
435 445
436 def test_main(verbose=None): 446 def test_main(verbose=None):
437 support.run_unittest(TestWeakSet) 447 support.run_unittest(TestWeakSet)
438 448
439 if __name__ == "__main__": 449 if __name__ == "__main__":
440 test_main(verbose=True) 450 test_main(verbose=True)
LEFTRIGHT

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