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
+6 -45
View File
@@ -2,6 +2,7 @@ use std::fmt::Debug;
use grapherity::algorithms;
use grapherity::maps::ElementMap;
use grapherity::testing::fixtures;
use grapherity::testing::fixtures::MakeTestGraph;
use grapherity::traits::GraphTopology;
@@ -257,12 +258,7 @@ where
let (graph, vertices, e) = G::single_edge();
let path = algorithms::dfs_find_path(&graph, vertices[0], vertices[1])
.expect("path should exist between adjacent vertices");
assert_eq!(
path.len(),
1,
"unexpected path length between adjacent vertices"
);
assert_eq!(path[0], e, "path should use the connecting edge");
assert_eq!(path, [e], "path should contain only the connecting edge");
}
fn dfs_find_path<G: MakeTestGraph>()
@@ -271,11 +267,8 @@ where
G::Edge: Debug,
{
let (graph, vertices, _, _) = G::standard();
let path = algorithms::dfs_find_path(&graph, vertices[0], vertices[9]).expect(&format!(
"path should exist between connected vertices {:?} and {:?}",
vertices[0], vertices[9]
));
assert_valid_path(&graph, &path, vertices[0], vertices[9]);
let path = algorithms::dfs_find_path(&graph, vertices[0], vertices[9]);
fixtures::assert_valid_path(&graph, path, vertices[0], &[vertices[9]]);
}
fn dfs_find_path_where_source_matches<G: MakeTestGraph>()
@@ -320,13 +313,8 @@ where
G::Edge: Debug,
{
let (graph, vertices, _, _) = G::standard();
let path = algorithms::dfs_find_path_where(&graph, vertices[0], |v| v == vertices[9]).expect(
&format!(
"path should exist between connected vertices {:?} and {:?}",
vertices[0], vertices[9]
),
);
assert_valid_path(&graph, &path, vertices[0], vertices[9]);
let path = algorithms::dfs_find_path_where(&graph, vertices[0], |v| v == vertices[9]);
_ = fixtures::assert_valid_path(&graph, path, vertices[0], &[vertices[9]]);
}
fn assert_dfs_visited<G: GraphTopology>(
@@ -369,30 +357,3 @@ fn assert_dfs_predecessors<G: GraphTopology>(
);
}
}
fn assert_valid_path<G: GraphTopology>(
graph: &G,
path: &[G::Edge],
source: G::Vertex,
target: G::Vertex,
) where
G::Vertex: Debug,
G::Edge: Debug,
{
assert!(!path.is_empty(), "path should be non-empty");
// Walks the path: tracks current vertex, confirm 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:?} (idx {i}, {v1:?} to {v2:?}) not incident to vertex {current:?}"
);
current = if v1 == current { v2 } else { v1 };
}
assert_eq!(
current, target,
"path should end at target {target:?}, but ended at {current:?}"
);
}