Skip to content

Commit 78e39b6

Browse files
committed
Added tests for matrix operations
1 parent 74a8081 commit 78e39b6

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

tests/test_math.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
lcm_using_gcd,
66
sieve_of_eratosthenes,
77
factorial,
8-
conversion)
8+
conversion,
9+
matrix_operations)
910

1011
class TestLCM(unittest.TestCase):
1112
def test_lcm(self):
@@ -35,5 +36,83 @@ def test_dec_to_hex(self):
3536
def test_hex_to_dex(self):
3637
self.assertEqual(conversion.hex_to_decimal('1E'), 30)
3738

39+
class TestMatrixOperations(unittest.TestCase):
40+
def test_matrix_addition(self):
41+
X = [[12,7,3],
42+
[4 ,5,6],
43+
[7 ,8,9]]
44+
45+
Y = [[5,8,1],
46+
[6,7,3],
47+
[4,5,9]]
48+
49+
matrix = matrix_operations.Matrix(X, Y)
50+
self.assertEqual(matrix.add(), [[17, 15, 4], [10, 12, 9], [11, 13, 18]])
51+
52+
53+
def test_matrix_subtraction(self):
54+
X = [[12,7,3],
55+
[4,5,6],
56+
[7,8,9]]
57+
58+
Y = [[5,8,1],
59+
[6,7,3],
60+
[4,5,9]]
61+
62+
matrix = matrix_operations.Matrix(X, Y)
63+
self.assertEqual(matrix.subtract(), [[7, -1, 2], [-2, -2, 3], [3, 3, 0]])
64+
65+
66+
def test_matrix_multiplication(self):
67+
X = [[12,7,3],
68+
[4,5,6],
69+
[7,8,9]]
70+
71+
Y = [[5,8,1,2],
72+
[6,7,3,0],
73+
[4,5,9,1]]
74+
75+
matrix = matrix_operations.Matrix(X, Y)
76+
self.assertEqual(matrix.multiply(), [[114, 160, 60, 27], [74, 97, 73, 14], [119, 157, 112, 23]])
77+
78+
79+
def test_matrix_transpose(self):
80+
X = [[12,7],
81+
[4 ,5],
82+
[3 ,8]]
83+
84+
matrix = matrix_operations.Matrix(X)
85+
self.assertEqual(matrix.transpose(), [[12, 4, 3],[7, 5, 8]])
86+
87+
88+
def test_matrix_rotate(self):
89+
X =[[1, 2, 3, 4 ],
90+
[5, 6, 7, 8 ],
91+
[9, 10, 11, 12 ],
92+
[13, 14, 15, 16 ]]
93+
94+
matrix = matrix_operations.Matrix(X)
95+
self.assertEqual(matrix.rotate(), [[5, 1, 2, 3], [9, 10, 6, 4], [13, 11, 7, 8], [14, 15, 16, 12]])
96+
97+
98+
def test_matrix_unique_paths(self):
99+
matrix = matrix_operations.Matrix()
100+
self.assertEqual(matrix.count_unique_paths(3, 3), 6)
101+
102+
def test_matrix_exceptions(self):
103+
X = [[12,7,3],
104+
[4,5,6],
105+
[7,8,9]]
106+
107+
Y = [[5,8],
108+
[6,7],
109+
[4,5]]
110+
111+
matrix = matrix_operations.Matrix(X, Y)
112+
113+
# test exception
114+
self.assertRaises(Exception, matrix.add)
115+
self.assertRaises(Exception, matrix.subtract)
116+
38117
if __name__ == '__main__':
39118
unittest.main()

0 commit comments

Comments
 (0)