Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
test.test_functools.TestReduce Class Reference

List of all members.

Public Member Functions

def test_reduce
def test_iterator_usage

Public Attributes

 max
 sofar
 n

Static Public Attributes

 func = functools.reduce

Detailed Description

Definition at line 358 of file test_functools.py.


Member Function Documentation

Definition at line 417 of file test_functools.py.

00417 
00418     def test_iterator_usage(self):
00419         class SequenceClass:
00420             def __init__(self, n):
00421                 self.n = n
00422             def __getitem__(self, i):
00423                 if 0 <= i < self.n:
00424                     return i
00425                 else:
00426                     raise IndexError
00427 
00428         from operator import add
00429         self.assertEqual(self.func(add, SequenceClass(5)), 10)
00430         self.assertEqual(self.func(add, SequenceClass(5), 42), 52)
00431         self.assertRaises(TypeError, self.func, add, SequenceClass(0))
00432         self.assertEqual(self.func(add, SequenceClass(0), 42), 42)
00433         self.assertEqual(self.func(add, SequenceClass(1)), 0)
00434         self.assertEqual(self.func(add, SequenceClass(1), 42), 42)
00435 
00436         d = {"one": 1, "two": 2, "three": 3}
00437         self.assertEqual(self.func(add, d), "".join(d.keys()))

Here is the call graph for this function:

Definition at line 361 of file test_functools.py.

00361 
00362     def test_reduce(self):
00363         class Squares:
00364             def __init__(self, max):
00365                 self.max = max
00366                 self.sofar = []
00367 
00368             def __len__(self):
00369                 return len(self.sofar)
00370 
00371             def __getitem__(self, i):
00372                 if not 0 <= i < self.max: raise IndexError
00373                 n = len(self.sofar)
00374                 while n <= i:
00375                     self.sofar.append(n*n)
00376                     n += 1
00377                 return self.sofar[i]
00378         def add(x, y):
00379             return x + y
00380         self.assertEqual(self.func(add, ['a', 'b', 'c'], ''), 'abc')
00381         self.assertEqual(
00382             self.func(add, [['a', 'c'], [], ['d', 'w']], []),
00383             ['a','c','d','w']
00384         )
00385         self.assertEqual(self.func(lambda x, y: x*y, range(2,8), 1), 5040)
00386         self.assertEqual(
00387             self.func(lambda x, y: x*y, range(2,21), 1),
00388             2432902008176640000
00389         )
00390         self.assertEqual(self.func(add, Squares(10)), 285)
00391         self.assertEqual(self.func(add, Squares(10), 0), 285)
00392         self.assertEqual(self.func(add, Squares(0), 0), 0)
00393         self.assertRaises(TypeError, self.func)
00394         self.assertRaises(TypeError, self.func, 42, 42)
00395         self.assertRaises(TypeError, self.func, 42, 42, 42)
00396         self.assertEqual(self.func(42, "1"), "1") # func is never called with one item
00397         self.assertEqual(self.func(42, "", "1"), "1") # func is never called with one item
00398         self.assertRaises(TypeError, self.func, 42, (42, 42))
00399         self.assertRaises(TypeError, self.func, add, []) # arg 2 must not be empty sequence with no initial value
00400         self.assertRaises(TypeError, self.func, add, "")
00401         self.assertRaises(TypeError, self.func, add, ())
00402         self.assertRaises(TypeError, self.func, add, object())
00403 
00404         class TestFailingIter:
00405             def __iter__(self):
00406                 raise RuntimeError
00407         self.assertRaises(RuntimeError, self.func, add, TestFailingIter())
00408 
00409         self.assertEqual(self.func(add, [], None), None)
00410         self.assertEqual(self.func(add, [], 42), 42)
00411 
00412         class BadSeq:
00413             def __getitem__(self, index):
00414                 raise ValueError
00415         self.assertRaises(ValueError, self.func, 42, BadSeq())

Here is the call graph for this function:


Member Data Documentation

Definition at line 359 of file test_functools.py.

Definition at line 364 of file test_functools.py.

Definition at line 420 of file test_functools.py.

Definition at line 365 of file test_functools.py.


The documentation for this class was generated from the following file: