]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_data_structures/src/graph/implementation/tests.rs
New upstream version 1.76.0+dfsg1
[rustc.git] / compiler / rustc_data_structures / src / graph / implementation / tests.rs
CommitLineData
9fa01778 1use crate::graph::implementation::*;
d9579d0f 2
d9579d0f
AL
3type TestGraph = Graph<&'static str, &'static str>;
4
5fn create_graph() -> TestGraph {
6 let mut graph = Graph::new();
7
8 // Create a simple graph
9 //
476ff2be
SL
10 // F
11 // |
12 // V
13 // A --> B --> C
14 // | ^
15 // v |
16 // D --> E
d9579d0f
AL
17
18 let a = graph.add_node("A");
19 let b = graph.add_node("B");
20 let c = graph.add_node("C");
21 let d = graph.add_node("D");
22 let e = graph.add_node("E");
23 let f = graph.add_node("F");
24
25 graph.add_edge(a, b, "AB");
26 graph.add_edge(b, c, "BC");
27 graph.add_edge(b, d, "BD");
28 graph.add_edge(d, e, "DE");
29 graph.add_edge(e, c, "EC");
30 graph.add_edge(f, b, "FB");
31
32 return graph;
33}
34
35#[test]
36fn each_node() {
37 let graph = create_graph();
38 let expected = ["A", "B", "C", "D", "E", "F"];
39 graph.each_node(|idx, node| {
40 assert_eq!(&expected[idx.0], graph.node_data(idx));
41 assert_eq!(expected[idx.0], node.data);
42 true
43 });
44}
45
46#[test]
47fn each_edge() {
48 let graph = create_graph();
49 let expected = ["AB", "BC", "BD", "DE", "EC", "FB"];
50 graph.each_edge(|idx, edge| {
d9579d0f
AL
51 assert_eq!(expected[idx.0], edge.data);
52 true
53 });
54}
55
dfeec247
XL
56fn test_adjacent_edges<N: PartialEq + Debug, E: PartialEq + Debug>(
57 graph: &Graph<N, E>,
58 start_index: NodeIndex,
59 start_data: N,
60 expected_incoming: &[(E, N)],
61 expected_outgoing: &[(E, N)],
62) {
d9579d0f
AL
63 assert!(graph.node_data(start_index) == &start_data);
64
65 let mut counter = 0;
66 for (edge_index, edge) in graph.incoming_edges(start_index) {
d9579d0f 67 assert!(counter < expected_incoming.len());
dfeec247
XL
68 debug!(
69 "counter={:?} expected={:?} edge_index={:?} edge={:?}",
70 counter, expected_incoming[counter], edge_index, edge
71 );
9c376795
FG
72 match &expected_incoming[counter] {
73 (e, n) => {
d9579d0f
AL
74 assert!(e == &edge.data);
75 assert!(n == graph.node_data(edge.source()));
76 assert!(start_index == edge.target);
77 }
78 }
79 counter += 1;
80 }
81 assert_eq!(counter, expected_incoming.len());
82
83 let mut counter = 0;
84 for (edge_index, edge) in graph.outgoing_edges(start_index) {
d9579d0f 85 assert!(counter < expected_outgoing.len());
dfeec247
XL
86 debug!(
87 "counter={:?} expected={:?} edge_index={:?} edge={:?}",
88 counter, expected_outgoing[counter], edge_index, edge
89 );
9c376795
FG
90 match &expected_outgoing[counter] {
91 (e, n) => {
d9579d0f
AL
92 assert!(e == &edge.data);
93 assert!(start_index == edge.source);
94 assert!(n == graph.node_data(edge.target));
95 }
96 }
97 counter += 1;
98 }
99 assert_eq!(counter, expected_outgoing.len());
100}
101
102#[test]
103fn each_adjacent_from_a() {
104 let graph = create_graph();
54a0048b 105 test_adjacent_edges(&graph, NodeIndex(0), "A", &[], &[("AB", "B")]);
d9579d0f
AL
106}
107
108#[test]
109fn each_adjacent_from_b() {
110 let graph = create_graph();
dfeec247
XL
111 test_adjacent_edges(
112 &graph,
113 NodeIndex(1),
114 "B",
115 &[("FB", "F"), ("AB", "A")],
116 &[("BD", "D"), ("BC", "C")],
117 );
d9579d0f
AL
118}
119
120#[test]
121fn each_adjacent_from_c() {
122 let graph = create_graph();
54a0048b 123 test_adjacent_edges(&graph, NodeIndex(2), "C", &[("EC", "E"), ("BC", "B")], &[]);
d9579d0f
AL
124}
125
126#[test]
127fn each_adjacent_from_d() {
128 let graph = create_graph();
54a0048b 129 test_adjacent_edges(&graph, NodeIndex(3), "D", &[("BD", "B")], &[("DE", "E")]);
d9579d0f 130}