/
trentkan-TestCollatz.py
executable file
·165 lines (131 loc) · 3.76 KB
/
trentkan-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
#!/usr/bin/env python3
# -------------------------------
# projects/collatz/TestCollatz.py
# Copyright (C) 2014
# Glenn P. Downing
# Contributor: Trent Kan
# -------------------------------
"""
To test the program:
% python TestCollatz.py >& TestCollatz.out
% chmod ugo+x TestCollatz.py
% TestCollatz.py >& TestCollatz.out
"""
# -------
# imports
# -------
import io
import unittest
from Collatz import collatz_read, collatz_eval, collatz_print, collatz_solve, get_cycle_len
# -----------
# TestCollatz
# -----------
class TestCollatz (unittest.TestCase) :
# ----
# read
# ----
def test_read1 (self) :
r = io.StringIO("1 10\n")
a = [0, 0]
b = collatz_read(r, a)
i, j = a
self.assertTrue(b == True)
self.assertTrue(i == 1)
self.assertTrue(j == 10)
def test_read2 (self) :
r = io.StringIO("345634 2451\n")
a = [0, 0]
b = collatz_read(r, a)
i, j = a
self.assertTrue(b == True)
self.assertTrue(i == 345634)
self.assertTrue(j == 2451)
def test_read3 (self) :
r = io.StringIO("4 400\n")
a = [0, 0]
b = collatz_read(r, a)
i, j = a
self.assertTrue(b == True)
self.assertTrue(i == 4)
self.assertTrue(j == 400)
# ----
# eval
# ----
def test_eval_1 (self) :
v = collatz_eval(1, 10)
self.assertTrue(v == 20)
def test_eval_2 (self) :
v = collatz_eval(100, 200)
self.assertTrue(v == 125)
def test_eval_3 (self) :
v = collatz_eval(201, 210)
self.assertTrue(v == 89)
def test_eval_4 (self) :
v = collatz_eval(900, 1000)
self.assertTrue(v == 174)
def test_eval5 (self) :
v = collatz_eval(50, 25)
self.assertTrue(v == 112)
# -----
# print
# -----
def test_print (self) :
w = io.StringIO()
collatz_print(w, 1, 10, 20)
self.assertTrue(w.getvalue() == "1 10 20\n")
def test_print2 (self) :
w = io.StringIO()
collatz_print(w, 10, 1, 20)
self.assertTrue(w.getvalue() == "10 1 20\n")
def test_print3 (self) :
w = io.StringIO()
collatz_print(w, 2, 5, 7)
self.assertTrue(w.getvalue() == "2 5 7\n")
# -----
# solve
# -----
def test_solve (self) :
r = io.StringIO("1 10\n100 200\n201 210\n900 1000\n")
w = io.StringIO()
collatz_solve(r, w)
self.assertTrue(w.getvalue() == "1 10 20\n100 200 125\n201 210 89\n900 1000 174\n")
#tests first optimization
def test_solve2 (self) :
r = io.StringIO("1 100\n50 100\n")
w = io.StringIO()
collatz_solve(r, w)
self.assertTrue(w.getvalue() == "1 100 119\n50 100 119\n")
def test_solve3 (self) :
r = io.StringIO("1 50\n600 2353\n325 565\n5676 543\n")
w = io.StringIO()
collatz_solve(r, w)
self.assertTrue(w.getvalue() == "1 50 112\n600 2353 183\n325 565 144\n5676 543 238\n")
def test_solve4 (self) :
r = io.StringIO("1 1\n")
w = io.StringIO()
collatz_solve(r, w)
self.assertTrue(w.getvalue() == "1 1 1\n")
# -----
# get_cycle_len
# -----
def test_get_cycle_len1(self) :
max_length = [0]
max_length[0] = 245
get_cycle_len(10, max_length)
self.assertTrue(max_length[0] == 245)
def test_get_cycle_len2 (self) :
max_length = [0]
max_length[0] = 4
get_cycle_len(50, max_length)
self.assertTrue(max_length[0] == 25)
def test_get_cycle_len3 (self) :
max_length = [0]
max_length[0] = 20
get_cycle_len(9, max_length)
self.assertTrue(max_length[0] == 20)
# ----
# main
# ----
print("TestCollatz.py")
unittest.main()
print("Done.")