|
| 1 | +import unittest |
| 2 | +from PathPlanning.AStar.a_star import AStarPlanner |
| 3 | + |
| 4 | + |
| 5 | +class TestAStar(unittest.TestCase): |
| 6 | + def test_basic_path(self): |
| 7 | + # create simple U-shaped obstacle walls |
| 8 | + ox = [i for i in range(60)] # bottom wall |
| 9 | + oy = [0 for _ in range(60)] |
| 10 | + |
| 11 | + for i in range(60): # right wall |
| 12 | + ox.append(60) |
| 13 | + oy.append(i) |
| 14 | + |
| 15 | + sx = 10.0 # start (x, y) |
| 16 | + sy = 10.0 |
| 17 | + gx = 50.0 # goal (x, y) |
| 18 | + gy = 50.0 |
| 19 | + grid_size = 2.0 |
| 20 | + robot_radius = 1.0 |
| 21 | + |
| 22 | + planner = AStarPlanner(ox, oy, grid_size, robot_radius) |
| 23 | + rx, ry = planner.planning(sx, sy, gx, gy) |
| 24 | + |
| 25 | + # basic sanity checks |
| 26 | + self.assertTrue(len(rx) > 0) |
| 27 | + self.assertEqual(len(rx), len(ry)) |
| 28 | + |
| 29 | + # the algorithm may return the path in either direction; |
| 30 | + # verify the two endpoints match {start, goal} regardless of order |
| 31 | + self.assertCountEqual( |
| 32 | + {(rx[0], ry[0]), (rx[-1], ry[-1])}, |
| 33 | + {(sx, sy), (gx, gy)} |
| 34 | + ) |
| 35 | + |
| 36 | + def test_start_equals_goal(self): |
| 37 | + # provide ONE dummy obstacle so min()/max() calls succeed |
| 38 | + sx = sy = gx = gy = 10.0 |
| 39 | + ox = [sx] |
| 40 | + oy = [sy] |
| 41 | + grid_size = 1.0 |
| 42 | + robot_radius = 1.0 |
| 43 | + |
| 44 | + planner = AStarPlanner(ox, oy, grid_size, robot_radius) |
| 45 | + rx, ry = planner.planning(sx, sy, gx, gy) |
| 46 | + |
| 47 | + # when start == goal, path should contain exactly that single point |
| 48 | + self.assertEqual(len(rx), 1) |
| 49 | + self.assertEqual((rx[0], ry[0]), (sx, sy)) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == '__main__': |
| 53 | + unittest.main() |
0 commit comments