1 /**
2 * Copyright (c) 2024-2025 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 #include <vector>
18
19 #include "driver/dependency_analyzer/dep_analyzer.h"
20 #include "macros.h"
21 #include "path_getter.h"
22 #include "os/filesystem.h"
23
24 namespace {
25
26 class DepAnalyzerTest : public testing::Test {
27 public:
28 DepAnalyzerTest() = default;
29 ~DepAnalyzerTest() override = default;
30 NO_COPY_SEMANTIC(DepAnalyzerTest);
31 NO_MOVE_SEMANTIC(DepAnalyzerTest);
32
RunDepAnalyzer(size_t testFolderNum)33 void RunDepAnalyzer(size_t testFolderNum)
34 {
35 std::string binPath = test::utils::DepAnalyzerTestsBinPathGet();
36 std::string testPath = test::utils::DepAnalyzerTestsPathGet(testFolderNum, 1);
37 options_ = {binPath.c_str(), testPath.c_str()};
38
39 depAnalyzer_.AnalyzeDeps(optionsSize_, options_.data());
40 }
41
GetExpectedAns(std::vector<std::string> & expected,size_t testFolderNum,size_t testFileCounter)42 void GetExpectedAns(std::vector<std::string> &expected, size_t testFolderNum, size_t testFileCounter)
43 {
44 for (size_t i = 1; i < testFileCounter + 1; ++i) {
45 std::string expPath = test::utils::DepAnalyzerTestsPathGet(testFolderNum, i);
46 expected.push_back(ark::os::GetAbsolutePath(expPath));
47 }
48 }
49
GetTestSourcePaths()50 const std::vector<std::string> &GetTestSourcePaths()
51 {
52 return depAnalyzer_.GetSourcePaths();
53 }
54
GetTestFileDirectDependencies()55 std::unordered_map<std::string, std::unordered_set<std::string>> &GetTestFileDirectDependencies()
56 {
57 return depAnalyzer_.GetFileDirectDependencies();
58 }
59
GetTestFileDirectDependants()60 std::unordered_map<std::string, std::unordered_set<std::string>> &GetTestFileDirectDependants()
61 {
62 return depAnalyzer_.GetFileDirectDependants();
63 }
64
65 private:
66 DepAnalyzer depAnalyzer_;
67
68 const size_t optionsSize_ = 2;
69 std::vector<const char *> options_ = {};
70 };
71
TEST_F(DepAnalyzerTest,Subtestv1)72 TEST_F(DepAnalyzerTest, Subtestv1)
73 {
74 size_t testFolderNum = 1;
75 RunDepAnalyzer(testFolderNum);
76 std::unordered_map<std::string, std::unordered_set<std::string>> dependenciesExpected;
77 std::unordered_map<std::string, std::unordered_set<std::string>> dependentsExpected;
78 std::string file1 = test::utils::DepAnalyzerTestsPathGet(1, 1);
79 dependenciesExpected[file1] = {};
80 dependentsExpected[file1] = {};
81 ASSERT(GetTestFileDirectDependencies() == dependenciesExpected);
82 ASSERT(GetTestFileDirectDependants() == dependentsExpected);
83 }
84
TEST_F(DepAnalyzerTest,Subtestv2)85 TEST_F(DepAnalyzerTest, Subtestv2)
86 {
87 size_t testFolderNum = 2;
88 RunDepAnalyzer(testFolderNum);
89 std::unordered_map<std::string, std::unordered_set<std::string>> dependenciesExpected;
90 std::unordered_map<std::string, std::unordered_set<std::string>> dependentsExpected;
91 std::string file1 = test::utils::DepAnalyzerTestsPathGet(2, 1);
92 std::string file2 = test::utils::DepAnalyzerTestsPathGet(2, 2);
93 std::string file3 = test::utils::DepAnalyzerTestsPathGet(2, 3);
94 std::string file4 = test::utils::DepAnalyzerTestsPathGet(2, 4);
95 dependenciesExpected[file1] = {file2};
96 dependenciesExpected[file2] = {file3};
97 dependenciesExpected[file3] = {file2, file4};
98 dependenciesExpected[file4] = {file2};
99 dependentsExpected[file1] = {};
100 dependentsExpected[file2] = {file3, file4, file1};
101 dependentsExpected[file3] = {file2};
102 dependentsExpected[file4] = {file3};
103 ASSERT(GetTestFileDirectDependencies() == dependenciesExpected);
104 ASSERT(GetTestFileDirectDependants() == dependentsExpected);
105 }
106
TEST_F(DepAnalyzerTest,Subtestv3)107 TEST_F(DepAnalyzerTest, Subtestv3)
108 {
109 size_t testFolderNum = 3;
110 RunDepAnalyzer(testFolderNum);
111 std::unordered_map<std::string, std::unordered_set<std::string>> dependenciesExpected;
112 std::unordered_map<std::string, std::unordered_set<std::string>> dependentsExpected;
113 std::string file1 = test::utils::DepAnalyzerTestsPathGet(3, 1);
114 std::string file2 = test::utils::DepAnalyzerTestsPathGet(3, 2);
115 dependenciesExpected[file1] = {file2};
116 dependentsExpected[file1] = {};
117 dependentsExpected[file2] = {file1};
118 ASSERT(GetTestFileDirectDependencies() == dependenciesExpected);
119 ASSERT(GetTestFileDirectDependants() == dependentsExpected);
120 }
121
TEST_F(DepAnalyzerTest,Subtestv4)122 TEST_F(DepAnalyzerTest, Subtestv4)
123 {
124 size_t testFolderNum = 4;
125 RunDepAnalyzer(testFolderNum);
126 std::unordered_map<std::string, std::unordered_set<std::string>> dependenciesExpected;
127 std::unordered_map<std::string, std::unordered_set<std::string>> dependentsExpected;
128 std::string file1 = test::utils::DepAnalyzerTestsPathGet(4, 1);
129 std::string file2 = test::utils::DepAnalyzerTestsPathGet(4, 2);
130 dependenciesExpected[file1] = {file2};
131 dependentsExpected[file1] = {};
132 dependentsExpected[file2] = {file1};
133 ASSERT(GetTestFileDirectDependencies() == dependenciesExpected);
134 ASSERT(GetTestFileDirectDependants() == dependentsExpected);
135 }
136
137 } // namespace
138