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