1 """Example of Recursion and PyUnit and AssertiveProgramming and FunctionalProgramming
2 """
3
4 import unittest
5
6 def fact(aNum):
7 assert aNum >= 1 and type(aNum) == int
8 if aNum == 1:
9 return 1
10 else:
11 return aNum * fact(aNum-1)
12
13 factFP = lambda n: n<=1 and 1 or factFP(n-1)*n
14
15 class FactorialTest(unittest.TestCase):
16 def test1(self):
17 self.assertEquals(1, fact(1))
18 def test2(self):
19 self.assertEquals(2, fact(2))
20 self.assertEquals(6, fact(3))
21 def testNegativeOrFloat(self):
22 self.assertRaises(AssertionError, fact, -2)
23 self.assertRaises(AssertionError, fact, 1.1)
24 def testOtherTypes(self):
25 self.assertRaises(AssertionError, fact, 'a')
26
27 if __name__=='__main__':
28 unittest.main(argv=('','-v'))