• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "ecmascript/pgo_profiler/pgo_profiler_encoder.h"
17 #include "ecmascript/pgo_profiler/pgo_profiler_manager.h"
18 #include "ecmascript/platform/file.h"
19 #include "ecmascript/tests/test_helper.h"
20 
21 using namespace panda;
22 using namespace panda::ecmascript;
23 using namespace panda::ecmascript::pgo;
24 
25 namespace panda::test {
26 class PGOProfilerEncoderTest : public testing::Test {
27 public:
28     using ApGenMode = PGOProfilerEncoder::ApGenMode;
29 
SetUpTestCase()30     static void SetUpTestCase()
31     {
32         GTEST_LOG_(INFO) << "SetUpTestCase";
33     }
34 
TearDownTestCase()35     static void TearDownTestCase()
36     {
37         GTEST_LOG_(INFO) << "TearDownCase";
38     }
39 
SetUp()40     void SetUp() override
41     {
42         testDir_ = "test_encoder_dir/";
43         testFilePath_ = testDir_ + "test_profile.ap";
44         mkdir(testDir_.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
45     }
46 
TearDown()47     void TearDown() override
48     {
49         if (FileExist(testFilePath_.c_str())) {
50             unlink(testFilePath_.c_str());
51         }
52         rmdir(testDir_.c_str());
53 
54         PGOProfilerManager::GetInstance()->SetDisablePGO(false);
55         PGOProfilerManager::GetInstance()->Destroy();
56     }
57 
58 protected:
CreateMockPGOInfo(uint32_t threshold=2)59     std::shared_ptr<PGOInfo> CreateMockPGOInfo(uint32_t threshold = 2)
60     {
61         auto pgoInfo = std::make_shared<PGOInfo>(threshold);
62         return pgoInfo;
63     }
64 
CreateTestFile(const std::string & path,const std::string & content="test content")65     void CreateTestFile(const std::string& path, const std::string& content = "test content")
66     {
67         std::ofstream file(path);
68         file << content;
69         file.close();
70     }
71 
72     std::string testDir_;
73     std::string testFilePath_;
74     static constexpr uint32_t DECODER_THRESHOLD = 2;
75 };
76 
HWTEST_F_L0(PGOProfilerEncoderTest,SaveWithValidPGOInfoTest)77 HWTEST_F_L0(PGOProfilerEncoderTest, SaveWithValidPGOInfoTest)
78 {
79     PGOProfilerEncoder encoder(testFilePath_, ApGenMode::OVERWRITE);
80     auto pgoInfo = CreateMockPGOInfo();
81     bool result = encoder.Save(pgoInfo);
82     ASSERT_TRUE(result == true || result == false); // Any result is acceptable as long as it doesn't crash
83 }
84 
HWTEST_F_L0(PGOProfilerEncoderTest,SaveWithNullPGOInfoTest)85 HWTEST_F_L0(PGOProfilerEncoderTest, SaveWithNullPGOInfoTest)
86 {
87     PGOProfilerEncoder encoder(testFilePath_, ApGenMode::OVERWRITE);
88     std::shared_ptr<PGOInfo> nullInfo = nullptr;
89     bool result = encoder.Save(nullInfo);
90     ASSERT_FALSE(result);
91 }
92 
HWTEST_F_L0(PGOProfilerEncoderTest,InvalidPathTest)93 HWTEST_F_L0(PGOProfilerEncoderTest, InvalidPathTest)
94 {
95     std::string invalidPath = "/invalid/path/that/should/not/exist/test.ap";
96     PGOProfilerEncoder encoder(invalidPath, ApGenMode::OVERWRITE);
97     auto pgoInfo = CreateMockPGOInfo();
98     bool result = encoder.Save(pgoInfo);
99     ASSERT_FALSE(result);
100 }
101 
HWTEST_F_L0(PGOProfilerEncoderTest,EmptyPathTest)102 HWTEST_F_L0(PGOProfilerEncoderTest, EmptyPathTest)
103 {
104     PGOProfilerEncoder encoder("", ApGenMode::OVERWRITE);
105     auto pgoInfo = CreateMockPGOInfo();
106     bool result = encoder.Save(pgoInfo);
107     ASSERT_FALSE(result);
108 }
109 
HWTEST_F_L0(PGOProfilerEncoderTest,EncoderModeToggleTest)110 HWTEST_F_L0(PGOProfilerEncoderTest, EncoderModeToggleTest)
111 {
112     PGOProfilerEncoder encoder(testFilePath_, ApGenMode::OVERWRITE);
113     ASSERT_EQ(encoder.GetApGenMode(), ApGenMode::OVERWRITE);
114     encoder.SetApGenMode(ApGenMode::MERGE);
115     ASSERT_EQ(encoder.GetApGenMode(), ApGenMode::MERGE);
116     encoder.SetApGenMode(ApGenMode::OVERWRITE);
117     ASSERT_EQ(encoder.GetApGenMode(), ApGenMode::OVERWRITE);
118 }
119 } // namespace panda::test