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 | |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame^] | 286 | # Test implementation detail: tuple re-use |
| 287 | self.assertEqual(len(set(map(id, product('abc', 'def')))), 1) |
| 288 | self.assertNotEqual(len(set(map(id, list(product('abc', 'def'))))), 1) |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 289 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 290 | def test_repeat(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 291 | self.assertEqual(lzip(range(3),repeat('a')), |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 292 | [(0, 'a'), (1, 'a'), (2, 'a')]) |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 293 | self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a']) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 294 | self.assertEqual(take(3, repeat('a')), ['a', 'a', 'a']) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 295 | self.assertEqual(list(repeat('a', 0)), []) |
| 296 | self.assertEqual(list(repeat('a', -3)), []) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 297 | self.assertRaises(TypeError, repeat) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 298 | self.assertRaises(TypeError, repeat, None, 3, 4) |
| 299 | self.assertRaises(TypeError, repeat, None, 'a') |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 300 | r = repeat(1+0j) |
| 301 | self.assertEqual(repr(r), 'repeat((1+0j))') |
| 302 | r = repeat(1+0j, 5) |
| 303 | self.assertEqual(repr(r), 'repeat((1+0j), 5)') |
| 304 | list(r) |
| 305 | self.assertEqual(repr(r), 'repeat((1+0j), 0)') |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 306 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 307 | def test_imap(self): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 308 | self.assertEqual(list(imap(operator.pow, range(3), range(1,7))), |
| 309 | [0**1, 1**2, 2**3]) |
Raymond Hettinger | 1dfde1d | 2008-01-22 23:25:35 +0000 | [diff] [blame] | 310 | def tupleize(*args): |
| 311 | return args |
| 312 | self.assertEqual(list(imap(tupleize, 'abc', range(5))), |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 313 | [('a',0),('b',1),('c',2)]) |
Raymond Hettinger | 1dfde1d | 2008-01-22 23:25:35 +0000 | [diff] [blame] | 314 | self.assertEqual(list(imap(tupleize, 'abc', count())), |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 315 | [('a',0),('b',1),('c',2)]) |
Raymond Hettinger | 1dfde1d | 2008-01-22 23:25:35 +0000 | [diff] [blame] | 316 | self.assertEqual(take(2,imap(tupleize, 'abc', count())), |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 317 | [('a',0),('b',1)]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 318 | self.assertEqual(list(imap(operator.pow, [])), []) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 319 | self.assertRaises(TypeError, imap) |
Raymond Hettinger | 1dfde1d | 2008-01-22 23:25:35 +0000 | [diff] [blame] | 320 | self.assertRaises(TypeError, list, imap(None, range(3), range(3))) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 321 | self.assertRaises(TypeError, imap, operator.neg) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 322 | self.assertRaises(TypeError, next, imap(10, range(5))) |
| 323 | self.assertRaises(ValueError, next, imap(errfunc, [4], [5])) |
| 324 | self.assertRaises(TypeError, next, imap(onearg, [4], [5])) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 325 | |
| 326 | def test_starmap(self): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 327 | self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))), |
| 328 | [0**1, 1**2, 2**3]) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 329 | self.assertEqual(take(3, starmap(operator.pow, izip(count(), count(1)))), |
| 330 | [0**1, 1**2, 2**3]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 331 | self.assertEqual(list(starmap(operator.pow, [])), []) |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 332 | self.assertEqual(list(starmap(operator.pow, [iter([4,5])])), [4**5]) |
| 333 | self.assertRaises(TypeError, list, starmap(operator.pow, [None])) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 334 | self.assertRaises(TypeError, starmap) |
| 335 | self.assertRaises(TypeError, starmap, operator.pow, [(4,5)], 'extra') |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 336 | self.assertRaises(TypeError, next, starmap(10, [(4,5)])) |
| 337 | self.assertRaises(ValueError, next, starmap(errfunc, [(4,5)])) |
| 338 | self.assertRaises(TypeError, next, starmap(onearg, [(4,5)])) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 339 | |
| 340 | def test_islice(self): |
| 341 | for args in [ # islice(args) should agree with range(args) |
| 342 | (10, 20, 3), |
| 343 | (10, 3, 20), |
| 344 | (10, 20), |
| 345 | (10, 3), |
| 346 | (20,) |
| 347 | ]: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 348 | self.assertEqual(list(islice(range(100), *args)), |
| 349 | list(range(*args))) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 350 | |
| 351 | for args, tgtargs in [ # Stop when seqn is exhausted |
| 352 | ((10, 110, 3), ((10, 100, 3))), |
| 353 | ((10, 110), ((10, 100))), |
| 354 | ((110,), (100,)) |
| 355 | ]: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 356 | self.assertEqual(list(islice(range(100), *args)), |
| 357 | list(range(*tgtargs))) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 358 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 359 | # Test stop=None |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 360 | self.assertEqual(list(islice(range(10), None)), list(range(10))) |
| 361 | self.assertEqual(list(islice(range(10), None, None)), list(range(10))) |
| 362 | self.assertEqual(list(islice(range(10), None, None, None)), list(range(10))) |
| 363 | self.assertEqual(list(islice(range(10), 2, None)), list(range(2, 10))) |
| 364 | 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] | 365 | |
Raymond Hettinger | fdf3bd6 | 2005-03-27 20:11:44 +0000 | [diff] [blame] | 366 | # Test number of items consumed SF #1171417 |
| 367 | it = iter(range(10)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 368 | self.assertEqual(list(islice(it, 3)), list(range(3))) |
| 369 | self.assertEqual(list(it), list(range(3, 10))) |
Raymond Hettinger | fdf3bd6 | 2005-03-27 20:11:44 +0000 | [diff] [blame] | 370 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 371 | # Test invalid arguments |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 372 | self.assertRaises(TypeError, islice, range(10)) |
| 373 | self.assertRaises(TypeError, islice, range(10), 1, 2, 3, 4) |
| 374 | self.assertRaises(ValueError, islice, range(10), -5, 10, 1) |
| 375 | self.assertRaises(ValueError, islice, range(10), 1, -5, -1) |
| 376 | self.assertRaises(ValueError, islice, range(10), 1, 10, -1) |
| 377 | self.assertRaises(ValueError, islice, range(10), 1, 10, 0) |
| 378 | self.assertRaises(ValueError, islice, range(10), 'a') |
| 379 | self.assertRaises(ValueError, islice, range(10), 'a', 1) |
| 380 | self.assertRaises(ValueError, islice, range(10), 1, 'a') |
| 381 | self.assertRaises(ValueError, islice, range(10), 'a', 1, 1) |
| 382 | self.assertRaises(ValueError, islice, range(10), 1, 'a', 1) |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 383 | self.assertEqual(len(list(islice(count(), 1, 10, maxsize))), 1) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 384 | |
| 385 | def test_takewhile(self): |
| 386 | data = [1, 3, 5, 20, 2, 4, 6, 8] |
| 387 | underten = lambda x: x<10 |
| 388 | self.assertEqual(list(takewhile(underten, data)), [1, 3, 5]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 389 | self.assertEqual(list(takewhile(underten, [])), []) |
| 390 | self.assertRaises(TypeError, takewhile) |
| 391 | self.assertRaises(TypeError, takewhile, operator.pow) |
| 392 | self.assertRaises(TypeError, takewhile, operator.pow, [(4,5)], 'extra') |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 393 | self.assertRaises(TypeError, next, takewhile(10, [(4,5)])) |
| 394 | self.assertRaises(ValueError, next, takewhile(errfunc, [(4,5)])) |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 395 | t = takewhile(bool, [1, 1, 1, 0, 0, 0]) |
| 396 | self.assertEqual(list(t), [1, 1, 1]) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 397 | self.assertRaises(StopIteration, next, t) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 398 | |
| 399 | def test_dropwhile(self): |
| 400 | data = [1, 3, 5, 20, 2, 4, 6, 8] |
| 401 | underten = lambda x: x<10 |
| 402 | self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8]) |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 403 | self.assertEqual(list(dropwhile(underten, [])), []) |
| 404 | self.assertRaises(TypeError, dropwhile) |
| 405 | self.assertRaises(TypeError, dropwhile, operator.pow) |
| 406 | self.assertRaises(TypeError, dropwhile, operator.pow, [(4,5)], 'extra') |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 407 | self.assertRaises(TypeError, next, dropwhile(10, [(4,5)])) |
| 408 | self.assertRaises(ValueError, next, dropwhile(errfunc, [(4,5)])) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 409 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 410 | def test_tee(self): |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 411 | n = 200 |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 412 | def irange(n): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 413 | for i in range(n): |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 414 | yield i |
| 415 | |
| 416 | a, b = tee([]) # test empty iterator |
| 417 | self.assertEqual(list(a), []) |
| 418 | self.assertEqual(list(b), []) |
| 419 | |
| 420 | a, b = tee(irange(n)) # test 100% interleaved |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 421 | self.assertEqual(lzip(a,b), lzip(range(n), range(n))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 422 | |
| 423 | a, b = tee(irange(n)) # test 0% interleaved |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 424 | self.assertEqual(list(a), list(range(n))) |
| 425 | self.assertEqual(list(b), list(range(n))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 426 | |
| 427 | a, b = tee(irange(n)) # test dealloc of leading iterator |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 428 | for i in range(100): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 429 | self.assertEqual(next(a), i) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 430 | del a |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 431 | self.assertEqual(list(b), list(range(n))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 432 | |
| 433 | a, b = tee(irange(n)) # test dealloc of trailing iterator |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 434 | for i in range(100): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 435 | self.assertEqual(next(a), i) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 436 | del b |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 437 | self.assertEqual(list(a), list(range(100, n))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 438 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 439 | for j in range(5): # test randomly interleaved |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 440 | order = [0]*n + [1]*n |
| 441 | random.shuffle(order) |
| 442 | lists = ([], []) |
| 443 | its = tee(irange(n)) |
| 444 | for i in order: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 445 | value = next(its[i]) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 446 | lists[i].append(value) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 447 | self.assertEqual(lists[0], list(range(n))) |
| 448 | self.assertEqual(lists[1], list(range(n))) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 449 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 450 | # test argument format checking |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 451 | self.assertRaises(TypeError, tee) |
| 452 | self.assertRaises(TypeError, tee, 3) |
| 453 | self.assertRaises(TypeError, tee, [1,2], 'x') |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 454 | self.assertRaises(TypeError, tee, [1,2], 3, 'x') |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 455 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 456 | # tee object should be instantiable |
| 457 | a, b = tee('abc') |
| 458 | c = type(a)('def') |
| 459 | self.assertEqual(list(c), list('def')) |
Raymond Hettinger | f0c5aec | 2003-10-26 14:25:56 +0000 | [diff] [blame] | 460 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 461 | # test long-lagged and multi-way split |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 462 | a, b, c = tee(range(2000), 3) |
| 463 | for i in range(100): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 464 | self.assertEqual(next(a), i) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 465 | self.assertEqual(list(b), list(range(2000))) |
| 466 | self.assertEqual([next(c), next(c)], list(range(2))) |
| 467 | self.assertEqual(list(a), list(range(100,2000))) |
| 468 | self.assertEqual(list(c), list(range(2,2000))) |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 469 | |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 470 | # test values of n |
| 471 | self.assertRaises(TypeError, tee, 'abc', 'invalid') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 472 | self.assertRaises(ValueError, tee, [], -1) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 473 | for n in range(5): |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 474 | result = tee('abc', n) |
| 475 | self.assertEqual(type(result), tuple) |
| 476 | self.assertEqual(len(result), n) |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 477 | self.assertEqual([list(x) for x in result], [list('abc')]*n) |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 478 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 479 | # tee pass-through to copyable iterator |
| 480 | a, b = tee('abc') |
| 481 | c, d = tee(a) |
| 482 | self.assert_(a is c) |
| 483 | |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 484 | # test tee_new |
| 485 | t1, t2 = tee('abc') |
| 486 | tnew = type(t1) |
| 487 | self.assertRaises(TypeError, tnew) |
| 488 | self.assertRaises(TypeError, tnew, 10) |
| 489 | t3 = tnew(t1) |
| 490 | self.assert_(list(t1) == list(t2) == list(t3) == list('abc')) |
Raymond Hettinger | f0c5aec | 2003-10-26 14:25:56 +0000 | [diff] [blame] | 491 | |
Raymond Hettinger | a9f6092 | 2004-10-17 16:40:14 +0000 | [diff] [blame] | 492 | # test that tee objects are weak referencable |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 493 | a, b = tee(range(10)) |
Raymond Hettinger | a9f6092 | 2004-10-17 16:40:14 +0000 | [diff] [blame] | 494 | p = proxy(a) |
| 495 | self.assertEqual(getattr(p, '__class__'), type(b)) |
| 496 | del a |
| 497 | self.assertRaises(ReferenceError, getattr, p, '__class__') |
| 498 | |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 499 | def test_StopIteration(self): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 500 | self.assertRaises(StopIteration, next, izip()) |
Raymond Hettinger | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame] | 501 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 502 | for f in (chain, cycle, izip, groupby): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 503 | self.assertRaises(StopIteration, next, f([])) |
| 504 | self.assertRaises(StopIteration, next, f(StopNow())) |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 505 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 506 | self.assertRaises(StopIteration, next, islice([], None)) |
| 507 | self.assertRaises(StopIteration, next, islice(StopNow(), None)) |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 508 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 509 | p, q = tee([]) |
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 | p, q = tee(StopNow()) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 513 | self.assertRaises(StopIteration, next, p) |
| 514 | self.assertRaises(StopIteration, next, q) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 515 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 516 | self.assertRaises(StopIteration, next, repeat(None, 0)) |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 517 | |
| 518 | for f in (ifilter, ifilterfalse, imap, takewhile, dropwhile, starmap): |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 519 | self.assertRaises(StopIteration, next, f(lambda x:x, [])) |
| 520 | self.assertRaises(StopIteration, next, f(lambda x:x, StopNow())) |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 521 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 522 | class TestGC(unittest.TestCase): |
| 523 | |
| 524 | def makecycle(self, iterator, container): |
| 525 | container.append(iterator) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 526 | next(iterator) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 527 | del container, iterator |
| 528 | |
| 529 | def test_chain(self): |
| 530 | a = [] |
| 531 | self.makecycle(chain(a), a) |
| 532 | |
| 533 | def test_cycle(self): |
| 534 | a = [] |
| 535 | self.makecycle(cycle([a]*2), a) |
| 536 | |
Raymond Hettinger | ff5dc0e | 2004-09-29 11:40:50 +0000 | [diff] [blame] | 537 | def test_dropwhile(self): |
| 538 | a = [] |
| 539 | self.makecycle(dropwhile(bool, [0, a, a]), a) |
| 540 | |
| 541 | def test_groupby(self): |
| 542 | a = [] |
| 543 | self.makecycle(groupby([a]*2, lambda x:x), a) |
| 544 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 545 | def test_ifilter(self): |
| 546 | a = [] |
| 547 | self.makecycle(ifilter(lambda x:True, [a]*2), a) |
| 548 | |
| 549 | def test_ifilterfalse(self): |
| 550 | a = [] |
| 551 | self.makecycle(ifilterfalse(lambda x:False, a), a) |
| 552 | |
| 553 | def test_izip(self): |
| 554 | a = [] |
| 555 | self.makecycle(izip([a]*2, [a]*3), a) |
| 556 | |
| 557 | def test_imap(self): |
| 558 | a = [] |
| 559 | self.makecycle(imap(lambda x:x, [a]*2), a) |
| 560 | |
| 561 | def test_islice(self): |
| 562 | a = [] |
| 563 | self.makecycle(islice([a]*2, None), a) |
| 564 | |
Raymond Hettinger | ff5dc0e | 2004-09-29 11:40:50 +0000 | [diff] [blame] | 565 | def test_repeat(self): |
| 566 | a = [] |
| 567 | self.makecycle(repeat(a), a) |
| 568 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 569 | def test_starmap(self): |
| 570 | a = [] |
| 571 | self.makecycle(starmap(lambda *t: t, [(a,a)]*2), a) |
| 572 | |
Raymond Hettinger | ff5dc0e | 2004-09-29 11:40:50 +0000 | [diff] [blame] | 573 | def test_takewhile(self): |
| 574 | a = [] |
| 575 | self.makecycle(takewhile(bool, [1, 0, a, a]), a) |
| 576 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 577 | def R(seqn): |
| 578 | 'Regular generator' |
| 579 | for i in seqn: |
| 580 | yield i |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 581 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 582 | class G: |
| 583 | 'Sequence using __getitem__' |
| 584 | def __init__(self, seqn): |
| 585 | self.seqn = seqn |
| 586 | def __getitem__(self, i): |
| 587 | return self.seqn[i] |
| 588 | |
| 589 | class I: |
| 590 | 'Sequence using iterator protocol' |
| 591 | def __init__(self, seqn): |
| 592 | self.seqn = seqn |
| 593 | self.i = 0 |
| 594 | def __iter__(self): |
| 595 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 596 | def __next__(self): |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 597 | if self.i >= len(self.seqn): raise StopIteration |
| 598 | v = self.seqn[self.i] |
| 599 | self.i += 1 |
| 600 | return v |
| 601 | |
| 602 | class Ig: |
| 603 | 'Sequence using iterator protocol defined with a generator' |
| 604 | def __init__(self, seqn): |
| 605 | self.seqn = seqn |
| 606 | self.i = 0 |
| 607 | def __iter__(self): |
| 608 | for val in self.seqn: |
| 609 | yield val |
| 610 | |
| 611 | class X: |
| 612 | 'Missing __getitem__ and __iter__' |
| 613 | def __init__(self, seqn): |
| 614 | self.seqn = seqn |
| 615 | self.i = 0 |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 616 | def __next__(self): |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 617 | if self.i >= len(self.seqn): raise StopIteration |
| 618 | v = self.seqn[self.i] |
| 619 | self.i += 1 |
| 620 | return v |
| 621 | |
| 622 | class N: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 623 | 'Iterator missing __next__()' |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 624 | def __init__(self, seqn): |
| 625 | self.seqn = seqn |
| 626 | self.i = 0 |
| 627 | def __iter__(self): |
| 628 | return self |
| 629 | |
| 630 | class E: |
| 631 | 'Test propagation of exceptions' |
| 632 | def __init__(self, seqn): |
| 633 | self.seqn = seqn |
| 634 | self.i = 0 |
| 635 | def __iter__(self): |
| 636 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 637 | def __next__(self): |
Raymond Hettinger | ffdb8bb | 2004-09-27 15:29:05 +0000 | [diff] [blame] | 638 | 3 // 0 |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 639 | |
| 640 | class S: |
| 641 | 'Test immediate stop' |
| 642 | def __init__(self, seqn): |
| 643 | pass |
| 644 | def __iter__(self): |
| 645 | return self |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 646 | def __next__(self): |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 647 | raise StopIteration |
| 648 | |
| 649 | def L(seqn): |
| 650 | 'Test multiple tiers of iterators' |
| 651 | return chain(imap(lambda x:x, R(Ig(G(seqn))))) |
| 652 | |
| 653 | |
| 654 | class TestVariousIteratorArgs(unittest.TestCase): |
| 655 | |
| 656 | def test_chain(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 657 | 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] | 658 | for g in (G, I, Ig, S, L, R): |
| 659 | self.assertEqual(list(chain(g(s))), list(g(s))) |
| 660 | self.assertEqual(list(chain(g(s), g(s))), list(g(s))+list(g(s))) |
| 661 | self.assertRaises(TypeError, chain, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 662 | self.assertRaises(TypeError, chain, N(s)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 663 | self.assertRaises(ZeroDivisionError, list, chain(E(s))) |
| 664 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 665 | def test_product(self): |
| 666 | for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)): |
| 667 | self.assertRaises(TypeError, product, X(s)) |
| 668 | self.assertRaises(TypeError, product, N(s)) |
| 669 | self.assertRaises(ZeroDivisionError, product, E(s)) |
| 670 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 671 | def test_cycle(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 672 | 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] | 673 | for g in (G, I, Ig, S, L, R): |
| 674 | tgtlen = len(s) * 3 |
| 675 | expected = list(g(s))*3 |
| 676 | actual = list(islice(cycle(g(s)), tgtlen)) |
| 677 | self.assertEqual(actual, expected) |
| 678 | self.assertRaises(TypeError, cycle, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 679 | self.assertRaises(TypeError, cycle, N(s)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 680 | self.assertRaises(ZeroDivisionError, list, cycle(E(s))) |
| 681 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 682 | def test_groupby(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 683 | 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] | 684 | for g in (G, I, Ig, S, L, R): |
| 685 | self.assertEqual([k for k, sb in groupby(g(s))], list(g(s))) |
| 686 | self.assertRaises(TypeError, groupby, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 687 | self.assertRaises(TypeError, groupby, N(s)) |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 688 | self.assertRaises(ZeroDivisionError, list, groupby(E(s))) |
| 689 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 690 | def test_ifilter(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 691 | 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] | 692 | for g in (G, I, Ig, S, L, R): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 693 | self.assertEqual(list(ifilter(isEven, g(s))), |
| 694 | [x for x in g(s) if isEven(x)]) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 695 | self.assertRaises(TypeError, ifilter, isEven, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 696 | self.assertRaises(TypeError, ifilter, isEven, N(s)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 697 | self.assertRaises(ZeroDivisionError, list, ifilter(isEven, E(s))) |
| 698 | |
| 699 | def test_ifilterfalse(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 700 | 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] | 701 | for g in (G, I, Ig, S, L, R): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 702 | self.assertEqual(list(ifilterfalse(isEven, g(s))), |
| 703 | [x for x in g(s) if isOdd(x)]) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 704 | self.assertRaises(TypeError, ifilterfalse, isEven, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 705 | self.assertRaises(TypeError, ifilterfalse, isEven, N(s)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 706 | self.assertRaises(ZeroDivisionError, list, ifilterfalse(isEven, E(s))) |
| 707 | |
| 708 | def test_izip(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 709 | 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] | 710 | for g in (G, I, Ig, S, L, R): |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 711 | self.assertEqual(list(izip(g(s))), lzip(g(s))) |
| 712 | 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] | 713 | self.assertRaises(TypeError, izip, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 714 | self.assertRaises(TypeError, izip, N(s)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 715 | self.assertRaises(ZeroDivisionError, list, izip(E(s))) |
| 716 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 717 | def test_iziplongest(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 718 | 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] | 719 | for g in (G, I, Ig, S, L, R): |
| 720 | self.assertEqual(list(izip_longest(g(s))), list(zip(g(s)))) |
| 721 | self.assertEqual(list(izip_longest(g(s), g(s))), list(zip(g(s), g(s)))) |
| 722 | self.assertRaises(TypeError, izip_longest, X(s)) |
| 723 | self.assertRaises(TypeError, izip_longest, N(s)) |
| 724 | self.assertRaises(ZeroDivisionError, list, izip_longest(E(s))) |
| 725 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 726 | def test_imap(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 727 | 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] | 728 | for g in (G, I, Ig, S, L, R): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 729 | self.assertEqual(list(imap(onearg, g(s))), |
| 730 | [onearg(x) for x in g(s)]) |
| 731 | self.assertEqual(list(imap(operator.pow, g(s), g(s))), |
| 732 | [x**x for x in g(s)]) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 733 | self.assertRaises(TypeError, imap, onearg, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 734 | self.assertRaises(TypeError, imap, onearg, N(s)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 735 | self.assertRaises(ZeroDivisionError, list, imap(onearg, E(s))) |
| 736 | |
| 737 | def test_islice(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 738 | 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] | 739 | for g in (G, I, Ig, S, L, R): |
| 740 | self.assertEqual(list(islice(g(s),1,None,2)), list(g(s))[1::2]) |
| 741 | self.assertRaises(TypeError, islice, X(s), 10) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 742 | self.assertRaises(TypeError, islice, N(s), 10) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 743 | self.assertRaises(ZeroDivisionError, list, islice(E(s), 10)) |
| 744 | |
| 745 | def test_starmap(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 746 | 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] | 747 | for g in (G, I, Ig, S, L, R): |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 748 | ss = lzip(s, s) |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 749 | self.assertEqual(list(starmap(operator.pow, g(ss))), |
| 750 | [x**x for x in g(s)]) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 751 | self.assertRaises(TypeError, starmap, operator.pow, X(ss)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 752 | self.assertRaises(TypeError, starmap, operator.pow, N(ss)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 753 | self.assertRaises(ZeroDivisionError, list, starmap(operator.pow, E(ss))) |
| 754 | |
| 755 | def test_takewhile(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 756 | 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] | 757 | for g in (G, I, Ig, S, L, R): |
| 758 | tgt = [] |
| 759 | for elem in g(s): |
| 760 | if not isEven(elem): break |
| 761 | tgt.append(elem) |
| 762 | self.assertEqual(list(takewhile(isEven, g(s))), tgt) |
| 763 | self.assertRaises(TypeError, takewhile, isEven, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 764 | self.assertRaises(TypeError, takewhile, isEven, N(s)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 765 | self.assertRaises(ZeroDivisionError, list, takewhile(isEven, E(s))) |
| 766 | |
| 767 | def test_dropwhile(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 768 | 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] | 769 | for g in (G, I, Ig, S, L, R): |
| 770 | tgt = [] |
| 771 | for elem in g(s): |
| 772 | if not tgt and isOdd(elem): continue |
| 773 | tgt.append(elem) |
| 774 | self.assertEqual(list(dropwhile(isOdd, g(s))), tgt) |
| 775 | self.assertRaises(TypeError, dropwhile, isOdd, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 776 | self.assertRaises(TypeError, dropwhile, isOdd, N(s)) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 777 | self.assertRaises(ZeroDivisionError, list, dropwhile(isOdd, E(s))) |
| 778 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 779 | def test_tee(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 780 | 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] | 781 | for g in (G, I, Ig, S, L, R): |
| 782 | it1, it2 = tee(g(s)) |
| 783 | self.assertEqual(list(it1), list(g(s))) |
| 784 | self.assertEqual(list(it2), list(g(s))) |
| 785 | self.assertRaises(TypeError, tee, X(s)) |
Thomas Wouters | 8690c4e | 2006-04-15 09:07:20 +0000 | [diff] [blame] | 786 | self.assertRaises(TypeError, tee, N(s)) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 787 | self.assertRaises(ZeroDivisionError, list, tee(E(s))[0]) |
| 788 | |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 789 | class LengthTransparency(unittest.TestCase): |
| 790 | |
| 791 | def test_repeat(self): |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 792 | from test.test_iterlen import len |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 793 | self.assertEqual(len(repeat(None, 50)), 50) |
| 794 | self.assertRaises(TypeError, len, repeat(None)) |
| 795 | |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 796 | class RegressionTests(unittest.TestCase): |
| 797 | |
| 798 | def test_sf_793826(self): |
| 799 | # Fix Armin Rigo's successful efforts to wreak havoc |
| 800 | |
| 801 | def mutatingtuple(tuple1, f, tuple2): |
| 802 | # this builds a tuple t which is a copy of tuple1, |
| 803 | # then calls f(t), then mutates t to be equal to tuple2 |
| 804 | # (needs len(tuple1) == len(tuple2)). |
| 805 | def g(value, first=[1]): |
| 806 | if first: |
| 807 | del first[:] |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 808 | f(next(z)) |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 809 | return value |
| 810 | items = list(tuple2) |
| 811 | items[1:1] = list(tuple1) |
| 812 | gen = imap(g, items) |
| 813 | z = izip(*[gen]*len(tuple1)) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 814 | next(z) |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 815 | |
| 816 | def f(t): |
| 817 | global T |
| 818 | T = t |
| 819 | first[:] = list(T) |
| 820 | |
| 821 | first = [] |
| 822 | mutatingtuple((1,2,3), f, (4,5,6)) |
| 823 | second = list(T) |
| 824 | self.assertEqual(first, second) |
| 825 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 826 | |
Raymond Hettinger | 9d7c870 | 2004-05-08 19:49:42 +0000 | [diff] [blame] | 827 | def test_sf_950057(self): |
| 828 | # Make sure that chain() and cycle() catch exceptions immediately |
| 829 | # rather than when shifting between input sources |
| 830 | |
| 831 | def gen1(): |
| 832 | hist.append(0) |
| 833 | yield 1 |
| 834 | hist.append(1) |
Tim Peters | beb7c0c | 2004-07-18 17:34:03 +0000 | [diff] [blame] | 835 | raise AssertionError |
Raymond Hettinger | 9d7c870 | 2004-05-08 19:49:42 +0000 | [diff] [blame] | 836 | hist.append(2) |
| 837 | |
| 838 | def gen2(x): |
| 839 | hist.append(3) |
| 840 | yield 2 |
| 841 | hist.append(4) |
| 842 | if x: |
| 843 | raise StopIteration |
| 844 | |
| 845 | hist = [] |
| 846 | self.assertRaises(AssertionError, list, chain(gen1(), gen2(False))) |
| 847 | self.assertEqual(hist, [0,1]) |
| 848 | |
| 849 | hist = [] |
| 850 | self.assertRaises(AssertionError, list, chain(gen1(), gen2(True))) |
| 851 | self.assertEqual(hist, [0,1]) |
| 852 | |
| 853 | hist = [] |
| 854 | self.assertRaises(AssertionError, list, cycle(gen1())) |
| 855 | self.assertEqual(hist, [0,1]) |
| 856 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 857 | class SubclassWithKwargsTest(unittest.TestCase): |
| 858 | def test_keywords_in_subclass(self): |
| 859 | # count is not subclassable... |
| 860 | for cls in (repeat, izip, ifilter, ifilterfalse, chain, imap, |
| 861 | starmap, islice, takewhile, dropwhile, cycle): |
| 862 | class Subclass(cls): |
| 863 | def __init__(self, newarg=None, *args): |
| 864 | cls.__init__(self, *args) |
| 865 | try: |
| 866 | Subclass(newarg=1) |
| 867 | except TypeError as err: |
| 868 | # we expect type errors because of wrong argument count |
| 869 | self.failIf("does not take keyword arguments" in err.args[0]) |
| 870 | |
| 871 | |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 872 | libreftest = """ Doctest for examples in the library reference: libitertools.tex |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 873 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 874 | |
| 875 | >>> amounts = [120.15, 764.05, 823.14] |
| 876 | >>> for checknum, amount in izip(count(1200), amounts): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 877 | ... print('Check %d is for $%.2f' % (checknum, amount)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 878 | ... |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 879 | Check 1200 is for $120.15 |
| 880 | Check 1201 is for $764.05 |
| 881 | Check 1202 is for $823.14 |
| 882 | |
| 883 | >>> import operator |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 884 | >>> for cube in imap(operator.pow, range(1,4), repeat(3)): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 885 | ... print(cube) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 886 | ... |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 887 | 1 |
| 888 | 8 |
| 889 | 27 |
| 890 | |
| 891 | >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele'] |
Raymond Hettinger | 3567a87 | 2003-06-28 05:44:36 +0000 | [diff] [blame] | 892 | >>> for name in islice(reportlines, 3, None, 2): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 893 | ... print(name.title()) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 894 | ... |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 895 | Alex |
| 896 | Laura |
| 897 | Martin |
| 898 | Walter |
| 899 | Samuele |
| 900 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 901 | >>> from operator import itemgetter |
| 902 | >>> 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] | 903 | >>> di = sorted(sorted(d.items()), key=itemgetter(1)) |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 904 | >>> for k, g in groupby(di, itemgetter(1)): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 905 | ... print(k, list(map(itemgetter(0), g))) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 906 | ... |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 907 | 1 ['a', 'c', 'e'] |
| 908 | 2 ['b', 'd', 'f'] |
| 909 | 3 ['g'] |
| 910 | |
Raymond Hettinger | 734fb57 | 2004-01-20 20:04:40 +0000 | [diff] [blame] | 911 | # Find runs of consecutive numbers using groupby. The key to the solution |
| 912 | # is differencing with a range so that consecutive numbers all appear in |
| 913 | # same group. |
| 914 | >>> 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] | 915 | >>> 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] | 916 | ... print(list(map(operator.itemgetter(1), g))) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 917 | ... |
Raymond Hettinger | 734fb57 | 2004-01-20 20:04:40 +0000 | [diff] [blame] | 918 | [1] |
| 919 | [4, 5, 6] |
| 920 | [10] |
| 921 | [15, 16, 17, 18] |
| 922 | [22] |
| 923 | [25, 26, 27, 28] |
| 924 | |
Raymond Hettinger | a098b33 | 2003-09-08 23:58:40 +0000 | [diff] [blame] | 925 | >>> def take(n, seq): |
| 926 | ... return list(islice(seq, n)) |
| 927 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 928 | >>> def enumerate(iterable): |
| 929 | ... return izip(count(), iterable) |
| 930 | |
| 931 | >>> def tabulate(function): |
| 932 | ... "Return function(0), function(1), ..." |
| 933 | ... return imap(function, count()) |
| 934 | |
| 935 | >>> def iteritems(mapping): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 936 | ... return izip(mapping.keys(), mapping.values()) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 937 | |
| 938 | >>> def nth(iterable, n): |
| 939 | ... "Returns the nth item" |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 940 | ... return list(islice(iterable, n, n+1)) |
| 941 | |
Raymond Hettinger | f77d033 | 2005-03-11 22:17:30 +0000 | [diff] [blame] | 942 | >>> def all(seq, pred=None): |
| 943 | ... "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] | 944 | ... for elem in ifilterfalse(pred, seq): |
| 945 | ... return False |
| 946 | ... return True |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 947 | |
Raymond Hettinger | f77d033 | 2005-03-11 22:17:30 +0000 | [diff] [blame] | 948 | >>> def any(seq, pred=None): |
| 949 | ... "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] | 950 | ... for elem in ifilter(pred, seq): |
| 951 | ... return True |
| 952 | ... return False |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 953 | |
Raymond Hettinger | f77d033 | 2005-03-11 22:17:30 +0000 | [diff] [blame] | 954 | >>> def no(seq, pred=None): |
| 955 | ... "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] | 956 | ... for elem in ifilter(pred, seq): |
| 957 | ... return False |
| 958 | ... return True |
Raymond Hettinger | 77fe69b | 2003-08-08 04:33:19 +0000 | [diff] [blame] | 959 | |
Raymond Hettinger | f77d033 | 2005-03-11 22:17:30 +0000 | [diff] [blame] | 960 | >>> def quantify(seq, pred=None): |
| 961 | ... "Count how many times the predicate is true in the sequence" |
Raymond Hettinger | 77fe69b | 2003-08-08 04:33:19 +0000 | [diff] [blame] | 962 | ... return sum(imap(pred, seq)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 963 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 964 | >>> def padnone(seq): |
| 965 | ... "Returns the sequence elements and then returns None indefinitely" |
| 966 | ... return chain(seq, repeat(None)) |
| 967 | |
| 968 | >>> def ncycles(seq, n): |
| 969 | ... "Returns the sequence elements n times" |
| 970 | ... return chain(*repeat(seq, n)) |
| 971 | |
| 972 | >>> def dotproduct(vec1, vec2): |
| 973 | ... return sum(imap(operator.mul, vec1, vec2)) |
| 974 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 975 | >>> def flatten(listOfLists): |
| 976 | ... return list(chain(*listOfLists)) |
| 977 | |
| 978 | >>> def repeatfunc(func, times=None, *args): |
| 979 | ... "Repeat calls to func with specified arguments." |
| 980 | ... " Example: repeatfunc(random.random)" |
| 981 | ... if times is None: |
| 982 | ... return starmap(func, repeat(args)) |
| 983 | ... else: |
| 984 | ... return starmap(func, repeat(args, times)) |
| 985 | |
Raymond Hettinger | d591f66 | 2003-10-26 15:34:50 +0000 | [diff] [blame] | 986 | >>> def pairwise(iterable): |
| 987 | ... "s -> (s0,s1), (s1,s2), (s2, s3), ..." |
| 988 | ... a, b = tee(iterable) |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 989 | ... try: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 990 | ... next(b) |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 991 | ... except StopIteration: |
| 992 | ... pass |
| 993 | ... return izip(a, b) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 994 | |
| 995 | This is not part of the examples but it tests to make sure the definitions |
| 996 | perform as purported. |
| 997 | |
Raymond Hettinger | a098b33 | 2003-09-08 23:58:40 +0000 | [diff] [blame] | 998 | >>> take(10, count()) |
| 999 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 1000 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1001 | >>> list(enumerate('abc')) |
| 1002 | [(0, 'a'), (1, 'b'), (2, 'c')] |
| 1003 | |
| 1004 | >>> list(islice(tabulate(lambda x: 2*x), 4)) |
| 1005 | [0, 2, 4, 6] |
| 1006 | |
| 1007 | >>> nth('abcde', 3) |
| 1008 | ['d'] |
| 1009 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 1010 | >>> all([2, 4, 6, 8], lambda x: x%2==0) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1011 | True |
| 1012 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 1013 | >>> all([2, 3, 6, 8], lambda x: x%2==0) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1014 | False |
| 1015 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 1016 | >>> any([2, 4, 6, 8], lambda x: x%2==0) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1017 | True |
| 1018 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 1019 | >>> any([1, 3, 5, 9], lambda x: x%2==0,) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1020 | False |
| 1021 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 1022 | >>> no([1, 3, 5, 9], lambda x: x%2==0) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1023 | True |
| 1024 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 1025 | >>> no([1, 2, 5, 9], lambda x: x%2==0) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1026 | False |
| 1027 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1028 | >>> quantify(range(99), lambda x: x%2==0) |
Raymond Hettinger | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame] | 1029 | 50 |
| 1030 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 1031 | >>> a = [[1, 2, 3], [4, 5, 6]] |
| 1032 | >>> flatten(a) |
| 1033 | [1, 2, 3, 4, 5, 6] |
| 1034 | |
| 1035 | >>> list(repeatfunc(pow, 5, 2, 3)) |
| 1036 | [8, 8, 8, 8, 8] |
| 1037 | |
| 1038 | >>> import random |
| 1039 | >>> take(5, imap(int, repeatfunc(random.random))) |
| 1040 | [0, 0, 0, 0, 0] |
| 1041 | |
Raymond Hettinger | d591f66 | 2003-10-26 15:34:50 +0000 | [diff] [blame] | 1042 | >>> list(pairwise('abcd')) |
| 1043 | [('a', 'b'), ('b', 'c'), ('c', 'd')] |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1044 | |
Raymond Hettinger | d591f66 | 2003-10-26 15:34:50 +0000 | [diff] [blame] | 1045 | >>> list(pairwise([])) |
| 1046 | [] |
| 1047 | |
| 1048 | >>> list(pairwise('a')) |
Raymond Hettinger | befa37d | 2003-06-18 19:25:37 +0000 | [diff] [blame] | 1049 | [] |
| 1050 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1051 | >>> list(islice(padnone('abc'), 0, 6)) |
| 1052 | ['a', 'b', 'c', None, None, None] |
| 1053 | |
| 1054 | >>> list(ncycles('abc', 3)) |
| 1055 | ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] |
| 1056 | |
| 1057 | >>> dotproduct([1,2,3], [4,5,6]) |
| 1058 | 32 |
| 1059 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1060 | """ |
| 1061 | |
| 1062 | __test__ = {'libreftest' : libreftest} |
| 1063 | |
| 1064 | def test_main(verbose=None): |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 1065 | test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC, |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1066 | RegressionTests, LengthTransparency, |
| 1067 | SubclassWithKwargsTest) |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1068 | test_support.run_unittest(*test_classes) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1069 | |
| 1070 | # verify reference counting |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1071 | if verbose and hasattr(sys, "gettotalrefcount"): |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 1072 | import gc |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 1073 | counts = [None] * 5 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1074 | for i in range(len(counts)): |
Raymond Hettinger | f0fa1c0 | 2003-05-29 07:18:57 +0000 | [diff] [blame] | 1075 | test_support.run_unittest(*test_classes) |
Raymond Hettinger | 0242070 | 2003-06-29 20:36:23 +0000 | [diff] [blame] | 1076 | gc.collect() |
Raymond Hettinger | 8fd3f87 | 2003-05-02 22:38:07 +0000 | [diff] [blame] | 1077 | counts[i] = sys.gettotalrefcount() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1078 | print(counts) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1079 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1080 | # doctest the examples in the library reference |
Raymond Hettinger | 929f06c | 2003-05-16 23:16:36 +0000 | [diff] [blame] | 1081 | test_support.run_doctest(sys.modules[__name__], verbose) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1082 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1083 | if __name__ == "__main__": |
| 1084 | test_main(verbose=True) |