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