-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathime257-TestCollatz.py
executable file
·168 lines (138 loc) · 3.94 KB
/
ime257-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
"""Test Collatz"""
#!/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_calculate
# -----------
# TestCollatz
# -----------
class TestCollatz(TestCase):
"""
Class for testing Collatz
"""
# ----
# read
# ----
def test_read(self):
""" Ensure that input string is read and parsed correctly """
s_value = "1 10\n"
i_value, j_value = collatz_read(s_value)
self.assertEqual(i_value, 1)
self.assertEqual(j_value, 10)
# ----
# calculate
# ----
def test_calculate_1(self):
"""
Ensure calculate functionality is correct
"""
c_value = collatz_calculate(10)
self.assertEqual(c_value, 7)
def test_calculate_2(self):
"""
Ensure calculate functionality is correct
"""
c_value = collatz_calculate(20000)
self.assertEqual(c_value, 31)
def test_calculate_3(self):
"""
Ensure calculate functionality is correct
"""
c_value = collatz_calculate(1500)
self.assertEqual(c_value, 48)
# ----
# eval
# ----
def test_eval_1(self):
"""
Ensure eval functionality is correct
"""
v_value = collatz_eval(1, 10)
self.assertEqual(v_value, 20)
def test_eval_2(self):
"""
Ensure eval functionality is correct
"""
v_value = collatz_eval(100, 200)
self.assertEqual(v_value, 125)
def test_eval_3(self):
"""
Ensure eval functionality is correct
"""
v_value = collatz_eval(201, 210)
self.assertEqual(v_value, 89)
def test_eval_4(self):
"""
Ensure eval functionality is correct
"""
v_value = collatz_eval(900, 1000)
self.assertEqual(v_value, 174)
def test_eval_5(self):
"""
Make sure even range in the wrong order works
"""
v_value = collatz_eval(1000, 900)
self.assertEqual(v_value, 174)
def test_eval_6(self):
"""
Make sure even range in the wrong order works
"""
v_value = collatz_eval(210, 201)
self.assertEqual(v_value, 89)
def test_eval_7(self):
"""
Make sure even range in the wrong order works
"""
v_value = collatz_eval(10, 1)
self.assertEqual(v_value, 20)
def test_eval_8(self):
"""
Test the b < m and m = e/2 + 1 then mcl(b,e) = mcl(m,e)
"""
v_value = collatz_eval(10, 100)
self.assertEqual(v_value, 119)
def test_eval_9(self):
"""
Test the b < m and m = e/2 + 1 then mcl(b,e) = mcl(m,e)
"""
v_value = collatz_eval(1, 348)
self.assertEqual(v_value, 144)
def test_eval_10(self):
"""
Test the b < m and m = e/2 + 1 then mcl(b,e) = mcl(m,e)
"""
v_value = collatz_eval(18, 20)
self.assertEqual(v_value, 21)
# -----
# print
# -----
def test_print(self):
""" Ensure print functionality is correct """
w_value = StringIO()
collatz_print(w_value, 1, 10, 20)
self.assertEqual(w_value.getvalue(), "1 10 20\n")
# -----
# solve
# -----
def test_solve(self):
""" Ensure solve functionality is correct """
r_value = StringIO("1 10\n100 200\n201 210\n900 1000\n1000 900\n")
w_value = StringIO()
collatz_solve(r_value, w_value)
self.assertEqual(
w_value.getvalue(),
"1 10 20\n100 200 125\n201 210 89\n900 1000 174\n1000 900 174\n")
# ----
# main
# ----
if __name__ == "__main__":
main()