blob: 9fc0f01b0e070b5195c2f8960c5958264b569767 [file] [log] [blame]
robertphillips423f6462015-10-19 12:15:55 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/utils/SkRandom.h"
9#include "src/core/SkTTopoSort.h"
10#include "tests/Test.h"
robertphillips423f6462015-10-19 12:15:55 -070011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "tools/ToolUtils.h"
robertphillips423f6462015-10-19 12:15:55 -070013
Mike Kleinea3f0142019-03-20 11:12:10 -050014typedef void (*CreateGraphPF)(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph);
robertphillips423f6462015-10-19 12:15:55 -070015
16/* Simple diamond
17 * 3
18 * / \
19 * 1 2
20 * \ /
21 * 0
22 */
Mike Kleinea3f0142019-03-20 11:12:10 -050023static void create_graph0(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) {
24 ToolUtils::TopoTestNode::AllocNodes(graph, 4);
robertphillips423f6462015-10-19 12:15:55 -070025
Robert Phillips4150eea2018-02-07 17:08:21 -050026 (*graph)[0]->dependsOn((*graph)[1].get());
27 (*graph)[0]->dependsOn((*graph)[2].get());
28 (*graph)[1]->dependsOn((*graph)[3].get());
29 (*graph)[2]->dependsOn((*graph)[3].get());
robertphillips423f6462015-10-19 12:15:55 -070030}
31
32/* Simple chain
33 * 3
34 * |
35 * 2
36 * |
37 * 1
38 * |
39 * 0
40 */
Mike Kleinea3f0142019-03-20 11:12:10 -050041static void create_graph1(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) {
42 ToolUtils::TopoTestNode::AllocNodes(graph, 4);
robertphillips423f6462015-10-19 12:15:55 -070043
Robert Phillips4150eea2018-02-07 17:08:21 -050044 (*graph)[0]->dependsOn((*graph)[1].get());
45 (*graph)[1]->dependsOn((*graph)[2].get());
46 (*graph)[2]->dependsOn((*graph)[3].get());
robertphillips423f6462015-10-19 12:15:55 -070047}
48
49/* Loop
50 * 2
51 * / \
52 * 0 --- 1
53 */
Mike Kleinea3f0142019-03-20 11:12:10 -050054static void create_graph2(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) {
55 ToolUtils::TopoTestNode::AllocNodes(graph, 3);
robertphillips423f6462015-10-19 12:15:55 -070056
Robert Phillips4150eea2018-02-07 17:08:21 -050057 (*graph)[0]->dependsOn((*graph)[1].get());
58 (*graph)[1]->dependsOn((*graph)[2].get());
59 (*graph)[2]->dependsOn((*graph)[0].get());
robertphillips423f6462015-10-19 12:15:55 -070060}
61
62/* Double diamond
63 * 6
64 * / \
65 * 4 5
66 * \ /
67 * 3
68 * / \
69 * 1 2
70 * \ /
71 * 0
72 */
Mike Kleinea3f0142019-03-20 11:12:10 -050073static void create_graph3(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) {
74 ToolUtils::TopoTestNode::AllocNodes(graph, 7);
robertphillips423f6462015-10-19 12:15:55 -070075
Robert Phillips4150eea2018-02-07 17:08:21 -050076 (*graph)[0]->dependsOn((*graph)[1].get());
77 (*graph)[0]->dependsOn((*graph)[2].get());
78 (*graph)[1]->dependsOn((*graph)[3].get());
79 (*graph)[2]->dependsOn((*graph)[3].get());
robertphillips423f6462015-10-19 12:15:55 -070080
Robert Phillips4150eea2018-02-07 17:08:21 -050081 (*graph)[3]->dependsOn((*graph)[4].get());
82 (*graph)[3]->dependsOn((*graph)[5].get());
83 (*graph)[4]->dependsOn((*graph)[6].get());
84 (*graph)[5]->dependsOn((*graph)[6].get());
robertphillips423f6462015-10-19 12:15:55 -070085}
86
87/* Two independent diamonds
88 * 3 7
89 * / \ / \
90 * 1 2 5 6
91 * \ / \ /
92 * 0 4
93 */
Mike Kleinea3f0142019-03-20 11:12:10 -050094static void create_graph4(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) {
95 ToolUtils::TopoTestNode::AllocNodes(graph, 8);
robertphillips423f6462015-10-19 12:15:55 -070096
Robert Phillips4150eea2018-02-07 17:08:21 -050097 (*graph)[0]->dependsOn((*graph)[1].get());
98 (*graph)[0]->dependsOn((*graph)[2].get());
99 (*graph)[1]->dependsOn((*graph)[3].get());
100 (*graph)[2]->dependsOn((*graph)[3].get());
robertphillips423f6462015-10-19 12:15:55 -0700101
Robert Phillips4150eea2018-02-07 17:08:21 -0500102 (*graph)[4]->dependsOn((*graph)[5].get());
103 (*graph)[4]->dependsOn((*graph)[6].get());
104 (*graph)[5]->dependsOn((*graph)[7].get());
105 (*graph)[6]->dependsOn((*graph)[7].get());
robertphillips423f6462015-10-19 12:15:55 -0700106}
107
108DEF_TEST(TopoSort, reporter) {
109 SkRandom rand;
110
111 struct {
112 CreateGraphPF fCreate;
113 bool fExpectedResult;
114 } tests[] = {
115 { create_graph0, true },
116 { create_graph1, true },
117 { create_graph2, false },
118 { create_graph3, true },
119 { create_graph4, true },
120 };
121
122 for (size_t i = 0; i < SK_ARRAY_COUNT(tests); ++i) {
Mike Kleinea3f0142019-03-20 11:12:10 -0500123 SkTArray<sk_sp<ToolUtils::TopoTestNode>> graph;
halcanary9d524f22016-03-29 09:03:52 -0700124
robertphillips423f6462015-10-19 12:15:55 -0700125 (tests[i].fCreate)(&graph);
126
Mike Kleinea3f0142019-03-20 11:12:10 -0500127 ToolUtils::TopoTestNode::Shuffle(&graph, &rand);
robertphillips423f6462015-10-19 12:15:55 -0700128
Mike Kleinea3f0142019-03-20 11:12:10 -0500129 bool actualResult = SkTTopoSort<ToolUtils::TopoTestNode>(&graph);
robertphillips423f6462015-10-19 12:15:55 -0700130 REPORTER_ASSERT(reporter, actualResult == tests[i].fExpectedResult);
131
132 if (tests[i].fExpectedResult) {
133 for (int j = 0; j < graph.count(); ++j) {
134 REPORTER_ASSERT(reporter, graph[j]->check());
135 }
136 }
137
138 //SkDEBUGCODE(print(graph);)
robertphillips423f6462015-10-19 12:15:55 -0700139 }
140}