Compare commits
4 Commits
main
...
test_20260
| Author | SHA1 | Date |
|---|---|---|
|
|
1394fd5cf4 | |
|
|
ca86405b31 | |
|
|
6c09cb9292 | |
|
|
aa1d62a53b |
|
|
@ -1,3 +1,4 @@
|
||||||
|
#define _USE_MATH_DEFINES
|
||||||
#include "app.hpp"
|
#include "app.hpp"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
cmake_minimum_required(VERSION 3.10.0)
|
||||||
|
project(test_main)
|
||||||
|
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_directories(D:/workspace/googletest-demo/test_main)
|
||||||
|
#add_executable(demo max.cpp main.cpp)
|
||||||
|
|
||||||
|
# 调用 find_package,它会使用你提供的变量
|
||||||
|
find_package(GTest REQUIRED)
|
||||||
|
include(CTest)
|
||||||
|
enable_testing()
|
||||||
|
|
||||||
|
add_executable(test_main test_app.cpp ../src/app.cpp)
|
||||||
|
|
||||||
|
target_link_libraries(test_main gtest gmock gtest_main)
|
||||||
|
include(GoogleTest)
|
||||||
|
gtest_discover_tests(test_main)
|
||||||
|
|
@ -0,0 +1,120 @@
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include "app.hpp"
|
||||||
|
#include <chrono>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
|
using namespace fzkj;
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// Task::ToString 测试
|
||||||
|
// ============================================================
|
||||||
|
|
||||||
|
TEST(TaskToStringTest, testTaskToStringNormalInput) {
|
||||||
|
Task task;
|
||||||
|
task.task_id = 123;
|
||||||
|
task.source_event_id = 456;
|
||||||
|
task.description = "Test task description";
|
||||||
|
task.priority = "高";
|
||||||
|
task.criticality = "关键";
|
||||||
|
// 设置已知时间点 (UTC 2023-10-01 12:30:00)
|
||||||
|
std::tm tm = {};
|
||||||
|
tm.tm_year = 2023 - 1900;
|
||||||
|
tm.tm_mon = 10 - 1;
|
||||||
|
tm.tm_mday = 1;
|
||||||
|
tm.tm_hour = 12;
|
||||||
|
tm.tm_min = 30;
|
||||||
|
tm.tm_sec = 0;
|
||||||
|
tm.tm_isdst = 0;
|
||||||
|
std::time_t t = std::mktime(&tm);
|
||||||
|
// mktime 将本地时间转为 time_t,gmtime 再转回 tm,为了确保一致性,我们直接用 time_t 构造
|
||||||
|
// 注意:由于时区差异,这里使用系统时钟的 to_time_t 进行模拟
|
||||||
|
task.created_at = std::chrono::system_clock::from_time_t(t);
|
||||||
|
|
||||||
|
std::string result = task.ToString();
|
||||||
|
EXPECT_NE(result.find("Task{id=123"), std::string::npos);
|
||||||
|
EXPECT_NE(result.find("source_event=456"), std::string::npos);
|
||||||
|
EXPECT_NE(result.find("desc=\"Test task description\""), std::string::npos);
|
||||||
|
EXPECT_NE(result.find("priority=高"), std::string::npos);
|
||||||
|
EXPECT_NE(result.find("criticality=关键"), std::string::npos);
|
||||||
|
EXPECT_NE(result.find("created="), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TaskToStringTest, testTaskToStringBoundaryValues) {
|
||||||
|
Task task;
|
||||||
|
task.task_id = 0; // 最小 ID
|
||||||
|
task.source_event_id = 0;
|
||||||
|
task.description = ""; // 空字符串
|
||||||
|
task.priority = "";
|
||||||
|
task.criticality = "";
|
||||||
|
task.created_at = std::chrono::system_clock::from_time_t(0); // Epoch 时间
|
||||||
|
|
||||||
|
std::string result = task.ToString();
|
||||||
|
EXPECT_NE(result.find("Task{id=0"), std::string::npos);
|
||||||
|
EXPECT_NE(result.find("source_event=0"), std::string::npos);
|
||||||
|
EXPECT_NE(result.find("desc=\"\""), std::string::npos);
|
||||||
|
EXPECT_NE(result.find("priority="), std::string::npos);
|
||||||
|
EXPECT_NE(result.find("criticality="), std::string::npos);
|
||||||
|
// Epoch 时间在 UTC 下应为 1970-01-01 00:00:00
|
||||||
|
EXPECT_NE(result.find("1970-01-01 00:00:00"), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TaskToStringTest, testTaskToStringSpecialCharacters) {
|
||||||
|
Task task;
|
||||||
|
task.task_id = 999;
|
||||||
|
task.source_event_id = 888;
|
||||||
|
task.description = "Contains \"quotes\" and, commas"; // 包含引号和逗号
|
||||||
|
task.priority = "中";
|
||||||
|
task.criticality = "一般";
|
||||||
|
task.created_at = std::chrono::system_clock::now();
|
||||||
|
|
||||||
|
std::string result = task.ToString();
|
||||||
|
// 验证特殊字符被原样输出,未做转义
|
||||||
|
EXPECT_NE(result.find("desc=\"Contains \"quotes\" and, commas\""), std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// BattleSolution::ToString 测试
|
||||||
|
// ============================================================
|
||||||
|
|
||||||
|
TEST(BattleSolutionToStringTest, testBattleSolutionToStringNormalInput) {
|
||||||
|
BattleSolution sol;
|
||||||
|
sol.solution_id = 100;
|
||||||
|
sol.task_id = 10;
|
||||||
|
sol.name = "Solution Alpha";
|
||||||
|
sol.score = 95.5;
|
||||||
|
sol.is_distributed = true;
|
||||||
|
|
||||||
|
std::string result = sol.ToString();
|
||||||
|
std::ostringstream expected_ss;
|
||||||
|
expected_ss << "BattleSolution{id=100, task_id=10, name=\"Solution Alpha\", score=95.5, distributed=是}";
|
||||||
|
EXPECT_EQ(result, expected_ss.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BattleSolutionToStringTest, testBattleSolutionToStringBoundaryValues) {
|
||||||
|
BattleSolution sol;
|
||||||
|
sol.solution_id = 0; // 最小 ID
|
||||||
|
sol.task_id = 0;
|
||||||
|
sol.name = ""; // 空字符串
|
||||||
|
sol.score = 0.0; // 零分数
|
||||||
|
sol.is_distributed = false; // 未分发
|
||||||
|
|
||||||
|
std::string result = sol.ToString();
|
||||||
|
std::ostringstream expected_ss;
|
||||||
|
expected_ss << "BattleSolution{id=0, task_id=0, name=\"\", score=0, distributed=否}";
|
||||||
|
EXPECT_EQ(result, expected_ss.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BattleSolutionToStringTest, testBattleSolutionToStringSpecialScenario) {
|
||||||
|
BattleSolution sol;
|
||||||
|
sol.solution_id = 2;
|
||||||
|
sol.task_id = 1;
|
||||||
|
sol.name = "Bad Solution";
|
||||||
|
sol.score = -10.5; // 负数分数
|
||||||
|
sol.is_distributed = false;
|
||||||
|
|
||||||
|
std::string result = sol.ToString();
|
||||||
|
std::ostringstream expected_ss;
|
||||||
|
expected_ss << "BattleSolution{id=2, task_id=1, name=\"Bad Solution\", score=-10.5, distributed=否}";
|
||||||
|
EXPECT_EQ(result, expected_ss.str());
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue