about summary refs log tree commit diff
path: root/compiler/rustc_data_structures/src/graph/vec_graph/tests.rs
blob: 78caf75f5b4e4a36e83620bc8cf7fc00414bef48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use super::*;
use crate::graph;

fn create_graph() -> VecGraph<usize> {
    // Create a simple graph
    //
    //          5
    //          |
    //          V
    //    0 --> 1 --> 2
    //          |
    //          v
    //          3 --> 4
    //
    //    6

    VecGraph::new(7, vec![(0, 1), (1, 2), (1, 3), (3, 4), (5, 1)])
}

fn create_graph_with_back_refs() -> VecGraph<usize, true> {
    // Same as above
    VecGraph::new(7, vec![(0, 1), (1, 2), (1, 3), (3, 4), (5, 1)])
}

#[test]
fn num_nodes() {
    let graph = create_graph();
    assert_eq!(graph.num_nodes(), 7);

    let graph = create_graph_with_back_refs();
    assert_eq!(graph.num_nodes(), 7);
}

#[test]
fn successors() {
    let graph = create_graph();
    assert_eq!(graph.successors(0), &[1]);
    assert_eq!(graph.successors(1), &[2, 3]);
    assert_eq!(graph.successors(2), &[] as &[usize]);
    assert_eq!(graph.successors(3), &[4]);
    assert_eq!(graph.successors(4), &[] as &[usize]);
    assert_eq!(graph.successors(5), &[1]);
    assert_eq!(graph.successors(6), &[] as &[usize]);

    let graph = create_graph_with_back_refs();
    assert_eq!(graph.successors(0), &[1]);
    assert_eq!(graph.successors(1), &[2, 3]);
    assert_eq!(graph.successors(2), &[] as &[usize]);
    assert_eq!(graph.successors(3), &[4]);
    assert_eq!(graph.successors(4), &[] as &[usize]);
    assert_eq!(graph.successors(5), &[1]);
    assert_eq!(graph.successors(6), &[] as &[usize]);
}

#[test]
fn predecessors() {
    let graph = create_graph_with_back_refs();
    assert_eq!(graph.predecessors(0), &[]);
    assert_eq!(graph.predecessors(1), &[0, 5]);
    assert_eq!(graph.predecessors(2), &[1]);
    assert_eq!(graph.predecessors(3), &[1]);
    assert_eq!(graph.predecessors(4), &[3]);
    assert_eq!(graph.predecessors(5), &[]);
    assert_eq!(graph.predecessors(6), &[]);
}

#[test]
fn dfs() {
    let graph = create_graph();
    let dfs: Vec<_> = graph::depth_first_search(&graph, 0).collect();
    assert_eq!(dfs, vec![0, 1, 3, 4, 2]);

    let graph = create_graph_with_back_refs();
    let dfs: Vec<_> = graph::depth_first_search(&graph, 0).collect();
    assert_eq!(dfs, vec![0, 1, 3, 4, 2]);
}