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 | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame^] | 90 | self.assertEqual(list(izip()), zip()) |
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 | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame^] | 202 | self.assertRaises(StopIteration, izip().next) |
| 203 | |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 204 | for f in (chain, cycle, izip): |
| 205 | self.assertRaises(StopIteration, f([]).next) |
| 206 | self.assertRaises(StopIteration, f(StopNow()).next) |
| 207 | |
| 208 | self.assertRaises(StopIteration, islice([], None).next) |
| 209 | self.assertRaises(StopIteration, islice(StopNow(), None).next) |
| 210 | |
| 211 | self.assertRaises(StopIteration, repeat(None, 0).next) |
| 212 | |
| 213 | for f in (ifilter, ifilterfalse, imap, takewhile, dropwhile, starmap): |
| 214 | self.assertRaises(StopIteration, f(lambda x:x, []).next) |
| 215 | self.assertRaises(StopIteration, f(lambda x:x, StopNow()).next) |
| 216 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 217 | def R(seqn): |
| 218 | 'Regular generator' |
| 219 | for i in seqn: |
| 220 | yield i |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 221 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 222 | class G: |
| 223 | 'Sequence using __getitem__' |
| 224 | def __init__(self, seqn): |
| 225 | self.seqn = seqn |
| 226 | def __getitem__(self, i): |
| 227 | return self.seqn[i] |
| 228 | |
| 229 | class I: |
| 230 | 'Sequence using iterator protocol' |
| 231 | def __init__(self, seqn): |
| 232 | self.seqn = seqn |
| 233 | self.i = 0 |
| 234 | def __iter__(self): |
| 235 | return self |
| 236 | def next(self): |
| 237 | if self.i >= len(self.seqn): raise StopIteration |
| 238 | v = self.seqn[self.i] |
| 239 | self.i += 1 |
| 240 | return v |
| 241 | |
| 242 | class Ig: |
| 243 | 'Sequence using iterator protocol defined with a generator' |
| 244 | def __init__(self, seqn): |
| 245 | self.seqn = seqn |
| 246 | self.i = 0 |
| 247 | def __iter__(self): |
| 248 | for val in self.seqn: |
| 249 | yield val |
| 250 | |
| 251 | class X: |
| 252 | 'Missing __getitem__ and __iter__' |
| 253 | def __init__(self, seqn): |
| 254 | self.seqn = seqn |
| 255 | self.i = 0 |
| 256 | def next(self): |
| 257 | if self.i >= len(self.seqn): raise StopIteration |
| 258 | v = self.seqn[self.i] |
| 259 | self.i += 1 |
| 260 | return v |
| 261 | |
| 262 | class N: |
| 263 | 'Iterator missing next()' |
| 264 | def __init__(self, seqn): |
| 265 | self.seqn = seqn |
| 266 | self.i = 0 |
| 267 | def __iter__(self): |
| 268 | return self |
| 269 | |
| 270 | class E: |
| 271 | 'Test propagation of exceptions' |
| 272 | def __init__(self, seqn): |
| 273 | self.seqn = seqn |
| 274 | self.i = 0 |
| 275 | def __iter__(self): |
| 276 | return self |
| 277 | def next(self): |
| 278 | 3/0 |
| 279 | |
| 280 | class S: |
| 281 | 'Test immediate stop' |
| 282 | def __init__(self, seqn): |
| 283 | pass |
| 284 | def __iter__(self): |
| 285 | return self |
| 286 | def next(self): |
| 287 | raise StopIteration |
| 288 | |
| 289 | def L(seqn): |
| 290 | 'Test multiple tiers of iterators' |
| 291 | return chain(imap(lambda x:x, R(Ig(G(seqn))))) |
| 292 | |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 293 | class TestGC(unittest.TestCase): |
| 294 | |
| 295 | def makecycle(self, iterator, container): |
| 296 | container.append(iterator) |
| 297 | iterator.next() |
| 298 | del container, iterator |
| 299 | |
| 300 | def test_chain(self): |
| 301 | a = [] |
| 302 | self.makecycle(chain(a), a) |
| 303 | |
| 304 | def test_cycle(self): |
| 305 | a = [] |
| 306 | self.makecycle(cycle([a]*2), a) |
| 307 | |
| 308 | def test_ifilter(self): |
| 309 | a = [] |
| 310 | self.makecycle(ifilter(lambda x:True, [a]*2), a) |
| 311 | |
| 312 | def test_ifilterfalse(self): |
| 313 | a = [] |
| 314 | self.makecycle(ifilterfalse(lambda x:False, a), a) |
| 315 | |
| 316 | def test_izip(self): |
| 317 | a = [] |
| 318 | self.makecycle(izip([a]*2, [a]*3), a) |
| 319 | |
| 320 | def test_imap(self): |
| 321 | a = [] |
| 322 | self.makecycle(imap(lambda x:x, [a]*2), a) |
| 323 | |
| 324 | def test_islice(self): |
| 325 | a = [] |
| 326 | self.makecycle(islice([a]*2, None), a) |
| 327 | |
| 328 | def test_starmap(self): |
| 329 | a = [] |
| 330 | self.makecycle(starmap(lambda *t: t, [(a,a)]*2), a) |
| 331 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 332 | |
| 333 | class TestVariousIteratorArgs(unittest.TestCase): |
| 334 | |
| 335 | def test_chain(self): |
| 336 | for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
| 337 | for g in (G, I, Ig, S, L, R): |
| 338 | self.assertEqual(list(chain(g(s))), list(g(s))) |
| 339 | self.assertEqual(list(chain(g(s), g(s))), list(g(s))+list(g(s))) |
| 340 | self.assertRaises(TypeError, chain, X(s)) |
| 341 | self.assertRaises(TypeError, list, chain(N(s))) |
| 342 | self.assertRaises(ZeroDivisionError, list, chain(E(s))) |
| 343 | |
| 344 | def test_cycle(self): |
| 345 | for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
| 346 | for g in (G, I, Ig, S, L, R): |
| 347 | tgtlen = len(s) * 3 |
| 348 | expected = list(g(s))*3 |
| 349 | actual = list(islice(cycle(g(s)), tgtlen)) |
| 350 | self.assertEqual(actual, expected) |
| 351 | self.assertRaises(TypeError, cycle, X(s)) |
| 352 | self.assertRaises(TypeError, list, cycle(N(s))) |
| 353 | self.assertRaises(ZeroDivisionError, list, cycle(E(s))) |
| 354 | |
| 355 | def test_ifilter(self): |
| 356 | for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
| 357 | for g in (G, I, Ig, S, L, R): |
| 358 | self.assertEqual(list(ifilter(isEven, g(s))), filter(isEven, g(s))) |
| 359 | self.assertRaises(TypeError, ifilter, isEven, X(s)) |
| 360 | self.assertRaises(TypeError, list, ifilter(isEven, N(s))) |
| 361 | self.assertRaises(ZeroDivisionError, list, ifilter(isEven, E(s))) |
| 362 | |
| 363 | def test_ifilterfalse(self): |
| 364 | for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
| 365 | for g in (G, I, Ig, S, L, R): |
| 366 | self.assertEqual(list(ifilterfalse(isEven, g(s))), filter(isOdd, g(s))) |
| 367 | self.assertRaises(TypeError, ifilterfalse, isEven, X(s)) |
| 368 | self.assertRaises(TypeError, list, ifilterfalse(isEven, N(s))) |
| 369 | self.assertRaises(ZeroDivisionError, list, ifilterfalse(isEven, E(s))) |
| 370 | |
| 371 | def test_izip(self): |
| 372 | for s in ("123", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
| 373 | for g in (G, I, Ig, S, L, R): |
| 374 | self.assertEqual(list(izip(g(s))), zip(g(s))) |
| 375 | self.assertEqual(list(izip(g(s), g(s))), zip(g(s), g(s))) |
| 376 | self.assertRaises(TypeError, izip, X(s)) |
| 377 | self.assertRaises(TypeError, list, izip(N(s))) |
| 378 | self.assertRaises(ZeroDivisionError, list, izip(E(s))) |
| 379 | |
| 380 | def test_imap(self): |
| 381 | for s in (range(10), range(0), range(100), (7,11), xrange(20,50,5)): |
| 382 | for g in (G, I, Ig, S, L, R): |
| 383 | self.assertEqual(list(imap(onearg, g(s))), map(onearg, g(s))) |
| 384 | self.assertEqual(list(imap(operator.pow, g(s), g(s))), map(operator.pow, g(s), g(s))) |
| 385 | self.assertRaises(TypeError, imap, onearg, X(s)) |
| 386 | self.assertRaises(TypeError, list, imap(onearg, N(s))) |
| 387 | self.assertRaises(ZeroDivisionError, list, imap(onearg, E(s))) |
| 388 | |
| 389 | def test_islice(self): |
| 390 | for s in ("12345", "", range(1000), ('do', 1.2), xrange(2000,2200,5)): |
| 391 | for g in (G, I, Ig, S, L, R): |
| 392 | self.assertEqual(list(islice(g(s),1,None,2)), list(g(s))[1::2]) |
| 393 | self.assertRaises(TypeError, islice, X(s), 10) |
| 394 | self.assertRaises(TypeError, list, islice(N(s), 10)) |
| 395 | self.assertRaises(ZeroDivisionError, list, islice(E(s), 10)) |
| 396 | |
| 397 | def test_starmap(self): |
| 398 | for s in (range(10), range(0), range(100), (7,11), xrange(20,50,5)): |
| 399 | for g in (G, I, Ig, S, L, R): |
| 400 | ss = zip(s, s) |
| 401 | self.assertEqual(list(starmap(operator.pow, g(ss))), map(operator.pow, g(s), g(s))) |
| 402 | self.assertRaises(TypeError, starmap, operator.pow, X(ss)) |
| 403 | self.assertRaises(TypeError, list, starmap(operator.pow, N(ss))) |
| 404 | self.assertRaises(ZeroDivisionError, list, starmap(operator.pow, E(ss))) |
| 405 | |
| 406 | def test_takewhile(self): |
| 407 | for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
| 408 | for g in (G, I, Ig, S, L, R): |
| 409 | tgt = [] |
| 410 | for elem in g(s): |
| 411 | if not isEven(elem): break |
| 412 | tgt.append(elem) |
| 413 | self.assertEqual(list(takewhile(isEven, g(s))), tgt) |
| 414 | self.assertRaises(TypeError, takewhile, isEven, X(s)) |
| 415 | self.assertRaises(TypeError, list, takewhile(isEven, N(s))) |
| 416 | self.assertRaises(ZeroDivisionError, list, takewhile(isEven, E(s))) |
| 417 | |
| 418 | def test_dropwhile(self): |
| 419 | for s in (range(10), range(0), range(1000), (7,11), xrange(2000,2200,5)): |
| 420 | for g in (G, I, Ig, S, L, R): |
| 421 | tgt = [] |
| 422 | for elem in g(s): |
| 423 | if not tgt and isOdd(elem): continue |
| 424 | tgt.append(elem) |
| 425 | self.assertEqual(list(dropwhile(isOdd, g(s))), tgt) |
| 426 | self.assertRaises(TypeError, dropwhile, isOdd, X(s)) |
| 427 | self.assertRaises(TypeError, list, dropwhile(isOdd, N(s))) |
| 428 | self.assertRaises(ZeroDivisionError, list, dropwhile(isOdd, E(s))) |
| 429 | |
| 430 | |
| 431 | libreftest = """ Doctest for examples in the library reference: libitertools.tex |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 432 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 433 | |
| 434 | >>> amounts = [120.15, 764.05, 823.14] |
| 435 | >>> for checknum, amount in izip(count(1200), amounts): |
| 436 | ... print 'Check %d is for $%.2f' % (checknum, amount) |
| 437 | ... |
| 438 | Check 1200 is for $120.15 |
| 439 | Check 1201 is for $764.05 |
| 440 | Check 1202 is for $823.14 |
| 441 | |
| 442 | >>> import operator |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 443 | >>> for cube in imap(operator.pow, xrange(1,4), repeat(3)): |
| 444 | ... print cube |
| 445 | ... |
| 446 | 1 |
| 447 | 8 |
| 448 | 27 |
| 449 | |
| 450 | >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele'] |
Raymond Hettinger | 3567a87 | 2003-06-28 05:44:36 +0000 | [diff] [blame] | 451 | >>> for name in islice(reportlines, 3, None, 2): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 452 | ... print name.title() |
| 453 | ... |
| 454 | Alex |
| 455 | Laura |
| 456 | Martin |
| 457 | Walter |
| 458 | Samuele |
| 459 | |
| 460 | >>> def enumerate(iterable): |
| 461 | ... return izip(count(), iterable) |
| 462 | |
| 463 | >>> def tabulate(function): |
| 464 | ... "Return function(0), function(1), ..." |
| 465 | ... return imap(function, count()) |
| 466 | |
| 467 | >>> def iteritems(mapping): |
| 468 | ... return izip(mapping.iterkeys(), mapping.itervalues()) |
| 469 | |
| 470 | >>> def nth(iterable, n): |
| 471 | ... "Returns the nth item" |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 472 | ... return list(islice(iterable, n, n+1)) |
| 473 | |
| 474 | >>> def all(pred, seq): |
| 475 | ... "Returns True if pred(x) is True for every element in the iterable" |
Raymond Hettinger | 77fe69b | 2003-08-08 04:33:19 +0000 | [diff] [blame] | 476 | ... return False not in imap(pred, seq) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 477 | |
| 478 | >>> def some(pred, seq): |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 479 | ... "Returns True if pred(x) is True for at least one element in the iterable" |
Raymond Hettinger | 77fe69b | 2003-08-08 04:33:19 +0000 | [diff] [blame] | 480 | ... return True in imap(pred, seq) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 481 | |
| 482 | >>> def no(pred, seq): |
| 483 | ... "Returns True if pred(x) is False for every element in the iterable" |
Raymond Hettinger | 77fe69b | 2003-08-08 04:33:19 +0000 | [diff] [blame] | 484 | ... return True not in imap(pred, seq) |
| 485 | |
| 486 | >>> def quantify(pred, seq): |
| 487 | ... "Count how many times the predicate is True in the sequence" |
| 488 | ... return sum(imap(pred, seq)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 489 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 490 | >>> def padnone(seq): |
| 491 | ... "Returns the sequence elements and then returns None indefinitely" |
| 492 | ... return chain(seq, repeat(None)) |
| 493 | |
| 494 | >>> def ncycles(seq, n): |
| 495 | ... "Returns the sequence elements n times" |
| 496 | ... return chain(*repeat(seq, n)) |
| 497 | |
| 498 | >>> def dotproduct(vec1, vec2): |
| 499 | ... return sum(imap(operator.mul, vec1, vec2)) |
| 500 | |
Raymond Hettinger | befa37d | 2003-06-18 19:25:37 +0000 | [diff] [blame] | 501 | >>> def window(seq, n=2): |
| 502 | ... "Returns a sliding window (of width n) over data from the iterable" |
| 503 | ... " s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... " |
| 504 | ... it = iter(seq) |
| 505 | ... result = tuple(islice(it, n)) |
| 506 | ... if len(result) == n: |
| 507 | ... yield result |
| 508 | ... for elem in it: |
| 509 | ... result = result[1:] + (elem,) |
| 510 | ... yield result |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 511 | |
Raymond Hettinger | 3567a87 | 2003-06-28 05:44:36 +0000 | [diff] [blame] | 512 | >>> def take(n, seq): |
| 513 | ... return list(islice(seq, n)) |
| 514 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 515 | This is not part of the examples but it tests to make sure the definitions |
| 516 | perform as purported. |
| 517 | |
| 518 | >>> list(enumerate('abc')) |
| 519 | [(0, 'a'), (1, 'b'), (2, 'c')] |
| 520 | |
| 521 | >>> list(islice(tabulate(lambda x: 2*x), 4)) |
| 522 | [0, 2, 4, 6] |
| 523 | |
| 524 | >>> nth('abcde', 3) |
| 525 | ['d'] |
| 526 | |
| 527 | >>> all(lambda x: x%2==0, [2, 4, 6, 8]) |
| 528 | True |
| 529 | |
| 530 | >>> all(lambda x: x%2==0, [2, 3, 6, 8]) |
| 531 | False |
| 532 | |
| 533 | >>> some(lambda x: x%2==0, [2, 4, 6, 8]) |
| 534 | True |
| 535 | |
| 536 | >>> some(lambda x: x%2==0, [1, 3, 5, 9]) |
| 537 | False |
| 538 | |
| 539 | >>> no(lambda x: x%2==0, [1, 3, 5, 9]) |
| 540 | True |
| 541 | |
| 542 | >>> no(lambda x: x%2==0, [1, 2, 5, 9]) |
| 543 | False |
| 544 | |
Raymond Hettinger | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame^] | 545 | >>> quantify(lambda x: x%2==0, xrange(99)) |
| 546 | 50 |
| 547 | |
Raymond Hettinger | befa37d | 2003-06-18 19:25:37 +0000 | [diff] [blame] | 548 | >>> list(window('abc')) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 549 | [('a', 'b'), ('b', 'c')] |
| 550 | |
Raymond Hettinger | befa37d | 2003-06-18 19:25:37 +0000 | [diff] [blame] | 551 | >>> list(window('abc',5)) |
| 552 | [] |
| 553 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 554 | >>> list(islice(padnone('abc'), 0, 6)) |
| 555 | ['a', 'b', 'c', None, None, None] |
| 556 | |
| 557 | >>> list(ncycles('abc', 3)) |
| 558 | ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] |
| 559 | |
| 560 | >>> dotproduct([1,2,3], [4,5,6]) |
| 561 | 32 |
| 562 | |
Raymond Hettinger | 3567a87 | 2003-06-28 05:44:36 +0000 | [diff] [blame] | 563 | >>> take(10, count()) |
| 564 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 565 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 566 | """ |
| 567 | |
| 568 | __test__ = {'libreftest' : libreftest} |
| 569 | |
| 570 | def test_main(verbose=None): |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 571 | test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 572 | test_support.run_unittest(*test_classes) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 573 | |
| 574 | # verify reference counting |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 575 | if verbose and hasattr(sys, "gettotalrefcount"): |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 576 | import gc |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 577 | counts = [None] * 5 |
| 578 | for i in xrange(len(counts)): |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 579 | test_support.run_unittest(*test_classes) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 580 | gc.collect() |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 581 | counts[i] = sys.gettotalrefcount() |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 582 | print counts |
| 583 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 584 | # doctest the examples in the library reference |
Raymond Hettinger | 929f06c | 2003-05-16 23:16:36 +0000 | [diff] [blame] | 585 | test_support.run_doctest(sys.modules[__name__], verbose) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 586 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 587 | if __name__ == "__main__": |
| 588 | test_main(verbose=True) |