use rustsim_pathfinding::astar::{astar, astar_grid2d};
#[test]
fn astar_simple_grid_path() {
let result = astar_grid2d((0, 0), (3, 3), 5, 5, &|_x, _y| true, true);
let result = result.expect("should find a path");
assert_eq!(*result.path.first().unwrap(), (0, 0));
assert_eq!(*result.path.last().unwrap(), (3, 3));
assert!(result.cost > 0.0);
}
#[test]
fn astar_blocked_path() {
let result = astar_grid2d((0, 0), (4, 0), 5, 1, &|x, _y| x != 2, false);
assert!(result.is_none(), "wall should block the path");
}
#[test]
fn astar_manhattan_path() {
let result = astar_grid2d((0, 0), (2, 2), 5, 5, &|_x, _y| true, false);
let result = result.expect("should find a path");
assert_eq!(*result.path.first().unwrap(), (0, 0));
assert_eq!(*result.path.last().unwrap(), (2, 2));
assert!((result.cost - 4.0).abs() < 1e-10);
}
#[test]
fn astar_generic_graph() {
let result = astar(
"A",
"D",
|_a, _b| 1.0,
|node: &&str| -> Vec<(&str, f64)> {
match *node {
"A" => vec![("B", 1.0), ("C", 4.0)],
"B" => vec![("C", 1.0), ("D", 5.0)],
"C" => vec![("D", 1.0)],
_ => vec![],
}
},
);
let result = result.expect("should find a path");
assert_eq!(result.path, vec!["A", "B", "C", "D"]);
assert!((result.cost - 3.0).abs() < 1e-10);
}