CppGenerate/test1/test_alert_manager.cpp

118 lines
4.0 KiB
C++
Raw Permalink Normal View History

2026-04-24 06:51:07 +00:00
#include <gtest/gtest.h>
#include <memory>
#include "alert_manager.hpp"
using namespace std;
class AlertManagerTest : public ::testing::Test {
protected:
AlertManager alertManager;
void SetUp() override {
alertManager.initialize();
}
};
// 测试 resetAlert 正常输入
TEST_F(AlertManagerTest, testResetAlertNormalInput) {
// 触发一个高度上限预警
alertManager.checkAltitudeAlert(150.0f, 100);
EXPECT_TRUE(alertManager.hasActiveAlerts());
// 重置该预警
alertManager.resetAlert(AlertType::ALTITUDE_UPPER);
// 验证预警状态已恢复为 INACTIVE
auto it = alertManager.getAlertHistory().end();
if (alertManager.getAlertHistory().size() > 0) {
it = alertManager.getAlertHistory().begin();
}
EXPECT_EQ(it->status, AlertStatus::INACTIVE);
}
// 测试 resetAlert 边界值:重置 NONE 类型
TEST_F(AlertManagerTest, testResetAlertBoundaryValueNoneType) {
// 尝试重置 NONE 类型,应无副作用
alertManager.resetAlert(AlertType::NONE);
EXPECT_FALSE(alertManager.hasActiveAlerts());
}
// 测试 resetAllAlerts 正常输入
TEST_F(AlertManagerTest, testResetAllAlertsNormalInput) {
// 触发多个预警
alertManager.checkAltitudeAlert(150.0f, 100);
alertManager.triggerLowBattery(200, 15.0f);
EXPECT_TRUE(alertManager.hasActiveAlerts());
// 重置所有预警
alertManager.resetAllAlerts();
// 验证所有预警均被清除
EXPECT_FALSE(alertManager.hasActiveAlerts());
EXPECT_FALSE(alertManager.hasUnacknowledgedAlerts());
}
// 测试 resetAllAlerts 特殊场景:无预警时调用
TEST_F(AlertManagerTest, testResetAllAlertsSpecialCaseNoAlerts) {
// 确保初始状态下无预警
EXPECT_FALSE(alertManager.hasActiveAlerts());
EXPECT_FALSE(alertManager.hasUnacknowledgedAlerts());
// 调用重置函数
alertManager.resetAllAlerts();
// 验证状态仍为无预警
EXPECT_FALSE(alertManager.hasActiveAlerts());
EXPECT_FALSE(alertManager.hasUnacknowledgedAlerts());
}
// 测试 hasActiveAlerts 正常输入:存在活动预警
TEST_F(AlertManagerTest, testHasActiveAlertsNormalInputWithActiveAlert) {
// 触发一个活动预警
alertManager.checkAltitudeAlert(150.0f, 100);
EXPECT_TRUE(alertManager.hasActiveAlerts());
}
// 测试 hasActiveAlerts 正常输入:无活动预警
TEST_F(AlertManagerTest, testHasActiveAlertsNormalInputWithoutActiveAlert) {
// 初始状态无预警
EXPECT_FALSE(alertManager.hasActiveAlerts());
}
// 测试 hasActiveAlerts 边界值:确认后预警应视为活动
TEST_F(AlertManagerTest, testHasActiveAlertsBoundaryValueAcknowledgedAlert) {
// 触发并确认预警
alertManager.checkAltitudeAlert(150.0f, 100);
alertManager.acknowledgeAlert(AlertType::ALTITUDE_UPPER, 200);
// 确认后状态为 ACKNOWLEDGED仍属于活动状态
EXPECT_TRUE(alertManager.hasActiveAlerts());
}
// 测试 hasUnacknowledgedAlerts 正常输入:存在未确认预警
TEST_F(AlertManagerTest, testHasUnacknowledgedAlertsNormalInputWithUnacknowledgedAlert) {
// 触发预警但未确认
alertManager.checkAltitudeAlert(150.0f, 100);
EXPECT_TRUE(alertManager.hasUnacknowledgedAlerts());
}
// 测试 hasUnacknowledgedAlerts 正常输入:已确认预警不应计入
TEST_F(AlertManagerTest, testHasUnacknowledgedAlertsNormalInputAfterAcknowledgment) {
// 触发并确认预警
alertManager.checkAltitudeAlert(150.0f, 100);
alertManager.acknowledgeAlert(AlertType::ALTITUDE_UPPER, 200);
// 状态变为 ACKNOWLEDGED不再算作未确认
EXPECT_FALSE(alertManager.hasUnacknowledgedAlerts());
}
// 测试 hasUnacknowledgedAlerts 特殊场景:仅 RESOLVED 预警
TEST_F(AlertManagerTest, testHasUnacknowledgedAlertsSpecialCaseResolvedOnly) {
// 手动设置一个已解决的预警
AlertInfo info(AlertType::LOW_BATTERY);
info.status = AlertStatus::RESOLVED;
info.trigger_time = 100;
alertManager.alerts_[AlertType::LOW_BATTERY] = info;
EXPECT_FALSE(alertManager.hasUnacknowledgedAlerts());
}