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