use multiobjective_search::Output;
use proptest::prelude::*;
use crate::brute_force::pareto_costs;
use crate::fixtures::{EdgeList, Problem};
pub fn check<const N: usize>(
problem: &Problem<N>,
out: &Output<usize, [u32; N]>,
) -> Result<(), TestCaseError> {
let costs: Vec<_> = out.solutions.iter().map(|s| s.cost).collect();
prop_assert_eq!(&costs, &pareto_costs(problem));
check_paths(problem, out)
}
pub fn check_paths<const N: usize>(
problem: &Problem<N>,
out: &Output<usize, [u32; N]>,
) -> Result<(), TestCaseError> {
for solution in &out.solutions {
prop_assert_eq!(solution.path.first(), Some(&problem.start));
prop_assert!(problem.goals[*solution.path.last().unwrap()]);
prop_assert!(path_costs(&problem.graph, &solution.path).contains(&solution.cost));
}
Ok(())
}
fn path_costs<const N: usize>(graph: &EdgeList<N>, path: &[usize]) -> Vec<[u32; N]> {
let mut costs = vec![[0; N]];
for step in path.windows(2) {
let edges: Vec<_> = graph
.edges
.iter()
.filter(|(from, to, _)| *from == step[0] && *to == step[1])
.map(|&(_, _, cost)| cost)
.collect();
costs = costs
.iter()
.flat_map(|c| {
edges
.iter()
.map(move |e| core::array::from_fn(|i| c[i] + e[i]))
})
.collect();
}
costs
}