Add From and FromGraph tests for FrozenGraph and for GraphTopologyAddition

The new tests are analogous to the existing Clone tests. Currently, only FrozenGraph implements From and FromGraph, but not GraphTopologyAddition, so the tests relying on GraphTopologyAddition are currently unused.
This commit is contained in:
2026-09-25 08:41:10 +02:00
parent d438a4288a
commit a9190f88db
4 changed files with 98 additions and 1 deletions
+2
View File
@@ -255,10 +255,12 @@ impl FromGraph for FrozenGraph {
#[cfg(test)]
mod trait_tests {
use crate::models::AppendGraph;
use super::FrozenGraph;
crate::graph_topology_tests!(FrozenGraph);
crate::graph_topology_clone_tests!(FrozenGraph);
crate::graph_topology_from_graph_tests!(FrozenGraph, AppendGraph);
}
#[cfg(test)]
+3 -1
View File
@@ -2,8 +2,10 @@
pub mod fixtures;
pub mod graph_topology_addition_clone_testing;
pub mod graph_topology_clone_testing;
pub mod graph_topology_addition_deletion_testing;
pub mod graph_topology_addition_from_graph_testing;
pub mod graph_topology_addition_testing;
pub mod graph_topology_clone_testing;
pub mod graph_topology_deletion_testing;
pub mod graph_topology_from_graph_testing;
pub mod graph_topology_testing;
@@ -0,0 +1,32 @@
use crate::testing::fixtures;
use crate::testing::fixtures::MakeTestGraph;
use crate::traits::{FromGraph, GraphTopologyAddition};
#[doc(hidden)]
#[macro_export]
macro_rules! graph_topology_addition_from_graph_tests {
($T:ty, $U:ty) => {
$crate::graph_topology_addition_from_graph_tests!(@wrap $T, $U,
from_graph_mutation_independent,
);
};
(@wrap $T:ty, $U:ty, $($name:ident),* $(,)?) => {
$(
#[test]
fn $name() {
$crate::testing::graph_topology_addition_from_graph_testing::$name::<$T, $U>();
}
)*
};
}
pub fn from_graph_mutation_independent<G, H>()
where
G: FromGraph + GraphTopologyAddition,
H: MakeTestGraph + GraphTopologyAddition,
{
let (mut source, vertices, _) = H::single_edge();
let (mut graph, vertex_map, _) = G::from_graph(&source);
fixtures::assert_mutation_independent(&mut source, &mut graph, &vertices, |v| vertex_map[v]);
}
@@ -0,0 +1,61 @@
use std::fmt::Debug;
use crate::testing::fixtures;
use crate::testing::fixtures::MakeTestGraph;
use crate::traits::{FromGraph, GraphTopology};
#[doc(hidden)]
#[macro_export]
macro_rules! graph_topology_from_graph_tests {
($T:ty, $U:ty) => {
$crate::graph_topology_from_graph_tests!(@wrap $T, $U,
from,
from_graph,
);
};
(@wrap $T:ty, $U:ty, $($name:ident),* $(,)?) => {
$(
#[test]
fn $name() {
$crate::testing::graph_topology_from_graph_testing::$name::<$T, $U>();
}
)*
};
}
pub fn from<G, H>()
where
G: for<'a> From<&'a H> + GraphTopology,
H: MakeTestGraph,
{
let (source, _, _, _) = H::standard();
let graph = G::from(&source);
assert_eq!(graph.vertex_count(), 10, "unexpected vertex count");
assert_eq!(graph.edge_count(), 18, "unexpected edge count");
let mut expected: Vec<_> = source.vertices().map(|v| source.degree(v)).collect();
let mut actual: Vec<_> = graph.vertices().map(|v| graph.degree(v)).collect();
expected.sort();
actual.sort();
assert_eq!(actual, expected, "degree sequence should match");
}
pub fn from_graph<G: FromGraph, H: MakeTestGraph>()
where
G::Vertex: Debug,
G::Edge: Debug,
H::Vertex: Debug,
H::Edge: Debug,
{
let (source, vertices, edges, _) = H::standard();
let (graph, vertex_map, edge_map) = G::from_graph(&source);
fixtures::assert_isomorphic(
&source,
&graph,
&vertices,
|v| vertex_map[v],
&edges,
|e| edge_map[e],
);
}