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