AI 自动生成测试用例
This commit is contained in:
parent
fa2a0dee39
commit
2b5c7bfcea
|
|
@ -0,0 +1,93 @@
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "src/utils.cpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
// 测试 unused_function 函数
|
||||||
|
// 注意:该函数是静态函数,无法从外部直接调用
|
||||||
|
// 因此无法为其编写直接的单元测试
|
||||||
|
// 但我们可以验证它不会影响其他函数的行为
|
||||||
|
|
||||||
|
// 测试 test_unused_code 函数
|
||||||
|
class TestUnusedCodeTest : public ::testing::Test {
|
||||||
|
protected:
|
||||||
|
void SetUp() override {
|
||||||
|
// 重定向 cout 到 stringstream,以便捕获输出
|
||||||
|
originalCoutBuffer = std::cout.rdbuf();
|
||||||
|
std::cout.rdbuf(testOutput.rdbuf());
|
||||||
|
}
|
||||||
|
|
||||||
|
void TearDown() override {
|
||||||
|
// 恢复 cout 的原始缓冲区
|
||||||
|
std::cout.rdbuf(originalCoutBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::stringstream testOutput;
|
||||||
|
std::streambuf* originalCoutBuffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 正常输入测试:验证函数正常执行且输出正确
|
||||||
|
TEST_F(TestUnusedCodeTest, NormalExecution) {
|
||||||
|
// 调用函数
|
||||||
|
test_unused_code();
|
||||||
|
|
||||||
|
// 验证输出
|
||||||
|
std::string output = testOutput.str();
|
||||||
|
// 移除可能的换行符
|
||||||
|
if (!output.empty() && output.back() == '\n') {
|
||||||
|
output.pop_back();
|
||||||
|
}
|
||||||
|
|
||||||
|
EXPECT_EQ(output, "200");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 边界值测试:验证函数在多次调用后仍然正常工作
|
||||||
|
TEST_F(TestUnusedCodeTest, MultipleCalls) {
|
||||||
|
// 第一次调用
|
||||||
|
test_unused_code();
|
||||||
|
std::string output1 = testOutput.str();
|
||||||
|
|
||||||
|
// 清空输出缓冲区
|
||||||
|
testOutput.str("");
|
||||||
|
testOutput.clear();
|
||||||
|
|
||||||
|
// 第二次调用
|
||||||
|
test_unused_code();
|
||||||
|
std::string output2 = testOutput.str();
|
||||||
|
|
||||||
|
// 移除可能的换行符
|
||||||
|
if (!output1.empty() && output1.back() == '\n') {
|
||||||
|
output1.pop_back();
|
||||||
|
}
|
||||||
|
if (!output2.empty() && output2.back() == '\n') {
|
||||||
|
output2.pop_back();
|
||||||
|
}
|
||||||
|
|
||||||
|
EXPECT_EQ(output1, "200");
|
||||||
|
EXPECT_EQ(output2, "200");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 特殊场景测试:验证函数不会抛出异常
|
||||||
|
TEST_F(TestUnusedCodeTest, NoExceptionThrown) {
|
||||||
|
// 验证函数调用不会抛出任何异常
|
||||||
|
EXPECT_NO_THROW(test_unused_code());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 特殊场景测试:验证函数执行后程序状态正常
|
||||||
|
TEST_F(TestUnusedCodeTest, ProgramStateUnaffected) {
|
||||||
|
// 记录调用前的输出流状态
|
||||||
|
auto beforeState = std::cout.rdstate();
|
||||||
|
|
||||||
|
// 调用函数
|
||||||
|
test_unused_code();
|
||||||
|
|
||||||
|
// 验证输出流状态未改变(除了正常的输出操作)
|
||||||
|
auto afterState = std::cout.rdstate();
|
||||||
|
EXPECT_EQ(beforeState, afterState);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 主函数
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
|
return RUN_ALL_TESTS();
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue