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