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