Compare commits
1 Commits
main
...
test_20260
| Author | SHA1 | Date |
|---|---|---|
|
|
2d14c9f5e3 |
|
|
@ -0,0 +1,25 @@
|
||||||
|
cmake_minimum_required(VERSION 3.10.0)
|
||||||
|
project(test_plan_execute_t1)
|
||||||
|
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 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
|
||||||
|
|
||||||
|
include(CTest)
|
||||||
|
enable_testing()
|
||||||
|
|
||||||
|
add_executable(test_plan_execute_t1 test_app.cpp ../src/app.cpp)
|
||||||
|
|
||||||
|
target_link_libraries(test_plan_execute_t1 gtest gmock gtest_main)
|
||||||
|
include(GoogleTest)
|
||||||
|
gtest_discover_tests(test_plan_execute_t1)
|
||||||
|
|
@ -0,0 +1,363 @@
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include "app.hpp"
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// CmsEngine 测试夹具
|
||||||
|
// ============================================================================
|
||||||
|
class CmsEngineTest : public ::testing::Test {
|
||||||
|
protected:
|
||||||
|
void SetUp() override {
|
||||||
|
// 初始化引擎
|
||||||
|
engine = new CmsEngine();
|
||||||
|
|
||||||
|
// 准备测试事件
|
||||||
|
EventRecord event1;
|
||||||
|
event1.id = "EVT-001";
|
||||||
|
event1.priority = 100;
|
||||||
|
event1.status = EventStatus::Pending;
|
||||||
|
event1.description = "Test event 1";
|
||||||
|
|
||||||
|
EventRecord event2;
|
||||||
|
event2.id = "EVT-002";
|
||||||
|
event2.priority = 200;
|
||||||
|
event2.status = EventStatus::Pending;
|
||||||
|
event2.description = "Test event 2";
|
||||||
|
|
||||||
|
EventRecord event3;
|
||||||
|
event3.id = "EVT-003";
|
||||||
|
event3.priority = 50;
|
||||||
|
event3.status = EventStatus::Generated; // 非 Pending 状态
|
||||||
|
event3.description = "Test event 3";
|
||||||
|
|
||||||
|
// 注入测试事件
|
||||||
|
engine->ingestEvent(event1);
|
||||||
|
engine->ingestEvent(event2);
|
||||||
|
engine->ingestEvent(event3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TearDown() override {
|
||||||
|
delete engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
CmsEngine* engine;
|
||||||
|
};
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// processPendingEvents 测试用例
|
||||||
|
// ============================================================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 测试正常处理待处理事件
|
||||||
|
*
|
||||||
|
* 验证 processPendingEvents 能正确处理所有 Pending 状态的事件,
|
||||||
|
* 返回处理数量,并生成对应的 TaskPlan。
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testProcessPendingEventsNormal) {
|
||||||
|
// 执行:处理待处理事件
|
||||||
|
size_t processedCount = engine->processPendingEvents();
|
||||||
|
|
||||||
|
// 验证:应该处理了 2 个 Pending 事件(EVT-001 和 EVT-002)
|
||||||
|
EXPECT_EQ(processedCount, 2);
|
||||||
|
|
||||||
|
// 验证:事件状态已更新
|
||||||
|
const EventRecord* event1 = engine->findEventById("EVT-001");
|
||||||
|
ASSERT_NE(event1, nullptr);
|
||||||
|
EXPECT_EQ(event1->status, EventStatus::Generated);
|
||||||
|
|
||||||
|
const EventRecord* event2 = engine->findEventById("EVT-002");
|
||||||
|
ASSERT_NE(event2, nullptr);
|
||||||
|
EXPECT_EQ(event2->status, EventStatus::Generated);
|
||||||
|
|
||||||
|
// 验证:EVT-003 状态未改变(原本就是 Generated)
|
||||||
|
const EventRecord* event3 = engine->findEventById("EVT-003");
|
||||||
|
ASSERT_NE(event3, nullptr);
|
||||||
|
EXPECT_EQ(event3->status, EventStatus::Generated);
|
||||||
|
|
||||||
|
// 验证:生成了对应的 TaskPlan
|
||||||
|
const std::vector<TaskPlan>& plans = engine->getAllPlans();
|
||||||
|
EXPECT_EQ(plans.size(), 2);
|
||||||
|
|
||||||
|
// 验证:生成的计划与事件关联
|
||||||
|
bool foundPlan1 = false;
|
||||||
|
bool foundPlan2 = false;
|
||||||
|
for (const auto& plan : plans) {
|
||||||
|
if (plan.relatedEventId == "EVT-001") {
|
||||||
|
foundPlan1 = true;
|
||||||
|
EXPECT_EQ(plan.name, "AutoPlan-EVT-001");
|
||||||
|
EXPECT_EQ(plan.type, PlanType::Centralized);
|
||||||
|
EXPECT_EQ(plan.status, PlanStatus::Drafting);
|
||||||
|
EXPECT_FLOAT_EQ(plan.resourceQuota, 0.5f);
|
||||||
|
EXPECT_EQ(plan.constraints, "自动生成约束");
|
||||||
|
} else if (plan.relatedEventId == "EVT-002") {
|
||||||
|
foundPlan2 = true;
|
||||||
|
EXPECT_EQ(plan.name, "AutoPlan-EVT-002");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EXPECT_TRUE(foundPlan1);
|
||||||
|
EXPECT_TRUE(foundPlan2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 测试无待处理事件时的处理
|
||||||
|
*
|
||||||
|
* 验证当事件库中没有 Pending 状态的事件时,processPendingEvents 返回 0。
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testProcessPendingEventsNoPending) {
|
||||||
|
// 先处理所有 Pending 事件
|
||||||
|
engine->processPendingEvents();
|
||||||
|
|
||||||
|
// 再次执行:此时没有 Pending 事件
|
||||||
|
size_t processedCount = engine->processPendingEvents();
|
||||||
|
|
||||||
|
// 验证:处理数量为 0
|
||||||
|
EXPECT_EQ(processedCount, 0);
|
||||||
|
|
||||||
|
// 验证:计划数量没有增加
|
||||||
|
const std::vector<TaskPlan>& plans = engine->getAllPlans();
|
||||||
|
EXPECT_EQ(plans.size(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 测试空事件库的处理
|
||||||
|
*
|
||||||
|
* 验证当事件库为空时,processPendingEvents 返回 0。
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testProcessPendingEventsEmpty) {
|
||||||
|
// 创建新的空引擎
|
||||||
|
CmsEngine emptyEngine;
|
||||||
|
|
||||||
|
// 执行:处理待处理事件
|
||||||
|
size_t processedCount = emptyEngine.processPendingEvents();
|
||||||
|
|
||||||
|
// 验证:处理数量为 0
|
||||||
|
EXPECT_EQ(processedCount, 0);
|
||||||
|
|
||||||
|
// 验证:没有生成任何计划
|
||||||
|
const std::vector<TaskPlan>& plans = emptyEngine.getAllPlans();
|
||||||
|
EXPECT_TRUE(plans.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 测试混合状态事件的处理
|
||||||
|
*
|
||||||
|
* 验证 processPendingEvents 只处理 Pending 状态的事件,
|
||||||
|
* 忽略其他状态的事件。
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testProcessPendingEventsMixedStatus) {
|
||||||
|
// 添加更多不同状态的事件
|
||||||
|
EventRecord event4;
|
||||||
|
event4.id = "EVT-004";
|
||||||
|
event4.priority = 150;
|
||||||
|
event4.status = EventStatus::Pending;
|
||||||
|
event4.description = "Test event 4";
|
||||||
|
|
||||||
|
EventRecord event5;
|
||||||
|
event5.id = "EVT-005";
|
||||||
|
event5.priority = 180;
|
||||||
|
event5.status = EventStatus::Generated;
|
||||||
|
event5.description = "Test event 5";
|
||||||
|
|
||||||
|
engine->ingestEvent(event4);
|
||||||
|
engine->ingestEvent(event5);
|
||||||
|
|
||||||
|
// 执行:处理待处理事件
|
||||||
|
size_t processedCount = engine->processPendingEvents();
|
||||||
|
|
||||||
|
// 验证:只处理了 Pending 事件(EVT-001, EVT-002, EVT-004)
|
||||||
|
EXPECT_EQ(processedCount, 3);
|
||||||
|
|
||||||
|
// 验证:EVT-005 状态未改变
|
||||||
|
const EventRecord* event5Ptr = engine->findEventById("EVT-005");
|
||||||
|
ASSERT_NE(event5Ptr, nullptr);
|
||||||
|
EXPECT_EQ(event5Ptr->status, EventStatus::Generated);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 测试多次调用 processPendingEvents 的幂等性
|
||||||
|
*
|
||||||
|
* 验证多次调用 processPendingEvents 不会重复处理已处理的事件。
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testProcessPendingEventsIdempotent) {
|
||||||
|
// 第一次调用
|
||||||
|
size_t firstCount = engine->processPendingEvents();
|
||||||
|
EXPECT_EQ(firstCount, 2);
|
||||||
|
|
||||||
|
// 第二次调用
|
||||||
|
size_t secondCount = engine->processPendingEvents();
|
||||||
|
EXPECT_EQ(secondCount, 0);
|
||||||
|
|
||||||
|
// 第三次调用
|
||||||
|
size_t thirdCount = engine->processPendingEvents();
|
||||||
|
EXPECT_EQ(thirdCount, 0);
|
||||||
|
|
||||||
|
// 验证:计划数量保持不变
|
||||||
|
const std::vector<TaskPlan>& plans = engine->getAllPlans();
|
||||||
|
EXPECT_EQ(plans.size(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// findEventById 测试用例
|
||||||
|
// ============================================================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 测试查找已存在的事件
|
||||||
|
*
|
||||||
|
* 验证 findEventById 能正确返回已存在事件的指针。
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testFindEventByIdFound) {
|
||||||
|
// 执行:查找已存在的事件
|
||||||
|
const EventRecord* event = engine->findEventById("EVT-001");
|
||||||
|
|
||||||
|
// 验证:找到事件
|
||||||
|
ASSERT_NE(event, nullptr);
|
||||||
|
EXPECT_EQ(event->id, "EVT-001");
|
||||||
|
EXPECT_EQ(event->priority, 100);
|
||||||
|
EXPECT_EQ(event->status, EventStatus::Pending);
|
||||||
|
EXPECT_EQ(event->description, "Test event 1");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 测试查找不存在的事件
|
||||||
|
*
|
||||||
|
* 验证 findEventById 在查找不存在的事件时返回 nullptr。
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testFindEventByIdNotFound) {
|
||||||
|
// 执行:查找不存在的事件
|
||||||
|
const EventRecord* event = engine->findEventById("NONEXISTENT");
|
||||||
|
|
||||||
|
// 验证:未找到事件
|
||||||
|
EXPECT_EQ(event, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 测试查找空字符串 ID
|
||||||
|
*
|
||||||
|
* 验证 findEventById 在查找空字符串 ID 时返回 nullptr。
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testFindEventByIdEmptyId) {
|
||||||
|
// 执行:查找空字符串 ID
|
||||||
|
const EventRecord* event = engine->findEventById("");
|
||||||
|
|
||||||
|
// 验证:未找到事件
|
||||||
|
EXPECT_EQ(event, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 测试查找特殊字符 ID
|
||||||
|
*
|
||||||
|
* 验证 findEventById 能正确处理包含特殊字符的 ID。
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testFindEventByIdSpecialChars) {
|
||||||
|
// 添加包含特殊字符的事件
|
||||||
|
EventRecord specialEvent;
|
||||||
|
specialEvent.id = "EVT-@#$%";
|
||||||
|
specialEvent.priority = 50;
|
||||||
|
specialEvent.status = EventStatus::Pending;
|
||||||
|
specialEvent.description = "Special event";
|
||||||
|
engine->ingestEvent(specialEvent);
|
||||||
|
|
||||||
|
// 执行:查找特殊字符 ID
|
||||||
|
const EventRecord* event = engine->findEventById("EVT-@#$%");
|
||||||
|
|
||||||
|
// 验证:找到事件
|
||||||
|
ASSERT_NE(event, nullptr);
|
||||||
|
EXPECT_EQ(event->id, "EVT-@#$%");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 测试查找长字符串 ID
|
||||||
|
*
|
||||||
|
* 验证 findEventById 能正确处理超长 ID。
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testFindEventByIdLongId) {
|
||||||
|
// 添加超长 ID 的事件
|
||||||
|
std::string longId(1000, 'A');
|
||||||
|
EventRecord longEvent;
|
||||||
|
longEvent.id = longId;
|
||||||
|
longEvent.priority = 50;
|
||||||
|
longEvent.status = EventStatus::Pending;
|
||||||
|
longEvent.description = "Long ID event";
|
||||||
|
engine->ingestEvent(longEvent);
|
||||||
|
|
||||||
|
// 执行:查找超长 ID
|
||||||
|
const EventRecord* event = engine->findEventById(longId);
|
||||||
|
|
||||||
|
// 验证:找到事件
|
||||||
|
ASSERT_NE(event, nullptr);
|
||||||
|
EXPECT_EQ(event->id, longId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 测试查找事件后修改事件状态
|
||||||
|
*
|
||||||
|
* 验证通过 findEventById 返回的指针可以正确反映事件状态的变更。
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testFindEventByIdAfterStatusChange) {
|
||||||
|
// 先处理事件,改变状态
|
||||||
|
engine->processPendingEvents();
|
||||||
|
|
||||||
|
// 执行:查找已处理的事件
|
||||||
|
const EventRecord* event = engine->findEventById("EVT-001");
|
||||||
|
|
||||||
|
// 验证:事件状态已更新
|
||||||
|
ASSERT_NE(event, nullptr);
|
||||||
|
EXPECT_EQ(event->status, EventStatus::Generated);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 测试在空事件库中查找
|
||||||
|
*
|
||||||
|
* 验证在空事件库中调用 findEventById 返回 nullptr。
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testFindEventByIdEmptyEngine) {
|
||||||
|
// 创建新的空引擎
|
||||||
|
CmsEngine emptyEngine;
|
||||||
|
|
||||||
|
// 执行:在空引擎中查找
|
||||||
|
const EventRecord* event = emptyEngine.findEventById("EVT-001");
|
||||||
|
|
||||||
|
// 验证:未找到事件
|
||||||
|
EXPECT_EQ(event, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 测试查找多个事件中的特定事件
|
||||||
|
*
|
||||||
|
* 验证 findEventById 在多个事件中能正确找到目标事件。
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testFindEventByIdAmongMany) {
|
||||||
|
// 添加更多事件
|
||||||
|
for (int i = 0; i < 100; ++i) {
|
||||||
|
EventRecord event;
|
||||||
|
event.id = "EVT-" + std::to_string(1000 + i);
|
||||||
|
event.priority = i;
|
||||||
|
event.status = EventStatus::Pending;
|
||||||
|
event.description = "Bulk event " + std::to_string(i);
|
||||||
|
engine->ingestEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 执行:查找特定事件
|
||||||
|
const EventRecord* event = engine->findEventById("EVT-1050");
|
||||||
|
|
||||||
|
// 验证:找到事件
|
||||||
|
ASSERT_NE(event, nullptr);
|
||||||
|
EXPECT_EQ(event->id, "EVT-1050");
|
||||||
|
EXPECT_EQ(event->priority, 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief 测试查找事件后验证 const 正确性
|
||||||
|
*
|
||||||
|
* 验证 findEventById 返回的 const 指针不能修改事件内容。
|
||||||
|
*/
|
||||||
|
TEST_F(CmsEngineTest, testFindEventByIdConstCorrectness) {
|
||||||
|
// 执行:查找事件
|
||||||
|
const EventRecord* event = engine->findEventById("EVT-001");
|
||||||
|
|
||||||
|
// 验证:返回的是 const 指针
|
||||||
|
ASSERT_NE(event, nullptr);
|
||||||
|
// 以下代码如果取消注释,编译应该失败(const 正确性)
|
||||||
|
// event->status = EventStatus::Generated; // 编译错误
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue