Compare commits
3 Commits
main
...
test_20260
| Author | SHA1 | Date |
|---|---|---|
|
|
d3907c5617 | |
|
|
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,294 @@
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "alert_manager.hpp"
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
// Mock time function for deterministic testing
|
||||||
|
uint32_t mock_timestamp = 1640995200; // 2022-01-01 00:00:00 UTC
|
||||||
|
|
||||||
|
// Mock the time function
|
||||||
|
uint32_t get_mock_timestamp() {
|
||||||
|
return mock_timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Override the global time function in alert_manager.cpp
|
||||||
|
extern "C" uint32_t std_time(nullptr) {
|
||||||
|
return mock_timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test fixture for AlertManager
|
||||||
|
class AlertManagerTest : public ::testing::Test {
|
||||||
|
protected:
|
||||||
|
AlertManager alert_manager;
|
||||||
|
|
||||||
|
void SetUp() override {
|
||||||
|
mock_timestamp = 1640995200;
|
||||||
|
alert_manager.initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TearDown() override {
|
||||||
|
alert_manager.resetAllAlerts();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Test case: AlertManager 构造函数初始化默认阈值
|
||||||
|
TEST_F(AlertManagerTest, testAlertManagerConstructorDefaultThresholds) {
|
||||||
|
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, testAlertManagerDestructorNoSideEffects) {
|
||||||
|
// Destructor should not throw or cause undefined behavior
|
||||||
|
// Simply verify it can be called without issues
|
||||||
|
AlertManager temp_manager;
|
||||||
|
// No assertion needed as destructor is trivial
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: initialize 输出初始化信息
|
||||||
|
TEST_F(AlertManagerTest, testInitializeOutputInfo) {
|
||||||
|
// Capture output
|
||||||
|
testing::internal::CaptureStdout();
|
||||||
|
alert_manager.initialize();
|
||||||
|
std::string output = testing::internal::GetCapturedStdout();
|
||||||
|
EXPECT_NE(output.find("预警管理器初始化完成。"), std::string::npos);
|
||||||
|
EXPECT_NE(output.find("默认预警阈值:上限=100米,下限=-50米"), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: checkAltitudeAlert 高度正常时无预警
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertNormalHeight) {
|
||||||
|
alert_manager.checkAltitudeAlert(50.0f, get_mock_timestamp());
|
||||||
|
EXPECT_FALSE(alert_manager.hasActiveAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: checkAltitudeAlert 超过上限触发预警
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertUpperThresholdTrigger) {
|
||||||
|
alert_manager.checkAltitudeAlert(101.0f, get_mock_timestamp());
|
||||||
|
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, testCheckAltitudeAlertLowerThresholdTrigger) {
|
||||||
|
alert_manager.checkAltitudeAlert(-51.0f, get_mock_timestamp());
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_EQ(active_alerts.size(), 1);
|
||||||
|
EXPECT_EQ(active_alerts[0].type, AlertType::ALTITUDE_LOWER);
|
||||||
|
EXPECT_EQ(active_alerts[0].status, AlertStatus::ACTIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: checkAltitudeAlert 上限恢复正常解除预警
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertUpperThresholdResolve) {
|
||||||
|
alert_manager.checkAltitudeAlert(101.0f, get_mock_timestamp());
|
||||||
|
alert_manager.checkAltitudeAlert(99.0f, get_mock_timestamp());
|
||||||
|
EXPECT_FALSE(alert_manager.hasActiveAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: checkAltitudeAlert 下限恢复正常解除预警
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertLowerThresholdResolve) {
|
||||||
|
alert_manager.checkAltitudeAlert(-51.0f, get_mock_timestamp());
|
||||||
|
alert_manager.checkAltitudeAlert(-49.0f, get_mock_timestamp());
|
||||||
|
EXPECT_FALSE(alert_manager.hasActiveAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
// Capture stderr
|
||||||
|
testing::internal::CaptureStderr();
|
||||||
|
alert_manager.setAltitudeThresholds(50.0f, 100.0f);
|
||||||
|
std::string error_output = testing::internal::GetCapturedStderr();
|
||||||
|
EXPECT_NE(error_output.find("错误:上限阈值必须大于下限阈值。"), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: triggerSensorFailure 触发传感器故障预警
|
||||||
|
TEST_F(AlertManagerTest, testTriggerSensorFailure) {
|
||||||
|
alert_manager.triggerSensorFailure(get_mock_timestamp());
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_EQ(active_alerts.size(), 1);
|
||||||
|
EXPECT_EQ(active_alerts[0].type, AlertType::SENSOR_FAILURE);
|
||||||
|
EXPECT_EQ(active_alerts[0].description, "传感器故障");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: triggerLowBattery 触发低电量预警
|
||||||
|
TEST_F(AlertManagerTest, testTriggerLowBattery) {
|
||||||
|
alert_manager.triggerLowBattery(get_mock_timestamp(), 15.5f);
|
||||||
|
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.5%"), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: triggerCommunicationError 触发通信错误预警
|
||||||
|
TEST_F(AlertManagerTest, testTriggerCommunicationError) {
|
||||||
|
alert_manager.triggerCommunicationError(get_mock_timestamp());
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_EQ(active_alerts.size(), 1);
|
||||||
|
EXPECT_EQ(active_alerts[0].type, AlertType::COMMUNICATION_ERROR);
|
||||||
|
EXPECT_EQ(active_alerts[0].description, "通信错误");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: acknowledgeAllAlerts 确认所有活动预警
|
||||||
|
TEST_F(AlertManagerTest, testAcknowledgeAllAlerts) {
|
||||||
|
alert_manager.triggerSensorFailure(get_mock_timestamp());
|
||||||
|
alert_manager.triggerLowBattery(get_mock_timestamp(), 10.0f);
|
||||||
|
alert_manager.acknowledgeAllAlerts(get_mock_timestamp());
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_TRUE(active_alerts.empty());
|
||||||
|
EXPECT_TRUE(alert_manager.hasUnacknowledgedAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: acknowledgeAlert 确认指定类型预警
|
||||||
|
TEST_F(AlertManagerTest, testAcknowledgeAlertSpecificType) {
|
||||||
|
alert_manager.triggerSensorFailure(get_mock_timestamp());
|
||||||
|
alert_manager.acknowledgeAlert(AlertType::SENSOR_FAILURE, get_mock_timestamp());
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_TRUE(active_alerts.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: resetAlert 重置指定类型预警
|
||||||
|
TEST_F(AlertManagerTest, testResetAlert) {
|
||||||
|
alert_manager.triggerSensorFailure(get_mock_timestamp());
|
||||||
|
alert_manager.resetAlert(AlertType::SENSOR_FAILURE);
|
||||||
|
EXPECT_FALSE(alert_manager.hasActiveAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: resetAllAlerts 清除所有活动预警
|
||||||
|
TEST_F(AlertManagerTest, testResetAllAlertsClearAll) {
|
||||||
|
alert_manager.triggerSensorFailure(get_mock_timestamp());
|
||||||
|
alert_manager.triggerLowBattery(get_mock_timestamp(), 10.0f);
|
||||||
|
alert_manager.resetAllAlerts();
|
||||||
|
EXPECT_FALSE(alert_manager.hasActiveAlerts());
|
||||||
|
EXPECT_TRUE(alert_manager.getAlertHistory(10).size() >= 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: hasActiveAlerts 检查是否存在活动预警
|
||||||
|
TEST_F(AlertManagerTest, testHasActiveAlertsWhenNoAlerts) {
|
||||||
|
EXPECT_FALSE(alert_manager.hasActiveAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: hasActiveAlerts 检查存在活动预警
|
||||||
|
TEST_F(AlertManagerTest, testHasActiveAlertsWhenAlertsExist) {
|
||||||
|
alert_manager.triggerSensorFailure(get_mock_timestamp());
|
||||||
|
EXPECT_TRUE(alert_manager.hasActiveAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: hasUnacknowledgedAlerts 检查未确认预警
|
||||||
|
TEST_F(AlertManagerTest, testHasUnacknowledgedAlertsWhenActive) {
|
||||||
|
alert_manager.triggerSensorFailure(get_mock_timestamp());
|
||||||
|
EXPECT_TRUE(alert_manager.hasUnacknowledgedAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: hasUnacknowledgedAlerts 检查已确认预警
|
||||||
|
TEST_F(AlertManagerTest, testHasUnacknowledgedAlertsWhenAcknowledged) {
|
||||||
|
alert_manager.triggerSensorFailure(get_mock_timestamp());
|
||||||
|
alert_manager.acknowledgeAlert(AlertType::SENSOR_FAILURE, get_mock_timestamp());
|
||||||
|
EXPECT_FALSE(alert_manager.hasUnacknowledgedAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: getActiveAlerts 返回活动预警列表
|
||||||
|
TEST_F(AlertManagerTest, testGetActiveAlertsReturnsCorrectList) {
|
||||||
|
alert_manager.triggerSensorFailure(get_mock_timestamp());
|
||||||
|
alert_manager.triggerLowBattery(get_mock_timestamp(), 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, testGetAlertHistoryMaxCount) {
|
||||||
|
// Add multiple alerts
|
||||||
|
for (int i = 0; i < 5; ++i) {
|
||||||
|
alert_manager.triggerSensorFailure(get_mock_timestamp() + i);
|
||||||
|
}
|
||||||
|
auto history = alert_manager.getAlertHistory(3);
|
||||||
|
EXPECT_EQ(history.size(), 3);
|
||||||
|
EXPECT_EQ(history[0].trigger_time, get_mock_timestamp() + 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: getStatusDescription 生成系统状态描述
|
||||||
|
TEST_F(AlertManagerTest, testGetStatusDescriptionWithActiveAlerts) {
|
||||||
|
alert_manager.triggerSensorFailure(get_mock_timestamp());
|
||||||
|
std::string status = alert_manager.getStatusDescription();
|
||||||
|
EXPECT_NE(status.find("有1个活动预警"), std::string::npos);
|
||||||
|
EXPECT_NE(status.find("传感器故障"), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: getStatusDescription 生成系统正常状态描述
|
||||||
|
TEST_F(AlertManagerTest, testGetStatusDescriptionNormalState) {
|
||||||
|
std::string status = alert_manager.getStatusDescription();
|
||||||
|
EXPECT_EQ(status, "系统状态:正常");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: clearHistory 清空历史记录
|
||||||
|
TEST_F(AlertManagerTest, testClearHistoryClearsAll) {
|
||||||
|
alert_manager.triggerSensorFailure(get_mock_timestamp());
|
||||||
|
alert_manager.clearHistory();
|
||||||
|
EXPECT_TRUE(alert_manager.getAlertHistory(10).empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: addToHistory 添加预警到历史记录并限制数量
|
||||||
|
TEST_F(AlertManagerTest, testAddToHistoryLimitSize) {
|
||||||
|
const size_t max_history = 5;
|
||||||
|
// Simulate adding more than max_history entries
|
||||||
|
for (int i = 0; i < max_history + 2; ++i) {
|
||||||
|
AlertInfo alert;
|
||||||
|
alert.type = AlertType::SENSOR_FAILURE;
|
||||||
|
alert.status = AlertStatus::ACTIVE;
|
||||||
|
alert.trigger_time = get_mock_timestamp() + i;
|
||||||
|
alert.description = "Test alert";
|
||||||
|
alert_manager.addToHistory(alert);
|
||||||
|
}
|
||||||
|
auto history = alert_manager.getAlertHistory(max_history);
|
||||||
|
EXPECT_EQ(history.size(), max_history);
|
||||||
|
EXPECT_EQ(history[0].trigger_time, get_mock_timestamp() + max_history + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: triggerAlert 内部方法触发新预警
|
||||||
|
TEST_F(AlertManagerTest, testTriggerAlertInternalMethod) {
|
||||||
|
alert_manager.triggerAlert(AlertType::ALTITUDE_UPPER, get_mock_timestamp(), "Test description");
|
||||||
|
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].description, "Test description");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: updateAlertStatus 更新预警状态为已确认
|
||||||
|
TEST_F(AlertManagerTest, testUpdateAlertStatusAcknowledge) {
|
||||||
|
alert_manager.triggerSensorFailure(get_mock_timestamp());
|
||||||
|
alert_manager.updateAlertStatus(AlertType::SENSOR_FAILURE, AlertStatus::ACKNOWLEDGED, get_mock_timestamp());
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_TRUE(active_alerts.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: updateAlertStatus 更新预警状态为已解决
|
||||||
|
TEST_F(AlertManagerTest, testUpdateAlertStatusResolve) {
|
||||||
|
alert_manager.triggerSensorFailure(get_mock_timestamp());
|
||||||
|
alert_manager.updateAlertStatus(AlertType::SENSOR_FAILURE, AlertStatus::RESOLVED, get_mock_timestamp());
|
||||||
|
auto active_alerts = alert_manager.getActiveAlerts();
|
||||||
|
EXPECT_TRUE(active_alerts.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: getAlertTypeName 返回正确名称
|
||||||
|
TEST_F(AlertManagerTest, testGetAlertTypeName) {
|
||||||
|
EXPECT_EQ(AlertManager::getAlertTypeName(AlertType::ALTITUDE_UPPER), "高度上限预警");
|
||||||
|
EXPECT_EQ(AlertManager::getAlertTypeName(AlertType::UNKNOWN), "未知预警");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: getAlertStatusName 返回正确状态名
|
||||||
|
TEST_F(AlertManagerTest, testGetAlertStatusName) {
|
||||||
|
EXPECT_EQ(AlertManager::getAlertStatusName(AlertStatus::ACTIVE), "预警中");
|
||||||
|
EXPECT_EQ(AlertManager::getAlertStatusName(AlertStatus::UNKNOWN), "未知状态");
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue