AI 自动生成测试用例

This commit is contained in:
lids 2026-04-14 11:15:04 +08:00
parent fa2a0dee39
commit 9e853bb24d
3 changed files with 286 additions and 0 deletions

90
tests/test_errors.cpp Normal file
View File

@ -0,0 +1,90 @@
#include "gtest/gtest.h"
#include "src/errors.cpp"
#include <iostream>
#include <csetjmp>
#include <csignal>
// 全局跳转点,用于处理信号
static std::jmp_buf jump_buffer;
// 信号处理函数
static void signal_handler(int sig) {
std::longjmp(jump_buffer, 1);
}
// 测试夹具类
class ErrorsTest : public ::testing::Test {
protected:
// 保存旧的信号处理器
struct sigaction old_action{};
struct sigaction new_action{};
void SetUp() override {
// 设置 SIGSEGV 和 SIGABRT 的信号处理器
new_action.sa_handler = signal_handler;
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = 0;
sigaction(SIGSEGV, &new_action, &old_action);
sigaction(SIGABRT, &new_action, nullptr);
}
void TearDown() override {
// 恢复旧的信号处理器
sigaction(SIGSEGV, &old_action, nullptr);
}
};
// 测试 test_null_pointer 函数
// 该函数会解引用空指针,预期会触发段错误 (SIGSEGV) 或程序终止
TEST_F(ErrorsTest, test_null_pointer_TriggersCrash) {
// 设置跳转点,如果发生信号则跳转回来
if (setjmp(jump_buffer) == 0) {
// 尝试调用会崩溃的函数
test_null_pointer();
// 如果执行到这里,说明没有崩溃,测试失败
FAIL() << "Expected test_null_pointer to cause a crash (SIGSEGV), but it didn't.";
} else {
// 成功捕获到信号,测试通过
SUCCEED();
}
}
// 测试 test_array_out_of_bounds 函数
// 该函数会访问数组越界,预期会触发段错误 (SIGSEGV) 或程序终止
TEST_F(ErrorsTest, test_array_out_of_bounds_TriggersCrash) {
if (setjmp(jump_buffer) == 0) {
test_array_out_of_bounds();
FAIL() << "Expected test_array_out_of_bounds to cause a crash (SIGSEGV), but it didn't.";
} else {
SUCCEED();
}
}
// 测试 test_uninitialized_var 函数
// 该函数使用未初始化的变量,行为是未定义的。
// 我们无法可靠地测试其输出,但可以验证函数能运行而不崩溃(尽管结果不可预测)。
// 注意:这是一个“正常”路径测试,因为函数定义本身没有语法错误,可以执行。
TEST_F(ErrorsTest, test_uninitialized_var_RunsWithoutCrash) {
// 由于使用未初始化变量是未定义行为,它可能崩溃也可能不崩溃。
// 我们只测试函数可以被调用而不导致必然的、可捕获的崩溃如SIGSEGV
// 我们使用一个 try-catch 块但C++标准异常通常捕获不了内存错误。
// 因此,我们设置一个信号处理器,如果它因未初始化变量而意外崩溃,我们也能捕获。
if (setjmp(jump_buffer) == 0) {
test_uninitialized_var();
// 如果执行到这里说明没有触发我们设置的信号处理器SIGSEGV/SIGABRT
// 这被认为是测试通过,因为函数执行完成了。
// 我们无法断言它的输出,因为 val 是未定义的。
SUCCEED();
} else {
// 如果它意外地因为未初始化变量而崩溃了,我们也记录下来。
// 这在某些平台/编译器上是有可能的。
ADD_FAILURE() << "test_uninitialized_var caused a signal. This is possible UB.";
}
}
// 主函数
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

101
tests/test_main.cpp Normal file
View File

