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