forked from budang/collatz-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathds38352-TestCollatz.py
executable file
·187 lines (148 loc) · 4.78 KB
/
ds38352-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
#!/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
"""Tests harnesses for Collatz"""
# -------
# imports
# -------
from io import StringIO
from unittest import main, TestCase
from Collatz import collatz_read, collatz_eval, collatz_print, collatz_solve, CACHE
"""CACHE is specfic to my implementation"""
# -----------
# TestCollatz
# -----------
class TestCollatz(TestCase):
"""Test harness"""
# ----
# read
# ----
def test_read_1(self):
"""Basic read functionality"""
line = "1 10\n"
begin, end = collatz_read(line)
self.assertEqual(begin, 1)
self.assertEqual(end, 10)
def test_read_2(self):
"""Read input in exactly, even if reversed"""
line = "999 1\n"
begin, end = collatz_read(line)
self.assertEqual(begin, 999)
self.assertEqual(end, 1)
def test_read_4(self):
"""Additional ints in a line are ignored"""
line = "10 20 30 40 50\n"
begin, end = collatz_read(line)
self.assertEqual(begin, 10)
self.assertEqual(end, 20)
def test_read_5(self):
"""Short of input should break in read"""
error = False
try:
collatz_read("1\n")
except IndexError:
error = True
self.assertTrue(error)
# ----
# eval
# ----
def test_eval_1(self):
"""Correctness: 89"""
value = collatz_eval(201, 210)
self.assertEqual(value, 89)
def test_eval_2(self):
"""Correctness: 174"""
value = collatz_eval(900, 1000)
self.assertEqual(value, 174)
def test_eval_3(self):
"""Reversed range performs normal"""
value = collatz_eval(312, 42)
self.assertEqual(value, 128)
def test_eval_4(self):
"""Correctness: 259; and test for-loop exec"""
value = collatz_eval(999999, 999999)
self.assertEqual(value, 259)
def test_eval_5(self):
"""Corectness for longest path"""
value = collatz_eval(837799, 837799)
self.assertEqual(value, 525)
# -----
# cache
# -----
def test_cache_1(self):
"""While evaluating 3, 5 should be cached correctly"""
collatz_eval(3, 3)
self.assertEqual(CACHE[5], 6)
def test_cache_2(self):
"""While evaluating 3, 25 should not be cached"""
collatz_eval(3, 3)
self.assertFalse(25 in CACHE)
def test_cache_3(self):
"""Testing cache for a larger range"""
collatz_eval(1, 312)
self.assertEqual(CACHE[156], 37)
# -----
# print
# -----
def test_print_1(self):
"""Basic print functionality"""
output = StringIO()
collatz_print(output, 1, 10, 20)
self.assertEqual(output.getvalue(), "1 10 20\n")
def test_print_2(self):
"""Reversed range executes, but prints same as input"""
output = StringIO()
collatz_print(output, 20, 10, 21)
self.assertEqual(output.getvalue(), "20 10 21\n")
def test_print_3(self):
"""Test bogus numbers"""
output = StringIO()
collatz_print(output, -10, 0, 999999999999999999999)
self.assertEqual(output.getvalue(), "-10 0 999999999999999999999\n")
# -----
# solve
# -----
def test_solve_1(self):
"""Basic read and print functionality"""
inln = StringIO("1 10\n100 200\n201 210\n900 1000\n")
output = StringIO()
collatz_solve(inln, output)
self.assertEqual(
output.getvalue(), "1 10 20\n100 200 125\n201 210 89\n900 1000 174\n")
def test_solve_2(self):
"""Skip empty lines"""
inln = StringIO("\n\n1 10\n\n100 200\n")
output = StringIO()
collatz_solve(inln, output)
self.assertEqual(
output.getvalue(), "1 10 20\n100 200 125\n")
def test_solve_3(self):
"""Reverse range, but print input and eval"""
inln = StringIO("200 100\n")
output = StringIO()
collatz_solve(inln, output)
self.assertEqual(
output.getvalue(), "200 100 125\n")
def test_solve_4(self):
"""Additional input should be ignored"""
inln = StringIO("10 20 30 40 50 60 70\n")
output = StringIO()
collatz_solve(inln, output)
self.assertEqual(
output.getvalue(), "10 20 21\n")
def test_solve_5(self):
"""Combination of all solve tests"""
inln = StringIO("\n\n20 1 15 100 60 78\n")
output = StringIO()
collatz_solve(inln, output)
self.assertEqual(
output.getvalue(), "20 1 21\n")
# ----
# main
# ----
if __name__ == "__main__":
main()