blob: d0b1ce80b9ed2c43ab70422cbd7f21fc78208cc8 [file] [log] [blame]
Raymond Hettinger96ef8112003-02-01 00:10:11 +00001import unittest
2from test import test_support
3from itertools import *
Raymond Hettinger2012f172003-02-07 05:32:58 +00004import sys
Raymond Hettinger96ef8112003-02-01 00:10:11 +00005
6class TestBasicOps(unittest.TestCase):
Raymond Hettinger61fe64d2003-02-23 04:40:07 +00007 def test_chain(self):
8 self.assertEqual(list(chain('abc', 'def')), list('abcdef'))
9
Raymond Hettinger96ef8112003-02-01 00:10:11 +000010 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 Hettinger61fe64d2003-02-23 04:40:07 +000015 def test_cycle(self):
16 self.assertEqual(list(islice(cycle('abc'),10)), list('abcabcabca'))
17 self.assertEqual(list(cycle('')), [])
18
Raymond Hettinger96ef8112003-02-01 00:10:11 +000019 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 Hettinger96ef8112003-02-01 00:10:11 +000023 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 Hettinger60eca932003-02-09 06:40:58 +000027
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 Hettinger96ef8112003-02-01 00:10:11 +000036
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 Hettinger61fe64d2003-02-23 04:40:07 +000045 self.assertEqual(list(repeat('a', 3)), ['a', 'a', 'a'])
Raymond Hettinger96ef8112003-02-01 00:10:11 +000046 self.assertRaises(TypeError, repeat)
47
Raymond Hettinger96ef8112003-02-01 00:10:11 +000048 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 Hettinger2012f172003-02-07 05:32:58 +000061 self.assertRaises(TypeError, list, starmap(operator.pow, [[4,5]]))
Raymond Hettinger96ef8112003-02-01 00:10:11 +000062
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 Hettinger14ef54c2003-05-02 19:04:37 +000080 # 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 Hettinger96ef8112003-02-01 00:10:11 +000087 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 Hettinger14ef54c2003-05-02 19:04:37 +000092 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 Hettinger2012f172003-02-07 05:32:58 +000097 self.assertEqual(len(list(islice(count(), 1, 10, sys.maxint))), 1)
Raymond Hettinger96ef8112003-02-01 00:10:11 +000098
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
109libreftest = """ Doctest for examples in the library reference, libitertools.tex
110
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000111
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...
116Check 1200 is for $120.15
117Check 1201 is for $764.05
118Check 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...
1251
1268
12727
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...
133Alex
134Laura
135Martin
136Walter
137Samuele
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 Hettinger60eca932003-02-09 06:40:58 +0000151... 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 Hettinger96ef8112003-02-01 00:10:11 +0000164
Raymond Hettinger61fe64d2003-02-23 04:40:07 +0000165>>> def pairwise(seq):
166... "s -> (s0,s1), (s1,s2), (s2, s3), ..."
167... return izip(seq, islice(seq,1,len(seq)))
168
Raymond Hettinger14ef54c2003-05-02 19:04:37 +0000169>>> 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
181This is not part of the examples but it tests to make sure the definitions
182perform 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])
194True
195
196>>> all(lambda x: x%2==0, [2, 3, 6, 8])
197False
198
199>>> some(lambda x: x%2==0, [2, 4, 6, 8])
200True
201
202>>> some(lambda x: x%2==0, [1, 3, 5, 9])
203False
204
205>>> no(lambda x: x%2==0, [1, 3, 5, 9])
206True
207
208>>> no(lambda x: x%2==0, [1, 2, 5, 9])
209False
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])
22132
222
223
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000224"""
225
226__test__ = {'libreftest' : libreftest}
227
228def test_main(verbose=None):
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000229 suite = unittest.TestSuite()
Walter Dörwald21d3a322003-05-01 17:45:56 +0000230 suite.addTest(unittest.makeSuite(TestBasicOps))
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000231 test_support.run_suite(suite)
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000232
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 Hettinger874d9bc2003-02-01 02:33:45 +0000239 counts.append(sys.gettotalrefcount()-i)
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000240 print counts
241
Raymond Hettinger14ef54c2003-05-02 19:04:37 +0000242 # doctest the examples in the library reference
243 import doctest
244 doctest.testmod(sys.modules[__name__])
245
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000246if __name__ == "__main__":
247 test_main(verbose=True)