Files
matrixmania/test/test_matrixmania/test_matmul.py
T
2026-10-22 23:56:08 +02:00

86 lines
2.2 KiB
Python

"""Unit-Tests für die mathematischen Matrixfunktionen matmul und transpose."""
import pytest
from matrixmania.compute import matmul, 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_identity() -> None:
"""Testet die Matrixmultiplikation mit der Einheitsmatrix."""
a = [
[1.0, 2.0],
[3.0, 4.0],
]
identity = [
[1.0, 0.0],
[0.0, 1.0],
]
assert matmul(a, identity) == a
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]], [])
with pytest.raises(ValueError):
matmul([[]], [[1.0]])
with pytest.raises(ValueError):
matmul([[1.0]], [[]])
with pytest.raises(ValueError):
matmul([[1.0, 2.0], [3.0]], [[1.0], [2.0]])
with pytest.raises(ValueError):
matmul([[1.0, 2.0], [3.0, 4.0]], [[1.0], [2.0, 3.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([[]])
with pytest.raises(ValueError):
transpose([[1.0, 2.0], [3.0]])