#include #include "app.hpp" #include #include #include 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()); }