1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include <unistd.h>
19
20 #include "logger.h"
21 #include "utd_common.h"
22 #include "graph.h"
23
24 using namespace testing::ext;
25 using namespace OHOS::UDMF;
26 using namespace OHOS;
27 namespace OHOS::Test {
28 enum TestNodes : uint32_t {
29 POINT_A = 0,
30 POINT_B,
31 POINT_C,
32 POINT_D,
33 POINT_E,
34 POINT_F,
35 POINT_G,
36 POINT_H
37 };
38 class GraphTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 void SetUp() override;
43 void TearDown() override;
44 };
45
SetUpTestCase()46 void GraphTest::SetUpTestCase()
47 {
48 }
49
TearDownTestCase()50 void GraphTest::TearDownTestCase()
51 {
52 }
53
SetUp()54 void GraphTest::SetUp()
55 {
56 }
57
TearDown()58 void GraphTest::TearDown()
59 {
60 }
61
62 /**
63 * @tc.name: DfsUnconnectedGraph001
64 * @tc.desc: DfsUnconnectedGraph
65 * @tc.type: FUNC
66 */
67 HWTEST_F(GraphTest, DfsUnconnectedGraph001, TestSize.Level1)
68 {
69 LOG_INFO(UDMF_TEST, "DfsUnconnectedGraph001 begin.");
70 uint32_t vextexNum = 3; // total point
71 vector<vector<uint32_t>> edges={{TestNodes::POINT_A, TestNodes::POINT_B},
72 {TestNodes::POINT_B, TestNodes::POINT_C},
73 {TestNodes::POINT_C, TestNodes::POINT_A}
74 };
75 Graph graph(vextexNum);
76 for (uint32_t i = 0; i < edges.size(); i++) {
77 graph.AddEdge(edges[i][0], edges[i][1]);
78 }
79 bool isDAGFlag = graph.DfsUnconnectedGraph([&](uint32_t currNode) -> bool
__anon2e0085520102(uint32_t currNode) 80 { return false; });
81 EXPECT_EQ(isDAGFlag, false);
82 LOG_INFO(UDMF_TEST, "DfsUnconnectedGraph001 end.");
83 }
84
85 HWTEST_F(GraphTest, DfsUnconnectedGraph002, TestSize.Level1)
86 {
87 LOG_INFO(UDMF_TEST, "DfsUnconnectedGraph002 begin.");
88 uint32_t vextexNum = 2;
89 vector<vector<uint32_t>> edges={{TestNodes::POINT_A, TestNodes::POINT_B},
90 {TestNodes::POINT_B, TestNodes::POINT_A},
91 };
92 Graph graph(vextexNum);
93 for (uint32_t i = 0; i < edges.size(); i++) {
94 graph.AddEdge(edges[i][0], edges[i][1]);
95 }
96 bool isDAGFlag = graph.DfsUnconnectedGraph([&](uint32_t currNode) -> bool
__anon2e0085520202(uint32_t currNode) 97 { return false; });
98 EXPECT_EQ(isDAGFlag, false);
99 LOG_INFO(UDMF_TEST, "DfsUnconnectedGraph002 end.");
100 }
101
102 HWTEST_F(GraphTest, DfsUnconnectedGraph003, TestSize.Level1)
103 {
104 LOG_INFO(UDMF_TEST, "DfsUnconnectedGraph003 begin.");
105 uint32_t vextexNum =8;
106 vector<vector<uint32_t>> edges = {
107 {TestNodes::POINT_A, TestNodes::POINT_B},
108 {TestNodes::POINT_B, TestNodes::POINT_C},
109 {TestNodes::POINT_C, TestNodes::POINT_D},
110 {TestNodes::POINT_D, TestNodes::POINT_E},
111 {TestNodes::POINT_D, TestNodes::POINT_E},
112 {TestNodes::POINT_E, TestNodes::POINT_F},
113 {TestNodes::POINT_E, TestNodes::POINT_G},
114 {TestNodes::POINT_G, TestNodes::POINT_H},
115 {TestNodes::POINT_H, TestNodes::POINT_E},
116 };
117 Graph graph(vextexNum);
118 for (uint32_t i = 0; i < edges.size(); i++) {
119 graph.AddEdge(edges[i][0], edges[i][1]);
120 }
121 bool isDAGFlag = graph.DfsUnconnectedGraph([&](uint32_t currNode) -> bool
__anon2e0085520302(uint32_t currNode) 122 { return false; });
123 EXPECT_EQ(isDAGFlag, true);
124 LOG_INFO(UDMF_TEST, "DfsUnconnectedGraph003 end.");
125 }
126 } // OHOS::Test