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 | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 4 | import sys |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 5 | import operator |
| 6 | |
| 7 | def onearg(x): |
| 8 | 'Test function of one argument' |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 9 | return 2*x |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 10 | |
| 11 | def errfunc(*args): |
| 12 | 'Test function that raises an error' |
| 13 | raise ValueError |
| 14 | |
| 15 | def gen3(): |
| 16 | 'Non-restartable source sequence' |
| 17 | for i in (0, 1, 2): |
| 18 | yield i |
| 19 | |
| 20 | def isEven(x): |
| 21 | 'Test predicate' |
| 22 | return x%2==0 |
| 23 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 24 | def isOdd(x): |
| 25 | 'Test predicate' |
| 26 | return x%2==1 |
| 27 | |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 28 | class StopNow: |
| 29 | 'Class emulating an empty iterable.' |
| 30 | def __iter__(self): |
| 31 | return self |
| 32 | def next(self): |
| 33 | raise StopIteration |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 34 | |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame^] | 35 | def take(n, seq): |
| 36 | 'Convenience function for partially consuming a long of infinite iterable' |
| 37 | return list(islice(seq, n)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 38 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 39 | class TestBasicOps(unittest.TestCase): |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 40 | def test_chain(self): |
| 41 | self.assertEqual(list(chain('abc', 'def')), list('abcdef')) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 42 | self.assertEqual(list(chain('abc')), list('abc')) |
| 43 | self.assertEqual(list(chain('')), []) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame^] | 44 | self.assertEqual(take(4, chain('abc', 'def')), list('abcd')) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 45 | self.assertRaises(TypeError, chain, 2, 3) |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 46 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 47 | def test_count(self): |
| 48 | self.assertEqual(zip('abc',count()), [('a', 0), ('b', 1), ('c', 2)]) |
| 49 | self.assertEqual(zip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)]) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame^] | 50 | self.assertEqual(take(2, zip('abc',count(3))), [('a', 3), ('b', 4)]) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 51 | self.assertRaises(TypeError, count, 2, 3) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 52 | self.assertRaises(TypeError, count, 'a') |
| 53 | c = count(sys.maxint-2) # verify that rollover doesn't crash |
| 54 | c.next(); c.next(); c.next(); c.next(); c.next() |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 55 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 56 | def test_cycle(self): |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame^] | 57 | self.assertEqual(take(10, cycle('abc')), list('abcabcabca')) |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 58 | self.assertEqual(list(cycle('')), []) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 59 | self.assertRaises(TypeError, cycle) |
| 60 | self.assertRaises(TypeError, cycle, 5) |
| 61 | 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] | 62 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 63 | def test_ifilter(self): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 64 | self.assertEqual(list(ifilter(isEven, range(6))), [0,2,4]) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 65 | self.assertEqual(list(ifilter(None, [0,1,0,2,0])), [1,2]) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame^] | 66 | self.assertEqual(take(4, ifilter(isEven, count())), [0,2,4,6]) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 67 | self.assertRaises(TypeError, ifilter) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 68 | self.assertRaises(TypeError, ifilter, lambda x:x) |
| 69 | self.assertRaises(TypeError, ifilter, lambda x:x, range(6), 7) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 70 | self.assertRaises(TypeError, ifilter, isEven, 3) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 71 | self.assertRaises(TypeError, ifilter(range(6), range(6)).next) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 72 | |
| 73 | def test_ifilterfalse(self): |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 74 | self.assertEqual(list(ifilterfalse(isEven, range(6))), [1,3,5]) |
| 75 | 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^] | 76 | self.assertEqual(take(4, ifilterfalse(isEven, count())), [1,3,5,7]) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 77 | self.assertRaises(TypeError, ifilterfalse) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 78 | self.assertRaises(TypeError, ifilterfalse, lambda x:x) |
| 79 | self.assertRaises(TypeError, ifilterfalse, lambda x:x, range(6), 7) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 80 | self.assertRaises(TypeError, ifilterfalse, isEven, 3) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 81 | self.assertRaises(TypeError, ifilterfalse(range(6), range(6)).next) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 82 | |
| 83 | def test_izip(self): |
| 84 | ans = [(x,y) for x, y in izip('abc',count())] |
| 85 | self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 86 | self.assertEqual(list(izip('abc', range(6))), zip('abc', range(6))) |
| 87 | self.assertEqual(list(izip('abcdef', range(3))), zip('abcdef', range(3))) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame^] | 88 | self.assertEqual(take(3,izip('abcdef', count())), zip('abcdef', range(3))) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 89 | self.assertEqual(list(izip('abcdef')), zip('abcdef')) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 90 | self.assertRaises(TypeError, izip) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 91 | self.assertRaises(TypeError, izip, 3) |
| 92 | self.assertRaises(TypeError, izip, range(3), 3) |
| 93 | # Check tuple re-use (implementation detail) |
| 94 | self.assertEqual([tuple(list(pair)) for pair in izip('abc', 'def')], |
| 95 | zip('abc', 'def')) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 96 | self.assertEqual([pair for pair in izip('abc', 'def')], |
| 97 | zip('abc', 'def')) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 98 | ids = map(id, izip('abc', 'def')) |
| 99 | self.assertEqual(min(ids), max(ids)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 100 | ids = map(id, list(izip('abc', 'def'))) |
| 101 | self.assertEqual(len(dict.fromkeys(ids)), len(ids)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 102 | |
| 103 | def test_repeat(self): |
| 104 | self.assertEqual(zip(xrange(3),repeat('a')), |
| 105 | [(0, 'a'), (1, 'a'), (2, 'a')]) |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 106 | self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a']) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame^] | 107 | self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a']) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 108 | self.assertEqual(list(repeat('a', 0)), []) |
| 109 | self.assertEqual(list(repeat('a', -3)), []) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 110 | self.assertRaises(TypeError, repeat) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 111 | self.assertRaises(TypeError, repeat, None, 3, 4) |
| 112 | self.assertRaises(TypeError, repeat, None, 'a') |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 113 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 114 | def test_imap(self): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 115 | self.assertEqual(list(imap(operator.pow, range(3), range(1,7))), |
| 116 | [0**1, 1**2, 2**3]) |
| 117 | self.assertEqual(list(imap(None, 'abc', range(5))), |
| 118 | [('a',0),('b',1),('c',2)]) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame^] | 119 | self.assertEqual(list(imap(None, 'abc', count())), |
| 120 | [('a',0),('b',1),('c',2)]) |
| 121 | self.assertEqual(take(2,imap(None, 'abc', count())), |
| 122 | [('a',0),('b',1)]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 123 | self.assertEqual(list(imap(operator.pow, [])), []) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 124 | self.assertRaises(TypeError, imap) |
| 125 | self.assertRaises(TypeError, imap, operator.neg) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 126 | self.assertRaises(TypeError, imap(10, range(5)).next) |
| 127 | self.assertRaises(ValueError, imap(errfunc, [4], [5]).next) |
| 128 | self.assertRaises(TypeError, imap(onearg, [4], [5]).next) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 129 | |
| 130 | def test_starmap(self): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 131 | self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))), |
| 132 | [0**1, 1**2, 2**3]) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame^] | 133 | self.assertEqual(take(3, starmap(operator.pow, izip(count(), count(1)))), |
| 134 | [0**1, 1**2, 2**3]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 135 | self.assertEqual(list(starmap(operator.pow, [])), []) |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 136 | self.assertRaises(TypeError, list, starmap(operator.pow, [[4,5]])) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 137 | self.assertRaises(TypeError, starmap) |
| 138 | self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra') |
| 139 | self.assertRaises(TypeError, starmap(10, [(4,5)]).next) |
| 140 | self.assertRaises(ValueError, starmap(errfunc, [(4,5)]).next) |
| 141 | self.assertRaises(TypeError, starmap(onearg, [(4,5)]).next) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 142 | |
| 143 | def test_islice(self): |
| 144 | for args in [ # islice(args) should agree with range(args) |
| 145 | (10, 20, 3), |
| 146 | (10, 3, 20), |
| 147 | (10, 20), |
| 148 | (10, 3), |
| 149 | (20,) |
| 150 | ]: |
| 151 | self.assertEqual(list(islice(xrange(100), *args)), range(*args)) |
| 152 | |
| 153 | for args, tgtargs in [ # Stop when seqn is exhausted |
| 154 | ((10, 110, 3), ((10, 100, 3))), |
| 155 | ((10, 110), ((10, 100))), |
| 156 | ((110,), (100,)) |
| 157 | ]: |
| 158 | self.assertEqual(list(islice(xrange(100), *args)), range(*tgtargs)) |
| 159 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 160 | # Test stop=None |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 161 | self.assertEqual(list(islice(xrange(10), None)), range(10)) |
| 162 | self.assertEqual(list(islice(xrange(10), 2, None)), range(2, 10)) |
| 163 | self.assertEqual(list(islice(xrange(10), 1, None, 2)), range(1, 10, 2)) |
| 164 | |
| 165 | # Test invalid arguments |
Raymond Hettinger | 341deb7 | 2003-05-02 19:44:20 +0000 | [diff] [blame] | 166 | self.assertRaises(TypeError, islice, xrange(10)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 167 | self.assertRaises(TypeError, islice, xrange(10), 1, 2, 3, 4) |
| 168 | self.assertRaises(ValueError, islice, xrange(10), -5, 10, 1) |
| 169 | self.assertRaises(ValueError, islice, xrange(10), 1, -5, -1) |
| 170 | self.assertRaises(ValueError, islice, xrange(10), 1, 10, -1) |
| 171 | self.assertRaises(ValueError, islice, xrange(10), 1, 10, 0) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 172 | self.assertRaises(ValueError, islice, xrange(10), 'a') |
| 173 | self.assertRaises(ValueError, islice, xrange(10), 'a', 1) |
| 174 | self.assertRaises(ValueError, islice, xrange(10), 1, 'a') |
| 175 | self.assertRaises(ValueError, islice, xrange(10), 'a', 1, 1) |
| 176 | self.assertRaises(ValueError, islice, xrange(10), 1, 'a', 1) |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 177 | self.assertEqual(len(list(islice(count(), 1, 10, sys.maxint))), 1) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 178 | |
| 179 | def test_takewhile(self): |
| 180 | data = [1, 3, 5, 20, 2, 4, 6, 8] |
| 181 | underten = lambda x: x<10 |
| 182 | self.assertEqual(list(takewhile(underten, data)), [1, 3, 5]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 183 | self.assertEqual(list(takewhile(underten, [])), []) |
| 184 | self.assertRaises(TypeError, takewhile) |
| 185 | self.assertRaises(TypeError, takewhile, operator.pow) |
| 186 | self.assertRaises(TypeError, takewhile, operator.pow, [(4,5)], 'extra') |
| 187 | self.assertRaises(TypeError, takewhile(10, [(4,5)]).next) |
| 188 | self.assertRaises(ValueError, takewhile(errfunc, [(4,5)]).next) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 189 | |
| 190 | def test_dropwhile(self): |
| 191 | data = [1, 3, 5, 20, 2, 4, 6, 8] |
| 192 | underten = lambda x: x<10 |
| 193 | self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 194 | self.assertEqual(list(dropwhile(underten, [])), []) |
| 195 | self.assertRaises(TypeError, dropwhile) |
| 196 | self.assertRaises(TypeError, dropwhile, operator.pow) |
| 197 | self.assertRaises(TypeError, dropwhile, operator.pow, [(4,5)], 'extra') |
| 198 | self.assertRaises(TypeError, dropwhile(10, [(4,5)]).next) |
| 199 | self.assertRaises(ValueError, dropwhile(errfunc, [(4,5)]).next) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 200 | |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 201 | def test_StopIteration(self): |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 202 | for f in (chain, cycle, izip): |
| 203 | self.assertRaises(StopIteration, f([]).next) |
| 204 | self.assertRaises(StopIteration, f(StopNow()).next) |
| 205 | |
| 206 | self.assertRaises(StopIteration, islice([], None).next) |
| 207 | self.assertRaises(StopIteration, islice(StopNow(), None).next) |
| 208 | |
| 209 | self.assertRaises(StopIteration, repeat(None, 0).next) |
| 210 | |
| 211 | for f in (ifilter, ifilterfalse, imap, takewhile, dropwhile, starmap): |
| 212 | self.assertRaises(StopIteration, f(lambda x:x, []).next) |
| 213 | self.assertRaises(StopIteration, f(lambda x:x, StopNow()).next) |
| 214 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 215 | def R(seqn): |
| 216 | 'Regular generator' |
| 217 | for i in seqn: |
| 218 | yield i |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 219 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 220 | class G: |
| 221 | 'Sequence using __getitem__' |
| 222 | def __init__(self, seqn): |
| 223 | self.seqn = seqn |
| 224 | def __getitem__(self, i): |
| 225 | return self.seqn[i] |
| 226 | |
| 227 | class I: |
| 228 | 'Sequence using iterator protocol' |
| 229 | def __init__(self, seqn): |
| 230 | self.seqn = seqn |
| 231 | self.i = 0 |
| 232 | def __iter__(self): |
| 233 | return self |
| 234 | def next(self): |
| 235 | if self.i >= len(self.seqn): raise StopIteration |
| 236 | v = self.seqn[self.i] |
| 237 | self.i += 1 |
| 238 | return v |
| 239 | |
| 240 | class Ig: |
| 241 | 'Sequence using iterator protocol defined with a generator' |
| 242 | def __init__(self, seqn): |
| 243 | self.seqn = seqn |
| 244 | self.i = 0 |
| 245 | def __iter__(self): |
| 246 | for val in self.seqn: |
| 247 | yield val |
| 248 | |
| 249 | class X: |
| 250 | 'Missing __getitem__ and __iter__' |
| 251 | def __init__(self, seqn): |
| 252 | self.seqn = seqn |
| 253 | self.i = 0 |
| 254 | def next(self): |
| 255 | if self.i >= len(self.seqn): raise StopIteration |
| 256 | v = self.seqn[self.i] |
| 257 | self.i += 1 |
| 258 | return v |
| 259 | |
| 260 | class N: |
| 261 | 'Iterator missing next()' |
| 262 | def __init__(self, seqn): |
| 263 | self.seqn = seqn |
| 264 | self.i = 0 |
| 265 | def __iter__(self): |
| 266 | return self |
| 267 | |
| 268 | class E: |
| 269 | 'Test propagation of exceptions' |
| 270 | def __init__(self, seqn): |
| 271 | self.seqn = seqn |
| 272 | self.i = 0 |
| 273 | def __iter__(self): |
| 274 | return self |
| 275 | def next(self): |
| 276 | 3/0 |
| 277 | |
| 278 | class S: |
| 279 | 'Test immediate stop' |
| 280 | def __init__(self, seqn): |
| 281 | pass |
| 282 | def __iter__(self): |
| 283 | return self |
| 284 | def next(self): |
| 285 | raise StopIteration |
| 286 | |
| 287 | def L(seqn): |
| 288 | 'Test multiple tiers of iterators' |
| 289 | return chain(imap(lambda x:x, R(Ig(G(seqn))))) |
| 290 | |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame^] | 291 | class TestGC(unittest.TestCase): |
| 292 | |
| 293 | def makecycle(self, iterator, container): |
| 294 | container.append(iterator) |
| 295 | iterator.next() |
| 296 | del container, iterator |
| 297 | |
| 298 | def test_chain(self): |
| 299 | a = [] |
| 300 | self.makecycle(chain(a), a) |
| 301 | |
| 302 | def test_cycle(self): |
| 303 | a = [] |
| 304 | self.makecycle(cycle([a]*2), a) |
| 305 | |
| 306 | def test_ifilter(self): |
| 307 | a = [] |
| 308 | self.makecycle(ifilter(lambda x:True, [a]*2), a) |
| 309 | |
| 310 | def test_ifilterfalse(self): |
| 311 | a = [] |
| 312 | self.makecycle(ifilterfalse(lambda x:False, a), a) |
| 313 | |
| 314 | def test_izip(self): |
| 315 | a = [] |
| 316 | self.makecycle(izip([a]*2, [a]*3), a) |
| 317 | |
| 318 | def test_imap(self): |
| 319 | a = [] |
| 320 | self.makecycle(imap(lambda x:x, [a]*2), a) |
| 321 | |
| 322 | def test_islice(self): |
| 323 | a = [] |
| 324 | self.makecycle(islice([a]*2, None), a) |
| 325 | |
| 326 | def test_starmap(self): |
| 327 | a = [] |
| 328 | self.makecycle(starmap(lambda *t: t, [(a,a)]*2), a) |
| 329 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 330 | |
| 331 | class TestVariousIteratorArgs(unittest.TestCase): |
| 332 | |
| 333 | def test_chain(self): |
| 334 | for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
| 335 | for g in (G, I, Ig, S, L, R): |
| 336 | self.assertEqual(list(chain(g(s))), list(g(s))) |
| 337 | self.assertEqual(list(chain(g(s), g(s))), list(g(s))+list(g(s))) |
| 338 | self.assertRaises(TypeError, chain, X(s)) |
| 339 | self.assertRaises(TypeError, list, chain(N(s))) |
| 340 | self.assertRaises(ZeroDivisionError, list, chain(E(s))) |
| 341 | |
| 342 | def test_cycle(self): |
| 343 | for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
| 344 | for g in (G, I, Ig, S, L, R): |
| 345 | tgtlen = len(s) * 3 |
| 346 | expected = list(g(s))*3 |
| 347 | actual = list(islice(cycle(g(s)), tgtlen)) |
| 348 | self.assertEqual(actual, expected) |
| 349 | self.assertRaises(TypeError, cycle, X(s)) |
| 350 | self.assertRaises(TypeError, list, cycle(N(s))) |
| 351 | self.assertRaises(ZeroDivisionError, list, cycle(E(s))) |
| 352 | |
| 353 | def test_ifilter(self): |
| 354 | for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
| 355 | for g in (G, I, Ig, S, L, R): |
| 356 | self.assertEqual(list(ifilter(isEven, g(s))), filter(isEven, g(s))) |
| 357 | self.assertRaises(TypeError, ifilter, isEven, X(s)) |
| 358 | self.assertRaises(TypeError, list, ifilter(isEven, N(s))) |
| 359 | self.assertRaises(ZeroDivisionError, list, ifilter(isEven, E(s))) |
| 360 | |
| 361 | def test_ifilterfalse(self): |
| 362 | for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
| 363 | for g in (G, I, Ig, S, L, R): |
| 364 | self.assertEqual(list(ifilterfalse(isEven, g(s))), filter(isOdd, g(s))) |
| 365 | self.assertRaises(TypeError, ifilterfalse, isEven, X(s)) |
| 366 | self.assertRaises(TypeError, list, ifilterfalse(isEven, N(s))) |
| 367 | self.assertRaises(ZeroDivisionError, list, ifilterfalse(isEven, E(s))) |
| 368 | |
| 369 | def test_izip(self): |
| 370 | for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
| 371 | for g in (G, I, Ig, S, L, R): |
| 372 | self.assertEqual(list(izip(g(s))), zip(g(s))) |
| 373 | self.assertEqual(list(izip(g(s), g(s))), zip(g(s), g(s))) |
| 374 | self.assertRaises(TypeError, izip, X(s)) |
| 375 | self.assertRaises(TypeError, list, izip(N(s))) |
| 376 | self.assertRaises(ZeroDivisionError, list, izip(E(s))) |
| 377 | |
| 378 | def test_imap(self): |
| 379 | for s in (range(10), range(0), range(100), (7,11), xrange(20,50,5)): |
| 380 | for g in (G, I, Ig, S, L, R): |
| 381 | self.assertEqual(list(imap(onearg, g(s))), map(onearg, g(s))) |
| 382 | self.assertEqual(list(imap(operator.pow, g(s), g(s))), map(operator.pow, g(s), g(s))) |
| 383 | self.assertRaises(TypeError, imap, onearg, X(s)) |
| 384 | self.assertRaises(TypeError, list, imap(onearg, N(s))) |
| 385 | self.assertRaises(ZeroDivisionError, list, imap(onearg, E(s))) |
| 386 | |
| 387 | def test_islice(self): |
| 388 | for s in ("12345", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
| 389 | for g in (G, I, Ig, S, L, R): |
| 390 | self.assertEqual(list(islice(g(s),1,None,2)), list(g(s))[1::2]) |
| 391 | self.assertRaises(TypeError, islice, X(s), 10) |
| 392 | self.assertRaises(TypeError, list, islice(N(s), 10)) |
| 393 | self.assertRaises(ZeroDivisionError, list, islice(E(s), 10)) |
| 394 | |
| 395 | def test_starmap(self): |
| 396 | for s in (range(10), range(0), range(100), (7,11), xrange(20,50,5)): |
| 397 | for g in (G, I, Ig, S, L, R): |
| 398 | ss = zip(s, s) |
| 399 | self.assertEqual(list(starmap(operator.pow, g(ss))), map(operator.pow, g(s), g(s))) |
| 400 | self.assertRaises(TypeError, starmap, operator.pow, X(ss)) |
| 401 | self.assertRaises(TypeError, list, starmap(operator.pow, N(ss))) |
| 402 | self.assertRaises(ZeroDivisionError, list, starmap(operator.pow, E(ss))) |
| 403 | |
| 404 | def test_takewhile(self): |
| 405 | for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
| 406 | for g in (G, I, Ig, S, L, R): |
| 407 | tgt = [] |
| 408 | for elem in g(s): |
| 409 | if not isEven(elem): break |
| 410 | tgt.append(elem) |
| 411 | self.assertEqual(list(takewhile(isEven, g(s))), tgt) |
| 412 | self.assertRaises(TypeError, takewhile, isEven, X(s)) |
| 413 | self.assertRaises(TypeError, list, takewhile(isEven, N(s))) |
| 414 | self.assertRaises(ZeroDivisionError, list, takewhile(isEven, E(s))) |
| 415 | |
| 416 | def test_dropwhile(self): |
| 417 | for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
| 418 | for g in (G, I, Ig, S, L, R): |
| 419 | tgt = [] |
| 420 | for elem in g(s): |
| 421 | if not tgt and isOdd(elem): continue |
| 422 | tgt.append(elem) |
| 423 | self.assertEqual(list(dropwhile(isOdd, g(s))), tgt) |
| 424 | self.assertRaises(TypeError, dropwhile, isOdd, X(s)) |
| 425 | self.assertRaises(TypeError, list, dropwhile(isOdd, N(s))) |
| 426 | self.assertRaises(ZeroDivisionError, list, dropwhile(isOdd, E(s))) |
| 427 | |
| 428 | |
| 429 | libreftest = """ Doctest for examples in the library reference: libitertools.tex |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 430 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 431 | |
| 432 | >>> amounts = [120.15, 764.05, 823.14] |
| 433 | >>> for checknum, amount in izip(count(1200), amounts): |
| 434 | ... print 'Check %d is for $%.2f' % (checknum, amount) |
| 435 | ... |
| 436 | Check 1200 is for $120.15 |
| 437 | Check 1201 is for $764.05 |
| 438 | Check 1202 is for $823.14 |
| 439 | |
| 440 | >>> import operator |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 441 | >>> for cube in imap(operator.pow, xrange(1,4), repeat(3)): |
| 442 | ... print cube |
| 443 | ... |
| 444 | 1 |
| 445 | 8 |
| 446 | 27 |
| 447 | |
| 448 | >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele'] |
Raymond Hettinger | 3567a87 | 2003-06-28 05:44:36 +0000 | [diff] [blame] | 449 | >>> for name in islice(reportlines, 3, None, 2): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 450 | ... print name.title() |
| 451 | ... |
| 452 | Alex |
| 453 | Laura |
| 454 | Martin |
| 455 | Walter |
| 456 | Samuele |
| 457 | |
| 458 | >>> def enumerate(iterable): |
| 459 | ... return izip(count(), iterable) |
| 460 | |
| 461 | >>> def tabulate(function): |
| 462 | ... "Return function(0), function(1), ..." |
| 463 | ... return imap(function, count()) |
| 464 | |
| 465 | >>> def iteritems(mapping): |
| 466 | ... return izip(mapping.iterkeys(), mapping.itervalues()) |
| 467 | |
| 468 | >>> def nth(iterable, n): |
| 469 | ... "Returns the nth item" |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 470 | ... return list(islice(iterable, n, n+1)) |
| 471 | |
| 472 | >>> def all(pred, seq): |
| 473 | ... "Returns True if pred(x) is True for every element in the iterable" |
| 474 | ... return not nth(ifilterfalse(pred, seq), 0) |
| 475 | |
| 476 | >>> def some(pred, seq): |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame^] | 477 | ... "Returns True if pred(x) is True for at least one element in the iterable" |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 478 | ... return bool(nth(ifilter(pred, seq), 0)) |
| 479 | |
| 480 | >>> def no(pred, seq): |
| 481 | ... "Returns True if pred(x) is False for every element in the iterable" |
| 482 | ... return not nth(ifilter(pred, seq), 0) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 483 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 484 | >>> def padnone(seq): |
| 485 | ... "Returns the sequence elements and then returns None indefinitely" |
| 486 | ... return chain(seq, repeat(None)) |
| 487 | |
| 488 | >>> def ncycles(seq, n): |
| 489 | ... "Returns the sequence elements n times" |
| 490 | ... return chain(*repeat(seq, n)) |
| 491 | |
| 492 | >>> def dotproduct(vec1, vec2): |
| 493 | ... return sum(imap(operator.mul, vec1, vec2)) |
| 494 | |
Raymond Hettinger | befa37d | 2003-06-18 19:25:37 +0000 | [diff] [blame] | 495 | >>> def window(seq, n=2): |
| 496 | ... "Returns a sliding window (of width n) over data from the iterable" |
| 497 | ... " s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... " |
| 498 | ... it = iter(seq) |
| 499 | ... result = tuple(islice(it, n)) |
| 500 | ... if len(result) == n: |
| 501 | ... yield result |
| 502 | ... for elem in it: |
| 503 | ... result = result[1:] + (elem,) |
| 504 | ... yield result |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 505 | |
Raymond Hettinger | 3567a87 | 2003-06-28 05:44:36 +0000 | [diff] [blame] | 506 | >>> def take(n, seq): |
| 507 | ... return list(islice(seq, n)) |
| 508 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 509 | This is not part of the examples but it tests to make sure the definitions |
| 510 | perform as purported. |
| 511 | |
| 512 | >>> list(enumerate('abc')) |
| 513 | [(0, 'a'), (1, 'b'), (2, 'c')] |
| 514 | |
| 515 | >>> list(islice(tabulate(lambda x: 2*x), 4)) |
| 516 | [0, 2, 4, 6] |
| 517 | |
| 518 | >>> nth('abcde', 3) |
| 519 | ['d'] |
| 520 | |
| 521 | >>> all(lambda x: x%2==0, [2, 4, 6, 8]) |
| 522 | True |
| 523 | |
| 524 | >>> all(lambda x: x%2==0, [2, 3, 6, 8]) |
| 525 | False |
| 526 | |
| 527 | >>> some(lambda x: x%2==0, [2, 4, 6, 8]) |
| 528 | True |
| 529 | |
| 530 | >>> some(lambda x: x%2==0, [1, 3, 5, 9]) |
| 531 | False |
| 532 | |
| 533 | >>> no(lambda x: x%2==0, [1, 3, 5, 9]) |
| 534 | True |
| 535 | |
| 536 | >>> no(lambda x: x%2==0, [1, 2, 5, 9]) |
| 537 | False |
| 538 | |
Raymond Hettinger | befa37d | 2003-06-18 19:25:37 +0000 | [diff] [blame] | 539 | >>> list(window('abc')) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 540 | [('a', 'b'), ('b', 'c')] |
| 541 | |
Raymond Hettinger | befa37d | 2003-06-18 19:25:37 +0000 | [diff] [blame] | 542 | >>> list(window('abc',5)) |
| 543 | [] |
| 544 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 545 | >>> list(islice(padnone('abc'), 0, 6)) |
| 546 | ['a', 'b', 'c', None, None, None] |
| 547 | |
| 548 | >>> list(ncycles('abc', 3)) |
| 549 | ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] |
| 550 | |
| 551 | >>> dotproduct([1,2,3], [4,5,6]) |
| 552 | 32 |
| 553 | |
Raymond Hettinger | 3567a87 | 2003-06-28 05:44:36 +0000 | [diff] [blame] | 554 | >>> take(10, count()) |
| 555 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 556 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 557 | """ |
| 558 | |
| 559 | __test__ = {'libreftest' : libreftest} |
| 560 | |
| 561 | def test_main(verbose=None): |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame^] | 562 | test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 563 | test_support.run_unittest(*test_classes) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 564 | |
| 565 | # verify reference counting |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 566 | if verbose and hasattr(sys, "gettotalrefcount"): |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame^] | 567 | import gc |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 568 | counts = [None] * 5 |
| 569 | for i in xrange(len(counts)): |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 570 | test_support.run_unittest(*test_classes) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame^] | 571 | gc.collect() |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 572 | counts[i] = sys.gettotalrefcount() |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 573 | print counts |
| 574 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 575 | # doctest the examples in the library reference |
Raymond Hettinger | 929f06c | 2003-05-16 23:16:36 +0000 | [diff] [blame] | 576 | test_support.run_doctest(sys.modules[__name__], verbose) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 577 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 578 | if __name__ == "__main__": |
| 579 | test_main(verbose=True) |