]>
git.proxmox.com Git - rustc.git/blob - src/librustc_data_structures/graph/implementation/tests.rs
1 use crate::graph
::implementation
::*;
4 type TestGraph
= Graph
<&'
static str, &'
static str>;
6 fn create_graph() -> TestGraph
{
7 let mut graph
= Graph
::new();
9 // Create a simple graph
19 let a
= graph
.add_node("A");
20 let b
= graph
.add_node("B");
21 let c
= graph
.add_node("C");
22 let d
= graph
.add_node("D");
23 let e
= graph
.add_node("E");
24 let f
= graph
.add_node("F");
26 graph
.add_edge(a
, b
, "AB");
27 graph
.add_edge(b
, c
, "BC");
28 graph
.add_edge(b
, d
, "BD");
29 graph
.add_edge(d
, e
, "DE");
30 graph
.add_edge(e
, c
, "EC");
31 graph
.add_edge(f
, b
, "FB");
38 let graph
= create_graph();
39 let expected
= ["A", "B", "C", "D", "E", "F"];
40 graph
.each_node(|idx
, node
| {
41 assert_eq
!(&expected
[idx
.0], graph
.node_data(idx
));
42 assert_eq
!(expected
[idx
.0], node
.data
);
49 let graph
= create_graph();
50 let expected
= ["AB", "BC", "BD", "DE", "EC", "FB"];
51 graph
.each_edge(|idx
, edge
| {
52 assert_eq
!(expected
[idx
.0], edge
.data
);
57 fn test_adjacent_edges
<N
: PartialEq
+ Debug
, E
: PartialEq
+ Debug
>(
59 start_index
: NodeIndex
,
61 expected_incoming
: &[(E
, N
)],
62 expected_outgoing
: &[(E
, N
)],
64 assert
!(graph
.node_data(start_index
) == &start_data
);
67 for (edge_index
, edge
) in graph
.incoming_edges(start_index
) {
68 assert
!(counter
< expected_incoming
.len());
70 "counter={:?} expected={:?} edge_index={:?} edge={:?}",
71 counter
, expected_incoming
[counter
], edge_index
, edge
73 match expected_incoming
[counter
] {
75 assert
!(e
== &edge
.data
);
76 assert
!(n
== graph
.node_data(edge
.source()));
77 assert
!(start_index
== edge
.target
);
82 assert_eq
!(counter
, expected_incoming
.len());
85 for (edge_index
, edge
) in graph
.outgoing_edges(start_index
) {
86 assert
!(counter
< expected_outgoing
.len());
88 "counter={:?} expected={:?} edge_index={:?} edge={:?}",
89 counter
, expected_outgoing
[counter
], edge_index
, edge
91 match expected_outgoing
[counter
] {
93 assert
!(e
== &edge
.data
);
94 assert
!(start_index
== edge
.source
);
95 assert
!(n
== graph
.node_data(edge
.target
));
100 assert_eq
!(counter
, expected_outgoing
.len());
104 fn each_adjacent_from_a() {
105 let graph
= create_graph();
106 test_adjacent_edges(&graph
, NodeIndex(0), "A", &[], &[("AB", "B")]);
110 fn each_adjacent_from_b() {
111 let graph
= create_graph();
116 &[("FB", "F"), ("AB", "A")],
117 &[("BD", "D"), ("BC", "C")],
122 fn each_adjacent_from_c() {
123 let graph
= create_graph();
124 test_adjacent_edges(&graph
, NodeIndex(2), "C", &[("EC", "E"), ("BC", "B")], &[]);
128 fn each_adjacent_from_d() {
129 let graph
= create_graph();
130 test_adjacent_edges(&graph
, NodeIndex(3), "D", &[("BD", "B")], &[("DE", "E")]);