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