MatrixMania/test/test_matmul.py

42 lines
1.9 KiB
Python

import unittest
from matrixmania.compute import matmul # Importiere die Funktion aus deiner compute.py
import math
class TestMatmul(unittest.TestCase):
def test_standard_multiplication(self):
# Testfall: Standard 2x2 * 2x2 Multiplikation
mA = [[1, 2], [3, 4]]
mB = [[5, 6], [7, 8]]
# Erwartetes Ergebnis: [[1*5+2*7, 1*6+2*8], [3*5+4*7, 3*6+4*8]]
# = [[5+14, 6+16], [15+28, 18+32]] = [[19, 22], [43, 50]]
expected = [[19, 22], [43, 50]]
self.assertEqual(matmul(mA, mB), expected, "Sollte Standard 2x2 Multiplikation korrekt durchführen")
def test_multiplication_with_identity(self):
# Testfall: Matrix * Einheitsmatrix = Matrix selbst
mA = [[10, 20], [30, 40]]
mB = [[1, 0], [0, 1]]
expected = [[10, 20], [30, 40]]
self.assertEqual(matmul(mA, mB), expected, "Sollte Multiplikation mit Einheitsmatrix korrekt durchführen")
def test_multiplication_2x3_3x2(self):
# Testfall: Beispiel mit unterschiedlichen Dimensionen
mA = [[1, 2, 3], [4, 5, 6]] # 2x3
mB = [[7, 8], [9, 10], [11, 12]] # 3x2
# Erwartetes Ergebnis: 2x2 Matrix
# [[1*7+2*9+3*11, 1*8+2*10+3*12], [4*7+5*9+6*11, 4*8+5*10+6*12]]
# = [[7+18+33, 8+20+36], [28+45+66, 32+50+72]] = [[58, 64], [139, 154]]
expected = [[58, 64], [139, 154]]
self.assertEqual(matmul(mA, mB), expected, "Sollte 2x3 * 3x2 Multiplikation korrekt durchführen")
def test_dimension_mismatch_error(self):
# Testfall: Prüfung der ValueError-Ausnahme bei falscher Dimension
mA = [[1, 2], [3, 4]] # 2x2
mB = [[5], [6], [7]] # 3x1 -> 2 != 3
with self.assertRaises(ValueError) as context:
matmul(mA, mB)
self.assertTrue('Mismatch of Width of A and Height of B' in str(context.exception))
if __name__ == '__main__':
unittest.main()