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