diff --git a/PathPlanning/BidirectionalAStar/bidirectional_a_star.py b/PathPlanning/BidirectionalAStar/bidirectional_a_star.py index 5387cca1ad..adcdaaa636 100644 --- a/PathPlanning/BidirectionalAStar/bidirectional_a_star.py +++ b/PathPlanning/BidirectionalAStar/bidirectional_a_star.py @@ -59,6 +59,7 @@ def planning(self, sx, sy, gx, gy): output: rx: x position list of the final path ry: y position list of the final path + Both lists are empty when the goal cannot be reached. """ start_node = self.Node(self.calc_xy_index(sx, self.min_x), @@ -78,11 +79,11 @@ def planning(self, sx, sy, gx, gy): while True: if len(open_set_A) == 0: print("Open set A is empty..") - break + return [], [] if len(open_set_B) == 0: print("Open set B is empty..") - break + return [], [] c_id_A = min( open_set_A, diff --git a/tests/test_bidirectional_a_star.py b/tests/test_bidirectional_a_star.py new file mode 100644 index 0000000000..6f77a83fbb --- /dev/null +++ b/tests/test_bidirectional_a_star.py @@ -0,0 +1,26 @@ +import conftest +from PathPlanning.BidirectionalAStar import bidirectional_a_star as m + + +def test_1(): + m.show_animation = False + m.main() + + +def test_unreachable_goal_returns_empty_path(): + m.show_animation = False + # Square boundary from 0 to 10 with an inner wall at x = 5. + ox, oy = [], [] + for i in range(11): + ox.extend([0.0, 10.0, float(i), float(i), 5.0]) + oy.extend([float(i), float(i), 0.0, 10.0, float(i)]) + planner = m.BidirectionalAStarPlanner(ox, oy, 1.0, 0.5) + + rx, ry = planner.planning(2.0, 5.0, 8.0, 5.0) + + assert rx == [] + assert ry == [] + + +if __name__ == '__main__': + conftest.run_this_test(__file__)