From dd33eb024d954bf92a828c0d58d8263db0978583 Mon Sep 17 00:00:00 2001 From: d0cd Date: Mon, 14 Nov 2022 22:11:35 -0800 Subject: [PATCH] Add test --- compiler/passes/src/common/graph/mod.rs | 51 ++++++++++++++----------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/compiler/passes/src/common/graph/mod.rs b/compiler/passes/src/common/graph/mod.rs index 087a7934a3..ea8ba68718 100644 --- a/compiler/passes/src/common/graph/mod.rs +++ b/compiler/passes/src/common/graph/mod.rs @@ -143,6 +143,14 @@ mod test { impl Node for u32 {} + fn check_post_order(graph: &DiGraph, expected: &[N]) { + let result = graph.post_order(); + assert!(result.is_ok()); + + let order: Vec = result.unwrap().into_iter().collect(); + assert_eq!(order, expected); + } + #[test] fn test_post_order() { let mut graph = DiGraph::::new(IndexSet::new()); @@ -153,21 +161,29 @@ mod test { graph.add_edge(3, 4); graph.add_edge(4, 5); - // At this point, the graph looks like: - // 1 - // / \ - // 2 3 - // \ / - // 4 - // | - // 5 + check_post_order(&graph, &[5, 4, 2, 3, 1]); - let result = graph.post_order(); - assert!(result.is_ok()); + let mut graph = DiGraph::::new(IndexSet::new()); - let order: Vec = result.unwrap().into_iter().collect(); - let expected = Vec::from([5u32, 4, 2, 3, 1]); - assert_eq!(order, expected); + // F -> B + graph.add_edge(6, 2); + // B -> A + graph.add_edge(2, 1); + // B -> D + graph.add_edge(2, 4); + // D -> C + graph.add_edge(4, 3); + // D -> E + graph.add_edge(4, 5); + // F -> G + graph.add_edge(6, 7); + // G -> I + graph.add_edge(7, 9); + // I -> H + graph.add_edge(9, 8); + + // A, C, E, D, B, H, I, G, F. + check_post_order(&graph, &[1, 3, 5, 4, 2, 8, 9, 7, 6]); } #[test] @@ -179,15 +195,6 @@ mod test { graph.add_edge(2, 4); graph.add_edge(4, 1); - // At this point, the graph looks like: - // 1 - // | - // 2 - // / \ - // 3 4 - // | - // 1 - let result = graph.post_order(); assert!(result.is_err());