Compare commits
4 Commits
main
...
test_20260
| Author | SHA1 | Date |
|---|---|---|
|
|
fa7d6fcff2 | |
|
|
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,156 @@
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "alert_manager.hpp"
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// Mock class to capture output for testing
|
||||||
|
class MockAlertManager : public AlertManager {
|
||||||
|
public:
|
||||||
|
std::vector<std::string> captured_output;
|
||||||
|
|
||||||
|
void print(const std::string& msg) override {
|
||||||
|
captured_output.push_back(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Override to prevent actual console output
|
||||||
|
void triggerAlert(AlertType type, uint32_t timestamp, const std::string& description) override {
|
||||||
|
AlertInfo alert;
|
||||||
|
alert.type = type;
|
||||||
|
alert.status = AlertStatus::ACTIVE;
|
||||||
|
alert.trigger_time = timestamp;
|
||||||
|
alert.description = description;
|
||||||
|
alerts_[type] = alert;
|
||||||
|
addToHistory(alert);
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateAlertStatus(AlertType type, AlertStatus new_status, uint32_t timestamp) override {
|
||||||
|
auto it = alerts_.find(type);
|
||||||
|
if (it == alerts_.end()) return;
|
||||||
|
AlertInfo& alert = it->second;
|
||||||
|
alert.status = new_status;
|
||||||
|
alert.acknowledge_time = (new_status == AlertStatus::ACKNOWLEDGED) ? timestamp : alert.acknowledge_time;
|
||||||
|
alert.resolve_time = (new_status == AlertStatus::RESOLVED) ? timestamp : alert.resolve_time;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Test fixture for AlertManager
|
||||||
|
class AlertManagerTest : public ::testing::Test {
|
||||||
|
protected:
|
||||||
|
std::unique_ptr<MockAlertManager> alert_manager_;
|
||||||
|
|
||||||
|
void SetUp() override {
|
||||||
|
alert_manager_ = std::make_unique<MockAlertManager>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TearDown() override {
|
||||||
|
alert_manager_->captured_output.clear();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Test case: 正常输入测试 - 检查高度上限预警触发
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertNormalUpperTrigger) {
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
alert_manager_->checkAltitudeAlert(105.0f, 1234567890);
|
||||||
|
|
||||||
|
EXPECT_TRUE(alert_manager_->hasActiveAlerts());
|
||||||
|
EXPECT_EQ(alert_manager_->getActiveAlerts().size(), 1);
|
||||||
|
EXPECT_EQ(alert_manager_->getActiveAlerts()[0].type, AlertType::ALTITUDE_UPPER);
|
||||||
|
EXPECT_EQ(alert_manager_->getActiveAlerts()[0].status, AlertStatus::ACTIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: 正常输入测试 - 检查高度下限预警触发
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertNormalLowerTrigger) {
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
alert_manager_->checkAltitudeAlert(-55.0f, 1234567890);
|
||||||
|
|
||||||
|
EXPECT_TRUE(alert_manager_->hasActiveAlerts());
|
||||||
|
EXPECT_EQ(alert_manager_->getActiveAlerts().size(), 1);
|
||||||
|
EXPECT_EQ(alert_manager_->getActiveAlerts()[0].type, AlertType::ALTITUDE_LOWER);
|
||||||
|
EXPECT_EQ(alert_manager_->getActiveAlerts()[0].status, AlertStatus::ACTIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: 边界值测试 - 高度等于上限阈值,不应触发预警
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertBoundaryUpperEqual) {
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
alert_manager_->checkAltitudeAlert(100.0f, 1234567890);
|
||||||
|
|
||||||
|
EXPECT_FALSE(alert_manager_->hasActiveAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: 边界值测试 - 高度等于下限阈值,不应触发预警
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertBoundaryLowerEqual) {
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
alert_manager_->checkAltitudeAlert(-50.0f, 1234567890);
|
||||||
|
|
||||||
|
EXPECT_FALSE(alert_manager_->hasActiveAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: 异常输入测试 - 上限阈值小于下限阈值,应输出错误提示
|
||||||
|
TEST_F(AlertManagerTest, testSetAltitudeThresholdsInvalidInput) {
|
||||||
|
alert_manager_->setAltitudeThresholds(-10.0f, 10.0f);
|
||||||
|
|
||||||
|
EXPECT_EQ(alert_manager_->upper_threshold_, 100.0f); // Should remain unchanged
|
||||||
|
EXPECT_EQ(alert_manager_->lower_threshold_, -50.0f);
|
||||||
|
EXPECT_TRUE(alert_manager_->captured_output.empty()); // No error message printed
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: 特殊场景测试 - 高度从超限恢复到正常范围,预警应被解决
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertRecoveryFromUpper) {
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
alert_manager_->checkAltitudeAlert(105.0f, 1234567890); // Trigger upper alert
|
||||||
|
EXPECT_TRUE(alert_manager_->hasActiveAlerts());
|
||||||
|
|
||||||
|
alert_manager_->checkAltitudeAlert(95.0f, 1234567891); // Recovery
|
||||||
|
EXPECT_FALSE(alert_manager_->hasActiveAlerts());
|
||||||
|
EXPECT_EQ(alert_manager_->getActiveAlerts().size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: 特殊场景测试 - 高度从超限恢复到正常范围,下限预警应被解决
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertRecoveryFromLower) {
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
alert_manager_->checkAltitudeAlert(-55.0f, 1234567890); // Trigger lower alert
|
||||||
|
EXPECT_TRUE(alert_manager_->hasActiveAlerts());
|
||||||
|
|
||||||
|
alert_manager_->checkAltitudeAlert(-45.0f, 1234567891); // Recovery
|
||||||
|
EXPECT_FALSE(alert_manager_->hasActiveAlerts());
|
||||||
|
EXPECT_EQ(alert_manager_->getActiveAlerts().size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: 正常输入测试 - 设置合法阈值,检查是否更新成功
|
||||||
|
TEST_F(AlertManagerTest, testSetAltitudeThresholdsValidInput) {
|
||||||
|
alert_manager_->setAltitudeThresholds(200.0f, -100.0f);
|
||||||
|
|
||||||
|
EXPECT_EQ(alert_manager_->upper_threshold_, 200.0f);
|
||||||
|
EXPECT_EQ(alert_manager_->lower_threshold_, -100.0f);
|
||||||
|
EXPECT_TRUE(alert_manager_->hasActiveAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: 特殊场景测试 - 多次调用 checkAltitudeAlert,验证状态一致性
|
||||||
|
TEST_F(AlertManagerTest, testCheckAltitudeAlertMultipleCalls) {
|
||||||
|
alert_manager_->setAltitudeThresholds(100.0f, -50.0f);
|
||||||
|
|
||||||
|
alert_manager_->checkAltitudeAlert(105.0f, 1234567890); // Trigger
|
||||||
|
EXPECT_TRUE(alert_manager_->hasActiveAlerts());
|
||||||
|
|
||||||
|
alert_manager_->checkAltitudeAlert(105.0f, 1234567891); // Still above
|
||||||
|
EXPECT_TRUE(alert_manager_->hasActiveAlerts());
|
||||||
|
|
||||||
|
alert_manager_->checkAltitudeAlert(95.0f, 1234567892); // Recover
|
||||||
|
EXPECT_FALSE(alert_manager_->hasActiveAlerts());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: 边界值测试 - 阈值为极小值和极大值
|
||||||
|
TEST_F(AlertManagerTest, testSetAltitudeThresholdsExtremeValues) {
|
||||||
|
alert_manager_->setAltitudeThresholds(FLT_MAX, FLT_MIN);
|
||||||
|
|
||||||
|
EXPECT_EQ(alert_manager_->upper_threshold_, FLT_MAX);
|
||||||
|
EXPECT_EQ(alert_manager_->lower_threshold_, FLT_MIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case: 异常输入测试 - 空值输入(使用零值)
|
||||||
|
TEST_F(AlertManagerTest, testSetAltitudeThresholdsZeroValues) {
|
||||||
|
alert_manager_->setAltitudeThresholds(0.0f, 0.0f);
|
||||||
|
|
||||||
|
EXPECT_EQ(alert_manager_->upper_threshold_, 100.0f);
|
||||||
|
EXPECT_EQ(alert_manager_->lower_threshold_, -50.0f);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue