From 46d81b7985229f97e3c2effc005774358e8b0260 Mon Sep 17 00:00:00 2001 From: Indeximal Date: Wed, 8 Jun 2022 00:31:33 +0200 Subject: [PATCH] Added test for GraphMap::from_graph --- src/graphmap.rs | 5 ++--- tests/graphmap.rs | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/graphmap.rs b/src/graphmap.rs index 92fe82590..4a9860c24 100644 --- a/src/graphmap.rs +++ b/src/graphmap.rs @@ -522,9 +522,8 @@ where /// Creates a `GraphMap` that corresponds to the given `Graph`. /// - /// Note that node and edge indices in the `Graph` are lost in this process, - /// but the weights are kept. - /// **Warning**: Parallel edges are lost, only the last one remains. + /// **Warning**: Nodes with the same weight are merged and only the last parallel edge + /// is kept, because node and edge indices in the `Graph` are lost in this process. /// /// Computes in **O(|V| + |E|)** time (average). pub fn from_graph(graph: Graph) -> Self diff --git a/tests/graphmap.rs b/tests/graphmap.rs index 07589419b..1426ea4db 100644 --- a/tests/graphmap.rs +++ b/tests/graphmap.rs @@ -119,7 +119,7 @@ fn remove_node() { graph.remove_node(2); let neighbors: Vec = graph.neighbors(1).collect(); - assert_eq!(neighbors, [] as [u32; 0]); + assert_eq!(neighbors, []); let edges: Vec<(u32, u32, _)> = graph.all_edges().collect(); assert_eq!(edges, []); @@ -325,6 +325,25 @@ fn test_into_graph() { } } +#[test] +fn test_from_graph() { + let mut gr: Graph = Graph::new(); + let node_a = gr.add_node(12); + let node_b = gr.add_node(13); + let node_c = gr.add_node(14); + gr.add_edge(node_a, node_b, 1000); + gr.add_edge(node_b, node_c, 999); + gr.add_edge(node_c, node_a, 1111); + gr.add_node(42); + let gr = gr; + + let graph: GraphMap = GraphMap::from_graph(gr.clone()); + println!("{}", Dot::new(&gr)); + println!("{}", Dot::new(&graph)); + + assert!(petgraph::algo::is_isomorphic(&gr, &graph)); +} + #[test] fn test_all_edges_mut() { // graph with edge weights equal to in+out