253 lines
8.2 KiB
C++
253 lines
8.2 KiB
C++
|
|
#include "gtest/gtest.h"
|
||
|
|
#include "alert_manager.hpp"
|
||
|
|
#include <memory>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
// Mock 用于测试输出,避免实际打印
|
||
|
|
class MockAlertManager : public AlertManager {
|
||
|
|
public:
|
||
|
|
MockAlertManager() : AlertManager() {}
|
||
|
|
~MockAlertManager() override = default;
|
||
|
|
|
||
|
|
// 重写虚函数以捕获输出
|
||
|
|
void triggerAlert(AlertType type, uint32_t timestamp, const std::string& description) override {
|
||
|
|
triggered_alerts_.push_back({type, timestamp, description});
|
||
|
|
}
|
||
|
|
|
||
|
|
void updateAlertStatus(AlertType type, AlertStatus new_status, uint32_t timestamp) override {
|
||
|
|
updated_statuses_.push_back({type, new_status, timestamp});
|
||
|
|
}
|
||
|
|
|
||
|
|
std::vector<std::tuple<AlertType, uint32_t, std::string>> getTriggeredAlerts() const { return triggered_alerts_; }
|
||
|
|
std::vector<std::tuple<AlertType, AlertStatus, uint32_t>> getUpdatedStatuses() const { return updated_statuses_; }
|
||
|
|
|
||
|
|
void clearTriggeredAlerts() { triggered_alerts_.clear(); }
|
||
|
|
void clearUpdatedStatuses() { updated_statuses_.clear(); }
|
||
|
|
|
||
|
|
private:
|
||
|
|
std::vector<std::tuple<AlertType, uint32_t, std::string>> triggered_alerts_;
|
||
|
|
std::vector<std::tuple<AlertType, AlertStatus, uint32_t>> updated_statuses_;
|
||
|
|
};
|
||
|
|
|
||
|
|
// 测试用例定义
|
||
|
|
TEST(AlertManagerTest, testAlertManagerNormalInitialization) {
|
||
|
|
MockAlertManager manager;
|
||
|
|
|
||
|
|
EXPECT_FLOAT_EQ(manager.upper_threshold_, 100.0f);
|
||
|
|
EXPECT_FLOAT_EQ(manager.lower_threshold_, -50.0f);
|
||
|
|
EXPECT_TRUE(manager.alerts_.empty());
|
||
|
|
EXPECT_TRUE(manager.alert_history_.empty());
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(AlertManagerTest, testAlertManagerDestructor) {
|
||
|
|
MockAlertManager* manager = new MockAlertManager();
|
||
|
|
delete manager;
|
||
|
|
|
||
|
|
// 析构应无异常,仅验证未崩溃
|
||
|
|
EXPECT_TRUE(true);
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(AlertManagerTest, testCheckAltitudeAlertNormalUpperThreshold) {
|
||
|
|
MockAlertManager manager;
|
||
|
|
const float altitude = 105.0f;
|
||
|
|
const uint32_t timestamp = 1234567890;
|
||
|
|
|
||
|
|
manager.checkAltitudeAlert(altitude, timestamp);
|
||
|
|
|
||
|
|
auto alerts = manager.getTriggeredAlerts();
|
||
|
|
EXPECT_EQ(alerts.size(), 1);
|
||
|
|
EXPECT_EQ(std::get<0>(alerts[0]), AlertType::ALTITUDE_UPPER);
|
||
|
|
EXPECT_EQ(std::get<1>(alerts[0]), timestamp);
|
||
|
|
EXPECT_TRUE(std::get<2>(alerts[0]).find("超过上限阈值") != std::string::npos);
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(AlertManagerTest, testCheckAltitudeAlertNormalLowerThreshold) {
|
||
|
|
MockAlertManager manager;
|
||
|
|
const float altitude = -55.0f;
|
||
|
|
const uint32_t timestamp = 1234567890;
|
||
|
|
|
||
|
|
manager.checkAltitudeAlert(altitude, timestamp);
|
||
|
|
|
||
|
|
auto alerts = manager.getTriggeredAlerts();
|
||
|
|
EXPECT_EQ(alerts.size(), 1);
|
||
|
|
EXPECT_EQ(std::get<0>(alerts[0]), AlertType::ALTITUDE_LOWER);
|
||
|
|
EXPECT_EQ(std::get<1>(alerts[0]), timestamp);
|
||
|
|
EXPECT_TRUE(std::get<2>(alerts[0]).find("低于下限阈值") != std::string::npos);
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(AlertManagerTest, testCheckAltitudeAlertNormalResetAfterRecovery) {
|
||
|
|
MockAlertManager manager;
|
||
|
|
const uint32_t timestamp1 = 1234567890;
|
||
|
|
const uint32_t timestamp2 = 1234567891;
|
||
|
|
|
||
|
|
// 触发上限预警
|
||
|
|
manager.checkAltitudeAlert(105.0f, timestamp1);
|
||
|
|
EXPECT_EQ(manager.getTriggeredAlerts().size(), 1);
|
||
|
|
|
||
|
|
// 恢复正常高度,应触发解决状态
|
||
|
|
manager.checkAltitudeAlert(95.0f, timestamp2);
|
||
|
|
auto updates = manager.getUpdatedStatuses();
|
||
|
|
EXPECT_EQ(updates.size(), 1);
|
||
|
|
EXPECT_EQ(std::get<0>(updates[0]), AlertType::ALTITUDE_UPPER);
|
||
|
|
EXPECT_EQ(std::get<1>(updates[0]), AlertStatus::RESOLVED);
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(AlertManagerTest, testSetAltitudeThresholdsValidRange) {
|
||
|
|
MockAlertManager manager;
|
||
|
|
const float upper = 200.0f;
|
||
|
|
const float lower = -100.0f;
|
||
|
|
|
||
|
|
manager.setAltitudeThresholds(upper, lower);
|
||
|
|
|
||
|
|
EXPECT_FLOAT_EQ(manager.upper_threshold_, upper);
|
||
|
|
EXPECT_FLOAT_EQ(manager.lower_threshold_, lower);
|
||
|
|
EXPECT_EQ(manager.getTriggeredAlerts().size(), 0); // 不应触发新预警
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(AlertManagerTest, testSetAltitudeThresholdsInvalidRange) {
|
||
|
|
MockAlertManager manager;
|
||
|
|
|
||
|
|
// 上限不大于下限,不应修改且应有错误提示(通过日志捕获)
|
||
|
|
manager.setAltitudeThresholds(-50.0f, 100.0f);
|
||
|
|
|
||
|
|
EXPECT_FLOAT_EQ(manager.upper_threshold_, 100.0f);
|
||
|
|
EXPECT_FLOAT_EQ(manager.lower_threshold_, -50.0f);
|
||
|
|
EXPECT_EQ(manager.getTriggeredAlerts().size(), 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(AlertManagerTest, testResetAllAlertsClearsActiveAlerts) {
|
||
|
|
MockAlertManager manager;
|
||
|
|
const uint32_t timestamp = 1234567890;
|
||
|
|
|
||
|
|
// 手动添加一个活动预警
|
||
|
|
AlertInfo alert;
|
||
|
|
alert.type = AlertType::ALTITUDE_UPPER;
|
||
|
|
alert.status = AlertStatus::ACTIVE;
|
||
|
|
alert.trigger_time = timestamp;
|
||
|
|
alert.description = "test";
|
||
|
|
manager.alerts_[AlertType::ALTITUDE_UPPER] = alert;
|
||
|
|
|
||
|
|
manager.resetAllAlerts();
|
||
|
|
|
||
|
|
EXPECT_TRUE(manager.alerts_.empty());
|
||
|
|
EXPECT_TRUE(manager.getUpdatedStatuses().empty());
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(AlertManagerTest, testHasActiveAlertsNoAlerts) {
|
||
|
|
MockAlertManager manager;
|
||
|
|
|
||
|
|
EXPECT_FALSE(manager.hasActiveAlerts());
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(AlertManagerTest, testHasActiveAlertsWithActiveAlert) {
|
||
|
|
MockAlertManager manager;
|
||
|
|
const uint32_t timestamp = 1234567890;
|
||
|
|
|
||
|
|
AlertInfo alert;
|
||
|
|
alert.type = AlertType::ALTITUDE_UPPER;
|
||
|
|
alert.status = AlertStatus::ACTIVE;
|
||
|
|
alert.trigger_time = timestamp;
|
||
|
|
alert.description = "test";
|
||
|
|
manager.alerts_[AlertType::ALTITUDE_UPPER] = alert;
|
||
|
|
|
||
|
|
EXPECT_TRUE(manager.hasActiveAlerts());
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(AlertManagerTest, testGetActiveAlertsEmpty) {
|
||
|
|
MockAlertManager manager;
|
||
|
|
|
||
|
|
auto active_alerts = manager.getActiveAlerts();
|
||
|
|
EXPECT_TRUE(active_alerts.empty());
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(AlertManagerTest, testGetActiveAlertsWithOneAlert) {
|
||
|
|
MockAlertManager manager;
|
||
|
|
const uint32_t timestamp = 1234567890;
|
||
|
|
|
||
|
|
AlertInfo alert;
|
||
|
|
alert.type = AlertType::ALTITUDE_UPPER;
|
||
|
|
alert.status = AlertStatus::ACTIVE;
|
||
|
|
alert.trigger_time = timestamp;
|
||
|
|
alert.description = "test";
|
||
|
|
manager.alerts_[AlertType::ALTITUDE_UPPER] = alert;
|
||
|
|
|
||
|
|
auto active_alerts = 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(AlertManagerTest, testGetAlertHistoryMaxCount) {
|
||
|
|
MockAlertManager manager;
|
||
|
|
const uint32_t timestamp = 1234567890;
|
||
|
|
|
||
|
|
// 添加多个历史记录
|
||
|
|
for (int i = 0; i < 5; ++i) {
|
||
|
|
AlertInfo alert;
|
||
|
|
alert.type = AlertType::ALTITUDE_UPPER;
|
||
|
|
alert.status = AlertStatus::ACTIVE;
|
||
|
|
alert.trigger_time = timestamp + i;
|
||
|
|
alert.description = "test";
|
||
|
|
manager.addToHistory(alert);
|
||
|
|
}
|
||
|
|
|
||
|
|
auto history = manager.getAlertHistory(3);
|
||
|
|
EXPECT_EQ(history.size(), 3);
|
||
|
|
EXPECT_EQ(history[0].trigger_time, timestamp + 2);
|
||
|
|
EXPECT_EQ(history[2].trigger_time, timestamp);
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(AlertManagerTest, testGetAlertHistoryLimitExceeded) {
|
||
|
|
MockAlertManager manager;
|
||
|
|
const uint32_t timestamp = 1234567890;
|
||
|
|
|
||
|
|
for (int i = 0; i < 10; ++i) {
|
||
|
|
AlertInfo alert;
|
||
|
|
alert.type = AlertType::ALTITUDE_UPPER;
|
||
|
|
alert.status = AlertStatus::ACTIVE;
|
||
|
|
alert.trigger_time = timestamp + i;
|
||
|
|
alert.description = "test";
|
||
|
|
manager.addToHistory(alert);
|
||
|
|
}
|
||
|
|
|
||
|
|
auto history = manager.getAlertHistory(5);
|
||
|
|
EXPECT_EQ(history.size(), 5);
|
||
|
|
EXPECT_EQ(history[0].trigger_time, timestamp + 9);
|
||
|
|
EXPECT_EQ(history[4].trigger_time, timestamp + 5);
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(AlertManagerTest, testClearHistoryClearsAllRecords) {
|
||
|
|
MockAlertManager manager;
|
||
|
|
const uint32_t timestamp = 1234567890;
|
||
|
|
|
||
|
|
for (int i = 0; i < 3; ++i) {
|
||
|
|
AlertInfo alert;
|
||
|
|
alert.type = AlertType::ALTITUDE_UPPER;
|
||
|
|
alert.status = AlertStatus::ACTIVE;
|
||
|
|
alert.trigger_time = timestamp + i;
|
||
|
|
alert.description = "test";
|
||
|
|
manager.addToHistory(alert);
|
||
|
|
}
|
||
|
|
|
||
|
|
manager.clearHistory();
|
||
|
|
|
||
|
|
EXPECT_TRUE(manager.alert_history_.empty());
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(AlertManagerTest, testAddToHistoryLimiting) {
|
||
|
|
MockAlertManager manager;
|
||
|
|
const uint32_t timestamp = 1234567890;
|
||
|
|
|
||
|
|
for (int i = 0; i < 10; ++i) {
|
||
|
|
AlertInfo alert;
|
||
|
|
alert.type = AlertType::ALTITUDE_UPPER;
|
||
|
|
alert.status = AlertStatus::ACTIVE;
|
||
|
|
alert.trigger_time = timestamp + i;
|
||
|
|
alert.description = "test";
|
||
|
|
manager.addToHistory(alert);
|
||
|
|
}
|
||
|
|
|
||
|
|
EXPECT_EQ(manager.alert_history_.size(), MAX_HISTORY);
|
||
|
|
EXPECT_EQ(manager.alert_history_[0].trigger_time, timestamp + 1);
|
||
|
|
EXPECT_EQ(manager.alert_history_[MAX_HISTORY - 1].trigger_time, timestamp + 9);
|
||
|
|
}
|