-
Notifications
You must be signed in to change notification settings - Fork 0
/
chukwudi-TestCollatz.py
executable file
·121 lines (97 loc) · 2.76 KB
/
chukwudi-TestCollatz.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
# -------------------------------
# projects/collatz/TestCollatz.py
# Copyright (C) 2014
# Glenn P. Downing
# -------------------------------
"""
To test the program:
% python TestCollatz.py > TestCollatz.out
% chmod ugo+x TestCollatz.py
% TestCollatz.py > TestCollatz.out
"""
# -------
# imports
# -------
import StringIO
import unittest
from Collatz import collatz_read, collatz_eval, collatz_print, collatz_solve
# -----------
# TestCollatz
# -----------
class TestCollatz (unittest.TestCase) :
# ----
# read
# ----
def test_read_1 (self) :
r = StringIO.StringIO("300 400\n")
a = [0, 0]
b = collatz_read(r, a)
self.assert_(b == True)
self.assert_(a[0] == 300)
self.assert_(a[1] == 400)
def test_read_2 (self) :
r = StringIO.StringIO("105 115\n")
a = [0, 0]
b = collatz_read(r, a)
self.assert_(b == True)
self.assert_(a[0] == 105)
self.assert_(a[1] == 115)
def test_read_3 (self) :
r = StringIO.StringIO("603 703\n")
a = [0, 0]
b = collatz_read(r, a)
self.assert_(b == True)
self.assert_(a[0] == 603)
self.assert_(a[1] == 703)
# ----
# eval
# ----
def test_eval_1 (self) :
v = collatz_eval(300, 400)
self.assert_(v == 144)
def test_eval_2 (self) :
v = collatz_eval(105, 115)
self.assert_(v == 114)
def test_eval_3 (self) :
v = collatz_eval(603, 703)
self.assert_(v == 171)
# -----
# print
# -----
def test_print_1 (self) :
w = StringIO.StringIO()
collatz_print(w, 300, 400, 144)
self.assert_(w.getvalue() == "300 400 144\n")
def test_print_2 (self) :
w = StringIO.StringIO()
collatz_print(w, 105, 115, 114)
self.assert_(w.getvalue() == "105 115 114\n")
def test_print_3 (self) :
w = StringIO.StringIO()
collatz_print(w, 603, 703, 171)
self.assert_(w.getvalue() == "603 703 171\n")
# -----
# solve
# -----
def test_solve_1 (self) :
r = StringIO.StringIO("300 400\n")
w = StringIO.StringIO()
collatz_solve(r, w)
self.assert_(w.getvalue() == "300 400 144\n")
def test_solve_2 (self) :
r = StringIO.StringIO("300 400\n105 115\n")
w = StringIO.StringIO()
collatz_solve(r, w)
self.assert_(w.getvalue() == "300 400 144\n105 115 114\n")
def test_solve_3 (self) :
r = StringIO.StringIO("300 400\n105 115\n603 703")
w = StringIO.StringIO()
collatz_solve(r, w)
self.assert_(w.getvalue() == "300 400 144\n105 115 114\n603 703 171\n")
# ----
# main
# ----
print "TestCollatz.py"
unittest.main()
print "Done."