masque/masque/test/test_utils.py

170 lines
5.3 KiB
Python
Raw Normal View History

from pathlib import Path
import numpy
from numpy.testing import assert_equal, assert_allclose
from numpy import pi
2026-04-01 21:16:03 -07:00
import pytest
from ..utils import remove_duplicate_vertices, remove_colinear_vertices, poly_contains_points, rotation_matrix_2d, apply_transforms, DeferredDict
from ..file.utils import tmpfile
2026-04-01 21:16:03 -07:00
from ..utils.curves import bezier
from ..error import PatternError
2026-02-15 12:36:13 -08:00
def test_remove_duplicate_vertices() -> None:
# Closed path (default)
v = [[0, 0], [1, 1], [1, 1], [2, 2], [0, 0]]
v_clean = remove_duplicate_vertices(v, closed_path=True)
# The last [0,0] is a duplicate of the first [0,0] if closed_path=True
assert_equal(v_clean, [[0, 0], [1, 1], [2, 2]])
2026-02-15 12:36:13 -08:00
# Open path
v_clean_open = remove_duplicate_vertices(v, closed_path=False)
assert_equal(v_clean_open, [[0, 0], [1, 1], [2, 2], [0, 0]])
2026-02-15 12:36:13 -08:00
def test_remove_colinear_vertices() -> None:
v = [[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [1, 1], [0, 0]]
v_clean = remove_colinear_vertices(v, closed_path=True)
# [1, 0] is between [0, 0] and [2, 0]
# [2, 1] is between [2, 0] and [2, 2]
# [1, 1] is between [2, 2] and [0, 0]
assert_equal(v_clean, [[0, 0], [2, 0], [2, 2]])
2026-02-15 12:36:13 -08:00
def test_remove_colinear_vertices_exhaustive() -> None:
# U-turn
v = [[0, 0], [10, 0], [0, 0]]
v_clean = remove_colinear_vertices(v, closed_path=False, preserve_uturns=True)
2026-02-15 12:36:13 -08:00
# Open path should keep ends. [10,0] is between [0,0] and [0,0]?
# They are colinear, but it's a 180 degree turn.
# We preserve 180 degree turns if preserve_uturns is True.
assert len(v_clean) == 3
v_collapsed = remove_colinear_vertices(v, closed_path=False, preserve_uturns=False)
# If not preserving u-turns, it should collapse to just the endpoints
assert len(v_collapsed) == 2
2026-02-15 12:36:13 -08:00
# 180 degree U-turn in closed path
v = [[0, 0], [10, 0], [5, 0]]
v_clean = remove_colinear_vertices(v, closed_path=True, preserve_uturns=False)
2026-02-15 12:36:13 -08:00
assert len(v_clean) == 2
2026-02-15 12:36:13 -08:00
def test_poly_contains_points() -> None:
v = [[0, 0], [10, 0], [10, 10], [0, 10]]
pts = [[5, 5], [-1, -1], [10, 10], [11, 5]]
inside = poly_contains_points(v, pts)
assert_equal(inside, [True, False, True, False])
2026-02-15 12:36:13 -08:00
def test_rotation_matrix_2d() -> None:
m = rotation_matrix_2d(pi / 2)
assert_allclose(m, [[0, -1], [1, 0]], atol=1e-10)
2026-02-15 12:36:13 -08:00
def test_rotation_matrix_non_manhattan() -> None:
# 45 degrees
2026-02-15 12:36:13 -08:00
m = rotation_matrix_2d(pi / 4)
s = numpy.sqrt(2) / 2
assert_allclose(m, [[s, -s], [s, s]], atol=1e-10)
2026-02-15 12:36:13 -08:00
def test_apply_transforms() -> None:
# cumulative [x_offset, y_offset, rotation (rad), mirror_x (0 or 1)]
t1 = [10, 20, 0, 0]
t2 = [[5, 0, 0, 0], [0, 5, 0, 0]]
combined = apply_transforms(t1, t2)
2026-03-09 02:35:35 -07:00
assert_equal(combined, [[15, 20, 0, 0, 1], [10, 25, 0, 0, 1]])
2026-02-15 12:36:13 -08:00
def test_apply_transforms_advanced() -> None:
# Ox4: (x, y, rot, mir)
# Outer: mirror x (axis 0), then rotate 90 deg CCW
# apply_transforms logic for mirror uses y *= -1 (which is axis 0 mirror)
2026-02-15 12:36:13 -08:00
outer = [0, 0, pi / 2, 1]
# Inner: (10, 0, 0, 0)
inner = [10, 0, 0, 0]
2026-02-15 12:36:13 -08:00
combined = apply_transforms(outer, inner)
# 1. mirror inner y if outer mirrored: (10, 0) -> (10, 0)
# 2. rotate by outer rotation (pi/2): (10, 0) -> (0, 10)
# 3. add outer offset (0, 0) -> (0, 10)
2026-03-09 02:35:35 -07:00
assert_allclose(combined[0], [0, 10, pi / 2, 1, 1], atol=1e-10)
2026-04-01 21:16:03 -07:00
def test_bezier_validates_weight_length() -> None:
with pytest.raises(PatternError, match='one entry per control point'):
bezier([[0, 0], [1, 1]], [0, 0.5, 1], weights=[1])
with pytest.raises(PatternError, match='one entry per control point'):
bezier([[0, 0], [1, 1]], [0, 0.5, 1], weights=[1, 2, 3])
def test_bezier_accepts_exact_weight_count() -> None:
samples = bezier([[0, 0], [1, 1]], [0, 0.5, 1], weights=[1, 2])
assert_allclose(samples, [[0, 0], [2 / 3, 2 / 3], [1, 1]], atol=1e-10)
def test_deferred_dict_accessors_resolve_values_once() -> None:
calls = 0
def make_value() -> int:
nonlocal calls
calls += 1
return 7
deferred = DeferredDict[str, int]()
deferred["x"] = make_value
assert deferred.get("missing", 9) == 9
assert deferred.get("x") == 7
assert list(deferred.values()) == [7]
assert list(deferred.items()) == [("x", 7)]
assert calls == 1
def test_deferred_dict_mutating_accessors_preserve_value_semantics() -> None:
calls = 0
def make_value() -> int:
nonlocal calls
calls += 1
return 7
deferred = DeferredDict[str, int]()
assert deferred.setdefault("x", 5) == 5
assert deferred["x"] == 5
assert deferred.setdefault("y", make_value) == 7
assert deferred["y"] == 7
assert calls == 1
copy_deferred = deferred.copy()
assert isinstance(copy_deferred, DeferredDict)
assert copy_deferred["x"] == 5
assert copy_deferred["y"] == 7
assert calls == 1
assert deferred.pop("x") == 5
assert deferred.pop("missing", 9) == 9
assert deferred.popitem() == ("y", 7)
def test_tmpfile_cleans_up_on_exception(tmp_path: Path) -> None:
target = tmp_path / "out.txt"
temp_path = None
try:
with tmpfile(target) as stream:
temp_path = Path(stream.name)
stream.write(b"hello")
raise RuntimeError("boom")
except RuntimeError:
pass
assert temp_path is not None
assert not target.exists()
assert not temp_path.exists()