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