Compare commits
3 Commits
main
...
test_20260
| Author | SHA1 | Date |
|---|---|---|
|
|
12a4a1cd77 | |
|
|
ddddb6f3b6 | |
|
|
c41d8d8cf7 |
|
|
@ -1,9 +1,8 @@
|
||||||
cmake_minimum_required(VERSION 3.10)
|
cmake_minimum_required(VERSION 3.10.0)
|
||||||
project(HeightMeasurementSystem VERSION 1.0.0 LANGUAGES CXX)
|
project(HeightMeasurementSystem VERSION 1.0.0 LANGUAGES CXX)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 14)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
|
||||||
|
|
||||||
# Output directories
|
# Output directories
|
||||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
|
@ -12,26 +11,7 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||||
|
|
||||||
# Include directories
|
# Include directories
|
||||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||||
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
|
|
||||||
# Main executable
|
# Add subdirectory for tests
|
||||||
add_executable(hsm_main
|
add_subdirectory(tests)
|
||||||
src/main.cpp
|
|
||||||
src/app.cpp
|
|
||||||
src/altitude_calculator.cpp
|
|
||||||
src/data_logger.cpp
|
|
||||||
src/sensor_simulator.cpp
|
|
||||||
src/alert_manager.cpp
|
|
||||||
)
|
|
||||||
|
|
||||||
# Test executable
|
|
||||||
add_executable(hsm_test
|
|
||||||
tests/basic_test.cpp
|
|
||||||
src/altitude_calculator.cpp
|
|
||||||
src/data_logger.cpp
|
|
||||||
src/sensor_simulator.cpp
|
|
||||||
src/alert_manager.cpp
|
|
||||||
)
|
|
||||||
|
|
||||||
# Enable testing
|
|
||||||
enable_testing()
|
|
||||||
add_test(NAME BasicTest COMMAND hsm_test)
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
project(HeightMeasurementSystem VERSION 1.0.0 LANGUAGES CXX)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
|
# Output directories
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||||
|
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||||
|
|
||||||
|
# Include directories
|
||||||
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||||
|
|
||||||
|
# Main executable
|
||||||
|
add_executable(hsm_main
|
||||||
|
src/main.cpp
|
||||||
|
src/app.cpp
|
||||||
|
src/altitude_calculator.cpp
|
||||||
|
src/data_logger.cpp
|
||||||
|
src/sensor_simulator.cpp
|
||||||
|
src/alert_manager.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
# Test executable
|
||||||
|
add_executable(hsm_test
|
||||||
|
tests/basic_test.cpp
|
||||||
|
src/altitude_calculator.cpp
|
||||||
|
src/data_logger.cpp
|
||||||
|
src/sensor_simulator.cpp
|
||||||
|
src/alert_manager.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
# Enable testing
|
||||||
|
enable_testing()
|
||||||
|
add_test(NAME BasicTest COMMAND hsm_test)
|
||||||
|
|
@ -5,6 +5,8 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 预警类型枚举
|
* @brief 预警类型枚举
|
||||||
|
|
@ -188,6 +190,14 @@ public:
|
||||||
*/
|
*/
|
||||||
float getLowerThreshold() const { return lower_threshold_; }
|
float getLowerThreshold() const { return lower_threshold_; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 获取高度阈值(上下限)
|
||||||
|
* @return pair<上限阈值, 下限阈值>
|
||||||
|
*/
|
||||||
|
std::pair<float, float> getAltitudeThresholds() const {
|
||||||
|
return std::make_pair(upper_threshold_, lower_threshold_);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 检查传感器是否正常
|
* @brief 检查传感器是否正常
|
||||||
* @return 传感器状态
|
* @return 传感器状态
|
||||||
|
|
@ -197,26 +207,6 @@ public:
|
||||||
return it == alerts_.end() || it->second.status != AlertStatus::ACTIVE;
|
return it == alerts_.end() || it->second.status != AlertStatus::ACTIVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
// 预警阈值
|
|
||||||
float upper_threshold_;
|
|
||||||
float lower_threshold_;
|
|
||||||
|
|
||||||
// 当前预警状态
|
|
||||||
std::unordered_map<AlertType, AlertInfo> alerts_;
|
|
||||||
|
|
||||||
// 预警历史记录
|
|
||||||
std::vector<AlertInfo> alert_history_;
|
|
||||||
|
|
||||||
// 历史记录最大数量
|
|
||||||
static const size_t MAX_HISTORY = 1000;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 添加预警到历史记录
|
|
||||||
* @param alert 预警信息
|
|
||||||
*/
|
|
||||||
void addToHistory(const AlertInfo& alert);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 触发预警
|
* @brief 触发预警
|
||||||
* @param type 预警类型
|
* @param type 预警类型
|
||||||
|
|
@ -246,6 +236,26 @@ private:
|
||||||
* @return 状态名称
|
* @return 状态名称
|
||||||
*/
|
*/
|
||||||
static std::string getAlertStatusName(AlertStatus status);
|
static std::string getAlertStatusName(AlertStatus status);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// 预警阈值
|
||||||
|
float upper_threshold_;
|
||||||
|
float lower_threshold_;
|
||||||
|
|
||||||
|
// 当前预警状态
|
||||||
|
std::unordered_map<AlertType, AlertInfo> alerts_;
|
||||||
|
|
||||||
|
// 预警历史记录
|
||||||
|
std::vector<AlertInfo> alert_history_;
|
||||||
|
|
||||||
|
// 历史记录最大数量
|
||||||
|
static const size_t MAX_HISTORY = 1000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 添加预警到历史记录
|
||||||
|
* @param alert 预警信息
|
||||||
|
*/
|
||||||
|
void addToHistory(const AlertInfo& alert);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // HSM_ALERT_MANAGER_HPP
|
#endif // HSM_ALERT_MANAGER_HPP
|
||||||
|
|
@ -288,7 +288,7 @@ std::string AlertManager::getAlertStatusName(AlertStatus status) {
|
||||||
case AlertStatus::INACTIVE: return "未激活";
|
case AlertStatus::INACTIVE: return "未激活";
|
||||||
case AlertStatus::ACTIVE: return "预警中";
|
case AlertStatus::ACTIVE: return "预警中";
|
||||||
case AlertStatus::ACKNOWLEDGED: return "已确认";
|
case AlertStatus::ACKNOWLEDGED: return "已确认";
|
||||||
case AlertType::RESOLVED: return "已解决";
|
case AlertStatus::RESOLVED: return "已解决";
|
||||||
default: return "未知状态";
|
default: return "未知状态";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
# GoogleTest配置
|
||||||
|
include(FetchContent)
|
||||||
|
FetchContent_Declare(
|
||||||
|
googletest
|
||||||
|
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
|
||||||
|
)
|
||||||
|
# For Windows: Prevent overriding the parent project's compiler/linker settings
|
||||||
|
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||||
|
FetchContent_MakeAvailable(googletest)
|
||||||
|
|
||||||
|
# 包含目录
|
||||||
|
include_directories(${CMAKE_SOURCE_DIR}/src)
|
||||||
|
include_directories(${CMAKE_SOURCE_DIR}/include)
|
||||||
|
|
||||||
|
# 启用测试
|
||||||
|
enable_testing()
|
||||||
|
|
||||||
|
# 添加alert_manager的单元测试
|
||||||
|
add_executable(test_alert_manager test_alert_manager.cpp ../src/alert_manager.cpp)
|
||||||
|
|
||||||
|
# 设置UTF-8编码支持
|
||||||
|
if(MSVC)
|
||||||
|
target_compile_options(test_alert_manager PRIVATE /utf-8)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
target_link_libraries(test_alert_manager gtest gmock gtest_main)
|
||||||
|
|
||||||
|
# 注册测试
|
||||||
|
include(GoogleTest)
|
||||||
|
gtest_discover_tests(test_alert_manager)
|
||||||
|
|
@ -0,0 +1,245 @@
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "alert_manager.hpp"
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
// Mock time function for deterministic testing
|
||||||
|
uint32_t mock_timestamp = 1000;
|
||||||
|
uint32_t getMockTimestamp() { return mock_timestamp; }
|
||||||
|
|
||||||
|
// Override the global timestamp getter in alert_manager.cpp
|
||||||
|
extern "C" uint32_t getTimestamp() { return getMockTimestamp(); }
|
||||||
|
|
||||||
|
// Test fixture for AlertManager
|
||||||
|
class AlertManagerTest : public ::testing::Test {
|
||||||
|
protected:
|
||||||
|
AlertManager alert_manager;
|
||||||
|
|
||||||
|
void SetUp() override {
|
||||||
|
mock_timestamp = 1000;
|
||||||
|
alert_manager.initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
void advanceTime(uint32_t seconds) {
|
||||||
|
mock_timestamp += seconds;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Test case: AlertManager 构造函数初始化默认阈值
|
||||||
|
TEST_F(AlertManagerTest, testAlertManagerConstructorNormalInput) {
|
||||||
|
EXPECT_FLOAT_EQ(alert_manager.getAltitudeThresholds().first, 100.0f);
|
||||||
|
EXPECT_FLOAT_EQ(alert_manager.getAltitudeThresholds().second, -50.0f);
|
||||||
|
EXPECT_TRUE(alert_manager.getActiveAlerts().empty());
|
||||||
|
EXPECT_TRUE(alert_manager.getAlertHistory(10).empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: AlertManager 析构函数无副作用
|
||||||
|
TEST_F(AlertManagerTest, testAlertManagerDestructorNoSideEffect) {
|
||||||
|
// Destructor has no logic, just verify it's callable
|
||||||
|
AlertManager temp_manager;
|
||||||
|
EXPECT_NO_THROW({});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: initialize 输出正确信息
|
||||||
|
TEST_F(AlertManagerTest, testInitializeOutputCorrectInfo) {
|
||||||
|
std::stringstream buffer;
|
||||||
|
std::streambuf* old_cout = std::cout.rdbuf(buffer.rdbuf());
|
||||||
|
alert_manager.initialize();
|
||||||
|
std::cout.rdbuf(old_cout);
|
||||||
|
|
||||||
|
EXPECT_NE(buffer.str().find("预警管理器初始化完成。"), std::string::npos);
|
||||||
|
EXPECT_NE(buffer.str().find("默认预警阈值:上限=100米,下限=-50米"), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: checkAltitudeAlert 触发上限预警
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertTriggerUpper) {
|
||||||
|
alert_manager.checkAltitudeAlert(105.0f, getMockTimestamp());
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_EQ(active_alerts.size(), 1);
|
||||||
|
EXPECT_EQ(active_alerts[0].type, AlertType::ALTITUDE_UPPER);
|
||||||
|
EXPECT_EQ(active_alerts[0].status, AlertStatus::ACTIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: checkAltitudeAlert 恢复正常状态
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertResolveUpper) {
|
||||||
|
alert_manager.checkAltitudeAlert(105.0f, getMockTimestamp());
|
||||||
|
alert_manager.checkAltitudeAlert(95.0f, getMockTimestamp());
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_TRUE(active_alerts.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: checkAltitudeAlert 触发下限预警
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertTriggerLower) {
|
||||||
|
alert_manager.checkAltitudeAlert(-55.0f, getMockTimestamp());
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_EQ(active_alerts.size(), 1);
|
||||||
|
EXPECT_EQ(active_alerts[0].type, AlertType::ALTITUDE_LOWER);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: setAltitudeThresholds 合法输入更新阈值
|
||||||
|
TEST_F(AlertManagerTest, testSetAltitudeThresholdsValidInput) {
|
||||||
|
alert_manager.setAltitudeThresholds(200.0f, -100.0f);
|
||||||
|
auto thresholds = alert_manager.getAltitudeThresholds();
|
||||||
|
EXPECT_FLOAT_EQ(thresholds.first, 200.0f);
|
||||||
|
EXPECT_FLOAT_EQ(thresholds.second, -100.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: setAltitudeThresholds 非法输入输出错误提示
|
||||||
|
TEST_F(AlertManagerTest, testSetAltitudeThresholdsInvalidInput) {
|
||||||
|
std::stringstream buffer;
|
||||||
|
std::streambuf* old_cerr = std::cerr.rdbuf(buffer.rdbuf());
|
||||||
|
alert_manager.setAltitudeThresholds(50.0f, 100.0f);
|
||||||
|
std::cerr.rdbuf(old_cerr);
|
||||||
|
EXPECT_NE(buffer.str().find("错误:上限阈值必须大于下限阈值。"), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: triggerSensorFailure 触发传感器故障预警
|
||||||
|
TEST_F(AlertManagerTest, testTriggerSensorFailure) {
|
||||||
|
alert_manager.triggerSensorFailure(getMockTimestamp());
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_EQ(active_alerts.size(), 1);
|
||||||
|
EXPECT_EQ(active_alerts[0].type, AlertType::SENSOR_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: triggerLowBattery 触发低电量预警
|
||||||
|
TEST_F(AlertManagerTest, testTriggerLowBattery) {
|
||||||
|
alert_manager.triggerLowBattery(getMockTimestamp(), 15.0f);
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_EQ(active_alerts.size(), 1);
|
||||||
|
EXPECT_EQ(active_alerts[0].type, AlertType::LOW_BATTERY);
|
||||||
|
EXPECT_NE(active_alerts[0].description.find("低电量警告:当前电量15%"), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: triggerCommunicationError 触发通信错误预警
|
||||||
|
TEST_F(AlertManagerTest, testTriggerCommunicationError) {
|
||||||
|
alert_manager.triggerCommunicationError(getMockTimestamp());
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_EQ(active_alerts.size(), 1);
|
||||||
|
EXPECT_EQ(active_alerts[0].type, AlertType::COMMUNICATION_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: acknowledgeAllAlerts 确认所有活动预警
|
||||||
|
TEST_F(AlertManagerTest, testAcknowledgeAllAlerts) {
|
||||||
|
alert_manager.triggerSensorFailure(getMockTimestamp());
|
||||||
|
alert_manager.triggerLowBattery(getMockTimestamp(), 10.0f);
|
||||||
|
alert_manager.acknowledgeAllAlerts(getMockTimestamp());
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_TRUE(active_alerts.empty());
|
||||||
|
EXPECT_TRUE(alert_manager.hasUnacknowledgedAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: acknowledgeAlert 确认指定类型预警
|
||||||
|
TEST_F(AlertManagerTest, testAcknowledgeAlert) {
|
||||||
|
alert_manager.triggerSensorFailure(getMockTimestamp());
|
||||||
|
alert_manager.acknowledgeAlert(AlertType::SENSOR_FAILURE, getMockTimestamp());
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_TRUE(active_alerts.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: resetAlert 重置指定类型预警
|
||||||
|
TEST_F(AlertManagerTest, testResetAlert) {
|
||||||
|
alert_manager.triggerSensorFailure(getMockTimestamp());
|
||||||
|
alert_manager.resetAlert(AlertType::SENSOR_FAILURE);
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_TRUE(active_alerts.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: resetAllAlerts 重置所有预警
|
||||||
|
TEST_F(AlertManagerTest, testResetAllAlerts) {
|
||||||
|
alert_manager.triggerSensorFailure(getMockTimestamp());
|
||||||
|
alert_manager.triggerLowBattery(getMockTimestamp(), 10.0f);
|
||||||
|
alert_manager.resetAllAlerts();
|
||||||
|
EXPECT_TRUE(alert_manager.getActiveAlerts().empty());
|
||||||
|
EXPECT_TRUE(alert_manager.getAlertHistory(10).empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: hasActiveAlerts 检查是否存在活动预警
|
||||||
|
TEST_F(AlertManagerTest, testHasActiveAlertsWhenNoAlerts) {
|
||||||
|
EXPECT_FALSE(alert_manager.hasActiveAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: hasActiveAlerts 检查存在活动预警
|
||||||
|
TEST_F(AlertManagerTest, testHasActiveAlertsWhenHasAlerts) {
|
||||||
|
alert_manager.triggerSensorFailure(getMockTimestamp());
|
||||||
|
EXPECT_TRUE(alert_manager.hasActiveAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: hasUnacknowledgedAlerts 检查未确认预警
|
||||||
|
TEST_F(AlertManagerTest, testHasUnacknowledgedAlertsWhenNoAcknowledged) {
|
||||||
|
alert_manager.triggerSensorFailure(getMockTimestamp());
|
||||||
|
EXPECT_TRUE(alert_manager.hasUnacknowledgedAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: getActiveAlerts 返回活动预警列表
|
||||||
|
TEST_F(AlertManagerTest, testGetActiveAlertsReturnsCorrectList) {
|
||||||
|
alert_manager.triggerSensorFailure(getMockTimestamp());
|
||||||
|
alert_manager.triggerLowBattery(getMockTimestamp(), 10.0f);
|
||||||
|
auto alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_EQ(alerts.size(), 2);
|
||||||
|
EXPECT_EQ(alerts[0].type, AlertType::SENSOR_FAILURE);
|
||||||
|
EXPECT_EQ(alerts[1].type, AlertType::LOW_BATTERY);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: getAlertHistory 获取历史记录(最大数量限制)
|
||||||
|
TEST_F(AlertManagerTest, testGetAlertHistoryMaxCountLimit) {
|
||||||
|
for (int i = 0; i < 5; ++i) {
|
||||||
|
alert_manager.triggerSensorFailure(getMockTimestamp());
|
||||||
|
advanceTime(1);
|
||||||
|
}
|
||||||
|
auto history = alert_manager.getAlertHistory(3);
|
||||||
|
EXPECT_EQ(history.size(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: getStatusDescription 生成系统状态描述
|
||||||
|
TEST_F(AlertManagerTest, testGetStatusDescriptionWithActiveAlerts) {
|
||||||
|
alert_manager.triggerSensorFailure(getMockTimestamp());
|
||||||
|
std::string status_desc = alert_manager.getStatusDescription();
|
||||||
|
EXPECT_NE(status_desc.find("有1个活动预警"), std::string::npos);
|
||||||
|
EXPECT_NE(status_desc.find("传感器故障"), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: clearHistory 清空历史记录
|
||||||
|
TEST_F(AlertManagerTest, testClearHistory) {
|
||||||
|
alert_manager.triggerSensorFailure(getMockTimestamp());
|
||||||
|
alert_manager.clearHistory();
|
||||||
|
EXPECT_TRUE(alert_manager.getAlertHistory(10).empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: addToHistory 添加历史记录并保持最大数量
|
||||||
|
TEST_F(AlertManagerTest, testAddToHistoryMaintainMaxSize) {
|
||||||
|
const size_t MAX_HISTORY = 5;
|
||||||
|
for (int i = 0; i < MAX_HISTORY + 1; ++i) {
|
||||||
|
alert_manager.triggerSensorFailure(getMockTimestamp());
|
||||||
|
advanceTime(1);
|
||||||
|
}
|
||||||
|
auto history = alert_manager.getAlertHistory(MAX_HISTORY);
|
||||||
|
EXPECT_EQ(history.size(), MAX_HISTORY);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: triggerAlert 触发新预警并添加到历史
|
||||||
|
TEST_F(AlertManagerTest, testTriggerAlertAddsToHistory) {
|
||||||
|
alert_manager.triggerAlert(AlertType::ALTITUDE_UPPER, getMockTimestamp(), "测试预警");
|
||||||
|
auto history = alert_manager.getAlertHistory(10);
|
||||||
|
EXPECT_EQ(history.size(), 1);
|
||||||
|
EXPECT_EQ(history[0].type, AlertType::ALTITUDE_UPPER);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: updateAlertStatus 更新状态并触发反馈
|
||||||
|
TEST_F(AlertManagerTest, testUpdateAlertStatusChangesStatus) {
|
||||||
|
alert_manager.triggerSensorFailure(getMockTimestamp());
|
||||||
|
alert_manager.updateAlertStatus(AlertType::SENSOR_FAILURE, AlertStatus::ACKNOWLEDGED, getMockTimestamp());
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_TRUE(active_alerts.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: getAlertTypeName 返回正确名称
|
||||||
|
TEST_F(AlertManagerTest, testGetAlertTypeName) {
|
||||||
|
EXPECT_EQ(alert_manager.getAlertTypeName(AlertType::ALTITUDE_UPPER), "高度上限预警");
|
||||||
|
EXPECT_EQ(alert_manager.getAlertTypeName(AlertType::NONE), "无");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: getAlertStatusName 返回正确状态名
|
||||||
|
TEST_F(AlertManagerTest, testGetAlertStatusName) {
|
||||||
|
EXPECT_EQ(alert_manager.getAlertStatusName(AlertStatus::ACTIVE), "预警中");
|
||||||
|
EXPECT_EQ(alert_manager.getAlertStatusName(AlertStatus::RESOLVED), "已解决");
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue