aboutsummaryrefslogtreecommitdiffstats
path: root/yolean/src/test/java/com/yahoo/yolean/chain/DirectedGraphTest.java
blob: b37c57b536b5eb44734b80db6b34ad7955d24fee (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.yolean.chain;

import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;

import static org.junit.Assert.assertTrue;

public class DirectedGraphTest {

    private DirectedGraph graph;
    private final Vertex[] v = new Vertex[10];

    @Before
    public void setup() {
        for (int i = 0; i < v.length; i++) {
            v[i] = new TestVertex(i);
        }

        graph = new DirectedGraph();
    }

    @Test
    public void before_all_are_prioritized_first() {
        graph.addVertex(v[0]);
        graph.addBeginningVertex(v[1]);

        assertTrue(graph.topologicalSort().containsAll(Arrays.asList(v[1], v[0])));
    }

    @Test
    public void vertex_can_be_placed_before_before_all_vertices() {
        graph.addVertex(v[0]);
        graph.addBeginningVertex(v[1]);
        graph.addEdge(v[0], v[1]);

        assertTrue(graph.topologicalSort().containsAll(Arrays.asList(v[0], v[1])));
    }

    static class TestVertex implements Vertex {

        private final int id;

        TestVertex(int id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "Vertex{" + id + '}';
        }
    }
}