forked from ivanmilara/gittutorial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
62 lines (56 loc) · 1.53 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import mathtools
def test_isPrime():
fails = 0
print "Testing isPrime"
primes = [2, 3, 5, 7, 983, 991, 997]
not_primes = [4, 6, 8, 9, 993, 994, 995, 999]
for prime in primes:
if mathtools.isPrime(prime):
print '+ ', # Pass the test
else:
fails += 1
print '- ',
for not_prime in not_primes:
if mathtools.isPrime(not_prime):
fails += 1
print '- ',
else:
print '+ ', # Pass the test
if not fails:
print "TEST OK"
else:
print ("FOUND ", fails, " ERRORS")
def test_factorial():
print "Testing factorial "
fails = 0
factorials = [(5, 120), (10, 3628800), (7, 5040), (12, 479001600)]
for f in factorials:
if mathtools.factorial(f[0]) == f[1]:
print '+ ', # Pass the test
else:
fails += 1
print '- ',
if not fails:
print "TEST OK"
else:
print "FOUND ", fails, " ERRORS"
def test_fib():
print "Testing fib "
fails = 0
fib = [(1, 1), (0, 0), (3, 2), (4, 3), (6, 8), (7, 13), (20, 6765)]
for f in fib:
if mathtools.fib(f[0]) == f[1]:
print '+ ', # Pass the test
else:
fails += 1
print '- ',
if not fails:
print "TEST OK"
else:
print "FOUND ", fails, " ERRORS"
functions = ('isPrime', 'factorial', 'fib')
for f in functions:
if hasattr(mathtools, f):
globals()['test_'+f]()
else:
print "*", f, "IS NOT DEFINED"