-
Notifications
You must be signed in to change notification settings - Fork 38
/
JIC539-TestCollatz.py
executable file
·307 lines (267 loc) · 10.3 KB
/
JIC539-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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"""This module is the test harness with all unit tests"""
# !/usr/bin/env python3
# -------------------------------
# projects/collatz/TestCollatz.py
# Copyright (C) 2016
# Glenn P. Downing
# -------------------------------
# https://docs.python.org/3.4/reference/simple_stmts.html#grammar-token-assert_stmt
# -------
# imports
# -------
from io import StringIO
from unittest import main, TestCase
from Collatz import collatz_read, collatz_eval, collatz_print, collatz_solve, \
collatz_eval_not_precached, check_range, one_cycle_length
# -----------
# TestCollatz
# -----------
class TestCollatz(TestCase):
"""
class will be responsible for the entire test harness via its methods
"""
# ----
# read
# ----
def test_read_1(self):
"""
Method will ensure reading is properly done with the given string
:return: will return success or failure based on assertion of equality
"""
input_string = "1 10\n"
i, j = collatz_read(input_string)
self.assertEqual(i, 1)
self.assertEqual(j, 10)
def test_read_2(self):
"""
Method will ensure reading is properly done with the given string
:return: will return success or failure based on assertion of equality
"""
input_string = "100 10\n"
i, j = collatz_read(input_string)
self.assertEqual(i, 100)
self.assertEqual(j, 10)
def test_read_3(self):
"""
Method will ensure reading is properly done with the given string
:return: will return success or failure based on assertion of equality
"""
input_string = "5555 11110\n"
i, j = collatz_read(input_string)
self.assertEqual(i, 5555)
self.assertEqual(j, 11110)
def test_read_4(self):
"""
Method will ensure reading is properly done with the given string
:return: will return success or failure based on assertion of equality
"""
input_string = " 5555 11110\n"
i, j = collatz_read(input_string)
self.assertEqual(i, 5555)
self.assertEqual(j, 11110)
# ----
# eval
# ----
def test_eval_1(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = collatz_eval(1, 10)
self.assertEqual(returned_val, 20)
def test_eval_2(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = collatz_eval(100, 200)
self.assertEqual(returned_val, 125)
def test_eval_3(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = collatz_eval(201, 210)
self.assertEqual(returned_val, 89)
def test_eval_4(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = collatz_eval(900, 1000)
self.assertEqual(returned_val, 174)
def test_eval_5(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = collatz_eval(10, 1)
self.assertEqual(returned_val, 20)
def test_eval_6(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = collatz_eval(200, 100)
self.assertEqual(returned_val, 125)
def test_eval_7(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = collatz_eval(210, 201)
self.assertEqual(returned_val, 89)
def test_eval_8(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = collatz_eval(1000, 900)
self.assertEqual(returned_val, 174)
def test_eval_10(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = collatz_eval(1, 2)
self.assertEqual(returned_val, 2)
# ------------------
# eval_not_precached
# ------------------
def test_eval_not_precached_1(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = collatz_eval_not_precached(1, 10)
self.assertEqual(returned_val, 20)
def test_eval_not_precached_2(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = collatz_eval_not_precached(704511, 704511)
self.assertEqual(returned_val, 243)
def test_eval_not_precached_3(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = collatz_eval_not_precached(81517, 773776)
self.assertEqual(returned_val, 509)
# -----
# print
# -----
def test_check_range_1(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = check_range(1, 499)
self.assertEqual(returned_val, 144)
def test_check_range_2(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = check_range(501, 999)
self.assertEqual(returned_val, 179)
def test_check_range_3(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = check_range(999501, 999999)
self.assertEqual(returned_val, 290)
# ----------------
# one_cycle_length
# ----------------
def test_one_cycle_length_1(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = one_cycle_length(1)
self.assertEqual(returned_val, 1)
def test_one_cycle_length_2(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = one_cycle_length(837799)
self.assertEqual(returned_val, 525)
def test_one_cycle_length_3(self):
"""
Method will ensure the test is returning the correct collatz value
:return: will return success or failure based on assertion of equality
"""
returned_val = one_cycle_length(704511)
self.assertEqual(returned_val, 243)
# -----
# print
# -----
def test_print_1(self):
"""
Method will ensure printing is properly done with the given string
:return: will return success or failure based on assertion of equality
"""
writer = StringIO()
collatz_print(writer, 1, 10, 20)
self.assertEqual(writer.getvalue(), "1 10 20\n")
def test_print_2(self):
"""
Method will ensure printing is properly done with the given string
:return: will return success or failure based on assertion of equality
"""
writer = StringIO()
collatz_print(writer, 211817, 633542, 509)
self.assertEqual(writer.getvalue(), "211817 633542 509\n")
def test_print_3(self):
"""
Method will ensure printing is properly done with the given string
:return: will return success or failure based on assertion of equality
"""
writer = StringIO()
collatz_print(writer, 201356, 684930, 509)
self.assertEqual(writer.getvalue(), "201356 684930 509\n")
# -----
# solve
# -----
def test_solve_1(self):
"""
Method will ensure calculation is correct for the given
simulated file. Given multiple newlines.
:return: will return success or failure based on assertion of equality
"""
read = StringIO("1 10\n100 200\n201 210\n900 1000\n")
write = StringIO()
collatz_solve(read, write)
self.assertEqual(write.getvalue(),
"1 10 20\n100 200 125\n201 210 89\n900 1000 174\n")
def test_solve_2(self):
"""
Method will ensure calculation is correct for the given
simulated file. Given multiple newlines.
:return: will return success or failure based on assertion of equality
"""
read = StringIO("23545 900418\n993538 566823\n447212 56804\n")
write = StringIO()
collatz_solve(read, write)
self.assertEqual(write.getvalue(),
"23545 900418 525\n993538 566823 525\n447212 56804 449\n")
def test_solve_3(self):
"""
Method will ensure calculation is correct for the given
simulated file. Given multiple newlines.
:return: will return success or failure based on assertion of equality
"""
read = StringIO("736556 527536\n81843 993308\n880202 469143\n")
write = StringIO()
collatz_solve(read, write)
self.assertEqual(write.getvalue(),
"736556 527536 509\n81843 993308 525\n880202 469143 525\n")
# ----
# main
# ----
if __name__ == "__main__":
main()