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

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

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 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_io.py ('k') | Lib/test/test_kqueue.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 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
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 self.assertEqual(len(c), r) # r-leng th combinations 161 self.assertEqual(len(c), r) # r-leng th combinations
162 self.assertEqual(len(set(c)), r) # no dup licate elements 162 self.assertEqual(len(set(c)), r) # no dup licate elements
163 self.assertEqual(list(c), sorted(c)) # keep o riginal ordering 163 self.assertEqual(list(c), sorted(c)) # keep o riginal ordering
164 self.assertTrue(all(e in values for e in c)) # ele ments taken from input iterable 164 self.assertTrue(all(e in values for e in c)) # ele ments taken from input iterable
165 self.assertEqual(list(c), 165 self.assertEqual(list(c),
166 [e for e in values if e in c]) # comb is a subsequence of the input iterable 166 [e for e in values if e in c]) # comb is a subsequence of the input iterable
167 self.assertEqual(result, list(combinations1(values, r))) # match es first pure python version 167 self.assertEqual(result, list(combinations1(values, r))) # match es first pure python version
168 self.assertEqual(result, list(combinations2(values, r))) # match es second pure python version 168 self.assertEqual(result, list(combinations2(values, r))) # match es second pure python version
169 self.assertEqual(result, list(combinations3(values, r))) # match es second pure python version 169 self.assertEqual(result, list(combinations3(values, r))) # match es second pure python version
170 170
171 @support.impl_detail("tuple reuse is specific to CPython") 171 # Test implementation detail: tuple re-use
172 def test_combinations_tuple_reuse(self):
173 self.assertEqual(len(set(map(id, combinations('abcde', 3)))), 1) 172 self.assertEqual(len(set(map(id, combinations('abcde', 3)))), 1)
174 self.assertNotEqual(len(set(map(id, list(combinations('abcde', 3))))), 1 ) 173 self.assertNotEqual(len(set(map(id, list(combinations('abcde', 3))))), 1 )
175 174
176 def test_combinations_with_replacement(self): 175 def test_combinations_with_replacement(self):
177 cwr = combinations_with_replacement 176 cwr = combinations_with_replacement
178 self.assertRaises(TypeError, cwr, 'abc') # missing r argument 177 self.assertRaises(TypeError, cwr, 'abc') # missing r argument
179 self.assertRaises(TypeError, cwr, 'abc', 2, 1) # too many arguments 178 self.assertRaises(TypeError, cwr, 'abc', 2, 1) # too many arguments
180 self.assertRaises(TypeError, cwr, None) # pool is not iterable 179 self.assertRaises(TypeError, cwr, None) # pool is not iterable
181 self.assertRaises(ValueError, cwr, 'abc', -2) # r is negative 180 self.assertRaises(ValueError, cwr, 'abc', -2) # r is negative
182 self.assertEqual(list(cwr('ABC', 2)), 181 self.assertEqual(list(cwr('ABC', 2)),
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 self.assertEqual(len(c), r) # r-leng th combinations 231 self.assertEqual(len(c), r) # r-leng th combinations
233 noruns = [k for k,v in groupby(c)] # combo without consecutive repeats 232 noruns = [k for k,v in groupby(c)] # combo without consecutive repeats
234 self.assertEqual(len(noruns), len(set(noruns))) # no rep eats other than consecutive 233 self.assertEqual(len(noruns), len(set(noruns))) # no rep eats other than consecutive
235 self.assertEqual(list(c), sorted(c)) # keep o riginal ordering 234 self.assertEqual(list(c), sorted(c)) # keep o riginal ordering
236 self.assertTrue(all(e in values for e in c)) # ele ments taken from input iterable 235 self.assertTrue(all(e in values for e in c)) # ele ments taken from input iterable
237 self.assertEqual(noruns, 236 self.assertEqual(noruns,
238 [e for e in values if e in c]) # comb i s a subsequence of the input iterable 237 [e for e in values if e in c]) # comb i s a subsequence of the input iterable
239 self.assertEqual(result, list(cwr1(values, r))) # matche s first pure python version 238 self.assertEqual(result, list(cwr1(values, r))) # matche s first pure python version
240 self.assertEqual(result, list(cwr2(values, r))) # matche s second pure python version 239 self.assertEqual(result, list(cwr2(values, r))) # matche s second pure python version
241 240
242 @support.impl_detail("tuple reuse is specific to CPython") 241 # Test implementation detail: tuple re-use
243 def test_combinations_with_replacement_tuple_reuse(self):
244 cwr = combinations_with_replacement
245 self.assertEqual(len(set(map(id, cwr('abcde', 3)))), 1) 242 self.assertEqual(len(set(map(id, cwr('abcde', 3)))), 1)
246 self.assertNotEqual(len(set(map(id, list(cwr('abcde', 3))))), 1) 243 self.assertNotEqual(len(set(map(id, list(cwr('abcde', 3))))), 1)
247 244
248 def test_permutations(self): 245 def test_permutations(self):
249 self.assertRaises(TypeError, permutations) # too few argume nts 246 self.assertRaises(TypeError, permutations) # too few argume nts
250 self.assertRaises(TypeError, permutations, 'abc', 2, 1) # too many argum ents 247 self.assertRaises(TypeError, permutations, 'abc', 2, 1) # too many argum ents
251 self.assertRaises(TypeError, permutations, None) # pool is not it erable 248 self.assertRaises(TypeError, permutations, None) # pool is not it erable
252 self.assertRaises(ValueError, permutations, 'abc', -2) # r is negative 249 self.assertRaises(ValueError, permutations, 'abc', -2) # r is negative
253 self.assertEqual(list(permutations('abc', 32)), []) # r > n 250 self.assertEqual(list(permutations('abc', 32)), []) # r > n
254 self.assertRaises(TypeError, permutations, 'abc', 's') # r is not an in t or None 251 self.assertRaises(TypeError, permutations, 'abc', 's') # r is not an in t or None
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 for p in result: 295 for p in result:
299 self.assertEqual(len(p), r) # r-leng th permutations 296 self.assertEqual(len(p), r) # r-leng th permutations
300 self.assertEqual(len(set(p)), r) # no dup licate elements 297 self.assertEqual(len(set(p)), r) # no dup licate elements
301 self.assertTrue(all(e in values for e in p)) # ele ments taken from input iterable 298 self.assertTrue(all(e in values for e in p)) # ele ments taken from input iterable
302 self.assertEqual(result, list(permutations1(values, r))) # match es first pure python version 299 self.assertEqual(result, list(permutations1(values, r))) # match es first pure python version
303 self.assertEqual(result, list(permutations2(values, r))) # match es second pure python version 300 self.assertEqual(result, list(permutations2(values, r))) # match es second pure python version
304 if r == n: 301 if r == n:
305 self.assertEqual(result, list(permutations(values, None))) # test r as None 302 self.assertEqual(result, list(permutations(values, None))) # test r as None
306 self.assertEqual(result, list(permutations(values))) # test default r 303 self.assertEqual(result, list(permutations(values))) # test default r
307 304
308 @support.impl_detail("tuple resuse is CPython specific") 305 # Test implementation detail: tuple re-use
309 def test_permutations_tuple_reuse(self):
310 self.assertEqual(len(set(map(id, permutations('abcde', 3)))), 1) 306 self.assertEqual(len(set(map(id, permutations('abcde', 3)))), 1)
311 self.assertNotEqual(len(set(map(id, list(permutations('abcde', 3))))), 1 ) 307 self.assertNotEqual(len(set(map(id, list(permutations('abcde', 3))))), 1 )
312 308
313 def test_combinatorics(self): 309 def test_combinatorics(self):
314 # Test relationships between product(), permutations(), 310 # Test relationships between product(), permutations(),
315 # combinations() and combinations_with_replacement(). 311 # combinations() and combinations_with_replacement().
316 312
317 for n in range(6): 313 for n in range(6):
318 s = 'ABCDEFG'[:n] 314 s = 'ABCDEFG'[:n]
319 for r in range(8): 315 for r in range(8):
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 # XXX This is rather silly now that builtin zip() calls zip()... 559 # XXX This is rather silly now that builtin zip() calls zip()...
564 ans = [(x,y) for x, y in zip('abc',count())] 560 ans = [(x,y) for x, y in zip('abc',count())]
565 self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)]) 561 self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
566 self.assertEqual(list(zip('abc', range(6))), lzip('abc', range(6))) 562 self.assertEqual(list(zip('abc', range(6))), lzip('abc', range(6)))
567 self.assertEqual(list(zip('abcdef', range(3))), lzip('abcdef', range(3)) ) 563 self.assertEqual(list(zip('abcdef', range(3))), lzip('abcdef', range(3)) )
568 self.assertEqual(take(3,zip('abcdef', count())), lzip('abcdef', range(3) )) 564 self.assertEqual(take(3,zip('abcdef', count())), lzip('abcdef', range(3) ))
569 self.assertEqual(list(zip('abcdef')), lzip('abcdef')) 565 self.assertEqual(list(zip('abcdef')), lzip('abcdef'))
570 self.assertEqual(list(zip()), lzip()) 566 self.assertEqual(list(zip()), lzip())
571 self.assertRaises(TypeError, zip, 3) 567 self.assertRaises(TypeError, zip, 3)
572 self.assertRaises(TypeError, zip, range(3), 3) 568 self.assertRaises(TypeError, zip, range(3), 3)
569 # Check tuple re-use (implementation detail)
573 self.assertEqual([tuple(list(pair)) for pair in zip('abc', 'def')], 570 self.assertEqual([tuple(list(pair)) for pair in zip('abc', 'def')],
574 lzip('abc', 'def')) 571 lzip('abc', 'def'))
575 self.assertEqual([pair for pair in zip('abc', 'def')], 572 self.assertEqual([pair for pair in zip('abc', 'def')],
576 lzip('abc', 'def')) 573 lzip('abc', 'def'))
577
578 @support.impl_detail("tuple reuse is specific to CPython")
579 def test_zip_tuple_reuse(self):
580 ids = list(map(id, zip('abc', 'def'))) 574 ids = list(map(id, zip('abc', 'def')))
581 self.assertEqual(min(ids), max(ids)) 575 self.assertEqual(min(ids), max(ids))
582 ids = list(map(id, list(zip('abc', 'def')))) 576 ids = list(map(id, list(zip('abc', 'def'))))
583 self.assertEqual(len(dict.fromkeys(ids)), len(ids)) 577 self.assertEqual(len(dict.fromkeys(ids)), len(ids))
584 578
585 def test_ziplongest(self): 579 def test_ziplongest(self):
586 for args in [ 580 for args in [
587 ['abc', range(6)], 581 ['abc', range(6)],
588 [range(6), 'abc'], 582 [range(6), 'abc'],
589 [range(1000), range(2000,2100), range(3000,3050)], 583 [range(1000), range(2000,2100), range(3000,3050)],
(...skipping 22 matching lines...) Expand all
612 "zip_longest('abc', fv=1)", 606 "zip_longest('abc', fv=1)",
613 "zip_longest('abc', fillvalue=1, bogus_keyword=None)", 607 "zip_longest('abc', fillvalue=1, bogus_keyword=None)",
614 ]: 608 ]:
615 try: 609 try:
616 eval(stmt, globals(), locals()) 610 eval(stmt, globals(), locals())
617 except TypeError: 611 except TypeError:
618 pass 612 pass
619 else: 613 else:
620 self.fail('Did not raise Type in: ' + stmt) 614 self.fail('Did not raise Type in: ' + stmt)
621 615
616 # Check tuple re-use (implementation detail)
622 self.assertEqual([tuple(list(pair)) for pair in zip_longest('abc', 'def' )], 617 self.assertEqual([tuple(list(pair)) for pair in zip_longest('abc', 'def' )],
623 list(zip('abc', 'def'))) 618 list(zip('abc', 'def')))
624 self.assertEqual([pair for pair in zip_longest('abc', 'def')], 619 self.assertEqual([pair for pair in zip_longest('abc', 'def')],
625 list(zip('abc', 'def'))) 620 list(zip('abc', 'def')))
626
627 @support.impl_detail("tuple reuse is specific to CPython")
628 def test_zip_longest_tuple_reuse(self):
629 ids = list(map(id, zip_longest('abc', 'def'))) 621 ids = list(map(id, zip_longest('abc', 'def')))
630 self.assertEqual(min(ids), max(ids)) 622 self.assertEqual(min(ids), max(ids))
631 ids = list(map(id, list(zip_longest('abc', 'def')))) 623 ids = list(map(id, list(zip_longest('abc', 'def'))))
632 self.assertEqual(len(dict.fromkeys(ids)), len(ids)) 624 self.assertEqual(len(dict.fromkeys(ids)), len(ids))
633 625
634 def test_bug_7244(self): 626 def test_bug_7244(self):
635 627
636 class Repeater: 628 class Repeater:
637 # this class is similar to itertools.repeat 629 # this class is similar to itertools.repeat
638 def __init__(self, o, t, e): 630 def __init__(self, o, t, e):
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
722 set('abcdefg'), range(11), tuple(range(13))] 714 set('abcdefg'), range(11), tuple(range(13))]
723 for i in range(100): 715 for i in range(100):
724 args = [random.choice(argtypes) for j in range(random.randrange(5))] 716 args = [random.choice(argtypes) for j in range(random.randrange(5))]
725 expected_len = prod(map(len, args)) 717 expected_len = prod(map(len, args))
726 self.assertEqual(len(list(product(*args))), expected_len) 718 self.assertEqual(len(list(product(*args))), expected_len)
727 self.assertEqual(list(product(*args)), list(product1(*args))) 719 self.assertEqual(list(product(*args)), list(product1(*args)))
728 self.assertEqual(list(product(*args)), list(product2(*args))) 720 self.assertEqual(list(product(*args)), list(product2(*args)))
729 args = map(iter, args) 721 args = map(iter, args)
730 self.assertEqual(len(list(product(*args))), expected_len) 722 self.assertEqual(len(list(product(*args))), expected_len)
731 723
732 @support.impl_detail("tuple reuse is specific to CPython") 724 # Test implementation detail: tuple re-use
733 def test_product_tuple_reuse(self):
734 self.assertEqual(len(set(map(id, product('abc', 'def')))), 1) 725 self.assertEqual(len(set(map(id, product('abc', 'def')))), 1)
735 self.assertNotEqual(len(set(map(id, list(product('abc', 'def'))))), 1) 726 self.assertNotEqual(len(set(map(id, list(product('abc', 'def'))))), 1)
736 727
737 def test_repeat(self): 728 def test_repeat(self):
738 self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a']) 729 self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a'])
739 self.assertEqual(lzip(range(3),repeat('a')), 730 self.assertEqual(lzip(range(3),repeat('a')),
740 [(0, 'a'), (1, 'a'), (2, 'a')]) 731 [(0, 'a'), (1, 'a'), (2, 'a')])
741 self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a']) 732 self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a'])
742 self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a']) 733 self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a'])
743 self.assertEqual(list(repeat('a', 0)), []) 734 self.assertEqual(list(repeat('a', 0)), [])
(...skipping 974 matching lines...) Expand 10 before | Expand all | Expand 10 after
1718 support.run_unittest(*test_classes) 1709 support.run_unittest(*test_classes)
1719 gc.collect() 1710 gc.collect()
1720 counts[i] = sys.gettotalrefcount() 1711 counts[i] = sys.gettotalrefcount()
1721 print(counts) 1712 print(counts)
1722 1713
1723 # doctest the examples in the library reference 1714 # doctest the examples in the library reference
1724 support.run_doctest(sys.modules[__name__], verbose) 1715 support.run_doctest(sys.modules[__name__], verbose)
1725 1716
1726 if __name__ == "__main__": 1717 if __name__ == "__main__":
1727 test_main(verbose=True) 1718 test_main(verbose=True)
OLDNEW
« no previous file with comments | « Lib/test/test_io.py ('k') | Lib/test/test_kqueue.py » ('j') | no next file with comments »

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