"""Tests for the 2-D and 3-D rotation helpers in :mod:`matrixmania.compute`.""" from __future__ import annotations import math import pytest from matrixmania.compute import rot_2d, rot_3D def assert_matrix_close(actual: list[list[float]], expected: list[list[float]]) -> None: """Assert that two matrices are numerically equal element by element.""" assert len(actual) == len(expected) for actual_row, expected_row in zip(actual, expected): assert len(actual_row) == len(expected_row) assert actual_row == pytest.approx(expected_row) class TestRot2D: """Tests for :func:`matrixmania.compute.rot_2d`.""" @pytest.mark.parametrize( "angle,expected", [ (0, [[1.0, 0.0], [0.0, 1.0]]), (90, [[0.0, -1.0], [1.0, 0.0]]), (180, [[-1.0, 0.0], [0.0, -1.0]]), (-90, [[0.0, 1.0], [-1.0, 0.0]]), ], ) def test_degrees(self, angle: float, expected: list[list[float]]) -> None: """Rotation matrices for angles given in degrees.""" assert_matrix_close(rot_2d(angle), expected) @pytest.mark.parametrize( "angle", [0.0, math.pi / 2, math.pi, -math.pi / 2], ) def test_radians(self, angle: float) -> None: """Radians and equivalent degrees yield the same matrix.""" assert_matrix_close( rot_2d(angle, unit="rad"), rot_2d(math.degrees(angle), unit="deg") ) def test_default_unit_is_degrees(self) -> None: """The default unit is degrees.""" assert_matrix_close(rot_2d(90), rot_2d(math.pi / 2, unit="rad")) class TestRot3D: """Tests for :func:`matrixmania.compute.rot_3D`.""" @pytest.mark.parametrize("axis", ["x", "y", "z"]) def test_zero_degrees_is_identity(self, axis: str) -> None: """A zero rotation yields the identity matrix for every axis.""" expected = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]] assert_matrix_close(rot_3D(0, axis), expected) def test_x_90_degrees(self) -> None: """Rotation of 90 degrees around the x axis.""" expected = [[1.0, 0.0, 0.0], [0.0, 0.0, -1.0], [0.0, 1.0, 0.0]] assert_matrix_close(rot_3D(90, "x"), expected) def test_y_90_degrees(self) -> None: """Rotation of 90 degrees around the y axis.""" expected = [[0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [-1.0, 0.0, 0.0]] assert_matrix_close(rot_3D(90, "y"), expected) def test_z_90_degrees(self) -> None: """Rotation of 90 degrees around the z axis.""" expected = [[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]] assert_matrix_close(rot_3D(90, "z"), expected) @pytest.mark.parametrize("axis", ["x", "y", "z"]) def test_radians_match_degrees(self, axis: str) -> None: """Radians and equivalent degrees yield the same matrix.""" angle_rad = math.pi / 2 assert_matrix_close( rot_3D(angle_rad, axis, unit="rad"), rot_3D(math.degrees(angle_rad), axis, unit="deg"), ) def test_default_unit_is_degrees(self) -> None: """The default unit is degrees.""" assert_matrix_close(rot_3D(90, "z"), rot_3D(math.pi / 2, "z", unit="rad")) @pytest.mark.parametrize("axis", ["a", "X", "", "xy"]) def test_invalid_axis_raises(self, axis: str) -> None: """An unsupported axis raises ``ValueError``.""" with pytest.raises(ValueError): rot_3D(90, axis)