@ -0,0 +1,101 @@
#include <gtest/gtest.h>
#include "test_errors.h"
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
// 测试主函数 main
TEST(MainTest, MainExecutesAllErrorFunctions) {
// 由于 main 函数调用了一系列会触发错误或副作用的函数,
// 直接测试其返回值可能不够。
// 策略:通过检查标准输出/错误或程序退出码来验证函数被调用。
// 注意:这依赖于 test_errors.h 中函数的实现(例如,它们可能打印信息)。
// 假设这些函数在出错时会打印特定信息到 stderr 或导致非零退出码(如果被包装)。
// 更健壮的方法是使用死亡测试death test来验证程序是否因特定错误而终止。
// 这里我们演示两种方法:
// 方法1直接调用 main 并检查其返回值如果函数设计为返回0
// 由于 main 函数总是返回 0这个测试价值有限但可以验证链接和基本执行。
// 为了隔离测试,我们通常不直接测试 main而是测试其调用的函数。
// 因此,这个测试用例主要作为集成测试的占位符。
// 在实际项目中main 函数的逻辑应尽可能简单,将测试重点放在其调用的函数上。
// 方法2使用死亡测试验证某些错误函数会导致程序终止例如空指针解引用
// 注意:这需要 test_errors.h 中的函数确实会导致程序崩溃(如 test_null_pointer
// 假设 test_null_pointer() 会解引用空指针导致段错误。
// 使用 ASSERT_DEATH 来验证。
// 注意:死亡测试在某些环境(如 Windows 或某些 sanitizers下可能不可靠。
// 这里我们注释掉,因为 test_errors.h 的实现未知,可能被修改为不崩溃。
/*
ASSERT_DEATH({
test_null_pointer();
}, ".*"); // 匹配任何错误输出
*/
// 由于上述不确定性,我们采用一个更安全的测试:
// 验证 main 函数可以正常编译、链接并执行到返回,没有抛出异常。
// 这通过调用 main 函数并检查其返回值应为0来实现。
// 注意:直接调用 main 函数在 C++ 标准中通常是允许的,但有些编译器可能警告。
// 我们使用 extern "C" 来避免名称修饰问题(如果 main 是 C++函数)。
// 实际上main 函数是全局函数,可以直接调用。
testing::internal::CaptureStdout(); // 捕获 stdout 以避免测试输出干扰
testing::internal::CaptureStderr(); // 捕获 stderr
int result = main(); // 调用 main 函数
std::string output = testing::internal::GetCapturedStdout();
std::string errors = testing::internal::GetCapturedStderr();
// 断言 main 返回 0
EXPECT_EQ(result, 0);
// 可选:断言没有标准错误输出(如果错误函数被设计为静默)。
// 或者断言有错误输出(如果错误函数打印了信息)。
// 这里我们不做具体断言,因为 test_errors.h 的实现未知。
// 可以输出以供调试
// std::cout << "stdout: " << output << std::endl;
// std::cout << "stderr: " << errors << std::endl;
}
// 边界条件测试:无参数 main 函数(本身就是无参数)
// 此测试用例强调 main 函数不接受参数,但我们的 main 函数确实如此。
// 没有实际的边界值需要测试,因为 main 函数签名固定。
// 异常输入测试:不适用,因为 main 函数无参数。
// 特殊场景测试:验证 main 函数在多次调用下的行为(如果需要)。
// 注意:多次调用 main 通常不是预期用法,但我们可以测试其幂等性(如果函数设计如此)。
TEST(MainTest, MainCanBeCalledMultipleTimes) {
// 第一次调用
testing::internal::CaptureStdout();
testing::internal::CaptureStderr();
int result1 = main();
std::string output1 = testing::internal::GetCapturedStdout();
std::string errors1 = testing::internal::GetCapturedStderr();
EXPECT_EQ(result1, 0);
// 第二次调用
testing::internal::CaptureStdout();
testing::internal::CaptureStderr();
int result2 = main();
std::string output2 = testing::internal::GetCapturedStdout();
std::string errors2 = testing::internal::GetCapturedStderr();
EXPECT_EQ(result2, 0);
// 可选:比较两次调用的输出是否一致(如果函数有确定输出)。
// 由于 test_errors.h 中的函数可能有副作用(如内存泄漏),输出可能不同。
// 这里我们不做断言。
}
// 集成测试:验证 main 函数调用的所有函数都被执行。
// 这可以通过 mock 或 spy 来实现,但这里我们假设 test_errors.h 中的函数
// 有可观察的副作用(如写入文件、全局变量)。
// 由于 test_errors.h 的实现未知,我们无法编写具体断言。
// 作为示例,我们假设 test_file_leak() 会创建一个临时文件。
// 我们可以检查文件是否被创建(如果知道文件名)。
// 这里我们跳过,因为需要知道实现细节。
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

