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

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

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 5 years, 12 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_iter.py ('k') | Lib/test/test_json/test_decode.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 itertools import * 3 from itertools import *
4 from weakref import proxy 4 from weakref import proxy
5 from decimal import Decimal 5 from decimal import Decimal
6 from fractions import Fraction 6 from fractions import Fraction
7 import sys 7 import sys
8 import operator 8 import operator
9 import random 9 import random
10 import copy 10 import copy
11 import pickle 11 import pickle
12 from functools import reduce 12 from functools import reduce
13 import sys
14 import struct
13 maxsize = support.MAX_Py_ssize_t 15 maxsize = support.MAX_Py_ssize_t
14 minsize = -maxsize-1 16 minsize = -maxsize-1
15 17
16 def lzip(*args): 18 def lzip(*args):
17 return list(zip(*args)) 19 return list(zip(*args))
18 20
19 def onearg(x): 21 def onearg(x):
20 'Test function of one argument' 22 'Test function of one argument'
21 return 2*x 23 return 2*x
22 24
(...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 c = count(3) 500 c = count(3)
499 self.assertEqual(repr(c), 'count(3)') 501 self.assertEqual(repr(c), 'count(3)')
500 next(c) 502 next(c)
501 self.assertEqual(repr(c), 'count(4)') 503 self.assertEqual(repr(c), 'count(4)')
502 c = count(-9) 504 c = count(-9)
503 self.assertEqual(repr(c), 'count(-9)') 505 self.assertEqual(repr(c), 'count(-9)')
504 next(c) 506 next(c)
505 self.assertEqual(repr(count(10.25)), 'count(10.25)') 507 self.assertEqual(repr(count(10.25)), 'count(10.25)')
506 self.assertEqual(next(c), -8) 508 self.assertEqual(next(c), -8)
507 for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5): 509 for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5):
508 # Test repr (ignoring the L in longs) 510 # Test repr
509 r1 = repr(count(i)).replace('L', '') 511 r1 = repr(count(i))
510 r2 = 'count(%r)'.__mod__(i).replace('L', '') 512 r2 = 'count(%r)'.__mod__(i)
511 self.assertEqual(r1, r2) 513 self.assertEqual(r1, r2)
512 514
513 # check copy, deepcopy, pickle 515 # check copy, deepcopy, pickle
514 for value in -3, 3, maxsize-5, maxsize+5: 516 for value in -3, 3, maxsize-5, maxsize+5:
515 c = count(value) 517 c = count(value)
516 self.assertEqual(next(copy.copy(c)), value) 518 self.assertEqual(next(copy.copy(c)), value)
517 self.assertEqual(next(copy.deepcopy(c)), value) 519 self.assertEqual(next(copy.deepcopy(c)), value)
518 self.pickletest(count(value)) 520 self.pickletest(count(value))
519 521
520 #check proper internal error handling for large "step' sizes 522 #check proper internal error handling for large "step' sizes
(...skipping 27 matching lines...) Expand all
548 c = count(-9, -3) 550 c = count(-9, -3)
549 self.assertEqual(repr(c), 'count(-9, -3)') 551 self.assertEqual(repr(c), 'count(-9, -3)')
550 next(c) 552 next(c)
551 self.assertEqual(repr(c), 'count(-12, -3)') 553 self.assertEqual(repr(c), 'count(-12, -3)')
552 self.assertEqual(repr(c), 'count(-12, -3)') 554 self.assertEqual(repr(c), 'count(-12, -3)')
553 self.assertEqual(repr(count(10.5, 1.25)), 'count(10.5, 1.25)') 555 self.assertEqual(repr(count(10.5, 1.25)), 'count(10.5, 1.25)')
554 self.assertEqual(repr(count(10.5, 1)), 'count(10.5)') # suppre ss step=1 when it's an int 556 self.assertEqual(repr(count(10.5, 1)), 'count(10.5)') # suppre ss step=1 when it's an int
555 self.assertEqual(repr(count(10.5, 1.00)), 'count(10.5, 1.0)') # do sho w float values lilke 1.0 557 self.assertEqual(repr(count(10.5, 1.00)), 'count(10.5, 1.0)') # do sho w float values lilke 1.0
556 for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5): 558 for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5):
557 for j in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 1, 10, sys.ma xsize-5, sys.maxsize+5): 559 for j in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 1, 10, sys.ma xsize-5, sys.maxsize+5):
558 # Test repr (ignoring the L in longs) 560 # Test repr
559 r1 = repr(count(i, j)).replace('L', '') 561 r1 = repr(count(i, j))
560 if j == 1: 562 if j == 1:
561 r2 = ('count(%r)' % i).replace('L', '') 563 r2 = ('count(%r)' % i)
562 else: 564 else:
563 r2 = ('count(%r, %r)' % (i, j)).replace('L', '') 565 r2 = ('count(%r, %r)' % (i, j))
564 self.assertEqual(r1, r2) 566 self.assertEqual(r1, r2)
565 self.pickletest(count(i, j)) 567 self.pickletest(count(i, j))
566 568
567 def test_cycle(self): 569 def test_cycle(self):
568 self.assertEqual(take(10, cycle('abc')), list('abcabcabca')) 570 self.assertEqual(take(10, cycle('abc')), list('abcabcabca'))
569 self.assertEqual(list(cycle('')), []) 571 self.assertEqual(list(cycle('')), [])
570 self.assertRaises(TypeError, cycle) 572 self.assertRaises(TypeError, cycle)
571 self.assertRaises(TypeError, cycle, 5) 573 self.assertRaises(TypeError, cycle, 5)
572 self.assertEqual(list(islice(cycle(gen3()),10)), [0,1,2,0,1,2,0,1,2,0]) 574 self.assertEqual(list(islice(cycle(gen3()),10)), [0,1,2,0,1,2,0,1,2,0])
573 575
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after
1190 a, b = tee('abc') 1192 a, b = tee('abc')
1191 c, d = tee(a) 1193 c, d = tee(a)
1192 self.assertIs(a, c) 1194 self.assertIs(a, c)
1193 1195
1194 # test tee_new 1196 # test tee_new
1195 t1, t2 = tee('abc') 1197 t1, t2 = tee('abc')
1196 tnew = type(t1) 1198 tnew = type(t1)
1197 self.assertRaises(TypeError, tnew) 1199 self.assertRaises(TypeError, tnew)
1198 self.assertRaises(TypeError, tnew, 10) 1200 self.assertRaises(TypeError, tnew, 10)
1199 t3 = tnew(t1) 1201 t3 = tnew(t1)
1200 self.assertTrue(list(t1) == list(t2) == list(t3) == list('abc')) 1202 self.assertEqual(list(t1), list('abc'))
1203 self.assertEqual(list(t2), list('abc'))
1204 self.assertEqual(list(t3), list('abc'))
1201 1205
1202 # test that tee objects are weak referencable 1206 # test that tee objects are weak referencable
1203 a, b = tee(range(10)) 1207 a, b = tee(range(10))
1204 p = proxy(a) 1208 p = proxy(a)
1205 self.assertEqual(getattr(p, '__class__'), type(b)) 1209 self.assertEqual(getattr(p, '__class__'), type(b))
1206 del a 1210 del a
1207 self.assertRaises(ReferenceError, getattr, p, '__class__') 1211 self.assertRaises(ReferenceError, getattr, p, '__class__')
1208 1212
1209 ans = list('abc') 1213 ans = list('abc')
1210 long_ans = list(range(10000)) 1214 long_ans = list(range(10000))
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1260 self.assertEqual(list(copy.deepcopy(b)), long_ans[60:]) 1264 self.assertEqual(list(copy.deepcopy(b)), long_ans[60:])
1261 self.assertEqual(list(a), long_ans[100:]) 1265 self.assertEqual(list(a), long_ans[100:])
1262 self.assertEqual(list(b), long_ans[60:]) 1266 self.assertEqual(list(b), long_ans[60:])
1263 1267
1264 # check pickle 1268 # check pickle
1265 self.pickletest(iter(tee('abc'))) 1269 self.pickletest(iter(tee('abc')))
1266 a, b = tee('abc') 1270 a, b = tee('abc')
1267 self.pickletest(a, compare=ans) 1271 self.pickletest(a, compare=ans)
1268 self.pickletest(b, compare=ans) 1272 self.pickletest(b, compare=ans)
1269 1273
1274 # Issue 13454: Crash when deleting backward iterator from tee()
1275 def test_tee_del_backward(self):
1276 forward, backward = tee(repeat(None, 20000000))
1277 any(forward) # exhaust the iterator
1278 del backward
1279
1270 def test_StopIteration(self): 1280 def test_StopIteration(self):
1271 self.assertRaises(StopIteration, next, zip()) 1281 self.assertRaises(StopIteration, next, zip())
1272 1282
1273 for f in (chain, cycle, zip, groupby): 1283 for f in (chain, cycle, zip, groupby):
1274 self.assertRaises(StopIteration, next, f([])) 1284 self.assertRaises(StopIteration, next, f([]))
1275 self.assertRaises(StopIteration, next, f(StopNow())) 1285 self.assertRaises(StopIteration, next, f(StopNow()))
1276 1286
1277 self.assertRaises(StopIteration, next, islice([], None)) 1287 self.assertRaises(StopIteration, next, islice([], None))
1278 self.assertRaises(StopIteration, next, islice(StopNow(), None)) 1288 self.assertRaises(StopIteration, next, islice(StopNow(), None))
1279 1289
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after
1793 for cls in (repeat, zip, filter, filterfalse, chain, map, 1803 for cls in (repeat, zip, filter, filterfalse, chain, map,
1794 starmap, islice, takewhile, dropwhile, cycle, compress): 1804 starmap, islice, takewhile, dropwhile, cycle, compress):
1795 class Subclass(cls): 1805 class Subclass(cls):
1796 def __init__(self, newarg=None, *args): 1806 def __init__(self, newarg=None, *args):
1797 cls.__init__(self, *args) 1807 cls.__init__(self, *args)
1798 try: 1808 try:
1799 Subclass(newarg=1) 1809 Subclass(newarg=1)
1800 except TypeError as err: 1810 except TypeError as err:
1801 # we expect type errors because of wrong argument count 1811 # we expect type errors because of wrong argument count
1802 self.assertNotIn("does not take keyword arguments", err.args[0]) 1812 self.assertNotIn("does not take keyword arguments", err.args[0])
1813
1814 @support.cpython_only
1815 class SizeofTest(unittest.TestCase):
1816 def setUp(self):
1817 self.ssize_t = struct.calcsize('n')
1818
1819 check_sizeof = support.check_sizeof
1820
1821 def test_product_sizeof(self):
1822 basesize = support.calcobjsize('3Pi')
1823 check = self.check_sizeof
1824 check(product('ab', '12'), basesize + 2 * self.ssize_t)
1825 check(product(*(('abc',) * 10)), basesize + 10 * self.ssize_t)
1826
1827 def test_combinations_sizeof(self):
1828 basesize = support.calcobjsize('3Pni')
1829 check = self.check_sizeof
1830 check(combinations('abcd', 3), basesize + 3 * self.ssize_t)
1831 check(combinations(range(10), 4), basesize + 4 * self.ssize_t)
1832
1833 def test_combinations_with_replacement_sizeof(self):
1834 cwr = combinations_with_replacement
1835 basesize = support.calcobjsize('3Pni')
1836 check = self.check_sizeof
1837 check(cwr('abcd', 3), basesize + 3 * self.ssize_t)
1838 check(cwr(range(10), 4), basesize + 4 * self.ssize_t)
1839
1840 def test_permutations_sizeof(self):
1841 basesize = support.calcobjsize('4Pni')
1842 check = self.check_sizeof
1843 check(permutations('abcd'),
1844 basesize + 4 * self.ssize_t + 4 * self.ssize_t)
1845 check(permutations('abcd', 3),
1846 basesize + 4 * self.ssize_t + 3 * self.ssize_t)
1847 check(permutations('abcde', 3),
1848 basesize + 5 * self.ssize_t + 3 * self.ssize_t)
1849 check(permutations(range(10), 4),
1850 basesize + 10 * self.ssize_t + 4 * self.ssize_t)
1803 1851
1804 1852
1805 libreftest = """ Doctest for examples in the library reference: libitertools.tex 1853 libreftest = """ Doctest for examples in the library reference: libitertools.tex
1806 1854
1807 1855
1808 >>> amounts = [120.15, 764.05, 823.14] 1856 >>> amounts = [120.15, 764.05, 823.14]
1809 >>> for checknum, amount in zip(count(1200), amounts): 1857 >>> for checknum, amount in zip(count(1200), amounts):
1810 ... print('Check %d is for $%.2f' % (checknum, amount)) 1858 ... print('Check %d is for $%.2f' % (checknum, amount))
1811 ... 1859 ...
1812 Check 1200 is for $120.15 1860 Check 1200 is for $120.15
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
2029 >>> list(unique_justseen('ABBCcAD', str.lower)) 2077 >>> list(unique_justseen('ABBCcAD', str.lower))
2030 ['A', 'B', 'C', 'A', 'D'] 2078 ['A', 'B', 'C', 'A', 'D']
2031 2079
2032 """ 2080 """
2033 2081
2034 __test__ = {'libreftest' : libreftest} 2082 __test__ = {'libreftest' : libreftest}
2035 2083
2036 def test_main(verbose=None): 2084 def test_main(verbose=None):
2037 test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC, 2085 test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC,
2038 RegressionTests, LengthTransparency, 2086 RegressionTests, LengthTransparency,
2039 SubclassWithKwargsTest, TestExamples) 2087 SubclassWithKwargsTest, TestExamples,
2088 SizeofTest)
2040 support.run_unittest(*test_classes) 2089 support.run_unittest(*test_classes)
2041 2090
2042 # verify reference counting 2091 # verify reference counting
2043 if verbose and hasattr(sys, "gettotalrefcount"): 2092 if verbose and hasattr(sys, "gettotalrefcount"):
2044 import gc 2093 import gc
2045 counts = [None] * 5 2094 counts = [None] * 5
2046 for i in range(len(counts)): 2095 for i in range(len(counts)):
2047 support.run_unittest(*test_classes) 2096 support.run_unittest(*test_classes)
2048 gc.collect() 2097 gc.collect()
2049 counts[i] = sys.gettotalrefcount() 2098 counts[i] = sys.gettotalrefcount()
2050 print(counts) 2099 print(counts)
2051 2100
2052 # doctest the examples in the library reference 2101 # doctest the examples in the library reference
2053 support.run_doctest(sys.modules[__name__], verbose) 2102 support.run_doctest(sys.modules[__name__], verbose)
2054 2103
2055 if __name__ == "__main__": 2104 if __name__ == "__main__":
2056 test_main(verbose=True) 2105 test_main(verbose=True)
LEFTRIGHT

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