mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Add itertools.accumulate().
This commit is contained in:
parent
2f9a77a389
commit
482ba77245
3 changed files with 179 additions and 0 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue