forked from budang/collatz-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs46485-TestCollatz.py
251 lines (213 loc) · 6.12 KB
/
cs46485-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
#!/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
from Collatz import collatz_solve, odd_n, dec_range, cycle_length
# -----------
# TestCollatz
# -----------
class TestCollatz(TestCase):
# ----
# odd_n
# ----
def test_odd_n_1(self):
"""
Runs odd_n function with the value 7 and should evaluated to 11.
"""
ans = odd_n(7)
self.assertEqual(ans, 11)
def test_odd_n_2(self):
"""
Runs odd_n function with the value 25 and should evaluated to 38.
"""
ans = odd_n(25)
self.assertEqual(ans, 38)
def test_odd_n_3(self):
"""
Runs odd_n function with the value 105 and should evaluated to 39.
"""
ans = odd_n(105)
self.assertEqual(ans, 158)
# ----
# dec_range
# ----
def test_dec_range_1(self):
"""
Runs dec_range for values 1 and 10, should return 6.
"""
beg = dec_range(1, 10)
self.assertEqual(beg, 6)
def test_dec_range_2(self):
"""
Runs dec_range for values 30 and 35, should return 30.
"""
beg = dec_range(30, 35)
self.assertEqual(beg, 30)
def test_dec_range_3(self):
"""
Runs dec_range for values 100 and 200, should return 30.
"""
beg = dec_range(100, 200)
self.assertEqual(beg, 101)
# ----
# cycle_length
# ----
def cycle_length_1(self):
"""
Runs cycle_length with value of 1
Should run into the base case
"""
cycle_len = cycle_length(1)
self.assertEqual(cycle_len, 1)
def cycle_length_2(self):
"""
Runs cycle_length with value of 53
Should return value of 12
"""
cycle_len = cycle_length(53)
self.assertEqual(cycle_len, 12)
def cycle_length_3(self):
"""
Runs cycle_length with value of 99999
Should return value of 227
"""
cycle_len = cycle_length(99999)
self.assertEqual(cycle_len, 227)
# ----
# read
# ----
def test_read_1(self):
"""
Tests that values are being read properly
"""
string = "1 10\n"
beg, end = collatz_read(string)
self.assertEqual(beg, 1)
self.assertEqual(end, 10)
def test_read_2(self):
"""
Tests that values are being read properly
"""
string = "10000 723\n"
beg, end = collatz_read(string)
self.assertEqual(beg, 10000)
self.assertEqual(end, 723)
def test_read_3(self):
"""
Tests that values are being read properly
"""
string = "100 7233\n"
beg, end = collatz_read(string)
self.assertEqual(beg, 100)
self.assertEqual(end, 7233)
# ----
# eval
# ----
def test_eval_1(self):
"""
Runs collatz_eval with values 20 and 100
Should return max_cycle_length of 119
"""
ans = collatz_eval(20, 100)
self.assertEqual(ans, 119)
def test_eval_2(self):
"""
Runs collatz_eval with values 1 and 999999
Should return max_cycle_length of 119
"""
ans = collatz_eval(1, 999999)
self.assertEqual(ans, 525)
def test_eval_3(self):
"""
Runs collatz_eval with values 40000 and 20
Should return max_cycle_length of 324
"""
ans = collatz_eval(40000, 20)
self.assertEqual(ans, 324)
def test_eval_4(self):
"""
Runs collatz_eval with values 1 and 2
Should return max_cycle_length of 2
"""
ans = collatz_eval(1, 2)
self.assertEqual(ans, 2)
# -----
# print
# -----
def test_print_1(self):
"""
Tests the writer
"""
writer = StringIO()
collatz_print(writer, 20, 100, 119)
self.assertEqual(writer.getvalue(), "20 100 119\n")
def test_print_2(self):
"""
Tests the writer
"""
writer = StringIO()
collatz_print(writer, 40000, 20, 324)
self.assertEqual(writer.getvalue(), "40000 20 324\n")
def test_print_3(self):
"""
Tests the writer
"""
writer = StringIO()
collatz_print(writer, 1, 1, 1)
self.assertEqual(writer.getvalue(), "1 1 1\n")
writer = StringIO()
collatz_print(writer, 40000, 20, 324)
self.assertEqual(writer.getvalue(), "40000 20 324\n")
# -----
# solve
# -----
def test_solve_empty(self):
"""
Tests the solver, writer, and reader
Makes sure the right value is received and printed
Checks to see if empty strings are handled correctly
"""
reader = StringIO("719 6121\n\n1 23945\n")
writer = StringIO()
collatz_solve(reader, writer)
self.assertEqual(
writer.getvalue(), "719 6121 238\n1 23945 282\n")
def test_solve_1(self):
"""
Tests the solver, writer, and reader
Makes sure the right value is received and printed
"""
def test_solve(self):
"""
Tests the solver, writer, and reader
Makes sure the right value is received and printed
"""
reader = StringIO("20 100\n1 999999\n40000 20\n1 2\n")
writer = StringIO()
collatz_solve(reader, writer)
self.assertEqual(
writer.getvalue(), "20 100 119\n1 999999 525\n40000 20 324\n1 2 2\n")
def test_solve_2(self):
"""
Tests the solver, writer, and reader
Makes sure the right value is received and printed
"""
reader = StringIO("1 1\n")
writer = StringIO()
collatz_solve(reader, writer)
self.assertEqual(
writer.getvalue(), "1 1 1\n")
# ----
# main
# ----
if __name__ == "__main__":
main()