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