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