def test_shortest(): m = Map(7, 7) m.create_food(Pos(5, 5)) s = Snake(m, Direc.RIGHT, [Pos(2, 3), Pos(2, 2), Pos(2, 1)], [PointType.HEAD_R, PointType.BODY_HOR, PointType.BODY_HOR]) solver = PathSolver(s) act_path = solver.shortest_path_to_food() act_path = solver.shortest_path_to_food() # Check idempotence expect_path = [ Direc.RIGHT, Direc.RIGHT, Direc.DOWN, Direc.DOWN, Direc.DOWN ] assert len(act_path) == len(expect_path) for i, direc in enumerate(act_path): assert direc == expect_path[i] assert solver.table[5][1].dist == 5 assert solver.table[5][1].dist == solver.table[5][5].dist # Empty path assert not solver.shortest_path_to(s.tail())
def test_shortest_path_to_food(self): m = Map(7, 7) m.create_food(Pos(5, 5)) s = Snake(m, Direc.RIGHT, [Pos(2, 3), Pos(2, 2), Pos(2, 1)], [PointType.HEAD_R, PointType.BODY_HOR, PointType.BODY_HOR]) solver = PathSolver(s) # noinspection PyUnusedLocal act_path = solver.shortest_path_to_food() act_path = solver.shortest_path_to_food() # Check idempotency # Idempotency is pretty much checking for side effects. # You're checking whether or not the algorithm will produce the same result if it is run multiple times. # We do this multiple times to verify that the table has been properly reset. expect_path = [ Direc.RIGHT, Direc.RIGHT, Direc.DOWN, Direc.DOWN, Direc.DOWN ] assert len(act_path) == len(expect_path) for i, direc in enumerate(act_path): assert direc == expect_path[i] assert solver.table[5][1].dist == 5 assert solver.table[5][1].dist == solver.table[5][5].dist # Empty path assert not solver.shortest_path_to(s.tail())