-
Notifications
You must be signed in to change notification settings - Fork 0
/
mas5774-TestCollatz.py
executable file
·201 lines (154 loc) · 4.39 KB
/
mas5774-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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
# -------------------------------
# Copyright (C) 2014
# Mark Sandan
# -------------------------------
"""
To test the program:
% coverage3 run --branch TestCollatz.py
To obtain coverage of the test:
% coverage3 report -m
"""
# -------
# imports
# -------
from io import StringIO
from unittest import main, TestCase
from Collatz import Cache, collatz_read, collatz_eval, collatz_print, collatz_solve
# -----------
# TestCollatz
# -----------
class TestCollatz (TestCase) :
# ----
# Cache
# ----
def test_cache_1 (self) :
c = Cache(10)
self.assertEqual(10, c.size())
def test_cache_2 (self) :
c = Cache(10)
c.write(2, 10)
self.assertEqual(c.read(2), 10)
def test_cache_3 (self) :
c = Cache(0)
self.assertEqual(c.size(), 0)
def test_cache_4 (self) :
c = Cache(10)
c.write(1, 100)
self.assertEqual(c.read(1), 100)
def test_cache_5 (self) :
c = Cache(10)
self.assertEqual(c.read(3), 0)
# ----
# read
# ----
def test_read_1 (self) :
r = StringIO("1 10\n100 200\n201 210\n900 1000\n")
i, j = collatz_read(r)
self.assertEqual(i, 1)
self.assertEqual(j, 10)
def test_read_2 (self) :
r = StringIO("10 1\n100 200\n201 210\n900 1000\n")
i, j = collatz_read(r)
self.assertEqual(i, 10)
self.assertEqual(j, 1)
def test_read_3 (self) :
r = StringIO("")
j = collatz_read(r)
self.assertListEqual([], j)
# ----
# eval
# ----
def test_eval_1 (self) :
v = collatz_eval(1, 10)
self.assertEqual(v, 20)
def test_eval_2 (self) :
v = collatz_eval(100, 200)
self.assertEqual(v, 125)
def test_eval_3 (self) :
v = collatz_eval(201, 210)
self.assertEqual(v, 89)
def test_eval_4 (self) :
v = collatz_eval(900, 1000)
self.assertEqual(v, 174)
def test_eval_5 (self) :
v = collatz_eval(900, 1000)
self.assertNotEqual(v, 0)
def test_eval_6 (self) :
v = collatz_eval(100, 100)
self.assertNotEqual(v, 0)
def test_eval_7 (self) :
try:
collatz_eval(0,1)
except AssertionError:
pass
except e:
self.fail('Unexpected exception raised: ',e)
else:
self.fail('AssertionError not raised')
def test_eval_8 (self) :
try:
collatz_eval(1,0)
except AssertionError:
pass
except e:
self.fail('Unexpected exception raised: ',e)
else:
self.fail('AssertionError not raised')
def test_eval_9 (self) :
try:
collatz_eval(0,0)
except AssertionError:
pass
except e:
self.fail('Unexpected exception raised: ',e)
else:
self.fail('AssertionError not raised')
def test_eval_10 (self) :
v = collatz_eval(20, 20)
self.assertEqual(v, 8)
def test_eval_11 (self) :
w = collatz_eval(19, 19)
self.assertEqual(w, 21)
def test_eval_12 (self) :
w = collatz_eval(1, 999999)
self.assertEqual(w, 525)
def test_eval_13 (self) :
w = collatz_eval(999999, 1)
self.assertEqual(w, 525)
def test_eval_14 (self) :
w = collatz_eval(999999, 999999)
self.assertEqual(w, 259)
# -----
# print
# -----
def test_print_1 (self) :
w = StringIO()
collatz_print(w, 1, 10, 20)
self.assertEqual(w.getvalue(), "1 10 20\n")
def test_print_2 (self) :
w = StringIO()
collatz_print(w, 1, 1, 1)
self.assertEqual(w.getvalue(), "1 1 1\n")
# -----
# solve
# -----
def test_solve_1 (self) :
r = StringIO("1 10\n100 200\n201 210\n900 1000\n")
w = StringIO()
collatz_solve(r, w)
self.assertEqual(w.getvalue(), "1 10 20\n100 200 125\n201 210 89\n900 1000 174\n")
def test_solve_2 (self) :
r = StringIO("")
w = StringIO()
collatz_solve(r, w)
self.assertEqual(w.getvalue(), "")
def test_solve_3 (self) :
r = StringIO("1 10\n100 200\n201 210\n900 1000\n")
w = StringIO()
collatz_solve(r, w)
self.assertEqual(w.getvalue(), "1 10 20\n100 200 125\n201 210 89\n900 1000 174\n")
# ----
# main
# ----
main()