File size: 1,772 Bytes
19faf57 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
"""Unit tests exercising the new alphageometry modules (utils + visualizer).
These tests are intentionally small and dependency-free so they run in CI and
local developer environments without extra setup.
"""
from __future__ import annotations
import tempfile
from alphageometry.modules import utils, visualizer
def test_polygon_area_and_triangulation():
# simple unit square
square = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]
area = utils.polygon_area(square)
assert abs(area - 1.0) < 1e-12
tris = utils.earclip_triangulate(square)
# triangulation of quad -> 2 triangles
assert len(tris) == 2
# triangles should use indices from 0..3
for t in tris:
assert all(0 <= i < 4 for i in t)
def test_rdp_simplify():
# stair-step polyline: many points but simplifiable
pts = [(float(i), 0.0) for i in range(20)]
simplified = utils.rdp_simplify(pts, epsilon=0.5)
assert len(simplified) < len(pts)
assert simplified[0] == pts[0]
assert simplified[-1] == pts[-1]
def test_visualizer_svg_and_json_roundtrip():
tri_verts = [(0.0, 0.0), (1.0, 0.0), (0.5, 0.8)]
triangles = [(0, 1, 2)]
svg = visualizer.mesh_to_svg(tri_verts, triangles)
assert svg.startswith("<svg")
j = visualizer.mesh_to_json(tri_verts, triangles)
assert "vertices" in j and "triangles" in j
def test_visualizer_writes_svg_file(tmp_path):
square = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]
svg = visualizer.polygon_to_svg(square)
out = tmp_path / "out.svg"
out.write_text(svg, encoding="utf-8")
# ensure file exists and contains svg markup
txt = out.read_text(encoding="utf-8")
assert "<svg" in txt and "polygon" in txt
|