103 lines
2.4 KiB
Python
103 lines
2.4 KiB
Python
import math
|
|
|
|
import pytest
|
|
|
|
from matrixmania import rot_2D, rot_3D
|
|
|
|
|
|
def test_rot_2d_quarter_turn() -> None:
|
|
"""A quarter turn rotates a 2D point counterclockwise."""
|
|
# Arrange
|
|
angle = math.pi / 2
|
|
|
|
# Act
|
|
result = rot_2D(angle)
|
|
|
|
# Assert
|
|
assert result[0] == pytest.approx([0.0, -1.0])
|
|
assert result[1] == pytest.approx([1.0, 0.0])
|
|
|
|
|
|
def test_rot_3d_around_x_axis() -> None:
|
|
"""A quarter turn returns the expected matrix around x."""
|
|
# Arrange
|
|
angle = math.pi / 2
|
|
expected = [[1.0, 0.0, 0.0], [0.0, 0.0, -1.0], [0.0, 1.0, 0.0]]
|
|
|
|
# Act
|
|
result = rot_3D(angle, "x")
|
|
|
|
# Assert
|
|
for actual_row, expected_row in zip(result, expected):
|
|
assert actual_row == pytest.approx(expected_row)
|
|
|
|
|
|
def test_rot_3d_around_y_axis() -> None:
|
|
"""A quarter turn returns the expected matrix around y."""
|
|
# Arrange
|
|
angle = math.pi / 2
|
|
expected = [[0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [-1.0, 0.0, 0.0]]
|
|
|
|
# Act
|
|
result = rot_3D(angle, "y")
|
|
|
|
# Assert
|
|
for actual_row, expected_row in zip(result, expected):
|
|
assert actual_row == pytest.approx(expected_row)
|
|
|
|
|
|
def test_rot_3d_around_z_axis() -> None:
|
|
"""A quarter turn returns the expected matrix around z."""
|
|
# Arrange
|
|
angle = math.pi / 2
|
|
expected = [[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]
|
|
|
|
# Act
|
|
result = rot_3D(angle, "z")
|
|
|
|
# Assert
|
|
for actual_row, expected_row in zip(result, expected):
|
|
assert actual_row == pytest.approx(expected_row)
|
|
|
|
|
|
def test_rot_3d_rejects_empty_axis() -> None:
|
|
"""An empty axis is invalid."""
|
|
# Arrange
|
|
angle = math.pi / 2
|
|
axis = ""
|
|
|
|
# Act
|
|
with pytest.raises(ValueError) as error:
|
|
rot_3D(angle, axis)
|
|
|
|
# Assert
|
|
assert str(error.value) == "axis must be 'x', 'y', or 'z'"
|
|
|
|
|
|
def test_rot_3d_rejects_unknown_axis() -> None:
|
|
"""An unknown axis is invalid."""
|
|
# Arrange
|
|
angle = math.pi / 2
|
|
axis = "a"
|
|
|
|
# Act
|
|
with pytest.raises(ValueError) as error:
|
|
rot_3D(angle, axis)
|
|
|
|
# Assert
|
|
assert str(error.value) == "axis must be 'x', 'y', or 'z'"
|
|
|
|
|
|
def test_rot_3d_rejects_uppercase_axis() -> None:
|
|
"""Axis values must be lowercase."""
|
|
# Arrange
|
|
angle = math.pi / 2
|
|
axis = "X"
|
|
|
|
# Act
|
|
with pytest.raises(ValueError) as error:
|
|
rot_3D(angle, axis)
|
|
|
|
# Assert
|
|
assert str(error.value) == "axis must be 'x', 'y', or 'z'"
|