* ------------------------------------------------------------------------- * This file is part of the MindStudio project.
* Copyright (c) 2026 Huawei Technologies Co.,Ltd.
*
* MindStudio is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* ------------------------------------------------------------------------- */
#include <gtest/gtest.h>
#include <filesystem>
#include "StringUtil.h"
#include "../TestSuit.h"
#include "MemSnapshotParser.h"
using namespace Dic::Module;
using namespace Dic;
class MemSnapshotParserTest : public ::testing::Test {
public:
static void SetUpTestSuite() {
testDir = TestSuit::GetTestDataFile("snapshot");
testPicklePath = testDir + "snapshot_invalid.pkl";
testLogPath = testDir + "snapshot_invalid.log";
testOutputDbPath = testDir + "snapshot_invalid.pkl.db";
std::ofstream pickleFile(testPicklePath);
pickleFile.close();
parser = &MemSnapshotParser::Instance();
ASSERT_TRUE(parser != nullptr);
}
static void TearDownTestSuite() {
parser->Reset();
FileUtil::RemoveFile(testPicklePath);
FileUtil::RemoveFile(testOutputDbPath);
}
protected:
static std::string testDir;
static std::string testPicklePath;
static std::string testLogPath;
static std::string testOutputDbPath;
static MemSnapshotParser *parser;
};
std::string MemSnapshotParserTest::testDir;
std::string MemSnapshotParserTest::testPicklePath;
std::string MemSnapshotParserTest::testLogPath;
std::string MemSnapshotParserTest::testOutputDbPath;
MemSnapshotParser *MemSnapshotParserTest::parser = nullptr;
TEST_F(MemSnapshotParserTest, Reset) {
parser->GetParseContext().Reset(testPicklePath, testLogPath, testOutputDbPath);
parser->GetParseContext().SetState(ParserState::Processing);
parser->GetParseContext().SetProgress(50);
parser->Reset();
EXPECT_EQ(parser->GetParseContext().GetState(), ParserState::INIT);
EXPECT_EQ(parser->GetParseContext().GetProgress(), 0);
EXPECT_TRUE(parser->GetParseContext().GetPicklePath().empty());
EXPECT_TRUE(parser->GetParseContext().GetLogPath().empty());
EXPECT_TRUE(parser->GetParseContext().GetOutputDbPath().empty());
}
TEST_F(MemSnapshotParserTest, ParseContextManagement) {
parser->GetParseContext().Reset(testPicklePath, testLogPath, testOutputDbPath);
EXPECT_EQ(parser->GetParseContext().GetPicklePath(), testPicklePath);
EXPECT_EQ(parser->GetParseContext().GetLogPath(), testLogPath);
EXPECT_EQ(parser->GetParseContext().GetOutputDbPath(), testOutputDbPath);
EXPECT_EQ(parser->GetParseContext().GetState(), ParserState::INIT);
EXPECT_EQ(parser->GetParseContext().GetProgress(), 0);
parser->GetParseContext().SetState(ParserState::Processing);
EXPECT_EQ(parser->GetParseContext().GetState(), ParserState::Processing);
parser->GetParseContext().SetProgress(75);
EXPECT_EQ(parser->GetParseContext().GetProgress(), 75);
EXPECT_FALSE(parser->GetParseContext().IsFinished());
parser->GetParseContext().SetState(ParserState::FINISH_SUCCESS);
EXPECT_TRUE(parser->GetParseContext().IsFinished());
parser->GetParseContext().SetState(ParserState::FINISH_FAILURE);
EXPECT_TRUE(parser->GetParseContext().IsFinished());
}
TEST_F(MemSnapshotParserTest, CheckIfParsingNeed) {
parser->Reset();
parser->GetParseContext().Reset(testPicklePath, testLogPath, testOutputDbPath);
bool needParse = parser->CheckIfParsingNeed(parser->GetParseContext());
EXPECT_TRUE(needParse);
std::ofstream dbFile(testOutputDbPath);
dbFile.close();
needParse = parser->CheckIfParsingNeed(parser->GetParseContext());
EXPECT_TRUE(needParse);
FileUtil::RemoveFile(testOutputDbPath);
}