54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
import math
|
||
|
||
import pytest
|
||
|
||
from matrixmania.compute import matmul, rot_2D, rot_3D
|
||
|
||
|
||
def assert_matrix(actual: list[list[float]], expected: list[list[float]]) -> None:
|
||
"""Compares two matrices element‑wise with a float tolerance."""
|
||
assert len(actual) == len(expected)
|
||
for actual_row, expected_row in zip(actual, expected, strict=True):
|
||
assert actual_row == pytest.approx(expected_row)
|
||
|
||
|
||
def test_rot_2D_identity() -> None:
|
||
assert_matrix(rot_2D(0.0), [[1.0, 0.0], [0.0, 1.0]])
|
||
|
||
|
||
def test_rot_3D_x_90_degrees() -> None:
|
||
matrix = rot_3D(math.pi / 2, "x")
|
||
assert_matrix(matrix, [[1.0, 0.0, 0.0], [0.0, 0.0, -1.0], [0.0, 1.0, 0.0]])
|
||
|
||
|
||
def test_rot_3D_y_90_degrees() -> None:
|
||
matrix = rot_3D(math.pi / 2, "y")
|
||
assert_matrix(matrix, [[0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [-1.0, 0.0, 0.0]])
|
||
|
||
|
||
def test_rot_3D_z_90_degrees() -> None:
|
||
matrix = rot_3D(math.pi / 2, "z")
|
||
assert_matrix(matrix, [[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]])
|
||
|
||
|
||
def test_rot_3D_zero_angle_is_identity() -> None:
|
||
identity = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
|
||
for axis in ("x", "y", "z"):
|
||
assert_matrix(rot_3D(0.0, axis), identity)
|
||
|
||
|
||
def test_rot_3D_rotates_vector() -> None:
|
||
rotated = matmul(rot_3D(math.pi / 2, "z"), [[1.0], [0.0], [0.0]])
|
||
assert_matrix(rotated, [[0.0], [1.0], [0.0]])
|
||
|
||
|
||
def test_rot_3D_invalid_axis() -> None:
|
||
for axis in ("w", "", "X", 1, None):
|
||
with pytest.raises(ValueError):
|
||
rot_3D(1.0, axis)
|
||
|
||
|
||
def test_rot_3D_invalid_angle() -> None:
|
||
with pytest.raises(ValueError):
|
||
rot_3D("not a number", "z")
|