Files
sambu 582b7d19bf test: 登记 unit.pointer 套件(414 行)并给测试框架补函数指针非空断言
- 新宏 TF_CHECK_FN_NOT_NULL:ISO C 禁止函数指针与对象指针互转,原 TF_CHECK_NOT_NULL
  在 -Wpedantic 下无法用于 HAL 后端函数指针断言;测试里 8 处改为新宏
- tests/test_runner.c 补 unit.pointer 表项(此前只加了声明,导致 ctest 报"未找到套件")
- ctest 9/9 通过
2026-09-14 20:30:13 +08:00

57 lines
3.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file test_framework.h
* @brief 自研轻量测试框架(零第三方依赖)
*
* 教材对应:《C语言程序设计项目化教程》项目十 —— 系统整体测试与调试。
* 设计目标:约 150 行、无外部依赖、断言失败能给出文件与行号,
* 既可服务本工程的单元/集成测试,也可作为学生阅读与仿写的范例。
*/
#ifndef SHS_TEST_FRAMEWORK_H
#define SHS_TEST_FRAMEWORK_H
#include <stdbool.h>
#include <stddef.h>
/** 测试套件函数签名:一个套件 = 一个函数 */
typedef void (*tf_suite_fn)(void);
/** 全局统计(定义在 test_framework.c) */
extern int tf_suites_run;
extern int tf_total_checks;
extern int tf_total_failures;
/** 开始/结束一个测试套件 */
void tf_begin(const char *suite_name);
int tf_end(void);
/** 断言原语:返回是否通过,并在失败时打印 file:line 与表达式 */
bool tf_check_bool(bool ok, const char *expr, const char *file, int line);
bool tf_check_int(long actual, long expected, const char *expr, const char *file, int line);
bool tf_check_uint(unsigned long actual, unsigned long expected, const char *expr, const char *file, int line);
bool tf_check_double(double actual, double expected, double eps, const char *expr, const char *file, int line);
bool tf_check_str(const char *actual, const char *expected, const char *expr, const char *file, int line);
bool tf_check_ptr_not_null(const void *ptr, const char *expr, const char *file, int line);
bool tf_check_ptr_null(const void *ptr, const char *expr, const char *file, int line);
/** 是否输出 ANSI 彩色(仅当标准输出是终端时) */
void tf_set_color(bool enabled);
/** 测试文件名与用例名,用于失败信息上下文 */
void tf_set_context(const char *file, const char *case_name);
/* ---------------------------------------------------------------- 断言宏 */
#define TF_CHECK(expr) tf_check_bool((expr) ? true : false, #expr, __FILE__, __LINE__)
#define TF_CHECK_INT(actual, expected) tf_check_int((long)(actual), (long)(expected), #actual, __FILE__, __LINE__)
#define TF_CHECK_UINT(actual, expected) tf_check_uint((unsigned long)(actual), (unsigned long)(expected), #actual, __FILE__, __LINE__)
#define TF_CHECK_DBL(actual, expected, eps) tf_check_double((double)(actual), (double)(expected), (double)(eps), #actual, __FILE__, __LINE__)
#define TF_CHECK_STR(actual, expected) tf_check_str((const char *)(actual), (const char *)(expected), #actual, __FILE__, __LINE__)
#define TF_CHECK_NOT_NULL(ptr) tf_check_ptr_not_null((const void *)(ptr), #ptr, __FILE__, __LINE__)
#define TF_CHECK_NULL(ptr) tf_check_ptr_null((const void *)(ptr), #ptr, __FILE__, __LINE__)
/* 函数指针专用:ISO C 禁止函数指针与对象指针互转(-Wpedantic),故另立一个宏(项目七 函数指针) */
#define TF_CHECK_FN_NOT_NULL(fn) tf_check_bool(((fn) != NULL), #fn " 非空(函数指针)", __FILE__, __LINE__)
#define TF_CHECK_ERR(actual, expected) tf_check_int((long)(actual), (long)(expected), #actual " /* shs_err_t */", __FILE__, __LINE__)
/** 用例分组标记:仅用于失败信息可读性 */
#define TF_CASE(name) tf_set_context(__FILE__, (name))
#endif /* SHS_TEST_FRAMEWORK_H */