/** * @file test_framework.c * @brief 自研轻量测试框架的实现 */ #include "framework/test_framework.h" #include #include #include int tf_suites_run = 0; int tf_total_checks = 0; int tf_total_failures = 0; static const char *g_suite_name = "(none)"; static int g_suite_checks = 0; static int g_suite_fails = 0; static bool g_color = false; static const char *g_case_file = ""; static const char *g_case_name = "(case)"; #define C_RED (g_color ? "\033[31m" : "") #define C_GREEN (g_color ? "\033[32m" : "") #define C_YELLOW (g_color ? "\033[33m" : "") #define C_RESET (g_color ? "\033[0m" : "") void tf_set_color(bool enabled) { g_color = enabled; } void tf_set_context(const char *file, const char *case_name) { g_case_file = (file != NULL) ? file : ""; g_case_name = (case_name != NULL) ? case_name : "(case)"; } void tf_begin(const char *suite_name) { g_suite_name = (suite_name != NULL) ? suite_name : "(unnamed)"; g_suite_checks = 0; g_suite_fails = 0; tf_suites_run++; printf("%s[RUN ]%s %s\n", C_YELLOW, C_RESET, g_suite_name); } int tf_end(void) { int failed = g_suite_fails; if (failed == 0) { printf("%s[PASS]%s %-28s 断言 %d 项全部通过\n", C_GREEN, C_RESET, g_suite_name, g_suite_checks); } else { printf("%s[FAIL]%s %-28s 断言 %d 项,失败 %d 项\n", C_RED, C_RESET, g_suite_name, g_suite_checks, failed); } tf_total_checks += g_suite_checks; tf_total_failures += failed; return failed; } static void tf_report_failure(const char *what, const char *expr, const char *file, int line) { fprintf(stderr, "%s[FAIL]%s %s | %s:%d | %s | %s: %s\n", C_RED, C_RESET, g_suite_name, file, line, g_case_name, what, expr); } bool tf_check_bool(bool ok, const char *expr, const char *file, int line) { g_suite_checks++; if (!ok) { g_suite_fails++; tf_report_failure("条件", expr, file, line); } return ok; } bool tf_check_int(long actual, long expected, const char *expr, const char *file, int line) { g_suite_checks++; if (actual != expected) { g_suite_fails++; fprintf(stderr, "%s[FAIL]%s %s | %s:%d | %s | 实际 %ld != 期望 %ld\n", C_RED, C_RESET, g_suite_name, file, line, expr, actual, expected); return false; } return true; } bool tf_check_uint(unsigned long actual, unsigned long expected, const char *expr, const char *file, int line) { g_suite_checks++; if (actual != expected) { g_suite_fails++; fprintf(stderr, "%s[FAIL]%s %s | %s:%d | %s | 实际 %lu != 期望 %lu\n", C_RED, C_RESET, g_suite_name, file, line, expr, actual, expected); return false; } return true; } bool tf_check_double(double actual, double expected, double eps, const char *expr, const char *file, int line) { g_suite_checks++; double diff = actual - expected; if (diff < 0.0) { diff = -diff; } if (diff > eps) { g_suite_fails++; fprintf(stderr, "%s[FAIL]%s %s | %s:%d | %s | 实际 %.6f != 期望 %.6f (容差 %.6f)\n", C_RED, C_RESET, g_suite_name, file, line, expr, actual, expected, eps); return false; } return true; } bool tf_check_str(const char *actual, const char *expected, const char *expr, const char *file, int line) { g_suite_checks++; bool ok; if (actual == NULL || expected == NULL) { ok = (actual == expected); } else { ok = (strcmp(actual, expected) == 0); } if (!ok) { g_suite_fails++; fprintf(stderr, "%s[FAIL]%s %s | %s:%d | %s | 实际 \"%s\" != 期望 \"%s\"\n", C_RED, C_RESET, g_suite_name, file, line, expr, actual ? actual : "(null)", expected ? expected : "(null)"); return false; } return true; } bool tf_check_ptr_not_null(const void *ptr, const char *expr, const char *file, int line) { g_suite_checks++; if (ptr == NULL) { g_suite_fails++; tf_report_failure("非空指针", expr, file, line); return false; } return true; } bool tf_check_ptr_null(const void *ptr, const char *expr, const char *file, int line) { g_suite_checks++; if (ptr != NULL) { g_suite_fails++; tf_report_failure("应为空指针", expr, file, line); return false; } return true; }