This repository has been archived by the owner on May 30, 2024. It is now read-only.
forked from budang/collatz-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jlv2337-TestCollatz.py
executable file
·214 lines (180 loc) · 5.17 KB
/
jlv2337-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
#!/usr/bin/env python3
"""Unit Tests - White Box Testing for Collatz"""
# -------------------------------
# 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
# -----------
# TestCollatz
# -----------
class TestCollatz(TestCase):
"""
Unit Tests for Collatz.py functions/methods
"""
# ----
# read
# ----
def test_read_1(self):
"""
Tests that the inputs are read from each line
"""
line = "1 10\n"
first, second = collatz_read(line)
self.assertEqual(first, 1)
self.assertEqual(second, 10)
def test_read_2(self):
"""
Tests that the input can be read with first > second
"""
line = "999999 1\n"
first, second = collatz_read(line)
self.assertEqual(first, 999999)
self.assertEqual(second, 1)
def test_read_3(self):
"""
Tests that the inputs are read from each line
"""
line = "100 50000\n"
first, second = collatz_read(line)
self.assertEqual(first, 100)
self.assertEqual(second, 50000)
def test_read_4(self):
"""
Tests that the inputs are read from each line
"""
reader = StringIO("9 70060\n")
for item in reader:
first, second = collatz_read(item)
self.assertEqual(first, 9)
self.assertEqual(second, 70060)
def test_read_5(self):
"""
Tests that multiple lines of the input are read.
"""
reader = StringIO("700 3\n500 2\n")
for item in reader:
first, second = collatz_read(item)
self.assertEqual(first, 500)
self.assertEqual(second, 2)
# ----
# eval
# ----
def test_eval_1(self):
"""
Tests for max cycle between 1 - 10
"""
maxc = collatz_eval(1, 10)
self.assertEqual(maxc, 20)
def test_eval_2(self):
"""
Tests for max cycle between 100 - 200
"""
maxc = collatz_eval(100, 200)
self.assertEqual(maxc, 125)
def test_eval_3(self):
"""
Tests for max cycle between 201 - 210
"""
maxc = collatz_eval(201, 210)
self.assertEqual(maxc, 89)
def test_eval_4(self):
"""
Tests for max cycle between 900 - 1000
"""
maxc = collatz_eval(900, 1000)
self.assertEqual(maxc, 174)
def test_eval_5(self):
"""
Tests for max cycle between 1000 - 899
"""
maxc = collatz_eval(1000, 899)
self.assertEqual(maxc, 174)
def test_eval_6(self):
"""
Tests for max cycle between 501 - 2
"""
maxc = collatz_eval(501, 2)
self.assertEqual(maxc, 144)
def test_eval_7(self):
"""
Tests for max cycle multiple times
"""
maxc = collatz_eval(501, 2)
maxc = collatz_eval(1, 2)
maxc = collatz_eval(1, 1)
maxc = collatz_eval(6, 2)
self.assertEqual(maxc, 9)
def test_eval_8(self):
"""
Tests for max cycle between 1001 - 2000
"""
maxc = collatz_eval(1001, 2000)
self.assertEqual(maxc, 182)
def test_eval_9(self):
"""
Tests for max cycle between 1000 2000
"""
maxc = collatz_eval(1000, 2000)
self.assertEqual(maxc, 182)
def test_eval_10(self):
"""
Tests for max cycle between 1000 - 5000
"""
maxc = collatz_eval(1000, 5000)
self.assertEqual(maxc, 238)
def test_eval_11(self):
"""
Tests for max cycle between 1001 - 5001
"""
maxc = collatz_eval(1001, 5001)
self.assertEqual(maxc, 238)
def test_eval_12(self):
"""
Tests for max cycle between 5001 - 1001
"""
maxc = collatz_eval(5001, 1001)
self.assertEqual(maxc, 238)
# -----
# print
# -----
def test_print_1(self):
"""
Tests printing to output source.
"""
writer = StringIO()
collatz_print(writer, 1, 10, 20)
self.assertEqual(writer.getvalue(), "1 10 20\n")
# -----
# solve
# -----
def test_solve_1(self):
"""
Tests for multiple max cycle evals.
"""
reader = StringIO("1 10\n100 200\n201 210\n900 1000\n")
writer = StringIO()
collatz_solve(reader, writer)
self.assertEqual(writer.getvalue(),
"1 10 20\n100 200 125\n201 210 89\n900 1000 174\n")
def test_solve_3(self):
"""
Tests for multiple max cycle evals triggering the meta cache.
"""
reader = StringIO("10 1\n2000 1000\n1000 2000\n1001 5001\n")
writer = StringIO()
collatz_solve(reader, writer)
self.assertEqual(writer.getvalue(),
"10 1 20\n2000 1000 182\n1000 2000 182\n1001 5001 238\n")
# ----
# main
# ----
if __name__ == "__main__":
main()