Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 3 | from itertools import * |
Raymond Hettinger | a9f6092 | 2004-10-17 16:40:14 +0000 | [diff] [blame] | 4 | from weakref import proxy |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 5 | import sys |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 6 | import operator |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 7 | import random |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 8 | from functools import reduce |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | maxsize = support.MAX_Py_ssize_t |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 10 | minsize = -maxsize-1 |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 12 | def lzip(*args): |
| 13 | return list(zip(*args)) |
| 14 | |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 15 | def onearg(x): |
| 16 | 'Test function of one argument' |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 17 | return 2*x |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 18 | |
| 19 | def errfunc(*args): |
| 20 | 'Test function that raises an error' |
| 21 | raise ValueError |
| 22 | |
| 23 | def gen3(): |
| 24 | 'Non-restartable source sequence' |
| 25 | for i in (0, 1, 2): |
| 26 | yield i |
| 27 | |
| 28 | def isEven(x): |
| 29 | 'Test predicate' |
| 30 | return x%2==0 |
| 31 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 32 | def isOdd(x): |
| 33 | 'Test predicate' |
| 34 | return x%2==1 |
| 35 | |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 36 | class StopNow: |
| 37 | 'Class emulating an empty iterable.' |
| 38 | def __iter__(self): |
| 39 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 40 | def __next__(self): |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 41 | raise StopIteration |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 42 | |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 43 | def take(n, seq): |
| 44 | 'Convenience function for partially consuming a long of infinite iterable' |
| 45 | return list(islice(seq, n)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 46 | |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 47 | def prod(iterable): |
| 48 | return reduce(operator.mul, iterable, 1) |
| 49 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 50 | def fact(n): |
| 51 | 'Factorial' |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 52 | return prod(range(1, n+1)) |
| 53 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 54 | class TestBasicOps(unittest.TestCase): |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 55 | def test_chain(self): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 56 | |
| 57 | def chain2(*iterables): |
| 58 | 'Pure python version in the docs' |
| 59 | for it in iterables: |
| 60 | for element in it: |
| 61 | yield element |
| 62 | |
| 63 | for c in (chain, chain2): |
| 64 | self.assertEqual(list(c('abc', 'def')), list('abcdef')) |
| 65 | self.assertEqual(list(c('abc')), list('abc')) |
| 66 | self.assertEqual(list(c('')), []) |
| 67 | self.assertEqual(take(4, c('abc', 'def')), list('abcd')) |
| 68 | self.assertRaises(TypeError, list,c(2, 3)) |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 69 | |
| 70 | def test_chain_from_iterable(self): |
| 71 | self.assertEqual(list(chain.from_iterable(['abc', 'def'])), list('abcdef')) |
| 72 | self.assertEqual(list(chain.from_iterable(['abc'])), list('abc')) |
| 73 | self.assertEqual(list(chain.from_iterable([''])), []) |
| 74 | self.assertEqual(take(4, chain.from_iterable(['abc', 'def'])), list('abcd')) |
| 75 | self.assertRaises(TypeError, list, chain.from_iterable([2, 3])) |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 76 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 77 | def test_combinations(self): |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame^] | 78 | self.assertRaises(TypeError, combinations, 'abc') # missing r argument |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 79 | self.assertRaises(TypeError, combinations, 'abc', 2, 1) # too many arguments |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 80 | self.assertRaises(TypeError, combinations, None) # pool is not iterable |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 81 | self.assertRaises(ValueError, combinations, 'abc', -2) # r is negative |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame^] | 82 | self.assertEqual(list(combinations('abc', 32)), []) # r > n |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 83 | self.assertEqual(list(combinations(range(4), 3)), |
| 84 | [(0,1,2), (0,1,3), (0,2,3), (1,2,3)]) |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 85 | |
| 86 | def combinations1(iterable, r): |
| 87 | 'Pure python version shown in the docs' |
| 88 | pool = tuple(iterable) |
| 89 | n = len(pool) |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame^] | 90 | if r > n: |
| 91 | return |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 92 | indices = list(range(r)) |
| 93 | yield tuple(pool[i] for i in indices) |
| 94 | while 1: |
| 95 | for i in reversed(range(r)): |
| 96 | if indices[i] != i + n - r: |
| 97 | break |
| 98 | else: |
| 99 | return |
| 100 | indices[i] += 1 |
| 101 | for j in range(i+1, r): |
| 102 | indices[j] = indices[j-1] + 1 |
| 103 | yield tuple(pool[i] for i in indices) |
| 104 | |
| 105 | def combinations2(iterable, r): |
| 106 | 'Pure python version shown in the docs' |
| 107 | pool = tuple(iterable) |
| 108 | n = len(pool) |
| 109 | for indices in permutations(range(n), r): |
| 110 | if sorted(indices) == list(indices): |
| 111 | yield tuple(pool[i] for i in indices) |
| 112 | |
| 113 | for n in range(7): |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 114 | values = [5*x-12 for x in range(n)] |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame^] | 115 | for r in range(n+2): |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 116 | result = list(combinations(values, r)) |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame^] | 117 | 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] | 118 | self.assertEqual(len(result), len(set(result))) # no repeats |
| 119 | self.assertEqual(result, sorted(result)) # lexicographic order |
| 120 | for c in result: |
| 121 | self.assertEqual(len(c), r) # r-length combinations |
| 122 | self.assertEqual(len(set(c)), r) # no duplicate elements |
| 123 | self.assertEqual(list(c), sorted(c)) # keep original ordering |
| 124 | self.assert_(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] | 125 | self.assertEqual(list(c), |
| 126 | [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] | 127 | self.assertEqual(result, list(combinations1(values, r))) # matches first pure python version |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame^] | 128 | self.assertEqual(result, list(combinations2(values, r))) # matches second pure python version |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 129 | |
| 130 | # Test implementation detail: tuple re-use |
| 131 | self.assertEqual(len(set(map(id, combinations('abcde', 3)))), 1) |
| 132 | self.assertNotEqual(len(set(map(id, list(combinations('abcde', 3))))), 1) |
| 133 | |
| 134 | def test_permutations(self): |
| 135 | self.assertRaises(TypeError, permutations) # too few arguments |
| 136 | self.assertRaises(TypeError, permutations, 'abc', 2, 1) # too many arguments |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 137 | self.assertRaises(TypeError, permutations, None) # pool is not iterable |
| 138 | self.assertRaises(ValueError, permutations, 'abc', -2) # r is negative |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame^] | 139 | self.assertEqual(list(permutations('abc', 32)), []) # r > n |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 140 | 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] | 141 | self.assertEqual(list(permutations(range(3), 2)), |
| 142 | [(0,1), (0,2), (1,0), (1,2), (2,0), (2,1)]) |
| 143 | |
| 144 | def permutations1(iterable, r=None): |
| 145 | 'Pure python version shown in the docs' |
| 146 | pool = tuple(iterable) |
| 147 | n = len(pool) |
| 148 | r = n if r is None else r |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame^] | 149 | if r > n: |
| 150 | return |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 151 | indices = list(range(n)) |
| 152 | cycles = list(range(n-r+1, n+1))[::-1] |
| 153 | yield tuple(pool[i] for i in indices[:r]) |
| 154 | while n: |
| 155 | for i in reversed(range(r)): |
| 156 | cycles[i] -= 1 |
| 157 | if cycles[i] == 0: |
| 158 | indices[i:] = indices[i+1:] + indices[i:i+1] |
| 159 | cycles[i] = n - i |
| 160 | else: |
| 161 | j = cycles[i] |
| 162 | indices[i], indices[-j] = indices[-j], indices[i] |
| 163 | yield tuple(pool[i] for i in indices[:r]) |
| 164 | break |
| 165 | else: |
| 166 | return |
| 167 | |
| 168 | def permutations2(iterable, r=None): |
| 169 | 'Pure python version shown in the docs' |
| 170 | pool = tuple(iterable) |
| 171 | n = len(pool) |
| 172 | r = n if r is None else r |
| 173 | for indices in product(range(n), repeat=r): |
| 174 | if len(set(indices)) == r: |
| 175 | yield tuple(pool[i] for i in indices) |
| 176 | |
| 177 | for n in range(7): |
| 178 | values = [5*x-12 for x in range(n)] |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame^] | 179 | for r in range(n+2): |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 180 | result = list(permutations(values, r)) |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame^] | 181 | 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] | 182 | self.assertEqual(len(result), len(set(result))) # no repeats |
| 183 | self.assertEqual(result, sorted(result)) # lexicographic order |
| 184 | for p in result: |
| 185 | self.assertEqual(len(p), r) # r-length permutations |
| 186 | self.assertEqual(len(set(p)), r) # no duplicate elements |
| 187 | self.assert_(all(e in values for e in p)) # elements taken from input iterable |
| 188 | self.assertEqual(result, list(permutations1(values, r))) # matches first pure python version |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame^] | 189 | self.assertEqual(result, list(permutations2(values, r))) # matches second pure python version |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 190 | if r == n: |
| 191 | self.assertEqual(result, list(permutations(values, None))) # test r as None |
| 192 | self.assertEqual(result, list(permutations(values))) # test default r |
| 193 | |
| 194 | # Test implementation detail: tuple re-use |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 195 | self.assertEqual(len(set(map(id, permutations('abcde', 3)))), 1) |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 196 | self.assertNotEqual(len(set(map(id, list(permutations('abcde', 3))))), 1) |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 197 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 198 | def test_count(self): |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 199 | self.assertEqual(lzip('abc',count()), [('a', 0), ('b', 1), ('c', 2)]) |
| 200 | self.assertEqual(lzip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)]) |
| 201 | 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] | 202 | self.assertEqual(take(2, zip('abc',count(-1))), [('a', -1), ('b', 0)]) |
| 203 | self.assertEqual(take(2, zip('abc',count(-3))), [('a', -3), ('b', -2)]) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 204 | self.assertRaises(TypeError, count, 2, 3) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 205 | self.assertRaises(TypeError, count, 'a') |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 206 | self.assertEqual(list(islice(count(maxsize-5), 10)), |
| 207 | list(range(maxsize-5, maxsize+5))) |
| 208 | self.assertEqual(list(islice(count(-maxsize-5), 10)), |
| 209 | list(range(-maxsize-5, -maxsize+5))) |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 210 | c = count(3) |
| 211 | self.assertEqual(repr(c), 'count(3)') |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 212 | next(c) |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 213 | self.assertEqual(repr(c), 'count(4)') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 214 | c = count(-9) |
| 215 | self.assertEqual(repr(c), 'count(-9)') |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 216 | next(c) |
| 217 | self.assertEqual(next(c), -8) |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 218 | for i in (-sys.maxsize-5, -sys.maxsize+5 ,-10, -1, 0, 10, sys.maxsize-5, sys.maxsize+5): |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 219 | # Test repr (ignoring the L in longs) |
| 220 | r1 = repr(count(i)).replace('L', '') |
| 221 | r2 = 'count(%r)'.__mod__(i).replace('L', '') |
| 222 | self.assertEqual(r1, r2) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 223 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 224 | def test_cycle(self): |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 225 | self.assertEqual(take(10, cycle('abc')), list('abcabcabca')) |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 226 | self.assertEqual(list(cycle('')), []) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 227 | self.assertRaises(TypeError, cycle) |
| 228 | self.assertRaises(TypeError, cycle, 5) |
| 229 | 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] | 230 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 231 | def test_groupby(self): |
| 232 | # Check whether it accepts arguments correctly |
| 233 | self.assertEqual([], list(groupby([]))) |
| 234 | self.assertEqual([], list(groupby([], key=id))) |
| 235 | self.assertRaises(TypeError, list, groupby('abc', [])) |
| 236 | self.assertRaises(TypeError, groupby, None) |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 237 | self.assertRaises(TypeError, groupby, 'abc', lambda x:x, 10) |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 238 | |
| 239 | # Check normal input |
| 240 | s = [(0, 10, 20), (0, 11,21), (0,12,21), (1,13,21), (1,14,22), |
| 241 | (2,15,22), (3,16,23), (3,17,23)] |
| 242 | dup = [] |
| 243 | for k, g in groupby(s, lambda r:r[0]): |
| 244 | for elem in g: |
| 245 | self.assertEqual(k, elem[0]) |
| 246 | dup.append(elem) |
| 247 | self.assertEqual(s, dup) |
| 248 | |
| 249 | # Check nested case |
| 250 | dup = [] |
| 251 | for k, g in groupby(s, lambda r:r[0]): |
| 252 | for ik, ig in groupby(g, lambda r:r[2]): |
| 253 | for elem in ig: |
| 254 | self.assertEqual(k, elem[0]) |
| 255 | self.assertEqual(ik, elem[2]) |
| 256 | dup.append(elem) |
| 257 | self.assertEqual(s, dup) |
| 258 | |
| 259 | # Check case where inner iterator is not used |
| 260 | keys = [k for k, g in groupby(s, lambda r:r[0])] |
| 261 | expectedkeys = set([r[0] for r in s]) |
| 262 | self.assertEqual(set(keys), expectedkeys) |
| 263 | self.assertEqual(len(keys), len(expectedkeys)) |
| 264 | |
| 265 | # Exercise pipes and filters style |
| 266 | s = 'abracadabra' |
| 267 | # sort s | uniq |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 268 | r = [k for k, g in groupby(sorted(s))] |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 269 | self.assertEqual(r, ['a', 'b', 'c', 'd', 'r']) |
| 270 | # sort s | uniq -d |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 271 | 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] | 272 | self.assertEqual(r, ['a', 'b', 'r']) |
| 273 | # sort s | uniq -c |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 274 | r = [(len(list(g)), k) for k, g in groupby(sorted(s))] |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 275 | self.assertEqual(r, [(5, 'a'), (2, 'b'), (1, 'c'), (1, 'd'), (2, 'r')]) |
| 276 | # sort s | uniq -c | sort -rn | head -3 |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 277 | 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] | 278 | self.assertEqual(r, [(5, 'a'), (2, 'r'), (2, 'b')]) |
| 279 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 280 | # iter.__next__ failure |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 281 | class ExpectedError(Exception): |
| 282 | pass |
| 283 | def delayed_raise(n=0): |
| 284 | for i in range(n): |
| 285 | yield 'yo' |
| 286 | raise ExpectedError |
| 287 | def gulp(iterable, keyp=None, func=list): |
| 288 | return [func(g) for k, g in groupby(iterable, keyp)] |
| 289 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 290 | # iter.__next__ failure on outer object |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 291 | self.assertRaises(ExpectedError, gulp, delayed_raise(0)) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 292 | # iter.__next__ failure on inner object |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 293 | self.assertRaises(ExpectedError, gulp, delayed_raise(1)) |
| 294 | |
| 295 | # __cmp__ failure |
| 296 | class DummyCmp: |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 297 | def __eq__(self, dst): |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 298 | raise ExpectedError |
| 299 | s = [DummyCmp(), DummyCmp(), None] |
| 300 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 301 | # __eq__ failure on outer object |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 302 | self.assertRaises(ExpectedError, gulp, s, func=id) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 303 | # __eq__ failure on inner object |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 304 | self.assertRaises(ExpectedError, gulp, s) |
| 305 | |
| 306 | # keyfunc failure |
| 307 | def keyfunc(obj): |
| 308 | if keyfunc.skip > 0: |
| 309 | keyfunc.skip -= 1 |
| 310 | return obj |
| 311 | else: |
| 312 | raise ExpectedError |
| 313 | |
| 314 | # keyfunc failure on outer object |
| 315 | keyfunc.skip = 0 |
| 316 | self.assertRaises(ExpectedError, gulp, [None], keyfunc) |
| 317 | keyfunc.skip = 1 |
| 318 | self.assertRaises(ExpectedError, gulp, [None, None], keyfunc) |
| 319 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 320 | def test_filter(self): |
| 321 | self.assertEqual(list(filter(isEven, range(6))), [0,2,4]) |
| 322 | self.assertEqual(list(filter(None, [0,1,0,2,0])), [1,2]) |
| 323 | self.assertEqual(list(filter(bool, [0,1,0,2,0])), [1,2]) |
| 324 | self.assertEqual(take(4, filter(isEven, count())), [0,2,4,6]) |
| 325 | self.assertRaises(TypeError, filter) |
| 326 | self.assertRaises(TypeError, filter, lambda x:x) |
| 327 | self.assertRaises(TypeError, filter, lambda x:x, range(6), 7) |
| 328 | self.assertRaises(TypeError, filter, isEven, 3) |
| 329 | self.assertRaises(TypeError, next, filter(range(6), range(6))) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 330 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 331 | def test_filterfalse(self): |
| 332 | self.assertEqual(list(filterfalse(isEven, range(6))), [1,3,5]) |
| 333 | self.assertEqual(list(filterfalse(None, [0,1,0,2,0])), [0,0,0]) |
| 334 | self.assertEqual(list(filterfalse(bool, [0,1,0,2,0])), [0,0,0]) |
| 335 | self.assertEqual(take(4, filterfalse(isEven, count())), [1,3,5,7]) |
| 336 | self.assertRaises(TypeError, filterfalse) |
| 337 | self.assertRaises(TypeError, filterfalse, lambda x:x) |
| 338 | self.assertRaises(TypeError, filterfalse, lambda x:x, range(6), 7) |
| 339 | self.assertRaises(TypeError, filterfalse, isEven, 3) |
| 340 | self.assertRaises(TypeError, next, filterfalse(range(6), range(6))) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 341 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 342 | def test_zip(self): |
| 343 | # XXX This is rather silly now that builtin zip() calls zip()... |
| 344 | ans = [(x,y) for x, y in zip('abc',count())] |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 345 | self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 346 | self.assertEqual(list(zip('abc', range(6))), lzip('abc', range(6))) |
| 347 | self.assertEqual(list(zip('abcdef', range(3))), lzip('abcdef', range(3))) |
| 348 | self.assertEqual(take(3,zip('abcdef', count())), lzip('abcdef', range(3))) |
| 349 | self.assertEqual(list(zip('abcdef')), lzip('abcdef')) |
| 350 | self.assertEqual(list(zip()), lzip()) |
| 351 | self.assertRaises(TypeError, zip, 3) |
| 352 | self.assertRaises(TypeError, zip, range(3), 3) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 353 | # Check tuple re-use (implementation detail) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 354 | self.assertEqual([tuple(list(pair)) for pair in zip('abc', 'def')], |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 355 | lzip('abc', 'def')) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 356 | self.assertEqual([pair for pair in zip('abc', 'def')], |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 357 | lzip('abc', 'def')) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 358 | ids = list(map(id, zip('abc', 'def'))) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 359 | self.assertEqual(min(ids), max(ids)) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 360 | ids = list(map(id, list(zip('abc', 'def')))) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 361 | self.assertEqual(len(dict.fromkeys(ids)), len(ids)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 362 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 363 | def test_ziplongest(self): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 364 | for args in [ |
| 365 | ['abc', range(6)], |
| 366 | [range(6), 'abc'], |
| 367 | [range(1000), range(2000,2100), range(3000,3050)], |
| 368 | [range(1000), range(0), range(3000,3050), range(1200), range(1500)], |
| 369 | [range(1000), range(0), range(3000,3050), range(1200), range(1500), range(0)], |
| 370 | ]: |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 371 | target = [tuple([arg[i] if i < len(arg) else None for arg in args]) |
| 372 | for i in range(max(map(len, args)))] |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 373 | self.assertEqual(list(zip_longest(*args)), target) |
| 374 | self.assertEqual(list(zip_longest(*args, **{})), target) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 375 | 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] | 376 | self.assertEqual(list(zip_longest(*args, **dict(fillvalue='X'))), target) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 377 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 378 | 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] | 379 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 380 | self.assertEqual(list(zip_longest()), list(zip())) |
| 381 | self.assertEqual(list(zip_longest([])), list(zip([]))) |
| 382 | self.assertEqual(list(zip_longest('abcdef')), list(zip('abcdef'))) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 383 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 384 | self.assertEqual(list(zip_longest('abc', 'defg', **{})), |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 385 | list(zip(list('abc')+[None], 'defg'))) # empty keyword dict |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 386 | self.assertRaises(TypeError, zip_longest, 3) |
| 387 | self.assertRaises(TypeError, zip_longest, range(3), 3) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 388 | |
| 389 | for stmt in [ |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 390 | "zip_longest('abc', fv=1)", |
| 391 | "zip_longest('abc', fillvalue=1, bogus_keyword=None)", |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 392 | ]: |
| 393 | try: |
| 394 | eval(stmt, globals(), locals()) |
| 395 | except TypeError: |
| 396 | pass |
| 397 | else: |
| 398 | self.fail('Did not raise Type in: ' + stmt) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 399 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 400 | # Check tuple re-use (implementation detail) |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 401 | self.assertEqual([tuple(list(pair)) for pair in zip_longest('abc', 'def')], |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 402 | list(zip('abc', 'def'))) |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 403 | self.assertEqual([pair for pair in zip_longest('abc', 'def')], |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 404 | list(zip('abc', 'def'))) |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 405 | ids = list(map(id, zip_longest('abc', 'def'))) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 406 | self.assertEqual(min(ids), max(ids)) |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 407 | ids = list(map(id, list(zip_longest('abc', 'def')))) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 408 | self.assertEqual(len(dict.fromkeys(ids)), len(ids)) |
| 409 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 410 | def test_product(self): |
| 411 | for args, result in [ |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 412 | ([], [()]), # zero iterables |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 413 | (['ab'], [('a',), ('b',)]), # one iterable |
| 414 | ([range(2), range(3)], [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)]), # two iterables |
| 415 | ([range(0), range(2), range(3)], []), # first iterable with zero length |
| 416 | ([range(2), range(0), range(3)], []), # middle iterable with zero length |
| 417 | ([range(2), range(3), range(0)], []), # last iterable with zero length |
| 418 | ]: |
| 419 | self.assertEqual(list(product(*args)), result) |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 420 | for r in range(4): |
| 421 | self.assertEqual(list(product(*(args*r))), |
| 422 | list(product(*args, **dict(repeat=r)))) |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 423 | self.assertEqual(len(list(product(*[range(7)]*6))), 7**6) |
| 424 | self.assertRaises(TypeError, product, range(6), None) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 425 | |
| 426 | def product1(*args, **kwds): |
| 427 | pools = list(map(tuple, args)) * kwds.get('repeat', 1) |
| 428 | n = len(pools) |
| 429 | if n == 0: |
| 430 | yield () |
| 431 | return |
| 432 | if any(len(pool) == 0 for pool in pools): |
| 433 | return |
| 434 | indices = [0] * n |
| 435 | yield tuple(pool[i] for pool, i in zip(pools, indices)) |
| 436 | while 1: |
| 437 | for i in reversed(range(n)): # right to left |
| 438 | if indices[i] == len(pools[i]) - 1: |
| 439 | continue |
| 440 | indices[i] += 1 |
| 441 | for j in range(i+1, n): |
| 442 | indices[j] = 0 |
| 443 | yield tuple(pool[i] for pool, i in zip(pools, indices)) |
| 444 | break |
| 445 | else: |
| 446 | return |
| 447 | |
| 448 | def product2(*args, **kwds): |
| 449 | 'Pure python version used in docs' |
| 450 | pools = list(map(tuple, args)) * kwds.get('repeat', 1) |
| 451 | result = [[]] |
| 452 | for pool in pools: |
| 453 | result = [x+[y] for x in result for y in pool] |
| 454 | for prod in result: |
| 455 | yield tuple(prod) |
| 456 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 457 | argtypes = ['', 'abc', '', range(0), range(4), dict(a=1, b=2, c=3), |
| 458 | set('abcdefg'), range(11), tuple(range(13))] |
| 459 | for i in range(100): |
| 460 | args = [random.choice(argtypes) for j in range(random.randrange(5))] |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 461 | expected_len = prod(map(len, args)) |
| 462 | self.assertEqual(len(list(product(*args))), expected_len) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 463 | self.assertEqual(list(product(*args)), list(product1(*args))) |
| 464 | self.assertEqual(list(product(*args)), list(product2(*args))) |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 465 | args = map(iter, args) |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 466 | self.assertEqual(len(list(product(*args))), expected_len) |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 467 | |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 468 | # Test implementation detail: tuple re-use |
| 469 | self.assertEqual(len(set(map(id, product('abc', 'def')))), 1) |
| 470 | self.assertNotEqual(len(set(map(id, list(product('abc', 'def'))))), 1) |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 471 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 472 | def test_repeat(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 473 | self.assertEqual(lzip(range(3),repeat('a')), |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 474 | [(0, 'a'), (1, 'a'), (2, 'a')]) |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 475 | self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a']) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 476 | self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a']) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 477 | self.assertEqual(list(repeat('a', 0)), []) |
| 478 | self.assertEqual(list(repeat('a', -3)), []) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 479 | self.assertRaises(TypeError, repeat) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 480 | self.assertRaises(TypeError, repeat, None, 3, 4) |
| 481 | self.assertRaises(TypeError, repeat, None, 'a') |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 482 | r = repeat(1+0j) |
| 483 | self.assertEqual(repr(r), 'repeat((1+0j))') |
| 484 | r = repeat(1+0j, 5) |
| 485 | self.assertEqual(repr(r), 'repeat((1+0j), 5)') |
| 486 | list(r) |
| 487 | self.assertEqual(repr(r), 'repeat((1+0j), 0)') |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 488 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 489 | def test_map(self): |
| 490 | self.assertEqual(list(map(operator.pow, range(3), range(1,7))), |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 491 | [0**1, 1**2, 2**3]) |
Raymond Hettinger | 1dfde1d | 2008-01-22 23:25:35 +0000 | [diff] [blame] | 492 | def tupleize(*args): |
| 493 | return args |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 494 | self.assertEqual(list(map(tupleize, 'abc', range(5))), |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 495 | [('a',0),('b',1),('c',2)]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 496 | self.assertEqual(list(map(tupleize, 'abc', count())), |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 497 | [('a',0),('b',1),('c',2)]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 498 | self.assertEqual(take(2,map(tupleize, 'abc', count())), |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 499 | [('a',0),('b',1)]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 500 | self.assertEqual(list(map(operator.pow, [])), []) |
| 501 | self.assertRaises(TypeError, map) |
| 502 | self.assertRaises(TypeError, list, map(None, range(3), range(3))) |
| 503 | self.assertRaises(TypeError, map, operator.neg) |
| 504 | self.assertRaises(TypeError, next, map(10, range(5))) |
| 505 | self.assertRaises(ValueError, next, map(errfunc, [4], [5])) |
| 506 | self.assertRaises(TypeError, next, map(onearg, [4], [5])) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 507 | |
| 508 | def test_starmap(self): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 509 | self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))), |
| 510 | [0**1, 1**2, 2**3]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 511 | self.assertEqual(take(3, starmap(operator.pow, zip(count(), count(1)))), |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 512 | [0**1, 1**2, 2**3]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 513 | self.assertEqual(list(starmap(operator.pow, [])), []) |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 514 | self.assertEqual(list(starmap(operator.pow, [iter([4,5])])), [4**5]) |
| 515 | self.assertRaises(TypeError, list, starmap(operator.pow, [None])) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 516 | self.assertRaises(TypeError, starmap) |
| 517 | self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra') |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 518 | self.assertRaises(TypeError, next, starmap(10, [(4,5)])) |
| 519 | self.assertRaises(ValueError, next, starmap(errfunc, [(4,5)])) |
| 520 | self.assertRaises(TypeError, next, starmap(onearg, [(4,5)])) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 521 | |
| 522 | def test_islice(self): |
| 523 | for args in [ # islice(args) should agree with range(args) |
| 524 | (10, 20, 3), |
| 525 | (10, 3, 20), |
| 526 | (10, 20), |
| 527 | (10, 3), |
| 528 | (20,) |
| 529 | ]: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 530 | self.assertEqual(list(islice(range(100), *args)), |
| 531 | list(range(*args))) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 532 | |
| 533 | for args, tgtargs in [ # Stop when seqn is exhausted |
| 534 | ((10, 110, 3), ((10, 100, 3))), |
| 535 | ((10, 110), ((10, 100))), |
| 536 | ((110,), (100,)) |
| 537 | ]: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 538 | self.assertEqual(list(islice(range(100), *args)), |
| 539 | list(range(*tgtargs))) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 540 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 541 | # Test stop=None |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 542 | self.assertEqual(list(islice(range(10), None)), list(range(10))) |
| 543 | self.assertEqual(list(islice(range(10), None, None)), list(range(10))) |
| 544 | self.assertEqual(list(islice(range(10), None, None, None)), list(range(10))) |
| 545 | self.assertEqual(list(islice(range(10), 2, None)), list(range(2, 10))) |
| 546 | 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] | 547 | |
Raymond Hettinger | fdf3bd6 | 2005-03-27 20:11:44 +0000 | [diff] [blame] | 548 | # Test number of items consumed SF #1171417 |
| 549 | it = iter(range(10)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 550 | self.assertEqual(list(islice(it, 3)), list(range(3))) |
| 551 | self.assertEqual(list(it), list(range(3, 10))) |
Raymond Hettinger | fdf3bd6 | 2005-03-27 20:11:44 +0000 | [diff] [blame] | 552 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 553 | # Test invalid arguments |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 554 | self.assertRaises(TypeError, islice, range(10)) |
| 555 | self.assertRaises(TypeError, islice, range(10), 1, 2, 3, 4) |
| 556 | self.assertRaises(ValueError, islice, range(10), -5, 10, 1) |
| 557 | self.assertRaises(ValueError, islice, range(10), 1, -5, -1) |
| 558 | self.assertRaises(ValueError, islice, range(10), 1, 10, -1) |
| 559 | self.assertRaises(ValueError, islice, range(10), 1, 10, 0) |
| 560 | self.assertRaises(ValueError, islice, range(10), 'a') |
| 561 | self.assertRaises(ValueError, islice, range(10), 'a', 1) |
| 562 | self.assertRaises(ValueError, islice, range(10), 1, 'a') |
| 563 | self.assertRaises(ValueError, islice, range(10), 'a', 1, 1) |
| 564 | self.assertRaises(ValueError, islice, range(10), 1, 'a', 1) |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 565 | self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 566 | |
| 567 | def test_takewhile(self): |
| 568 | data = [1, 3, 5, 20, 2, 4, 6, 8] |
| 569 | underten = lambda x: x<10 |
| 570 | self.assertEqual(list(takewhile(underten, data)), [1, 3, 5]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 571 | self.assertEqual(list(takewhile(underten, [])), []) |
| 572 | self.assertRaises(TypeError, takewhile) |
| 573 | self.assertRaises(TypeError, takewhile, operator.pow) |
| 574 | self.assertRaises(TypeError, takewhile, operator.pow, [(4,5)], 'extra') |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 575 | self.assertRaises(TypeError, next, takewhile(10, [(4,5)])) |
| 576 | self.assertRaises(ValueError, next, takewhile(errfunc, [(4,5)])) |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 577 | t = takewhile(bool, [1, 1, 1, 0, 0, 0]) |
| 578 | self.assertEqual(list(t), [1, 1, 1]) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 579 | self.assertRaises(StopIteration, next, t) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 580 | |
| 581 | def test_dropwhile(self): |
| 582 | data = [1, 3, 5, 20, 2, 4, 6, 8] |
| 583 | underten = lambda x: x<10 |
| 584 | self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 585 | self.assertEqual(list(dropwhile(underten, [])), []) |
| 586 | self.assertRaises(TypeError, dropwhile) |
| 587 | self.assertRaises(TypeError, dropwhile, operator.pow) |
| 588 | self.assertRaises(TypeError, dropwhile, operator.pow, [(4,5)], 'extra') |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 589 | self.assertRaises(TypeError, next, dropwhile(10, [(4,5)])) |
| 590 | self.assertRaises(ValueError, next, dropwhile(errfunc, [(4,5)])) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 591 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 592 | def test_tee(self): |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 593 | n = 200 |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 594 | def irange(n): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 595 | for i in range(n): |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 596 | yield i |
| 597 | |
| 598 | a, b = tee([]) # test empty iterator |
| 599 | self.assertEqual(list(a), []) |
| 600 | self.assertEqual(list(b), []) |
| 601 | |
| 602 | a, b = tee(irange(n)) # test 100% interleaved |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 603 | self.assertEqual(lzip(a,b), lzip(range(n), range(n))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 604 | |
| 605 | a, b = tee(irange(n)) # test 0% interleaved |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 606 | self.assertEqual(list(a), list(range(n))) |
| 607 | self.assertEqual(list(b), list(range(n))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 608 | |
| 609 | a, b = tee(irange(n)) # test dealloc of leading iterator |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 610 | for i in range(100): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 611 | self.assertEqual(next(a), i) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 612 | del a |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 613 | self.assertEqual(list(b), list(range(n))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 614 | |
| 615 | a, b = tee(irange(n)) # test dealloc of trailing iterator |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 616 | for i in range(100): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 617 | self.assertEqual(next(a), i) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 618 | del b |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 619 | self.assertEqual(list(a), list(range(100, n))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 620 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 621 | for j in range(5): # test randomly interleaved |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 622 | order = [0]*n + [1]*n |
| 623 | random.shuffle(order) |
| 624 | lists = ([], []) |
| 625 | its = tee(irange(n)) |
| 626 | for i in order: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 627 | value = next(its[i]) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 628 | lists[i].append(value) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 629 | self.assertEqual(lists[0], list(range(n))) |
| 630 | self.assertEqual(lists[1], list(range(n))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 631 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 632 | # test argument format checking |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 633 | self.assertRaises(TypeError, tee) |
| 634 | self.assertRaises(TypeError, tee, 3) |
| 635 | self.assertRaises(TypeError, tee, [1,2], 'x') |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 636 | self.assertRaises(TypeError, tee, [1,2], 3, 'x') |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 637 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 638 | # tee object should be instantiable |
| 639 | a, b = tee('abc') |
| 640 | c = type(a)('def') |
| 641 | self.assertEqual(list(c), list('def')) |
Raymond Hettinger | f0c5aec | 2003-10-26 14:25:56 +0000 | [diff] [blame] | 642 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 643 | # test long-lagged and multi-way split |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 644 | a, b, c = tee(range(2000), 3) |
| 645 | for i in range(100): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 646 | self.assertEqual(next(a), i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 647 | self.assertEqual(list(b), list(range(2000))) |
| 648 | self.assertEqual([next(c), next(c)], list(range(2))) |
| 649 | self.assertEqual(list(a), list(range(100,2000))) |
| 650 | self.assertEqual(list(c), list(range(2,2000))) |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 651 | |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 652 | # test values of n |
| 653 | self.assertRaises(TypeError, tee, 'abc', 'invalid') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 654 | self.assertRaises(ValueError, tee, [], -1) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 655 | for n in range(5): |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 656 | result = tee('abc', n) |
| 657 | self.assertEqual(type(result), tuple) |
| 658 | self.assertEqual(len(result), n) |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 659 | self.assertEqual([list(x) for x in result], [list('abc')]*n) |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 660 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 661 | # tee pass-through to copyable iterator |
| 662 | a, b = tee('abc') |
| 663 | c, d = tee(a) |
| 664 | self.assert_(a is c) |
| 665 | |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 666 | # test tee_new |
| 667 | t1, t2 = tee('abc') |
| 668 | tnew = type(t1) |
| 669 | self.assertRaises(TypeError, tnew) |
| 670 | self.assertRaises(TypeError, tnew, 10) |
| 671 | t3 = tnew(t1) |
| 672 | self.assert_(list(t1) == list(t2) == list(t3) == list('abc')) |
Raymond Hettinger | f0c5aec | 2003-10-26 14:25:56 +0000 | [diff] [blame] | 673 | |
Raymond Hettinger | a9f6092 | 2004-10-17 16:40:14 +0000 | [diff] [blame] | 674 | # test that tee objects are weak referencable |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 675 | a, b = tee(range(10)) |
Raymond Hettinger | a9f6092 | 2004-10-17 16:40:14 +0000 | [diff] [blame] | 676 | p = proxy(a) |
| 677 | self.assertEqual(getattr(p, '__class__'), type(b)) |
| 678 | del a |
| 679 | self.assertRaises(ReferenceError, getattr, p, '__class__') |
| 680 | |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 681 | def test_StopIteration(self): |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 682 | self.assertRaises(StopIteration, next, zip()) |
Raymond Hettinger | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame] | 683 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 684 | for f in (chain, cycle, zip, groupby): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 685 | self.assertRaises(StopIteration, next, f([])) |
| 686 | self.assertRaises(StopIteration, next, f(StopNow())) |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 687 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 688 | self.assertRaises(StopIteration, next, islice([], None)) |
| 689 | self.assertRaises(StopIteration, next, islice(StopNow(), None)) |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 690 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 691 | p, q = tee([]) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 692 | self.assertRaises(StopIteration, next, p) |
| 693 | self.assertRaises(StopIteration, next, q) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 694 | p, q = tee(StopNow()) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 695 | self.assertRaises(StopIteration, next, p) |
| 696 | self.assertRaises(StopIteration, next, q) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 697 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 698 | self.assertRaises(StopIteration, next, repeat(None, 0)) |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 699 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 700 | for f in (filter, filterfalse, map, takewhile, dropwhile, starmap): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 701 | self.assertRaises(StopIteration, next, f(lambda x:x, [])) |
| 702 | self.assertRaises(StopIteration, next, f(lambda x:x, StopNow())) |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 703 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 704 | class TestExamples(unittest.TestCase): |
| 705 | |
| 706 | def test_chain(self): |
| 707 | self.assertEqual(''.join(chain('ABC', 'DEF')), 'ABCDEF') |
| 708 | |
| 709 | def test_chain_from_iterable(self): |
| 710 | self.assertEqual(''.join(chain.from_iterable(['ABC', 'DEF'])), 'ABCDEF') |
| 711 | |
| 712 | def test_combinations(self): |
| 713 | self.assertEqual(list(combinations('ABCD', 2)), |
| 714 | [('A','B'), ('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')]) |
| 715 | self.assertEqual(list(combinations(range(4), 3)), |
| 716 | [(0,1,2), (0,1,3), (0,2,3), (1,2,3)]) |
| 717 | |
| 718 | def test_count(self): |
| 719 | self.assertEqual(list(islice(count(10), 5)), [10, 11, 12, 13, 14]) |
| 720 | |
| 721 | def test_cycle(self): |
| 722 | self.assertEqual(list(islice(cycle('ABCD'), 12)), list('ABCDABCDABCD')) |
| 723 | |
| 724 | def test_dropwhile(self): |
| 725 | self.assertEqual(list(dropwhile(lambda x: x<5, [1,4,6,4,1])), [6,4,1]) |
| 726 | |
| 727 | def test_groupby(self): |
| 728 | self.assertEqual([k for k, g in groupby('AAAABBBCCDAABBB')], |
| 729 | list('ABCDAB')) |
| 730 | self.assertEqual([(list(g)) for k, g in groupby('AAAABBBCCD')], |
| 731 | [list('AAAA'), list('BBB'), list('CC'), list('D')]) |
| 732 | |
| 733 | def test_filter(self): |
| 734 | self.assertEqual(list(filter(lambda x: x%2, range(10))), [1,3,5,7,9]) |
| 735 | |
| 736 | def test_filterfalse(self): |
| 737 | self.assertEqual(list(filterfalse(lambda x: x%2, range(10))), [0,2,4,6,8]) |
| 738 | |
| 739 | def test_map(self): |
| 740 | self.assertEqual(list(map(pow, (2,3,10), (5,2,3))), [32, 9, 1000]) |
| 741 | |
| 742 | def test_islice(self): |
| 743 | self.assertEqual(list(islice('ABCDEFG', 2)), list('AB')) |
| 744 | self.assertEqual(list(islice('ABCDEFG', 2, 4)), list('CD')) |
| 745 | self.assertEqual(list(islice('ABCDEFG', 2, None)), list('CDEFG')) |
| 746 | self.assertEqual(list(islice('ABCDEFG', 0, None, 2)), list('ACEG')) |
| 747 | |
| 748 | def test_zip(self): |
| 749 | self.assertEqual(list(zip('ABCD', 'xy')), [('A', 'x'), ('B', 'y')]) |
| 750 | |
| 751 | def test_zip_longest(self): |
| 752 | self.assertEqual(list(zip_longest('ABCD', 'xy', fillvalue='-')), |
| 753 | [('A', 'x'), ('B', 'y'), ('C', '-'), ('D', '-')]) |
| 754 | |
| 755 | def test_permutations(self): |
| 756 | self.assertEqual(list(permutations('ABCD', 2)), |
| 757 | list(map(tuple, 'AB AC AD BA BC BD CA CB CD DA DB DC'.split()))) |
| 758 | self.assertEqual(list(permutations(range(3))), |
| 759 | [(0,1,2), (0,2,1), (1,0,2), (1,2,0), (2,0,1), (2,1,0)]) |
| 760 | |
| 761 | def test_product(self): |
| 762 | self.assertEqual(list(product('ABCD', 'xy')), |
| 763 | list(map(tuple, 'Ax Ay Bx By Cx Cy Dx Dy'.split()))) |
| 764 | self.assertEqual(list(product(range(2), repeat=3)), |
| 765 | [(0,0,0), (0,0,1), (0,1,0), (0,1,1), |
| 766 | (1,0,0), (1,0,1), (1,1,0), (1,1,1)]) |
| 767 | |
| 768 | def test_repeat(self): |
| 769 | self.assertEqual(list(repeat(10, 3)), [10, 10, 10]) |
| 770 | |
| 771 | def test_stapmap(self): |
| 772 | self.assertEqual(list(starmap(pow, [(2,5), (3,2), (10,3)])), |
| 773 | [32, 9, 1000]) |
| 774 | |
| 775 | def test_takewhile(self): |
| 776 | self.assertEqual(list(takewhile(lambda x: x<5, [1,4,6,4,1])), [1,4]) |
| 777 | |
| 778 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 779 | class TestGC(unittest.TestCase): |
| 780 | |
| 781 | def makecycle(self, iterator, container): |
| 782 | container.append(iterator) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 783 | next(iterator) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 784 | del container, iterator |
| 785 | |
| 786 | def test_chain(self): |
| 787 | a = [] |
| 788 | self.makecycle(chain(a), a) |
| 789 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 790 | def test_chain_from_iterable(self): |
| 791 | a = [] |
| 792 | self.makecycle(chain.from_iterable([a]), a) |
| 793 | |
| 794 | def test_combinations(self): |
| 795 | a = [] |
| 796 | self.makecycle(combinations([1,2,a,3], 3), a) |
| 797 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 798 | def test_cycle(self): |
| 799 | a = [] |
| 800 | self.makecycle(cycle([a]*2), a) |
| 801 | |
Raymond Hettinger | ff5dc0e | 2004-09-29 11:40:50 +0000 | [diff] [blame] | 802 | def test_dropwhile(self): |
| 803 | a = [] |
| 804 | self.makecycle(dropwhile(bool, [0, a, a]), a) |
| 805 | |
| 806 | def test_groupby(self): |
| 807 | a = [] |
| 808 | self.makecycle(groupby([a]*2, lambda x:x), a) |
| 809 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 810 | def test_issue2246(self): |
| 811 | # Issue 2246 -- the _grouper iterator was not included in GC |
| 812 | n = 10 |
| 813 | keyfunc = lambda x: x |
| 814 | for i, j in groupby(range(n), key=keyfunc): |
| 815 | keyfunc.__dict__.setdefault('x',[]).append(j) |
| 816 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 817 | def test_filter(self): |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 818 | a = [] |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 819 | self.makecycle(filter(lambda x:True, [a]*2), a) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 820 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 821 | def test_filterfalse(self): |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 822 | a = [] |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 823 | self.makecycle(filterfalse(lambda x:False, a), a) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 824 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 825 | def test_zip(self): |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 826 | a = [] |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 827 | self.makecycle(zip([a]*2, [a]*3), a) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 828 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 829 | def test_zip_longest(self): |
| 830 | a = [] |
| 831 | self.makecycle(zip_longest([a]*2, [a]*3), a) |
| 832 | b = [a, None] |
| 833 | self.makecycle(zip_longest([a]*2, [a]*3, fillvalue=b), a) |
| 834 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 835 | def test_map(self): |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 836 | a = [] |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 837 | self.makecycle(map(lambda x:x, [a]*2), a) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 838 | |
| 839 | def test_islice(self): |
| 840 | a = [] |
| 841 | self.makecycle(islice([a]*2, None), a) |
| 842 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 843 | def test_permutations(self): |
| 844 | a = [] |
| 845 | self.makecycle(permutations([1,2,a,3], 3), a) |
| 846 | |
| 847 | def test_product(self): |
| 848 | a = [] |
| 849 | self.makecycle(product([1,2,a,3], repeat=3), a) |
| 850 | |
Raymond Hettinger | ff5dc0e | 2004-09-29 11:40:50 +0000 | [diff] [blame] | 851 | def test_repeat(self): |
| 852 | a = [] |
| 853 | self.makecycle(repeat(a), a) |
| 854 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 855 | def test_starmap(self): |
| 856 | a = [] |
| 857 | self.makecycle(starmap(lambda *t: t, [(a,a)]*2), a) |
| 858 | |
Raymond Hettinger | ff5dc0e | 2004-09-29 11:40:50 +0000 | [diff] [blame] | 859 | def test_takewhile(self): |
| 860 | a = [] |
| 861 | self.makecycle(takewhile(bool, [1, 0, a, a]), a) |
| 862 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 863 | def R(seqn): |
| 864 | 'Regular generator' |
| 865 | for i in seqn: |
| 866 | yield i |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 867 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 868 | class G: |
| 869 | 'Sequence using __getitem__' |
| 870 | def __init__(self, seqn): |
| 871 | self.seqn = seqn |
| 872 | def __getitem__(self, i): |
| 873 | return self.seqn[i] |
| 874 | |
| 875 | class I: |
| 876 | 'Sequence using iterator protocol' |
| 877 | def __init__(self, seqn): |
| 878 | self.seqn = seqn |
| 879 | self.i = 0 |
| 880 | def __iter__(self): |
| 881 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 882 | def __next__(self): |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 883 | if self.i >= len(self.seqn): raise StopIteration |
| 884 | v = self.seqn[self.i] |
| 885 | self.i += 1 |
| 886 | return v |
| 887 | |
| 888 | class Ig: |
| 889 | 'Sequence using iterator protocol defined with a generator' |
| 890 | def __init__(self, seqn): |
| 891 | self.seqn = seqn |
| 892 | self.i = 0 |
| 893 | def __iter__(self): |
| 894 | for val in self.seqn: |
| 895 | yield val |
| 896 | |
| 897 | class X: |
| 898 | 'Missing __getitem__ and __iter__' |
| 899 | def __init__(self, seqn): |
| 900 | self.seqn = seqn |
| 901 | self.i = 0 |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 902 | def __next__(self): |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 903 | if self.i >= len(self.seqn): raise StopIteration |
| 904 | v = self.seqn[self.i] |
| 905 | self.i += 1 |
| 906 | return v |
| 907 | |
| 908 | class N: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 909 | 'Iterator missing __next__()' |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 910 | def __init__(self, seqn): |
| 911 | self.seqn = seqn |
| 912 | self.i = 0 |
| 913 | def __iter__(self): |
| 914 | return self |
| 915 | |
| 916 | class E: |
| 917 | 'Test propagation of exceptions' |
| 918 | def __init__(self, seqn): |
| 919 | self.seqn = seqn |
| 920 | self.i = 0 |
| 921 | def __iter__(self): |
| 922 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 923 | def __next__(self): |
Raymond Hettinger | ffdb8bb | 2004-09-27 15:29:05 +0000 | [diff] [blame] | 924 | 3 // 0 |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 925 | |
| 926 | class S: |
| 927 | 'Test immediate stop' |
| 928 | def __init__(self, seqn): |
| 929 | pass |
| 930 | def __iter__(self): |
| 931 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 932 | def __next__(self): |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 933 | raise StopIteration |
| 934 | |
| 935 | def L(seqn): |
| 936 | 'Test multiple tiers of iterators' |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 937 | return chain(map(lambda x:x, R(Ig(G(seqn))))) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 938 | |
| 939 | |
| 940 | class TestVariousIteratorArgs(unittest.TestCase): |
| 941 | |
| 942 | def test_chain(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 943 | 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] | 944 | for g in (G, I, Ig, S, L, R): |
| 945 | self.assertEqual(list(chain(g(s))), list(g(s))) |
| 946 | 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] | 947 | self.assertRaises(TypeError, list, chain(X(s))) |
Mark Dickinson | 26a96fa | 2008-03-01 02:27:46 +0000 | [diff] [blame] | 948 | self.assertRaises(TypeError, list, chain(N(s))) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 949 | self.assertRaises(ZeroDivisionError, list, chain(E(s))) |
| 950 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 951 | def test_product(self): |
| 952 | for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
| 953 | self.assertRaises(TypeError, product, X(s)) |
| 954 | self.assertRaises(TypeError, product, N(s)) |
| 955 | self.assertRaises(ZeroDivisionError, product, E(s)) |
| 956 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 957 | def test_cycle(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 958 | 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] | 959 | for g in (G, I, Ig, S, L, R): |
| 960 | tgtlen = len(s) * 3 |
| 961 | expected = list(g(s))*3 |
| 962 | actual = list(islice(cycle(g(s)), tgtlen)) |
| 963 | self.assertEqual(actual, expected) |
| 964 | self.assertRaises(TypeError, cycle, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 965 | self.assertRaises(TypeError, cycle, N(s)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 966 | self.assertRaises(ZeroDivisionError, list, cycle(E(s))) |
| 967 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 968 | def test_groupby(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 969 | 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] | 970 | for g in (G, I, Ig, S, L, R): |
| 971 | self.assertEqual([k for k, sb in groupby(g(s))], list(g(s))) |
| 972 | self.assertRaises(TypeError, groupby, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 973 | self.assertRaises(TypeError, groupby, N(s)) |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 974 | self.assertRaises(ZeroDivisionError, list, groupby(E(s))) |
| 975 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 976 | def test_filter(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 977 | 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] | 978 | for g in (G, I, Ig, S, L, R): |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 979 | self.assertEqual(list(filter(isEven, g(s))), |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 980 | [x for x in g(s) if isEven(x)]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 981 | self.assertRaises(TypeError, filter, isEven, X(s)) |
| 982 | self.assertRaises(TypeError, filter, isEven, N(s)) |
| 983 | self.assertRaises(ZeroDivisionError, list, filter(isEven, E(s))) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 984 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 985 | def test_filterfalse(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 986 | 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] | 987 | for g in (G, I, Ig, S, L, R): |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 988 | self.assertEqual(list(filterfalse(isEven, g(s))), |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 989 | [x for x in g(s) if isOdd(x)]) |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 990 | self.assertRaises(TypeError, filterfalse, isEven, X(s)) |
| 991 | self.assertRaises(TypeError, filterfalse, isEven, N(s)) |
| 992 | self.assertRaises(ZeroDivisionError, list, filterfalse(isEven, E(s))) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 993 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 994 | def test_zip(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 995 | 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] | 996 | for g in (G, I, Ig, S, L, R): |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 997 | self.assertEqual(list(zip(g(s))), lzip(g(s))) |
| 998 | self.assertEqual(list(zip(g(s), g(s))), lzip(g(s), g(s))) |
| 999 | self.assertRaises(TypeError, zip, X(s)) |
| 1000 | self.assertRaises(TypeError, zip, N(s)) |
| 1001 | self.assertRaises(ZeroDivisionError, list, zip(E(s))) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1002 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1003 | def test_ziplongest(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1004 | 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] | 1005 | for g in (G, I, Ig, S, L, R): |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 1006 | self.assertEqual(list(zip_longest(g(s))), list(zip(g(s)))) |
| 1007 | self.assertEqual(list(zip_longest(g(s), g(s))), list(zip(g(s), g(s)))) |
| 1008 | self.assertRaises(TypeError, zip_longest, X(s)) |
| 1009 | self.assertRaises(TypeError, zip_longest, N(s)) |
| 1010 | self.assertRaises(ZeroDivisionError, list, zip_longest(E(s))) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1011 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1012 | def test_map(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1013 | 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] | 1014 | for g in (G, I, Ig, S, L, R): |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1015 | self.assertEqual(list(map(onearg, g(s))), |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1016 | [onearg(x) for x in g(s)]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1017 | self.assertEqual(list(map(operator.pow, g(s), g(s))), |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1018 | [x**x for x in g(s)]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1019 | self.assertRaises(TypeError, map, onearg, X(s)) |
| 1020 | self.assertRaises(TypeError, map, onearg, N(s)) |
| 1021 | self.assertRaises(ZeroDivisionError, list, map(onearg, E(s))) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1022 | |
| 1023 | def test_islice(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1024 | 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] | 1025 | for g in (G, I, Ig, S, L, R): |
| 1026 | self.assertEqual(list(islice(g(s),1,None,2)), list(g(s))[1::2]) |
| 1027 | self.assertRaises(TypeError, islice, X(s), 10) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 1028 | self.assertRaises(TypeError, islice, N(s), 10) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1029 | self.assertRaises(ZeroDivisionError, list, islice(E(s), 10)) |
| 1030 | |
| 1031 | def test_starmap(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1032 | 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] | 1033 | for g in (G, I, Ig, S, L, R): |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 1034 | ss = lzip(s, s) |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1035 | self.assertEqual(list(starmap(operator.pow, g(ss))), |
| 1036 | [x**x for x in g(s)]) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1037 | self.assertRaises(TypeError, starmap, operator.pow, X(ss)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 1038 | self.assertRaises(TypeError, starmap, operator.pow, N(ss)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1039 | self.assertRaises(ZeroDivisionError, list, starmap(operator.pow, E(ss))) |
| 1040 | |
| 1041 | def test_takewhile(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1042 | 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] | 1043 | for g in (G, I, Ig, S, L, R): |
| 1044 | tgt = [] |
| 1045 | for elem in g(s): |
| 1046 | if not isEven(elem): break |
| 1047 | tgt.append(elem) |
| 1048 | self.assertEqual(list(takewhile(isEven, g(s))), tgt) |
| 1049 | self.assertRaises(TypeError, takewhile, isEven, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 1050 | self.assertRaises(TypeError, takewhile, isEven, N(s)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1051 | self.assertRaises(ZeroDivisionError, list, takewhile(isEven, E(s))) |
| 1052 | |
| 1053 | def test_dropwhile(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1054 | 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] | 1055 | for g in (G, I, Ig, S, L, R): |
| 1056 | tgt = [] |
| 1057 | for elem in g(s): |
| 1058 | if not tgt and isOdd(elem): continue |
| 1059 | tgt.append(elem) |
| 1060 | self.assertEqual(list(dropwhile(isOdd, g(s))), tgt) |
| 1061 | self.assertRaises(TypeError, dropwhile, isOdd, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 1062 | self.assertRaises(TypeError, dropwhile, isOdd, N(s)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1063 | self.assertRaises(ZeroDivisionError, list, dropwhile(isOdd, E(s))) |
| 1064 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 1065 | def test_tee(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1066 | 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] | 1067 | for g in (G, I, Ig, S, L, R): |
| 1068 | it1, it2 = tee(g(s)) |
| 1069 | self.assertEqual(list(it1), list(g(s))) |
| 1070 | self.assertEqual(list(it2), list(g(s))) |
| 1071 | self.assertRaises(TypeError, tee, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 1072 | self.assertRaises(TypeError, tee, N(s)) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 1073 | self.assertRaises(ZeroDivisionError, list, tee(E(s))[0]) |
| 1074 | |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 1075 | class LengthTransparency(unittest.TestCase): |
| 1076 | |
| 1077 | def test_repeat(self): |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 1078 | from test.test_iterlen import len |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 1079 | self.assertEqual(len(repeat(None, 50)), 50) |
| 1080 | self.assertRaises(TypeError, len, repeat(None)) |
| 1081 | |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 1082 | class RegressionTests(unittest.TestCase): |
| 1083 | |
| 1084 | def test_sf_793826(self): |
| 1085 | # Fix Armin Rigo's successful efforts to wreak havoc |
| 1086 | |
| 1087 | def mutatingtuple(tuple1, f, tuple2): |
| 1088 | # this builds a tuple t which is a copy of tuple1, |
| 1089 | # then calls f(t), then mutates t to be equal to tuple2 |
| 1090 | # (needs len(tuple1) == len(tuple2)). |
| 1091 | def g(value, first=[1]): |
| 1092 | if first: |
| 1093 | del first[:] |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1094 | f(next(z)) |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 1095 | return value |
| 1096 | items = list(tuple2) |
| 1097 | items[1:1] = list(tuple1) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1098 | gen = map(g, items) |
| 1099 | z = zip(*[gen]*len(tuple1)) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1100 | next(z) |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 1101 | |
| 1102 | def f(t): |
| 1103 | global T |
| 1104 | T = t |
| 1105 | first[:] = list(T) |
| 1106 | |
| 1107 | first = [] |
| 1108 | mutatingtuple((1,2,3), f, (4,5,6)) |
| 1109 | second = list(T) |
| 1110 | self.assertEqual(first, second) |
| 1111 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1112 | |
Raymond Hettinger | 9d7c870 | 2004-05-08 19:49:42 +0000 | [diff] [blame] | 1113 | def test_sf_950057(self): |
| 1114 | # Make sure that chain() and cycle() catch exceptions immediately |
| 1115 | # rather than when shifting between input sources |
| 1116 | |
| 1117 | def gen1(): |
| 1118 | hist.append(0) |
| 1119 | yield 1 |
| 1120 | hist.append(1) |
Tim Peters | beb7c0c | 2004-07-18 17:34:03 +0000 | [diff] [blame] | 1121 | raise AssertionError |
Raymond Hettinger | 9d7c870 | 2004-05-08 19:49:42 +0000 | [diff] [blame] | 1122 | hist.append(2) |
| 1123 | |
| 1124 | def gen2(x): |
| 1125 | hist.append(3) |
| 1126 | yield 2 |
| 1127 | hist.append(4) |
| 1128 | if x: |
| 1129 | raise StopIteration |
| 1130 | |
| 1131 | hist = [] |
| 1132 | self.assertRaises(AssertionError, list, chain(gen1(), gen2(False))) |
| 1133 | self.assertEqual(hist, [0,1]) |
| 1134 | |
| 1135 | hist = [] |
| 1136 | self.assertRaises(AssertionError, list, chain(gen1(), gen2(True))) |
| 1137 | self.assertEqual(hist, [0,1]) |
| 1138 | |
| 1139 | hist = [] |
| 1140 | self.assertRaises(AssertionError, list, cycle(gen1())) |
| 1141 | self.assertEqual(hist, [0,1]) |
| 1142 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1143 | class SubclassWithKwargsTest(unittest.TestCase): |
| 1144 | def test_keywords_in_subclass(self): |
| 1145 | # count is not subclassable... |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1146 | for cls in (repeat, zip, filter, filterfalse, chain, map, |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1147 | starmap, islice, takewhile, dropwhile, cycle): |
| 1148 | class Subclass(cls): |
| 1149 | def __init__(self, newarg=None, *args): |
| 1150 | cls.__init__(self, *args) |
| 1151 | try: |
| 1152 | Subclass(newarg=1) |
| 1153 | except TypeError as err: |
| 1154 | # we expect type errors because of wrong argument count |
| 1155 | self.failIf("does not take keyword arguments" in err.args[0]) |
| 1156 | |
| 1157 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1158 | libreftest = """ Doctest for examples in the library reference: libitertools.tex |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1159 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1160 | |
| 1161 | >>> amounts = [120.15, 764.05, 823.14] |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1162 | >>> for checknum, amount in zip(count(1200), amounts): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1163 | ... print('Check %d is for $%.2f' % (checknum, amount)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1164 | ... |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1165 | Check 1200 is for $120.15 |
| 1166 | Check 1201 is for $764.05 |
| 1167 | Check 1202 is for $823.14 |
| 1168 | |
| 1169 | >>> import operator |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1170 | >>> for cube in map(operator.pow, range(1,4), repeat(3)): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1171 | ... print(cube) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1172 | ... |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1173 | 1 |
| 1174 | 8 |
| 1175 | 27 |
| 1176 | |
| 1177 | >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele'] |
Raymond Hettinger | 3567a87 | 2003-06-28 05:44:36 +0000 | [diff] [blame] | 1178 | >>> for name in islice(reportlines, 3, None, 2): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1179 | ... print(name.title()) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1180 | ... |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1181 | Alex |
| 1182 | Laura |
| 1183 | Martin |
| 1184 | Walter |
| 1185 | Samuele |
| 1186 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 1187 | >>> from operator import itemgetter |
| 1188 | >>> 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] | 1189 | >>> di = sorted(sorted(d.items()), key=itemgetter(1)) |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 1190 | >>> for k, g in groupby(di, itemgetter(1)): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1191 | ... print(k, list(map(itemgetter(0), g))) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1192 | ... |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 1193 | 1 ['a', 'c', 'e'] |
| 1194 | 2 ['b', 'd', 'f'] |
| 1195 | 3 ['g'] |
| 1196 | |
Raymond Hettinger | 734fb57 | 2004-01-20 20:04:40 +0000 | [diff] [blame] | 1197 | # Find runs of consecutive numbers using groupby. The key to the solution |
| 1198 | # is differencing with a range so that consecutive numbers all appear in |
| 1199 | # same group. |
| 1200 | >>> 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] | 1201 | >>> 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] | 1202 | ... print(list(map(operator.itemgetter(1), g))) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1203 | ... |
Raymond Hettinger | 734fb57 | 2004-01-20 20:04:40 +0000 | [diff] [blame] | 1204 | [1] |
| 1205 | [4, 5, 6] |
| 1206 | [10] |
| 1207 | [15, 16, 17, 18] |
| 1208 | [22] |
| 1209 | [25, 26, 27, 28] |
| 1210 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1211 | >>> def take(n, iterable): |
| 1212 | ... "Return first n items of the iterable as a list" |
| 1213 | ... return list(islice(iterable, n)) |
Raymond Hettinger | a098b33 | 2003-09-08 23:58:40 +0000 | [diff] [blame] | 1214 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1215 | >>> def enumerate(iterable, start=0): |
| 1216 | ... return zip(count(start), iterable) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1217 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1218 | >>> def tabulate(function, start=0): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1219 | ... "Return function(0), function(1), ..." |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1220 | ... return map(function, count(start)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1221 | |
| 1222 | >>> def nth(iterable, n): |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1223 | ... "Returns the nth item or empty list" |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1224 | ... return list(islice(iterable, n, n+1)) |
| 1225 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1226 | >>> def quantify(iterable, pred=bool): |
| 1227 | ... "Count how many times the predicate is true" |
| 1228 | ... return sum(map(pred, iterable)) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1229 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1230 | >>> def padnone(iterable): |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1231 | ... "Returns the sequence elements and then returns None indefinitely" |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1232 | ... return chain(iterable, repeat(None)) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1233 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1234 | >>> def ncycles(iterable, n): |
| 1235 | ... "Returns the seqeuence elements n times" |
| 1236 | ... return chain(*repeat(iterable, n)) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1237 | |
| 1238 | >>> def dotproduct(vec1, vec2): |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1239 | ... return sum(map(operator.mul, vec1, vec2)) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1240 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 1241 | >>> def flatten(listOfLists): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1242 | ... return list(chain.from_iterable(listOfLists)) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 1243 | |
| 1244 | >>> def repeatfunc(func, times=None, *args): |
| 1245 | ... "Repeat calls to func with specified arguments." |
| 1246 | ... " Example: repeatfunc(random.random)" |
| 1247 | ... if times is None: |
| 1248 | ... return starmap(func, repeat(args)) |
| 1249 | ... else: |
| 1250 | ... return starmap(func, repeat(args, times)) |
| 1251 | |
Raymond Hettinger | d591f66 | 2003-10-26 15:34:50 +0000 | [diff] [blame] | 1252 | >>> def pairwise(iterable): |
| 1253 | ... "s -> (s0,s1), (s1,s2), (s2, s3), ..." |
| 1254 | ... a, b = tee(iterable) |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 1255 | ... try: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1256 | ... next(b) |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 1257 | ... except StopIteration: |
| 1258 | ... pass |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1259 | ... return zip(a, b) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1260 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1261 | >>> def grouper(n, iterable, fillvalue=None): |
Raymond Hettinger | f5a2e47 | 2008-07-30 07:37:37 +0000 | [diff] [blame] | 1262 | ... "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1263 | ... args = [iter(iterable)] * n |
Benjamin Peterson | d18de0e | 2008-07-31 20:21:46 +0000 | [diff] [blame] | 1264 | ... return zip_longest(fillvalue=fillvalue, *args) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1265 | |
| 1266 | >>> def roundrobin(*iterables): |
Raymond Hettinger | f5a2e47 | 2008-07-30 07:37:37 +0000 | [diff] [blame] | 1267 | ... "roundrobin('ABC', 'D', 'EF') --> A D E B F C" |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1268 | ... # Recipe credited to George Sakkis |
| 1269 | ... pending = len(iterables) |
| 1270 | ... nexts = cycle(iter(it).__next__ for it in iterables) |
| 1271 | ... while pending: |
| 1272 | ... try: |
| 1273 | ... for next in nexts: |
| 1274 | ... yield next() |
| 1275 | ... except StopIteration: |
| 1276 | ... pending -= 1 |
| 1277 | ... nexts = cycle(islice(nexts, pending)) |
| 1278 | |
| 1279 | >>> def powerset(iterable): |
| 1280 | ... "powerset('ab') --> set([]), set(['a']), set(['b']), set(['a', 'b'])" |
| 1281 | ... # Recipe credited to Eric Raymond |
| 1282 | ... pairs = [(2**i, x) for i, x in enumerate(iterable)] |
| 1283 | ... for n in range(2**len(pairs)): |
| 1284 | ... yield set(x for m, x in pairs if m&n) |
| 1285 | |
| 1286 | >>> def compress(data, selectors): |
Raymond Hettinger | f5a2e47 | 2008-07-30 07:37:37 +0000 | [diff] [blame] | 1287 | ... "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F" |
| 1288 | ... return (d for d, s in zip(data, selectors) if s) |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1289 | |
| 1290 | >>> def combinations_with_replacement(iterable, r): |
| 1291 | ... "combinations_with_replacement('ABC', 3) --> AA AB AC BB BC CC" |
| 1292 | ... pool = tuple(iterable) |
| 1293 | ... n = len(pool) |
| 1294 | ... indices = [0] * r |
| 1295 | ... yield tuple(pool[i] for i in indices) |
| 1296 | ... while 1: |
| 1297 | ... for i in reversed(range(r)): |
| 1298 | ... if indices[i] != n - 1: |
| 1299 | ... break |
| 1300 | ... else: |
| 1301 | ... return |
| 1302 | ... indices[i:] = [indices[i] + 1] * (r - i) |
| 1303 | ... yield tuple(pool[i] for i in indices) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1304 | |
Raymond Hettinger | ad9d96b | 2009-01-02 21:39:07 +0000 | [diff] [blame] | 1305 | >>> def unique_everseen(iterable, key=None): |
| 1306 | ... "List unique elements, preserving order. Remember all elements ever seen." |
| 1307 | ... # unique_everseen('AAAABBBCCDAABBB') --> A B C D |
| 1308 | ... # unique_everseen('ABBCcAD', str.lower) --> A B C D |
| 1309 | ... seen = set() |
| 1310 | ... seen_add = seen.add |
| 1311 | ... if key is None: |
| 1312 | ... for element in iterable: |
| 1313 | ... if element not in seen: |
| 1314 | ... seen_add(element) |
| 1315 | ... yield element |
| 1316 | ... else: |
| 1317 | ... for element in iterable: |
| 1318 | ... k = key(element) |
| 1319 | ... if k not in seen: |
| 1320 | ... seen_add(k) |
| 1321 | ... yield element |
| 1322 | |
| 1323 | >>> def unique_justseen(iterable, key=None): |
| 1324 | ... "List unique elements, preserving order. Remember only the element just seen." |
| 1325 | ... # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B |
| 1326 | ... # unique_justseen('ABBCcAD', str.lower) --> A B C A D |
| 1327 | ... return map(next, map(itemgetter(1), groupby(iterable, key))) |
| 1328 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1329 | This is not part of the examples but it tests to make sure the definitions |
| 1330 | perform as purported. |
| 1331 | |
Raymond Hettinger | a098b33 | 2003-09-08 23:58:40 +0000 | [diff] [blame] | 1332 | >>> take(10, count()) |
| 1333 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 1334 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1335 | >>> list(enumerate('abc')) |
| 1336 | [(0, 'a'), (1, 'b'), (2, 'c')] |
| 1337 | |
| 1338 | >>> list(islice(tabulate(lambda x: 2*x), 4)) |
| 1339 | [0, 2, 4, 6] |
| 1340 | |
| 1341 | >>> nth('abcde', 3) |
| 1342 | ['d'] |
| 1343 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1344 | >>> quantify(range(99), lambda x: x%2==0) |
Raymond Hettinger | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame] | 1345 | 50 |
| 1346 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 1347 | >>> a = [[1, 2, 3], [4, 5, 6]] |
| 1348 | >>> flatten(a) |
| 1349 | [1, 2, 3, 4, 5, 6] |
| 1350 | |
| 1351 | >>> list(repeatfunc(pow, 5, 2, 3)) |
| 1352 | [8, 8, 8, 8, 8] |
| 1353 | |
| 1354 | >>> import random |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1355 | >>> take(5, map(int, repeatfunc(random.random))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 1356 | [0, 0, 0, 0, 0] |
| 1357 | |
Raymond Hettinger | d591f66 | 2003-10-26 15:34:50 +0000 | [diff] [blame] | 1358 | >>> list(pairwise('abcd')) |
| 1359 | [('a', 'b'), ('b', 'c'), ('c', 'd')] |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1360 | |
Raymond Hettinger | d591f66 | 2003-10-26 15:34:50 +0000 | [diff] [blame] | 1361 | >>> list(pairwise([])) |
| 1362 | [] |
| 1363 | |
| 1364 | >>> list(pairwise('a')) |
Raymond Hettinger | befa37d | 2003-06-18 19:25:37 +0000 | [diff] [blame] | 1365 | [] |
| 1366 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1367 | >>> list(islice(padnone('abc'), 0, 6)) |
| 1368 | ['a', 'b', 'c', None, None, None] |
| 1369 | |
| 1370 | >>> list(ncycles('abc', 3)) |
| 1371 | ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] |
| 1372 | |
| 1373 | >>> dotproduct([1,2,3], [4,5,6]) |
| 1374 | 32 |
| 1375 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1376 | >>> list(grouper(3, 'abcdefg', 'x')) |
| 1377 | [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')] |
| 1378 | |
| 1379 | >>> list(roundrobin('abc', 'd', 'ef')) |
| 1380 | ['a', 'd', 'e', 'b', 'f', 'c'] |
| 1381 | |
| 1382 | >>> list(map(sorted, powerset('ab'))) |
| 1383 | [[], ['a'], ['b'], ['a', 'b']] |
| 1384 | |
| 1385 | >>> list(compress('abcdef', [1,0,1,0,1,1])) |
| 1386 | ['a', 'c', 'e', 'f'] |
| 1387 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 1388 | >>> list(combinations_with_replacement('abc', 2)) |
| 1389 | [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b'), ('b', 'c'), ('c', 'c')] |
| 1390 | |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame^] | 1391 | >>> list(combinations_with_replacement('01', 3)) |
| 1392 | [('0', '0', '0'), ('0', '0', '1'), ('0', '1', '1'), ('1', '1', '1')] |
| 1393 | |
| 1394 | >>> def combinations_with_replacement2(iterable, r): |
| 1395 | ... 'Alternate version that filters from product()' |
| 1396 | ... pool = tuple(iterable) |
| 1397 | ... n = len(pool) |
| 1398 | ... for indices in product(range(n), repeat=r): |
| 1399 | ... if sorted(indices) == list(indices): |
| 1400 | ... yield tuple(pool[i] for i in indices) |
| 1401 | |
| 1402 | >>> list(combinations_with_replacement('abc', 2)) == list(combinations_with_replacement2('abc', 2)) |
| 1403 | True |
| 1404 | |
| 1405 | >>> list(combinations_with_replacement('01', 3)) == list(combinations_with_replacement2('01', 3)) |
| 1406 | True |
| 1407 | |
| 1408 | >>> list(combinations_with_replacement('2310', 6)) == list(combinations_with_replacement2('2310', 6)) |
| 1409 | True |
| 1410 | |
Raymond Hettinger | ad9d96b | 2009-01-02 21:39:07 +0000 | [diff] [blame] | 1411 | >>> list(unique_everseen('AAAABBBCCDAABBB')) |
| 1412 | ['A', 'B', 'C', 'D'] |
| 1413 | |
| 1414 | >>> list(unique_everseen('ABBCcAD', str.lower)) |
| 1415 | ['A', 'B', 'C', 'D'] |
| 1416 | |
| 1417 | >>> list(unique_justseen('AAAABBBCCDAABBB')) |
| 1418 | ['A', 'B', 'C', 'D', 'A', 'B'] |
| 1419 | |
| 1420 | >>> list(unique_justseen('ABBCcAD', str.lower)) |
| 1421 | ['A', 'B', 'C', 'A', 'D'] |
| 1422 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1423 | """ |
| 1424 | |
| 1425 | __test__ = {'libreftest' : libreftest} |
| 1426 | |
| 1427 | def test_main(verbose=None): |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 1428 | test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC, |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1429 | RegressionTests, LengthTransparency, |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1430 | SubclassWithKwargsTest, TestExamples) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1431 | support.run_unittest(*test_classes) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1432 | |
| 1433 | # verify reference counting |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1434 | if verbose and hasattr(sys, "gettotalrefcount"): |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 1435 | import gc |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 1436 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1437 | for i in range(len(counts)): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1438 | support.run_unittest(*test_classes) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 1439 | gc.collect() |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 1440 | counts[i] = sys.gettotalrefcount() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1441 | print(counts) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1442 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1443 | # doctest the examples in the library reference |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1444 | support.run_doctest(sys.modules[__name__], verbose) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1445 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1446 | if __name__ == "__main__": |
| 1447 | test_main(verbose=True) |