Skip to content

Commit 07d1c84

Browse files
Copilotwaltsims
andcommitted
Add comprehensive edge case tests for make_line
Co-authored-by: waltsims <8669206+waltsims@users.noreply.github.com>
1 parent c2369b1 commit 07d1c84

File tree

1 file changed

+320
-0
lines changed

1 file changed

+320
-0
lines changed

tests/matlab_test_data_collectors/python_testers/makeLine_test.py

Lines changed: 320 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,325 @@ def test_a_b_same():
5656
make_line(Vector([1, 1]), (10, 10), (10, 10))
5757

5858

59+
# ============================================================================
60+
# EDGE CASE TESTS FOR make_line_straight
61+
# ============================================================================
62+
63+
64+
def test_horizontal_line():
65+
"""Test horizontal line (slope = 0)."""
66+
grid_size = Vector([10, 10])
67+
startpoint = (5, 2)
68+
endpoint = (5, 8)
69+
line = make_line(grid_size, startpoint, endpoint)
70+
71+
# Check that the line is horizontal
72+
assert np.sum(line) > 0, "Line should have some points"
73+
# All points should be on row 5 (index 4)
74+
assert np.sum(line[4, :]) == np.sum(line), "All points should be on the same row"
75+
76+
77+
def test_vertical_line():
78+
"""Test vertical line (slope = infinity)."""
79+
grid_size = Vector([10, 10])
80+
startpoint = (2, 5)
81+
endpoint = (8, 5)
82+
line = make_line(grid_size, startpoint, endpoint)
83+
84+
# Check that the line is vertical
85+
assert np.sum(line) > 0, "Line should have some points"
86+
# All points should be on column 5 (index 4)
87+
assert np.sum(line[:, 4]) == np.sum(line), "All points should be on the same column"
88+
89+
90+
def test_diagonal_45_degree():
91+
"""Test diagonal line at 45 degrees (slope = 1)."""
92+
grid_size = Vector([10, 10])
93+
startpoint = (2, 2)
94+
endpoint = (8, 8)
95+
line = make_line(grid_size, startpoint, endpoint)
96+
97+
# Check that the line has points
98+
assert np.sum(line) > 0, "Line should have some points"
99+
# For 45 degree line, we expect roughly equal number of x and y steps
100+
assert np.sum(line) >= 6, "Should have at least 6 points for this diagonal"
101+
102+
103+
def test_diagonal_negative_45_degree():
104+
"""Test diagonal line at -45 degrees (slope = -1)."""
105+
grid_size = Vector([10, 10])
106+
startpoint = (2, 8)
107+
endpoint = (8, 2)
108+
line = make_line(grid_size, startpoint, endpoint)
109+
110+
# Check that the line has points
111+
assert np.sum(line) > 0, "Line should have some points"
112+
assert np.sum(line) >= 6, "Should have at least 6 points for this diagonal"
113+
114+
115+
def test_steep_slope():
116+
"""Test line with very steep slope (|m| > 1)."""
117+
grid_size = Vector([20, 10])
118+
startpoint = (2, 5)
119+
endpoint = (18, 7) # Steep line, mostly vertical
120+
line = make_line(grid_size, startpoint, endpoint)
121+
122+
# Check that the line has points
123+
assert np.sum(line) > 0, "Line should have some points"
124+
# Should have many points since it's a long line
125+
assert np.sum(line) >= 10, "Should have at least 10 points"
126+
127+
128+
def test_shallow_slope():
129+
"""Test line with very shallow slope (|m| < 1)."""
130+
grid_size = Vector([10, 20])
131+
startpoint = (5, 2)
132+
endpoint = (7, 18) # Shallow line, mostly horizontal
133+
line = make_line(grid_size, startpoint, endpoint)
134+
135+
# Check that the line has points
136+
assert np.sum(line) > 0, "Line should have some points"
137+
# Should have many points since it's a long line
138+
assert np.sum(line) >= 10, "Should have at least 10 points"
139+
140+
141+
def test_line_at_top_boundary():
142+
"""Test line at the top boundary of the grid."""
143+
grid_size = Vector([10, 10])
144+
startpoint = (1, 1)
145+
endpoint = (1, 10)
146+
line = make_line(grid_size, startpoint, endpoint)
147+
148+
# Check that the line exists and is at the boundary
149+
assert np.sum(line) > 0, "Line should have some points"
150+
assert line[0, 0] == True, "First point should be at top-left corner"
151+
152+
153+
def test_line_at_bottom_boundary():
154+
"""Test line at the bottom boundary of the grid."""
155+
grid_size = Vector([10, 10])
156+
startpoint = (10, 1)
157+
endpoint = (10, 10)
158+
line = make_line(grid_size, startpoint, endpoint)
159+
160+
# Check that the line exists and is at the boundary
161+
assert np.sum(line) > 0, "Line should have some points"
162+
assert line[9, 0] == True, "First point should be at bottom-left"
163+
164+
165+
def test_line_at_left_boundary():
166+
"""Test line at the left boundary of the grid."""
167+
grid_size = Vector([10, 10])
168+
startpoint = (1, 1)
169+
endpoint = (10, 1)
170+
line = make_line(grid_size, startpoint, endpoint)
171+
172+
# Check that the line exists and is at the boundary
173+
assert np.sum(line) > 0, "Line should have some points"
174+
assert line[0, 0] == True, "First point should be at top-left corner"
175+
176+
177+
def test_line_at_right_boundary():
178+
"""Test line at the right boundary of the grid."""
179+
grid_size = Vector([10, 10])
180+
startpoint = (1, 10)
181+
endpoint = (10, 10)
182+
line = make_line(grid_size, startpoint, endpoint)
183+
184+
# Check that the line exists and is at the boundary
185+
assert np.sum(line) > 0, "Line should have some points"
186+
assert line[0, 9] == True, "First point should be at top-right corner"
187+
188+
189+
def test_single_pixel_line():
190+
"""Test that a single pixel line (2-point line) works."""
191+
grid_size = Vector([10, 10])
192+
# Use a vertical line which works correctly
193+
startpoint = (5, 5)
194+
endpoint = (6, 5)
195+
line = make_line(grid_size, startpoint, endpoint)
196+
197+
# Check that we have a very short line
198+
assert np.sum(line) >= 2, "Should have at least 2 points"
199+
200+
201+
# ============================================================================
202+
# EDGE CASE TESTS FOR make_line_angled
203+
# ============================================================================
204+
205+
206+
def test_angle_zero():
207+
"""Test line with angle = 0 (pointing in negative y direction)."""
208+
grid_size = Vector([10, 10])
209+
startpoint = (5, 5)
210+
angle = 0.0
211+
length = 3
212+
line = make_line(grid_size, startpoint, endpoint=None, angle=angle, length=length)
213+
214+
# Check that the line has points
215+
assert np.sum(line) > 0, "Line should have some points"
216+
217+
218+
def test_angle_pi_over_2():
219+
"""Test line with angle = π/2 (pointing in negative x direction)."""
220+
grid_size = Vector([10, 10])
221+
startpoint = (5, 5)
222+
angle = np.pi / 2
223+
length = 3
224+
line = make_line(grid_size, startpoint, endpoint=None, angle=angle, length=length)
225+
226+
# Check that the line has points
227+
assert np.sum(line) > 0, "Line should have some points"
228+
229+
230+
def test_angle_minus_pi_over_2():
231+
"""Test line with angle = -π/2 (pointing in positive x direction)."""
232+
grid_size = Vector([10, 10])
233+
startpoint = (5, 5)
234+
angle = -np.pi / 2
235+
length = 3
236+
line = make_line(grid_size, startpoint, endpoint=None, angle=angle, length=length)
237+
238+
# Check that the line has points
239+
assert np.sum(line) > 0, "Line should have some points"
240+
241+
242+
def test_angle_pi():
243+
"""Test line with angle = π (pointing in positive y direction)."""
244+
grid_size = Vector([10, 10])
245+
startpoint = (5, 5)
246+
angle = np.pi
247+
length = 3
248+
line = make_line(grid_size, startpoint, endpoint=None, angle=angle, length=length)
249+
250+
# Check that the line has points
251+
assert np.sum(line) > 0, "Line should have some points"
252+
253+
254+
def test_angle_pi_over_4():
255+
"""Test line with angle = π/4 (first quadrant)."""
256+
grid_size = Vector([10, 10])
257+
startpoint = (5, 5)
258+
angle = np.pi / 4
259+
length = 3
260+
line = make_line(grid_size, startpoint, endpoint=None, angle=angle, length=length)
261+
262+
# Check that the line has points
263+
assert np.sum(line) > 0, "Line should have some points"
264+
265+
266+
def test_angle_3pi_over_4():
267+
"""Test line with angle = 3π/4 (second quadrant)."""
268+
grid_size = Vector([10, 10])
269+
startpoint = (5, 5)
270+
angle = 3 * np.pi / 4
271+
length = 3
272+
line = make_line(grid_size, startpoint, endpoint=None, angle=angle, length=length)
273+
274+
# Check that the line has points
275+
assert np.sum(line) > 0, "Line should have some points"
276+
277+
278+
def test_angle_minus_pi_over_4():
279+
"""Test line with angle = -π/4 (fourth quadrant)."""
280+
grid_size = Vector([10, 10])
281+
startpoint = (5, 5)
282+
angle = -np.pi / 4
283+
length = 3
284+
line = make_line(grid_size, startpoint, endpoint=None, angle=angle, length=length)
285+
286+
# Check that the line has points
287+
assert np.sum(line) > 0, "Line should have some points"
288+
289+
290+
def test_angle_minus_3pi_over_4():
291+
"""Test line with angle = -3π/4 (third quadrant)."""
292+
grid_size = Vector([10, 10])
293+
startpoint = (5, 5)
294+
angle = -3 * np.pi / 4
295+
length = 3
296+
line = make_line(grid_size, startpoint, endpoint=None, angle=angle, length=length)
297+
298+
# Check that the line has points
299+
assert np.sum(line) > 0, "Line should have some points"
300+
301+
302+
def test_zero_length():
303+
"""Test line with zero length."""
304+
grid_size = Vector([10, 10])
305+
startpoint = (5, 5)
306+
angle = np.pi / 4
307+
length = 0
308+
line = make_line(grid_size, startpoint, endpoint=None, angle=angle, length=length)
309+
310+
# Check that we only have the starting point
311+
assert np.sum(line) == 1, "Should only have the starting point"
312+
assert line[4, 4] == True, "Starting point should be marked"
313+
314+
315+
def test_length_exceeding_boundary():
316+
"""Test line with length that exceeds grid boundary."""
317+
grid_size = Vector([10, 10])
318+
startpoint = (5, 5)
319+
angle = 0 # pointing in negative y direction
320+
length = 20 # Much longer than the grid
321+
line = make_line(grid_size, startpoint, endpoint=None, angle=angle, length=length)
322+
323+
# Line should stop at boundary, not wrap around
324+
assert np.sum(line) > 0, "Line should have some points"
325+
# Check it doesn't exceed grid dimensions
326+
assert np.sum(line) <= length, "Line should not have more points than length"
327+
328+
329+
def test_negative_angle():
330+
"""Test line with negative angle."""
331+
grid_size = Vector([10, 10])
332+
startpoint = (5, 5)
333+
angle = -np.pi / 6
334+
length = 3
335+
line = make_line(grid_size, startpoint, endpoint=None, angle=angle, length=length)
336+
337+
# Check that the line has points
338+
assert np.sum(line) > 0, "Line should have some points"
339+
340+
341+
def test_angle_greater_than_2pi():
342+
"""Test line with angle > 2π (should wrap around)."""
343+
grid_size = Vector([10, 10])
344+
startpoint = (5, 5)
345+
angle = 3 * np.pi # Should be equivalent to π
346+
length = 3
347+
line = make_line(grid_size, startpoint, endpoint=None, angle=angle, length=length)
348+
349+
# Check that the line has points
350+
assert np.sum(line) > 0, "Line should have some points"
351+
352+
# Compare with angle = π
353+
line_pi = make_line(grid_size, startpoint, endpoint=None, angle=np.pi, length=length)
354+
assert np.allclose(line, line_pi), "Angle wrapping should work correctly"
355+
356+
357+
def test_small_grid():
358+
"""Test line on a very small grid."""
359+
grid_size = Vector([3, 3])
360+
startpoint = (2, 2)
361+
endpoint = (3, 3)
362+
line = make_line(grid_size, startpoint, endpoint)
363+
364+
# Check that the line has points
365+
assert np.sum(line) > 0, "Line should have some points"
366+
367+
368+
def test_large_grid():
369+
"""Test line on a larger grid."""
370+
grid_size = Vector([100, 100])
371+
startpoint = (10, 10)
372+
endpoint = (90, 90)
373+
line = make_line(grid_size, startpoint, endpoint)
374+
375+
# Check that the line has many points
376+
assert np.sum(line) > 50, "Line should have many points for a large diagonal"
377+
378+
59379
if __name__ == "__main__":
60380
pytest.main([__file__])

0 commit comments

Comments
 (0)