forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_maths.py
207 lines (152 loc) · 5.01 KB
/
test_maths.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from algorithms.maths import (
int2base, base2int,
extended_gcd,
gcd, lcm,
gen_strobogrammatic, strobogrammatic_in_range,
is_strobogrammatic, is_strobogrammatic2,
find_next_square, find_next_square2,
prime_check, prime_check2,
primes,
pythagoras,
is_prime,
encrypt, decrypt, generate_key
)
import unittest
class TestBaseConversion(unittest.TestCase):
"""
Test for the file base_conversion.py
Arguments:
unittest {[type]} -- [description]
"""
def test_int2base(self):
self.assertEqual("101", int2base(5, 2))
self.assertEqual("0", int2base(0, 2))
self.assertEqual("FF", int2base(255, 16))
def test_base2int(self):
self.assertEqual(5, base2int("101", 2))
self.assertEqual(0, base2int("0", 2))
self.assertEqual(255, base2int("FF", 16))
class TestExtendedGcd(unittest.TestCase):
"""[summary]
Test for the file extended_gcd.py
Arguments:
unittest {[type]} -- [description]
"""
def test_extended_gcd(self):
self.assertEqual((0, 1, 2), extended_gcd(8, 2))
self.assertEqual((0, 1, 17), extended_gcd(13, 17))
class TestGcd(unittest.TestCase):
"""[summary]
Test for the file gcd.py
Arguments:
unittest {[type]} -- [description]
"""
def test_gcd(self):
self.assertEqual(4, gcd(8, 12))
self.assertEqual(1, gcd(13, 17))
def test_lcm(self):
self.assertEqual(24, lcm(8, 12))
class TestGenerateStroboGrammatic(unittest.TestCase):
"""[summary]
Test for the file generate_strobogrammatic.py
Arguments:
unittest {[type]} -- [description]
"""
def test_gen_strobomatic(self):
self.assertEqual(['88', '11', '96', '69'], gen_strobogrammatic(2))
def test_strobogrammatic_in_range(self):
self.assertEqual(4, strobogrammatic_in_range("10", "100"))
class TestIsStrobogrammatic(unittest.TestCase):
"""[summary]
Test for the file is_strobogrammatic.py
Arguments:
unittest {[type]} -- [description]
"""
def test_is_strobogrammatic(self):
self.assertTrue(is_strobogrammatic("69"))
self.assertFalse(is_strobogrammatic("14"))
def test_is_strobogrammatic2(self):
self.assertTrue(is_strobogrammatic2("69"))
self.assertFalse(is_strobogrammatic2("14"))
class TestNextPerfectSquare(unittest.TestCase):
"""[summary]
Test for the file next_perfect_square.py
Arguments:
unittest {[type]} -- [description]
"""
def test_find_next_square(self):
self.assertEqual(36, find_next_square(25))
self.assertEqual(1, find_next_square(0))
def test_find_next_square2(self):
self.assertEqual(36, find_next_square2(25))
self.assertEqual(1, find_next_square2(0))
class TestPrimesSieveOfEratosthenes(unittest.TestCase):
"""[summary]
Test for the file primes_sieve_of_eratosthenes.py
Arguments:
unittest {[type]} -- [description]
"""
def test_primes(self):
self.assertEqual([2, 3, 5, 7], primes(7))
class TestPrimeTest(unittest.TestCase):
"""[summary]
Test for the file prime_test.py
Arguments:
unittest {[type]} -- [description]
"""
def test_prime_test(self):
"""
checks all prime numbers between 2 up to 100.
Between 2 up to 100 exists 25 prime numbers!
"""
counter = 0
for i in range(2, 101):
if prime_check(i):
counter += 1
self.assertEqual(25, counter)
def test_prime_test2(self):
"""
checks all prime numbers between 2 up to 100.
Between 2 up to 100 exists 25 prime numbers!
"""
counter = 0
for i in range(2, 101):
if prime_check2(i):
counter += 1
self.assertEqual(25, counter)
class TestPythagoras(unittest.TestCase):
"""[summary]
Test for the file pythagoras.py
Arguments:
unittest {[type]} -- [description]
"""
def test_pythagoras(self):
self.assertEqual("Hypotenuse = 3.605551275463989", pythagoras(3, 2, "?"))
class TestRabinMiller(unittest.TestCase):
"""[summary]
Test for the file rabin_miller.py
Arguments:
unittest {[type]} -- [description]
"""
def test_is_prime(self):
self.assertTrue(is_prime(7, 2))
self.assertTrue(is_prime(13, 11))
self.assertFalse(is_prime(6, 2))
class TestRSA(unittest.TestCase):
"""[summary]
Test for the file rsa.py
Arguments:
unittest {[type]} -- [description]
"""
def test_encrypt_decrypt(self):
self.assertEqual(7, decrypt(encrypt(7, 23, 143), 47, 143))
# def test_key_generator(self): # this test takes a while!
# for i in range(100):
# print("step {0}".format(i))
# n, e, d = generate_key(26)
# data = 2
# en = encrypt(data, e, n)
# dec = decrypt(en, d, n)
# self.assertEqual(data,dec)
if __name__ == "__main__":
unittest.main()