robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/utils/SkRandom.h" |
| 9 | #include "src/core/SkTTopoSort.h" |
| 10 | #include "tests/Test.h" |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "tools/ToolUtils.h" |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 13 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 14 | typedef void (*CreateGraphPF)(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 15 | |
| 16 | /* Simple diamond |
| 17 | * 3 |
| 18 | * / \ |
| 19 | * 1 2 |
| 20 | * \ / |
| 21 | * 0 |
| 22 | */ |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 23 | static void create_graph0(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) { |
| 24 | ToolUtils::TopoTestNode::AllocNodes(graph, 4); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 25 | |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 26 | (*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()); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | /* Simple chain |
| 33 | * 3 |
| 34 | * | |
| 35 | * 2 |
| 36 | * | |
| 37 | * 1 |
| 38 | * | |
| 39 | * 0 |
| 40 | */ |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 41 | static void create_graph1(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) { |
| 42 | ToolUtils::TopoTestNode::AllocNodes(graph, 4); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 43 | |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 44 | (*graph)[0]->dependsOn((*graph)[1].get()); |
| 45 | (*graph)[1]->dependsOn((*graph)[2].get()); |
| 46 | (*graph)[2]->dependsOn((*graph)[3].get()); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /* Loop |
| 50 | * 2 |
| 51 | * / \ |
| 52 | * 0 --- 1 |
| 53 | */ |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 54 | static void create_graph2(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) { |
| 55 | ToolUtils::TopoTestNode::AllocNodes(graph, 3); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 56 | |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 57 | (*graph)[0]->dependsOn((*graph)[1].get()); |
| 58 | (*graph)[1]->dependsOn((*graph)[2].get()); |
| 59 | (*graph)[2]->dependsOn((*graph)[0].get()); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* Double diamond |
| 63 | * 6 |
| 64 | * / \ |
| 65 | * 4 5 |
| 66 | * \ / |
| 67 | * 3 |
| 68 | * / \ |
| 69 | * 1 2 |
| 70 | * \ / |
| 71 | * 0 |
| 72 | */ |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 73 | static void create_graph3(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) { |
| 74 | ToolUtils::TopoTestNode::AllocNodes(graph, 7); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 75 | |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 76 | (*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()); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 80 | |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 81 | (*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()); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* Two independent diamonds |
| 88 | * 3 7 |
| 89 | * / \ / \ |
| 90 | * 1 2 5 6 |
| 91 | * \ / \ / |
| 92 | * 0 4 |
| 93 | */ |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 94 | static void create_graph4(SkTArray<sk_sp<ToolUtils::TopoTestNode>>* graph) { |
| 95 | ToolUtils::TopoTestNode::AllocNodes(graph, 8); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 96 | |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 97 | (*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()); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 101 | |
Robert Phillips | 4150eea | 2018-02-07 17:08:21 -0500 | [diff] [blame] | 102 | (*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()); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | DEF_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 Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 123 | SkTArray<sk_sp<ToolUtils::TopoTestNode>> graph; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 124 | |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 125 | (tests[i].fCreate)(&graph); |
| 126 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 127 | ToolUtils::TopoTestNode::Shuffle(&graph, &rand); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 128 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 129 | bool actualResult = SkTTopoSort<ToolUtils::TopoTestNode>(&graph); |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 130 | 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);) |
robertphillips | 423f646 | 2015-10-19 12:15:55 -0700 | [diff] [blame] | 139 | } |
| 140 | } |