95
tests/test_memory.cpp Normal file
View File

@ -0,0 +1,95 @@
#include "gtest/gtest.h"
#include "src/memory.cpp"
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <filesystem>
namespace fs = std::filesystem;
// 测试 test_memory_leak 函数
// 注意:内存泄漏测试通常需要借助外部工具(如 Valgrind、AddressSanitizer或自定义分配器来检测。
// 这里的测试主要验证函数能正常执行而不崩溃,并检查其副作用(如文件创建)。
// 对于内存泄漏,我们依赖外部工具或运行时检测。
TEST(MemoryTest, MemoryLeakFunctionRuns) {
// 测试函数是否能正常执行而不抛出异常或崩溃
EXPECT_NO_THROW(test_memory_leak());
// 该函数没有返回值或外部可观测的副作用,因此主要测试其可执行性
}
// 测试 test_double_free 函数
// 重复释放是未定义行为,可能导致程序崩溃。
// 我们期望测试能捕获到崩溃或异常,但具体行为取决于编译器和运行时环境。
// 在某些环境下(如使用 AddressSanitizer可能会抛出异常或终止程序。
// 这里我们使用 EXPECT_DEATH 来测试是否会导致程序终止(在支持的情况下)。
TEST(MemoryTest, DoubleFreeCausesTermination) {
// 注意EXPECT_DEATH 只在支持死亡测试的配置下工作(如使用 AddressSanitizer 或特定平台)。
// 如果死亡测试不可用,这个测试可能会被跳过或失败。
// 我们使用一个简单的死亡断言来检查程序是否因重复释放而终止。
// 正则表达式 ".*" 匹配任何死亡输出。
EXPECT_DEATH(test_double_free(), ".*");
}
// 测试 test_file_leak 函数
// 文件句柄泄漏测试:验证函数执行后文件是否被创建,但句柄未关闭。
// 我们无法直接测试句柄泄漏,但可以验证文件被创建。
// 注意:在函数执行后,文件 "test.txt" 应该存在于当前目录中。
TEST(MemoryTest, FileLeakCreatesFile) {
// 首先,如果文件已存在,删除它以确保测试的纯净性
std::string filename = "test.txt";
if (fs::exists(filename)) {
fs::remove(filename);
}
// 执行函数,应该创建文件
EXPECT_NO_THROW(test_file_leak());
// 验证文件是否被创建
EXPECT_TRUE(fs::exists(filename)) << "File should be created by test_file_leak";
// 可选:检查文件内容(虽然函数没有写入,但文件被打开为写入模式)
// 这里我们只检查存在性。
// 清理:删除测试文件,避免影响其他测试
if (fs::exists(filename)) {
fs::remove(filename);
}
}
// 边界和异常测试:由于这些函数没有参数,我们主要测试其行为。
// 对于 test_memory_leak 和 test_file_leak我们测试多次调用是否累积泄漏。
// 注意:这通常需要外部工具检测。
TEST(MemoryTest, MultipleMemoryLeakCalls) {
// 多次调用,验证不会崩溃
for (int i = 0; i < 5; ++i) {
EXPECT_NO_THROW(test_memory_leak());
}
}
TEST(MemoryTest, MultipleFileLeakCalls) {
std::string filename = "test.txt";
// 清理可能存在的旧文件
if (fs::exists(filename)) {
fs::remove(filename);
}
// 多次调用,每次都会重新打开文件(可能泄漏句柄)
for (int i = 0; i < 3; ++i) {
EXPECT_NO_THROW(test_file_leak());
EXPECT_TRUE(fs::exists(filename)) << "File should exist after call " << i;
}
// 清理
if (fs::exists(filename)) {
fs::remove(filename);
}
}
// 特殊场景:测试在内存不足时 test_memory_leak 的行为(如果可能)
// 这通常难以模拟,但我们可以使用自定义分配器或模拟器。
// 由于时间限制,这里不实现。
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}