From 82995f29f24bd80725c47897c28aed4e7a8cb3ed Mon Sep 17 00:00:00 2001 From: lids <1713278948@qq.com> Date: Thu, 4 Jun 2026 10:48:43 +0800 Subject: [PATCH] =?UTF-8?q?AI=20=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/CMakeLists.txt | 25 +++ tests/test_app.cpp | 362 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 387 insertions(+) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/test_app.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..4cb68f5 --- /dev/null +++ b/tests/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/tests/test_app.cpp b/tests/test_app.cpp new file mode 100644 index 0000000..d509d19 --- /dev/null +++ b/tests/test_app.cpp @@ -0,0 +1,362 @@ +#include +#include "app.hpp" + +// ============================================================================ +// 测试夹具:CmsEngineTest +// ============================================================================ +class CmsEngineTest : public ::testing::Test { +protected: + void SetUp() override { + // 每个测试用例开始前重置引擎 + engine = new CmsEngine(); + } + + void TearDown() override { + delete engine; + } + + CmsEngine* engine; +}; + +// ============================================================================ +// ingestEvent 测试 +// ============================================================================ + +/** + * @brief 测试 ingestEvent 正常输入:有效事件应成功接入 + */ +TEST_F(CmsEngineTest, testIngestEventNormal) { + EventRecord event; + event.id = "EVT-001"; + event.priority = 100; + event.status = EventStatus::Pending; + + bool result = engine->ingestEvent(event); + EXPECT_TRUE(result); + + // 验证事件已存储 + const EventRecord* found = engine->findEventById("EVT-001"); + ASSERT_NE(found, nullptr); + EXPECT_EQ(found->id, "EVT-001"); + EXPECT_EQ(found->priority, 100); + EXPECT_EQ(found->status, EventStatus::Pending); +} + +/** + * @brief 测试 ingestEvent 空 ID:应返回 false + */ +TEST_F(CmsEngineTest, testIngestEventEmptyId) { + EventRecord event; + event.id = ""; + event.priority = 100; + + bool result = engine->ingestEvent(event); + EXPECT_FALSE(result); + + // 验证事件未被存储 + const EventRecord* found = engine->findEventById(""); + EXPECT_EQ(found, nullptr); +} + +/** + * @brief 测试 ingestEvent 优先级边界:优先级为 0 应成功 + */ +TEST_F(CmsEngineTest, testIngestEventPriorityMin) { + EventRecord event; + event.id = "EVT-002"; + event.priority = 0; + + bool result = engine->ingestEvent(event); + EXPECT_TRUE(result); + + const EventRecord* found = engine->findEventById("EVT-002"); + ASSERT_NE(found, nullptr); + EXPECT_EQ(found->priority, 0); +} + +/** + * @brief 测试 ingestEvent 优先级边界:优先级为 255 应成功 + */ +TEST_F(CmsEngineTest, testIngestEventPriorityMax) { + EventRecord event; + event.id = "EVT-003"; + event.priority = 255; + + bool result = engine->ingestEvent(event); + EXPECT_TRUE(result); + + const EventRecord* found = engine->findEventById("EVT-003"); + ASSERT_NE(found, nullptr); + EXPECT_EQ(found->priority, 255); +} + +/** + * @brief 测试 ingestEvent 优先级越界:优先级 > 255 应返回 false + */ +TEST_F(CmsEngineTest, testIngestEventPriorityOverflow) { + EventRecord event; + event.id = "EVT-004"; + event.priority = 256; + + bool result = engine->ingestEvent(event); + EXPECT_FALSE(result); + + const EventRecord* found = engine->findEventById("EVT-004"); + EXPECT_EQ(found, nullptr); +} + +/** + * @brief 测试 ingestEvent 多次接入:多个事件应全部成功 + */ +TEST_F(CmsEngineTest, testIngestEventMultiple) { + EventRecord event1; + event1.id = "EVT-005"; + event1.priority = 50; + + EventRecord event2; + event2.id = "EVT-006"; + event2.priority = 150; + + EXPECT_TRUE(engine->ingestEvent(event1)); + EXPECT_TRUE(engine->ingestEvent(event2)); + + const EventRecord* found1 = engine->findEventById("EVT-005"); + const EventRecord* found2 = engine->findEventById("EVT-006"); + ASSERT_NE(found1, nullptr); + ASSERT_NE(found2, nullptr); + EXPECT_EQ(found1->priority, 50); + EXPECT_EQ(found2->priority, 150); +} + +// ============================================================================ +// processPendingEvents 测试 +// ============================================================================ + +/** + * @brief 测试 processPendingEvents 正常处理:Pending 事件应被处理 + */ +TEST_F(CmsEngineTest, testProcessPendingEventsNormal) { + EventRecord event; + event.id = "EVT-010"; + event.priority = 100; + event.status = EventStatus::Pending; + engine->ingestEvent(event); + + size_t count = engine->processPendingEvents(); + EXPECT_EQ(count, 1); + + // 验证事件状态已更新 + const EventRecord* found = engine->findEventById("EVT-010"); + ASSERT_NE(found, nullptr); + EXPECT_EQ(found->status, EventStatus::Generated); + + // 验证生成了对应的 TaskPlan + const auto& plans = engine->getAllPlans(); + ASSERT_EQ(plans.size(), 1); + EXPECT_EQ(plans[0].relatedEventId, "EVT-010"); + EXPECT_EQ(plans[0].name, "AutoPlan-EVT-010"); + EXPECT_EQ(plans[0].type, PlanType::Centralized); + EXPECT_EQ(plans[0].status, PlanStatus::Drafting); + EXPECT_EQ(plans[0].resourceQuota, 0.5); + EXPECT_EQ(plans[0].constraints, "自动生成约束"); +} + +/** + * @brief 测试 processPendingEvents 无 Pending 事件:应返回 0 + */ +TEST_F(CmsEngineTest, testProcessPendingEventsNoPending) { + EventRecord event; + event.id = "EVT-011"; + event.priority = 100; + event.status = EventStatus::Generated; // 非 Pending 状态 + engine->ingestEvent(event); + + size_t count = engine->processPendingEvents(); + EXPECT_EQ(count, 0); + + // 验证事件状态未改变 + const EventRecord* found = engine->findEventById("EVT-011"); + ASSERT_NE(found, nullptr); + EXPECT_EQ(found->status, EventStatus::Generated); + + // 验证未生成 TaskPlan + const auto& plans = engine->getAllPlans(); + EXPECT_EQ(plans.size(), 0); +} + +/** + * @brief 测试 processPendingEvents 空事件库:应返回 0 + */ +TEST_F(CmsEngineTest, testProcessPendingEventsEmpty) { + size_t count = engine->processPendingEvents(); + EXPECT_EQ(count, 0); + + const auto& plans = engine->getAllPlans(); + EXPECT_EQ(plans.size(), 0); +} + +/** + * @brief 测试 processPendingEvents 多个 Pending 事件:应全部处理 + */ +TEST_F(CmsEngineTest, testProcessPendingEventsMultiple) { + EventRecord event1; + event1.id = "EVT-012"; + event1.priority = 100; + event1.status = EventStatus::Pending; + engine->ingestEvent(event1); + + EventRecord event2; + event2.id = "EVT-013"; + event2.priority = 200; + event2.status = EventStatus::Pending; + engine->ingestEvent(event2); + + EventRecord event3; + event3.id = "EVT-014"; + event3.priority = 50; + event3.status = EventStatus::Generated; // 非 Pending + engine->ingestEvent(event3); + + size_t count = engine->processPendingEvents(); + EXPECT_EQ(count, 2); + + // 验证事件状态 + const EventRecord* found1 = engine->findEventById("EVT-012"); + const EventRecord* found2 = engine->findEventById("EVT-013"); + const EventRecord* found3 = engine->findEventById("EVT-014"); + ASSERT_NE(found1, nullptr); + ASSERT_NE(found2, nullptr); + ASSERT_NE(found3, nullptr); + EXPECT_EQ(found1->status, EventStatus::Generated); + EXPECT_EQ(found2->status, EventStatus::Generated); + EXPECT_EQ(found3->status, EventStatus::Generated); // 未改变 + + // 验证生成了 2 个 TaskPlan + const auto& plans = engine->getAllPlans(); + ASSERT_EQ(plans.size(), 2); + EXPECT_EQ(plans[0].relatedEventId, "EVT-012"); + EXPECT_EQ(plans[1].relatedEventId, "EVT-013"); +} + +/** + * @brief 测试 processPendingEvents 生成 ID 自增:ID 应连续 + */ +TEST_F(CmsEngineTest, testProcessPendingEventsIdAutoIncrement) { + EventRecord event1; + event1.id = "EVT-015"; + event1.priority = 100; + event1.status = EventStatus::Pending; + engine->ingestEvent(event1); + + EventRecord event2; + event2.id = "EVT-016"; + event2.priority = 100; + event2.status = EventStatus::Pending; + engine->ingestEvent(event2); + + engine->processPendingEvents(); + + const auto& plans = engine->getAllPlans(); + ASSERT_EQ(plans.size(), 2); + // ID 格式为 "ID-xxxxx",自增 + EXPECT_EQ(plans[0].id, "ID-00001"); + EXPECT_EQ(plans[1].id, "ID-00002"); +} + +// ============================================================================ +// findEventById 测试 +// ============================================================================ + +/** + * @brief 测试 findEventById 正常查找:应返回正确的事件指针 + */ +TEST_F(CmsEngineTest, testFindEventByIdNormal) { + EventRecord event; + event.id = "EVT-020"; + event.priority = 100; + event.status = EventStatus::Pending; + engine->ingestEvent(event); + + const EventRecord* found = engine->findEventById("EVT-020"); + ASSERT_NE(found, nullptr); + EXPECT_EQ(found->id, "EVT-020"); + EXPECT_EQ(found->priority, 100); + EXPECT_EQ(found->status, EventStatus::Pending); +} + +/** + * @brief 测试 findEventById 查找不存在的 ID:应返回 nullptr + */ +TEST_F(CmsEngineTest, testFindEventByIdNotFound) { + EventRecord event; + event.id = "EVT-021"; + event.priority = 100; + engine->ingestEvent(event); + + const EventRecord* found = engine->findEventById("NONEXISTENT"); + EXPECT_EQ(found, nullptr); +} + +/** + * @brief 测试 findEventById 空字符串查找:应返回 nullptr + */ +TEST_F(CmsEngineTest, testFindEventByIdEmptyString) { + EventRecord event; + event.id = "EVT-022"; + event.priority = 100; + engine->ingestEvent(event); + + const EventRecord* found = engine->findEventById(""); + EXPECT_EQ(found, nullptr); +} + +/** + * @brief 测试 findEventById 空事件库:应返回 nullptr + */ +TEST_F(CmsEngineTest, testFindEventByIdEmptyStore) { + const EventRecord* found = engine->findEventById("ANY_ID"); + EXPECT_EQ(found, nullptr); +} + +/** + * @brief 测试 findEventById 多个事件中查找:应返回正确的事件 + */ +TEST_F(CmsEngineTest, testFindEventByIdMultiple) { + EventRecord event1; + event1.id = "EVT-023"; + event1.priority = 50; + engine->ingestEvent(event1); + + EventRecord event2; + event2.id = "EVT-024"; + event2.priority = 200; + engine->ingestEvent(event2); + + EventRecord event3; + event3.id = "EVT-025"; + event3.priority = 100; + engine->ingestEvent(event3); + + const EventRecord* found = engine->findEventById("EVT-024"); + ASSERT_NE(found, nullptr); + EXPECT_EQ(found->id, "EVT-024"); + EXPECT_EQ(found->priority, 200); +} + +/** + * @brief 测试 findEventById 查找已处理事件:应返回正确的事件 + */ +TEST_F(CmsEngineTest, testFindEventByIdProcessedEvent) { + EventRecord event; + event.id = "EVT-026"; + event.priority = 100; + event.status = EventStatus::Pending; + engine->ingestEvent(event); + + engine->processPendingEvents(); + + const EventRecord* found = engine->findEventById("EVT-026"); + ASSERT_NE(found, nullptr); + EXPECT_EQ(found->id, "EVT-026"); + EXPECT_EQ(found->status, EventStatus::Generated); +}