测试用例

This commit is contained in:
2026-08-29 11:39:28 +08:00
parent a0758766c5
commit 8fd65b0de0
19 changed files with 1723 additions and 432 deletions
+47 -24
View File
@@ -4,36 +4,59 @@
// Math/matrix.t.c
#include "c_Test.h"
void heavy_calculation(void) {
double sum = 0.0;
for (int i = 0; i < 5000000; ++i) {
sum += sin((double)i) * cos((double)i);
}
// 随便加一个没意义的断言防止被编译器完全优化掉
C_ASSERT(sum != 12345.6, "密集数学计算检验");
// ==========================================
// 模拟业务环境与测试用例
// ==========================================
int* file_mock_data = NULL;
// 环境 A 的启动与关闭(针对内存/文件操作)
static void setup() {
file_mock_data = (int*)malloc(sizeof(int) * 3);
file_mock_data[0] = 10; file_mock_data[1] = 20; file_mock_data[2] = 30;
printf(" " COLOR_CYAN "[INFO] Setup: Memory initialized." COLOR_RESET "\n");
}
// ------------------------------------------------------------------------------
// 🛠️ 步骤 A: 编写独立的测试用例函数 (C_TEST_CASE)
// ------------------------------------------------------------------------------
C_TEST_CASE(test_quick_operations) {
int a = 1, b = 1;
C_ASSERT_INT_EQ(a, b, "极速操作验证");
static void teardown() {
free(file_mock_data);
file_mock_data = NULL;
printf(" " COLOR_CYAN "[INFO] Teardown: Memory released." COLOR_RESET "\n");
}
C_TEST_CASE(test_heavy_workload) {
heavy_calculation();
// --- 具体测试用例 ---
// 用例 1:需要内存环境
static void test_with_memory() {
ASSERT_INT_EQ(20, file_mock_data[1]);
}
// ------------------------------------------------------------------------------
// 🚀 步骤 B: 在主执行块中注册并依次运行它们
// ------------------------------------------------------------------------------
int main(int argc, char** argv) {
C_TEST_SUITE_BEGIN(MathMatrixFunctionSuite)
// 用例 2:故意制造失败,检查内存是否能安全释放,且颜色是否正确
static void test_with_memory_failure() {
ASSERT_INT_EQ(99, file_mock_data[0]); // 这里会失败并 return 退出
}
C_RUN_TEST_CASE(test_quick_operations);
C_RUN_TEST_CASE(test_heavy_workload);
// 用例 3:纯数学计算,完全不需要任何专属启动和关闭环境
static void test_pure_math() {
int result = 1 + 1;
ASSERT_INT_EQ(2, result);
}
C_TEST_SUITE_END()
// ==========================================
// 主程序入口
// ==========================================
int main() {
TEST_START(Starting Unit Tests);
// 运行需要内存环境的用例
RUN_TEST_FIXTURE(test_with_memory, setup, teardown);
RUN_TEST_FIXTURE(test_with_memory_failure, setup, teardown);
// 运行普通无环境要求的用例
RUN_TEST(test_pure_math);
// 打印最终统计报告
TEST_REPORT();
RETURN_TEST_STATUS;
}