305 lines
8.7 KiB
C++
305 lines
8.7 KiB
C++
#include <gtest/gtest.h>
|
||
#include <vector>
|
||
#include <string>
|
||
#include <cstdint>
|
||
#include <ctime>
|
||
#include "app.hpp"
|
||
|
||
/**
|
||
* 测试文件: test_app.cpp
|
||
* 被测文件: app.cpp (dsu namespace)
|
||
* 说明: 由于s_storage为静态全局变量,无法在测试中直接重置,
|
||
* 各测试用例使用唯一的channelId以避免数据干扰。
|
||
* 注意:StorageStatus 和 QueryCondition 定义在全局命名空间。
|
||
*/
|
||
|
||
// ==================== DSU_Write 测试 ====================
|
||
|
||
class DSUWriteTest : public ::testing::Test {
|
||
protected:
|
||
uint32_t channelId = 0;
|
||
time_t timestamp = 1000000;
|
||
|
||
void SetUp() override {
|
||
static uint32_t channelCounter = 2000;
|
||
channelId = channelCounter++;
|
||
}
|
||
};
|
||
|
||
// 测试正常写入数据流
|
||
TEST_F(DSUWriteTest, testDSUWriteNormalInput) {
|
||
std::vector<uint8_t> data = {0x01, 0x02, 0x03, 0x04};
|
||
bool result = dsu::DSU_Write(data, channelId, timestamp);
|
||
EXPECT_TRUE(result);
|
||
}
|
||
|
||
// 测试空数据写入应返回false
|
||
TEST_F(DSUWriteTest, testDSUWriteEmptyData) {
|
||
std::vector<uint8_t> data;
|
||
bool result = dsu::DSU_Write(data, channelId, timestamp);
|
||
EXPECT_FALSE(result);
|
||
}
|
||
|
||
// 测试channelId和timestamp为0的边界值
|
||
TEST_F(DSUWriteTest, testDSUWriteBoundaryZeroValues) {
|
||
std::vector<uint8_t> data = {0xFF};
|
||
bool result = dsu::DSU_Write(data, 0, 0);
|
||
EXPECT_TRUE(result);
|
||
}
|
||
|
||
// 测试channelId和timestamp为最大值
|
||
TEST_F(DSUWriteTest, testDSUWriteMaxValues) {
|
||
std::vector<uint8_t> data = {0xAB, 0xCD};
|
||
bool result = dsu::DSU_Write(data, UINT32_MAX, static_cast<time_t>(UINT32_MAX));
|
||
EXPECT_TRUE(result);
|
||
}
|
||
|
||
// 测试单字节数据写入
|
||
TEST_F(DSUWriteTest, testDSUWriteSingleByte) {
|
||
std::vector<uint8_t> data = {0x42};
|
||
bool result = dsu::DSU_Write(data, channelId, timestamp);
|
||
EXPECT_TRUE(result);
|
||
}
|
||
|
||
// 测试大数据块写入
|
||
TEST_F(DSUWriteTest, testDSUWriteLargeData) {
|
||
std::vector<uint8_t> data(1024, 0xAA);
|
||
bool result = dsu::DSU_Write(data, channelId, timestamp);
|
||
EXPECT_TRUE(result);
|
||
}
|
||
|
||
// ==================== DSU_CheckDisk 测试 ====================
|
||
|
||
// 测试磁盘健康状态检查(正常状态)
|
||
TEST(DSUCheckDiskTest, testDSUCheckDiskHealthy) {
|
||
StorageStatus status = dsu::DSU_CheckDisk();
|
||
EXPECT_TRUE(status == StorageStatus::Healthy ||
|
||
status == StorageStatus::Full);
|
||
}
|
||
|
||
// 测试磁盘存储满告警(写入超过1000条触发Full)
|
||
TEST(DSUCheckDiskTest, testDSUCheckDiskFull) {
|
||
uint32_t fillChannel = 9000;
|
||
for (int i = 0; i < 1005; ++i) {
|
||
std::vector<uint8_t> data = {static_cast<uint8_t>(i & 0xFF)};
|
||
dsu::DSU_Write(data, fillChannel, static_cast<time_t>(i));
|
||
}
|
||
StorageStatus status = dsu::DSU_CheckDisk();
|
||
EXPECT_EQ(status, StorageStatus::Full);
|
||
}
|
||
|
||
// ==================== DSU_Query 测试 ====================
|
||
|
||
class DSUQueryTest : public ::testing::Test {
|
||
protected:
|
||
uint32_t channelId = 0;
|
||
|
||
void SetUp() override {
|
||
static uint32_t channelCounter = 5000;
|
||
channelId = channelCounter++;
|
||
}
|
||
};
|
||
|
||
// 测试正常查询返回匹配数据
|
||
TEST_F(DSUQueryTest, testDSUQueryNormalInput) {
|
||
std::vector<uint8_t> data1 = {0x01, 0x02};
|
||
std::vector<uint8_t> data2 = {0x03, 0x04};
|
||
dsu::DSU_Write(data1, channelId, 100);
|
||
dsu::DSU_Write(data2, channelId, 200);
|
||
|
||
QueryCondition condition;
|
||
condition.channelId = channelId;
|
||
condition.startTime = 100;
|
||
condition.endTime = 200;
|
||
condition.maxResults = 10;
|
||
|
||
std::vector<std::vector<uint8_t>> results;
|
||
std::string log;
|
||
bool ret = dsu::DSU_Query(condition, results, log);
|
||
|
||
EXPECT_TRUE(ret);
|
||
EXPECT_EQ(results.size(), 2u);
|
||
EXPECT_EQ(results[0], data1);
|
||
EXPECT_EQ(results[1], data2);
|
||
}
|
||
|
||
// 测试无匹配数据时返回false
|
||
TEST_F(DSUQueryTest, testDSUQueryNoMatch) {
|
||
QueryCondition condition;
|
||
condition.channelId = 99999;
|
||
condition.startTime = 0;
|
||
condition.endTime = 100;
|
||
condition.maxResults = 10;
|
||
|
||
std::vector<std::vector<uint8_t>> results;
|
||
std::string log;
|
||
bool ret = dsu::DSU_Query(condition, results, log);
|
||
|
||
EXPECT_FALSE(ret);
|
||
EXPECT_TRUE(results.empty());
|
||
}
|
||
|
||
// 测试maxResults限制查询结果数量
|
||
TEST_F(DSUQueryTest, testDSUQueryMaxResultsLimit) {
|
||
for (int i = 0; i < 5; ++i) {
|
||
std::vector<uint8_t> data = {static_cast<uint8_t>(i)};
|
||
dsu::DSU_Write(data, channelId, static_cast<time_t>(300 + i));
|
||
}
|
||
|
||
QueryCondition condition;
|
||
condition.channelId = channelId;
|
||
condition.startTime = 300;
|
||
condition.endTime = 400;
|
||
condition.maxResults = 3;
|
||
|
||
std::vector<std::vector<uint8_t>> results;
|
||
std::string log;
|
||
bool ret = dsu::DSU_Query(condition, results, log);
|
||
|
||
EXPECT_TRUE(ret);
|
||
EXPECT_LE(results.size(), 3u);
|
||
}
|
||
|
||
// 测试时间范围精确查询
|
||
TEST_F(DSUQueryTest, testDSUQueryTimeRange) {
|
||
std::vector<uint8_t> data1 = {0x10};
|
||
std::vector<uint8_t> data2 = {0x20};
|
||
std::vector<uint8_t> data3 = {0x30};
|
||
dsu::DSU_Write(data1, channelId, 500);
|
||
dsu::DSU_Write(data2, channelId, 600);
|
||
dsu::DSU_Write(data3, channelId, 700);
|
||
|
||
QueryCondition condition;
|
||
condition.channelId = channelId;
|
||
condition.startTime = 550;
|
||
condition.endTime = 650;
|
||
condition.maxResults = 10;
|
||
|
||
std::vector<std::vector<uint8_t>> results;
|
||
std::string log;
|
||
bool ret = dsu::DSU_Query(condition, results, log);
|
||
|
||
EXPECT_TRUE(ret);
|
||
EXPECT_EQ(results.size(), 1u);
|
||
EXPECT_EQ(results[0], data2);
|
||
}
|
||
|
||
// 测试查询日志包含结果数量信息
|
||
TEST_F(DSUQueryTest, testDSUQueryLogOutput) {
|
||
std::vector<uint8_t> data = {0x01};
|
||
dsu::DSU_Write(data, channelId, 100);
|
||
|
||
QueryCondition condition;
|
||
condition.channelId = channelId;
|
||
condition.startTime = 100;
|
||
condition.endTime = 100;
|
||
condition.maxResults = 10;
|
||
|
||
std::vector<std::vector<uint8_t>> results;
|
||
std::string log;
|
||
dsu::DSU_Query(condition, results, log);
|
||
|
||
EXPECT_FALSE(log.empty());
|
||
EXPECT_NE(log.find("Query returned"), std::string::npos);
|
||
}
|
||
|
||
// ==================== DSU_Replay 测试 ====================
|
||
|
||
class DSUReplayTest : public ::testing::Test {
|
||
protected:
|
||
uint32_t channelId = 0;
|
||
|
||
void SetUp() override {
|
||
static uint32_t channelCounter = 7000;
|
||
channelId = channelCounter++;
|
||
}
|
||
};
|
||
|
||
// 测试正常回放数据
|
||
TEST_F(DSUReplayTest, testDSUReplayNormalInput) {
|
||
std::vector<uint8_t> data1 = {0x01, 0x02};
|
||
std::vector<uint8_t> data2 = {0x03, 0x04};
|
||
dsu::DSU_Write(data1, channelId, 100);
|
||
dsu::DSU_Write(data2, channelId, 200);
|
||
|
||
QueryCondition condition;
|
||
condition.channelId = channelId;
|
||
condition.startTime = 100;
|
||
condition.endTime = 200;
|
||
condition.maxResults = 10;
|
||
|
||
std::vector<uint8_t> replayOutput;
|
||
std::string log;
|
||
bool ret = dsu::DSU_Replay(condition, replayOutput, log);
|
||
|
||
EXPECT_TRUE(ret);
|
||
EXPECT_EQ(replayOutput.size(), 4u);
|
||
EXPECT_EQ(replayOutput[0], 0x01);
|
||
EXPECT_EQ(replayOutput[1], 0x02);
|
||
EXPECT_EQ(replayOutput[2], 0x03);
|
||
EXPECT_EQ(replayOutput[3], 0x04);
|
||
}
|
||
|
||
// 测试无数据时回放返回false
|
||
TEST_F(DSUReplayTest, testDSUReplayNoData) {
|
||
QueryCondition condition;
|
||
condition.channelId = 88888;
|
||
condition.startTime = 0;
|
||
condition.endTime = 100;
|
||
condition.maxResults = 10;
|
||
|
||
std::vector<uint8_t> replayOutput;
|
||
std::string log;
|
||
bool ret = dsu::DSU_Replay(condition, replayOutput, log);
|
||
|
||
EXPECT_FALSE(ret);
|
||
}
|
||
|
||
// 测试多数据块按序拼接回放
|
||
TEST_F(DSUReplayTest, testDSUReplayMultipleBlocks) {
|
||
std::vector<uint8_t> data1 = {0xAA};
|
||
std::vector<uint8_t> data2 = {0xBB, 0xCC};
|
||
std::vector<uint8_t> data3 = {0xDD, 0xEE, 0xFF};
|
||
dsu::DSU_Write(data1, channelId, 100);
|
||
dsu::DSU_Write(data2, channelId, 200);
|
||
dsu::DSU_Write(data3, channelId, 300);
|
||
|
||
QueryCondition condition;
|
||
condition.channelId = channelId;
|
||
condition.startTime = 100;
|
||
condition.endTime = 300;
|
||
condition.maxResults = 10;
|
||
|
||
std::vector<uint8_t> replayOutput;
|
||
std::string log;
|
||
bool ret = dsu::DSU_Replay(condition, replayOutput, log);
|
||
|
||
EXPECT_TRUE(ret);
|
||
EXPECT_EQ(replayOutput.size(), 6u);
|
||
EXPECT_EQ(replayOutput[0], 0xAA);
|
||
EXPECT_EQ(replayOutput[1], 0xBB);
|
||
EXPECT_EQ(replayOutput[2], 0xCC);
|
||
EXPECT_EQ(replayOutput[3], 0xDD);
|
||
EXPECT_EQ(replayOutput[4], 0xEE);
|
||
EXPECT_EQ(replayOutput[5], 0xFF);
|
||
}
|
||
|
||
// 测试回放日志包含完成信息
|
||
TEST_F(DSUReplayTest, testDSUReplayLogContainsInfo) {
|
||
std::vector<uint8_t> data = {0x01};
|
||
dsu::DSU_Write(data, channelId, 100);
|
||
|
||
QueryCondition condition;
|
||
condition.channelId = channelId;
|
||
condition.startTime = 100;
|
||
condition.endTime = 100;
|
||
condition.maxResults = 10;
|
||
|
||
std::vector<uint8_t> replayOutput;
|
||
std::string log;
|
||
dsu::DSU_Replay(condition, replayOutput, log);
|
||
|
||
EXPECT_FALSE(log.empty());
|
||
EXPECT_NE(log.find("Replay completed"), std::string::npos);
|
||
}
|