Add Clone to graph models, and tests

This commit is contained in:
2026-09-19 17:19:47 +02:00
parent 4c6a8a43e4
commit e593298eba
6 changed files with 164 additions and 0 deletions
@@ -0,0 +1,56 @@
use crate::testing::fixtures::MakeTestGraph;
use crate::traits::GraphTopologyAddition;
#[doc(hidden)]
#[macro_export]
macro_rules! graph_topology_addition_clone_tests {
($T:ty) => {
$crate::graph_topology_addition_clone_tests!(@wrap $T,
clone_independent,
);
};
(@wrap $T:ty, $($name:ident),* $(,)?) => {
$(
#[test]
fn $name() {
$crate::testing::graph_topology_addition_clone_testing::$name::<$T>();
}
)*
};
}
pub fn clone_independent<G: Clone + GraphTopologyAddition>() {
let (mut graph, [u, v], _) = G::single_edge();
let mut clone = graph.clone();
let w = clone.add_vertex();
clone.add_edge(u, w);
assert_eq!(clone.vertex_count(), 3, "precondition: vertex count of clone");
assert_eq!(clone.edge_count(), 2, "precondition: edge count of clone");
assert_eq!(
graph.vertex_count(),
2,
"mutating the clone should not affect the vertex count of the original"
);
assert_eq!(
graph.edge_count(),
1,
"mutating the clone should not affect the edge count of the original"
);
let x = graph.add_vertex();
graph.add_edge(v, x);
assert_eq!(graph.vertex_count(), 3, "precondition: vertex count of original");
assert_eq!(graph.edge_count(), 2, "precondition: edge count of original");
assert_eq!(
clone.vertex_count(),
3,
"mutating the original should not affect the vertex count of the clone"
);
assert_eq!(
clone.edge_count(),
2,
"mutating the original should not affect the edge count of the clone"
);
}