Compare commits
4 Commits
main
...
test_20260
| Author | SHA1 | Date |
|---|---|---|
|
|
d1008b2b5d | |
|
|
e1e90f5fa8 | |
|
|
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,25 @@
|
||||||
|
cmake_minimum_required(VERSION 3.10.0)
|
||||||
|
project(test_CppGenerate)
|
||||||
|
include(FetchContent)
|
||||||
|
if (MSVC)
|
||||||
|
add_compile_options(/utf-8)
|
||||||
|
endif()
|
||||||
|
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)
|
||||||
|
set(CMAKE_CXX_STANDARD 14)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
|
||||||
|
|
||||||
|
include(CTest)
|
||||||
|
enable_testing()
|
||||||
|
|
||||||
|
add_executable(test_CppGenerate test_alert_manager.cpp ../src/alert_manager.cpp)
|
||||||
|
|
||||||
|
target_link_libraries(test_CppGenerate gtest gmock gtest_main)
|
||||||
|
include(GoogleTest)
|
||||||
|
gtest_discover_tests(test_CppGenerate)
|
||||||
|
|
@ -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,269 @@
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "alert_manager.hpp"
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// Mock time function for deterministic testing
|
||||||
|
uint32_t mock_timestamp = 1640995200; // 2022-01-01 00:00:00 UTC
|
||||||
|
|
||||||
|
// Mock the time function to return a fixed value
|
||||||
|
uint32_t getMockTimestamp() {
|
||||||
|
return mock_timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Override the triggerAlert and updateAlertStatus methods for testing
|
||||||
|
void AlertManager::triggerAlert(AlertType type, uint32_t timestamp, const std::string& description) {
|
||||||
|
AlertInfo alert;
|
||||||
|
alert.type = type;
|
||||||
|
alert.status = AlertStatus::ACTIVE;
|
||||||
|
alert.trigger_time = timestamp;
|
||||||
|
alert.description = description;
|
||||||
|
alerts_[type] = alert;
|
||||||
|
addToHistory(alert);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AlertManager::updateAlertStatus(AlertType type, AlertStatus new_status, uint32_t timestamp) {
|
||||||
|
auto it = alerts_.find(type);
|
||||||
|
if (it == alerts_.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
AlertInfo& alert = it->second;
|
||||||
|
alert.status = new_status;
|
||||||
|
switch (new_status) {
|
||||||
|
case AlertStatus::ACKNOWLEDGED:
|
||||||
|
alert.acknowledge_time = timestamp;
|
||||||
|
break;
|
||||||
|
case AlertStatus::RESOLVED:
|
||||||
|
alert.resolve_time = timestamp;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Update history
|
||||||
|
for (auto& hist_alert : alert_history_) {
|
||||||
|
if (hist_alert.type == type && hist_alert.trigger_time == alert.trigger_time) {
|
||||||
|
hist_alert = alert;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test fixture for AlertManager
|
||||||
|
class AlertManagerTest : public ::testing::Test {
|
||||||
|
protected:
|
||||||
|
std::unique_ptr<AlertManager> alert_manager_;
|
||||||
|
|
||||||
|
void SetUp() override {
|
||||||
|
alert_manager_ = std::make_unique<AlertManager>();
|
||||||
|
mock_timestamp = 1640995200;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TearDown() override {
|
||||||
|
alert_manager_->clearHistory();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Test checkAltitudeAlert with normal input
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertNormalInput) {
|
||||||
|
// Arrange
|
||||||
|
const float altitude = 120.0f;
|
||||||
|
const uint32_t timestamp = getMockTimestamp();
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
alert_manager_->checkAltitudeAlert(altitude, timestamp);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
EXPECT_TRUE(alert_manager_->hasActiveAlerts());
|
||||||
|
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);
|
||||||
|
EXPECT_EQ(active_alerts[0].trigger_time, timestamp);
|
||||||
|
EXPECT_EQ(active_alerts[0].description, "高度超过上限阈值:当前120米 > 阈值100米");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test checkAltitudeAlert when altitude is within bounds
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertWithinBounds) {
|
||||||
|
// Arrange
|
||||||
|
const float altitude = 80.0f;
|
||||||
|
const uint32_t timestamp = getMockTimestamp();
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
alert_manager_->checkAltitudeAlert(altitude, timestamp);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
EXPECT_FALSE(alert_manager_->hasActiveAlerts());
|
||||||
|
EXPECT_EQ(alert_manager_->getActiveAlerts().size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test checkAltitudeAlert when altitude crosses lower threshold
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertLowerThresholdCrossed) {
|
||||||
|
// Arrange
|
||||||
|
const float altitude = -60.0f;
|
||||||
|
const uint32_t timestamp = getMockTimestamp();
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
alert_manager_->checkAltitudeAlert(altitude, timestamp);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
EXPECT_TRUE(alert_manager_->hasActiveAlerts());
|
||||||
|
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);
|
||||||
|
EXPECT_EQ(active_alerts[0].trigger_time, timestamp);
|
||||||
|
EXPECT_EQ(active_alerts[0].description, "高度低于下限阈值:当前-60米 < 阈值-50米");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test setAltitudeThresholds with valid inputs
|
||||||
|
TEST_F(AlertManagerTest, testSetAltitudeThresholdsValidInputs) {
|
||||||
|
// Arrange
|
||||||
|
const float upper = 150.0f;
|
||||||
|
const float lower = -70.0f;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
alert_manager_->setAltitudeThresholds(upper, lower);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
EXPECT_FLOAT_EQ(alert_manager_->getUpperThreshold(), upper);
|
||||||
|
EXPECT_FLOAT_EQ(alert_manager_->getLowerThreshold(), lower);
|
||||||
|
EXPECT_FALSE(alert_manager_->hasActiveAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test setAltitudeThresholds with invalid inputs (upper <= lower)
|
||||||
|
TEST_F(AlertManagerTest, testSetAltitudeThresholdsInvalidInputs) {
|
||||||
|
// Arrange
|
||||||
|
const float upper = 50.0f;
|
||||||
|
const float lower = 100.0f;
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
EXPECT_NO_THROW(alert_manager_->setAltitudeThresholds(upper, lower));
|
||||||
|
EXPECT_TRUE(alert_manager_->hasActiveAlerts());
|
||||||
|
EXPECT_EQ(alert_manager_->getActiveAlerts().size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test checkAltitudeAlert after threshold change
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertAfterThresholdChange) {
|
||||||
|
// Arrange
|
||||||
|
const float altitude = 120.0f;
|
||||||
|
const uint32_t timestamp = getMockTimestamp();
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
alert_manager_->checkAltitudeAlert(altitude, timestamp);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
EXPECT_TRUE(alert_manager_->hasActiveAlerts());
|
||||||
|
auto active_alerts = alert_manager_->getActiveAlerts();
|
||||||
|
EXPECT_EQ(active_alerts.size(), 1);
|
||||||
|
EXPECT_EQ(active_alerts[0].type, AlertType::ALTITUDE_UPPER);
|
||||||
|
|
||||||
|
// Change threshold to allow altitude to be valid
|
||||||
|
alert_manager_->setAltitudeThresholds(130.0f, -50.0f);
|
||||||
|
|
||||||
|
// Re-check
|
||||||
|
alert_manager_->checkAltitudeAlert(altitude, timestamp + 1);
|
||||||
|
|
||||||
|
// Assert that alert is resolved
|
||||||
|
EXPECT_FALSE(alert_manager_->hasActiveAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test checkAltitudeAlert with zero altitude
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertZeroAltitude) {
|
||||||
|
// Arrange
|
||||||
|
const float altitude = 0.0f;
|
||||||
|
const uint32_t timestamp = getMockTimestamp();
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
alert_manager_->checkAltitudeAlert(altitude, timestamp);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
EXPECT_FALSE(alert_manager_->hasActiveAlerts());
|
||||||
|
EXPECT_EQ(alert_manager_->getActiveAlerts().size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test checkAltitudeAlert with extreme values
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertExtremeValues) {
|
||||||
|
// Arrange
|
||||||
|
const float altitude = 1e6f; // Very high
|
||||||
|
const uint32_t timestamp = getMockTimestamp();
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
alert_manager_->checkAltitudeAlert(altitude, timestamp);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
EXPECT_TRUE(alert_manager_->hasActiveAlerts());
|
||||||
|
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);
|
||||||
|
EXPECT_EQ(active_alerts[0].trigger_time, timestamp);
|
||||||
|
EXPECT_NE(active_alerts[0].description.find("1e+06"), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test setAltitudeThresholds with equal values
|
||||||
|
TEST_F(AlertManagerTest, testSetAltitudeThresholdsEqualValues) {
|
||||||
|
// Arrange
|
||||||
|
const float upper = 100.0f;
|
||||||
|
const float lower = 100.0f;
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
|
EXPECT_NO_THROW(alert_manager_->setAltitudeThresholds(upper, lower));
|
||||||
|
EXPECT_TRUE(alert_manager_->hasActiveAlerts());
|
||||||
|
EXPECT_EQ(alert_manager_->getActiveAlerts().size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test checkAltitudeAlert with negative altitude below lower threshold
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertNegativeBelowLower) {
|
||||||
|
// Arrange
|
||||||
|
const float altitude = -100.0f;
|
||||||
|
const uint32_t timestamp = getMockTimestamp();
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
alert_manager_->checkAltitudeAlert(altitude, timestamp);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
EXPECT_TRUE(alert_manager_->hasActiveAlerts());
|
||||||
|
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);
|
||||||
|
EXPECT_EQ(active_alerts[0].trigger_time, timestamp);
|
||||||
|
EXPECT_EQ(active_alerts[0].description, "高度低于下限阈值:当前-100米 < 阈值-50米");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test checkAltitudeAlert with altitude exactly at upper threshold
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertAtUpperThreshold) {
|
||||||
|
// Arrange
|
||||||
|
const float altitude = 100.0f;
|
||||||
|
const uint32_t timestamp = getMockTimestamp();
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
alert_manager_->checkAltitudeAlert(altitude, timestamp);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
EXPECT_FALSE(alert_manager_->hasActiveAlerts());
|
||||||
|
EXPECT_EQ(alert_manager_->getActiveAlerts().size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test checkAltitudeAlert with altitude exactly at lower threshold
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertAtLowerThreshold) {
|
||||||
|
// Arrange
|
||||||
|
const float altitude = -50.0f;
|
||||||
|
const uint32_t timestamp = getMockTimestamp();
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
alert_manager_->checkAltitudeAlert(altitude, timestamp);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
EXPECT_FALSE(alert_manager_->hasActiveAlerts());
|
||||||
|
EXPECT_EQ(alert_manager_->getActiveAlerts().size(), 0);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue