Skip to content

Commit 225c32c

Browse files
author
motok822
committed
BBoxの交差
1 parent 57a0d89 commit 225c32c

File tree

6 files changed

+533
-12
lines changed

6 files changed

+533
-12
lines changed

backend/paths/tests/__init__.py

Whitespace-only changes.

backend/paths/tests/test_utils.py

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
"""Tests for paths/utils.py"""
2+
3+
from paths.utils import (
4+
a_contains_b,
5+
a_contains_bottom_edge_of_b,
6+
a_contains_left_edge_of_b,
7+
a_contains_only_left_down_corner_of_b,
8+
a_contains_only_left_up_corner_of_b,
9+
a_contains_upper_edge_of_b,
10+
calc_necessary_bbox,
11+
)
12+
13+
14+
class TestBboxContainmentFunctions:
15+
"""Test bbox containment helper functions"""
16+
17+
def test_a_contains_b_true(self):
18+
"""Test when bbox A completely contains bbox B"""
19+
# A: (0, 0, 10, 10), B: (2, 2, 8, 8)
20+
assert a_contains_b(0, 0, 10, 10, 2, 2, 8, 8) is True
21+
22+
def test_a_contains_b_false(self):
23+
"""Test when bbox A does not contain bbox B"""
24+
# A: (0, 0, 10, 10), B: (5, 5, 15, 15)
25+
assert a_contains_b(0, 0, 10, 10, 5, 5, 15, 15) is False
26+
27+
def test_a_contains_b_equal(self):
28+
"""Test when bbox A and B are identical"""
29+
# A: (0, 0, 10, 10), B: (0, 0, 10, 10)
30+
assert a_contains_b(0, 0, 10, 10, 0, 0, 10, 10) is True
31+
32+
def test_a_contains_only_left_down_corner_of_b_true(self):
33+
"""Test when A contains only the left-down corner of B"""
34+
# A: (0, 0, 5, 5), B: (3, 3, 10, 10)
35+
assert a_contains_only_left_down_corner_of_b(0, 0, 5, 5, 3, 3, 10, 10) is True
36+
37+
def test_a_contains_only_left_down_corner_of_b_false(self):
38+
"""Test when A does not contain only the left-down corner of B"""
39+
# A: (0, 0, 10, 10), B: (5, 5, 15, 15)
40+
assert a_contains_only_left_down_corner_of_b(0, 0, 10, 10, 5, 5, 15, 15) is True
41+
42+
def test_a_contains_only_left_up_corner_of_b_true(self):
43+
"""Test when A contains only the left-up corner of B"""
44+
# A: (5, 5, 15, 15), B: (0, 0, 10, 10)
45+
assert a_contains_only_left_up_corner_of_b(5, 5, 15, 15, 0, 0, 10, 10) is True
46+
47+
def test_a_contains_left_edge_of_b_true(self):
48+
"""Test when A contains the left edge of B"""
49+
# A: (0, 0, 5, 20), B: (3, 5, 10, 15)
50+
assert a_contains_left_edge_of_b(0, 0, 5, 20, 3, 5, 10, 15) is True
51+
52+
def test_a_contains_bottom_edge_of_b_true(self):
53+
"""Test when A contains the bottom edge of B"""
54+
# A: (0, 0, 20, 5), B: (5, 3, 15, 10)
55+
assert a_contains_bottom_edge_of_b(0, 0, 20, 5, 5, 3, 15, 10) is True
56+
57+
def test_a_contains_upper_edge_of_b_true(self):
58+
"""Test when A contains the upper edge of B"""
59+
# A: (0, 10, 20, 20), B: (5, 5, 15, 15)
60+
assert a_contains_upper_edge_of_b(0, 10, 20, 20, 5, 5, 15, 15) is True
61+
62+
def test_a_contains_b_with_negative_coords(self):
63+
"""Test a_contains_b with negative coordinates"""
64+
# A: (-10, -10, 10, 10), B: (-5, -5, 5, 5)
65+
assert a_contains_b(-10, -10, 10, 10, -5, -5, 5, 5) is True
66+
67+
def test_a_contains_b_edge_case(self):
68+
"""Test when B touches the edge of A"""
69+
# A: (0, 0, 10, 10), B: (0, 0, 10, 5)
70+
assert a_contains_b(0, 0, 10, 10, 0, 0, 10, 5) is True
71+
72+
def test_a_contains_only_left_down_corner_edge_case(self):
73+
"""Test left-down corner with exact boundaries"""
74+
test_case = (0, 0, 6, 6, 5, 5, 10, 10)
75+
assert a_contains_only_left_down_corner_of_b(*test_case) is True
76+
assert a_contains_b(*test_case) is False
77+
assert a_contains_only_left_up_corner_of_b(*test_case) is False
78+
assert a_contains_left_edge_of_b(*test_case) is False
79+
assert a_contains_bottom_edge_of_b(*test_case) is False
80+
assert a_contains_upper_edge_of_b(*test_case) is False
81+
82+
def test_a_contains_left_edge_of_b_false(self):
83+
"""Test when A does not contain left edge of B"""
84+
# A: (10, 10, 20, 20), B: (0, 5, 15, 15)
85+
assert a_contains_left_edge_of_b(10, 10, 20, 20, 0, 5, 15, 15) is False
86+
87+
def test_a_contains_bottom_edge_of_b_false(self):
88+
"""Test when A does not contain bottom edge of B"""
89+
# A: (10, 10, 20, 20), B: (5, 0, 15, 15)
90+
assert a_contains_bottom_edge_of_b(10, 10, 20, 20, 5, 0, 15, 15) is False
91+
92+
def test_a_contains_only_left_up_corner_edge_case(self):
93+
"""Test left-up corner exclusively"""
94+
test_case = (8, 8, 15, 15, 5, 5, 10, 10)
95+
assert a_contains_only_left_up_corner_of_b(*test_case) is True
96+
assert a_contains_b(*test_case) is False
97+
assert a_contains_only_left_down_corner_of_b(*test_case) is False
98+
assert a_contains_left_edge_of_b(*test_case) is False
99+
assert a_contains_bottom_edge_of_b(*test_case) is False
100+
assert a_contains_upper_edge_of_b(*test_case) is False
101+
102+
def test_a_contains_left_edge_exclusively(self):
103+
"""Test left edge exclusively"""
104+
test_case = (0, 0, 5, 20, 3, 5, 10, 15)
105+
assert a_contains_left_edge_of_b(*test_case) is True
106+
assert a_contains_b(*test_case) is False
107+
assert a_contains_only_left_down_corner_of_b(*test_case) is False
108+
assert a_contains_only_left_up_corner_of_b(*test_case) is False
109+
assert a_contains_bottom_edge_of_b(*test_case) is False
110+
assert a_contains_upper_edge_of_b(*test_case) is False
111+
112+
def test_a_contains_bottom_edge_exclusively(self):
113+
"""Test bottom edge exclusively"""
114+
test_case = (0, 0, 20, 5, 5, 3, 15, 10)
115+
assert a_contains_bottom_edge_of_b(*test_case) is True
116+
assert a_contains_b(*test_case) is False
117+
assert a_contains_only_left_down_corner_of_b(*test_case) is False
118+
assert a_contains_only_left_up_corner_of_b(*test_case) is False
119+
assert a_contains_left_edge_of_b(*test_case) is False
120+
assert a_contains_upper_edge_of_b(*test_case) is False
121+
122+
def test_a_contains_upper_edge_exclusively(self):
123+
"""Test upper edge exclusively"""
124+
test_case = (0, 10, 20, 20, 5, 5, 15, 15)
125+
assert a_contains_upper_edge_of_b(*test_case) is True
126+
assert a_contains_b(*test_case) is False
127+
assert a_contains_only_left_down_corner_of_b(*test_case) is False
128+
assert a_contains_only_left_up_corner_of_b(*test_case) is False
129+
assert a_contains_left_edge_of_b(*test_case) is False
130+
assert a_contains_bottom_edge_of_b(*test_case) is False
131+
132+
def test_no_containment_at_all(self):
133+
"""Test when bboxes are completely separate"""
134+
test_case = (0, 0, 5, 5, 10, 10, 15, 15)
135+
assert a_contains_b(*test_case) is False
136+
assert a_contains_only_left_down_corner_of_b(*test_case) is False
137+
assert a_contains_only_left_up_corner_of_b(*test_case) is False
138+
assert a_contains_left_edge_of_b(*test_case) is False
139+
assert a_contains_bottom_edge_of_b(*test_case) is False
140+
assert a_contains_upper_edge_of_b(*test_case) is False
141+
142+
143+
class TestCalcNecessaryBbox:
144+
"""Test calc_necessary_bbox function"""
145+
146+
def test_new_bbox_contains_previous(self):
147+
"""Test when new bbox completely contains previous bbox"""
148+
result = calc_necessary_bbox(0, 0, 10, 10, 2, 2, 8, 8)
149+
assert result == [(0, 0, 10, 10)]
150+
151+
def test_previous_bbox_contains_new(self):
152+
"""Test when previous bbox completely contains new bbox"""
153+
result = calc_necessary_bbox(2, 2, 8, 8, 0, 0, 10, 10)
154+
assert result == []
155+
156+
def test_new_bbox_identical_to_previous(self):
157+
"""Test when bboxes are identical"""
158+
result = calc_necessary_bbox(0, 0, 10, 10, 0, 0, 10, 10)
159+
print(result)
160+
assert result == []
161+
162+
def test_new_contains_only_left_down_corner(self):
163+
"""Test when new bbox contains only left-down corner of previous"""
164+
result = calc_necessary_bbox(0, 0, 5, 5, 3, 3, 10, 10)
165+
assert result == [(0, 0, 3, 5), (3, 0, 5, 3)]
166+
167+
def test_previous_contains_only_left_down_corner(self):
168+
"""Test when previous bbox contains only left-down corner of new"""
169+
result = calc_necessary_bbox(5, 5, 10, 10, 0, 0, 7, 7)
170+
assert result == [(7, 5, 10, 10), (5, 7, 7, 10)]
171+
172+
def test_new_contains_only_left_up_corner(self):
173+
"""Test when new bbox contains only left-up corner of previous"""
174+
result = calc_necessary_bbox(0, 0, 5, 10, 3, 5, 10, 15)
175+
print(result)
176+
assert result == [(0, 0, 3, 10), (3, 0, 5, 5)]
177+
178+
def test_new_contains_left_edge(self):
179+
"""Test when new bbox contains left edge of previous"""
180+
result = calc_necessary_bbox(0, 0, 5, 20, 3, 5, 10, 15)
181+
assert result == [(0, 0, 3, 20), (3, 0, 5, 15), (3, 15, 5, 20)]
182+
183+
def test_no_overlap(self):
184+
"""Test when bboxes don't overlap"""
185+
result = calc_necessary_bbox(0, 0, 5, 5, 10, 10, 15, 15)
186+
assert result == [(0, 0, 5, 5)]
187+
188+
def test_previous_contains_left_edge_of_new(self):
189+
"""Test when previous bbox contains left edge of new"""
190+
result = calc_necessary_bbox(5, 5, 10, 10, 0, 0, 7, 15)
191+
assert result == [(7, 5, 10, 10)]
192+
193+
def test_new_contains_bottom_edge(self):
194+
"""Test when new bbox contains bottom edge of previous"""
195+
result = calc_necessary_bbox(0, 0, 20, 5, 5, 3, 15, 10)
196+
assert result == [(0, 0, 20, 3), (0, 3, 5, 5), (15, 3, 20, 5)]
197+
198+
def test_previous_contains_bottom_edge_of_new(self):
199+
"""Test when previous bbox contains bottom edge of new"""
200+
result = calc_necessary_bbox(5, 5, 15, 10, 0, 0, 20, 7)
201+
assert result == [(5, 7, 15, 10)]
202+
203+
def test_new_contains_upper_edge(self):
204+
"""Test when new bbox contains upper edge of previous"""
205+
result = calc_necessary_bbox(0, 10, 20, 20, 5, 5, 15, 15)
206+
assert result == [(0, 15, 20, 20), (0, 10, 5, 15), (15, 10, 20, 15)]
207+
208+
def test_previous_contains_upper_edge_of_new(self):
209+
"""Test when previous bbox contains upper edge of new"""
210+
result = calc_necessary_bbox(5, 5, 15, 15, 0, 10, 20, 20)
211+
assert result == [(5, 5, 15, 10)]

0 commit comments

Comments
 (0)