Refactor tests: use MakeTestGraph in model-specific tests, deconstruct vertex arrays where useful, adjust local variable names

This commit is contained in:
2026-09-04 09:28:00 +02:00
parent 016d127bd1
commit 4c6a8a43e4
11 changed files with 153 additions and 195 deletions
+6 -6
View File
@@ -58,7 +58,7 @@ impl<G: GraphTopologyAddition> MakeTestGraph for G {
(vertices[7], vertices[8]),
(vertices[7], vertices[9]),
]
.map(|(v1, v2)| (graph.add_edge(v1, v2), v1, v2));
.map(|(u, v)| (graph.add_edge(u, v), u, v));
let i = |vertex, edge| Incidence { vertex, edge };
let incidences = [
vec![i(vertices[1], edges[0].0), i(vertices[1], edges[1].0)],
@@ -323,13 +323,13 @@ where
// 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:?}");
let (u, v) = graph.incident_vertices(e);
assert_ne!(u, v, "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:?}"
u == current || v == current,
"path edge {e:?} (index {i}, vertices {u:?} to {v:?}) not incident to vertex {current:?}"
);
current = if v1 == current { v2 } else { v1 };
current = if u == current { v } else { u };
}
assert!(
targets.contains(&current),
@@ -50,11 +50,11 @@ pub fn delete_edge_add_edge<G: GraphTopologyDeletion + GraphTopologyAddition>()
where
G::Edge: Debug,
{
let (mut graph, vertices, e) = G::single_edge();
let (mut graph, [v0, v1], e) = G::single_edge();
graph.delete_edge(e);
assert_eq!(graph.edge_count(), 0, "unexpected edge count after delete");
assert_ne!(
graph.add_edge(vertices[0], vertices[1]),
graph.add_edge(v0, v1),
e,
"unexpected duplicate edge after re-add"
);
+10 -10
View File
@@ -77,12 +77,12 @@ pub fn reserve_edges_increases_capacity<G: GraphTopologyAddition>() {
pub fn reserve_edges_prevents_reallocation_on_add<G: GraphTopologyAddition>() {
let mut graph = G::default();
let v1 = graph.add_vertex();
let v2 = graph.add_vertex();
let u = graph.add_vertex();
let v = graph.add_vertex();
graph.reserve_edges(10);
let capacity_before = graph.edge_capacity();
for _ in 0..10 {
graph.add_edge(v1, v2);
graph.add_edge(u, v);
}
assert_eq!(
graph.edge_capacity(),
@@ -116,10 +116,10 @@ where
G::Edge: Debug,
{
let mut graph = G::default();
let v1 = graph.add_vertex();
let v2 = graph.add_vertex();
let e = graph.add_edge(v1, v2);
assert_ne!(graph.add_edge(v1, v2), e, "unexpected duplicate edge");
let u = graph.add_vertex();
let v = graph.add_vertex();
let e = graph.add_edge(u, v);
assert_ne!(graph.add_edge(u, v), e, "unexpected duplicate edge");
}
pub fn vertex_map_new_vertex<G: GraphTopologyAddition>() {
@@ -139,10 +139,10 @@ pub fn vertex_map_new_vertex<G: GraphTopologyAddition>() {
pub fn edge_map_new_edge<G: GraphTopologyAddition>() {
let mut graph = G::default();
let v1 = graph.add_vertex();
let v2 = graph.add_vertex();
let u = graph.add_vertex();
let v = graph.add_vertex();
let mut map = graph.edge_map(28);
let e = graph.add_edge(v1, v2);
let e = graph.add_edge(u, v);
assert_eq!(map[e], 28);
map[e] = 8;
assert_eq!(
+18 -26
View File
@@ -136,7 +136,7 @@ where
}
pub fn delete_edge<G: GraphTopologyDeletion + GraphTopologyAddition>() {
let (mut graph, vertices, e) = G::single_edge();
let (mut graph, [v0, v1], e) = G::single_edge();
assert_eq!(
graph.vertex_count(),
2,
@@ -144,16 +144,16 @@ pub fn delete_edge<G: GraphTopologyDeletion + GraphTopologyAddition>() {
);
assert_eq!(graph.edge_count(), 1, "unexpected edge count before delete");
assert!(
graph.are_adjacent(vertices[0], vertices[1]),
graph.are_adjacent(v0, v1),
"expected vertices to be adjacent before delete"
);
assert_eq!(
graph.degree(vertices[0]),
graph.degree(v0),
1,
"unexpected vertex degree before delete"
);
assert_eq!(
graph.degree(vertices[1]),
graph.degree(v1),
1,
"unexpected vertex degree before delete"
);
@@ -165,19 +165,11 @@ pub fn delete_edge<G: GraphTopologyDeletion + GraphTopologyAddition>() {
);
assert_eq!(graph.edge_count(), 0, "unexpected edge count after delete");
assert!(
!graph.are_adjacent(vertices[0], vertices[1]),
!graph.are_adjacent(v0, v1),
"unexpected adjacency after delete"
);
assert_eq!(
graph.degree(vertices[0]),
0,
"unexpected vertex degree after delete"
);
assert_eq!(
graph.degree(vertices[1]),
0,
"unexpected vertex degree after delete"
);
assert_eq!(graph.degree(v0), 0, "unexpected vertex degree after delete");
assert_eq!(graph.degree(v1), 0, "unexpected vertex degree after delete");
}
pub fn delete_edge_loop<G: GraphTopologyDeletion + GraphTopologyAddition>() {
@@ -209,7 +201,7 @@ pub fn delete_edge_loop<G: GraphTopologyDeletion + GraphTopologyAddition>() {
pub fn delete_edge_multiple<G: GraphTopologyDeletion + GraphTopologyAddition>() {
const K: usize = 2;
let (mut graph, vertices, edges) = G::multiple_edges::<K>();
let (mut graph, [v0, v1], edges) = G::multiple_edges::<K>();
assert_eq!(
graph.vertex_count(),
2,
@@ -217,16 +209,16 @@ pub fn delete_edge_multiple<G: GraphTopologyDeletion + GraphTopologyAddition>()
);
assert_eq!(graph.edge_count(), K, "unexpected edge count before delete");
assert!(
graph.are_adjacent(vertices[0], vertices[1]),
graph.are_adjacent(v0, v1),
"expected vertices to be adjacent before delete"
);
assert_eq!(
graph.degree(vertices[0]),
graph.degree(v0),
K,
"unexpected vertex degree before delete"
);
assert_eq!(
graph.degree(vertices[1]),
graph.degree(v1),
K,
"unexpected vertex degree before delete"
);
@@ -242,16 +234,16 @@ pub fn delete_edge_multiple<G: GraphTopologyDeletion + GraphTopologyAddition>()
"unexpected edge count after delete"
);
assert!(
graph.are_adjacent(vertices[0], vertices[1]),
graph.are_adjacent(v0, v1),
"expected vertices to be adjacent after delete"
);
assert_eq!(
graph.degree(vertices[0]),
graph.degree(v0),
K - 1,
"unexpected vertex degree after delete"
);
assert_eq!(
graph.degree(vertices[1]),
graph.degree(v1),
K - 1,
"unexpected vertex degree after delete"
);
@@ -305,11 +297,11 @@ where
let (mut graph, _, edges, _) = G::standard();
let e = edges[2].0;
graph.delete_edge(e);
for &(f, v1, v2) in edges.iter().filter(|&&(f, _, _)| f != e) {
let (u1, u2) = graph.incident_vertices(f);
for &(f, v0, v1) in edges.iter().filter(|&&(f, _, _)| f != e) {
let (u0, u1) = graph.incident_vertices(f);
assert!(
(u1 == v1 && u2 == v2) || (u1 == v2 && u2 == v1),
"unexpected incident vertices {u1:?} and {u2:?} for edge {f:?} after delete"
(u0 == v0 && u1 == v1) || (u0 == v1 && u1 == v0),
"unexpected incident vertices {u0:?} and {u1:?} for edge {f:?} after delete"
);
}
}
+26 -49
View File
@@ -135,25 +135,12 @@ where
G::Vertex: Debug,
{
const K: usize = 3;
let (graph, vertices, _) = G::multiple_edges::<K>();
let (graph, [v0, v1], _) = G::multiple_edges::<K>();
assert_eq!(graph.vertex_count(), 2, "unexpected vertex count");
assert_eq!(graph.edge_count(), K, "unexpected edge count");
assert_eq!(
graph.degree(vertices[0]),
K,
"unexpected degree of vertex {:?}",
vertices[0]
);
assert_eq!(
graph.degree(vertices[1]),
K,
"unexpected degree of vertex {:?}",
vertices[1]
);
assert!(
graph.are_adjacent(vertices[0], vertices[1]),
"should be adjacent"
);
assert_eq!(graph.degree(v0), K, "unexpected degree of vertex {v0:?}");
assert_eq!(graph.degree(v1), K, "unexpected degree of vertex {v1:?}",);
assert!(graph.are_adjacent(v0, v1), "should be adjacent");
}
pub fn are_adjacent_vertex_self<G: MakeTestGraph>() {
@@ -165,15 +152,9 @@ pub fn are_adjacent_vertex_self<G: MakeTestGraph>() {
}
pub fn are_adjacent_single_edge<G: MakeTestGraph>() {
let (graph, vertices, _) = G::single_edge();
assert!(
graph.are_adjacent(vertices[0], vertices[1]),
"should be adjacent"
);
assert!(
graph.are_adjacent(vertices[1], vertices[0]),
"should be adjacent"
);
let (graph, [v0, v1], _) = G::single_edge();
assert!(graph.are_adjacent(v0, v1), "should be adjacent");
assert!(graph.are_adjacent(v1, v0), "should be adjacent");
}
pub fn are_adjacent<G: MakeTestGraph>()
@@ -341,11 +322,11 @@ where
G::Vertex: Debug,
G::Edge: Debug,
{
let (graph, vertices, e) = G::single_edge();
let (u, v) = graph.incident_vertices(e);
let (graph, [v0, v1], e) = G::single_edge();
let (u0, u1) = graph.incident_vertices(e);
assert!(
(u == vertices[0] && v == vertices[1]) || (u == vertices[1] && v == vertices[0]),
"unexpected incident vertices {u:?} and {v:?} for edge {e:?}"
(u0 == v0 && u1 == v1) || (u0 == v1 && u1 == v0),
"unexpected incident vertices {u0:?} and {u1:?} for edge {e:?}"
);
}
@@ -367,20 +348,16 @@ where
G::Vertex: Debug,
G::Edge: Debug,
{
let (graph, vertices, edges) = G::multiple_edges::<2>();
let (graph, [v0, v1], edges) = G::multiple_edges::<2>();
assert_ne!(edges[0], edges[1], "edges should be distinct");
let (u, v) = graph.incident_vertices(edges[0]);
assert!(
(u == vertices[0] && v == vertices[1]) || (u == vertices[1] && v == vertices[0]),
"unexpected incident vertices {u:?} and {v:?} for first multi-edge {:?}",
edges[0]
);
let (u, v) = graph.incident_vertices(edges[1]);
assert!(
(u == vertices[0] && v == vertices[1]) || (u == vertices[1] && v == vertices[0]),
"unexpected incident vertices {u:?} and {v:?} for second multi-edge {:?}",
edges[1]
);
for i in 0..2 {
let (u0, u1) = graph.incident_vertices(edges[i]);
assert!(
(u0 == v0 && u1 == v1) || (u0 == v1 && u1 == v0),
"unexpected incident vertices {u0:?} and {u1:?} for multi-edge {:?} (index {i})",
edges[i]
);
}
}
pub fn incident_vertices<G: MakeTestGraph>()
@@ -389,11 +366,11 @@ where
G::Edge: Debug,
{
let (graph, _, edges, _) = G::standard();
for &(e, v1, v2) in edges.iter() {
let (u1, u2) = graph.incident_vertices(e);
for &(e, v0, v1) in edges.iter() {
let (u0, u1) = graph.incident_vertices(e);
assert!(
(u1 == v1 && u2 == v2) || (u1 == v2 && u2 == v1),
"unexpected incident vertices {u1:?} and {u2:?} for edge {e:?}"
(u0 == v0 && u1 == v1) || (u0 == v1 && u1 == v0),
"unexpected incident vertices {u0:?} and {u1:?} for edge {e:?}"
);
}
}
@@ -741,8 +718,8 @@ where
}
pub fn incidence_cursor_copy<G: MakeTestGraph>() {
let (graph, vertices, _) = G::two_edge_path();
let mut c1 = graph.incidence_cursor(vertices[1]);
let (graph, [_, v, _], _) = G::two_edge_path();
let mut c1 = graph.incidence_cursor(v);
assert!(c1.next(&graph).is_some(), "expected first incidence");
let mut c2 = c1;
assert!(