aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/TopoSortTest.cpp
blob: 9cee4853e0e522ec5b4973746360b16a631dca05 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkRandom.h"
#include "SkTTopoSort.h"
#include "Test.h"

#include "sk_tool_utils.h"

typedef void (*CreateGraphPF)(SkTDArray<sk_tool_utils::TopoTestNode*>* graph);

/* Simple diamond
 *       3
 *     /   \
 *    1     2
 *     \   /
 *       0
 */
static void create_graph0(SkTDArray<sk_tool_utils::TopoTestNode*>* graph) {
    sk_tool_utils::TopoTestNode::AllocNodes(graph, 4);

    (*graph)[0]->dependsOn((*graph)[1]);
    (*graph)[0]->dependsOn((*graph)[2]);
    (*graph)[1]->dependsOn((*graph)[3]);
    (*graph)[2]->dependsOn((*graph)[3]);
}

/* Simple chain
 *     3
 *     |
 *     2
 *     |
 *     1
 *     |
 *     0
 */
static void create_graph1(SkTDArray<sk_tool_utils::TopoTestNode*>* graph) {
    sk_tool_utils::TopoTestNode::AllocNodes(graph, 4);

    (*graph)[0]->dependsOn((*graph)[1]);
    (*graph)[1]->dependsOn((*graph)[2]);
    (*graph)[2]->dependsOn((*graph)[3]);
}

/* Loop
 *       2
 *     /   \
 *    0 --- 1
 */
static void create_graph2(SkTDArray<sk_tool_utils::TopoTestNode*>* graph) {
    sk_tool_utils::TopoTestNode::AllocNodes(graph, 3);

    (*graph)[0]->dependsOn((*graph)[1]);
    (*graph)[1]->dependsOn((*graph)[2]);
    (*graph)[2]->dependsOn((*graph)[0]);
}

/* Double diamond
 *       6
 *     /   \
 *    4     5
 *     \   /
 *       3
 *     /   \
 *    1     2
 *     \   /
 *       0
 */
static void create_graph3(SkTDArray<sk_tool_utils::TopoTestNode*>* graph) {
    sk_tool_utils::TopoTestNode::AllocNodes(graph, 7);

    (*graph)[0]->dependsOn((*graph)[1]);
    (*graph)[0]->dependsOn((*graph)[2]);
    (*graph)[1]->dependsOn((*graph)[3]);
    (*graph)[2]->dependsOn((*graph)[3]);

    (*graph)[3]->dependsOn((*graph)[4]);
    (*graph)[3]->dependsOn((*graph)[5]);
    (*graph)[4]->dependsOn((*graph)[6]);
    (*graph)[5]->dependsOn((*graph)[6]);
}

/* Two independent diamonds
 *       3           7
 *     /   \       /   \
 *    1     2     5     6
 *     \   /       \   /
 *       0           4
 */
static void create_graph4(SkTDArray<sk_tool_utils::TopoTestNode*>* graph) {
    sk_tool_utils::TopoTestNode::AllocNodes(graph, 8);

    (*graph)[0]->dependsOn((*graph)[1]);
    (*graph)[0]->dependsOn((*graph)[2]);
    (*graph)[1]->dependsOn((*graph)[3]);
    (*graph)[2]->dependsOn((*graph)[3]);

    (*graph)[4]->dependsOn((*graph)[5]);
    (*graph)[4]->dependsOn((*graph)[6]);
    (*graph)[5]->dependsOn((*graph)[7]);
    (*graph)[6]->dependsOn((*graph)[7]);
}

DEF_TEST(TopoSort, reporter) {
    SkRandom rand;

    struct {
        CreateGraphPF fCreate;
        bool          fExpectedResult;
    } tests[] = {
        { create_graph0, true  },
        { create_graph1, true  },
        { create_graph2, false },
        { create_graph3, true  },
        { create_graph4, true  },
    };

    for (size_t i = 0; i < SK_ARRAY_COUNT(tests); ++i) {
        SkTDArray<sk_tool_utils::TopoTestNode*> graph;

        (tests[i].fCreate)(&graph);

        sk_tool_utils::TopoTestNode::Shuffle(&graph, &rand);

        bool actualResult = SkTTopoSort<sk_tool_utils::TopoTestNode>(&graph);
        REPORTER_ASSERT(reporter, actualResult == tests[i].fExpectedResult);

        if (tests[i].fExpectedResult) {
            for (int j = 0; j < graph.count(); ++j) {
                REPORTER_ASSERT(reporter, graph[j]->check());
            }
        }

        //SkDEBUGCODE(print(graph);)
        sk_tool_utils::TopoTestNode::DeallocNodes(&graph);
    }
}