• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2from test import support
3from itertools import *
4import weakref
5from decimal import Decimal
6from fractions import Fraction
7import operator
8import random
9import copy
10import pickle
11from functools import reduce
12import sys
13import struct
14import threading
15maxsize = support.MAX_Py_ssize_t
16minsize = -maxsize-1
17
18def lzip(*args):
19    return list(zip(*args))
20
21def onearg(x):
22    'Test function of one argument'
23    return 2*x
24
25def errfunc(*args):
26    'Test function that raises an error'
27    raise ValueError
28
29def gen3():
30    'Non-restartable source sequence'
31    for i in (0, 1, 2):
32        yield i
33
34def isEven(x):
35    'Test predicate'
36    return x%2==0
37
38def isOdd(x):
39    'Test predicate'
40    return x%2==1
41
42def tupleize(*args):
43    return args
44
45def irange(n):
46    for i in range(n):
47        yield i
48
49class StopNow:
50    'Class emulating an empty iterable.'
51    def __iter__(self):
52        return self
53    def __next__(self):
54        raise StopIteration
55
56def take(n, seq):
57    'Convenience function for partially consuming a long of infinite iterable'
58    return list(islice(seq, n))
59
60def prod(iterable):
61    return reduce(operator.mul, iterable, 1)
62
63def fact(n):
64    'Factorial'
65    return prod(range(1, n+1))
66
67# root level methods for pickling ability
68def testR(r):
69    return r[0]
70
71def testR2(r):
72    return r[2]
73
74def underten(x):
75    return x<10
76
77picklecopiers = [lambda s, proto=proto: pickle.loads(pickle.dumps(s, proto))
78                 for proto in range(pickle.HIGHEST_PROTOCOL + 1)]
79
80class TestBasicOps(unittest.TestCase):
81
82    def pickletest(self, protocol, it, stop=4, take=1, compare=None):
83        """Test that an iterator is the same after pickling, also when part-consumed"""
84        def expand(it, i=0):
85            # Recursively expand iterables, within sensible bounds
86            if i > 10:
87                raise RuntimeError("infinite recursion encountered")
88            if isinstance(it, str):
89                return it
90            try:
91                l = list(islice(it, stop))
92            except TypeError:
93                return it # can't expand it
94            return [expand(e, i+1) for e in l]
95
96        # Test the initial copy against the original
97        dump = pickle.dumps(it, protocol)
98        i2 = pickle.loads(dump)
99        self.assertEqual(type(it), type(i2))
100        a, b = expand(it), expand(i2)
101        self.assertEqual(a, b)
102        if compare:
103            c = expand(compare)
104            self.assertEqual(a, c)
105
106        # Take from the copy, and create another copy and compare them.
107        i3 = pickle.loads(dump)
108        took = 0
109        try:
110            for i in range(take):
111                next(i3)
112                took += 1
113        except StopIteration:
114            pass #in case there is less data than 'take'
115        dump = pickle.dumps(i3, protocol)
116        i4 = pickle.loads(dump)
117        a, b = expand(i3), expand(i4)
118        self.assertEqual(a, b)
119        if compare:
120            c = expand(compare[took:])
121            self.assertEqual(a, c);
122
123    def test_accumulate(self):
124        self.assertEqual(list(accumulate(range(10))),               # one positional arg
125                          [0, 1, 3, 6, 10, 15, 21, 28, 36, 45])
126        self.assertEqual(list(accumulate(iterable=range(10))),      # kw arg
127                          [0, 1, 3, 6, 10, 15, 21, 28, 36, 45])
128        for typ in int, complex, Decimal, Fraction:                 # multiple types
129            self.assertEqual(
130                list(accumulate(map(typ, range(10)))),
131                list(map(typ, [0, 1, 3, 6, 10, 15, 21, 28, 36, 45])))
132        self.assertEqual(list(accumulate('abc')), ['a', 'ab', 'abc'])   # works with non-numeric
133        self.assertEqual(list(accumulate([])), [])                  # empty iterable
134        self.assertEqual(list(accumulate([7])), [7])                # iterable of length one
135        self.assertRaises(TypeError, accumulate, range(10), 5, 6)   # too many args
136        self.assertRaises(TypeError, accumulate)                    # too few args
137        self.assertRaises(TypeError, accumulate, x=range(10))       # unexpected kwd arg
138        self.assertRaises(TypeError, list, accumulate([1, []]))     # args that don't add
139
140        s = [2, 8, 9, 5, 7, 0, 3, 4, 1, 6]
141        self.assertEqual(list(accumulate(s, min)),
142                         [2, 2, 2, 2, 2, 0, 0, 0, 0, 0])
143        self.assertEqual(list(accumulate(s, max)),
144                         [2, 8, 9, 9, 9, 9, 9, 9, 9, 9])
145        self.assertEqual(list(accumulate(s, operator.mul)),
146                         [2, 16, 144, 720, 5040, 0, 0, 0, 0, 0])
147        with self.assertRaises(TypeError):
148            list(accumulate(s, chr))                                # unary-operation
149        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
150            self.pickletest(proto, accumulate(range(10)))           # test pickling
151            self.pickletest(proto, accumulate(range(10), initial=7))
152        self.assertEqual(list(accumulate([10, 5, 1], initial=None)), [10, 15, 16])
153        self.assertEqual(list(accumulate([10, 5, 1], initial=100)), [100, 110, 115, 116])
154        self.assertEqual(list(accumulate([], initial=100)), [100])
155        with self.assertRaises(TypeError):
156            list(accumulate([10, 20], 100))
157
158    def test_chain(self):
159
160        def chain2(*iterables):
161            'Pure python version in the docs'
162            for it in iterables:
163                for element in it:
164                    yield element
165
166        for c in (chain, chain2):
167            self.assertEqual(list(c('abc', 'def')), list('abcdef'))
168            self.assertEqual(list(c('abc')), list('abc'))
169            self.assertEqual(list(c('')), [])
170            self.assertEqual(take(4, c('abc', 'def')), list('abcd'))
171            self.assertRaises(TypeError, list,c(2, 3))
172
173    def test_chain_from_iterable(self):
174        self.assertEqual(list(chain.from_iterable(['abc', 'def'])), list('abcdef'))
175        self.assertEqual(list(chain.from_iterable(['abc'])), list('abc'))
176        self.assertEqual(list(chain.from_iterable([''])), [])
177        self.assertEqual(take(4, chain.from_iterable(['abc', 'def'])), list('abcd'))
178        self.assertRaises(TypeError, list, chain.from_iterable([2, 3]))
179
180    def test_chain_reducible(self):
181        for oper in [copy.deepcopy] + picklecopiers:
182            it = chain('abc', 'def')
183            self.assertEqual(list(oper(it)), list('abcdef'))
184            self.assertEqual(next(it), 'a')
185            self.assertEqual(list(oper(it)), list('bcdef'))
186
187            self.assertEqual(list(oper(chain(''))), [])
188            self.assertEqual(take(4, oper(chain('abc', 'def'))), list('abcd'))
189            self.assertRaises(TypeError, list, oper(chain(2, 3)))
190        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
191            self.pickletest(proto, chain('abc', 'def'), compare=list('abcdef'))
192
193    def test_chain_setstate(self):
194        self.assertRaises(TypeError, chain().__setstate__, ())
195        self.assertRaises(TypeError, chain().__setstate__, [])
196        self.assertRaises(TypeError, chain().__setstate__, 0)
197        self.assertRaises(TypeError, chain().__setstate__, ([],))
198        self.assertRaises(TypeError, chain().__setstate__, (iter([]), []))
199        it = chain()
200        it.__setstate__((iter(['abc', 'def']),))
201        self.assertEqual(list(it), ['a', 'b', 'c', 'd', 'e', 'f'])
202        it = chain()
203        it.__setstate__((iter(['abc', 'def']), iter(['ghi'])))
204        self.assertEqual(list(it), ['ghi', 'a', 'b', 'c', 'd', 'e', 'f'])
205
206    def test_combinations(self):
207        self.assertRaises(TypeError, combinations, 'abc')       # missing r argument
208        self.assertRaises(TypeError, combinations, 'abc', 2, 1) # too many arguments
209        self.assertRaises(TypeError, combinations, None)        # pool is not iterable
210        self.assertRaises(ValueError, combinations, 'abc', -2)  # r is negative
211
212        for op in [lambda a:a] + picklecopiers:
213            self.assertEqual(list(op(combinations('abc', 32))), [])     # r > n
214
215            self.assertEqual(list(op(combinations('ABCD', 2))),
216                             [('A','B'), ('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')])
217            testIntermediate = combinations('ABCD', 2)
218            next(testIntermediate)
219            self.assertEqual(list(op(testIntermediate)),
220                             [('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')])
221
222            self.assertEqual(list(op(combinations(range(4), 3))),
223                             [(0,1,2), (0,1,3), (0,2,3), (1,2,3)])
224            testIntermediate = combinations(range(4), 3)
225            next(testIntermediate)
226            self.assertEqual(list(op(testIntermediate)),
227                             [(0,1,3), (0,2,3), (1,2,3)])
228
229
230        def combinations1(iterable, r):
231            'Pure python version shown in the docs'
232            pool = tuple(iterable)
233            n = len(pool)
234            if r > n:
235                return
236            indices = list(range(r))
237            yield tuple(pool[i] for i in indices)
238            while 1:
239                for i in reversed(range(r)):
240                    if indices[i] != i + n - r:
241                        break
242                else:
243                    return
244                indices[i] += 1
245                for j in range(i+1, r):
246                    indices[j] = indices[j-1] + 1
247                yield tuple(pool[i] for i in indices)
248
249        def combinations2(iterable, r):
250            'Pure python version shown in the docs'
251            pool = tuple(iterable)
252            n = len(pool)
253            for indices in permutations(range(n), r):
254                if sorted(indices) == list(indices):
255                    yield tuple(pool[i] for i in indices)
256
257        def combinations3(iterable, r):
258            'Pure python version from cwr()'
259            pool = tuple(iterable)
260            n = len(pool)
261            for indices in combinations_with_replacement(range(n), r):
262                if len(set(indices)) == r:
263                    yield tuple(pool[i] for i in indices)
264
265        for n in range(7):
266            values = [5*x-12 for x in range(n)]
267            for r in range(n+2):
268                result = list(combinations(values, r))
269                self.assertEqual(len(result), 0 if r>n else fact(n) / fact(r) / fact(n-r)) # right number of combs
270                self.assertEqual(len(result), len(set(result)))         # no repeats
271                self.assertEqual(result, sorted(result))                # lexicographic order
272                for c in result:
273                    self.assertEqual(len(c), r)                         # r-length combinations
274                    self.assertEqual(len(set(c)), r)                    # no duplicate elements
275                    self.assertEqual(list(c), sorted(c))                # keep original ordering
276                    self.assertTrue(all(e in values for e in c))           # elements taken from input iterable
277                    self.assertEqual(list(c),
278                                     [e for e in values if e in c])      # comb is a subsequence of the input iterable
279                self.assertEqual(result, list(combinations1(values, r))) # matches first pure python version
280                self.assertEqual(result, list(combinations2(values, r))) # matches second pure python version
281                self.assertEqual(result, list(combinations3(values, r))) # matches second pure python version
282
283                for proto in range(pickle.HIGHEST_PROTOCOL + 1):
284                    self.pickletest(proto, combinations(values, r))      # test pickling
285
286    @support.bigaddrspacetest
287    def test_combinations_overflow(self):
288        with self.assertRaises((OverflowError, MemoryError)):
289            combinations("AA", 2**29)
290
291        # Test implementation detail:  tuple re-use
292    @support.impl_detail("tuple reuse is specific to CPython")
293    def test_combinations_tuple_reuse(self):
294        self.assertEqual(len(set(map(id, combinations('abcde', 3)))), 1)
295        self.assertNotEqual(len(set(map(id, list(combinations('abcde', 3))))), 1)
296
297    def test_combinations_with_replacement(self):
298        cwr = combinations_with_replacement
299        self.assertRaises(TypeError, cwr, 'abc')       # missing r argument
300        self.assertRaises(TypeError, cwr, 'abc', 2, 1) # too many arguments
301        self.assertRaises(TypeError, cwr, None)        # pool is not iterable
302        self.assertRaises(ValueError, cwr, 'abc', -2)  # r is negative
303
304        for op in [lambda a:a] + picklecopiers:
305            self.assertEqual(list(op(cwr('ABC', 2))),
306                             [('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')])
307            testIntermediate = cwr('ABC', 2)
308            next(testIntermediate)
309            self.assertEqual(list(op(testIntermediate)),
310                             [('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')])
311
312
313        def cwr1(iterable, r):
314            'Pure python version shown in the docs'
315            # number items returned:  (n+r-1)! / r! / (n-1)! when n>0
316            pool = tuple(iterable)
317            n = len(pool)
318            if not n and r:
319                return
320            indices = [0] * r
321            yield tuple(pool[i] for i in indices)
322            while 1:
323                for i in reversed(range(r)):
324                    if indices[i] != n - 1:
325                        break
326                else:
327                    return
328                indices[i:] = [indices[i] + 1] * (r - i)
329                yield tuple(pool[i] for i in indices)
330
331        def cwr2(iterable, r):
332            'Pure python version shown in the docs'
333            pool = tuple(iterable)
334            n = len(pool)
335            for indices in product(range(n), repeat=r):
336                if sorted(indices) == list(indices):
337                    yield tuple(pool[i] for i in indices)
338
339        def numcombs(n, r):
340            if not n:
341                return 0 if r else 1
342            return fact(n+r-1) / fact(r)/ fact(n-1)
343
344        for n in range(7):
345            values = [5*x-12 for x in range(n)]
346            for r in range(n+2):
347                result = list(cwr(values, r))
348
349                self.assertEqual(len(result), numcombs(n, r))           # right number of combs
350                self.assertEqual(len(result), len(set(result)))         # no repeats
351                self.assertEqual(result, sorted(result))                # lexicographic order
352
353                regular_combs = list(combinations(values, r))           # compare to combs without replacement
354                if n == 0 or r <= 1:
355                    self.assertEqual(result, regular_combs)            # cases that should be identical
356                else:
357                    self.assertTrue(set(result) >= set(regular_combs))     # rest should be supersets of regular combs
358
359                for c in result:
360                    self.assertEqual(len(c), r)                         # r-length combinations
361                    noruns = [k for k,v in groupby(c)]                  # combo without consecutive repeats
362                    self.assertEqual(len(noruns), len(set(noruns)))     # no repeats other than consecutive
363                    self.assertEqual(list(c), sorted(c))                # keep original ordering
364                    self.assertTrue(all(e in values for e in c))           # elements taken from input iterable
365                    self.assertEqual(noruns,
366                                     [e for e in values if e in c])     # comb is a subsequence of the input iterable
367                self.assertEqual(result, list(cwr1(values, r)))         # matches first pure python version
368                self.assertEqual(result, list(cwr2(values, r)))         # matches second pure python version
369
370                for proto in range(pickle.HIGHEST_PROTOCOL + 1):
371                    self.pickletest(proto, cwr(values,r))               # test pickling
372
373    @support.bigaddrspacetest
374    def test_combinations_with_replacement_overflow(self):
375        with self.assertRaises((OverflowError, MemoryError)):
376            combinations_with_replacement("AA", 2**30)
377
378        # Test implementation detail:  tuple re-use
379    @support.impl_detail("tuple reuse is specific to CPython")
380    def test_combinations_with_replacement_tuple_reuse(self):
381        cwr = combinations_with_replacement
382        self.assertEqual(len(set(map(id, cwr('abcde', 3)))), 1)
383        self.assertNotEqual(len(set(map(id, list(cwr('abcde', 3))))), 1)
384
385    def test_permutations(self):
386        self.assertRaises(TypeError, permutations)              # too few arguments
387        self.assertRaises(TypeError, permutations, 'abc', 2, 1) # too many arguments
388        self.assertRaises(TypeError, permutations, None)        # pool is not iterable
389        self.assertRaises(ValueError, permutations, 'abc', -2)  # r is negative
390        self.assertEqual(list(permutations('abc', 32)), [])     # r > n
391        self.assertRaises(TypeError, permutations, 'abc', 's')  # r is not an int or None
392        self.assertEqual(list(permutations(range(3), 2)),
393                                           [(0,1), (0,2), (1,0), (1,2), (2,0), (2,1)])
394
395        def permutations1(iterable, r=None):
396            'Pure python version shown in the docs'
397            pool = tuple(iterable)
398            n = len(pool)
399            r = n if r is None else r
400            if r > n:
401                return
402            indices = list(range(n))
403            cycles = list(range(n-r+1, n+1))[::-1]
404            yield tuple(pool[i] for i in indices[:r])
405            while n:
406                for i in reversed(range(r)):
407                    cycles[i] -= 1
408                    if cycles[i] == 0:
409                        indices[i:] = indices[i+1:] + indices[i:i+1]
410                        cycles[i] = n - i
411                    else:
412                        j = cycles[i]
413                        indices[i], indices[-j] = indices[-j], indices[i]
414                        yield tuple(pool[i] for i in indices[:r])
415                        break
416                else:
417                    return
418
419        def permutations2(iterable, r=None):
420            'Pure python version shown in the docs'
421            pool = tuple(iterable)
422            n = len(pool)
423            r = n if r is None else r
424            for indices in product(range(n), repeat=r):
425                if len(set(indices)) == r:
426                    yield tuple(pool[i] for i in indices)
427
428        for n in range(7):
429            values = [5*x-12 for x in range(n)]
430            for r in range(n+2):
431                result = list(permutations(values, r))
432                self.assertEqual(len(result), 0 if r>n else fact(n) / fact(n-r))      # right number of perms
433                self.assertEqual(len(result), len(set(result)))         # no repeats
434                self.assertEqual(result, sorted(result))                # lexicographic order
435                for p in result:
436                    self.assertEqual(len(p), r)                         # r-length permutations
437                    self.assertEqual(len(set(p)), r)                    # no duplicate elements
438                    self.assertTrue(all(e in values for e in p))           # elements taken from input iterable
439                self.assertEqual(result, list(permutations1(values, r))) # matches first pure python version
440                self.assertEqual(result, list(permutations2(values, r))) # matches second pure python version
441                if r == n:
442                    self.assertEqual(result, list(permutations(values, None))) # test r as None
443                    self.assertEqual(result, list(permutations(values)))       # test default r
444
445                for proto in range(pickle.HIGHEST_PROTOCOL + 1):
446                    self.pickletest(proto, permutations(values, r))     # test pickling
447
448    @support.bigaddrspacetest
449    def test_permutations_overflow(self):
450        with self.assertRaises((OverflowError, MemoryError)):
451            permutations("A", 2**30)
452
453    @support.impl_detail("tuple reuse is specific to CPython")
454    def test_permutations_tuple_reuse(self):
455        self.assertEqual(len(set(map(id, permutations('abcde', 3)))), 1)
456        self.assertNotEqual(len(set(map(id, list(permutations('abcde', 3))))), 1)
457
458    def test_combinatorics(self):
459        # Test relationships between product(), permutations(),
460        # combinations() and combinations_with_replacement().
461
462        for n in range(6):
463            s = 'ABCDEFG'[:n]
464            for r in range(8):
465                prod = list(product(s, repeat=r))
466                cwr = list(combinations_with_replacement(s, r))
467                perm = list(permutations(s, r))
468                comb = list(combinations(s, r))
469
470                # Check size
471                self.assertEqual(len(prod), n**r)
472                self.assertEqual(len(cwr), (fact(n+r-1) / fact(r)/ fact(n-1)) if n else (not r))
473                self.assertEqual(len(perm), 0 if r>n else fact(n) / fact(n-r))
474                self.assertEqual(len(comb), 0 if r>n else fact(n) / fact(r) / fact(n-r))
475
476                # Check lexicographic order without repeated tuples
477                self.assertEqual(prod, sorted(set(prod)))
478                self.assertEqual(cwr, sorted(set(cwr)))
479                self.assertEqual(perm, sorted(set(perm)))
480                self.assertEqual(comb, sorted(set(comb)))
481
482                # Check interrelationships
483                self.assertEqual(cwr, [t for t in prod if sorted(t)==list(t)]) # cwr: prods which are sorted
484                self.assertEqual(perm, [t for t in prod if len(set(t))==r])    # perm: prods with no dups
485                self.assertEqual(comb, [t for t in perm if sorted(t)==list(t)]) # comb: perms that are sorted
486                self.assertEqual(comb, [t for t in cwr if len(set(t))==r])      # comb: cwrs without dups
487                self.assertEqual(comb, list(filter(set(cwr).__contains__, perm)))     # comb: perm that is a cwr
488                self.assertEqual(comb, list(filter(set(perm).__contains__, cwr)))     # comb: cwr that is a perm
489                self.assertEqual(comb, sorted(set(cwr) & set(perm)))            # comb: both a cwr and a perm
490
491    def test_compress(self):
492        self.assertEqual(list(compress(data='ABCDEF', selectors=[1,0,1,0,1,1])), list('ACEF'))
493        self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF'))
494        self.assertEqual(list(compress('ABCDEF', [0,0,0,0,0,0])), list(''))
495        self.assertEqual(list(compress('ABCDEF', [1,1,1,1,1,1])), list('ABCDEF'))
496        self.assertEqual(list(compress('ABCDEF', [1,0,1])), list('AC'))
497        self.assertEqual(list(compress('ABC', [0,1,1,1,1,1])), list('BC'))
498        n = 10000
499        data = chain.from_iterable(repeat(range(6), n))
500        selectors = chain.from_iterable(repeat((0, 1)))
501        self.assertEqual(list(compress(data, selectors)), [1,3,5] * n)
502        self.assertRaises(TypeError, compress, None, range(6))      # 1st arg not iterable
503        self.assertRaises(TypeError, compress, range(6), None)      # 2nd arg not iterable
504        self.assertRaises(TypeError, compress, range(6))            # too few args
505        self.assertRaises(TypeError, compress, range(6), None)      # too many args
506
507        # check copy, deepcopy, pickle
508        for op in [lambda a:copy.copy(a), lambda a:copy.deepcopy(a)] + picklecopiers:
509            for data, selectors, result1, result2 in [
510                ('ABCDEF', [1,0,1,0,1,1], 'ACEF', 'CEF'),
511                ('ABCDEF', [0,0,0,0,0,0], '', ''),
512                ('ABCDEF', [1,1,1,1,1,1], 'ABCDEF', 'BCDEF'),
513                ('ABCDEF', [1,0,1], 'AC', 'C'),
514                ('ABC', [0,1,1,1,1,1], 'BC', 'C'),
515                ]:
516
517                self.assertEqual(list(op(compress(data=data, selectors=selectors))), list(result1))
518                self.assertEqual(list(op(compress(data, selectors))), list(result1))
519                testIntermediate = compress(data, selectors)
520                if result1:
521                    next(testIntermediate)
522                    self.assertEqual(list(op(testIntermediate)), list(result2))
523
524
525    def test_count(self):
526        self.assertEqual(lzip('abc',count()), [('a', 0), ('b', 1), ('c', 2)])
527        self.assertEqual(lzip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)])
528        self.assertEqual(take(2, lzip('abc',count(3))), [('a', 3), ('b', 4)])
529        self.assertEqual(take(2, zip('abc',count(-1))), [('a', -1), ('b', 0)])
530        self.assertEqual(take(2, zip('abc',count(-3))), [('a', -3), ('b', -2)])
531        self.assertRaises(TypeError, count, 2, 3, 4)
532        self.assertRaises(TypeError, count, 'a')
533        self.assertEqual(take(10, count(maxsize-5)),
534                         list(range(maxsize-5, maxsize+5)))
535        self.assertEqual(take(10, count(-maxsize-5)),
536                         list(range(-maxsize-5, -maxsize+5)))
537        self.assertEqual(take(3, count(3.25)), [3.25, 4.25, 5.25])
538        self.assertEqual(take(3, count(3.25-4j)), [3.25-4j, 4.25-4j, 5.25-4j])
539        self.assertEqual(take(3, count(Decimal('1.1'))),
540                         [Decimal('1.1'), Decimal('2.1'), Decimal('3.1')])
541        self.assertEqual(take(3, count(Fraction(2, 3))),
542                         [Fraction(2, 3), Fraction(5, 3), Fraction(8, 3)])
543        BIGINT = 1<<1000
544        self.assertEqual(take(3, count(BIGINT)), [BIGINT, BIGINT+1, BIGINT+2])
545        c = count(3)
546        self.assertEqual(repr(c), 'count(3)')
547        next(c)
548        self.assertEqual(repr(c), 'count(4)')
549        c = count(-9)
550        self.assertEqual(repr(c), 'count(-9)')
551        next(c)
552        self.assertEqual(next(c), -8)
553        self.assertEqual(repr(count(10.25)), 'count(10.25)')
554        self.assertEqual(repr(count(10.0)), 'count(10.0)')
555        self.assertEqual(type(next(count(10.0))), float)
556        for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5):
557            # Test repr
558            r1 = repr(count(i))
559            r2 = 'count(%r)'.__mod__(i)
560            self.assertEqual(r1, r2)
561
562        # check copy, deepcopy, pickle
563        for value in -3, 3, maxsize-5, maxsize+5:
564            c = count(value)
565            self.assertEqual(next(copy.copy(c)), value)
566            self.assertEqual(next(copy.deepcopy(c)), value)
567            for proto in range(pickle.HIGHEST_PROTOCOL + 1):
568                self.pickletest(proto, count(value))
569
570        #check proper internal error handling for large "step' sizes
571        count(1, maxsize+5); sys.exc_info()
572
573    def test_count_with_stride(self):
574        self.assertEqual(lzip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)])
575        self.assertEqual(lzip('abc',count(start=2,step=3)),
576                         [('a', 2), ('b', 5), ('c', 8)])
577        self.assertEqual(lzip('abc',count(step=-1)),
578                         [('a', 0), ('b', -1), ('c', -2)])
579        self.assertRaises(TypeError, count, 'a', 'b')
580        self.assertEqual(lzip('abc',count(2,0)), [('a', 2), ('b', 2), ('c', 2)])
581        self.assertEqual(lzip('abc',count(2,1)), [('a', 2), ('b', 3), ('c', 4)])
582        self.assertEqual(lzip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)])
583        self.assertEqual(take(20, count(maxsize-15, 3)), take(20, range(maxsize-15, maxsize+100, 3)))
584        self.assertEqual(take(20, count(-maxsize-15, 3)), take(20, range(-maxsize-15,-maxsize+100, 3)))
585        self.assertEqual(take(3, count(10, maxsize+5)),
586                         list(range(10, 10+3*(maxsize+5), maxsize+5)))
587        self.assertEqual(take(3, count(2, 1.25)), [2, 3.25, 4.5])
588        self.assertEqual(take(3, count(2, 3.25-4j)), [2, 5.25-4j, 8.5-8j])
589        self.assertEqual(take(3, count(Decimal('1.1'), Decimal('.1'))),
590                         [Decimal('1.1'), Decimal('1.2'), Decimal('1.3')])
591        self.assertEqual(take(3, count(Fraction(2,3), Fraction(1,7))),
592                         [Fraction(2,3), Fraction(17,21), Fraction(20,21)])
593        BIGINT = 1<<1000
594        self.assertEqual(take(3, count(step=BIGINT)), [0, BIGINT, 2*BIGINT])
595        self.assertEqual(repr(take(3, count(10, 2.5))), repr([10, 12.5, 15.0]))
596        c = count(3, 5)
597        self.assertEqual(repr(c), 'count(3, 5)')
598        next(c)
599        self.assertEqual(repr(c), 'count(8, 5)')
600        c = count(-9, 0)
601        self.assertEqual(repr(c), 'count(-9, 0)')
602        next(c)
603        self.assertEqual(repr(c), 'count(-9, 0)')
604        c = count(-9, -3)
605        self.assertEqual(repr(c), 'count(-9, -3)')
606        next(c)
607        self.assertEqual(repr(c), 'count(-12, -3)')
608        self.assertEqual(repr(c), 'count(-12, -3)')
609        self.assertEqual(repr(count(10.5, 1.25)), 'count(10.5, 1.25)')
610        self.assertEqual(repr(count(10.5, 1)), 'count(10.5)')           # suppress step=1 when it's an int
611        self.assertEqual(repr(count(10.5, 1.00)), 'count(10.5, 1.0)')   # do show float values lilke 1.0
612        self.assertEqual(repr(count(10, 1.00)), 'count(10, 1.0)')
613        c = count(10, 1.0)
614        self.assertEqual(type(next(c)), int)
615        self.assertEqual(type(next(c)), float)
616        for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5):
617            for j in  (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 1, 10, sys.maxsize-5, sys.maxsize+5):
618                # Test repr
619                r1 = repr(count(i, j))
620                if j == 1:
621                    r2 = ('count(%r)' % i)
622                else:
623                    r2 = ('count(%r, %r)' % (i, j))
624                self.assertEqual(r1, r2)
625                for proto in range(pickle.HIGHEST_PROTOCOL + 1):
626                    self.pickletest(proto, count(i, j))
627
628    def test_cycle(self):
629        self.assertEqual(take(10, cycle('abc')), list('abcabcabca'))
630        self.assertEqual(list(cycle('')), [])
631        self.assertRaises(TypeError, cycle)
632        self.assertRaises(TypeError, cycle, 5)
633        self.assertEqual(list(islice(cycle(gen3()),10)), [0,1,2,0,1,2,0,1,2,0])
634
635        # check copy, deepcopy, pickle
636        c = cycle('abc')
637        self.assertEqual(next(c), 'a')
638        #simple copy currently not supported, because __reduce__ returns
639        #an internal iterator
640        #self.assertEqual(take(10, copy.copy(c)), list('bcabcabcab'))
641        self.assertEqual(take(10, copy.deepcopy(c)), list('bcabcabcab'))
642        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
643            self.assertEqual(take(10, pickle.loads(pickle.dumps(c, proto))),
644                             list('bcabcabcab'))
645            next(c)
646            self.assertEqual(take(10, pickle.loads(pickle.dumps(c, proto))),
647                             list('cabcabcabc'))
648            next(c)
649            next(c)
650        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
651            self.pickletest(proto, cycle('abc'))
652
653        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
654            # test with partial consumed input iterable
655            it = iter('abcde')
656            c = cycle(it)
657            _ = [next(c) for i in range(2)]      # consume 2 of 5 inputs
658            p = pickle.dumps(c, proto)
659            d = pickle.loads(p)                  # rebuild the cycle object
660            self.assertEqual(take(20, d), list('cdeabcdeabcdeabcdeab'))
661
662            # test with completely consumed input iterable
663            it = iter('abcde')
664            c = cycle(it)
665            _ = [next(c) for i in range(7)]      # consume 7 of 5 inputs
666            p = pickle.dumps(c, proto)
667            d = pickle.loads(p)                  # rebuild the cycle object
668            self.assertEqual(take(20, d), list('cdeabcdeabcdeabcdeab'))
669
670    def test_cycle_setstate(self):
671        # Verify both modes for restoring state
672
673        # Mode 0 is efficient.  It uses an incompletely consumed input
674        # iterator to build a cycle object and then passes in state with
675        # a list of previously consumed values.  There is no data
676        # overlap between the two.
677        c = cycle('defg')
678        c.__setstate__((list('abc'), 0))
679        self.assertEqual(take(20, c), list('defgabcdefgabcdefgab'))
680
681        # Mode 1 is inefficient.  It starts with a cycle object built
682        # from an iterator over the remaining elements in a partial
683        # cycle and then passes in state with all of the previously
684        # seen values (this overlaps values included in the iterator).
685        c = cycle('defg')
686        c.__setstate__((list('abcdefg'), 1))
687        self.assertEqual(take(20, c), list('defgabcdefgabcdefgab'))
688
689        # The first argument to setstate needs to be a tuple
690        with self.assertRaises(TypeError):
691            cycle('defg').__setstate__([list('abcdefg'), 0])
692
693        # The first argument in the setstate tuple must be a list
694        with self.assertRaises(TypeError):
695            c = cycle('defg')
696            c.__setstate__((tuple('defg'), 0))
697        take(20, c)
698
699        # The second argument in the setstate tuple must be an int
700        with self.assertRaises(TypeError):
701            cycle('defg').__setstate__((list('abcdefg'), 'x'))
702
703        self.assertRaises(TypeError, cycle('').__setstate__, ())
704        self.assertRaises(TypeError, cycle('').__setstate__, ([],))
705
706    def test_groupby(self):
707        # Check whether it accepts arguments correctly
708        self.assertEqual([], list(groupby([])))
709        self.assertEqual([], list(groupby([], key=id)))
710        self.assertRaises(TypeError, list, groupby('abc', []))
711        self.assertRaises(TypeError, groupby, None)
712        self.assertRaises(TypeError, groupby, 'abc', lambda x:x, 10)
713
714        # Check normal input
715        s = [(0, 10, 20), (0, 11,21), (0,12,21), (1,13,21), (1,14,22),
716             (2,15,22), (3,16,23), (3,17,23)]
717        dup = []
718        for k, g in groupby(s, lambda r:r[0]):
719            for elem in g:
720                self.assertEqual(k, elem[0])
721                dup.append(elem)
722        self.assertEqual(s, dup)
723
724        # Check normal pickled
725        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
726            dup = []
727            for k, g in pickle.loads(pickle.dumps(groupby(s, testR), proto)):
728                for elem in g:
729                    self.assertEqual(k, elem[0])
730                    dup.append(elem)
731            self.assertEqual(s, dup)
732
733        # Check nested case
734        dup = []
735        for k, g in groupby(s, testR):
736            for ik, ig in groupby(g, testR2):
737                for elem in ig:
738                    self.assertEqual(k, elem[0])
739                    self.assertEqual(ik, elem[2])
740                    dup.append(elem)
741        self.assertEqual(s, dup)
742
743        # Check nested and pickled
744        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
745            dup = []
746            for k, g in pickle.loads(pickle.dumps(groupby(s, testR), proto)):
747                for ik, ig in pickle.loads(pickle.dumps(groupby(g, testR2), proto)):
748                    for elem in ig:
749                        self.assertEqual(k, elem[0])
750                        self.assertEqual(ik, elem[2])
751                        dup.append(elem)
752            self.assertEqual(s, dup)
753
754
755        # Check case where inner iterator is not used
756        keys = [k for k, g in groupby(s, testR)]
757        expectedkeys = set([r[0] for r in s])
758        self.assertEqual(set(keys), expectedkeys)
759        self.assertEqual(len(keys), len(expectedkeys))
760
761        # Check case where inner iterator is used after advancing the groupby
762        # iterator
763        s = list(zip('AABBBAAAA', range(9)))
764        it = groupby(s, testR)
765        _, g1 = next(it)
766        _, g2 = next(it)
767        _, g3 = next(it)
768        self.assertEqual(list(g1), [])
769        self.assertEqual(list(g2), [])
770        self.assertEqual(next(g3), ('A', 5))
771        list(it)  # exhaust the groupby iterator
772        self.assertEqual(list(g3), [])
773
774        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
775            it = groupby(s, testR)
776            _, g = next(it)
777            next(it)
778            next(it)
779            self.assertEqual(list(pickle.loads(pickle.dumps(g, proto))), [])
780
781        # Exercise pipes and filters style
782        s = 'abracadabra'
783        # sort s | uniq
784        r = [k for k, g in groupby(sorted(s))]
785        self.assertEqual(r, ['a', 'b', 'c', 'd', 'r'])
786        # sort s | uniq -d
787        r = [k for k, g in groupby(sorted(s)) if list(islice(g,1,2))]
788        self.assertEqual(r, ['a', 'b', 'r'])
789        # sort s | uniq -c
790        r = [(len(list(g)), k) for k, g in groupby(sorted(s))]
791        self.assertEqual(r, [(5, 'a'), (2, 'b'), (1, 'c'), (1, 'd'), (2, 'r')])
792        # sort s | uniq -c | sort -rn | head -3
793        r = sorted([(len(list(g)) , k) for k, g in groupby(sorted(s))], reverse=True)[:3]
794        self.assertEqual(r, [(5, 'a'), (2, 'r'), (2, 'b')])
795
796        # iter.__next__ failure
797        class ExpectedError(Exception):
798            pass
799        def delayed_raise(n=0):
800            for i in range(n):
801                yield 'yo'
802            raise ExpectedError
803        def gulp(iterable, keyp=None, func=list):
804            return [func(g) for k, g in groupby(iterable, keyp)]
805
806        # iter.__next__ failure on outer object
807        self.assertRaises(ExpectedError, gulp, delayed_raise(0))
808        # iter.__next__ failure on inner object
809        self.assertRaises(ExpectedError, gulp, delayed_raise(1))
810
811        # __eq__ failure
812        class DummyCmp:
813            def __eq__(self, dst):
814                raise ExpectedError
815        s = [DummyCmp(), DummyCmp(), None]
816
817        # __eq__ failure on outer object
818        self.assertRaises(ExpectedError, gulp, s, func=id)
819        # __eq__ failure on inner object
820        self.assertRaises(ExpectedError, gulp, s)
821
822        # keyfunc failure
823        def keyfunc(obj):
824            if keyfunc.skip > 0:
825                keyfunc.skip -= 1
826                return obj
827            else:
828                raise ExpectedError
829
830        # keyfunc failure on outer object
831        keyfunc.skip = 0
832        self.assertRaises(ExpectedError, gulp, [None], keyfunc)
833        keyfunc.skip = 1
834        self.assertRaises(ExpectedError, gulp, [None, None], keyfunc)
835
836    def test_filter(self):
837        self.assertEqual(list(filter(isEven, range(6))), [0,2,4])
838        self.assertEqual(list(filter(None, [0,1,0,2,0])), [1,2])
839        self.assertEqual(list(filter(bool, [0,1,0,2,0])), [1,2])
840        self.assertEqual(take(4, filter(isEven, count())), [0,2,4,6])
841        self.assertRaises(TypeError, filter)
842        self.assertRaises(TypeError, filter, lambda x:x)
843        self.assertRaises(TypeError, filter, lambda x:x, range(6), 7)
844        self.assertRaises(TypeError, filter, isEven, 3)
845        self.assertRaises(TypeError, next, filter(range(6), range(6)))
846
847        # check copy, deepcopy, pickle
848        ans = [0,2,4]
849
850        c = filter(isEven, range(6))
851        self.assertEqual(list(copy.copy(c)), ans)
852        c = filter(isEven, range(6))
853        self.assertEqual(list(copy.deepcopy(c)), ans)
854        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
855            c = filter(isEven, range(6))
856            self.assertEqual(list(pickle.loads(pickle.dumps(c, proto))), ans)
857            next(c)
858            self.assertEqual(list(pickle.loads(pickle.dumps(c, proto))), ans[1:])
859        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
860            c = filter(isEven, range(6))
861            self.pickletest(proto, c)
862
863    def test_filterfalse(self):
864        self.assertEqual(list(filterfalse(isEven, range(6))), [1,3,5])
865        self.assertEqual(list(filterfalse(None, [0,1,0,2,0])), [0,0,0])
866        self.assertEqual(list(filterfalse(bool, [0,1,0,2,0])), [0,0,0])
867        self.assertEqual(take(4, filterfalse(isEven, count())), [1,3,5,7])
868        self.assertRaises(TypeError, filterfalse)
869        self.assertRaises(TypeError, filterfalse, lambda x:x)
870        self.assertRaises(TypeError, filterfalse, lambda x:x, range(6), 7)
871        self.assertRaises(TypeError, filterfalse, isEven, 3)
872        self.assertRaises(TypeError, next, filterfalse(range(6), range(6)))
873        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
874            self.pickletest(proto, filterfalse(isEven, range(6)))
875
876    def test_zip(self):
877        # XXX This is rather silly now that builtin zip() calls zip()...
878        ans = [(x,y) for x, y in zip('abc',count())]
879        self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
880        self.assertEqual(list(zip('abc', range(6))), lzip('abc', range(6)))
881        self.assertEqual(list(zip('abcdef', range(3))), lzip('abcdef', range(3)))
882        self.assertEqual(take(3,zip('abcdef', count())), lzip('abcdef', range(3)))
883        self.assertEqual(list(zip('abcdef')), lzip('abcdef'))
884        self.assertEqual(list(zip()), lzip())
885        self.assertRaises(TypeError, zip, 3)
886        self.assertRaises(TypeError, zip, range(3), 3)
887        self.assertEqual([tuple(list(pair)) for pair in zip('abc', 'def')],
888                         lzip('abc', 'def'))
889        self.assertEqual([pair for pair in zip('abc', 'def')],
890                         lzip('abc', 'def'))
891
892    @support.impl_detail("tuple reuse is specific to CPython")
893    def test_zip_tuple_reuse(self):
894        ids = list(map(id, zip('abc', 'def')))
895        self.assertEqual(min(ids), max(ids))
896        ids = list(map(id, list(zip('abc', 'def'))))
897        self.assertEqual(len(dict.fromkeys(ids)), len(ids))
898
899        # check copy, deepcopy, pickle
900        ans = [(x,y) for x, y in copy.copy(zip('abc',count()))]
901        self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
902
903        ans = [(x,y) for x, y in copy.deepcopy(zip('abc',count()))]
904        self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
905
906        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
907            ans = [(x,y) for x, y in pickle.loads(pickle.dumps(zip('abc',count()), proto))]
908            self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
909
910        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
911            testIntermediate = zip('abc',count())
912            next(testIntermediate)
913            ans = [(x,y) for x, y in pickle.loads(pickle.dumps(testIntermediate, proto))]
914            self.assertEqual(ans, [('b', 1), ('c', 2)])
915
916        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
917            self.pickletest(proto, zip('abc', count()))
918
919    def test_ziplongest(self):
920        for args in [
921                ['abc', range(6)],
922                [range(6), 'abc'],
923                [range(1000), range(2000,2100), range(3000,3050)],
924                [range(1000), range(0), range(3000,3050), range(1200), range(1500)],
925                [range(1000), range(0), range(3000,3050), range(1200), range(1500), range(0)],
926            ]:
927            target = [tuple([arg[i] if i < len(arg) else None for arg in args])
928                      for i in range(max(map(len, args)))]
929            self.assertEqual(list(zip_longest(*args)), target)
930            self.assertEqual(list(zip_longest(*args, **{})), target)
931            target = [tuple((e is None and 'X' or e) for e in t) for t in target]   # Replace None fills with 'X'
932            self.assertEqual(list(zip_longest(*args, **dict(fillvalue='X'))), target)
933
934        self.assertEqual(take(3,zip_longest('abcdef', count())), list(zip('abcdef', range(3)))) # take 3 from infinite input
935
936        self.assertEqual(list(zip_longest()), list(zip()))
937        self.assertEqual(list(zip_longest([])), list(zip([])))
938        self.assertEqual(list(zip_longest('abcdef')), list(zip('abcdef')))
939
940        self.assertEqual(list(zip_longest('abc', 'defg', **{})),
941                         list(zip(list('abc')+[None], 'defg'))) # empty keyword dict
942        self.assertRaises(TypeError, zip_longest, 3)
943        self.assertRaises(TypeError, zip_longest, range(3), 3)
944
945        for stmt in [
946            "zip_longest('abc', fv=1)",
947            "zip_longest('abc', fillvalue=1, bogus_keyword=None)",
948        ]:
949            try:
950                eval(stmt, globals(), locals())
951            except TypeError:
952                pass
953            else:
954                self.fail('Did not raise Type in:  ' + stmt)
955
956        self.assertEqual([tuple(list(pair)) for pair in zip_longest('abc', 'def')],
957                         list(zip('abc', 'def')))
958        self.assertEqual([pair for pair in zip_longest('abc', 'def')],
959                         list(zip('abc', 'def')))
960
961    @support.impl_detail("tuple reuse is specific to CPython")
962    def test_zip_longest_tuple_reuse(self):
963        ids = list(map(id, zip_longest('abc', 'def')))
964        self.assertEqual(min(ids), max(ids))
965        ids = list(map(id, list(zip_longest('abc', 'def'))))
966        self.assertEqual(len(dict.fromkeys(ids)), len(ids))
967
968    def test_zip_longest_pickling(self):
969        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
970            self.pickletest(proto, zip_longest("abc", "def"))
971            self.pickletest(proto, zip_longest("abc", "defgh"))
972            self.pickletest(proto, zip_longest("abc", "defgh", fillvalue=1))
973            self.pickletest(proto, zip_longest("", "defgh"))
974
975    def test_zip_longest_bad_iterable(self):
976        exception = TypeError()
977
978        class BadIterable:
979            def __iter__(self):
980                raise exception
981
982        with self.assertRaises(TypeError) as cm:
983            zip_longest(BadIterable())
984
985        self.assertIs(cm.exception, exception)
986
987    def test_bug_7244(self):
988
989        class Repeater:
990            # this class is similar to itertools.repeat
991            def __init__(self, o, t, e):
992                self.o = o
993                self.t = int(t)
994                self.e = e
995            def __iter__(self): # its iterator is itself
996                return self
997            def __next__(self):
998                if self.t > 0:
999                    self.t -= 1
1000                    return self.o
1001                else:
1002                    raise self.e
1003
1004        # Formerly this code in would fail in debug mode
1005        # with Undetected Error and Stop Iteration
1006        r1 = Repeater(1, 3, StopIteration)
1007        r2 = Repeater(2, 4, StopIteration)
1008        def run(r1, r2):
1009            result = []
1010            for i, j in zip_longest(r1, r2, fillvalue=0):
1011                with support.captured_output('stdout'):
1012                    print((i, j))
1013                result.append((i, j))
1014            return result
1015        self.assertEqual(run(r1, r2), [(1,2), (1,2), (1,2), (0,2)])
1016
1017        # Formerly, the RuntimeError would be lost
1018        # and StopIteration would stop as expected
1019        r1 = Repeater(1, 3, RuntimeError)
1020        r2 = Repeater(2, 4, StopIteration)
1021        it = zip_longest(r1, r2, fillvalue=0)
1022        self.assertEqual(next(it), (1, 2))
1023        self.assertEqual(next(it), (1, 2))
1024        self.assertEqual(next(it), (1, 2))
1025        self.assertRaises(RuntimeError, next, it)
1026
1027    def test_product(self):
1028        for args, result in [
1029            ([], [()]),                     # zero iterables
1030            (['ab'], [('a',), ('b',)]),     # one iterable
1031            ([range(2), range(3)], [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]),     # two iterables
1032            ([range(0), range(2), range(3)], []),           # first iterable with zero length
1033            ([range(2), range(0), range(3)], []),           # middle iterable with zero length
1034            ([range(2), range(3), range(0)], []),           # last iterable with zero length
1035            ]:
1036            self.assertEqual(list(product(*args)), result)
1037            for r in range(4):
1038                self.assertEqual(list(product(*(args*r))),
1039                                 list(product(*args, **dict(repeat=r))))
1040        self.assertEqual(len(list(product(*[range(7)]*6))), 7**6)
1041        self.assertRaises(TypeError, product, range(6), None)
1042
1043        def product1(*args, **kwds):
1044            pools = list(map(tuple, args)) * kwds.get('repeat', 1)
1045            n = len(pools)
1046            if n == 0:
1047                yield ()
1048                return
1049            if any(len(pool) == 0 for pool in pools):
1050                return
1051            indices = [0] * n
1052            yield tuple(pool[i] for pool, i in zip(pools, indices))
1053            while 1:
1054                for i in reversed(range(n)):  # right to left
1055                    if indices[i] == len(pools[i]) - 1:
1056                        continue
1057                    indices[i] += 1
1058                    for j in range(i+1, n):
1059                        indices[j] = 0
1060                    yield tuple(pool[i] for pool, i in zip(pools, indices))
1061                    break
1062                else:
1063                    return
1064
1065        def product2(*args, **kwds):
1066            'Pure python version used in docs'
1067            pools = list(map(tuple, args)) * kwds.get('repeat', 1)
1068            result = [[]]
1069            for pool in pools:
1070                result = [x+[y] for x in result for y in pool]
1071            for prod in result:
1072                yield tuple(prod)
1073
1074        argtypes = ['', 'abc', '', range(0), range(4), dict(a=1, b=2, c=3),
1075                    set('abcdefg'), range(11), tuple(range(13))]
1076        for i in range(100):
1077            args = [random.choice(argtypes) for j in range(random.randrange(5))]
1078            expected_len = prod(map(len, args))
1079            self.assertEqual(len(list(product(*args))), expected_len)
1080            self.assertEqual(list(product(*args)), list(product1(*args)))
1081            self.assertEqual(list(product(*args)), list(product2(*args)))
1082            args = map(iter, args)
1083            self.assertEqual(len(list(product(*args))), expected_len)
1084
1085    @support.bigaddrspacetest
1086    def test_product_overflow(self):
1087        with self.assertRaises((OverflowError, MemoryError)):
1088            product(*(['ab']*2**5), repeat=2**25)
1089
1090    @support.impl_detail("tuple reuse is specific to CPython")
1091    def test_product_tuple_reuse(self):
1092        self.assertEqual(len(set(map(id, product('abc', 'def')))), 1)
1093        self.assertNotEqual(len(set(map(id, list(product('abc', 'def'))))), 1)
1094
1095    def test_product_pickling(self):
1096        # check copy, deepcopy, pickle
1097        for args, result in [
1098            ([], [()]),                     # zero iterables
1099            (['ab'], [('a',), ('b',)]),     # one iterable
1100            ([range(2), range(3)], [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]),     # two iterables
1101            ([range(0), range(2), range(3)], []),           # first iterable with zero length
1102            ([range(2), range(0), range(3)], []),           # middle iterable with zero length
1103            ([range(2), range(3), range(0)], []),           # last iterable with zero length
1104            ]:
1105            self.assertEqual(list(copy.copy(product(*args))), result)
1106            self.assertEqual(list(copy.deepcopy(product(*args))), result)
1107            for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1108                self.pickletest(proto, product(*args))
1109
1110    def test_product_issue_25021(self):
1111        # test that indices are properly clamped to the length of the tuples
1112        p = product((1, 2),(3,))
1113        p.__setstate__((0, 0x1000))  # will access tuple element 1 if not clamped
1114        self.assertEqual(next(p), (2, 3))
1115        # test that empty tuple in the list will result in an immediate StopIteration
1116        p = product((1, 2), (), (3,))
1117        p.__setstate__((0, 0, 0x1000))  # will access tuple element 1 if not clamped
1118        self.assertRaises(StopIteration, next, p)
1119
1120    def test_repeat(self):
1121        self.assertEqual(list(repeat(object='a', times=3)), ['a', 'a', 'a'])
1122        self.assertEqual(lzip(range(3),repeat('a')),
1123                         [(0, 'a'), (1, 'a'), (2, 'a')])
1124        self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a'])
1125        self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a'])
1126        self.assertEqual(list(repeat('a', 0)), [])
1127        self.assertEqual(list(repeat('a', -3)), [])
1128        self.assertRaises(TypeError, repeat)
1129        self.assertRaises(TypeError, repeat, None, 3, 4)
1130        self.assertRaises(TypeError, repeat, None, 'a')
1131        r = repeat(1+0j)
1132        self.assertEqual(repr(r), 'repeat((1+0j))')
1133        r = repeat(1+0j, 5)
1134        self.assertEqual(repr(r), 'repeat((1+0j), 5)')
1135        list(r)
1136        self.assertEqual(repr(r), 'repeat((1+0j), 0)')
1137
1138        # check copy, deepcopy, pickle
1139        c = repeat(object='a', times=10)
1140        self.assertEqual(next(c), 'a')
1141        self.assertEqual(take(2, copy.copy(c)), list('a' * 2))
1142        self.assertEqual(take(2, copy.deepcopy(c)), list('a' * 2))
1143        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1144            self.pickletest(proto, repeat(object='a', times=10))
1145
1146    def test_repeat_with_negative_times(self):
1147        self.assertEqual(repr(repeat('a', -1)), "repeat('a', 0)")
1148        self.assertEqual(repr(repeat('a', -2)), "repeat('a', 0)")
1149        self.assertEqual(repr(repeat('a', times=-1)), "repeat('a', 0)")
1150        self.assertEqual(repr(repeat('a', times=-2)), "repeat('a', 0)")
1151
1152    def test_map(self):
1153        self.assertEqual(list(map(operator.pow, range(3), range(1,7))),
1154                         [0**1, 1**2, 2**3])
1155        self.assertEqual(list(map(tupleize, 'abc', range(5))),
1156                         [('a',0),('b',1),('c',2)])
1157        self.assertEqual(list(map(tupleize, 'abc', count())),
1158                         [('a',0),('b',1),('c',2)])
1159        self.assertEqual(take(2,map(tupleize, 'abc', count())),
1160                         [('a',0),('b',1)])
1161        self.assertEqual(list(map(operator.pow, [])), [])
1162        self.assertRaises(TypeError, map)
1163        self.assertRaises(TypeError, list, map(None, range(3), range(3)))
1164        self.assertRaises(TypeError, map, operator.neg)
1165        self.assertRaises(TypeError, next, map(10, range(5)))
1166        self.assertRaises(ValueError, next, map(errfunc, [4], [5]))
1167        self.assertRaises(TypeError, next, map(onearg, [4], [5]))
1168
1169        # check copy, deepcopy, pickle
1170        ans = [('a',0),('b',1),('c',2)]
1171
1172        c = map(tupleize, 'abc', count())
1173        self.assertEqual(list(copy.copy(c)), ans)
1174
1175        c = map(tupleize, 'abc', count())
1176        self.assertEqual(list(copy.deepcopy(c)), ans)
1177
1178        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1179            c = map(tupleize, 'abc', count())
1180            self.pickletest(proto, c)
1181
1182    def test_starmap(self):
1183        self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))),
1184                         [0**1, 1**2, 2**3])
1185        self.assertEqual(take(3, starmap(operator.pow, zip(count(), count(1)))),
1186                         [0**1, 1**2, 2**3])
1187        self.assertEqual(list(starmap(operator.pow, [])), [])
1188        self.assertEqual(list(starmap(operator.pow, [iter([4,5])])), [4**5])
1189        self.assertRaises(TypeError, list, starmap(operator.pow, [None]))
1190        self.assertRaises(TypeError, starmap)
1191        self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra')
1192        self.assertRaises(TypeError, next, starmap(10, [(4,5)]))
1193        self.assertRaises(ValueError, next, starmap(errfunc, [(4,5)]))
1194        self.assertRaises(TypeError, next, starmap(onearg, [(4,5)]))
1195
1196        # check copy, deepcopy, pickle
1197        ans = [0**1, 1**2, 2**3]
1198
1199        c = starmap(operator.pow, zip(range(3), range(1,7)))
1200        self.assertEqual(list(copy.copy(c)), ans)
1201
1202        c = starmap(operator.pow, zip(range(3), range(1,7)))
1203        self.assertEqual(list(copy.deepcopy(c)), ans)
1204
1205        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1206            c = starmap(operator.pow, zip(range(3), range(1,7)))
1207            self.pickletest(proto, c)
1208
1209    def test_islice(self):
1210        for args in [          # islice(args) should agree with range(args)
1211                (10, 20, 3),
1212                (10, 3, 20),
1213                (10, 20),
1214                (10, 10),
1215                (10, 3),
1216                (20,)
1217                ]:
1218            self.assertEqual(list(islice(range(100), *args)),
1219                             list(range(*args)))
1220
1221        for args, tgtargs in [  # Stop when seqn is exhausted
1222                ((10, 110, 3), ((10, 100, 3))),
1223                ((10, 110), ((10, 100))),
1224                ((110,), (100,))
1225                ]:
1226            self.assertEqual(list(islice(range(100), *args)),
1227                             list(range(*tgtargs)))
1228
1229        # Test stop=None
1230        self.assertEqual(list(islice(range(10), None)), list(range(10)))
1231        self.assertEqual(list(islice(range(10), None, None)), list(range(10)))
1232        self.assertEqual(list(islice(range(10), None, None, None)), list(range(10)))
1233        self.assertEqual(list(islice(range(10), 2, None)), list(range(2, 10)))
1234        self.assertEqual(list(islice(range(10), 1, None, 2)), list(range(1, 10, 2)))
1235
1236        # Test number of items consumed     SF #1171417
1237        it = iter(range(10))
1238        self.assertEqual(list(islice(it, 3)), list(range(3)))
1239        self.assertEqual(list(it), list(range(3, 10)))
1240
1241        it = iter(range(10))
1242        self.assertEqual(list(islice(it, 3, 3)), [])
1243        self.assertEqual(list(it), list(range(3, 10)))
1244
1245        # Test invalid arguments
1246        ra = range(10)
1247        self.assertRaises(TypeError, islice, ra)
1248        self.assertRaises(TypeError, islice, ra, 1, 2, 3, 4)
1249        self.assertRaises(ValueError, islice, ra, -5, 10, 1)
1250        self.assertRaises(ValueError, islice, ra, 1, -5, -1)
1251        self.assertRaises(ValueError, islice, ra, 1, 10, -1)
1252        self.assertRaises(ValueError, islice, ra, 1, 10, 0)
1253        self.assertRaises(ValueError, islice, ra, 'a')
1254        self.assertRaises(ValueError, islice, ra, 'a', 1)
1255        self.assertRaises(ValueError, islice, ra, 1, 'a')
1256        self.assertRaises(ValueError, islice, ra, 'a', 1, 1)
1257        self.assertRaises(ValueError, islice, ra, 1, 'a', 1)
1258        self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1)
1259
1260        # Issue #10323:  Less islice in a predictable state
1261        c = count()
1262        self.assertEqual(list(islice(c, 1, 3, 50)), [1])
1263        self.assertEqual(next(c), 3)
1264
1265        # check copy, deepcopy, pickle
1266        for args in [          # islice(args) should agree with range(args)
1267                (10, 20, 3),
1268                (10, 3, 20),
1269                (10, 20),
1270                (10, 3),
1271                (20,)
1272                ]:
1273            self.assertEqual(list(copy.copy(islice(range(100), *args))),
1274                             list(range(*args)))
1275            self.assertEqual(list(copy.deepcopy(islice(range(100), *args))),
1276                             list(range(*args)))
1277            for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1278                self.pickletest(proto, islice(range(100), *args))
1279
1280        # Issue #21321: check source iterator is not referenced
1281        # from islice() after the latter has been exhausted
1282        it = (x for x in (1, 2))
1283        wr = weakref.ref(it)
1284        it = islice(it, 1)
1285        self.assertIsNotNone(wr())
1286        list(it) # exhaust the iterator
1287        support.gc_collect()
1288        self.assertIsNone(wr())
1289
1290        # Issue #30537: islice can accept integer-like objects as
1291        # arguments
1292        class IntLike(object):
1293            def __init__(self, val):
1294                self.val = val
1295            def __index__(self):
1296                return self.val
1297        self.assertEqual(list(islice(range(100), IntLike(10))), list(range(10)))
1298        self.assertEqual(list(islice(range(100), IntLike(10), IntLike(50))),
1299                         list(range(10, 50)))
1300        self.assertEqual(list(islice(range(100), IntLike(10), IntLike(50), IntLike(5))),
1301                         list(range(10,50,5)))
1302
1303    def test_takewhile(self):
1304        data = [1, 3, 5, 20, 2, 4, 6, 8]
1305        self.assertEqual(list(takewhile(underten, data)), [1, 3, 5])
1306        self.assertEqual(list(takewhile(underten, [])), [])
1307        self.assertRaises(TypeError, takewhile)
1308        self.assertRaises(TypeError, takewhile, operator.pow)
1309        self.assertRaises(TypeError, takewhile, operator.pow, [(4,5)], 'extra')
1310        self.assertRaises(TypeError, next, takewhile(10, [(4,5)]))
1311        self.assertRaises(ValueError, next, takewhile(errfunc, [(4,5)]))
1312        t = takewhile(bool, [1, 1, 1, 0, 0, 0])
1313        self.assertEqual(list(t), [1, 1, 1])
1314        self.assertRaises(StopIteration, next, t)
1315
1316        # check copy, deepcopy, pickle
1317        self.assertEqual(list(copy.copy(takewhile(underten, data))), [1, 3, 5])
1318        self.assertEqual(list(copy.deepcopy(takewhile(underten, data))),
1319                        [1, 3, 5])
1320        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1321            self.pickletest(proto, takewhile(underten, data))
1322
1323    def test_dropwhile(self):
1324        data = [1, 3, 5, 20, 2, 4, 6, 8]
1325        self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8])
1326        self.assertEqual(list(dropwhile(underten, [])), [])
1327        self.assertRaises(TypeError, dropwhile)
1328        self.assertRaises(TypeError, dropwhile, operator.pow)
1329        self.assertRaises(TypeError, dropwhile, operator.pow, [(4,5)], 'extra')
1330        self.assertRaises(TypeError, next, dropwhile(10, [(4,5)]))
1331        self.assertRaises(ValueError, next, dropwhile(errfunc, [(4,5)]))
1332
1333        # check copy, deepcopy, pickle
1334        self.assertEqual(list(copy.copy(dropwhile(underten, data))), [20, 2, 4, 6, 8])
1335        self.assertEqual(list(copy.deepcopy(dropwhile(underten, data))),
1336                        [20, 2, 4, 6, 8])
1337        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1338            self.pickletest(proto, dropwhile(underten, data))
1339
1340    def test_tee(self):
1341        n = 200
1342
1343        a, b = tee([])        # test empty iterator
1344        self.assertEqual(list(a), [])
1345        self.assertEqual(list(b), [])
1346
1347        a, b = tee(irange(n)) # test 100% interleaved
1348        self.assertEqual(lzip(a,b), lzip(range(n), range(n)))
1349
1350        a, b = tee(irange(n)) # test 0% interleaved
1351        self.assertEqual(list(a), list(range(n)))
1352        self.assertEqual(list(b), list(range(n)))
1353
1354        a, b = tee(irange(n)) # test dealloc of leading iterator
1355        for i in range(100):
1356            self.assertEqual(next(a), i)
1357        del a
1358        self.assertEqual(list(b), list(range(n)))
1359
1360        a, b = tee(irange(n)) # test dealloc of trailing iterator
1361        for i in range(100):
1362            self.assertEqual(next(a), i)
1363        del b
1364        self.assertEqual(list(a), list(range(100, n)))
1365
1366        for j in range(5):   # test randomly interleaved
1367            order = [0]*n + [1]*n
1368            random.shuffle(order)
1369            lists = ([], [])
1370            its = tee(irange(n))
1371            for i in order:
1372                value = next(its[i])
1373                lists[i].append(value)
1374            self.assertEqual(lists[0], list(range(n)))
1375            self.assertEqual(lists[1], list(range(n)))
1376
1377        # test argument format checking
1378        self.assertRaises(TypeError, tee)
1379        self.assertRaises(TypeError, tee, 3)
1380        self.assertRaises(TypeError, tee, [1,2], 'x')
1381        self.assertRaises(TypeError, tee, [1,2], 3, 'x')
1382
1383        # tee object should be instantiable
1384        a, b = tee('abc')
1385        c = type(a)('def')
1386        self.assertEqual(list(c), list('def'))
1387
1388        # test long-lagged and multi-way split
1389        a, b, c = tee(range(2000), 3)
1390        for i in range(100):
1391            self.assertEqual(next(a), i)
1392        self.assertEqual(list(b), list(range(2000)))
1393        self.assertEqual([next(c), next(c)], list(range(2)))
1394        self.assertEqual(list(a), list(range(100,2000)))
1395        self.assertEqual(list(c), list(range(2,2000)))
1396
1397        # test values of n
1398        self.assertRaises(TypeError, tee, 'abc', 'invalid')
1399        self.assertRaises(ValueError, tee, [], -1)
1400        for n in range(5):
1401            result = tee('abc', n)
1402            self.assertEqual(type(result), tuple)
1403            self.assertEqual(len(result), n)
1404            self.assertEqual([list(x) for x in result], [list('abc')]*n)
1405
1406        # tee pass-through to copyable iterator
1407        a, b = tee('abc')
1408        c, d = tee(a)
1409        self.assertTrue(a is c)
1410
1411        # test tee_new
1412        t1, t2 = tee('abc')
1413        tnew = type(t1)
1414        self.assertRaises(TypeError, tnew)
1415        self.assertRaises(TypeError, tnew, 10)
1416        t3 = tnew(t1)
1417        self.assertTrue(list(t1) == list(t2) == list(t3) == list('abc'))
1418
1419        # test that tee objects are weak referencable
1420        a, b = tee(range(10))
1421        p = weakref.proxy(a)
1422        self.assertEqual(getattr(p, '__class__'), type(b))
1423        del a
1424        self.assertRaises(ReferenceError, getattr, p, '__class__')
1425
1426        ans = list('abc')
1427        long_ans = list(range(10000))
1428
1429        # check copy
1430        a, b = tee('abc')
1431        self.assertEqual(list(copy.copy(a)), ans)
1432        self.assertEqual(list(copy.copy(b)), ans)
1433        a, b = tee(list(range(10000)))
1434        self.assertEqual(list(copy.copy(a)), long_ans)
1435        self.assertEqual(list(copy.copy(b)), long_ans)
1436
1437        # check partially consumed copy
1438        a, b = tee('abc')
1439        take(2, a)
1440        take(1, b)
1441        self.assertEqual(list(copy.copy(a)), ans[2:])
1442        self.assertEqual(list(copy.copy(b)), ans[1:])
1443        self.assertEqual(list(a), ans[2:])
1444        self.assertEqual(list(b), ans[1:])
1445        a, b = tee(range(10000))
1446        take(100, a)
1447        take(60, b)
1448        self.assertEqual(list(copy.copy(a)), long_ans[100:])
1449        self.assertEqual(list(copy.copy(b)), long_ans[60:])
1450        self.assertEqual(list(a), long_ans[100:])
1451        self.assertEqual(list(b), long_ans[60:])
1452
1453        # check deepcopy
1454        a, b = tee('abc')
1455        self.assertEqual(list(copy.deepcopy(a)), ans)
1456        self.assertEqual(list(copy.deepcopy(b)), ans)
1457        self.assertEqual(list(a), ans)
1458        self.assertEqual(list(b), ans)
1459        a, b = tee(range(10000))
1460        self.assertEqual(list(copy.deepcopy(a)), long_ans)
1461        self.assertEqual(list(copy.deepcopy(b)), long_ans)
1462        self.assertEqual(list(a), long_ans)
1463        self.assertEqual(list(b), long_ans)
1464
1465        # check partially consumed deepcopy
1466        a, b = tee('abc')
1467        take(2, a)
1468        take(1, b)
1469        self.assertEqual(list(copy.deepcopy(a)), ans[2:])
1470        self.assertEqual(list(copy.deepcopy(b)), ans[1:])
1471        self.assertEqual(list(a), ans[2:])
1472        self.assertEqual(list(b), ans[1:])
1473        a, b = tee(range(10000))
1474        take(100, a)
1475        take(60, b)
1476        self.assertEqual(list(copy.deepcopy(a)), long_ans[100:])
1477        self.assertEqual(list(copy.deepcopy(b)), long_ans[60:])
1478        self.assertEqual(list(a), long_ans[100:])
1479        self.assertEqual(list(b), long_ans[60:])
1480
1481        # check pickle
1482        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1483            self.pickletest(proto, iter(tee('abc')))
1484            a, b = tee('abc')
1485            self.pickletest(proto, a, compare=ans)
1486            self.pickletest(proto, b, compare=ans)
1487
1488    # Issue 13454: Crash when deleting backward iterator from tee()
1489    def test_tee_del_backward(self):
1490        forward, backward = tee(repeat(None, 20000000))
1491        try:
1492            any(forward)  # exhaust the iterator
1493            del backward
1494        except:
1495            del forward, backward
1496            raise
1497
1498    def test_tee_reenter(self):
1499        class I:
1500            first = True
1501            def __iter__(self):
1502                return self
1503            def __next__(self):
1504                first = self.first
1505                self.first = False
1506                if first:
1507                    return next(b)
1508
1509        a, b = tee(I())
1510        with self.assertRaisesRegex(RuntimeError, "tee"):
1511            next(a)
1512
1513    def test_tee_concurrent(self):
1514        start = threading.Event()
1515        finish = threading.Event()
1516        class I:
1517            def __iter__(self):
1518                return self
1519            def __next__(self):
1520                start.set()
1521                finish.wait()
1522
1523        a, b = tee(I())
1524        thread = threading.Thread(target=next, args=[a])
1525        thread.start()
1526        try:
1527            start.wait()
1528            with self.assertRaisesRegex(RuntimeError, "tee"):
1529                next(b)
1530        finally:
1531            finish.set()
1532            thread.join()
1533
1534    def test_StopIteration(self):
1535        self.assertRaises(StopIteration, next, zip())
1536
1537        for f in (chain, cycle, zip, groupby):
1538            self.assertRaises(StopIteration, next, f([]))
1539            self.assertRaises(StopIteration, next, f(StopNow()))
1540
1541        self.assertRaises(StopIteration, next, islice([], None))
1542        self.assertRaises(StopIteration, next, islice(StopNow(), None))
1543
1544        p, q = tee([])
1545        self.assertRaises(StopIteration, next, p)
1546        self.assertRaises(StopIteration, next, q)
1547        p, q = tee(StopNow())
1548        self.assertRaises(StopIteration, next, p)
1549        self.assertRaises(StopIteration, next, q)
1550
1551        self.assertRaises(StopIteration, next, repeat(None, 0))
1552
1553        for f in (filter, filterfalse, map, takewhile, dropwhile, starmap):
1554            self.assertRaises(StopIteration, next, f(lambda x:x, []))
1555            self.assertRaises(StopIteration, next, f(lambda x:x, StopNow()))
1556
1557class TestExamples(unittest.TestCase):
1558
1559    def test_accumulate(self):
1560        self.assertEqual(list(accumulate([1,2,3,4,5])), [1, 3, 6, 10, 15])
1561
1562    def test_accumulate_reducible(self):
1563        # check copy, deepcopy, pickle
1564        data = [1, 2, 3, 4, 5]
1565        accumulated = [1, 3, 6, 10, 15]
1566
1567        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1568            it = accumulate(data)
1569            self.assertEqual(list(pickle.loads(pickle.dumps(it, proto))), accumulated[:])
1570            self.assertEqual(next(it), 1)
1571            self.assertEqual(list(pickle.loads(pickle.dumps(it, proto))), accumulated[1:])
1572        it = accumulate(data)
1573        self.assertEqual(next(it), 1)
1574        self.assertEqual(list(copy.deepcopy(it)), accumulated[1:])
1575        self.assertEqual(list(copy.copy(it)), accumulated[1:])
1576
1577    def test_accumulate_reducible_none(self):
1578        # Issue #25718: total is None
1579        it = accumulate([None, None, None], operator.is_)
1580        self.assertEqual(next(it), None)
1581        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1582            it_copy = pickle.loads(pickle.dumps(it, proto))
1583            self.assertEqual(list(it_copy), [True, False])
1584        self.assertEqual(list(copy.deepcopy(it)), [True, False])
1585        self.assertEqual(list(copy.copy(it)), [True, False])
1586
1587    def test_chain(self):
1588        self.assertEqual(''.join(chain('ABC', 'DEF')), 'ABCDEF')
1589
1590    def test_chain_from_iterable(self):
1591        self.assertEqual(''.join(chain.from_iterable(['ABC', 'DEF'])), 'ABCDEF')
1592
1593    def test_combinations(self):
1594        self.assertEqual(list(combinations('ABCD', 2)),
1595                         [('A','B'), ('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')])
1596        self.assertEqual(list(combinations(range(4), 3)),
1597                         [(0,1,2), (0,1,3), (0,2,3), (1,2,3)])
1598
1599    def test_combinations_with_replacement(self):
1600        self.assertEqual(list(combinations_with_replacement('ABC', 2)),
1601                         [('A','A'), ('A','B'), ('A','C'), ('B','B'), ('B','C'), ('C','C')])
1602
1603    def test_compress(self):
1604        self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF'))
1605
1606    def test_count(self):
1607        self.assertEqual(list(islice(count(10), 5)), [10, 11, 12, 13, 14])
1608
1609    def test_cycle(self):
1610        self.assertEqual(list(islice(cycle('ABCD'), 12)), list('ABCDABCDABCD'))
1611
1612    def test_dropwhile(self):
1613        self.assertEqual(list(dropwhile(lambda x: x<5, [1,4,6,4,1])), [6,4,1])
1614
1615    def test_groupby(self):
1616        self.assertEqual([k for k, g in groupby('AAAABBBCCDAABBB')],
1617                         list('ABCDAB'))
1618        self.assertEqual([(list(g)) for k, g in groupby('AAAABBBCCD')],
1619                         [list('AAAA'), list('BBB'), list('CC'), list('D')])
1620
1621    def test_filter(self):
1622        self.assertEqual(list(filter(lambda x: x%2, range(10))), [1,3,5,7,9])
1623
1624    def test_filterfalse(self):
1625        self.assertEqual(list(filterfalse(lambda x: x%2, range(10))), [0,2,4,6,8])
1626
1627    def test_map(self):
1628        self.assertEqual(list(map(pow, (2,3,10), (5,2,3))), [32, 9, 1000])
1629
1630    def test_islice(self):
1631        self.assertEqual(list(islice('ABCDEFG', 2)), list('AB'))
1632        self.assertEqual(list(islice('ABCDEFG', 2, 4)), list('CD'))
1633        self.assertEqual(list(islice('ABCDEFG', 2, None)), list('CDEFG'))
1634        self.assertEqual(list(islice('ABCDEFG', 0, None, 2)), list('ACEG'))
1635
1636    def test_zip(self):
1637        self.assertEqual(list(zip('ABCD', 'xy')), [('A', 'x'), ('B', 'y')])
1638
1639    def test_zip_longest(self):
1640        self.assertEqual(list(zip_longest('ABCD', 'xy', fillvalue='-')),
1641                         [('A', 'x'), ('B', 'y'), ('C', '-'), ('D', '-')])
1642
1643    def test_permutations(self):
1644        self.assertEqual(list(permutations('ABCD', 2)),
1645                         list(map(tuple, 'AB AC AD BA BC BD CA CB CD DA DB DC'.split())))
1646        self.assertEqual(list(permutations(range(3))),
1647                         [(0,1,2), (0,2,1), (1,0,2), (1,2,0), (2,0,1), (2,1,0)])
1648
1649    def test_product(self):
1650        self.assertEqual(list(product('ABCD', 'xy')),
1651                         list(map(tuple, 'Ax Ay Bx By Cx Cy Dx Dy'.split())))
1652        self.assertEqual(list(product(range(2), repeat=3)),
1653                        [(0,0,0), (0,0,1), (0,1,0), (0,1,1),
1654                         (1,0,0), (1,0,1), (1,1,0), (1,1,1)])
1655
1656    def test_repeat(self):
1657        self.assertEqual(list(repeat(10, 3)), [10, 10, 10])
1658
1659    def test_stapmap(self):
1660        self.assertEqual(list(starmap(pow, [(2,5), (3,2), (10,3)])),
1661                         [32, 9, 1000])
1662
1663    def test_takewhile(self):
1664        self.assertEqual(list(takewhile(lambda x: x<5, [1,4,6,4,1])), [1,4])
1665
1666
1667class TestPurePythonRoughEquivalents(unittest.TestCase):
1668
1669    @staticmethod
1670    def islice(iterable, *args):
1671        s = slice(*args)
1672        start, stop, step = s.start or 0, s.stop or sys.maxsize, s.step or 1
1673        it = iter(range(start, stop, step))
1674        try:
1675            nexti = next(it)
1676        except StopIteration:
1677            # Consume *iterable* up to the *start* position.
1678            for i, element in zip(range(start), iterable):
1679                pass
1680            return
1681        try:
1682            for i, element in enumerate(iterable):
1683                if i == nexti:
1684                    yield element
1685                    nexti = next(it)
1686        except StopIteration:
1687            # Consume to *stop*.
1688            for i, element in zip(range(i + 1, stop), iterable):
1689                pass
1690
1691    def test_islice_recipe(self):
1692        self.assertEqual(list(self.islice('ABCDEFG', 2)), list('AB'))
1693        self.assertEqual(list(self.islice('ABCDEFG', 2, 4)), list('CD'))
1694        self.assertEqual(list(self.islice('ABCDEFG', 2, None)), list('CDEFG'))
1695        self.assertEqual(list(self.islice('ABCDEFG', 0, None, 2)), list('ACEG'))
1696        # Test items consumed.
1697        it = iter(range(10))
1698        self.assertEqual(list(self.islice(it, 3)), list(range(3)))
1699        self.assertEqual(list(it), list(range(3, 10)))
1700        it = iter(range(10))
1701        self.assertEqual(list(self.islice(it, 3, 3)), [])
1702        self.assertEqual(list(it), list(range(3, 10)))
1703        # Test that slice finishes in predictable state.
1704        c = count()
1705        self.assertEqual(list(self.islice(c, 1, 3, 50)), [1])
1706        self.assertEqual(next(c), 3)
1707
1708
1709class TestGC(unittest.TestCase):
1710
1711    def makecycle(self, iterator, container):
1712        container.append(iterator)
1713        next(iterator)
1714        del container, iterator
1715
1716    def test_accumulate(self):
1717        a = []
1718        self.makecycle(accumulate([1,2,a,3]), a)
1719
1720    def test_chain(self):
1721        a = []
1722        self.makecycle(chain(a), a)
1723
1724    def test_chain_from_iterable(self):
1725        a = []
1726        self.makecycle(chain.from_iterable([a]), a)
1727
1728    def test_combinations(self):
1729        a = []
1730        self.makecycle(combinations([1,2,a,3], 3), a)
1731
1732    def test_combinations_with_replacement(self):
1733        a = []
1734        self.makecycle(combinations_with_replacement([1,2,a,3], 3), a)
1735
1736    def test_compress(self):
1737        a = []
1738        self.makecycle(compress('ABCDEF', [1,0,1,0,1,0]), a)
1739
1740    def test_count(self):
1741        a = []
1742        Int = type('Int', (int,), dict(x=a))
1743        self.makecycle(count(Int(0), Int(1)), a)
1744
1745    def test_cycle(self):
1746        a = []
1747        self.makecycle(cycle([a]*2), a)
1748
1749    def test_dropwhile(self):
1750        a = []
1751        self.makecycle(dropwhile(bool, [0, a, a]), a)
1752
1753    def test_groupby(self):
1754        a = []
1755        self.makecycle(groupby([a]*2, lambda x:x), a)
1756
1757    def test_issue2246(self):
1758        # Issue 2246 -- the _grouper iterator was not included in GC
1759        n = 10
1760        keyfunc = lambda x: x
1761        for i, j in groupby(range(n), key=keyfunc):
1762            keyfunc.__dict__.setdefault('x',[]).append(j)
1763
1764    def test_filter(self):
1765        a = []
1766        self.makecycle(filter(lambda x:True, [a]*2), a)
1767
1768    def test_filterfalse(self):
1769        a = []
1770        self.makecycle(filterfalse(lambda x:False, a), a)
1771
1772    def test_zip(self):
1773        a = []
1774        self.makecycle(zip([a]*2, [a]*3), a)
1775
1776    def test_zip_longest(self):
1777        a = []
1778        self.makecycle(zip_longest([a]*2, [a]*3), a)
1779        b = [a, None]
1780        self.makecycle(zip_longest([a]*2, [a]*3, fillvalue=b), a)
1781
1782    def test_map(self):
1783        a = []
1784        self.makecycle(map(lambda x:x, [a]*2), a)
1785
1786    def test_islice(self):
1787        a = []
1788        self.makecycle(islice([a]*2, None), a)
1789
1790    def test_permutations(self):
1791        a = []
1792        self.makecycle(permutations([1,2,a,3], 3), a)
1793
1794    def test_product(self):
1795        a = []
1796        self.makecycle(product([1,2,a,3], repeat=3), a)
1797
1798    def test_repeat(self):
1799        a = []
1800        self.makecycle(repeat(a), a)
1801
1802    def test_starmap(self):
1803        a = []
1804        self.makecycle(starmap(lambda *t: t, [(a,a)]*2), a)
1805
1806    def test_takewhile(self):
1807        a = []
1808        self.makecycle(takewhile(bool, [1, 0, a, a]), a)
1809
1810def R(seqn):
1811    'Regular generator'
1812    for i in seqn:
1813        yield i
1814
1815class G:
1816    'Sequence using __getitem__'
1817    def __init__(self, seqn):
1818        self.seqn = seqn
1819    def __getitem__(self, i):
1820        return self.seqn[i]
1821
1822class I:
1823    'Sequence using iterator protocol'
1824    def __init__(self, seqn):
1825        self.seqn = seqn
1826        self.i = 0
1827    def __iter__(self):
1828        return self
1829    def __next__(self):
1830        if self.i >= len(self.seqn): raise StopIteration
1831        v = self.seqn[self.i]
1832        self.i += 1
1833        return v
1834
1835class Ig:
1836    'Sequence using iterator protocol defined with a generator'
1837    def __init__(self, seqn):
1838        self.seqn = seqn
1839        self.i = 0
1840    def __iter__(self):
1841        for val in self.seqn:
1842            yield val
1843
1844class X:
1845    'Missing __getitem__ and __iter__'
1846    def __init__(self, seqn):
1847        self.seqn = seqn
1848        self.i = 0
1849    def __next__(self):
1850        if self.i >= len(self.seqn): raise StopIteration
1851        v = self.seqn[self.i]
1852        self.i += 1
1853        return v
1854
1855class N:
1856    'Iterator missing __next__()'
1857    def __init__(self, seqn):
1858        self.seqn = seqn
1859        self.i = 0
1860    def __iter__(self):
1861        return self
1862
1863class E:
1864    'Test propagation of exceptions'
1865    def __init__(self, seqn):
1866        self.seqn = seqn
1867        self.i = 0
1868    def __iter__(self):
1869        return self
1870    def __next__(self):
1871        3 // 0
1872
1873class S:
1874    'Test immediate stop'
1875    def __init__(self, seqn):
1876        pass
1877    def __iter__(self):
1878        return self
1879    def __next__(self):
1880        raise StopIteration
1881
1882def L(seqn):
1883    'Test multiple tiers of iterators'
1884    return chain(map(lambda x:x, R(Ig(G(seqn)))))
1885
1886
1887class TestVariousIteratorArgs(unittest.TestCase):
1888
1889    def test_accumulate(self):
1890        s = [1,2,3,4,5]
1891        r = [1,3,6,10,15]
1892        n = len(s)
1893        for g in (G, I, Ig, L, R):
1894            self.assertEqual(list(accumulate(g(s))), r)
1895        self.assertEqual(list(accumulate(S(s))), [])
1896        self.assertRaises(TypeError, accumulate, X(s))
1897        self.assertRaises(TypeError, accumulate, N(s))
1898        self.assertRaises(ZeroDivisionError, list, accumulate(E(s)))
1899
1900    def test_chain(self):
1901        for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
1902            for g in (G, I, Ig, S, L, R):
1903                self.assertEqual(list(chain(g(s))), list(g(s)))
1904                self.assertEqual(list(chain(g(s), g(s))), list(g(s))+list(g(s)))
1905            self.assertRaises(TypeError, list, chain(X(s)))
1906            self.assertRaises(TypeError, list, chain(N(s)))
1907            self.assertRaises(ZeroDivisionError, list, chain(E(s)))
1908
1909    def test_compress(self):
1910        for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
1911            n = len(s)
1912            for g in (G, I, Ig, S, L, R):
1913                self.assertEqual(list(compress(g(s), repeat(1))), list(g(s)))
1914            self.assertRaises(TypeError, compress, X(s), repeat(1))
1915            self.assertRaises(TypeError, compress, N(s), repeat(1))
1916            self.assertRaises(ZeroDivisionError, list, compress(E(s), repeat(1)))
1917
1918    def test_product(self):
1919        for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
1920            self.assertRaises(TypeError, product, X(s))
1921            self.assertRaises(TypeError, product, N(s))
1922            self.assertRaises(ZeroDivisionError, product, E(s))
1923
1924    def test_cycle(self):
1925        for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
1926            for g in (G, I, Ig, S, L, R):
1927                tgtlen = len(s) * 3
1928                expected = list(g(s))*3
1929                actual = list(islice(cycle(g(s)), tgtlen))
1930                self.assertEqual(actual, expected)
1931            self.assertRaises(TypeError, cycle, X(s))
1932            self.assertRaises(TypeError, cycle, N(s))
1933            self.assertRaises(ZeroDivisionError, list, cycle(E(s)))
1934
1935    def test_groupby(self):
1936        for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
1937            for g in (G, I, Ig, S, L, R):
1938                self.assertEqual([k for k, sb in groupby(g(s))], list(g(s)))
1939            self.assertRaises(TypeError, groupby, X(s))
1940            self.assertRaises(TypeError, groupby, N(s))
1941            self.assertRaises(ZeroDivisionError, list, groupby(E(s)))
1942
1943    def test_filter(self):
1944        for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
1945            for g in (G, I, Ig, S, L, R):
1946                self.assertEqual(list(filter(isEven, g(s))),
1947                                 [x for x in g(s) if isEven(x)])
1948            self.assertRaises(TypeError, filter, isEven, X(s))
1949            self.assertRaises(TypeError, filter, isEven, N(s))
1950            self.assertRaises(ZeroDivisionError, list, filter(isEven, E(s)))
1951
1952    def test_filterfalse(self):
1953        for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
1954            for g in (G, I, Ig, S, L, R):
1955                self.assertEqual(list(filterfalse(isEven, g(s))),
1956                                 [x for x in g(s) if isOdd(x)])
1957            self.assertRaises(TypeError, filterfalse, isEven, X(s))
1958            self.assertRaises(TypeError, filterfalse, isEven, N(s))
1959            self.assertRaises(ZeroDivisionError, list, filterfalse(isEven, E(s)))
1960
1961    def test_zip(self):
1962        for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
1963            for g in (G, I, Ig, S, L, R):
1964                self.assertEqual(list(zip(g(s))), lzip(g(s)))
1965                self.assertEqual(list(zip(g(s), g(s))), lzip(g(s), g(s)))
1966            self.assertRaises(TypeError, zip, X(s))
1967            self.assertRaises(TypeError, zip, N(s))
1968            self.assertRaises(ZeroDivisionError, list, zip(E(s)))
1969
1970    def test_ziplongest(self):
1971        for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
1972            for g in (G, I, Ig, S, L, R):
1973                self.assertEqual(list(zip_longest(g(s))), list(zip(g(s))))
1974                self.assertEqual(list(zip_longest(g(s), g(s))), list(zip(g(s), g(s))))
1975            self.assertRaises(TypeError, zip_longest, X(s))
1976            self.assertRaises(TypeError, zip_longest, N(s))
1977            self.assertRaises(ZeroDivisionError, list, zip_longest(E(s)))
1978
1979    def test_map(self):
1980        for s in (range(10), range(0), range(100), (7,11), range(20,50,5)):
1981            for g in (G, I, Ig, S, L, R):
1982                self.assertEqual(list(map(onearg, g(s))),
1983                                 [onearg(x) for x in g(s)])
1984                self.assertEqual(list(map(operator.pow, g(s), g(s))),
1985                                 [x**x for x in g(s)])
1986            self.assertRaises(TypeError, map, onearg, X(s))
1987            self.assertRaises(TypeError, map, onearg, N(s))
1988            self.assertRaises(ZeroDivisionError, list, map(onearg, E(s)))
1989
1990    def test_islice(self):
1991        for s in ("12345", "", range(1000), ('do', 1.2), range(2000,2200,5)):
1992            for g in (G, I, Ig, S, L, R):
1993                self.assertEqual(list(islice(g(s),1,None,2)), list(g(s))[1::2])
1994            self.assertRaises(TypeError, islice, X(s), 10)
1995            self.assertRaises(TypeError, islice, N(s), 10)
1996            self.assertRaises(ZeroDivisionError, list, islice(E(s), 10))
1997
1998    def test_starmap(self):
1999        for s in (range(10), range(0), range(100), (7,11), range(20,50,5)):
2000            for g in (G, I, Ig, S, L, R):
2001                ss = lzip(s, s)
2002                self.assertEqual(list(starmap(operator.pow, g(ss))),
2003                                 [x**x for x in g(s)])
2004            self.assertRaises(TypeError, starmap, operator.pow, X(ss))
2005            self.assertRaises(TypeError, starmap, operator.pow, N(ss))
2006            self.assertRaises(ZeroDivisionError, list, starmap(operator.pow, E(ss)))
2007
2008    def test_takewhile(self):
2009        for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
2010            for g in (G, I, Ig, S, L, R):
2011                tgt = []
2012                for elem in g(s):
2013                    if not isEven(elem): break
2014                    tgt.append(elem)
2015                self.assertEqual(list(takewhile(isEven, g(s))), tgt)
2016            self.assertRaises(TypeError, takewhile, isEven, X(s))
2017            self.assertRaises(TypeError, takewhile, isEven, N(s))
2018            self.assertRaises(ZeroDivisionError, list, takewhile(isEven, E(s)))
2019
2020    def test_dropwhile(self):
2021        for s in (range(10), range(0), range(1000), (7,11), range(2000,2200,5)):
2022            for g in (G, I, Ig, S, L, R):
2023                tgt = []
2024                for elem in g(s):
2025                    if not tgt and isOdd(elem): continue
2026                    tgt.append(elem)
2027                self.assertEqual(list(dropwhile(isOdd, g(s))), tgt)
2028            self.assertRaises(TypeError, dropwhile, isOdd, X(s))
2029            self.assertRaises(TypeError, dropwhile, isOdd, N(s))
2030            self.assertRaises(ZeroDivisionError, list, dropwhile(isOdd, E(s)))
2031
2032    def test_tee(self):
2033        for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
2034            for g in (G, I, Ig, S, L, R):
2035                it1, it2 = tee(g(s))
2036                self.assertEqual(list(it1), list(g(s)))
2037                self.assertEqual(list(it2), list(g(s)))
2038            self.assertRaises(TypeError, tee, X(s))
2039            self.assertRaises(TypeError, tee, N(s))
2040            self.assertRaises(ZeroDivisionError, list, tee(E(s))[0])
2041
2042class LengthTransparency(unittest.TestCase):
2043
2044    def test_repeat(self):
2045        self.assertEqual(operator.length_hint(repeat(None, 50)), 50)
2046        self.assertEqual(operator.length_hint(repeat(None, 0)), 0)
2047        self.assertEqual(operator.length_hint(repeat(None), 12), 12)
2048
2049    def test_repeat_with_negative_times(self):
2050        self.assertEqual(operator.length_hint(repeat(None, -1)), 0)
2051        self.assertEqual(operator.length_hint(repeat(None, -2)), 0)
2052        self.assertEqual(operator.length_hint(repeat(None, times=-1)), 0)
2053        self.assertEqual(operator.length_hint(repeat(None, times=-2)), 0)
2054
2055class RegressionTests(unittest.TestCase):
2056
2057    def test_sf_793826(self):
2058        # Fix Armin Rigo's successful efforts to wreak havoc
2059
2060        def mutatingtuple(tuple1, f, tuple2):
2061            # this builds a tuple t which is a copy of tuple1,
2062            # then calls f(t), then mutates t to be equal to tuple2
2063            # (needs len(tuple1) == len(tuple2)).
2064            def g(value, first=[1]):
2065                if first:
2066                    del first[:]
2067                    f(next(z))
2068                return value
2069            items = list(tuple2)
2070            items[1:1] = list(tuple1)
2071            gen = map(g, items)
2072            z = zip(*[gen]*len(tuple1))
2073            next(z)
2074
2075        def f(t):
2076            global T
2077            T = t
2078            first[:] = list(T)
2079
2080        first = []
2081        mutatingtuple((1,2,3), f, (4,5,6))
2082        second = list(T)
2083        self.assertEqual(first, second)
2084
2085
2086    def test_sf_950057(self):
2087        # Make sure that chain() and cycle() catch exceptions immediately
2088        # rather than when shifting between input sources
2089
2090        def gen1():
2091            hist.append(0)
2092            yield 1
2093            hist.append(1)
2094            raise AssertionError
2095            hist.append(2)
2096
2097        def gen2(x):
2098            hist.append(3)
2099            yield 2
2100            hist.append(4)
2101
2102        hist = []
2103        self.assertRaises(AssertionError, list, chain(gen1(), gen2(False)))
2104        self.assertEqual(hist, [0,1])
2105
2106        hist = []
2107        self.assertRaises(AssertionError, list, chain(gen1(), gen2(True)))
2108        self.assertEqual(hist, [0,1])
2109
2110        hist = []
2111        self.assertRaises(AssertionError, list, cycle(gen1()))
2112        self.assertEqual(hist, [0,1])
2113
2114    @support.skip_if_pgo_task
2115    def test_long_chain_of_empty_iterables(self):
2116        # Make sure itertools.chain doesn't run into recursion limits when
2117        # dealing with long chains of empty iterables. Even with a high
2118        # number this would probably only fail in Py_DEBUG mode.
2119        it = chain.from_iterable(() for unused in range(10000000))
2120        with self.assertRaises(StopIteration):
2121            next(it)
2122
2123    def test_issue30347_1(self):
2124        def f(n):
2125            if n == 5:
2126                list(b)
2127            return n != 6
2128        for (k, b) in groupby(range(10), f):
2129            list(b)  # shouldn't crash
2130
2131    def test_issue30347_2(self):
2132        class K:
2133            def __init__(self, v):
2134                pass
2135            def __eq__(self, other):
2136                nonlocal i
2137                i += 1
2138                if i == 1:
2139                    next(g, None)
2140                return True
2141        i = 0
2142        g = next(groupby(range(10), K))[1]
2143        for j in range(2):
2144            next(g, None)  # shouldn't crash
2145
2146
2147class SubclassWithKwargsTest(unittest.TestCase):
2148    def test_keywords_in_subclass(self):
2149        # count is not subclassable...
2150        for cls in (repeat, zip, filter, filterfalse, chain, map,
2151                    starmap, islice, takewhile, dropwhile, cycle, compress):
2152            class Subclass(cls):
2153                def __init__(self, newarg=None, *args):
2154                    cls.__init__(self, *args)
2155            try:
2156                Subclass(newarg=1)
2157            except TypeError as err:
2158                # we expect type errors because of wrong argument count
2159                self.assertNotIn("keyword arguments", err.args[0])
2160
2161@support.cpython_only
2162class SizeofTest(unittest.TestCase):
2163    def setUp(self):
2164        self.ssize_t = struct.calcsize('n')
2165
2166    check_sizeof = support.check_sizeof
2167
2168    def test_product_sizeof(self):
2169        basesize = support.calcobjsize('3Pi')
2170        check = self.check_sizeof
2171        check(product('ab', '12'), basesize + 2 * self.ssize_t)
2172        check(product(*(('abc',) * 10)), basesize + 10 * self.ssize_t)
2173
2174    def test_combinations_sizeof(self):
2175        basesize = support.calcobjsize('3Pni')
2176        check = self.check_sizeof
2177        check(combinations('abcd', 3), basesize + 3 * self.ssize_t)
2178        check(combinations(range(10), 4), basesize + 4 * self.ssize_t)
2179
2180    def test_combinations_with_replacement_sizeof(self):
2181        cwr = combinations_with_replacement
2182        basesize = support.calcobjsize('3Pni')
2183        check = self.check_sizeof
2184        check(cwr('abcd', 3), basesize + 3 * self.ssize_t)
2185        check(cwr(range(10), 4), basesize + 4 * self.ssize_t)
2186
2187    def test_permutations_sizeof(self):
2188        basesize = support.calcobjsize('4Pni')
2189        check = self.check_sizeof
2190        check(permutations('abcd'),
2191              basesize + 4 * self.ssize_t + 4 * self.ssize_t)
2192        check(permutations('abcd', 3),
2193              basesize + 4 * self.ssize_t + 3 * self.ssize_t)
2194        check(permutations('abcde', 3),
2195              basesize + 5 * self.ssize_t + 3 * self.ssize_t)
2196        check(permutations(range(10), 4),
2197              basesize + 10 * self.ssize_t + 4 * self.ssize_t)
2198
2199
2200libreftest = """ Doctest for examples in the library reference: libitertools.tex
2201
2202
2203>>> amounts = [120.15, 764.05, 823.14]
2204>>> for checknum, amount in zip(count(1200), amounts):
2205...     print('Check %d is for $%.2f' % (checknum, amount))
2206...
2207Check 1200 is for $120.15
2208Check 1201 is for $764.05
2209Check 1202 is for $823.14
2210
2211>>> import operator
2212>>> for cube in map(operator.pow, range(1,4), repeat(3)):
2213...    print(cube)
2214...
22151
22168
221727
2218
2219>>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele']
2220>>> for name in islice(reportlines, 3, None, 2):
2221...    print(name.title())
2222...
2223Alex
2224Laura
2225Martin
2226Walter
2227Samuele
2228
2229>>> from operator import itemgetter
2230>>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
2231>>> di = sorted(sorted(d.items()), key=itemgetter(1))
2232>>> for k, g in groupby(di, itemgetter(1)):
2233...     print(k, list(map(itemgetter(0), g)))
2234...
22351 ['a', 'c', 'e']
22362 ['b', 'd', 'f']
22373 ['g']
2238
2239# Find runs of consecutive numbers using groupby.  The key to the solution
2240# is differencing with a range so that consecutive numbers all appear in
2241# same group.
2242>>> data = [ 1,  4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
2243>>> for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]):
2244...     print(list(map(operator.itemgetter(1), g)))
2245...
2246[1]
2247[4, 5, 6]
2248[10]
2249[15, 16, 17, 18]
2250[22]
2251[25, 26, 27, 28]
2252
2253>>> def take(n, iterable):
2254...     "Return first n items of the iterable as a list"
2255...     return list(islice(iterable, n))
2256
2257>>> def prepend(value, iterator):
2258...     "Prepend a single value in front of an iterator"
2259...     # prepend(1, [2, 3, 4]) -> 1 2 3 4
2260...     return chain([value], iterator)
2261
2262>>> def enumerate(iterable, start=0):
2263...     return zip(count(start), iterable)
2264
2265>>> def tabulate(function, start=0):
2266...     "Return function(0), function(1), ..."
2267...     return map(function, count(start))
2268
2269>>> import collections
2270>>> def consume(iterator, n=None):
2271...     "Advance the iterator n-steps ahead. If n is None, consume entirely."
2272...     # Use functions that consume iterators at C speed.
2273...     if n is None:
2274...         # feed the entire iterator into a zero-length deque
2275...         collections.deque(iterator, maxlen=0)
2276...     else:
2277...         # advance to the empty slice starting at position n
2278...         next(islice(iterator, n, n), None)
2279
2280>>> def nth(iterable, n, default=None):
2281...     "Returns the nth item or a default value"
2282...     return next(islice(iterable, n, None), default)
2283
2284>>> def all_equal(iterable):
2285...     "Returns True if all the elements are equal to each other"
2286...     g = groupby(iterable)
2287...     return next(g, True) and not next(g, False)
2288
2289>>> def quantify(iterable, pred=bool):
2290...     "Count how many times the predicate is true"
2291...     return sum(map(pred, iterable))
2292
2293>>> def padnone(iterable):
2294...     "Returns the sequence elements and then returns None indefinitely"
2295...     return chain(iterable, repeat(None))
2296
2297>>> def ncycles(iterable, n):
2298...     "Returns the sequence elements n times"
2299...     return chain(*repeat(iterable, n))
2300
2301>>> def dotproduct(vec1, vec2):
2302...     return sum(map(operator.mul, vec1, vec2))
2303
2304>>> def flatten(listOfLists):
2305...     return list(chain.from_iterable(listOfLists))
2306
2307>>> def repeatfunc(func, times=None, *args):
2308...     "Repeat calls to func with specified arguments."
2309...     "   Example:  repeatfunc(random.random)"
2310...     if times is None:
2311...         return starmap(func, repeat(args))
2312...     else:
2313...         return starmap(func, repeat(args, times))
2314
2315>>> def pairwise(iterable):
2316...     "s -> (s0,s1), (s1,s2), (s2, s3), ..."
2317...     a, b = tee(iterable)
2318...     try:
2319...         next(b)
2320...     except StopIteration:
2321...         pass
2322...     return zip(a, b)
2323
2324>>> def grouper(n, iterable, fillvalue=None):
2325...     "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
2326...     args = [iter(iterable)] * n
2327...     return zip_longest(*args, fillvalue=fillvalue)
2328
2329>>> def roundrobin(*iterables):
2330...     "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
2331...     # Recipe credited to George Sakkis
2332...     pending = len(iterables)
2333...     nexts = cycle(iter(it).__next__ for it in iterables)
2334...     while pending:
2335...         try:
2336...             for next in nexts:
2337...                 yield next()
2338...         except StopIteration:
2339...             pending -= 1
2340...             nexts = cycle(islice(nexts, pending))
2341
2342>>> def powerset(iterable):
2343...     "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
2344...     s = list(iterable)
2345...     return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
2346
2347>>> def unique_everseen(iterable, key=None):
2348...     "List unique elements, preserving order. Remember all elements ever seen."
2349...     # unique_everseen('AAAABBBCCDAABBB') --> A B C D
2350...     # unique_everseen('ABBCcAD', str.lower) --> A B C D
2351...     seen = set()
2352...     seen_add = seen.add
2353...     if key is None:
2354...         for element in iterable:
2355...             if element not in seen:
2356...                 seen_add(element)
2357...                 yield element
2358...     else:
2359...         for element in iterable:
2360...             k = key(element)
2361...             if k not in seen:
2362...                 seen_add(k)
2363...                 yield element
2364
2365>>> def unique_justseen(iterable, key=None):
2366...     "List unique elements, preserving order. Remember only the element just seen."
2367...     # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
2368...     # unique_justseen('ABBCcAD', str.lower) --> A B C A D
2369...     return map(next, map(itemgetter(1), groupby(iterable, key)))
2370
2371>>> def first_true(iterable, default=False, pred=None):
2372...     '''Returns the first true value in the iterable.
2373...
2374...     If no true value is found, returns *default*
2375...
2376...     If *pred* is not None, returns the first item
2377...     for which pred(item) is true.
2378...
2379...     '''
2380...     # first_true([a,b,c], x) --> a or b or c or x
2381...     # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
2382...     return next(filter(pred, iterable), default)
2383
2384>>> def nth_combination(iterable, r, index):
2385...     'Equivalent to list(combinations(iterable, r))[index]'
2386...     pool = tuple(iterable)
2387...     n = len(pool)
2388...     if r < 0 or r > n:
2389...         raise ValueError
2390...     c = 1
2391...     k = min(r, n-r)
2392...     for i in range(1, k+1):
2393...         c = c * (n - k + i) // i
2394...     if index < 0:
2395...         index += c
2396...     if index < 0 or index >= c:
2397...         raise IndexError
2398...     result = []
2399...     while r:
2400...         c, n, r = c*r//n, n-1, r-1
2401...         while index >= c:
2402...             index -= c
2403...             c, n = c*(n-r)//n, n-1
2404...         result.append(pool[-1-n])
2405...     return tuple(result)
2406
2407
2408This is not part of the examples but it tests to make sure the definitions
2409perform as purported.
2410
2411>>> take(10, count())
2412[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2413
2414>>> list(prepend(1, [2, 3, 4]))
2415[1, 2, 3, 4]
2416
2417>>> list(enumerate('abc'))
2418[(0, 'a'), (1, 'b'), (2, 'c')]
2419
2420>>> list(islice(tabulate(lambda x: 2*x), 4))
2421[0, 2, 4, 6]
2422
2423>>> it = iter(range(10))
2424>>> consume(it, 3)
2425>>> next(it)
24263
2427>>> consume(it)
2428>>> next(it, 'Done')
2429'Done'
2430
2431>>> nth('abcde', 3)
2432'd'
2433
2434>>> nth('abcde', 9) is None
2435True
2436
2437>>> [all_equal(s) for s in ('', 'A', 'AAAA', 'AAAB', 'AAABA')]
2438[True, True, True, False, False]
2439
2440>>> quantify(range(99), lambda x: x%2==0)
244150
2442
2443>>> a = [[1, 2, 3], [4, 5, 6]]
2444>>> flatten(a)
2445[1, 2, 3, 4, 5, 6]
2446
2447>>> list(repeatfunc(pow, 5, 2, 3))
2448[8, 8, 8, 8, 8]
2449
2450>>> import random
2451>>> take(5, map(int, repeatfunc(random.random)))
2452[0, 0, 0, 0, 0]
2453
2454>>> list(pairwise('abcd'))
2455[('a', 'b'), ('b', 'c'), ('c', 'd')]
2456
2457>>> list(pairwise([]))
2458[]
2459
2460>>> list(pairwise('a'))
2461[]
2462
2463>>> list(islice(padnone('abc'), 0, 6))
2464['a', 'b', 'c', None, None, None]
2465
2466>>> list(ncycles('abc', 3))
2467['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
2468
2469>>> dotproduct([1,2,3], [4,5,6])
247032
2471
2472>>> list(grouper(3, 'abcdefg', 'x'))
2473[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]
2474
2475>>> list(roundrobin('abc', 'd', 'ef'))
2476['a', 'd', 'e', 'b', 'f', 'c']
2477
2478>>> list(powerset([1,2,3]))
2479[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
2480
2481>>> all(len(list(powerset(range(n)))) == 2**n for n in range(18))
2482True
2483
2484>>> list(powerset('abcde')) == sorted(sorted(set(powerset('abcde'))), key=len)
2485True
2486
2487>>> list(unique_everseen('AAAABBBCCDAABBB'))
2488['A', 'B', 'C', 'D']
2489
2490>>> list(unique_everseen('ABBCcAD', str.lower))
2491['A', 'B', 'C', 'D']
2492
2493>>> list(unique_justseen('AAAABBBCCDAABBB'))
2494['A', 'B', 'C', 'D', 'A', 'B']
2495
2496>>> list(unique_justseen('ABBCcAD', str.lower))
2497['A', 'B', 'C', 'A', 'D']
2498
2499>>> first_true('ABC0DEF1', '9', str.isdigit)
2500'0'
2501
2502>>> population = 'ABCDEFGH'
2503>>> for r in range(len(population) + 1):
2504...     seq = list(combinations(population, r))
2505...     for i in range(len(seq)):
2506...         assert nth_combination(population, r, i) == seq[i]
2507...     for i in range(-len(seq), 0):
2508...         assert nth_combination(population, r, i) == seq[i]
2509
2510
2511"""
2512
2513__test__ = {'libreftest' : libreftest}
2514
2515def test_main(verbose=None):
2516    test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC,
2517                    RegressionTests, LengthTransparency,
2518                    SubclassWithKwargsTest, TestExamples,
2519                    TestPurePythonRoughEquivalents,
2520                    SizeofTest)
2521    support.run_unittest(*test_classes)
2522
2523    # verify reference counting
2524    if verbose and hasattr(sys, "gettotalrefcount"):
2525        import gc
2526        counts = [None] * 5
2527        for i in range(len(counts)):
2528            support.run_unittest(*test_classes)
2529            gc.collect()
2530            counts[i] = sys.gettotalrefcount()
2531        print(counts)
2532
2533    # doctest the examples in the library reference
2534    support.run_doctest(sys.modules[__name__], verbose)
2535
2536if __name__ == "__main__":
2537    test_main(verbose=True)
2538