2015-10-19 19:15:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/utils/SkRandom.h"
|
2020-11-16 19:58:56 +00:00
|
|
|
#include "src/gpu/GrTTopoSort.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
2015-10-19 19:15:55 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2015-10-19 19:15:55 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
typedef void (*CreateGraphPF)(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph);
|
2015-10-19 19:15:55 +00:00
|
|
|
|
|
|
|
/* Simple diamond
|
|
|
|
* 3
|
2020-11-17 18:22:05 +00:00
|
|
|
* . .
|
2015-10-19 19:15:55 +00:00
|
|
|
* / \
|
|
|
|
* 1 2
|
2020-11-17 18:22:05 +00:00
|
|
|
* . .
|
2015-10-19 19:15:55 +00:00
|
|
|
* \ /
|
|
|
|
* 0
|
|
|
|
*/
|
2019-03-20 16:12:10 +00:00
|
|
|
static void create_graph0(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) {
|
|
|
|
ToolUtils::TopoTestNode::AllocNodes(graph, 4);
|
2015-10-19 19:15:55 +00:00
|
|
|
|
2018-02-07 22:08:21 +00:00
|
|
|
(*graph)[0]->dependsOn((*graph)[1].get());
|
|
|
|
(*graph)[0]->dependsOn((*graph)[2].get());
|
|
|
|
(*graph)[1]->dependsOn((*graph)[3].get());
|
|
|
|
(*graph)[2]->dependsOn((*graph)[3].get());
|
2015-10-19 19:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Simple chain
|
|
|
|
* 3
|
2020-11-17 18:22:05 +00:00
|
|
|
* ^
|
2015-10-19 19:15:55 +00:00
|
|
|
* |
|
|
|
|
* 2
|
2020-11-17 18:22:05 +00:00
|
|
|
* ^
|
2015-10-19 19:15:55 +00:00
|
|
|
* |
|
|
|
|
* 1
|
2020-11-17 18:22:05 +00:00
|
|
|
* ^
|
2015-10-19 19:15:55 +00:00
|
|
|
* |
|
|
|
|
* 0
|
|
|
|
*/
|
2019-03-20 16:12:10 +00:00
|
|
|
static void create_graph1(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) {
|
|
|
|
ToolUtils::TopoTestNode::AllocNodes(graph, 4);
|
2015-10-19 19:15:55 +00:00
|
|
|
|
2018-02-07 22:08:21 +00:00
|
|
|
(*graph)[0]->dependsOn((*graph)[1].get());
|
|
|
|
(*graph)[1]->dependsOn((*graph)[2].get());
|
|
|
|
(*graph)[2]->dependsOn((*graph)[3].get());
|
2015-10-19 19:15:55 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 18:22:05 +00:00
|
|
|
/* Simple Loop
|
2015-10-19 19:15:55 +00:00
|
|
|
* 2
|
2020-11-17 18:22:05 +00:00
|
|
|
* / .
|
2015-10-19 19:15:55 +00:00
|
|
|
* / \
|
2020-11-17 18:22:05 +00:00
|
|
|
* . \
|
|
|
|
* 0 ---> 1
|
2015-10-19 19:15:55 +00:00
|
|
|
*/
|
2019-03-20 16:12:10 +00:00
|
|
|
static void create_graph2(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) {
|
|
|
|
ToolUtils::TopoTestNode::AllocNodes(graph, 3);
|
2015-10-19 19:15:55 +00:00
|
|
|
|
2018-02-07 22:08:21 +00:00
|
|
|
(*graph)[0]->dependsOn((*graph)[1].get());
|
|
|
|
(*graph)[1]->dependsOn((*graph)[2].get());
|
|
|
|
(*graph)[2]->dependsOn((*graph)[0].get());
|
2015-10-19 19:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Double diamond
|
|
|
|
* 6
|
2020-11-17 18:22:05 +00:00
|
|
|
* . .
|
2015-10-19 19:15:55 +00:00
|
|
|
* / \
|
|
|
|
* 4 5
|
2020-11-17 18:22:05 +00:00
|
|
|
* . .
|
2015-10-19 19:15:55 +00:00
|
|
|
* \ /
|
|
|
|
* 3
|
2020-11-17 18:22:05 +00:00
|
|
|
* . .
|
2015-10-19 19:15:55 +00:00
|
|
|
* / \
|
|
|
|
* 1 2
|
2020-11-17 18:22:05 +00:00
|
|
|
* . .
|
2015-10-19 19:15:55 +00:00
|
|
|
* \ /
|
|
|
|
* 0
|
|
|
|
*/
|
2019-03-20 16:12:10 +00:00
|
|
|
static void create_graph3(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) {
|
|
|
|
ToolUtils::TopoTestNode::AllocNodes(graph, 7);
|
2015-10-19 19:15:55 +00:00
|
|
|
|
2018-02-07 22:08:21 +00:00
|
|
|
(*graph)[0]->dependsOn((*graph)[1].get());
|
|
|
|
(*graph)[0]->dependsOn((*graph)[2].get());
|
|
|
|
(*graph)[1]->dependsOn((*graph)[3].get());
|
|
|
|
(*graph)[2]->dependsOn((*graph)[3].get());
|
2015-10-19 19:15:55 +00:00
|
|
|
|
2018-02-07 22:08:21 +00:00
|
|
|
(*graph)[3]->dependsOn((*graph)[4].get());
|
|
|
|
(*graph)[3]->dependsOn((*graph)[5].get());
|
|
|
|
(*graph)[4]->dependsOn((*graph)[6].get());
|
|
|
|
(*graph)[5]->dependsOn((*graph)[6].get());
|
2015-10-19 19:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Two independent diamonds
|
|
|
|
* 3 7
|
2020-11-17 18:22:05 +00:00
|
|
|
* . . . .
|
2015-10-19 19:15:55 +00:00
|
|
|
* / \ / \
|
|
|
|
* 1 2 5 6
|
2020-11-17 18:22:05 +00:00
|
|
|
* . . . .
|
2015-10-19 19:15:55 +00:00
|
|
|
* \ / \ /
|
|
|
|
* 0 4
|
|
|
|
*/
|
2019-03-20 16:12:10 +00:00
|
|
|
static void create_graph4(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) {
|
|
|
|
ToolUtils::TopoTestNode::AllocNodes(graph, 8);
|
2015-10-19 19:15:55 +00:00
|
|
|
|
2018-02-07 22:08:21 +00:00
|
|
|
(*graph)[0]->dependsOn((*graph)[1].get());
|
|
|
|
(*graph)[0]->dependsOn((*graph)[2].get());
|
|
|
|
(*graph)[1]->dependsOn((*graph)[3].get());
|
|
|
|
(*graph)[2]->dependsOn((*graph)[3].get());
|
2015-10-19 19:15:55 +00:00
|
|
|
|
2018-02-07 22:08:21 +00:00
|
|
|
(*graph)[4]->dependsOn((*graph)[5].get());
|
|
|
|
(*graph)[4]->dependsOn((*graph)[6].get());
|
|
|
|
(*graph)[5]->dependsOn((*graph)[7].get());
|
|
|
|
(*graph)[6]->dependsOn((*graph)[7].get());
|
2015-10-19 19:15:55 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 18:22:05 +00:00
|
|
|
/* Two linked diamonds w/ two loops
|
|
|
|
* 5 6
|
|
|
|
* / . . \
|
|
|
|
* . \ / .
|
|
|
|
* 2 3 4
|
|
|
|
* \ . /
|
|
|
|
* . / \ .
|
|
|
|
* 0 1
|
|
|
|
*/
|
|
|
|
static void create_graph5(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) {
|
|
|
|
ToolUtils::TopoTestNode::AllocNodes(graph, 7);
|
|
|
|
|
|
|
|
(*graph)[0]->dependsOn((*graph)[3].get());
|
|
|
|
(*graph)[1]->dependsOn((*graph)[3].get());
|
|
|
|
(*graph)[2]->dependsOn((*graph)[0].get());
|
|
|
|
(*graph)[3]->dependsOn((*graph)[5].get());
|
|
|
|
(*graph)[3]->dependsOn((*graph)[6].get());
|
|
|
|
(*graph)[4]->dependsOn((*graph)[1].get());
|
|
|
|
(*graph)[5]->dependsOn((*graph)[2].get());
|
|
|
|
(*graph)[6]->dependsOn((*graph)[4].get());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Two disjoint loops
|
|
|
|
* 2 5
|
|
|
|
* / . / .
|
|
|
|
* / \ / \
|
|
|
|
* . \ . \
|
|
|
|
* 0 ---> 1 3 ---> 4
|
|
|
|
*/
|
|
|
|
static void create_graph6(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) {
|
|
|
|
ToolUtils::TopoTestNode::AllocNodes(graph, 6);
|
|
|
|
|
|
|
|
(*graph)[0]->dependsOn((*graph)[1].get());
|
|
|
|
(*graph)[1]->dependsOn((*graph)[2].get());
|
|
|
|
(*graph)[2]->dependsOn((*graph)[0].get());
|
|
|
|
|
|
|
|
(*graph)[3]->dependsOn((*graph)[4].get());
|
|
|
|
(*graph)[4]->dependsOn((*graph)[5].get());
|
|
|
|
(*graph)[5]->dependsOn((*graph)[3].get());
|
|
|
|
}
|
|
|
|
|
2015-10-19 19:15:55 +00:00
|
|
|
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 },
|
2020-11-17 18:22:05 +00:00
|
|
|
{ create_graph5, false },
|
|
|
|
{ create_graph6, false },
|
2015-10-19 19:15:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(tests); ++i) {
|
2019-03-20 16:12:10 +00:00
|
|
|
SkTArray<sk_sp<ToolUtils::TopoTestNode>> graph;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-19 19:15:55 +00:00
|
|
|
(tests[i].fCreate)(&graph);
|
|
|
|
|
2020-11-17 18:22:05 +00:00
|
|
|
const int numNodes = graph.count();
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::TopoTestNode::Shuffle(&graph, &rand);
|
2015-10-19 19:15:55 +00:00
|
|
|
|
2020-11-16 19:58:56 +00:00
|
|
|
bool actualResult = GrTTopoSort<ToolUtils::TopoTestNode>(&graph);
|
2015-10-19 19:15:55 +00:00
|
|
|
REPORTER_ASSERT(reporter, actualResult == tests[i].fExpectedResult);
|
2020-11-17 18:22:05 +00:00
|
|
|
REPORTER_ASSERT(reporter, numNodes == graph.count());
|
2015-10-19 19:15:55 +00:00
|
|
|
|
|
|
|
if (tests[i].fExpectedResult) {
|
2020-11-17 18:22:05 +00:00
|
|
|
for (const auto& node : graph) {
|
|
|
|
REPORTER_ASSERT(reporter, node->check());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// When the topological sort fails all the nodes should still appear in the result
|
|
|
|
// but their order can be somewhat arbitrary.
|
|
|
|
std::vector<bool> seen(numNodes, false);
|
|
|
|
|
|
|
|
for (const auto& node : graph) {
|
|
|
|
SkASSERT(node);
|
|
|
|
SkASSERT(!seen[node->id()]);
|
|
|
|
seen[node->id()] = true;
|
2015-10-19 19:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//SkDEBUGCODE(print(graph);)
|
|
|
|
}
|
|
|
|
}
|