Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
| 3 | from itertools import * |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 4 | import sys |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 5 | |
| 6 | class TestBasicOps(unittest.TestCase): |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 7 | def test_chain(self): |
| 8 | self.assertEqual(list(chain('abc', 'def')), list('abcdef')) |
| 9 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 10 | def test_count(self): |
| 11 | self.assertEqual(zip('abc',count()), [('a', 0), ('b', 1), ('c', 2)]) |
| 12 | self.assertEqual(zip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)]) |
| 13 | self.assertRaises(TypeError, count, 2, 3) |
| 14 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 15 | def test_cycle(self): |
| 16 | self.assertEqual(list(islice(cycle('abc'),10)), list('abcabcabca')) |
| 17 | self.assertEqual(list(cycle('')), []) |
| 18 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 19 | def test_ifilter(self): |
| 20 | def isEven(x): |
| 21 | return x%2==0 |
| 22 | self.assertEqual(list(ifilter(isEven, range(6))), [0,2,4]) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 23 | self.assertEqual(list(ifilter(None, [0,1,0,2,0])), [1,2]) |
| 24 | self.assertRaises(TypeError, ifilter) |
| 25 | self.assertRaises(TypeError, ifilter, 3) |
| 26 | self.assertRaises(TypeError, ifilter, isEven, 3) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 27 | |
| 28 | def test_ifilterfalse(self): |
| 29 | def isEven(x): |
| 30 | return x%2==0 |
| 31 | self.assertEqual(list(ifilterfalse(isEven, range(6))), [1,3,5]) |
| 32 | self.assertEqual(list(ifilterfalse(None, [0,1,0,2,0])), [0,0,0]) |
| 33 | self.assertRaises(TypeError, ifilterfalse) |
| 34 | self.assertRaises(TypeError, ifilterfalse, 3) |
| 35 | self.assertRaises(TypeError, ifilterfalse, isEven, 3) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 36 | |
| 37 | def test_izip(self): |
| 38 | ans = [(x,y) for x, y in izip('abc',count())] |
| 39 | self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)]) |
| 40 | self.assertRaises(TypeError, izip) |
| 41 | |
| 42 | def test_repeat(self): |
| 43 | self.assertEqual(zip(xrange(3),repeat('a')), |
| 44 | [(0, 'a'), (1, 'a'), (2, 'a')]) |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 45 | self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a']) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 46 | self.assertRaises(TypeError, repeat) |
| 47 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 48 | def test_imap(self): |
| 49 | import operator |
| 50 | self.assertEqual(list(imap(operator.pow, range(3), range(1,7))), |
| 51 | [0**1, 1**2, 2**3]) |
| 52 | self.assertEqual(list(imap(None, 'abc', range(5))), |
| 53 | [('a',0),('b',1),('c',2)]) |
| 54 | self.assertRaises(TypeError, imap) |
| 55 | self.assertRaises(TypeError, imap, operator.neg) |
| 56 | |
| 57 | def test_starmap(self): |
| 58 | import operator |
| 59 | self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))), |
| 60 | [0**1, 1**2, 2**3]) |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 61 | self.assertRaises(TypeError, list, starmap(operator.pow, [[4,5]])) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 62 | |
| 63 | def test_islice(self): |
| 64 | for args in [ # islice(args) should agree with range(args) |
| 65 | (10, 20, 3), |
| 66 | (10, 3, 20), |
| 67 | (10, 20), |
| 68 | (10, 3), |
| 69 | (20,) |
| 70 | ]: |
| 71 | self.assertEqual(list(islice(xrange(100), *args)), range(*args)) |
| 72 | |
| 73 | for args, tgtargs in [ # Stop when seqn is exhausted |
| 74 | ((10, 110, 3), ((10, 100, 3))), |
| 75 | ((10, 110), ((10, 100))), |
| 76 | ((110,), (100,)) |
| 77 | ]: |
| 78 | self.assertEqual(list(islice(xrange(100), *args)), range(*tgtargs)) |
| 79 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame^] | 80 | # Test stop=None |
| 81 | self.assertEqual(list(islice(xrange(10))), range(10)) |
| 82 | self.assertEqual(list(islice(xrange(10), None)), range(10)) |
| 83 | self.assertEqual(list(islice(xrange(10), 2, None)), range(2, 10)) |
| 84 | self.assertEqual(list(islice(xrange(10), 1, None, 2)), range(1, 10, 2)) |
| 85 | |
| 86 | # Test invalid arguments |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 87 | self.assertRaises(TypeError, islice, xrange(10), 1, 2, 3, 4) |
| 88 | self.assertRaises(ValueError, islice, xrange(10), -5, 10, 1) |
| 89 | self.assertRaises(ValueError, islice, xrange(10), 1, -5, -1) |
| 90 | self.assertRaises(ValueError, islice, xrange(10), 1, 10, -1) |
| 91 | self.assertRaises(ValueError, islice, xrange(10), 1, 10, 0) |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame^] | 92 | self.assertRaises(ValueError, islice, xrange(10), 'a') |
| 93 | self.assertRaises(ValueError, islice, xrange(10), 'a', 1) |
| 94 | self.assertRaises(ValueError, islice, xrange(10), 1, 'a') |
| 95 | self.assertRaises(ValueError, islice, xrange(10), 'a', 1, 1) |
| 96 | self.assertRaises(ValueError, islice, xrange(10), 1, 'a', 1) |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 97 | self.assertEqual(len(list(islice(count(), 1, 10, sys.maxint))), 1) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 98 | |
| 99 | def test_takewhile(self): |
| 100 | data = [1, 3, 5, 20, 2, 4, 6, 8] |
| 101 | underten = lambda x: x<10 |
| 102 | self.assertEqual(list(takewhile(underten, data)), [1, 3, 5]) |
| 103 | |
| 104 | def test_dropwhile(self): |
| 105 | data = [1, 3, 5, 20, 2, 4, 6, 8] |
| 106 | underten = lambda x: x<10 |
| 107 | self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8]) |
| 108 | |
| 109 | libreftest = """ Doctest for examples in the library reference, libitertools.tex |
| 110 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 111 | |
| 112 | >>> amounts = [120.15, 764.05, 823.14] |
| 113 | >>> for checknum, amount in izip(count(1200), amounts): |
| 114 | ... print 'Check %d is for $%.2f' % (checknum, amount) |
| 115 | ... |
| 116 | Check 1200 is for $120.15 |
| 117 | Check 1201 is for $764.05 |
| 118 | Check 1202 is for $823.14 |
| 119 | |
| 120 | >>> import operator |
| 121 | >>> import operator |
| 122 | >>> for cube in imap(operator.pow, xrange(1,4), repeat(3)): |
| 123 | ... print cube |
| 124 | ... |
| 125 | 1 |
| 126 | 8 |
| 127 | 27 |
| 128 | |
| 129 | >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele'] |
| 130 | >>> for name in islice(reportlines, 3, len(reportlines), 2): |
| 131 | ... print name.title() |
| 132 | ... |
| 133 | Alex |
| 134 | Laura |
| 135 | Martin |
| 136 | Walter |
| 137 | Samuele |
| 138 | |
| 139 | >>> def enumerate(iterable): |
| 140 | ... return izip(count(), iterable) |
| 141 | |
| 142 | >>> def tabulate(function): |
| 143 | ... "Return function(0), function(1), ..." |
| 144 | ... return imap(function, count()) |
| 145 | |
| 146 | >>> def iteritems(mapping): |
| 147 | ... return izip(mapping.iterkeys(), mapping.itervalues()) |
| 148 | |
| 149 | >>> def nth(iterable, n): |
| 150 | ... "Returns the nth item" |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 151 | ... return list(islice(iterable, n, n+1)) |
| 152 | |
| 153 | >>> def all(pred, seq): |
| 154 | ... "Returns True if pred(x) is True for every element in the iterable" |
| 155 | ... return not nth(ifilterfalse(pred, seq), 0) |
| 156 | |
| 157 | >>> def some(pred, seq): |
| 158 | ... "Returns True if pred(x) is True at least one element in the iterable" |
| 159 | ... return bool(nth(ifilter(pred, seq), 0)) |
| 160 | |
| 161 | >>> def no(pred, seq): |
| 162 | ... "Returns True if pred(x) is False for every element in the iterable" |
| 163 | ... return not nth(ifilter(pred, seq), 0) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 164 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 165 | >>> def pairwise(seq): |
| 166 | ... "s -> (s0,s1), (s1,s2), (s2, s3), ..." |
| 167 | ... return izip(seq, islice(seq,1,len(seq))) |
| 168 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame^] | 169 | >>> def padnone(seq): |
| 170 | ... "Returns the sequence elements and then returns None indefinitely" |
| 171 | ... return chain(seq, repeat(None)) |
| 172 | |
| 173 | >>> def ncycles(seq, n): |
| 174 | ... "Returns the sequence elements n times" |
| 175 | ... return chain(*repeat(seq, n)) |
| 176 | |
| 177 | >>> def dotproduct(vec1, vec2): |
| 178 | ... return sum(imap(operator.mul, vec1, vec2)) |
| 179 | |
| 180 | |
| 181 | This is not part of the examples but it tests to make sure the definitions |
| 182 | perform as purported. |
| 183 | |
| 184 | >>> list(enumerate('abc')) |
| 185 | [(0, 'a'), (1, 'b'), (2, 'c')] |
| 186 | |
| 187 | >>> list(islice(tabulate(lambda x: 2*x), 4)) |
| 188 | [0, 2, 4, 6] |
| 189 | |
| 190 | >>> nth('abcde', 3) |
| 191 | ['d'] |
| 192 | |
| 193 | >>> all(lambda x: x%2==0, [2, 4, 6, 8]) |
| 194 | True |
| 195 | |
| 196 | >>> all(lambda x: x%2==0, [2, 3, 6, 8]) |
| 197 | False |
| 198 | |
| 199 | >>> some(lambda x: x%2==0, [2, 4, 6, 8]) |
| 200 | True |
| 201 | |
| 202 | >>> some(lambda x: x%2==0, [1, 3, 5, 9]) |
| 203 | False |
| 204 | |
| 205 | >>> no(lambda x: x%2==0, [1, 3, 5, 9]) |
| 206 | True |
| 207 | |
| 208 | >>> no(lambda x: x%2==0, [1, 2, 5, 9]) |
| 209 | False |
| 210 | |
| 211 | >>> list(pairwise('abc')) |
| 212 | [('a', 'b'), ('b', 'c')] |
| 213 | |
| 214 | >>> list(islice(padnone('abc'), 0, 6)) |
| 215 | ['a', 'b', 'c', None, None, None] |
| 216 | |
| 217 | >>> list(ncycles('abc', 3)) |
| 218 | ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'] |
| 219 | |
| 220 | >>> dotproduct([1,2,3], [4,5,6]) |
| 221 | 32 |
| 222 | |
| 223 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 224 | """ |
| 225 | |
| 226 | __test__ = {'libreftest' : libreftest} |
| 227 | |
| 228 | def test_main(verbose=None): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 229 | suite = unittest.TestSuite() |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 230 | suite.addTest(unittest.makeSuite(TestBasicOps)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 231 | test_support.run_suite(suite) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 232 | |
| 233 | # verify reference counting |
| 234 | import sys |
| 235 | if verbose and hasattr(sys, "gettotalrefcount"): |
| 236 | counts = [] |
| 237 | for i in xrange(5): |
| 238 | test_support.run_suite(suite) |
Raymond Hettinger | 874d9bc | 2003-02-01 02:33:45 +0000 | [diff] [blame] | 239 | counts.append(sys.gettotalrefcount()-i) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 240 | print counts |
| 241 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame^] | 242 | # doctest the examples in the library reference |
| 243 | import doctest |
| 244 | doctest.testmod(sys.modules[__name__]) |
| 245 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 246 | if __name__ == "__main__": |
| 247 | test_main(verbose=True) |