chore(M0): 建立 CLion/CMake 工程骨架与测试框架
- CMake 多目标工程:shs_core 静态库 + smart_home 可执行 + shs_tests 测试 - CMakePresets.json:debug/release/asan/coverage 四个预设(debug 告警即错误) - 自研零依赖测试框架 tests/framework/test_framework.h + 运行器(套件过滤、CTest 集成) - 工具:tools/header_check.sh 头文件独立编译检查(接口冻结门) - 已实现公共层:errors / string_utils / num_utils,main 启动自检 - 实测:debug 预设 0 告警、ctest 3/3 通过(45 项断言)
This commit is contained in:
commit
578a770737
36 files changed
+2914
No files matched your search
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
* @file test_framework.c
|
||||
* @brief 自研轻量测试框架的实现
|
||||
*/
|
||||
#include "framework/test_framework.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* @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__)
|
||||
#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 */
|
||||
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* @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);
|
||||
|
||||
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, "项目二 数据类型、运算符与类型转换" },
|
||||
};
|
||||
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* @file test_num.c
|
||||
* @brief 单元测试:数据类型、运算符与类型转换(项目二)
|
||||
*/
|
||||
#include "framework/test_framework.h"
|
||||
#include "shs/num_utils.h"
|
||||
#include "shs/common.h"
|
||||
|
||||
void suite_unit_num(void)
|
||||
{
|
||||
/* ------------------------------------------------ 取值范围与截断 */
|
||||
TF_CASE("shs_int_clamp 边界");
|
||||
TF_CHECK_INT(shs_int_clamp(50, 0, 100), 50);
|
||||
TF_CHECK_INT(shs_int_clamp(-5, 0, 100), 0);
|
||||
TF_CHECK_INT(shs_int_clamp(120, 0, 100), 100);
|
||||
TF_CHECK_INT(shs_int_clamp(5, 10, 0), 5); /* 上下限颠倒也能容错 */
|
||||
|
||||
/* ------------------------------------------------ 浮点与整数转换 */
|
||||
TF_CASE("double -> int 的两种转换");
|
||||
TF_CHECK_INT(shs_round_to_int(23.6), 24);
|
||||
TF_CHECK_INT(shs_round_to_int(-23.6), -24);
|
||||
TF_CHECK_INT(shs_floor_to_int(23.9), 23); /* 强制转换=向零截断 */
|
||||
TF_CHECK_DBL(shs_double_clamp(120.0, -40.0, 80.0), 80.0, 1e-9);
|
||||
|
||||
/* ------------------------------------------------ 统计(数组 + 指针) */
|
||||
TF_CASE("数组统计:最值与平均值");
|
||||
const int samples[] = { 21, 25, 19, 30, 22 };
|
||||
const int count = SHS_ARRAY_LEN(samples);
|
||||
TF_CHECK_INT(shs_min_of(samples, count), 19);
|
||||
TF_CHECK_INT(shs_max_of(samples, count), 30);
|
||||
TF_CHECK_DBL(shs_average_of(samples, count), 23.4, 1e-9);
|
||||
TF_CHECK_INT(shs_min_of(NULL, 0), 0); /* 空集合兜底 */
|
||||
|
||||
/* ------------------------------------------------ 循环与递归 */
|
||||
TF_CASE("shs_pow_int(循环)与 shs_factorial(递归)");
|
||||
TF_CHECK_INT(shs_pow_int(2, 10), 1024);
|
||||
TF_CHECK_INT(shs_pow_int(10, 0), 1);
|
||||
TF_CHECK_INT(shs_pow_int(3, -1), 0); /* 负指数返回 0 */
|
||||
TF_CHECK_INT(shs_factorial(0), 1);
|
||||
TF_CHECK_INT(shs_factorial(6), 720);
|
||||
TF_CHECK_INT(shs_factorial(-1), -1); /* 非法输入 */
|
||||
|
||||
TF_CASE("shs_gcd(递归欧几里得)");
|
||||
TF_CHECK_INT(shs_gcd(48, 18), 6);
|
||||
TF_CHECK_INT(shs_gcd(17, 5), 1);
|
||||
TF_CHECK_INT(shs_gcd(-12, 18), 6);
|
||||
|
||||
/* ------------------------------------------------ 字符串转数值 */
|
||||
TF_CASE("shs_parse_int / shs_parse_double 的合法与非法分支");
|
||||
int iv = 0;
|
||||
TF_CHECK_ERR(shs_parse_int(" -128 ", &iv), SHS_OK);
|
||||
TF_CHECK_INT(iv, -128);
|
||||
TF_CHECK_ERR(shs_parse_int("12a", &iv), SHS_ERR_PARSE);
|
||||
TF_CHECK_ERR(shs_parse_int("", &iv), SHS_ERR_PARSE);
|
||||
TF_CHECK_ERR(shs_parse_int("99999999999", &iv), SHS_ERR_RANGE);
|
||||
|
||||
double dv = 0.0;
|
||||
TF_CHECK_ERR(shs_parse_double("26.5", &dv), SHS_OK);
|
||||
TF_CHECK_DBL(dv, 26.5, 1e-9);
|
||||
TF_CHECK_ERR(shs_parse_double("abc", &dv), SHS_ERR_PARSE);
|
||||
|
||||
bool bv = false;
|
||||
TF_CHECK_ERR(shs_parse_bool("ON", &bv), SHS_OK);
|
||||
TF_CHECK(bv);
|
||||
TF_CHECK_ERR(shs_parse_bool("否", &bv), SHS_OK);
|
||||
TF_CHECK(!bv);
|
||||
TF_CHECK_ERR(shs_parse_bool("maybe", &bv), SHS_ERR_PARSE);
|
||||
|
||||
/* ------------------------------------------------ 除零保护与百分比 */
|
||||
TF_CASE("shs_percent 除零保护");
|
||||
TF_CHECK_DBL(shs_percent(1, 4), 25.0, 1e-9);
|
||||
TF_CHECK_DBL(shs_percent(1, 0), 0.0, 1e-9);
|
||||
|
||||
/* ------------------------------------------------ 进制转换(位运算) */
|
||||
TF_CASE("shs_bytes_to_hex / shs_hex_to_bytes");
|
||||
const uint8_t frame[4] = { 0x5A, 0x01, 0x0A, 0xFF };
|
||||
char hex[16];
|
||||
TF_CHECK_ERR(shs_bytes_to_hex(frame, 4, hex, sizeof(hex)), SHS_OK);
|
||||
TF_CHECK_STR(hex, "5A010AFF");
|
||||
|
||||
uint8_t back[4] = { 0 };
|
||||
TF_CHECK_INT(shs_hex_to_bytes("5A010AFF", back, sizeof(back)), 4);
|
||||
TF_CHECK_INT(back[0], 0x5A);
|
||||
TF_CHECK_INT(back[3], 0xFF);
|
||||
TF_CHECK_INT(shs_hex_to_bytes("5A0", back, sizeof(back)), SHS_ERR_PARSE); /* 奇数长度 */
|
||||
|
||||
/* ------------------------------------------------ 哈希(位运算) */
|
||||
TF_CASE("shs_hash_string 稳定性");
|
||||
TF_CHECK_UINT(shs_hash_string("smart-home"), shs_hash_string("smart-home"));
|
||||
TF_CHECK(shs_hash_string("a") != shs_hash_string("b"));
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* @file test_string.c
|
||||
* @brief 单元测试:字符串与字符数组(项目五)
|
||||
*/
|
||||
#include "framework/test_framework.h"
|
||||
#include "shs/string_utils.h"
|
||||
|
||||
void suite_unit_string(void)
|
||||
{
|
||||
/* ------------------------------------------------ 长度与安全拷贝 */
|
||||
TF_CASE("shs_strlen / shs_strcpy");
|
||||
TF_CHECK_UINT(shs_strlen("smart home"), 10);
|
||||
TF_CHECK_UINT(shs_strlen(""), 0);
|
||||
TF_CHECK_UINT(shs_strlen(NULL), 0);
|
||||
|
||||
char buf[SHS_NAME_MAX];
|
||||
TF_CHECK_ERR(shs_strcpy(buf, sizeof(buf), "客厅主灯"), SHS_OK);
|
||||
TF_CHECK_STR(buf, "客厅主灯");
|
||||
TF_CHECK_ERR(shs_strcpy(buf, 4, "0123456789"), SHS_OK); /* 截断不溢出:只留 3 字节 + '\0' */
|
||||
TF_CHECK_STR(buf, "012");
|
||||
/* 中文是 UTF-8 多字节字符,长度按「字节」计算,缓冲区不足时必须截断而不是溢出 */
|
||||
TF_CHECK_UINT(shs_strlen("客厅主灯"), 12);
|
||||
TF_CHECK_ERR(shs_strcpy(buf, 8, "客厅主灯"), SHS_OK); /* 8 字节放不下 12 字节 */
|
||||
TF_CHECK_UINT(shs_strlen(buf), 7);
|
||||
TF_CHECK_ERR(shs_strcpy(NULL, 4, "x"), SHS_ERR_PARAM);
|
||||
|
||||
/* ------------------------------------------------ 连接 */
|
||||
TF_CASE("shs_strcat");
|
||||
char line[16];
|
||||
TF_CHECK_ERR(shs_strcpy(line, sizeof(line), "设备"), SHS_OK);
|
||||
TF_CHECK_ERR(shs_strcat(line, sizeof(line), "-03"), SHS_OK);
|
||||
TF_CHECK_STR(line, "设备-03");
|
||||
TF_CHECK_ERR(shs_strcat(line, 5, "溢出检查"), SHS_ERR_FULL);
|
||||
|
||||
/* ------------------------------------------------ 查找与包含 */
|
||||
TF_CASE("shs_str_find / shs_str_contains");
|
||||
TF_CHECK_NOT_NULL(shs_str_find("GET /api/devices HTTP/1.1", "/api/"));
|
||||
TF_CHECK_NULL(shs_str_find("GET /api/devices", "/admin"));
|
||||
TF_CHECK(shs_str_contains("温度超限报警", "报警"));
|
||||
TF_CHECK(!shs_str_contains("温度正常", "报警"));
|
||||
|
||||
/* ------------------------------------------------ 去空白与大小写 */
|
||||
TF_CASE("shs_str_trim / shs_str_to_upper");
|
||||
char trimmed[SHS_LINE_MAX];
|
||||
TF_CHECK_ERR(shs_strcpy(trimmed, sizeof(trimmed), " light-01 \t\n"), SHS_OK);
|
||||
TF_CHECK_ERR(shs_str_trim(trimmed), SHS_OK);
|
||||
TF_CHECK_STR(trimmed, "light-01");
|
||||
TF_CHECK_ERR(shs_str_to_upper(trimmed), SHS_OK);
|
||||
TF_CHECK_STR(trimmed, "LIGHT-01");
|
||||
TF_CHECK_ERR(shs_str_to_lower(trimmed), SHS_OK);
|
||||
TF_CHECK_STR(trimmed, "light-01");
|
||||
|
||||
/* ------------------------------------------------ 前缀后缀与大小写判等 */
|
||||
TF_CASE("shs_str_starts_with / ends_with / casecmp_equals");
|
||||
TF_CHECK(shs_str_starts_with("/api/devices", "/api"));
|
||||
TF_CHECK(!shs_str_starts_with("/other", "/api"));
|
||||
TF_CHECK(shs_str_ends_with("system.log", ".log"));
|
||||
TF_CHECK(!shs_str_ends_with("system.log", ".txt"));
|
||||
TF_CHECK(shs_strcasecmp_equals("True", "true"));
|
||||
TF_CHECK(!shs_strcasecmp_equals("True", "false"));
|
||||
|
||||
/* ------------------------------------------------ 转义(项目二 转义字符) */
|
||||
TF_CASE("shs_str_escape");
|
||||
char escaped[SHS_LINE_MAX];
|
||||
TF_CHECK(shs_str_escape("a\tb\nc", escaped, sizeof(escaped)) > 0);
|
||||
TF_CHECK_STR(escaped, "a\\tb\\nc");
|
||||
|
||||
/* ------------------------------------------------ 切分(字符数组 + 指针) */
|
||||
TF_CASE("shs_str_split");
|
||||
char workspace[SHS_LINE_MAX];
|
||||
char *items[8];
|
||||
int n = shs_str_split("id=3;name=light;state=on", ';', items, 8, workspace, sizeof(workspace));
|
||||
TF_CHECK_INT(n, 3);
|
||||
TF_CHECK_STR(items[0], "id=3");
|
||||
TF_CHECK_STR(items[2], "state=on");
|
||||
|
||||
/* ------------------------------------------------ 整数列表解析 */
|
||||
TF_CASE("shs_parse_int_list");
|
||||
int values[8];
|
||||
TF_CHECK_INT(shs_parse_int_list("1,2,30,400", ',', values, 8), 4);
|
||||
TF_CHECK_INT(values[2], 30);
|
||||
TF_CHECK_INT(values[3], 400);
|
||||
|
||||
/* ------------------------------------------------ 通配符(递归) */
|
||||
TF_CASE("shs_wildcard_match(递归调用)");
|
||||
TF_CHECK(shs_wildcard_match("/api/*", "/api/devices"));
|
||||
TF_CHECK(shs_wildcard_match("light-0?", "light-01"));
|
||||
TF_CHECK(!shs_wildcard_match("light-0?", "light-012"));
|
||||
TF_CHECK(shs_wildcard_match("*", "任意内容"));
|
||||
}
|
||||
Reference in new issue
Block a user