-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_geometry.py
More file actions
149 lines (107 loc) · 4.23 KB
/
test_geometry.py
File metadata and controls
149 lines (107 loc) · 4.23 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import pytest
from hypothesis import given, strategies as st
from pygexml.strategies import *
from pygexml.geometry import *
############## Tests for Point #####################
@given(st.integers(min_value=0), st.integers(min_value=0))
def test_point_construction(x: int, y: int) -> None:
point = Point(x=x, y=y)
assert point.x == x
assert point.y == y
@given(st.integers(min_value=0), st.integers(min_value=0))
def test_point_stringification(x: int, y: int) -> None:
point = Point(x=x, y=y)
assert str(point) == f"{x},{y}"
############## Tests for Box ####################
def test_box_simple_example() -> None:
p17 = Point(17, 17)
p42 = Point(42, 42)
box = Box(top_left=p17, bottom_right=p42)
assert box.top_left == p17
assert box.bottom_right == p42
assert box.width() == 25
assert box.height() == 25
@given(st_box_points())
def test_box_construction(pp: tuple[Point, Point]) -> None:
tl, br = pp
box = Box(top_left=tl, bottom_right=br)
assert box.top_left == tl
assert box.bottom_right == br
@given(st_box_points())
def test_box_construction_exception(pp: tuple[Point, Point]) -> None:
tl, br = pp
with pytest.raises(Exception, match="top left is not top left"):
Box(top_left=br, bottom_right=tl) # flipped points!
def test_box_construction_width_height_example() -> None:
tl = Point(17, 17)
box = Box.from_top_left_width_height(top_left=tl, width=25, height=25)
assert box.top_left == tl
assert box.bottom_right == Point(42, 42)
@given(st_box_points())
def test_box_construction_width_height(pp: tuple[Point, Point]) -> None:
tl, br = pp
box = Box.from_top_left_width_height(
top_left=tl, width=br.x - tl.x, height=br.y - tl.y
)
assert box.top_left == tl
assert box.bottom_right == br
@given(st_boxes)
def test_box_width(box: Box) -> None:
assert box.width() == abs(box.bottom_right.x - box.top_left.x)
@given(st_boxes)
def test_box_height(box: Box) -> None:
assert box.height() == abs(box.bottom_right.y - box.top_left.y)
def test_box_simple_contains() -> None:
box = Box(Point(17, 17), Point(42, 42))
assert box.contains(Point(23, 37))
assert not box.contains(Point(23, 666))
@given(st_points, st_boxes)
def test_box_containment(point: Point, box: Box) -> None:
assert box.contains(point) == (
box.top_left.x <= point.x <= box.bottom_right.x
and box.top_left.y <= point.y <= box.bottom_right.y
)
############## Tests for Polygon ####################
def test_polygon_simple_example() -> None:
p17 = Point(17, 17)
p42 = Point(42, 42)
polygon = Polygon(points=[p17, p42])
assert polygon.points == [p17, p42]
assert polygon.bounding_box().top_left == p17
assert polygon.bounding_box().bottom_right == p42
@given(st.lists(st_points, min_size=1))
def test_polygon_construction(points: list[Point]) -> None:
polygon = Polygon(points=points)
assert polygon.points == points
def test_polygon_construction_without_points() -> None:
with pytest.raises(GeometryError, match="Polygon: points must not be empty"):
Polygon(points=[])
def test_polygon_from_box_example() -> None:
box = Box(Point(17, 17), Point(42, 42))
polygon = Polygon.from_box(box)
assert polygon.points == [
Point(17, 17),
Point(42, 17),
Point(42, 42),
Point(17, 42),
]
@given(st_polygons)
def test_polygon_bounding_box_corners(polygon: Polygon) -> None:
points = polygon.points
min_x = min(point.x for point in points)
max_x = max(point.x for point in points)
min_y = min(point.y for point in points)
max_y = max(point.y for point in points)
bounding_box = polygon.bounding_box()
assert bounding_box.top_left == Point(x=min_x, y=min_y)
assert bounding_box.bottom_right == Point(x=max_x, y=max_y)
@given(st_polygons)
def test_polygon_bounding_box_contains(polygon: Polygon) -> None:
bounding_box = polygon.bounding_box()
for point in polygon.points:
assert bounding_box.contains(point)
@given(st_polygons)
def test_polygon_bounding_box_roundtrip(polygon: Polygon) -> None:
bounding_box = polygon.bounding_box()
box_polygon = Polygon.from_box(bounding_box)
assert box_polygon.bounding_box() == bounding_box