Spaces:
Running
Running
File size: 3,189 Bytes
19abe39 e9b7141 | 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | import pytest
from env.paper_state import PaperState, UNIT_SQUARE_CORNERS
from env.graph import VERTEX_TOL
def test_single_crease_no_interior_vertices():
paper = PaperState()
result = paper.add_crease([0.0, 0.5], [1.0, 0.5], 'V')
assert result['valid'] is True
interior = paper.graph.interior_vertices()
assert interior == [], f"Expected no interior vertices, got {interior}"
def test_anchor_points_initial():
paper = PaperState()
anchors = paper.anchor_points()
for corner in UNIT_SQUARE_CORNERS:
assert any(
abs(ax - corner[0]) < VERTEX_TOL and abs(ay - corner[1]) < VERTEX_TOL
for ax, ay in anchors
), f"Corner {corner} not found in anchor_points"
def test_anchor_points_grow():
paper = PaperState()
result = paper.add_crease([0.0, 0.5], [1.0, 0.5], 'V')
assert result['valid'] is True
anchors = paper.anchor_points()
def has_point(px, py):
return any(abs(ax - px) < VERTEX_TOL and abs(ay - py) < VERTEX_TOL for ax, ay in anchors)
assert has_point(0.0, 0.5), "(0, 0.5) should be in anchor_points after crease"
assert has_point(1.0, 0.5), "(1, 0.5) should be in anchor_points after crease"
def test_invalid_assignment():
paper = PaperState()
result = paper.add_crease([0.0, 0.5], [1.0, 0.5], 'X')
assert result['valid'] is False
assert 'invalid_assignment' in result['errors']
def test_fold_history():
paper = PaperState()
paper.add_crease([0.0, 0.5], [1.0, 0.5], 'M')
assert len(paper.fold_history) == 1
def test_unanchored_returns_false_anchored():
paper = PaperState()
result = paper.add_crease([0.3, 0.3], [0.7, 0.7], 'M')
assert result['anchored'] is False
def test_crease_edges_returned():
paper = PaperState()
paper.add_crease([0.0, 0.5], [1.0, 0.5], 'M')
edges = paper.crease_edges()
assert len(edges) >= 1
for e in edges:
assert e['assignment'] in ('M', 'V')
assert 'v1' in e
assert 'v2' in e
def test_two_intersecting_creases():
paper = PaperState()
r1 = paper.add_crease([0.0, 0.5], [1.0, 0.5], 'M')
r2 = paper.add_crease([0.5, 0.0], [0.5, 1.0], 'V')
assert r1['valid'] is True
assert r2['valid'] is True
interior = paper.graph.interior_vertices()
assert len(interior) >= 1
coords = [paper.graph.vertices[vid] for vid in interior]
assert any(abs(x - 0.5) < VERTEX_TOL and abs(y - 0.5) < VERTEX_TOL for x, y in coords)
def test_duplicate_crease_detection():
"""Adding the same crease twice → first duplicate=False, second duplicate=True."""
paper = PaperState()
r1 = paper.add_crease([0.0, 0.5], [1.0, 0.5], 'M')
assert r1['valid'] is True
assert r1['duplicate'] is False
r2 = paper.add_crease([0.0, 0.5], [1.0, 0.5], 'V') # same endpoints, different assignment
assert r2['valid'] is True
assert r2['duplicate'] is True
def test_error_returns_have_duplicate_false():
"""Error return paths include duplicate=False."""
paper = PaperState()
r_invalid = paper.add_crease([0.0, 0.5], [1.0, 0.5], 'X')
assert r_invalid['valid'] is False
assert r_invalid['duplicate'] is False
|