@@ -18,10 +18,18 @@ fn create_graph() -> VecGraph<usize> {
1818 VecGraph :: new ( 7 , vec ! [ ( 0 , 1 ) , ( 1 , 2 ) , ( 1 , 3 ) , ( 3 , 4 ) , ( 5 , 1 ) ] )
1919}
2020
21+ fn create_graph_with_back_refs ( ) -> VecGraph < usize , true > {
22+ // Same as above
23+ VecGraph :: new ( 7 , vec ! [ ( 0 , 1 ) , ( 1 , 2 ) , ( 1 , 3 ) , ( 3 , 4 ) , ( 5 , 1 ) ] )
24+ }
25+
2126#[ test]
2227fn num_nodes ( ) {
2328 let graph = create_graph ( ) ;
2429 assert_eq ! ( graph. num_nodes( ) , 7 ) ;
30+
31+ let graph = create_graph_with_back_refs ( ) ;
32+ assert_eq ! ( graph. num_nodes( ) , 7 ) ;
2533}
2634
2735#[ test]
@@ -34,11 +42,36 @@ fn successors() {
3442 assert_eq ! ( graph. successors( 4 ) , & [ ] as & [ usize ] ) ;
3543 assert_eq ! ( graph. successors( 5 ) , & [ 1 ] ) ;
3644 assert_eq ! ( graph. successors( 6 ) , & [ ] as & [ usize ] ) ;
45+
46+ let graph = create_graph_with_back_refs ( ) ;
47+ assert_eq ! ( graph. successors( 0 ) , & [ 1 ] ) ;
48+ assert_eq ! ( graph. successors( 1 ) , & [ 2 , 3 ] ) ;
49+ assert_eq ! ( graph. successors( 2 ) , & [ ] as & [ usize ] ) ;
50+ assert_eq ! ( graph. successors( 3 ) , & [ 4 ] ) ;
51+ assert_eq ! ( graph. successors( 4 ) , & [ ] as & [ usize ] ) ;
52+ assert_eq ! ( graph. successors( 5 ) , & [ 1 ] ) ;
53+ assert_eq ! ( graph. successors( 6 ) , & [ ] as & [ usize ] ) ;
54+ }
55+
56+ #[ test]
57+ fn predecessors ( ) {
58+ let graph = create_graph_with_back_refs ( ) ;
59+ assert_eq ! ( graph. predecessors( 0 ) , & [ ] ) ;
60+ assert_eq ! ( graph. predecessors( 1 ) , & [ 0 , 5 ] ) ;
61+ assert_eq ! ( graph. predecessors( 2 ) , & [ 1 ] ) ;
62+ assert_eq ! ( graph. predecessors( 3 ) , & [ 1 ] ) ;
63+ assert_eq ! ( graph. predecessors( 4 ) , & [ 3 ] ) ;
64+ assert_eq ! ( graph. predecessors( 5 ) , & [ ] ) ;
65+ assert_eq ! ( graph. predecessors( 6 ) , & [ ] ) ;
3766}
3867
3968#[ test]
4069fn dfs ( ) {
4170 let graph = create_graph ( ) ;
4271 let dfs: Vec < _ > = graph:: depth_first_search ( & graph, 0 ) . collect ( ) ;
4372 assert_eq ! ( dfs, vec![ 0 , 1 , 3 , 4 , 2 ] ) ;
73+
74+ let graph = create_graph_with_back_refs ( ) ;
75+ let dfs: Vec < _ > = graph:: depth_first_search ( & graph, 0 ) . collect ( ) ;
76+ assert_eq ! ( dfs, vec![ 0 , 1 , 3 , 4 , 2 ] ) ;
4477}
0 commit comments