blob: 12393010f5e4bd7a52759ff35efbec2dc57d0347 [file] [log] [blame]
Raymond Hettinger96ef8112003-02-01 00:10:11 +00001import unittest
2from test import test_support
3from itertools import *
4
5class TestBasicOps(unittest.TestCase):
6 def test_count(self):
7 self.assertEqual(zip('abc',count()), [('a', 0), ('b', 1), ('c', 2)])
8 self.assertEqual(zip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)])
9 self.assertRaises(TypeError, count, 2, 3)
10
11 def test_ifilter(self):
12 def isEven(x):
13 return x%2==0
14 self.assertEqual(list(ifilter(isEven, range(6))), [0,2,4])
15 self.assertEqual(list(ifilter(isEven, range(6), True)), [1,3,5])
16 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)
20 self.assertRaises(TypeError, ifilter, isEven, [3], True, 4)
21
22 def test_izip(self):
23 ans = [(x,y) for x, y in izip('abc',count())]
24 self.assertEqual(ans, [('a', 0), ('b', 1), ('c', 2)])
25 self.assertRaises(TypeError, izip)
26
27 def test_repeat(self):
28 self.assertEqual(zip(xrange(3),repeat('a')),
29 [(0, 'a'), (1, 'a'), (2, 'a')])
30 self.assertRaises(TypeError, repeat)
31
32 def test_times(self):
33 self.assertEqual(list(times(3)), [None]*3)
34 self.assertEqual(list(times(3, True)), [True]*3)
35 self.assertRaises(ValueError, times, -1)
36
37 def test_imap(self):
38 import operator
39 self.assertEqual(list(imap(operator.pow, range(3), range(1,7))),
40 [0**1, 1**2, 2**3])
41 self.assertEqual(list(imap(None, 'abc', range(5))),
42 [('a',0),('b',1),('c',2)])
43 self.assertRaises(TypeError, imap)
44 self.assertRaises(TypeError, imap, operator.neg)
45
46 def test_starmap(self):
47 import operator
48 self.assertEqual(list(starmap(operator.pow, zip(range(3), range(1,7)))),
49 [0**1, 1**2, 2**3])
50
51 def test_islice(self):
52 for args in [ # islice(args) should agree with range(args)
53 (10, 20, 3),
54 (10, 3, 20),
55 (10, 20),
56 (10, 3),
57 (20,)
58 ]:
59 self.assertEqual(list(islice(xrange(100), *args)), range(*args))
60
61 for args, tgtargs in [ # Stop when seqn is exhausted
62 ((10, 110, 3), ((10, 100, 3))),
63 ((10, 110), ((10, 100))),
64 ((110,), (100,))
65 ]:
66 self.assertEqual(list(islice(xrange(100), *args)), range(*tgtargs))
67
68 self.assertRaises(TypeError, islice, xrange(10))
69 self.assertRaises(TypeError, islice, xrange(10), 1, 2, 3, 4)
70 self.assertRaises(ValueError, islice, xrange(10), -5, 10, 1)
71 self.assertRaises(ValueError, islice, xrange(10), 1, -5, -1)
72 self.assertRaises(ValueError, islice, xrange(10), 1, 10, -1)
73 self.assertRaises(ValueError, islice, xrange(10), 1, 10, 0)
74
75 def test_takewhile(self):
76 data = [1, 3, 5, 20, 2, 4, 6, 8]
77 underten = lambda x: x<10
78 self.assertEqual(list(takewhile(underten, data)), [1, 3, 5])
79
80 def test_dropwhile(self):
81 data = [1, 3, 5, 20, 2, 4, 6, 8]
82 underten = lambda x: x<10
83 self.assertEqual(list(dropwhile(underten, data)), [20, 2, 4, 6, 8])
84
85libreftest = """ Doctest for examples in the library reference, libitertools.tex
86
87>>> for i in times(3):
88... print "Hello"
89...
90Hello
91Hello
92Hello
93
94>>> amounts = [120.15, 764.05, 823.14]
95>>> for checknum, amount in izip(count(1200), amounts):
96... print 'Check %d is for $%.2f' % (checknum, amount)
97...
98Check 1200 is for $120.15
99Check 1201 is for $764.05
100Check 1202 is for $823.14
101
102>>> import operator
103>>> import operator
104>>> for cube in imap(operator.pow, xrange(1,4), repeat(3)):
105... print cube
106...
1071
1088
10927
110
111>>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele']
112>>> for name in islice(reportlines, 3, len(reportlines), 2):
113... print name.title()
114...
115Alex
116Laura
117Martin
118Walter
119Samuele
120
121>>> def enumerate(iterable):
122... return izip(count(), iterable)
123
124>>> def tabulate(function):
125... "Return function(0), function(1), ..."
126... return imap(function, count())
127
128>>> def iteritems(mapping):
129... return izip(mapping.iterkeys(), mapping.itervalues())
130
131>>> def nth(iterable, n):
132... "Returns the nth item"
133... return islice(iterable, n, n+1).next()
134
135"""
136
137__test__ = {'libreftest' : libreftest}
138
139def test_main(verbose=None):
140 import test_itertools
141 suite = unittest.TestSuite()
142 for testclass in (TestBasicOps,
143 ):
144 suite.addTest(unittest.makeSuite(testclass))
145 test_support.run_suite(suite)
146 test_support.run_doctest(test_itertools, verbose)
147
148 # verify reference counting
149 import sys
150 if verbose and hasattr(sys, "gettotalrefcount"):
151 counts = []
152 for i in xrange(5):
153 test_support.run_suite(suite)
Raymond Hettinger874d9bc2003-02-01 02:33:45 +0000154 counts.append(sys.gettotalrefcount()-i)
Raymond Hettinger96ef8112003-02-01 00:10:11 +0000155 print counts
156
157if __name__ == "__main__":
158 test_main(verbose=True)