From a9190f88dbe364f421ade65ac9dfda5929998fee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Fri, 25 Sep 2026 08:41:10 +0200 Subject: [PATCH] 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. --- src/models/frozen_graph.rs | 2 + src/testing.rs | 4 +- ...ph_topology_addition_from_graph_testing.rs | 32 ++++++++++ .../graph_topology_from_graph_testing.rs | 61 +++++++++++++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/testing/graph_topology_addition_from_graph_testing.rs create mode 100644 src/testing/graph_topology_from_graph_testing.rs diff --git a/src/models/frozen_graph.rs b/src/models/frozen_graph.rs index 220d6c1..9e86039 100644 --- a/src/models/frozen_graph.rs +++ b/src/models/frozen_graph.rs @@ -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)] diff --git a/src/testing.rs b/src/testing.rs index 1049f84..8a841dd 100644 --- a/src/testing.rs +++ b/src/testing.rs @@ -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; diff --git a/src/testing/graph_topology_addition_from_graph_testing.rs b/src/testing/graph_topology_addition_from_graph_testing.rs new file mode 100644 index 0000000..9b5a5b4 --- /dev/null +++ b/src/testing/graph_topology_addition_from_graph_testing.rs @@ -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() +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]); +} diff --git a/src/testing/graph_topology_from_graph_testing.rs b/src/testing/graph_topology_from_graph_testing.rs new file mode 100644 index 0000000..82ef3e0 --- /dev/null +++ b/src/testing/graph_topology_from_graph_testing.rs @@ -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() +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() +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], + ); +}