Files
matrixmania/test/test_matrixmania/test_compute.py
T
2026-10-22 16:53:53 +02:00

155 lines
4.6 KiB
Python

"""Unit-Tests für die mathematischen Matrixfunktionen in compute.py."""
import math
import pytest
from matrixmania.compute import matmul, rot_2d, rot_3d, test_functions, transpose
def test_matmul_valid() -> None:
"""Testet die Matrixmultiplikation mit gültigen Matrizen."""
a = [
[3.0, 4.0, -1.0, 4.0],
[-2.0, 2.0, 5.0, 1.0],
]
b = [
[1.0, 3.0, -2.0],
[2.0, 5.0, 1.0],
[-1.0, 4.0, -4.0],
[2.0, 3.0, 6.0],
]
expected = [
[20.0, 37.0, 26.0],
[-1.0, 27.0, -8.0],
]
assert matmul(a, b) == expected
def test_matmul_invalid_dimensions() -> None:
"""Testet, dass bei inkompatiblen Matrixdimensionen ValueError geworfen wird."""
a = [[1.0, 2.0], [3.0, 4.0]]
b = [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]
with pytest.raises(ValueError):
matmul(a, b)
def test_matmul_empty_or_uneven() -> None:
"""Testet matmul mit leeren oder ungleichmäßigen Matrizen."""
with pytest.raises(ValueError):
matmul([], [[1.0]])
with pytest.raises(ValueError):
matmul([[1.0, 2.0], [3.0]], [[1.0], [2.0]])
def test_transpose_valid() -> None:
"""Testet das Transponieren einer regulären Matrix."""
matrix = [
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
]
expected = [
[1.0, 4.0],
[2.0, 5.0],
[3.0, 6.0],
]
assert transpose(matrix) == expected
def test_transpose_invalid() -> None:
"""Testet, dass transpose bei leeren oder unregelmäßigen Matrizen Fehler wirft."""
with pytest.raises(ValueError):
transpose([])
with pytest.raises(ValueError):
transpose([[1.0, 2.0], [3.0]])
def test_rot_2d() -> None:
"""Testet die 2D-Rotation für verschiedene Winkel."""
rot_0 = rot_2d(0.0)
assert math.isclose(rot_0[0][0], 1.0, abs_tol=1e-9)
assert math.isclose(rot_0[0][1], 0.0, abs_tol=1e-9)
assert math.isclose(rot_0[1][0], 0.0, abs_tol=1e-9)
assert math.isclose(rot_0[1][1], 1.0, abs_tol=1e-9)
rot_90 = rot_2d(math.pi / 2)
assert math.isclose(rot_90[0][0], 0.0, abs_tol=1e-9)
assert math.isclose(rot_90[0][1], -1.0, abs_tol=1e-9)
assert math.isclose(rot_90[1][0], 1.0, abs_tol=1e-9)
assert math.isclose(rot_90[1][1], 0.0, abs_tol=1e-9)
def test_rot_3d_x_axis() -> None:
"""Testet die 3D-Rotation um die x-Achse."""
matrix_x_0 = rot_3d(0.0, "x")
expected_identity = [
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
]
for r in range(3):
for c in range(3):
assert math.isclose(matrix_x_0[r][c], expected_identity[r][c], abs_tol=1e-9)
matrix_x_90 = rot_3d(math.pi / 2, "x")
v_y = [[0.0], [1.0], [0.0]]
rotated = matmul(matrix_x_90, v_y)
assert math.isclose(rotated[0][0], 0.0, abs_tol=1e-9)
assert math.isclose(rotated[1][0], 0.0, abs_tol=1e-9)
assert math.isclose(rotated[2][0], 1.0, abs_tol=1e-9)
def test_rot_3d_y_axis() -> None:
"""Testet die 3D-Rotation um die y-Achse."""
matrix_y_0 = rot_3d(0.0, "y")
expected_identity = [
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
]
for r in range(3):
for c in range(3):
assert math.isclose(matrix_y_0[r][c], expected_identity[r][c], abs_tol=1e-9)
matrix_y_90 = rot_3d(math.pi / 2, "y")
v_z = [[0.0], [0.0], [1.0]]
rotated = matmul(matrix_y_90, v_z)
assert math.isclose(rotated[0][0], 1.0, abs_tol=1e-9)
assert math.isclose(rotated[1][0], 0.0, abs_tol=1e-9)
assert math.isclose(rotated[2][0], 0.0, abs_tol=1e-9)
def test_rot_3d_z_axis() -> None:
"""Testet die 3D-Rotation um die z-Achse."""
matrix_z_0 = rot_3d(0.0, "z")
expected_identity = [
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
]
for r in range(3):
for c in range(3):
assert math.isclose(matrix_z_0[r][c], expected_identity[r][c], abs_tol=1e-9)
matrix_z_90 = rot_3d(math.pi / 2, "z")
v_x = [[1.0], [0.0], [0.0]]
rotated = matmul(matrix_z_90, v_x)
assert math.isclose(rotated[0][0], 0.0, abs_tol=1e-9)
assert math.isclose(rotated[1][0], 1.0, abs_tol=1e-9)
assert math.isclose(rotated[2][0], 0.0, abs_tol=1e-9)
def test_rot_3d_invalid_axis() -> None:
"""Testet, dass ungültige Achsen einen ValueError auslösen."""
with pytest.raises(ValueError, match="Ungültige Achse"):
rot_3d(1.0, "w")
with pytest.raises(ValueError):
rot_3d(0.5, "123")
with pytest.raises(ValueError):
rot_3d(0.0, "")
def test_test_functions_execution() -> None:
"""Testet die Ausführung der integrierten Testfunktion."""
test_functions()