added test directory; mit 3 testdateien

This commit is contained in:
Thomas Fischer 2025-11-06 16:51:37 +01:00
parent 3880163b92
commit 1afa47bbdb
3 changed files with 157 additions and 0 deletions

42
test/test_matmul.py Normal file
View File

@ -0,0 +1,42 @@
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()

39
test/test_rot.py Normal file
View File

@ -0,0 +1,39 @@
import unittest
from matrixmania.compute import rot_2D # Importiere die Funktion aus deiner compute.py
import math
class TestRot2D(unittest.TestCase):
# Setze eine Toleranz für Fließkommazahlen
TOLERANCE = 5
def test_zero_angle(self):
# Testfall: 0 Grad (0 Bogenmaß) Rotation -> Einheitsmatrix
expected = [[1.0, 0.0], [0.0, 1.0]]
result = rot_2D(0)
self.assertAlmostEqual(result[0][0], expected[0][0], self.TOLERANCE)
self.assertAlmostEqual(result[0][1], expected[0][1], self.TOLERANCE)
self.assertAlmostEqual(result[1][0], expected[1][0], self.TOLERANCE)
self.assertAlmostEqual(result[1][1], expected[1][1], self.TOLERANCE)
def test_ninety_degrees(self):
# Testfall: 90 Grad (pi/2 Bogenmaß) Rotation
# Erwartet: [[0, -1], [1, 0]]
expected = [[0.0, -1.0], [1.0, 0.0]]
result = rot_2D(math.pi / 2)
self.assertAlmostEqual(result[0][0], expected[0][0], self.TOLERANCE)
self.assertAlmostEqual(result[0][1], expected[0][1], self.TOLERANCE)
self.assertAlmostEqual(result[1][0], expected[1][0], self.TOLERANCE)
self.assertAlmostEqual(result[1][1], expected[1][1], self.TOLERANCE)
def test_one_eighty_degrees(self):
# Testfall: 180 Grad (pi Bogenmaß) Rotation
# Erwartet: [[-1, 0], [0, -1]]
expected = [[-1.0, 0.0], [0.0, -1.0]]
result = rot_2D(math.pi)
self.assertAlmostEqual(result[0][0], expected[0][0], self.TOLERANCE)
self.assertAlmostEqual(result[0][1], expected[0][1], self.TOLERANCE)
self.assertAlmostEqual(result[1][0], expected[1][0], self.TOLERANCE)
self.assertAlmostEqual(result[1][1], expected[1][1], self.TOLERANCE)
if __name__ == '__main__':
unittest.main()

76
test/test_transpose.py Normal file
View File

@ -0,0 +1,76 @@
import unittest
from matrixmania.compute import transpose # Anpassung an deine Paketstruktur
class TestTranspose(unittest.TestCase):
def test_square_matrix(self):
# Testfall: Standard 3x3 Quadratische Matrix
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
expected = [
[1, 4, 7],
[2, 5, 8],
[3, 6, 9]
]
self.assertEqual(transpose(matrix), expected, "Sollte quadratische Matrix korrekt transponieren")
def test_rectangular_matrix_2x3(self):
# Testfall: Rechteckige Matrix 2x3 -> 3x2
matrix = [
[10, 20, 30],
[40, 50, 60]
]
expected = [
[10, 40],
[20, 50],
[30, 60]
]
self.assertEqual(transpose(matrix), expected, "Sollte rechteckige 2x3 Matrix korrekt transponieren zu 3x2")
def test_rectangular_matrix_3x2(self):
# Testfall: Rechteckige Matrix 3x2 -> 2x3
matrix = [
[1, 2],
[3, 4],
[5, 6]
]
expected = [
[1, 3, 5],
[2, 4, 6]
]
self.assertEqual(transpose(matrix), expected, "Sollte rechteckige 3x2 Matrix korrekt transponieren zu 2x3")
def test_single_row(self):
# Testfall: Matrix mit nur einer Zeile (1xN)
matrix = [[1, 2, 3, 4]]
expected = [
[1],
[2],
[3],
[4]
]
self.assertEqual(transpose(matrix), expected, "Sollte eine 1xN Matrix zu einer Nx1 Matrix transponieren")
def test_empty_matrix_error(self):
# Testfall: Fehlerprüfung bei leerer Matrix
matrix = []
with self.assertRaises(ValueError) as context:
transpose(matrix)
self.assertTrue('matrix is empty' in str(context.exception), "Sollte ValueError bei leerer Matrix werfen")
def test_inconsistent_columns_error(self):
# Testfall: Fehlerprüfung bei ungleichmäßiger Spaltenanzahl (Ragged Matrix)
matrix = [
[1, 2, 3],
[4, 5], # Diese Zeile hat nur 2 Spalten
[7, 8, 9]
]
with self.assertRaises(ValueError) as context:
transpose(matrix)
self.assertTrue('Inconsistent number of columns' in str(context.exception), "Sollte ValueError bei ungleichmäßiger Spaltenanzahl werfen")
if __name__ == '__main__':
unittest.main()