AI 自动生成测试用例
This commit is contained in:
parent
9ceeaf09c6
commit
43652613fb
|
|
@ -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,274 @@
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <chrono>
|
||||||
|
#include "app.hpp"
|
||||||
|
|
||||||
|
// 辅助函数:创建一个有效的 EventRecord
|
||||||
|
EventRecord createValidEvent(const std::string& id = "EVT-001", uint32_t priority = 100) {
|
||||||
|
EventRecord event;
|
||||||
|
event.id = id;
|
||||||
|
event.priority = priority;
|
||||||
|
event.status = EventStatus::Pending;
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// 测试 ingestEvent 函数
|
||||||
|
// ============================================================
|
||||||
|
|
||||||
|
// 正常输入测试:有效的 EventRecord 应该被成功接入
|
||||||
|
TEST(CmsEngineTest, testIngestEventValidInput) {
|
||||||
|
CmsEngine engine;
|
||||||
|
EventRecord event = createValidEvent();
|
||||||
|
|
||||||
|
bool result = engine.ingestEvent(event);
|
||||||
|
|
||||||
|
EXPECT_TRUE(result);
|
||||||
|
// 验证事件已被存储
|
||||||
|
const EventRecord* storedEvent = engine.findEventById(event.id);
|
||||||
|
ASSERT_NE(storedEvent, nullptr);
|
||||||
|
EXPECT_EQ(storedEvent->id, event.id);
|
||||||
|
EXPECT_EQ(storedEvent->priority, event.priority);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 边界值测试:优先级为 0 的 EventRecord 应该被成功接入
|
||||||
|
TEST(CmsEngineTest, testIngestEventPriorityZero) {
|
||||||
|
CmsEngine engine;
|
||||||
|
EventRecord event = createValidEvent("EVT-002", 0);
|
||||||
|
|
||||||
|
bool result = engine.ingestEvent(event);
|
||||||
|
|
||||||
|
EXPECT_TRUE(result);
|
||||||
|
const EventRecord* storedEvent = engine.findEventById(event.id);
|
||||||
|
ASSERT_NE(storedEvent, nullptr);
|
||||||
|
EXPECT_EQ(storedEvent->priority, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 边界值测试:优先级为 255 的 EventRecord 应该被成功接入
|
||||||
|
TEST(CmsEngineTest, testIngestEventPriorityMax) {
|
||||||
|
CmsEngine engine;
|
||||||
|
EventRecord event = createValidEvent("EVT-003", 255);
|
||||||
|
|
||||||
|
bool result = engine.ingestEvent(event);
|
||||||
|
|
||||||
|
EXPECT_TRUE(result);
|
||||||
|
const EventRecord* storedEvent = engine.findEventById(event.id);
|
||||||
|
ASSERT_NE(storedEvent, nullptr);
|
||||||
|
EXPECT_EQ(storedEvent->priority, 255);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 异常输入测试:ID 为空的 EventRecord 应该被拒绝
|
||||||
|
TEST(CmsEngineTest, testIngestEventEmptyId) {
|
||||||
|
CmsEngine engine;
|
||||||
|
EventRecord event = createValidEvent("", 100);
|
||||||
|
|
||||||
|
bool result = engine.ingestEvent(event);
|
||||||
|
|
||||||
|
EXPECT_FALSE(result);
|
||||||
|
// 验证事件未被存储
|
||||||
|
const EventRecord* storedEvent = engine.findEventById("");
|
||||||
|
EXPECT_EQ(storedEvent, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 异常输入测试:优先级大于 255 的 EventRecord 应该被拒绝
|
||||||
|
TEST(CmsEngineTest, testIngestEventPriorityExceedsMax) {
|
||||||
|
CmsEngine engine;
|
||||||
|
EventRecord event = createValidEvent("EVT-004", 256);
|
||||||
|
|
||||||
|
bool result = engine.ingestEvent(event);
|
||||||
|
|
||||||
|
EXPECT_FALSE(result);
|
||||||
|
const EventRecord* storedEvent = engine.findEventById(event.id);
|
||||||
|
EXPECT_EQ(storedEvent, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 特殊场景测试:接入多个事件后验证所有事件都被正确存储
|
||||||
|
TEST(CmsEngineTest, testIngestEventMultipleEvents) {
|
||||||
|
CmsEngine engine;
|
||||||
|
|
||||||
|
EventRecord event1 = createValidEvent("EVT-001", 100);
|
||||||
|
EventRecord event2 = createValidEvent("EVT-002", 200);
|
||||||
|
EventRecord event3 = createValidEvent("EVT-003", 50);
|
||||||
|
|
||||||
|
EXPECT_TRUE(engine.ingestEvent(event1));
|
||||||
|
EXPECT_TRUE(engine.ingestEvent(event2));
|
||||||
|
EXPECT_TRUE(engine.ingestEvent(event3));
|
||||||
|
|
||||||
|
// 验证所有事件都被存储
|
||||||
|
EXPECT_NE(engine.findEventById("EVT-001"), nullptr);
|
||||||
|
EXPECT_NE(engine.findEventById("EVT-002"), nullptr);
|
||||||
|
EXPECT_NE(engine.findEventById("EVT-003"), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// 测试 processPendingEvents 函数
|
||||||
|
// ============================================================
|
||||||
|
|
||||||
|
// 正常输入测试:处理待处理事件
|
||||||
|
TEST(CmsEngineTest, testProcessPendingEventsValid) {
|
||||||
|
CmsEngine engine;
|
||||||
|
|
||||||
|
EventRecord event1 = createValidEvent("EVT-001", 100);
|
||||||
|
EventRecord event2 = createValidEvent("EVT-002", 200);
|
||||||
|
event2.status = EventStatus::Pending;
|
||||||
|
|
||||||
|
engine.ingestEvent(event1);
|
||||||
|
engine.ingestEvent(event2);
|
||||||
|
|
||||||
|
size_t processedCount = engine.processPendingEvents();
|
||||||
|
|
||||||
|
EXPECT_EQ(processedCount, 2);
|
||||||
|
|
||||||
|
// 验证事件状态已更新
|
||||||
|
const EventRecord* storedEvent1 = engine.findEventById("EVT-001");
|
||||||
|
ASSERT_NE(storedEvent1, nullptr);
|
||||||
|
EXPECT_EQ(storedEvent1->status, EventStatus::Generated);
|
||||||
|
|
||||||
|
const EventRecord* storedEvent2 = engine.findEventById("EVT-002");
|
||||||
|
ASSERT_NE(storedEvent2, nullptr);
|
||||||
|
EXPECT_EQ(storedEvent2->status, EventStatus::Generated);
|
||||||
|
|
||||||
|
// 验证生成了对应的 TaskPlan
|
||||||
|
const std::vector<TaskPlan>& plans = engine.getAllPlans();
|
||||||
|
EXPECT_EQ(plans.size(), 2);
|
||||||
|
EXPECT_EQ(plans[0].relatedEventId, "EVT-001");
|
||||||
|
EXPECT_EQ(plans[1].relatedEventId, "EVT-002");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 边界值测试:没有待处理事件时返回 0
|
||||||
|
TEST(CmsEngineTest, testProcessPendingEventsNoPending) {
|
||||||
|
CmsEngine engine;
|
||||||
|
|
||||||
|
EventRecord event1 = createValidEvent("EVT-001", 100);
|
||||||
|
event1.status = EventStatus::Generated;
|
||||||
|
EventRecord event2 = createValidEvent("EVT-002", 200);
|
||||||
|
event2.status = EventStatus::Generated;
|
||||||
|
|
||||||
|
engine.ingestEvent(event1);
|
||||||
|
engine.ingestEvent(event2);
|
||||||
|
|
||||||
|
size_t processedCount = engine.processPendingEvents();
|
||||||
|
|
||||||
|
EXPECT_EQ(processedCount, 0);
|
||||||
|
|
||||||
|
// 验证没有生成新的 TaskPlan
|
||||||
|
const std::vector<TaskPlan>& plans = engine.getAllPlans();
|
||||||
|
EXPECT_EQ(plans.size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 边界值测试:事件列表为空时返回 0
|
||||||
|
TEST(CmsEngineTest, testProcessPendingEventsEmptyEvents) {
|
||||||
|
CmsEngine engine;
|
||||||
|
|
||||||
|
size_t processedCount = engine.processPendingEvents();
|
||||||
|
|
||||||
|
EXPECT_EQ(processedCount, 0);
|
||||||
|
|
||||||
|
const std::vector<TaskPlan>& plans = engine.getAllPlans();
|
||||||
|
EXPECT_EQ(plans.size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 特殊场景测试:混合状态的事件处理
|
||||||
|
TEST(CmsEngineTest, testProcessPendingEventsMixedStatus) {
|
||||||
|
CmsEngine engine;
|
||||||
|
|
||||||
|
EventRecord event1 = createValidEvent("EVT-001", 100);
|
||||||
|
event1.status = EventStatus::Pending;
|
||||||
|
EventRecord event2 = createValidEvent("EVT-002", 200);
|
||||||
|
event2.status = EventStatus::Generated;
|
||||||
|
EventRecord event3 = createValidEvent("EVT-003", 50);
|
||||||
|
event3.status = EventStatus::Pending;
|
||||||
|
|
||||||
|
engine.ingestEvent(event1);
|
||||||
|
engine.ingestEvent(event2);
|
||||||
|
engine.ingestEvent(event3);
|
||||||
|
|
||||||
|
size_t processedCount = engine.processPendingEvents();
|
||||||
|
|
||||||
|
EXPECT_EQ(processedCount, 2);
|
||||||
|
|
||||||
|
// 验证只有 Pending 状态的事件被处理
|
||||||
|
const EventRecord* storedEvent1 = engine.findEventById("EVT-001");
|
||||||
|
ASSERT_NE(storedEvent1, nullptr);
|
||||||
|
EXPECT_EQ(storedEvent1->status, EventStatus::Generated);
|
||||||
|
|
||||||
|
const EventRecord* storedEvent2 = engine.findEventById("EVT-002");
|
||||||
|
ASSERT_NE(storedEvent2, nullptr);
|
||||||
|
EXPECT_EQ(storedEvent2->status, EventStatus::Generated);
|
||||||
|
|
||||||
|
const EventRecord* storedEvent3 = engine.findEventById("EVT-003");
|
||||||
|
ASSERT_NE(storedEvent3, nullptr);
|
||||||
|
EXPECT_EQ(storedEvent3->status, EventStatus::Generated);
|
||||||
|
|
||||||
|
// 验证生成了 2 个 TaskPlan
|
||||||
|
const std::vector<TaskPlan>& plans = engine.getAllPlans();
|
||||||
|
EXPECT_EQ(plans.size(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// 测试 findEventById 函数
|
||||||
|
// ============================================================
|
||||||
|
|
||||||
|
// 正常输入测试:查找已存在的事件
|
||||||
|
TEST(CmsEngineTest, testFindEventByIdExisting) {
|
||||||
|
CmsEngine engine;
|
||||||
|
EventRecord event = createValidEvent("EVT-001", 100);
|
||||||
|
engine.ingestEvent(event);
|
||||||
|
|
||||||
|
const EventRecord* foundEvent = engine.findEventById("EVT-001");
|
||||||
|
|
||||||
|
ASSERT_NE(foundEvent, nullptr);
|
||||||
|
EXPECT_EQ(foundEvent->id, "EVT-001");
|
||||||
|
EXPECT_EQ(foundEvent->priority, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 异常输入测试:查找不存在的事件返回 nullptr
|
||||||
|
TEST(CmsEngineTest, testFindEventByIdNonExistent) {
|
||||||
|
CmsEngine engine;
|
||||||
|
EventRecord event = createValidEvent("EVT-001", 100);
|
||||||
|
engine.ingestEvent(event);
|
||||||
|
|
||||||
|
const EventRecord* foundEvent = engine.findEventById("EVT-999");
|
||||||
|
|
||||||
|
EXPECT_EQ(foundEvent, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 边界值测试:查找空字符串 ID
|
||||||
|
TEST(CmsEngineTest, testFindEventByIdEmptyId) {
|
||||||
|
CmsEngine engine;
|
||||||
|
EventRecord event = createValidEvent("EVT-001", 100);
|
||||||
|
engine.ingestEvent(event);
|
||||||
|
|
||||||
|
const EventRecord* foundEvent = engine.findEventById("");
|
||||||
|
|
||||||
|
EXPECT_EQ(foundEvent, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 边界值测试:事件列表为空时查找
|
||||||
|
TEST(CmsEngineTest, testFindEventByIdEmptyEvents) {
|
||||||
|
CmsEngine engine;
|
||||||
|
|
||||||
|
const EventRecord* foundEvent = engine.findEventById("EVT-001");
|
||||||
|
|
||||||
|
EXPECT_EQ(foundEvent, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 特殊场景测试:查找多个事件中的特定事件
|
||||||
|
TEST(CmsEngineTest, testFindEventByIdMultipleEvents) {
|
||||||
|
CmsEngine engine;
|
||||||
|
|
||||||
|
EventRecord event1 = createValidEvent("EVT-001", 100);
|
||||||
|
EventRecord event2 = createValidEvent("EVT-002", 200);
|
||||||
|
EventRecord event3 = createValidEvent("EVT-003", 50);
|
||||||
|
|
||||||
|
engine.ingestEvent(event1);
|
||||||
|
engine.ingestEvent(event2);
|
||||||
|
engine.ingestEvent(event3);
|
||||||
|
|
||||||
|
const EventRecord* foundEvent = engine.findEventById("EVT-002");
|
||||||
|
|
||||||
|
ASSERT_NE(foundEvent, nullptr);
|
||||||
|
EXPECT_EQ(foundEvent->id, "EVT-002");
|
||||||
|
EXPECT_EQ(foundEvent->priority, 200);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue