-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_grid_cell.py
More file actions
93 lines (75 loc) · 3.07 KB
/
test_grid_cell.py
File metadata and controls
93 lines (75 loc) · 3.07 KB
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
"""Test cases for the GridCell model."""
import pandas as pd
import pytest
from graphomotor.core import models
@pytest.fixture
def cell() -> models.GridCell:
"""Create a grid cell representing one letter region."""
return models.GridCell(
x_min=10.0, x_max=25.0, y_min=80.0, y_max=97.0, index=0, label="A"
)
@pytest.mark.parametrize(
"x_min,x_max,y_min,y_max,expected_error",
[
(10.0, 5.0, 0.0, 1.0, "x_min .* must be less than x_max"),
(5.0, 5.0, 0.0, 1.0, "x_min .* must be less than x_max"),
(0.0, 1.0, 10.0, 5.0, "y_min .* must be less than y_max"),
(0.0, 1.0, 5.0, 5.0, "y_min .* must be less than y_max"),
],
)
def test_grid_cell_invalid_bounds(
x_min: float,
x_max: float,
y_min: float,
y_max: float,
expected_error: str,
) -> None:
"""Test that invalid or equal bounds raise ValueError."""
with pytest.raises(ValueError, match=expected_error):
models.GridCell(x_min=x_min, x_max=x_max, y_min=y_min, y_max=y_max)
def test_stroke_centroid_inside_cell(cell: models.GridCell) -> None:
"""Stroke whose centroid falls inside the cell should be contained."""
stroke_points = pd.DataFrame({"x": [15.0, 20.0, 17.0], "y": [85.0, 90.0, 95.0]})
assert cell.contains_points(stroke_points)
def test_stroke_centroid_outside_cell(cell: models.GridCell) -> None:
"""Stroke whose centroid falls outside the cell should not be contained."""
stroke_points = pd.DataFrame({"x": [33.8, 37.8, 35.3], "y": [85.8, 95.4, 90.1]})
assert not cell.contains_points(stroke_points)
@pytest.mark.parametrize(
"x_vals,y_vals",
[
([10.0, 10.0], [88.0, 89.0]),
([17.0, 18.0], [80.0, 80.0]),
],
ids=["left_boundary", "bottom_boundary"],
)
def test_stroke_centroid_on_lower_boundary(
cell: models.GridCell, x_vals: list[float], y_vals: list[float]
) -> None:
"""Stroke whose centroid lands on the lower/left boundary (min) is included."""
stroke_points = pd.DataFrame({"x": x_vals, "y": y_vals})
assert cell.contains_points(stroke_points)
@pytest.mark.parametrize(
"x_vals,y_vals",
[
([25.0, 25.0], [88.0, 89.0]),
([17.0, 18.0], [97.0, 97.0]),
],
ids=["right_boundary", "top_boundary"],
)
def test_stroke_centroid_on_upper_boundary(
cell: models.GridCell, x_vals: list[float], y_vals: list[float]
) -> None:
"""Stroke whose centroid lands on the upper/right boundary (max) is excluded."""
stroke_points = pd.DataFrame({"x": x_vals, "y": y_vals})
assert not cell.contains_points(stroke_points)
def test_stroke_points_span_outside_but_centroid_inside(
cell: models.GridCell,
) -> None:
"""Stroke with points outside the cell but centroid inside should be contained."""
stroke_points = pd.DataFrame({"x": [8.0, 22.0], "y": [78.0, 98.0]})
assert cell.contains_points(stroke_points)
def test_single_point_stroke(cell: models.GridCell) -> None:
"""Single-point stroke should use that point as its centroid."""
stroke_points = pd.DataFrame({"x": [17.5], "y": [90.0]})
assert cell.contains_points(stroke_points)