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

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

Issue 16510: Using appropriate checks in tests
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:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_inspect.py ('k') | Lib/test/test_itertools.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 # Test iterators. 1 # Test iterators.
2 2
3 import unittest 3 import unittest
4 from test.support import run_unittest, TESTFN, unlink, cpython_only 4 from test.support import run_unittest, TESTFN, unlink, cpython_only
5 import pickle 5 import pickle
6 import collections.abc 6 import collections.abc
7 7
8 # Test result of triple loop (too big to inline) 8 # Test result of triple loop (too big to inline)
9 TRIPLETS = [(0, 0, 0), (0, 0, 1), (0, 0, 2), 9 TRIPLETS = [(0, 0, 0), (0, 0, 1), (0, 0, 2),
10 (0, 1, 0), (0, 1, 1), (0, 1, 2), 10 (0, 1, 0), (0, 1, 1), (0, 1, 2),
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 res.append(val) 74 res.append(val)
75 self.assertEqual(res, seq) 75 self.assertEqual(res, seq)
76 76
77 # Helper to check picklability 77 # Helper to check picklability
78 def check_pickle(self, itorg, seq): 78 def check_pickle(self, itorg, seq):
79 d = pickle.dumps(itorg) 79 d = pickle.dumps(itorg)
80 it = pickle.loads(d) 80 it = pickle.loads(d)
81 # Cannot assert type equality because dict iterators unpickle as list 81 # Cannot assert type equality because dict iterators unpickle as list
82 # iterators. 82 # iterators.
83 # self.assertEqual(type(itorg), type(it)) 83 # self.assertEqual(type(itorg), type(it))
84 self.assertTrue(isinstance(it, collections.abc.Iterator)) 84 self.assertIsInstance(it, collections.abc.Iterator)
85 self.assertEqual(list(it), seq) 85 self.assertEqual(list(it), seq)
86 86
87 it = pickle.loads(d) 87 it = pickle.loads(d)
88 try: 88 try:
89 next(it) 89 next(it)
90 except StopIteration: 90 except StopIteration:
91 return 91 return
92 d = pickle.dumps(it) 92 d = pickle.dumps(it)
93 it = pickle.loads(d) 93 it = pickle.loads(d)
94 self.assertEqual(list(it), seq[1:]) 94 self.assertEqual(list(it), seq[1:])
95 95
96 # Test basic use of iter() function 96 # Test basic use of iter() function
97 def test_iter_basic(self): 97 def test_iter_basic(self):
98 self.check_iterator(iter(range(10)), list(range(10))) 98 self.check_iterator(iter(range(10)), list(range(10)))
99 99
100 # Test that iter(iter(x)) is the same as iter(x) 100 # Test that iter(iter(x)) is the same as iter(x)
101 def test_iter_idempotency(self): 101 def test_iter_idempotency(self):
102 seq = list(range(10)) 102 seq = list(range(10))
103 it = iter(seq) 103 it = iter(seq)
104 it2 = iter(it) 104 it2 = iter(it)
105 self.assertTrue(it is it2) 105 self.assertIs(it, it2)
106 106
107 # Test that for loops over iterators work 107 # Test that for loops over iterators work
108 def test_iter_for_loop(self): 108 def test_iter_for_loop(self):
109 self.check_for_loop(iter(range(10)), list(range(10))) 109 self.check_for_loop(iter(range(10)), list(range(10)))
110 110
111 # Test several independent iterators over the same list 111 # Test several independent iterators over the same list
112 def test_iter_independence(self): 112 def test_iter_independence(self):
113 seq = range(3) 113 seq = range(3)
114 res = [] 114 res = []
115 for i in iter(seq): 115 for i in iter(seq):
(...skipping 802 matching lines...) Expand 10 before | Expand all | Expand 10 after
918 lst.extend(gen()) 918 lst.extend(gen())
919 self.assertEqual(len(lst), 760) 919 self.assertEqual(len(lst), 760)
920 920
921 921
922 def test_main(): 922 def test_main():
923 run_unittest(TestCase) 923 run_unittest(TestCase)
924 924
925 925
926 if __name__ == "__main__": 926 if __name__ == "__main__":
927 test_main() 927 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_inspect.py ('k') | Lib/test/test_itertools.py » ('j') | no next file with comments »

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