Axiovora-X / tests /test_alphageometry_modules.py
ZAIDX11's picture
Add files using upload-large-folder tool
19faf57 verified
"""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