forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_math.py
More file actions
118 lines (85 loc) · 3.08 KB
/
test_math.py
File metadata and controls
118 lines (85 loc) · 3.08 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
import unittest
from pygorithm.math import (
lcm,
lcm_using_gcd,
sieve_of_eratosthenes,
factorial,
conversion,
matrix_operations)
class TestLCM(unittest.TestCase):
def test_lcm(self):
self.assertEqual(lcm.lcm([3, 12, 16]), 48)
def test_lcm_using_gcd(self):
self.assertEqual(lcm_using_gcd.lcm_using_gcd([3, 12, 16]), 48)
class TestSieveOfEratosthenes(unittest.TestCase):
def test_sieve_of_eratosthenes(self):
self.assertEqual(sieve_of_eratosthenes.sieve_of_eratosthenes(11), [2, 3, 5, 7, 11])
class TestFactorial(unittest.TestCase):
def test_factorial(self):
self.assertEqual(factorial.factorial(10), 3628800)
class TestConversion(unittest.TestCase):
def test_dec_to_bin(self):
self.assertEqual(conversion.decimal_to_binary(2), '10')
def test_bin_to_dec(self):
self.assertEqual(conversion.binary_to_decimal('1010'), 10)
def test_dec_to_hex(self):
self.assertEqual(conversion.decimal_to_hex(30), '1E')
def test_hex_to_dex(self):
self.assertEqual(conversion.hex_to_decimal('1E'), 30)
class TestMatrixOperations(unittest.TestCase):
def test_matrix_addition(self):
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
matrix = matrix_operations.Matrix(X, Y)
self.assertEqual(matrix.add(), [[17, 15, 4], [10, 12, 9], [11, 13, 18]])
def test_matrix_subtraction(self):
X = [[12,7,3],
[4,5,6],
[7,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
matrix = matrix_operations.Matrix(X, Y)
self.assertEqual(matrix.subtract(), [[7, -1, 2], [-2, -2, 3], [3, 3, 0]])
def test_matrix_multiplication(self):
X = [[12,7,3],
[4,5,6],
[7,8,9]]
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
matrix = matrix_operations.Matrix(X, Y)
self.assertEqual(matrix.multiply(), [[114, 160, 60, 27], [74, 97, 73, 14], [119, 157, 112, 23]])
def test_matrix_transpose(self):
X = [[12,7],
[4 ,5],
[3 ,8]]
matrix = matrix_operations.Matrix(X)
self.assertEqual(matrix.transpose(), [[12, 4, 3],[7, 5, 8]])
def test_matrix_rotate(self):
X =[[1, 2, 3, 4 ],
[5, 6, 7, 8 ],
[9, 10, 11, 12 ],
[13, 14, 15, 16 ]]
matrix = matrix_operations.Matrix(X)
self.assertEqual(matrix.rotate(), [[5, 1, 2, 3], [9, 10, 6, 4], [13, 11, 7, 8], [14, 15, 16, 12]])
def test_matrix_unique_paths(self):
matrix = matrix_operations.Matrix()
self.assertEqual(matrix.count_unique_paths(3, 3), 6)
def test_matrix_exceptions(self):
X = [[12,7,3],
[4,5,6],
[7,8,9]]
Y = [[5,8],
[6,7],
[4,5]]
matrix = matrix_operations.Matrix(X, Y)
# test exception
self.assertRaises(Exception, matrix.add)
self.assertRaises(Exception, matrix.subtract)
if __name__ == '__main__':
unittest.main()