-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhyperop_test.py
More file actions
171 lines (115 loc) · 4.1 KB
/
Copy pathhyperop_test.py
File metadata and controls
171 lines (115 loc) · 4.1 KB
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
import unittest
import itertools
import math
import operator
import hyperop as hyperop_lib
from hyperop import hyperop, bounded_hyperop
testing_values = range(1, 15)
def check_range(fA, fB):
for x, y in itertools.product(testing_values, repeat=2):
assert fA(x, y) == fB(x, y)
class PrimitiveSmallValues(unittest.TestCase):
def test_primitive_H1(self):
check_range(hyperop(1, primitive=True), operator.add)
def test_primitive_H2(self):
check_range(hyperop(2, primitive=True), operator.mul)
def test_primitive_H3(self):
check_range(hyperop(3, primitive=True), operator.pow)
class SmallValues(unittest.TestCase):
def test_H0(self):
def successor(_, b):
return 1 + b
check_range(hyperop(0), successor)
def test_H1(self):
check_range(hyperop(1), operator.add)
def test_H2(self):
check_range(hyperop(2), operator.mul)
def test_H3(self):
check_range(hyperop(3), operator.pow)
class KnownValues(unittest.TestCase):
def test_H4(self):
H = hyperop(4)
# Small fixed values, a=2
assert H(2, 1) == 2
assert H(2, 2) == 4
assert H(2, 3) == 16
assert H(2, 4) == 65536
# Small fixed values, a=3
assert H(3, 1) == 3
assert H(3, 2) == 27
assert H(3, 3) == 7625597484987
# Log of large value should be related to smaller one
a2_5 = round(math.log(H(2, 5), 2))
assert a2_5 == H(2, 4)
def test_H5(self):
H = hyperop(5)
# Small fixed values, a=2
assert H(2, 1) == 2
assert H(2, 2) == 4
assert H(2, 3) == 65536
# Small fixed values, a=3
assert H(3, 1) == 3
assert H(3, 2) == 7625597484987
class ValidRanges(unittest.TestCase):
def test_non_integral_H4(self):
H = hyperop(4)
with self.assertRaises(ValueError):
H(2, 0.5)
def test_non_integral_H5(self):
H = hyperop(5)
with self.assertRaises(ValueError):
H(1.1, 2)
with self.assertRaises(ValueError):
H(2, 1.1)
def test_non_integral_n(self):
with self.assertRaises(ValueError):
hyperop(1.2)
def test_non_negative_n(self):
with self.assertRaises(ValueError):
hyperop(-1)
class BoundedHyperop(unittest.TestCase):
def test_integer_bounds(self):
H = bounded_hyperop(4, bound=1000)
assert H(2, 5) == H.infinity
def test_complex_bounds(self):
H = bounded_hyperop(4, bound=1000)
assert H(5.0, 5) == H.infinity
def test_coorespondance(self):
'''
Check if the bounded hyperop matches with the regular
version for small values of a,b.
'''
bound = hyperop(4)(3, 3)
vals = range(1, 4)
for N in range(0, 5):
H = hyperop(N, primitive=True)
Hb = bounded_hyperop(N, bound=bound, primitive=True)
for a, b in itertools.product(vals, repeat=2):
assert H(a, b) == Hb(a, b)
class SpecialCases(unittest.TestCase):
def test_special_case_b0(self):
H0 = dict([(n, hyperop(n)) for n in range(15)])
Hb = dict([(n, bounded_hyperop(n)) for n in range(15)])
for H in [H0, Hb]:
for a in testing_values:
assert H[0](a, 0) == 1
assert H[1](a, 0) == a
assert H[2](a, 0) == 0
for n in range(3, 15):
assert H[n](a, 0) == 1
def test_special_case_a0(self):
H0 = dict([(n, hyperop(n)) for n in range(15)])
Hb = dict([(n, bounded_hyperop(n)) for n in range(15)])
for H in [H0, Hb]:
for b in testing_values:
assert H[0](0, b) == b + 1
assert H[1](0, b) == b
assert H[2](0, b) == 0
assert H[3](0, b) == 0
for n in range(4, 15):
assert H[n](0, b) == (b % 2 == 0)
class CheckMeta(unittest.TestCase):
def test_VersionNumberExists(self):
hyperop_lib.__version__
if __name__ == '__main__':
unittest.main()