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): |
| 78 | self.assertRaises(TypeError, combinations, 'abc') # missing r argument |
| 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 |
| 82 | self.assertRaises(ValueError, combinations, 'abc', 32) # r is too big |
| 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) |
| 90 | indices = list(range(r)) |
| 91 | yield tuple(pool[i] for i in indices) |
| 92 | while 1: |
| 93 | for i in reversed(range(r)): |
| 94 | if indices[i] != i + n - r: |
| 95 | break |
| 96 | else: |
| 97 | return |
| 98 | indices[i] += 1 |
| 99 | for j in range(i+1, r): |
| 100 | indices[j] = indices[j-1] + 1 |
| 101 | yield tuple(pool[i] for i in indices) |
| 102 | |
| 103 | def combinations2(iterable, r): |
| 104 | 'Pure python version shown in the docs' |
| 105 | pool = tuple(iterable) |
| 106 | n = len(pool) |
| 107 | for indices in permutations(range(n), r): |
| 108 | if sorted(indices) == list(indices): |
| 109 | yield tuple(pool[i] for i in indices) |
| 110 | |
| 111 | for n in range(7): |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 112 | values = [5*x-12 for x in range(n)] |
| 113 | for r in range(n+1): |
| 114 | result = list(combinations(values, r)) |
| 115 | self.assertEqual(len(result), fact(n) / fact(r) / fact(n-r)) # right number of combs |
| 116 | self.assertEqual(len(result), len(set(result))) # no repeats |
| 117 | self.assertEqual(result, sorted(result)) # lexicographic order |
| 118 | for c in result: |
| 119 | self.assertEqual(len(c), r) # r-length combinations |
| 120 | self.assertEqual(len(set(c)), r) # no duplicate elements |
| 121 | self.assertEqual(list(c), sorted(c)) # keep original ordering |
| 122 | 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] | 123 | self.assertEqual(list(c), |
| 124 | [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] | 125 | self.assertEqual(result, list(combinations1(values, r))) # matches first pure python version |
| 126 | self.assertEqual(result, list(combinations2(values, r))) # matches first pure python version |
| 127 | |
| 128 | # Test implementation detail: tuple re-use |
| 129 | self.assertEqual(len(set(map(id, combinations('abcde', 3)))), 1) |
| 130 | self.assertNotEqual(len(set(map(id, list(combinations('abcde', 3))))), 1) |
| 131 | |
| 132 | def test_permutations(self): |
| 133 | self.assertRaises(TypeError, permutations) # too few arguments |
| 134 | self.assertRaises(TypeError, permutations, 'abc', 2, 1) # too many arguments |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 135 | self.assertRaises(TypeError, permutations, None) # pool is not iterable |
| 136 | self.assertRaises(ValueError, permutations, 'abc', -2) # r is negative |
| 137 | self.assertRaises(ValueError, permutations, 'abc', 32) # r is too big |
| 138 | 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] | 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 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 191 | self.assertEqual(len(set(map(id, permutations('abcde', 3)))), 1) |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 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 | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 316 | def test_filter(self): |
| 317 | self.assertEqual(list(filter(isEven, range(6))), [0,2,4]) |
| 318 | self.assertEqual(list(filter(None, [0,1,0,2,0])), [1,2]) |
| 319 | self.assertEqual(list(filter(bool, [0,1,0,2,0])), [1,2]) |
| 320 | self.assertEqual(take(4, filter(isEven, count())), [0,2,4,6]) |
| 321 | self.assertRaises(TypeError, filter) |
| 322 | self.assertRaises(TypeError, filter, lambda x:x) |
| 323 | self.assertRaises(TypeError, filter, lambda x:x, range(6), 7) |
| 324 | self.assertRaises(TypeError, filter, isEven, 3) |
| 325 | self.assertRaises(TypeError, next, filter(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 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 338 | def test_zip(self): |
| 339 | # XXX This is rather silly now that builtin zip() calls zip()... |
| 340 | ans = [(x,y) for x, y in zip('abc',count())] |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 341 | self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 342 | self.assertEqual(list(zip('abc', range(6))), lzip('abc', range(6))) |
| 343 | self.assertEqual(list(zip('abcdef', range(3))), lzip('abcdef', range(3))) |
| 344 | self.assertEqual(take(3,zip('abcdef', count())), lzip('abcdef', range(3))) |
| 345 | self.assertEqual(list(zip('abcdef')), lzip('abcdef')) |
| 346 | self.assertEqual(list(zip()), lzip()) |
| 347 | self.assertRaises(TypeError, zip, 3) |
| 348 | self.assertRaises(TypeError, zip, range(3), 3) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 349 | # Check tuple re-use (implementation detail) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 350 | self.assertEqual([tuple(list(pair)) for pair in zip('abc', 'def')], |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 351 | lzip('abc', 'def')) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 352 | self.assertEqual([pair for pair in zip('abc', 'def')], |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 353 | lzip('abc', 'def')) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 354 | ids = list(map(id, zip('abc', 'def'))) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 355 | self.assertEqual(min(ids), max(ids)) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 356 | ids = list(map(id, list(zip('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 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 359 | def test_ziplongest(self): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 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 | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 381 | list(zip(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) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 421 | |
| 422 | def product1(*args, **kwds): |
| 423 | pools = list(map(tuple, args)) * kwds.get('repeat', 1) |
| 424 | n = len(pools) |
| 425 | if n == 0: |
| 426 | yield () |
| 427 | return |
| 428 | if any(len(pool) == 0 for pool in pools): |
| 429 | return |
| 430 | indices = [0] * n |
| 431 | yield tuple(pool[i] for pool, i in zip(pools, indices)) |
| 432 | while 1: |
| 433 | for i in reversed(range(n)): # right to left |
| 434 | if indices[i] == len(pools[i]) - 1: |
| 435 | continue |
| 436 | indices[i] += 1 |
| 437 | for j in range(i+1, n): |
| 438 | indices[j] = 0 |
| 439 | yield tuple(pool[i] for pool, i in zip(pools, indices)) |
| 440 | break |
| 441 | else: |
| 442 | return |
| 443 | |
| 444 | def product2(*args, **kwds): |
| 445 | 'Pure python version used in docs' |
| 446 | pools = list(map(tuple, args)) * kwds.get('repeat', 1) |
| 447 | result = [[]] |
| 448 | for pool in pools: |
| 449 | result = [x+[y] for x in result for y in pool] |
| 450 | for prod in result: |
| 451 | yield tuple(prod) |
| 452 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 453 | argtypes = ['', 'abc', '', range(0), range(4), dict(a=1, b=2, c=3), |
| 454 | set('abcdefg'), range(11), tuple(range(13))] |
| 455 | for i in range(100): |
| 456 | args = [random.choice(argtypes) for j in range(random.randrange(5))] |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 457 | expected_len = prod(map(len, args)) |
| 458 | self.assertEqual(len(list(product(*args))), expected_len) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 459 | self.assertEqual(list(product(*args)), list(product1(*args))) |
| 460 | self.assertEqual(list(product(*args)), list(product2(*args))) |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 461 | args = map(iter, args) |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 462 | self.assertEqual(len(list(product(*args))), expected_len) |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 463 | |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 464 | # Test implementation detail: tuple re-use |
| 465 | self.assertEqual(len(set(map(id, product('abc', 'def')))), 1) |
| 466 | self.assertNotEqual(len(set(map(id, list(product('abc', 'def'))))), 1) |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 467 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 468 | def test_repeat(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 469 | self.assertEqual(lzip(range(3),repeat('a')), |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 470 | [(0, 'a'), (1, 'a'), (2, 'a')]) |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 471 | self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a']) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 472 | self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a']) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 473 | self.assertEqual(list(repeat('a', 0)), []) |
| 474 | self.assertEqual(list(repeat('a', -3)), []) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 475 | self.assertRaises(TypeError, repeat) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 476 | self.assertRaises(TypeError, repeat, None, 3, 4) |
| 477 | self.assertRaises(TypeError, repeat, None, 'a') |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 478 | r = repeat(1+0j) |
| 479 | self.assertEqual(repr(r), 'repeat((1+0j))') |
| 480 | r = repeat(1+0j, 5) |
| 481 | self.assertEqual(repr(r), 'repeat((1+0j), 5)') |
| 482 | list(r) |
| 483 | self.assertEqual(repr(r), 'repeat((1+0j), 0)') |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 484 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 485 | def test_map(self): |
| 486 | self.assertEqual(list(map(operator.pow, range(3), range(1,7))), |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 487 | [0**1, 1**2, 2**3]) |
Raymond Hettinger | 1dfde1d | 2008-01-22 23:25:35 +0000 | [diff] [blame] | 488 | def tupleize(*args): |
| 489 | return args |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 490 | self.assertEqual(list(map(tupleize, 'abc', range(5))), |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 491 | [('a',0),('b',1),('c',2)]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 492 | self.assertEqual(list(map(tupleize, 'abc', count())), |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 493 | [('a',0),('b',1),('c',2)]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 494 | self.assertEqual(take(2,map(tupleize, 'abc', count())), |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 495 | [('a',0),('b',1)]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 496 | self.assertEqual(list(map(operator.pow, [])), []) |
| 497 | self.assertRaises(TypeError, map) |
| 498 | self.assertRaises(TypeError, list, map(None, range(3), range(3))) |
| 499 | self.assertRaises(TypeError, map, operator.neg) |
| 500 | self.assertRaises(TypeError, next, map(10, range(5))) |
| 501 | self.assertRaises(ValueError, next, map(errfunc, [4], [5])) |
| 502 | self.assertRaises(TypeError, next, map(onearg, [4], [5])) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 503 | |
| 504 | def test_starmap(self): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 505 | self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))), |
| 506 | [0**1, 1**2, 2**3]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 507 | self.assertEqual(take(3, starmap(operator.pow, zip(count(), count(1)))), |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 508 | [0**1, 1**2, 2**3]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 509 | self.assertEqual(list(starmap(operator.pow, [])), []) |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 510 | self.assertEqual(list(starmap(operator.pow, [iter([4,5])])), [4**5]) |
| 511 | self.assertRaises(TypeError, list, starmap(operator.pow, [None])) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 512 | self.assertRaises(TypeError, starmap) |
| 513 | self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra') |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 514 | self.assertRaises(TypeError, next, starmap(10, [(4,5)])) |
| 515 | self.assertRaises(ValueError, next, starmap(errfunc, [(4,5)])) |
| 516 | self.assertRaises(TypeError, next, starmap(onearg, [(4,5)])) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 517 | |
| 518 | def test_islice(self): |
| 519 | for args in [ # islice(args) should agree with range(args) |
| 520 | (10, 20, 3), |
| 521 | (10, 3, 20), |
| 522 | (10, 20), |
| 523 | (10, 3), |
| 524 | (20,) |
| 525 | ]: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 526 | self.assertEqual(list(islice(range(100), *args)), |
| 527 | list(range(*args))) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 528 | |
| 529 | for args, tgtargs in [ # Stop when seqn is exhausted |
| 530 | ((10, 110, 3), ((10, 100, 3))), |
| 531 | ((10, 110), ((10, 100))), |
| 532 | ((110,), (100,)) |
| 533 | ]: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 534 | self.assertEqual(list(islice(range(100), *args)), |
| 535 | list(range(*tgtargs))) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 536 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 537 | # Test stop=None |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 538 | self.assertEqual(list(islice(range(10), None)), list(range(10))) |
| 539 | self.assertEqual(list(islice(range(10), None, None)), list(range(10))) |
| 540 | self.assertEqual(list(islice(range(10), None, None, None)), list(range(10))) |
| 541 | self.assertEqual(list(islice(range(10), 2, None)), list(range(2, 10))) |
| 542 | 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] | 543 | |
Raymond Hettinger | fdf3bd6 | 2005-03-27 20:11:44 +0000 | [diff] [blame] | 544 | # Test number of items consumed SF #1171417 |
| 545 | it = iter(range(10)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 546 | self.assertEqual(list(islice(it, 3)), list(range(3))) |
| 547 | self.assertEqual(list(it), list(range(3, 10))) |
Raymond Hettinger | fdf3bd6 | 2005-03-27 20:11:44 +0000 | [diff] [blame] | 548 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 549 | # Test invalid arguments |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 550 | self.assertRaises(TypeError, islice, range(10)) |
| 551 | self.assertRaises(TypeError, islice, range(10), 1, 2, 3, 4) |
| 552 | self.assertRaises(ValueError, islice, range(10), -5, 10, 1) |
| 553 | self.assertRaises(ValueError, islice, range(10), 1, -5, -1) |
| 554 | self.assertRaises(ValueError, islice, range(10), 1, 10, -1) |
| 555 | self.assertRaises(ValueError, islice, range(10), 1, 10, 0) |
| 556 | self.assertRaises(ValueError, islice, range(10), 'a') |
| 557 | self.assertRaises(ValueError, islice, range(10), 'a', 1) |
| 558 | self.assertRaises(ValueError, islice, range(10), 1, 'a') |
| 559 | self.assertRaises(ValueError, islice, range(10), 'a', 1, 1) |
| 560 | self.assertRaises(ValueError, islice, range(10), 1, 'a', 1) |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 561 | self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 562 | |
| 563 | def test_takewhile(self): |
| 564 | data = [1, 3, 5, 20, 2, 4, 6, 8] |
| 565 | underten = lambda x: x<10 |
| 566 | self.assertEqual(list(takewhile(underten, data)), [1, 3, 5]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 567 | self.assertEqual(list(takewhile(underten, [])), []) |
| 568 | self.assertRaises(TypeError, takewhile) |
| 569 | self.assertRaises(TypeError, takewhile, operator.pow) |
| 570 | self.assertRaises(TypeError, takewhile, operator.pow, [(4,5)], 'extra') |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 571 | self.assertRaises(TypeError, next, takewhile(10, [(4,5)])) |
| 572 | self.assertRaises(ValueError, next, takewhile(errfunc, [(4,5)])) |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 573 | t = takewhile(bool, [1, 1, 1, 0, 0, 0]) |
| 574 | self.assertEqual(list(t), [1, 1, 1]) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 575 | self.assertRaises(StopIteration, next, t) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 576 | |
| 577 | def test_dropwhile(self): |
| 578 | data = [1, 3, 5, 20, 2, 4, 6, 8] |
| 579 | underten = lambda x: x<10 |
| 580 | self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 581 | self.assertEqual(list(dropwhile(underten, [])), []) |
| 582 | self.assertRaises(TypeError, dropwhile) |
| 583 | self.assertRaises(TypeError, dropwhile, operator.pow) |
| 584 | self.assertRaises(TypeError, dropwhile, operator.pow, [(4,5)], 'extra') |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 585 | self.assertRaises(TypeError, next, dropwhile(10, [(4,5)])) |
| 586 | self.assertRaises(ValueError, next, dropwhile(errfunc, [(4,5)])) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 587 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 588 | def test_tee(self): |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 589 | n = 200 |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 590 | def irange(n): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 591 | for i in range(n): |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 592 | yield i |
| 593 | |
| 594 | a, b = tee([]) # test empty iterator |
| 595 | self.assertEqual(list(a), []) |
| 596 | self.assertEqual(list(b), []) |
| 597 | |
| 598 | a, b = tee(irange(n)) # test 100% interleaved |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 599 | self.assertEqual(lzip(a,b), lzip(range(n), range(n))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 600 | |
| 601 | a, b = tee(irange(n)) # test 0% interleaved |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 602 | self.assertEqual(list(a), list(range(n))) |
| 603 | self.assertEqual(list(b), list(range(n))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 604 | |
| 605 | a, b = tee(irange(n)) # test dealloc of leading iterator |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 606 | for i in range(100): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 607 | self.assertEqual(next(a), i) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 608 | del a |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 609 | self.assertEqual(list(b), list(range(n))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 610 | |
| 611 | a, b = tee(irange(n)) # test dealloc of trailing iterator |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 612 | for i in range(100): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 613 | self.assertEqual(next(a), i) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 614 | del b |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 615 | self.assertEqual(list(a), list(range(100, n))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 616 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 617 | for j in range(5): # test randomly interleaved |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 618 | order = [0]*n + [1]*n |
| 619 | random.shuffle(order) |
| 620 | lists = ([], []) |
| 621 | its = tee(irange(n)) |
| 622 | for i in order: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 623 | value = next(its[i]) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 624 | lists[i].append(value) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 625 | self.assertEqual(lists[0], list(range(n))) |
| 626 | self.assertEqual(lists[1], list(range(n))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 627 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 628 | # test argument format checking |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 629 | self.assertRaises(TypeError, tee) |
| 630 | self.assertRaises(TypeError, tee, 3) |
| 631 | self.assertRaises(TypeError, tee, [1,2], 'x') |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 632 | self.assertRaises(TypeError, tee, [1,2], 3, 'x') |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 633 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 634 | # tee object should be instantiable |
| 635 | a, b = tee('abc') |
| 636 | c = type(a)('def') |
| 637 | self.assertEqual(list(c), list('def')) |
Raymond Hettinger | f0c5aec | 2003-10-26 14:25:56 +0000 | [diff] [blame] | 638 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 639 | # test long-lagged and multi-way split |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 640 | a, b, c = tee(range(2000), 3) |
| 641 | for i in range(100): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 642 | self.assertEqual(next(a), i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 643 | self.assertEqual(list(b), list(range(2000))) |
| 644 | self.assertEqual([next(c), next(c)], list(range(2))) |
| 645 | self.assertEqual(list(a), list(range(100,2000))) |
| 646 | self.assertEqual(list(c), list(range(2,2000))) |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 647 | |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 648 | # test values of n |
| 649 | self.assertRaises(TypeError, tee, 'abc', 'invalid') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 650 | self.assertRaises(ValueError, tee, [], -1) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 651 | for n in range(5): |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 652 | result = tee('abc', n) |
| 653 | self.assertEqual(type(result), tuple) |
| 654 | self.assertEqual(len(result), n) |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 655 | self.assertEqual([list(x) for x in result], [list('abc')]*n) |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 656 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 657 | # tee pass-through to copyable iterator |
| 658 | a, b = tee('abc') |
| 659 | c, d = tee(a) |
| 660 | self.assert_(a is c) |
| 661 | |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 662 | # test tee_new |
| 663 | t1, t2 = tee('abc') |
| 664 | tnew = type(t1) |
| 665 | self.assertRaises(TypeError, tnew) |
| 666 | self.assertRaises(TypeError, tnew, 10) |
| 667 | t3 = tnew(t1) |
| 668 | self.assert_(list(t1) == list(t2) == list(t3) == list('abc')) |
Raymond Hettinger | f0c5aec | 2003-10-26 14:25:56 +0000 | [diff] [blame] | 669 | |
Raymond Hettinger | a9f6092 | 2004-10-17 16:40:14 +0000 | [diff] [blame] | 670 | # test that tee objects are weak referencable |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 671 | a, b = tee(range(10)) |
Raymond Hettinger | a9f6092 | 2004-10-17 16:40:14 +0000 | [diff] [blame] | 672 | p = proxy(a) |
| 673 | self.assertEqual(getattr(p, '__class__'), type(b)) |
| 674 | del a |
| 675 | self.assertRaises(ReferenceError, getattr, p, '__class__') |
| 676 | |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 677 | def test_StopIteration(self): |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 678 | self.assertRaises(StopIteration, next, zip()) |
Raymond Hettinger | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame] | 679 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 680 | for f in (chain, cycle, zip, groupby): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 681 | self.assertRaises(StopIteration, next, f([])) |
| 682 | self.assertRaises(StopIteration, next, f(StopNow())) |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 683 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 684 | self.assertRaises(StopIteration, next, islice([], None)) |
| 685 | self.assertRaises(StopIteration, next, islice(StopNow(), None)) |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 686 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 687 | p, q = tee([]) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 688 | self.assertRaises(StopIteration, next, p) |
| 689 | self.assertRaises(StopIteration, next, q) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 690 | p, q = tee(StopNow()) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 691 | self.assertRaises(StopIteration, next, p) |
| 692 | self.assertRaises(StopIteration, next, q) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 693 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 694 | self.assertRaises(StopIteration, next, repeat(None, 0)) |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 695 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 696 | for f in (filter, filterfalse, map, takewhile, dropwhile, starmap): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 697 | self.assertRaises(StopIteration, next, f(lambda x:x, [])) |
| 698 | self.assertRaises(StopIteration, next, f(lambda x:x, StopNow())) |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 699 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 700 | class TestExamples(unittest.TestCase): |
| 701 | |
| 702 | def test_chain(self): |
| 703 | self.assertEqual(''.join(chain('ABC', 'DEF')), 'ABCDEF') |
| 704 | |
| 705 | def test_chain_from_iterable(self): |
| 706 | self.assertEqual(''.join(chain.from_iterable(['ABC', 'DEF'])), 'ABCDEF') |
| 707 | |
| 708 | def test_combinations(self): |
| 709 | self.assertEqual(list(combinations('ABCD', 2)), |
| 710 | [('A','B'), ('A','C'), ('A','D'), ('B','C'), ('B','D'), ('C','D')]) |
| 711 | self.assertEqual(list(combinations(range(4), 3)), |
| 712 | [(0,1,2), (0,1,3), (0,2,3), (1,2,3)]) |
| 713 | |
| 714 | def test_count(self): |
| 715 | self.assertEqual(list(islice(count(10), 5)), [10, 11, 12, 13, 14]) |
| 716 | |
| 717 | def test_cycle(self): |
| 718 | self.assertEqual(list(islice(cycle('ABCD'), 12)), list('ABCDABCDABCD')) |
| 719 | |
| 720 | def test_dropwhile(self): |
| 721 | self.assertEqual(list(dropwhile(lambda x: x<5, [1,4,6,4,1])), [6,4,1]) |
| 722 | |
| 723 | def test_groupby(self): |
| 724 | self.assertEqual([k for k, g in groupby('AAAABBBCCDAABBB')], |
| 725 | list('ABCDAB')) |
| 726 | self.assertEqual([(list(g)) for k, g in groupby('AAAABBBCCD')], |
| 727 | [list('AAAA'), list('BBB'), list('CC'), list('D')]) |
| 728 | |
| 729 | def test_filter(self): |
| 730 | self.assertEqual(list(filter(lambda x: x%2, range(10))), [1,3,5,7,9]) |
| 731 | |
| 732 | def test_filterfalse(self): |
| 733 | self.assertEqual(list(filterfalse(lambda x: x%2, range(10))), [0,2,4,6,8]) |
| 734 | |
| 735 | def test_map(self): |
| 736 | self.assertEqual(list(map(pow, (2,3,10), (5,2,3))), [32, 9, 1000]) |
| 737 | |
| 738 | def test_islice(self): |
| 739 | self.assertEqual(list(islice('ABCDEFG', 2)), list('AB')) |
| 740 | self.assertEqual(list(islice('ABCDEFG', 2, 4)), list('CD')) |
| 741 | self.assertEqual(list(islice('ABCDEFG', 2, None)), list('CDEFG')) |
| 742 | self.assertEqual(list(islice('ABCDEFG', 0, None, 2)), list('ACEG')) |
| 743 | |
| 744 | def test_zip(self): |
| 745 | self.assertEqual(list(zip('ABCD', 'xy')), [('A', 'x'), ('B', 'y')]) |
| 746 | |
| 747 | def test_zip_longest(self): |
| 748 | self.assertEqual(list(zip_longest('ABCD', 'xy', fillvalue='-')), |
| 749 | [('A', 'x'), ('B', 'y'), ('C', '-'), ('D', '-')]) |
| 750 | |
| 751 | def test_permutations(self): |
| 752 | self.assertEqual(list(permutations('ABCD', 2)), |
| 753 | list(map(tuple, 'AB AC AD BA BC BD CA CB CD DA DB DC'.split()))) |
| 754 | self.assertEqual(list(permutations(range(3))), |
| 755 | [(0,1,2), (0,2,1), (1,0,2), (1,2,0), (2,0,1), (2,1,0)]) |
| 756 | |
| 757 | def test_product(self): |
| 758 | self.assertEqual(list(product('ABCD', 'xy')), |
| 759 | list(map(tuple, 'Ax Ay Bx By Cx Cy Dx Dy'.split()))) |
| 760 | self.assertEqual(list(product(range(2), repeat=3)), |
| 761 | [(0,0,0), (0,0,1), (0,1,0), (0,1,1), |
| 762 | (1,0,0), (1,0,1), (1,1,0), (1,1,1)]) |
| 763 | |
| 764 | def test_repeat(self): |
| 765 | self.assertEqual(list(repeat(10, 3)), [10, 10, 10]) |
| 766 | |
| 767 | def test_stapmap(self): |
| 768 | self.assertEqual(list(starmap(pow, [(2,5), (3,2), (10,3)])), |
| 769 | [32, 9, 1000]) |
| 770 | |
| 771 | def test_takewhile(self): |
| 772 | self.assertEqual(list(takewhile(lambda x: x<5, [1,4,6,4,1])), [1,4]) |
| 773 | |
| 774 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 775 | class TestGC(unittest.TestCase): |
| 776 | |
| 777 | def makecycle(self, iterator, container): |
| 778 | container.append(iterator) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 779 | next(iterator) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 780 | del container, iterator |
| 781 | |
| 782 | def test_chain(self): |
| 783 | a = [] |
| 784 | self.makecycle(chain(a), a) |
| 785 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 786 | def test_chain_from_iterable(self): |
| 787 | a = [] |
| 788 | self.makecycle(chain.from_iterable([a]), a) |
| 789 | |
| 790 | def test_combinations(self): |
| 791 | a = [] |
| 792 | self.makecycle(combinations([1,2,a,3], 3), a) |
| 793 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 794 | def test_cycle(self): |
| 795 | a = [] |
| 796 | self.makecycle(cycle([a]*2), a) |
| 797 | |
Raymond Hettinger | ff5dc0e | 2004-09-29 11:40:50 +0000 | [diff] [blame] | 798 | def test_dropwhile(self): |
| 799 | a = [] |
| 800 | self.makecycle(dropwhile(bool, [0, a, a]), a) |
| 801 | |
| 802 | def test_groupby(self): |
| 803 | a = [] |
| 804 | self.makecycle(groupby([a]*2, lambda x:x), a) |
| 805 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 806 | def test_issue2246(self): |
| 807 | # Issue 2246 -- the _grouper iterator was not included in GC |
| 808 | n = 10 |
| 809 | keyfunc = lambda x: x |
| 810 | for i, j in groupby(range(n), key=keyfunc): |
| 811 | keyfunc.__dict__.setdefault('x',[]).append(j) |
| 812 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 813 | def test_filter(self): |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 814 | a = [] |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 815 | self.makecycle(filter(lambda x:True, [a]*2), a) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 816 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 817 | def test_filterfalse(self): |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 818 | a = [] |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 819 | self.makecycle(filterfalse(lambda x:False, a), a) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 820 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 821 | def test_zip(self): |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 822 | a = [] |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 823 | self.makecycle(zip([a]*2, [a]*3), a) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 824 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 825 | def test_zip_longest(self): |
| 826 | a = [] |
| 827 | self.makecycle(zip_longest([a]*2, [a]*3), a) |
| 828 | b = [a, None] |
| 829 | self.makecycle(zip_longest([a]*2, [a]*3, fillvalue=b), a) |
| 830 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 831 | def test_map(self): |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 832 | a = [] |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 833 | self.makecycle(map(lambda x:x, [a]*2), a) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 834 | |
| 835 | def test_islice(self): |
| 836 | a = [] |
| 837 | self.makecycle(islice([a]*2, None), a) |
| 838 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 839 | def test_permutations(self): |
| 840 | a = [] |
| 841 | self.makecycle(permutations([1,2,a,3], 3), a) |
| 842 | |
| 843 | def test_product(self): |
| 844 | a = [] |
| 845 | self.makecycle(product([1,2,a,3], repeat=3), a) |
| 846 | |
Raymond Hettinger | ff5dc0e | 2004-09-29 11:40:50 +0000 | [diff] [blame] | 847 | def test_repeat(self): |
| 848 | a = [] |
| 849 | self.makecycle(repeat(a), a) |
| 850 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 851 | def test_starmap(self): |
| 852 | a = [] |
| 853 | self.makecycle(starmap(lambda *t: t, [(a,a)]*2), a) |
| 854 | |
Raymond Hettinger | ff5dc0e | 2004-09-29 11:40:50 +0000 | [diff] [blame] | 855 | def test_takewhile(self): |
| 856 | a = [] |
| 857 | self.makecycle(takewhile(bool, [1, 0, a, a]), a) |
| 858 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 859 | def R(seqn): |
| 860 | 'Regular generator' |
| 861 | for i in seqn: |
| 862 | yield i |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 863 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 864 | class G: |
| 865 | 'Sequence using __getitem__' |
| 866 | def __init__(self, seqn): |
| 867 | self.seqn = seqn |
| 868 | def __getitem__(self, i): |
| 869 | return self.seqn[i] |
| 870 | |
| 871 | class I: |
| 872 | 'Sequence using iterator protocol' |
| 873 | def __init__(self, seqn): |
| 874 | self.seqn = seqn |
| 875 | self.i = 0 |
| 876 | def __iter__(self): |
| 877 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 878 | def __next__(self): |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 879 | if self.i >= len(self.seqn): raise StopIteration |
| 880 | v = self.seqn[self.i] |
| 881 | self.i += 1 |
| 882 | return v |
| 883 | |
| 884 | class Ig: |
| 885 | 'Sequence using iterator protocol defined with a generator' |
| 886 | def __init__(self, seqn): |
| 887 | self.seqn = seqn |
| 888 | self.i = 0 |
| 889 | def __iter__(self): |
| 890 | for val in self.seqn: |
| 891 | yield val |
| 892 | |
| 893 | class X: |
| 894 | 'Missing __getitem__ and __iter__' |
| 895 | def __init__(self, seqn): |
| 896 | self.seqn = seqn |
| 897 | self.i = 0 |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 898 | def __next__(self): |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 899 | if self.i >= len(self.seqn): raise StopIteration |
| 900 | v = self.seqn[self.i] |
| 901 | self.i += 1 |
| 902 | return v |
| 903 | |
| 904 | class N: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 905 | 'Iterator missing __next__()' |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 906 | def __init__(self, seqn): |
| 907 | self.seqn = seqn |
| 908 | self.i = 0 |
| 909 | def __iter__(self): |
| 910 | return self |
| 911 | |
| 912 | class E: |
| 913 | 'Test propagation of exceptions' |
| 914 | def __init__(self, seqn): |
| 915 | self.seqn = seqn |
| 916 | self.i = 0 |
| 917 | def __iter__(self): |
| 918 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 919 | def __next__(self): |
Raymond Hettinger | ffdb8bb | 2004-09-27 15:29:05 +0000 | [diff] [blame] | 920 | 3 // 0 |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 921 | |
| 922 | class S: |
| 923 | 'Test immediate stop' |
| 924 | def __init__(self, seqn): |
| 925 | pass |
| 926 | def __iter__(self): |
| 927 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 928 | def __next__(self): |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 929 | raise StopIteration |
| 930 | |
| 931 | def L(seqn): |
| 932 | 'Test multiple tiers of iterators' |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 933 | return chain(map(lambda x:x, R(Ig(G(seqn))))) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 934 | |
| 935 | |
| 936 | class TestVariousIteratorArgs(unittest.TestCase): |
| 937 | |
| 938 | def test_chain(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 939 | 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] | 940 | for g in (G, I, Ig, S, L, R): |
| 941 | self.assertEqual(list(chain(g(s))), list(g(s))) |
| 942 | 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] | 943 | self.assertRaises(TypeError, list, chain(X(s))) |
Mark Dickinson | 26a96fa | 2008-03-01 02:27:46 +0000 | [diff] [blame] | 944 | self.assertRaises(TypeError, list, chain(N(s))) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 945 | self.assertRaises(ZeroDivisionError, list, chain(E(s))) |
| 946 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 947 | def test_product(self): |
| 948 | for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
| 949 | self.assertRaises(TypeError, product, X(s)) |
| 950 | self.assertRaises(TypeError, product, N(s)) |
| 951 | self.assertRaises(ZeroDivisionError, product, E(s)) |
| 952 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 953 | def test_cycle(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 954 | 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] | 955 | for g in (G, I, Ig, S, L, R): |
| 956 | tgtlen = len(s) * 3 |
| 957 | expected = list(g(s))*3 |
| 958 | actual = list(islice(cycle(g(s)), tgtlen)) |
| 959 | self.assertEqual(actual, expected) |
| 960 | self.assertRaises(TypeError, cycle, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 961 | self.assertRaises(TypeError, cycle, N(s)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 962 | self.assertRaises(ZeroDivisionError, list, cycle(E(s))) |
| 963 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 964 | def test_groupby(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 965 | 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] | 966 | for g in (G, I, Ig, S, L, R): |
| 967 | self.assertEqual([k for k, sb in groupby(g(s))], list(g(s))) |
| 968 | self.assertRaises(TypeError, groupby, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 969 | self.assertRaises(TypeError, groupby, N(s)) |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 970 | self.assertRaises(ZeroDivisionError, list, groupby(E(s))) |
| 971 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 972 | def test_filter(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 973 | 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] | 974 | for g in (G, I, Ig, S, L, R): |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 975 | self.assertEqual(list(filter(isEven, g(s))), |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 976 | [x for x in g(s) if isEven(x)]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 977 | self.assertRaises(TypeError, filter, isEven, X(s)) |
| 978 | self.assertRaises(TypeError, filter, isEven, N(s)) |
| 979 | self.assertRaises(ZeroDivisionError, list, filter(isEven, E(s))) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 980 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 981 | def test_filterfalse(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 982 | 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] | 983 | for g in (G, I, Ig, S, L, R): |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 984 | self.assertEqual(list(filterfalse(isEven, g(s))), |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 985 | [x for x in g(s) if isOdd(x)]) |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 986 | self.assertRaises(TypeError, filterfalse, isEven, X(s)) |
| 987 | self.assertRaises(TypeError, filterfalse, isEven, N(s)) |
| 988 | self.assertRaises(ZeroDivisionError, list, filterfalse(isEven, E(s))) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 989 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 990 | def test_zip(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 991 | 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] | 992 | for g in (G, I, Ig, S, L, R): |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 993 | self.assertEqual(list(zip(g(s))), lzip(g(s))) |
| 994 | self.assertEqual(list(zip(g(s), g(s))), lzip(g(s), g(s))) |
| 995 | self.assertRaises(TypeError, zip, X(s)) |
| 996 | self.assertRaises(TypeError, zip, N(s)) |
| 997 | self.assertRaises(ZeroDivisionError, list, zip(E(s))) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 998 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 999 | def test_ziplongest(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1000 | 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] | 1001 | for g in (G, I, Ig, S, L, R): |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 1002 | self.assertEqual(list(zip_longest(g(s))), list(zip(g(s)))) |
| 1003 | self.assertEqual(list(zip_longest(g(s), g(s))), list(zip(g(s), g(s)))) |
| 1004 | self.assertRaises(TypeError, zip_longest, X(s)) |
| 1005 | self.assertRaises(TypeError, zip_longest, N(s)) |
| 1006 | self.assertRaises(ZeroDivisionError, list, zip_longest(E(s))) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1007 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1008 | def test_map(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1009 | 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] | 1010 | for g in (G, I, Ig, S, L, R): |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1011 | self.assertEqual(list(map(onearg, g(s))), |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1012 | [onearg(x) for x in g(s)]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1013 | self.assertEqual(list(map(operator.pow, g(s), g(s))), |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1014 | [x**x for x in g(s)]) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1015 | self.assertRaises(TypeError, map, onearg, X(s)) |
| 1016 | self.assertRaises(TypeError, map, onearg, N(s)) |
| 1017 | self.assertRaises(ZeroDivisionError, list, map(onearg, E(s))) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1018 | |
| 1019 | def test_islice(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1020 | 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] | 1021 | for g in (G, I, Ig, S, L, R): |
| 1022 | self.assertEqual(list(islice(g(s),1,None,2)), list(g(s))[1::2]) |
| 1023 | self.assertRaises(TypeError, islice, X(s), 10) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 1024 | self.assertRaises(TypeError, islice, N(s), 10) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1025 | self.assertRaises(ZeroDivisionError, list, islice(E(s), 10)) |
| 1026 | |
| 1027 | def test_starmap(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1028 | 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] | 1029 | for g in (G, I, Ig, S, L, R): |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 1030 | ss = lzip(s, s) |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1031 | self.assertEqual(list(starmap(operator.pow, g(ss))), |
| 1032 | [x**x for x in g(s)]) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1033 | self.assertRaises(TypeError, starmap, operator.pow, X(ss)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 1034 | self.assertRaises(TypeError, starmap, operator.pow, N(ss)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1035 | self.assertRaises(ZeroDivisionError, list, starmap(operator.pow, E(ss))) |
| 1036 | |
| 1037 | def test_takewhile(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1038 | 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] | 1039 | for g in (G, I, Ig, S, L, R): |
| 1040 | tgt = [] |
| 1041 | for elem in g(s): |
| 1042 | if not isEven(elem): break |
| 1043 | tgt.append(elem) |
| 1044 | self.assertEqual(list(takewhile(isEven, g(s))), tgt) |
| 1045 | self.assertRaises(TypeError, takewhile, isEven, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 1046 | self.assertRaises(TypeError, takewhile, isEven, N(s)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1047 | self.assertRaises(ZeroDivisionError, list, takewhile(isEven, E(s))) |
| 1048 | |
| 1049 | def test_dropwhile(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1050 | 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] | 1051 | for g in (G, I, Ig, S, L, R): |
| 1052 | tgt = [] |
| 1053 | for elem in g(s): |
| 1054 | if not tgt and isOdd(elem): continue |
| 1055 | tgt.append(elem) |
| 1056 | self.assertEqual(list(dropwhile(isOdd, g(s))), tgt) |
| 1057 | self.assertRaises(TypeError, dropwhile, isOdd, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 1058 | self.assertRaises(TypeError, dropwhile, isOdd, N(s)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1059 | self.assertRaises(ZeroDivisionError, list, dropwhile(isOdd, E(s))) |
| 1060 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 1061 | def test_tee(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1062 | 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] | 1063 | for g in (G, I, Ig, S, L, R): |
| 1064 | it1, it2 = tee(g(s)) |
| 1065 | self.assertEqual(list(it1), list(g(s))) |
| 1066 | self.assertEqual(list(it2), list(g(s))) |
| 1067 | self.assertRaises(TypeError, tee, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 1068 | self.assertRaises(TypeError, tee, N(s)) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 1069 | self.assertRaises(ZeroDivisionError, list, tee(E(s))[0]) |
| 1070 | |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 1071 | class LengthTransparency(unittest.TestCase): |
| 1072 | |
| 1073 | def test_repeat(self): |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 1074 | from test.test_iterlen import len |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 1075 | self.assertEqual(len(repeat(None, 50)), 50) |
| 1076 | self.assertRaises(TypeError, len, repeat(None)) |
| 1077 | |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 1078 | class RegressionTests(unittest.TestCase): |
| 1079 | |
| 1080 | def test_sf_793826(self): |
| 1081 | # Fix Armin Rigo's successful efforts to wreak havoc |
| 1082 | |
| 1083 | def mutatingtuple(tuple1, f, tuple2): |
| 1084 | # this builds a tuple t which is a copy of tuple1, |
| 1085 | # then calls f(t), then mutates t to be equal to tuple2 |
| 1086 | # (needs len(tuple1) == len(tuple2)). |
| 1087 | def g(value, first=[1]): |
| 1088 | if first: |
| 1089 | del first[:] |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1090 | f(next(z)) |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 1091 | return value |
| 1092 | items = list(tuple2) |
| 1093 | items[1:1] = list(tuple1) |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1094 | gen = map(g, items) |
| 1095 | z = zip(*[gen]*len(tuple1)) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1096 | next(z) |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 1097 | |
| 1098 | def f(t): |
| 1099 | global T |
| 1100 | T = t |
| 1101 | first[:] = list(T) |
| 1102 | |
| 1103 | first = [] |
| 1104 | mutatingtuple((1,2,3), f, (4,5,6)) |
| 1105 | second = list(T) |
| 1106 | self.assertEqual(first, second) |
| 1107 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1108 | |
Raymond Hettinger | 9d7c870 | 2004-05-08 19:49:42 +0000 | [diff] [blame] | 1109 | def test_sf_950057(self): |
| 1110 | # Make sure that chain() and cycle() catch exceptions immediately |
| 1111 | # rather than when shifting between input sources |
| 1112 | |
| 1113 | def gen1(): |
| 1114 | hist.append(0) |
| 1115 | yield 1 |
| 1116 | hist.append(1) |
Tim Peters | beb7c0c | 2004-07-18 17:34:03 +0000 | [diff] [blame] | 1117 | raise AssertionError |
Raymond Hettinger | 9d7c870 | 2004-05-08 19:49:42 +0000 | [diff] [blame] | 1118 | hist.append(2) |
| 1119 | |
| 1120 | def gen2(x): |
| 1121 | hist.append(3) |
| 1122 | yield 2 |
| 1123 | hist.append(4) |
| 1124 | if x: |
| 1125 | raise StopIteration |
| 1126 | |
| 1127 | hist = [] |
| 1128 | self.assertRaises(AssertionError, list, chain(gen1(), gen2(False))) |
| 1129 | self.assertEqual(hist, [0,1]) |
| 1130 | |
| 1131 | hist = [] |
| 1132 | self.assertRaises(AssertionError, list, chain(gen1(), gen2(True))) |
| 1133 | self.assertEqual(hist, [0,1]) |
| 1134 | |
| 1135 | hist = [] |
| 1136 | self.assertRaises(AssertionError, list, cycle(gen1())) |
| 1137 | self.assertEqual(hist, [0,1]) |
| 1138 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1139 | class SubclassWithKwargsTest(unittest.TestCase): |
| 1140 | def test_keywords_in_subclass(self): |
| 1141 | # count is not subclassable... |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1142 | for cls in (repeat, zip, filter, filterfalse, chain, map, |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1143 | starmap, islice, takewhile, dropwhile, cycle): |
| 1144 | class Subclass(cls): |
| 1145 | def __init__(self, newarg=None, *args): |
| 1146 | cls.__init__(self, *args) |
| 1147 | try: |
| 1148 | Subclass(newarg=1) |
| 1149 | except TypeError as err: |
| 1150 | # we expect type errors because of wrong argument count |
| 1151 | self.failIf("does not take keyword arguments" in err.args[0]) |
| 1152 | |
| 1153 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1154 | libreftest = """ Doctest for examples in the library reference: libitertools.tex |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1155 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1156 | |
| 1157 | >>> amounts = [120.15, 764.05, 823.14] |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1158 | >>> for checknum, amount in zip(count(1200), amounts): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1159 | ... print('Check %d is for $%.2f' % (checknum, amount)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1160 | ... |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1161 | Check 1200 is for $120.15 |
| 1162 | Check 1201 is for $764.05 |
| 1163 | Check 1202 is for $823.14 |
| 1164 | |
| 1165 | >>> import operator |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1166 | >>> for cube in map(operator.pow, range(1,4), repeat(3)): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1167 | ... print(cube) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1168 | ... |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1169 | 1 |
| 1170 | 8 |
| 1171 | 27 |
| 1172 | |
| 1173 | >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele'] |
Raymond Hettinger | 3567a87 | 2003-06-28 05:44:36 +0000 | [diff] [blame] | 1174 | >>> for name in islice(reportlines, 3, None, 2): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1175 | ... print(name.title()) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1176 | ... |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1177 | Alex |
| 1178 | Laura |
| 1179 | Martin |
| 1180 | Walter |
| 1181 | Samuele |
| 1182 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 1183 | >>> from operator import itemgetter |
| 1184 | >>> 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] | 1185 | >>> di = sorted(sorted(d.items()), key=itemgetter(1)) |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 1186 | >>> for k, g in groupby(di, itemgetter(1)): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1187 | ... print(k, list(map(itemgetter(0), g))) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1188 | ... |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 1189 | 1 ['a', 'c', 'e'] |
| 1190 | 2 ['b', 'd', 'f'] |
| 1191 | 3 ['g'] |
| 1192 | |
Raymond Hettinger | 734fb57 | 2004-01-20 20:04:40 +0000 | [diff] [blame] | 1193 | # Find runs of consecutive numbers using groupby. The key to the solution |
| 1194 | # is differencing with a range so that consecutive numbers all appear in |
| 1195 | # same group. |
| 1196 | >>> 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] | 1197 | >>> 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] | 1198 | ... print(list(map(operator.itemgetter(1), g))) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1199 | ... |
Raymond Hettinger | 734fb57 | 2004-01-20 20:04:40 +0000 | [diff] [blame] | 1200 | [1] |
| 1201 | [4, 5, 6] |
| 1202 | [10] |
| 1203 | [15, 16, 17, 18] |
| 1204 | [22] |
| 1205 | [25, 26, 27, 28] |
| 1206 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame^] | 1207 | >>> def take(n, iterable): |
| 1208 | ... "Return first n items of the iterable as a list" |
| 1209 | ... return list(islice(iterable, n)) |
Raymond Hettinger | a098b33 | 2003-09-08 23:58:40 +0000 | [diff] [blame] | 1210 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame^] | 1211 | >>> def enumerate(iterable, start=0): |
| 1212 | ... return zip(count(start), iterable) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1213 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame^] | 1214 | >>> def tabulate(function, start=0): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1215 | ... "Return function(0), function(1), ..." |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame^] | 1216 | ... return map(function, count(start)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1217 | |
| 1218 | >>> def nth(iterable, n): |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame^] | 1219 | ... "Returns the nth item or empty list" |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1220 | ... return list(islice(iterable, n, n+1)) |
| 1221 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame^] | 1222 | >>> def quantify(iterable, pred=bool): |
| 1223 | ... "Count how many times the predicate is true" |
| 1224 | ... return sum(map(pred, iterable)) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1225 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame^] | 1226 | >>> def padnone(iterable): |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1227 | ... "Returns the sequence elements and then returns None indefinitely" |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame^] | 1228 | ... return chain(iterable, repeat(None)) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1229 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame^] | 1230 | >>> def ncycles(iterable, n): |
| 1231 | ... "Returns the seqeuence elements n times" |
| 1232 | ... return chain(*repeat(iterable, n)) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1233 | |
| 1234 | >>> def dotproduct(vec1, vec2): |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1235 | ... return sum(map(operator.mul, vec1, vec2)) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1236 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 1237 | >>> def flatten(listOfLists): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1238 | ... return list(chain.from_iterable(listOfLists)) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 1239 | |
| 1240 | >>> def repeatfunc(func, times=None, *args): |
| 1241 | ... "Repeat calls to func with specified arguments." |
| 1242 | ... " Example: repeatfunc(random.random)" |
| 1243 | ... if times is None: |
| 1244 | ... return starmap(func, repeat(args)) |
| 1245 | ... else: |
| 1246 | ... return starmap(func, repeat(args, times)) |
| 1247 | |
Raymond Hettinger | d591f66 | 2003-10-26 15:34:50 +0000 | [diff] [blame] | 1248 | >>> def pairwise(iterable): |
| 1249 | ... "s -> (s0,s1), (s1,s2), (s2, s3), ..." |
| 1250 | ... a, b = tee(iterable) |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 1251 | ... try: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1252 | ... next(b) |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 1253 | ... except StopIteration: |
| 1254 | ... pass |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1255 | ... return zip(a, b) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1256 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1257 | >>> def grouper(n, iterable, fillvalue=None): |
| 1258 | ... "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')" |
| 1259 | ... args = [iter(iterable)] * n |
| 1260 | ... kwds = dict(fillvalue=fillvalue) |
| 1261 | ... return zip_longest(*args, **kwds) |
| 1262 | |
| 1263 | >>> def roundrobin(*iterables): |
| 1264 | ... "roundrobin('abc', 'd', 'ef') --> 'a', 'd', 'e', 'b', 'f', 'c'" |
| 1265 | ... # Recipe credited to George Sakkis |
| 1266 | ... pending = len(iterables) |
| 1267 | ... nexts = cycle(iter(it).__next__ for it in iterables) |
| 1268 | ... while pending: |
| 1269 | ... try: |
| 1270 | ... for next in nexts: |
| 1271 | ... yield next() |
| 1272 | ... except StopIteration: |
| 1273 | ... pending -= 1 |
| 1274 | ... nexts = cycle(islice(nexts, pending)) |
| 1275 | |
| 1276 | >>> def powerset(iterable): |
| 1277 | ... "powerset('ab') --> set([]), set(['a']), set(['b']), set(['a', 'b'])" |
| 1278 | ... # Recipe credited to Eric Raymond |
| 1279 | ... pairs = [(2**i, x) for i, x in enumerate(iterable)] |
| 1280 | ... for n in range(2**len(pairs)): |
| 1281 | ... yield set(x for m, x in pairs if m&n) |
| 1282 | |
| 1283 | >>> def compress(data, selectors): |
| 1284 | ... "compress('abcdef', [1,0,1,0,1,1]) --> a c e f" |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame^] | 1285 | ... decorated = zip(data, selectors) |
| 1286 | ... filtered = filter(operator.itemgetter(1), decorated) |
| 1287 | ... return map(operator.itemgetter(0), filtered) |
| 1288 | |
| 1289 | >>> def combinations_with_replacement(iterable, r): |
| 1290 | ... "combinations_with_replacement('ABC', 3) --> AA AB AC BB BC CC" |
| 1291 | ... pool = tuple(iterable) |
| 1292 | ... n = len(pool) |
| 1293 | ... indices = [0] * r |
| 1294 | ... yield tuple(pool[i] for i in indices) |
| 1295 | ... while 1: |
| 1296 | ... for i in reversed(range(r)): |
| 1297 | ... if indices[i] != n - 1: |
| 1298 | ... break |
| 1299 | ... else: |
| 1300 | ... return |
| 1301 | ... indices[i:] = [indices[i] + 1] * (r - i) |
| 1302 | ... yield tuple(pool[i] for i in indices) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1303 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1304 | This is not part of the examples but it tests to make sure the definitions |
| 1305 | perform as purported. |
| 1306 | |
Raymond Hettinger | a098b33 | 2003-09-08 23:58:40 +0000 | [diff] [blame] | 1307 | >>> take(10, count()) |
| 1308 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 1309 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1310 | >>> list(enumerate('abc')) |
| 1311 | [(0, 'a'), (1, 'b'), (2, 'c')] |
| 1312 | |
| 1313 | >>> list(islice(tabulate(lambda x: 2*x), 4)) |
| 1314 | [0, 2, 4, 6] |
| 1315 | |
| 1316 | >>> nth('abcde', 3) |
| 1317 | ['d'] |
| 1318 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1319 | >>> quantify(range(99), lambda x: x%2==0) |
Raymond Hettinger | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame] | 1320 | 50 |
| 1321 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 1322 | >>> a = [[1, 2, 3], [4, 5, 6]] |
| 1323 | >>> flatten(a) |
| 1324 | [1, 2, 3, 4, 5, 6] |
| 1325 | |
| 1326 | >>> list(repeatfunc(pow, 5, 2, 3)) |
| 1327 | [8, 8, 8, 8, 8] |
| 1328 | |
| 1329 | >>> import random |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 1330 | >>> take(5, map(int, repeatfunc(random.random))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 1331 | [0, 0, 0, 0, 0] |
| 1332 | |
Raymond Hettinger | d591f66 | 2003-10-26 15:34:50 +0000 | [diff] [blame] | 1333 | >>> list(pairwise('abcd')) |
| 1334 | [('a', 'b'), ('b', 'c'), ('c', 'd')] |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1335 | |
Raymond Hettinger | d591f66 | 2003-10-26 15:34:50 +0000 | [diff] [blame] | 1336 | >>> list(pairwise([])) |
| 1337 | [] |
| 1338 | |
| 1339 | >>> list(pairwise('a')) |
Raymond Hettinger | befa37d | 2003-06-18 19:25:37 +0000 | [diff] [blame] | 1340 | [] |
| 1341 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1342 | >>> list(islice(padnone('abc'), 0, 6)) |
| 1343 | ['a', 'b', 'c', None, None, None] |
| 1344 | |
| 1345 | >>> list(ncycles('abc', 3)) |
| 1346 | ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] |
| 1347 | |
| 1348 | >>> dotproduct([1,2,3], [4,5,6]) |
| 1349 | 32 |
| 1350 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1351 | >>> list(grouper(3, 'abcdefg', 'x')) |
| 1352 | [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')] |
| 1353 | |
| 1354 | >>> list(roundrobin('abc', 'd', 'ef')) |
| 1355 | ['a', 'd', 'e', 'b', 'f', 'c'] |
| 1356 | |
| 1357 | >>> list(map(sorted, powerset('ab'))) |
| 1358 | [[], ['a'], ['b'], ['a', 'b']] |
| 1359 | |
| 1360 | >>> list(compress('abcdef', [1,0,1,0,1,1])) |
| 1361 | ['a', 'c', 'e', 'f'] |
| 1362 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame^] | 1363 | >>> list(combinations_with_replacement('abc', 2)) |
| 1364 | [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b'), ('b', 'c'), ('c', 'c')] |
| 1365 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1366 | """ |
| 1367 | |
| 1368 | __test__ = {'libreftest' : libreftest} |
| 1369 | |
| 1370 | def test_main(verbose=None): |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 1371 | test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC, |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1372 | RegressionTests, LengthTransparency, |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1373 | SubclassWithKwargsTest, TestExamples) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1374 | support.run_unittest(*test_classes) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1375 | |
| 1376 | # verify reference counting |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1377 | if verbose and hasattr(sys, "gettotalrefcount"): |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 1378 | import gc |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 1379 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1380 | for i in range(len(counts)): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1381 | support.run_unittest(*test_classes) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 1382 | gc.collect() |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 1383 | counts[i] = sys.gettotalrefcount() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1384 | print(counts) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1385 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1386 | # doctest the examples in the library reference |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1387 | support.run_doctest(sys.modules[__name__], verbose) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1388 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1389 | if __name__ == "__main__": |
| 1390 | test_main(verbose=True) |