blob: 09b7d1320bdea5cda7fc522595840bb3ca4962e3 [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):
7 def test_count(self):
8 self.assertEqual(zip('abc',count()), [('a', 0), ('b', 1), ('c', 2)])
9 self.assertEqual(zip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)])
10 self.assertRaises(TypeError, count, 2, 3)
11
12 def test_ifilter(self):
13 def isEven(x):
14 return x%2==0
15 self.assertEqual(list(ifilter(isEven, range(6))), [0,2,4])
Raymond Hettinger96ef8112003-02-01 00:10:11 +000016 self.assertEqual(list(ifilter(None, [0,1,0,2,0])), [1,2])
17 self.assertRaises(TypeError, ifilter)
18 self.assertRaises(TypeError, ifilter, 3)
19 self.assertRaises(TypeError, ifilter, isEven, 3)
Raymond Hettinger60eca932003-02-09 06:40:58 +000020
21 def test_ifilterfalse(self):
22 def isEven(x):
23 return x%2==0
24 self.assertEqual(list(ifilterfalse(isEven, range(6))), [1,3,5])
25 self.assertEqual(list(ifilterfalse(None, [0,1,0,2,0])), [0,0,0])
26 self.assertRaises(TypeError, ifilterfalse)
27 self.assertRaises(TypeError, ifilterfalse, 3)
28 self.assertRaises(TypeError, ifilterfalse, isEven, 3)
Raymond Hettinger96ef8112003-02-01 00:10:11 +000029
30 def test_izip(self):
31 ans = [(x,y) for x, y in izip('abc',count())]
32 self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
33 self.assertRaises(TypeError, izip)
34
35 def test_repeat(self):
36 self.assertEqual(zip(xrange(3),repeat('a')),
37 [(0, 'a'), (1, 'a'), (2, 'a')])
38 self.assertRaises(TypeError, repeat)
39
40 def test_times(self):
41 self.assertEqual(list(times(3)), [None]*3)
42 self.assertEqual(list(times(3, True)), [True]*3)
43 self.assertRaises(ValueError, times, -1)
44
45 def test_imap(self):
46 import operator
47 self.assertEqual(list(imap(operator.pow, range(3), range(1,7))),
48 [0**1, 1**2, 2**3])
49 self.assertEqual(list(imap(None, 'abc', range(5))),
50 [('a',0),('b',1),('c',2)])
51 self.assertRaises(TypeError, imap)
52 self.assertRaises(TypeError, imap, operator.neg)
53
54 def test_starmap(self):
55 import operator
56 self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))),
57 [0**1, 1**2, 2**3])
Raymond Hettinger2012f172003-02-07 05:32:58 +000058 self.assertRaises(TypeError, list, starmap(operator.pow, [[4,5]]))
Raymond Hettinger96ef8112003-02-01 00:10:11 +000059
60 def test_islice(self):
61 for args in [ # islice(args) should agree with range(args)
62 (10, 20, 3),
63 (10, 3, 20),
64 (10, 20),
65 (10, 3),
66 (20,)
67 ]:
68 self.assertEqual(list(islice(xrange(100), *args)), range(*args))
69
70 for args, tgtargs in [ # Stop when seqn is exhausted
71 ((10, 110, 3), ((10, 100, 3))),
72 ((10, 110), ((10, 100))),
73 ((110,), (100,))
74 ]:
75 self.assertEqual(list(islice(xrange(100), *args)), range(*tgtargs))
76
77 self.assertRaises(TypeError, islice, xrange(10))
78 self.assertRaises(TypeError, islice, xrange(10), 1, 2, 3, 4)
79 self.assertRaises(ValueError, islice, xrange(10), -5, 10, 1)
80 self.assertRaises(ValueError, islice, xrange(10), 1, -5, -1)
81 self.assertRaises(ValueError, islice, xrange(10), 1, 10, -1)
82 self.assertRaises(ValueError, islice, xrange(10), 1, 10, 0)
Raymond Hettinger2012f172003-02-07 05:32:58 +000083 self.assertEqual(len(list(islice(count(), 1, 10, sys.maxint))), 1)
Raymond Hettinger96ef8112003-02-01 00:10:11 +000084
85 def test_takewhile(self):
86 data = [1, 3, 5, 20, 2, 4, 6, 8]
87 underten = lambda x: x<10
88 self.assertEqual(list(takewhile(underten, data)), [1, 3, 5])
89
90 def test_dropwhile(self):
91 data = [1, 3, 5, 20, 2, 4, 6, 8]
92 underten = lambda x: x<10
93 self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8])
94
95libreftest = """ Doctest for examples in the library reference, libitertools.tex
96
97>>> for i in times(3):
98... print "Hello"
99...
100Hello
101Hello
102Hello
103
104>>> amounts = [120.15, 764.05, 823.14]
105>>> for checknum, amount in izip(count(1200), amounts):
106... print 'Check %d is for $%.2f' % (checknum, amount)
107...
108Check 1200 is for $120.15
109Check 1201 is for $764.05
110Check 1202 is for $823.14
111
112>>> import operator
113>>> import operator
114>>> for cube in imap(operator.pow, xrange(1,4), repeat(3)):
115... print cube
116...
1171
1188
11927
120
121>>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele']
122>>> for name in islice(reportlines, 3, len(reportlines), 2):
123... print name.title()
124...
125Alex
126Laura
127Martin
128Walter
129Samuele
130
131>>> def enumerate(iterable):
132... return izip(count(), iterable)
133
134>>> def tabulate(function):
135... "Return function(0), function(1), ..."
136... return imap(function, count())
137
138>>> def iteritems(mapping):
139... return izip(mapping.iterkeys(), mapping.itervalues())
140
141>>> def nth(iterable, n):
142... "Returns the nth item"
Raymond Hettinger60eca932003-02-09 06:40:58 +0000143... return list(islice(iterable, n, n+1))
144
145>>> def all(pred, seq):
146... "Returns True if pred(x) is True for every element in the iterable"
147... return not nth(ifilterfalse(pred, seq), 0)
148
149>>> def some(pred, seq):
150... "Returns True if pred(x) is True at least one element in the iterable"
151... return bool(nth(ifilter(pred, seq), 0))
152
153>>> def no(pred, seq):
154... "Returns True if pred(x) is False for every element in the iterable"
155... return not nth(ifilter(pred, seq), 0)
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000156
157"""
158
159__test__ = {'libreftest' : libreftest}
160
161def test_main(verbose=None):
162 import test_itertools
163 suite = unittest.TestSuite()
164 for testclass in (TestBasicOps,
165 ):
166 suite.addTest(unittest.makeSuite(testclass))
167 test_support.run_suite(suite)
168 test_support.run_doctest(test_itertools, verbose)
169
170 # verify reference counting
171 import sys
172 if verbose and hasattr(sys, "gettotalrefcount"):
173 counts = []
174 for i in xrange(5):
175 test_support.run_suite(suite)
Raymond Hettinger874d9bc2003-02-01 02:33:45 +0000176 counts.append(sys.gettotalrefcount()-i)
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000177 print counts
178
179if __name__ == "__main__":
180 test_main(verbose=True)