Files
matrixmania/test/test_matrixmania/test_rot.py
T

98 lines
2.0 KiB
Python

import math
import pytest
from matrixmania import matmul, rot_2D, rot_3D
def test_rot_2D_zero_angle_returns_identity():
# Arrange
angle = 0.0
expected = [[1.0, 0.0], [0.0, 1.0]]
# Act
result = rot_2D(angle)
# Assert
assert result == expected
def test_rot_2D_rotates_point_by_90_degrees():
# Arrange
angle = math.radians(90)
point = [[1], [0]]
expected = [0.0, 1.0]
# Act
result = [row[0] for row in matmul(rot_2D(angle), point)]
# Assert
assert result == pytest.approx(expected, abs=1e-9)
def test_rot_2D_rotates_point_by_180_degrees():
# Arrange
angle = math.pi
point = [[2], [1]]
expected = [-2.0, -1.0]
# Act
result = [row[0] for row in matmul(rot_2D(angle), point)]
# Assert
assert result == pytest.approx(expected, abs=1e-9)
@pytest.mark.parametrize("axis", ["x", "y", "z"])
def test_rot_3D_zero_angle_returns_identity(axis):
# Arrange
angle = 0.0
expected = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
# Act
result = rot_3D(angle, axis)
# Assert
assert result == expected
@pytest.mark.parametrize(
"axis, point, expected",
[
("x", [[0], [1], [0]], [0, 0, 1]),
("y", [[0], [0], [1]], [1, 0, 0]),
("z", [[1], [0], [0]], [0, 1, 0]),
],
)
def test_rot_3D_rotates_point_by_90_degrees(axis, point, expected):
# Arrange
angle = math.radians(90)
# Act
result = [row[0] for row in matmul(rot_3D(angle, axis), point)]
# Assert
assert result == pytest.approx(expected, abs=1e-9)
@pytest.mark.parametrize("axis", ["x", "y", "z"])
def test_rot_3D_returns_3x3_matrix(axis):
# Arrange
angle = 1.0
# Act
result = rot_3D(angle, axis)
# Assert
assert len(result) == 3
assert all(len(row) == 3 for row in result)
@pytest.mark.parametrize("invalid_axis", ["w", "", "X", "xy"])
def test_rot_3D_invalid_axis_raises_value_error(invalid_axis):
# Arrange
angle = 1.0
# Act / Assert
with pytest.raises(ValueError):
rot_3D(angle, invalid_axis)