neues Verzeichnis geöffnet für wuerfwl, in dieses, game und compute kopiert, eine main Funktion erstellt, cube_game erstellt, test_rot in d "Test" Verzeichnis verschoben
121 lines
3.2 KiB
Python
121 lines
3.2 KiB
Python
"""Machine tests for the rotation functions in compute.py.
|
|
|
|
Every test is structured according to the
|
|
Arrange - Act - Assert pattern.
|
|
"""
|
|
|
|
from math import pi, sqrt
|
|
|
|
import pytest
|
|
|
|
from compute import matmul, rot_2D, rot_3D
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# rot_2D
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_rot_2D_quarter_turn() -> None:
|
|
"""rot_2D(pi/2) maps the unit vector (1, 0) onto (0, 1)."""
|
|
# Arrange
|
|
expected = [[0.0, -1.0], [1.0, 0.0]]
|
|
|
|
# Act
|
|
matrix = rot_2D(pi / 2)
|
|
|
|
# Assert
|
|
for row, exp_row in zip(matrix, expected):
|
|
for value, exp_value in zip(row, exp_row):
|
|
assert value == pytest.approx(exp_value)
|
|
|
|
|
|
def test_rot_2D_full_turn_is_identity() -> None:
|
|
"""rot_2D(2*pi) is the identity matrix."""
|
|
# Arrange
|
|
expected = [[1.0, 0.0], [0.0, 1.0]]
|
|
|
|
# Act
|
|
matrix = rot_2D(2 * pi)
|
|
|
|
# Assert
|
|
for row, exp_row in zip(matrix, expected):
|
|
for value, exp_value in zip(row, exp_row):
|
|
assert value == pytest.approx(exp_value)
|
|
|
|
|
|
def test_rot_2D_known_values_45_degrees() -> None:
|
|
"""rot_2D(pi/4) has the well-known sqrt(2)/2 entries."""
|
|
# Arrange
|
|
s = sqrt(2) / 2
|
|
expected = [[s, -s], [s, s]]
|
|
|
|
# Act
|
|
matrix = rot_2D(pi / 4)
|
|
|
|
# Assert
|
|
for row, exp_row in zip(matrix, expected):
|
|
for value, exp_value in zip(row, exp_row):
|
|
assert value == pytest.approx(exp_value)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# rot_3D
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_rot_3D_z_axis_quarter_turn() -> None:
|
|
"""rot_3D(pi/2, 'z') rotates (1, 0, 0) onto (0, 1, 0)."""
|
|
# Arrange
|
|
vector = [[1.0], [0.0], [0.0]] # column vector
|
|
expected = [[0.0], [1.0], [0.0]]
|
|
|
|
# Act
|
|
matrix = rot_3D(pi / 2, "z")
|
|
result = matmul(matrix, vector)
|
|
|
|
# Assert
|
|
for row, exp_row in zip(result, expected):
|
|
for value, exp_value in zip(row, exp_row):
|
|
assert value == pytest.approx(exp_value)
|
|
|
|
|
|
def test_rot_3D_x_axis_45_degrees() -> None:
|
|
"""rot_3D(pi/4, 'x') has the well-known sqrt(2)/2 entries."""
|
|
# Arrange
|
|
s = sqrt(2) / 2
|
|
expected = [[1.0, 0.0, 0.0],
|
|
[0.0, s, -s],
|
|
[0.0, s, s]]
|
|
|
|
# Act
|
|
matrix = rot_3D(pi / 4, "x")
|
|
|
|
# Assert
|
|
for row, exp_row in zip(matrix, expected):
|
|
for value, exp_value in zip(row, exp_row):
|
|
assert value == pytest.approx(exp_value)
|
|
|
|
|
|
def test_rot_3D_y_axis_keeps_y_axis() -> None:
|
|
"""rot_3D(angle, 'y') leaves the y-axis unchanged."""
|
|
# Arrange
|
|
vector = [[0.0], [1.0], [0.0]] # y-axis as column vector
|
|
expected = [[0.0], [1.0], [0.0]]
|
|
|
|
# Act
|
|
matrix = rot_3D(1.234, "y") # arbitrary angle
|
|
result = matmul(matrix, vector)
|
|
|
|
# Assert
|
|
for row, exp_row in zip(result, expected):
|
|
for value, exp_value in zip(row, exp_row):
|
|
assert value == pytest.approx(exp_value)
|
|
|
|
|
|
def test_rot_3D_invalid_axis_raises() -> None:
|
|
"""rot_3D raises a ValueError for an unknown axis."""
|
|
# Arrange
|
|
bad_axis = "a"
|
|
|
|
# Act / Assert
|
|
with pytest.raises(ValueError):
|
|
rot_3D(pi / 2, bad_axis) |