forked from budang/collatz-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjm72323-TestCollatz.py
166 lines (134 loc) · 4.34 KB
/
jm72323-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
#!/usr/bin/env python3
# -------------------------------
# projects/collatz/TestCollatz.py
# Copyright (C) 2016
# Glenn P. Downing
# -------------------------------
""" A test harness for Collatz. """
# 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,
calc_collatz)
# -----------
# TestCollatz
# -----------
class TestCollatz(TestCase):
""" A class for testing Collatz. """
# ----
# read
# ----
def test_read(self):
""" Read test 1 """
string = "1 10\n"
first_num, second_num = collatz_read(string)
self.assertEqual(first_num, 1)
self.assertEqual(second_num, 10)
def test_read_2(self):
""" Read test 2 """
string = "100 200\n"
first_num, second_num = collatz_read(string)
self.assertEqual(first_num, 100)
self.assertEqual(second_num, 200)
def test_read_3(self):
""" Read test 3 """
string = "201 210\n"
first_num, second_num = collatz_read(string)
self.assertEqual(first_num, 201)
self.assertEqual(second_num, 210)
# -------------
# calculate collatz
# -------------
def test_calc_collatz(self):
"""
Test calculating the max cycle length of one number
"""
max_cycle = calc_collatz(1, {})
self.assertEqual(max_cycle, 1)
def test_calc_collatz_2(self):
""" Collatz calculation test 2 """
max_cycle = calc_collatz(10, {})
self.assertEqual(max_cycle, 7)
def test_calc_collatz_3(self):
""" Collatz calculation test 3 """
max_cycle = calc_collatz(160, {})
self.assertEqual(max_cycle, 11)
# ----
# eval
# ----
def test_eval_1(self):
""" Test calculating the correct max cycle """
value = collatz_eval(1, 10)
self.assertEqual(value, 20)
def test_eval_2(self):
""" Eval test 2 """
value = collatz_eval(100, 200)
self.assertEqual(value, 125)
def test_eval_3(self):
""" Eval test 3 """
value = collatz_eval(201, 210)
self.assertEqual(value, 89)
def test_eval_4(self):
""" Eval test 4 """
value = collatz_eval(900, 1000)
self.assertEqual(value, 174)
def test_eval_5(self):
""" Eval test 5 inverse first and second """
value = collatz_eval(1000, 900)
self.assertEqual(value, 174)
def test_eval_6(self):
""" Eval test6 """
value = collatz_eval(999900, 1000000)
self.assertEqual(value, 259)
# -----
# print
# -----
def test_print(self):
""" Tests printing the values to the output. """
writer = StringIO()
collatz_print(writer, 1, 10, 20)
self.assertEqual(writer.getvalue(), "1 10 20\n")
def test_print_2(self):
""" Test printing 2 """
writer = StringIO()
collatz_print(writer, 100, 200, 125)
self.assertEqual(writer.getvalue(), "100 200 125\n")
def test_print_3(self):
""" Test printing 3 """
writer = StringIO()
collatz_print(writer, 201, 210, 89)
self.assertEqual(writer.getvalue(), "201 210 89\n")
# -----
# solve
# -----
def test_solve(self):
""" Tests solving for multiple input values. """
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_2(self):
""" Tests solve 2 """
reader = StringIO("1 200\n1 1000\n1 10000\n200 200000\n")
writer = StringIO()
collatz_solve(reader, writer)
self.assertEqual(writer.getvalue(),
"1 200 125\n1 1000 179\n1 10000 262\n200 200000 383\n")
def test_solve_3(self):
""" Tests solve 3, whitespace only """
reader = StringIO(" ")
writer = StringIO()
collatz_solve(reader, writer)
self.assertEqual(writer.getvalue(), "")
# ----
# main
# ----
if __name__ == "__main__":
main()