summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_itertools.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-12-01 22:48:00 (GMT)
committerRaymond Hettinger <python@rcn.com>2010-12-01 22:48:00 (GMT)
commit482ba772456259633fa8f0433b8b4220c55c9cab (patch)
tree7c365089bd3189f8a211f88673622ebfd6cc2def /Lib/test/test_itertools.py
parent2f9a77a389c4182d4960b8b143c4c456a16ea5f3 (diff)
downloadcpython-482ba772456259633fa8f0433b8b4220c55c9cab.zip
cpython-482ba772456259633fa8f0433b8b4220c55c9cab.tar.gz
cpython-482ba772456259633fa8f0433b8b4220c55c9cab.tar.bz2
Add itertools.accumulate().
Diffstat (limited to 'Lib/test/test_itertools.py')
-rw-r--r--Lib/test/test_itertools.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index fe6131a..8a67cff 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -56,6 +56,23 @@ def fact(n):
return prod(range(1, n+1))
class TestBasicOps(unittest.TestCase):
+
+ def test_accumulate(self):
+ self.assertEqual(list(accumulate(range(10))), # one positional arg
+ [0, 1, 3, 6, 10, 15, 21, 28, 36, 45])
+ self.assertEqual(list(accumulate(range(10), 100)), # two positional args
+ [100, 101, 103, 106, 110, 115, 121, 128, 136, 145])
+ self.assertEqual(list(accumulate(iterable=range(10), start=100)), # kw args
+ [100, 101, 103, 106, 110, 115, 121, 128, 136, 145])
+ for typ in int, complex, Decimal, Fraction: # multiple types
+ self.assertEqual(list(accumulate(range(10), typ(0))),
+ list(map(typ, [0, 1, 3, 6, 10, 15, 21, 28, 36, 45])))
+ self.assertEqual(list(accumulate([])), []) # empty iterable
+ self.assertRaises(TypeError, accumulate, range(10), 0, 5) # too many args
+ self.assertRaises(TypeError, accumulate) # too few args
+ self.assertRaises(TypeError, accumulate, range(10), x=7) # unexpected kwd args
+ self.assertRaises(TypeError, list, accumulate([1, []])) # args that don't add
+
def test_chain(self):
def chain2(*iterables):
@@ -932,6 +949,9 @@ class TestBasicOps(unittest.TestCase):
class TestExamples(unittest.TestCase):
+ def test_accumlate(self):
+ self.assertEqual(list(accumulate([1,2,3,4,5])), [1, 3, 6, 10, 15])
+
def test_chain(self):
self.assertEqual(''.join(chain('ABC', 'DEF')), 'ABCDEF')
@@ -1019,6 +1039,10 @@ class TestGC(unittest.TestCase):
next(iterator)
del container, iterator
+ def test_accumulate(self):
+ a = []
+ self.makecycle(accumulate([1,2,a,3]), a)
+
def test_chain(self):
a = []
self.makecycle(chain(a), a)
@@ -1188,6 +1212,17 @@ def L(seqn):
class TestVariousIteratorArgs(unittest.TestCase):
+ def test_accumulate(self):
+ s = [1,2,3,4,5]
+ r = [1,3,6,10,15]
+ n = len(s)
+ for g in (G, I, Ig, L, R):
+ self.assertEqual(list(accumulate(g(s))), r)
+ self.assertEqual(list(accumulate(S(s))), [])
+ self.assertRaises(TypeError, accumulate, X(s))
+ self.assertRaises(TypeError, accumulate, N(s))
+ self.assertRaises(ZeroDivisionError, list, accumulate(E(s)))
+
def test_chain(self):
for s in ("123", "", range(1000), ('do', 1.2), range(2000,2200,5)):
for g in (G, I, Ig, S, L, R):