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