Skip to content

Commit

Permalink
Added test for GraphMap::from_graph
Browse files Browse the repository at this point in the history
  • Loading branch information
Indeximal committed Jun 7, 2022
1 parent 00ab85b commit 46d81b7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/graphmap.rs
Expand Up @@ -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<Ix>(graph: Graph<N, E, Ty, Ix>) -> Self
Expand Down
21 changes: 20 additions & 1 deletion tests/graphmap.rs
Expand Up @@ -119,7 +119,7 @@ fn remove_node() {
graph.remove_node(2);

let neighbors: Vec<u32> = 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, []);
Expand Down Expand Up @@ -325,6 +325,25 @@ fn test_into_graph() {
}
}

#[test]
fn test_from_graph() {
let mut gr: Graph<u32, u32, Directed> = 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<u32, u32, Directed> = 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
Expand Down

0 comments on commit 46d81b7

Please sign in to comment.