1 use crate::graph::tests::TestGraph;
6 let graph = TestGraph::new(0, &[(0, 1), (0, 2), (1, 3), (2, 3)]);
7 let sccs: Sccs<_, usize> = Sccs::new(&graph);
8 assert_eq!(sccs.num_sccs(), 4);
9 assert_eq!(sccs.num_sccs(), 4);
14 // The order in which things will be visited is important to this
21 // and at this point detect a cycle. 2 will return back to 1 which
22 // will visit 3. 3 will visit 2 before the cycle is complete, and
23 // hence it too will return a cycle.
34 let graph = TestGraph::new(0, &[
41 let sccs: Sccs<_, usize> = Sccs::new(&graph);
42 assert_eq!(sccs.num_sccs(), 1);
46 fn test_three_sccs() {
56 let graph = TestGraph::new(0, &[
62 let sccs: Sccs<_, usize> = Sccs::new(&graph);
63 assert_eq!(sccs.num_sccs(), 3);
64 assert_eq!(sccs.scc(0), 1);
65 assert_eq!(sccs.scc(1), 0);
66 assert_eq!(sccs.scc(2), 0);
67 assert_eq!(sccs.scc(3), 2);
68 assert_eq!(sccs.successors(0), &[]);
69 assert_eq!(sccs.successors(1), &[0]);
70 assert_eq!(sccs.successors(2), &[0]);
74 fn test_find_state_2() {
75 // The order in which things will be visited is important to this
76 // test. It tests part of the `find_state` behavior. Here is the
89 let graph = TestGraph::new(0, &[
99 // For this graph, we will start in our DFS by visiting:
103 // and at this point detect a cycle. The state of 2 will thus be
104 // `InCycleWith { 1 }`. We will then visit the 1 -> 3 edge, which
105 // will attempt to visit 0 as well, thus going to the state
106 // `InCycleWith { 0 }`. Finally, node 1 will complete; the lowest
107 // depth of any successor was 3 which had depth 0, and thus it
108 // will be in the state `InCycleWith { 3 }`.
110 // When we finally traverse the `0 -> 4` edge and then visit node 2,
111 // the states of the nodes are:
113 // 0 BeingVisited { 0 }
114 // 1 InCycleWith { 3 }
115 // 2 InCycleWith { 1 }
116 // 3 InCycleWith { 0 }
118 // and hence 4 will traverse the links, finding an ultimate depth of 0.
119 // If will also collapse the states to the following:
121 // 0 BeingVisited { 0 }
122 // 1 InCycleWith { 3 }
123 // 2 InCycleWith { 1 }
124 // 3 InCycleWith { 0 }
126 let sccs: Sccs<_, usize> = Sccs::new(&graph);
127 assert_eq!(sccs.num_sccs(), 1);
128 assert_eq!(sccs.scc(0), 0);
129 assert_eq!(sccs.scc(1), 0);
130 assert_eq!(sccs.scc(2), 0);
131 assert_eq!(sccs.scc(3), 0);
132 assert_eq!(sccs.scc(4), 0);
133 assert_eq!(sccs.successors(0), &[]);
137 fn test_find_state_3() {
148 let graph = TestGraph::new(0, &[
158 let sccs: Sccs<_, usize> = Sccs::new(&graph);
159 assert_eq!(sccs.num_sccs(), 2);
160 assert_eq!(sccs.scc(0), 0);
161 assert_eq!(sccs.scc(1), 0);
162 assert_eq!(sccs.scc(2), 0);
163 assert_eq!(sccs.scc(3), 0);
164 assert_eq!(sccs.scc(4), 0);
165 assert_eq!(sccs.scc(5), 1);
166 assert_eq!(sccs.successors(0), &[]);
167 assert_eq!(sccs.successors(1), &[0]);