- 新宏 TF_CHECK_FN_NOT_NULL:ISO C 禁止函数指针与对象指针互转,原 TF_CHECK_NOT_NULL 在 -Wpedantic 下无法用于 HAL 后端函数指针断言;测试里 8 处改为新宏 - tests/test_runner.c 补 unit.pointer 表项(此前只加了声明,导致 ctest 报"未找到套件") - ctest 9/9 通过
112 lines
3.4 KiB
C
112 lines
3.4 KiB
C
/**
|
|
* @file test_runner.c
|
|
* @brief 测试入口:按套件名选择运行,并与 CTest 集成
|
|
*
|
|
* 用法:
|
|
* ./shs_tests # 运行全部套件
|
|
* ./shs_tests --suite unit.string
|
|
* ./shs_tests --list # 列出全部套件
|
|
* ./shs_tests --no-color
|
|
*/
|
|
#include "framework/test_framework.h"
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
/* 各测试套件的声明(新增套件时在此与 g_suites 表中登记) */
|
|
void suite_unit_string(void);
|
|
void suite_unit_num(void);
|
|
void suite_unit_branch(void);
|
|
void suite_unit_loop(void);
|
|
void suite_unit_bitops(void);
|
|
void suite_unit_errors(void);
|
|
void suite_unit_file(void);
|
|
void suite_unit_pointer(void);
|
|
|
|
typedef struct {
|
|
const char *name; /* 套件名,与 CTest 中注册的名字一致 */
|
|
tf_suite_fn fn;
|
|
const char *desc; /* 教材知识点说明 */
|
|
} test_suite_t;
|
|
|
|
static const test_suite_t g_suites[] = {
|
|
{ "unit.string", suite_unit_string, "项目五 字符数组与字符串处理" },
|
|
{ "unit.num", suite_unit_num, "项目二 数据类型、运算符与类型转换" },
|
|
{ "unit.branch", suite_unit_branch, "项目三 分支结构与条件判断" },
|
|
{ "unit.loop", suite_unit_loop, "项目四 循环结构与嵌套" },
|
|
{ "unit.bitops", suite_unit_bitops, "项目九 位运算与位标志" },
|
|
{ "unit.errors", suite_unit_errors, "项目十 错误处理与错误码" },
|
|
{ "unit.file", suite_unit_file, "项目九 文件与配置存储" },
|
|
{ "unit.pointer", suite_unit_pointer, "项目七 指针、出参与函数指针" }
|
|
};
|
|
|
|
#define SUITE_COUNT ((int)(sizeof(g_suites) / sizeof(g_suites[0])))
|
|
|
|
static bool is_tty_stdout(void)
|
|
{
|
|
#ifdef _WIN32
|
|
return false;
|
|
#else
|
|
return true;
|
|
#endif
|
|
}
|
|
|
|
static void run_suite(const test_suite_t *suite)
|
|
{
|
|
printf("\n=== 套件 %s (%s) ===\n", suite->name, suite->desc);
|
|
tf_begin(suite->name);
|
|
suite->fn();
|
|
(void)tf_end();
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
const char *only = NULL;
|
|
bool list_only = false;
|
|
bool use_color = is_tty_stdout();
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
if (strcmp(argv[i], "--suite") == 0 && i + 1 < argc) {
|
|
only = argv[++i];
|
|
} else if (strcmp(argv[i], "--list") == 0) {
|
|
list_only = true;
|
|
} else if (strcmp(argv[i], "--no-color") == 0) {
|
|
use_color = false;
|
|
} else {
|
|
fprintf(stderr, "未知参数: %s\n", argv[i]);
|
|
return 2;
|
|
}
|
|
}
|
|
tf_set_color(use_color);
|
|
|
|
if (list_only) {
|
|
for (int i = 0; i < SUITE_COUNT; i++) {
|
|
printf("%s\t%s\n", g_suites[i].name, g_suites[i].desc);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int matched = 0;
|
|
for (int i = 0; i < SUITE_COUNT; i++) {
|
|
if (only != NULL && strcmp(only, g_suites[i].name) != 0) {
|
|
continue;
|
|
}
|
|
run_suite(&g_suites[i]);
|
|
matched++;
|
|
}
|
|
|
|
if (only != NULL && matched == 0) {
|
|
fprintf(stderr, "未找到套件: %s\n", only);
|
|
return 2;
|
|
}
|
|
|
|
printf("\n---------------- 测试汇总 ----------------\n");
|
|
printf("套件数 : %d\n", tf_suites_run);
|
|
printf("断言数 : %d\n", tf_total_checks);
|
|
printf("失败数 : %d\n", tf_total_failures);
|
|
printf("结果 : %s\n", (tf_total_failures == 0) ? "全部通过" : "存在失败");
|
|
printf("------------------------------------------\n");
|
|
return (tf_total_failures == 0) ? 0 : 1;
|
|
}
|