forked from budang/collatz-tests
/
jdt2725-TestCollatz.py
executable file
·133 lines (106 loc) · 3.14 KB
/
jdt2725-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
#!/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
# -----------
# TestCollatz
# -----------
class TestCollatz(TestCase):
"""Testing Collatz"""
# ----
# read
# ----
def test_read_1(self):
"""Testing read"""
strg = "1 10\n"
start, end = collatz_read(strg)
self.assertEqual(start, 1)
self.assertEqual(end, 10)
def test_read_2(self):
"""Testing read"""
strg = "3 4\n"
start, end = collatz_read(strg)
self.assertEqual(start, 3)
self.assertEqual(end, 4)
def test_read_3(self):
"""Testing read"""
strg = "77 80\n"
start, end = collatz_read(strg)
self.assertEqual(start, 77)
self.assertEqual(end, 80)
# ----
# eval
# ----
def test_eval_1(self):
"""Testing eval"""
val = collatz_eval(1, 10)
self.assertEqual(val, 20)
def test_eval_2(self):
"""Testing eval"""
val = collatz_eval(100, 200)
self.assertEqual(val, 125)
def test_eval_3(self):
"""Testing eval"""
val = collatz_eval(201, 210)
self.assertEqual(val, 89)
def test_eval_4(self):
"""Testing eval"""
val = collatz_eval(900, 1000)
self.assertEqual(val, 174)
def test_eval_5(self):
"""Testing max cycle"""
val = collatz_eval(418515, 603657)
self.assertEqual(val, 470)
def test_eval_6(self):
"""Testing max cycle"""
val = collatz_eval(132501, 322152)
self.assertEqual(val, 443)
def test_eval_7(self):
"""Testing max cycle"""
val = collatz_eval(241267, 283022)
self.assertEqual(val, 407)
def test_eval_8(self):
"""Testing max cycle"""
val = collatz_eval(443846, 945884)
self.assertEqual(val, 525)
# -----
# print
# -----
def test_print_1(self):
"""Testing print"""
inputoutput = StringIO()
collatz_print(inputoutput, 1, 10, 20)
self.assertEqual(inputoutput.getvalue(), "1 10 20\n")
def test_print_2(self):
"""Testing print"""
inputoutput = StringIO()
collatz_print(inputoutput, 2, 11, 21)
self.assertEqual(inputoutput.getvalue(), "2 11 21\n")
def test_print_3(self):
"""Testing print"""
inputoutput = StringIO()
collatz_print(inputoutput, 4, 5, 6)
self.assertEqual(inputoutput.getvalue(), "4 5 6\n")
# -----
# solve
# -----
def test_solve(self):
"""Testing solve"""
read = StringIO("1 10\n100 200\n201 210\n900 1000\n")
write = StringIO()
collatz_solve(read, write)
self.assertEqual(write.getvalue(), "1 10 20\n100 200 125\n201 210 89\n900 1000 174\n")
# ----
# main
# ----
if __name__ == "__main__":
main()