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

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

Issue 16510: Using appropriate checks in tests
Patch Set: Created 5 years, 11 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 | « Lib/test/test_funcattrs.py ('k') | Lib/test/test_gc.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import abc 1 import abc
2 import collections 2 import collections
3 from itertools import permutations 3 from itertools import permutations
4 import pickle 4 import pickle
5 from random import choice 5 from random import choice
6 import sys 6 import sys
7 from test import support 7 from test import support
8 import unittest 8 import unittest
9 from weakref import proxy 9 from weakref import proxy
10 10
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 self.assertEqual(p(b=2), ((), {'a':1, 'b':2})) 98 self.assertEqual(p(b=2), ((), {'a':1, 'b':2}))
99 # keyword args in the call override those in the partial object 99 # keyword args in the call override those in the partial object
100 self.assertEqual(p(a=3, b=2), ((), {'a':3, 'b':2})) 100 self.assertEqual(p(a=3, b=2), ((), {'a':3, 'b':2}))
101 101
102 def test_positional(self): 102 def test_positional(self):
103 # make sure positional arguments are captured correctly 103 # make sure positional arguments are captured correctly
104 for args in [(), (0,), (0,1), (0,1,2), (0,1,2,3)]: 104 for args in [(), (0,), (0,1), (0,1,2), (0,1,2,3)]:
105 p = self.partial(capture, *args) 105 p = self.partial(capture, *args)
106 expected = args + ('x',) 106 expected = args + ('x',)
107 got, empty = p('x') 107 got, empty = p('x')
108 self.assertTrue(expected == got and empty == {}) 108 self.assertEqual(got, expected)
109 self.assertEqual(empty, {})
109 110
110 def test_keyword(self): 111 def test_keyword(self):
111 # make sure keyword arguments are captured correctly 112 # make sure keyword arguments are captured correctly
112 for a in ['a', 0, None, 3.5]: 113 for a in ['a', 0, None, 3.5]:
113 p = self.partial(capture, a=a) 114 p = self.partial(capture, a=a)
114 expected = {'a':a,'x':None} 115 expected = {'a':a,'x':None}
115 empty, got = p(x=None) 116 empty, got = p(x=None)
116 self.assertTrue(expected == got and empty == ()) 117 self.assertEqual(got, expected)
118 self.assertEqual(empty, ())
117 119
118 def test_no_side_effects(self): 120 def test_no_side_effects(self):
119 # make sure there are no side effects that affect subsequent calls 121 # make sure there are no side effects that affect subsequent calls
120 p = self.partial(capture, 0, a=1) 122 p = self.partial(capture, 0, a=1)
121 args1, kw1 = p(1, b=2) 123 args1, kw1 = p(1, b=2)
122 self.assertTrue(args1 == (0,1) and kw1 == {'a':1,'b':2}) 124 self.assertEqual(args1, (0,1))
125 self.assertEqual(kw1, {'a':1,'b':2})
123 args2, kw2 = p() 126 args2, kw2 = p()
124 self.assertTrue(args2 == (0,) and kw2 == {'a':1}) 127 self.assertEqual(args2, (0,))
128 self.assertEqual(kw2, {'a':1})
125 129
126 def test_error_propagation(self): 130 def test_error_propagation(self):
127 def f(x, y): 131 def f(x, y):
128 x / y 132 x / y
129 self.assertRaises(ZeroDivisionError, self.partial(f, 1, 0)) 133 self.assertRaises(ZeroDivisionError, self.partial(f, 1, 0))
130 self.assertRaises(ZeroDivisionError, self.partial(f, 1), 0) 134 self.assertRaises(ZeroDivisionError, self.partial(f, 1), 0)
131 self.assertRaises(ZeroDivisionError, self.partial(f), 1, 0) 135 self.assertRaises(ZeroDivisionError, self.partial(f), 1, 0)
132 self.assertRaises(ZeroDivisionError, self.partial(f, y=0), 1) 136 self.assertRaises(ZeroDivisionError, self.partial(f, y=0), 1)
133 137
134 def test_weakref(self): 138 def test_weakref(self):
(...skipping 754 matching lines...) Expand 10 before | Expand all | Expand 10 after
889 self.assertEqual(hits, 0) 893 self.assertEqual(hits, 0)
890 self.assertEqual(misses, 0) 894 self.assertEqual(misses, 0)
891 895
892 domain = range(5) 896 domain = range(5)
893 for i in range(1000): 897 for i in range(1000):
894 x, y = choice(domain), choice(domain) 898 x, y = choice(domain), choice(domain)
895 actual = f(x, y) 899 actual = f(x, y)
896 expected = orig(x, y) 900 expected = orig(x, y)
897 self.assertEqual(actual, expected) 901 self.assertEqual(actual, expected)
898 hits, misses, maxsize, currsize = f.cache_info() 902 hits, misses, maxsize, currsize = f.cache_info()
899 self.assertTrue(hits > misses) 903 self.assertGreater(hits, misses)
900 self.assertEqual(hits + misses, 1000) 904 self.assertEqual(hits + misses, 1000)
901 self.assertEqual(currsize, 20) 905 self.assertEqual(currsize, 20)
902 906
903 f.cache_clear() # test clearing 907 f.cache_clear() # test clearing
904 hits, misses, maxsize, currsize = f.cache_info() 908 hits, misses, maxsize, currsize = f.cache_info()
905 self.assertEqual(hits, 0) 909 self.assertEqual(hits, 0)
906 self.assertEqual(misses, 0) 910 self.assertEqual(misses, 0)
907 self.assertEqual(currsize, 0) 911 self.assertEqual(currsize, 0)
908 f(x, y) 912 f(x, y)
909 hits, misses, maxsize, currsize = f.cache_info() 913 hits, misses, maxsize, currsize = f.cache_info()
(...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after
1565 import gc 1569 import gc
1566 counts = [None] * 5 1570 counts = [None] * 5
1567 for i in range(len(counts)): 1571 for i in range(len(counts)):
1568 support.run_unittest(*test_classes) 1572 support.run_unittest(*test_classes)
1569 gc.collect() 1573 gc.collect()
1570 counts[i] = sys.gettotalrefcount() 1574 counts[i] = sys.gettotalrefcount()
1571 print(counts) 1575 print(counts)
1572 1576
1573 if __name__ == '__main__': 1577 if __name__ == '__main__':
1574 test_main(verbose=True) 1578 test_main(verbose=True)
OLDNEW
« no previous file with comments | « Lib/test/test_funcattrs.py ('k') | Lib/test/test_gc.py » ('j') | no next file with comments »

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