2026-08-29 00:53:40 +08:00
|
|
|
#include "c_Test.h"
|
|
|
|
|
#include "c_Compiler.h"
|
|
|
|
|
|
|
|
|
|
// Math/matrix.t.c
|
|
|
|
|
#include "c_Test.h"
|
|
|
|
|
|
2026-08-29 11:39:28 +08:00
|
|
|
// ==========================================
|
|
|
|
|
// 模拟业务环境与测试用例
|
|
|
|
|
// ==========================================
|
|
|
|
|
|
|
|
|
|
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");
|
2026-08-29 00:53:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-29 11:39:28 +08:00
|
|
|
static void teardown() {
|
|
|
|
|
free(file_mock_data);
|
|
|
|
|
file_mock_data = NULL;
|
|
|
|
|
printf(" " COLOR_CYAN "[INFO] Teardown: Memory released." COLOR_RESET "\n");
|
2026-08-29 00:53:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-29 11:39:28 +08:00
|
|
|
// --- 具体测试用例 ---
|
|
|
|
|
|
|
|
|
|
// 用例 1:需要内存环境
|
|
|
|
|
static void test_with_memory() {
|
|
|
|
|
ASSERT_INT_EQ(20, file_mock_data[1]);
|
2026-08-29 00:53:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-29 11:39:28 +08:00
|
|
|
// 用例 2:故意制造失败,检查内存是否能安全释放,且颜色是否正确
|
|
|
|
|
static void test_with_memory_failure() {
|
|
|
|
|
ASSERT_INT_EQ(99, file_mock_data[0]); // 这里会失败并 return 退出
|
|
|
|
|
}
|
2026-08-29 00:53:40 +08:00
|
|
|
|
2026-08-29 11:39:28 +08:00
|
|
|
// 用例 3:纯数学计算,完全不需要任何专属启动和关闭环境
|
|
|
|
|
static void test_pure_math() {
|
|
|
|
|
int result = 1 + 1;
|
|
|
|
|
ASSERT_INT_EQ(2, result);
|
|
|
|
|
}
|
2026-08-29 00:53:40 +08:00
|
|
|
|
2026-08-29 11:39:28 +08:00
|
|
|
|
|
|
|
|
// ==========================================
|
|
|
|
|
// 主程序入口
|
|
|
|
|
// ==========================================
|
|
|
|
|
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;
|
2026-08-29 00:53:40 +08:00
|
|
|
}
|