Move common test helpers to fixtures module

This commit is contained in:
2026-09-03 09:10:03 +02:00
parent 8a1d491943
commit a8b6f7591c
4 changed files with 126 additions and 204 deletions
+91
View File
@@ -1,4 +1,6 @@
use crate::maps::ElementMap;
use crate::traits::{GraphTopology, GraphTopologyAddition, Incidence};
use std::fmt::Debug;
pub trait MakeTestGraph: GraphTopology + Sized {
fn standard() -> (
@@ -162,3 +164,92 @@ impl<G: GraphTopologyAddition> MakeTestGraph for G {
(graph, vertices, [e0, e1, e2, e3])
}
}
pub fn assert_standard_unweighted_distances_v0<V, F>(actual_for: F, vertices: &[V])
where
V: Debug + Copy,
F: Fn(V) -> Option<u32>,
{
let expected = [0, 1, 2, 2, 2, 3, 3, 3, 3, 4];
for (i, v) in vertices.iter().enumerate() {
assert_eq!(
actual_for(*v),
Some(expected[i]),
"unexpected distance from vertex {:?} to vertex {:?}",
vertices[0],
vertices[i]
);
}
}
pub fn assert_standard_unweighted_predecessors_v0<V: Debug + Copy + PartialEq>(
actual: &ElementMap<V, Option<V>>,
vertices: &[V],
) {
let expected = [
vec![None],
vec![Some(vertices[0])],
vec![Some(vertices[1])],
vec![Some(vertices[1])],
vec![Some(vertices[1])],
vec![Some(vertices[2])],
vec![Some(vertices[2]), Some(vertices[3])],
vec![Some(vertices[4])],
vec![Some(vertices[4])],
vec![Some(vertices[5]), Some(vertices[6]), Some(vertices[7])],
];
for i in 0..10 {
assert!(
expected[i].contains(&actual[vertices[i]]),
"unexpected predecessor {:?} of vertex {:?}",
actual[vertices[i]],
vertices[i]
);
}
}
/// Asserts that the path given as `path_option` is a valid path in `graph`
///
/// Walks the path and asserts that it is `Some`, that it is empty if and only if
/// `source == target`, that its edges are pairwise incident in the order given, and that it starts
/// at `source` and ends at `target`.
pub fn assert_valid_path<G: GraphTopology>(
graph: &G,
path_option: Option<Vec<G::Edge>>,
source: G::Vertex,
targets: &[G::Vertex],
) -> Vec<G::Edge>
where
G::Vertex: Debug,
G::Edge: Debug,
{
let path = path_option.expect(&format!(
"path should exist between source vertex {:?} and connected targets {:?}",
source, targets
));
if targets.contains(&source) {
assert!(
path.is_empty(),
"path from source to itself should be empty"
);
} else {
assert!(!path.is_empty(), "path should not be empty");
// Walks the path: tracks current vertex, confirms each edge is incident to it.
let mut current = source;
for (i, &e) in path.iter().enumerate() {
let (v1, v2) = graph.incident_vertices(e);
assert_ne!(v1, v2, "path should not contain loop edge {e:?}");
assert!(
v1 == current || v2 == current,
"path edge {e:?} (index {i}, vertices {v1:?} to {v2:?}) not incident to vertex {current:?}"
);
current = if v1 == current { v2 } else { v1 };
}
assert!(
targets.contains(&current),
"path should end at a target vertex ({targets:?}), but ended at {current:?}"
);
}
path
}