5.5 KiB
5.5 KiB
DataRouter 单元测试用例文档
测试环境
| 项目 | 内容 |
|---|---|
| 语言标准 | C11 |
| 编译器 | GCC 11.4.0 |
| 测试框架 | 自定义轻量框架(宏 + goto cleanup) |
| 构建系统 | CMake ≥ 3.10 |
| 报文类型 | sensor / status / control 三个通道 |
运行方式
cmake -S . -B build
cmake --build build
ctest --test-dir build
# 或直接运行:
./build/test_datarouter
测试用例列表
1. 未初始化(1 个)
| # | 测试名 | 描述 | 步骤 | 预期 |
|---|---|---|---|---|
| 1 | error_uninit |
未初始化时不能发送 | ① 不调 init 直接 send_sensor(0, ...) |
返回 -1 |
2. 端口管理(7 个)
| # | 测试名 | 描述 | 步骤 | 预期 |
|---|---|---|---|---|
| 3 | port_create_basic |
基本创建 | ① datarouter_port_create() |
返回 ≥ 0 |
| 4 | port_create_multiple |
多端口唯一 ID | ① 连续创建 3 个端口 | ID 互不相同 |
| 5 | port_create_reuse |
删除后 ID 复用 | ① 创建 A ② 删除 A ③ 再创建 | 新 ID == A ID |
| 6 | port_delete_invalid |
删除无效 ID | ① delete(-1) ② delete(9999) |
不崩溃 |
| 7 | port_delete_twice |
重复删除 | ① 创建 ② 删除 ③ 再次删除 | 不崩溃 |
| 8 | port_default_state |
默认状态 | ① 创建端口 ② get_type() ③ get_handler() |
type == -1, handler == NULL |
| 9 | port_create_max |
满容量边界 | ① 循环创建 64 个 ② 再创建一个 | 64 个成功,第 65 个返回 INVALID |
3. 类型与 handler 设置获取(7 个)
| # | 测试名 | 描述 | 步骤 | 预期 |
|---|---|---|---|---|
| 10 | type_set_get |
设置并获取类型 | ① set_type(sensor) ② get_type() |
返回 sensor |
| 11 | type_clear |
清除类型 | ① set_type(sensor) ② set_type(-1) ③ get_type() |
返回 -1 |
| 12 | type_switch |
运行时切换类型 | ① set_type(sensor) ② set_type(status) ③ get_type() |
返回 status |
| 13 | type_from_invalid_port |
无效端口查询类型 | ① get_type(-1) ② get_type(999) |
均返回 -1 |
| 14 | handler_set_get |
设置并获取 handler | ① set_handler(f) ② get_handler() |
返回 f |
| 15 | handler_clear |
清除 handler | ① set_handler(f) ② set_handler(NULL) ③ get_handler() |
返回 NULL |
| 16 | handler_from_invalid_port |
无效端口查询 handler | ① get_handler(-1) ② get_handler(999) |
均返回 NULL |
4. 通道元数据查询(4 个)
| # | 测试名 | 描述 | 步骤 | 预期 |
|---|---|---|---|---|
| 17 | channel_count |
通道计数 | ① channel_count() |
3 |
| 18 | channel_names |
通道名称 | ① 依次 channel_name(0/1/2) |
"sensor" / "status" / "control" |
| 19 | channel_descs |
通道描述 | ① 依次 channel_desc(0/1/2) |
"Sensor data" / "Status report" / "Control command" |
| 20 | channel_invalid_id |
无效 ID | ① channel_name(-1/99) ② channel_desc(-1/99) |
均返回 NULL |
5. 发送触发(9 个)
| # | 测试名 | 描述 | 步骤 | 预期 |
|---|---|---|---|---|
| 21 | send_sensor |
sensor 类型发送 | ① 创建端口 ② set_type(sensor) + set_handler(test) ③ send_sensor(port, "hello", 5) |
返回 0;handler 被调用,参数正确 |
| 22 | send_status |
status 类型发送 | 同上,用 status 通道 | 返回 0 |
| 23 | send_control |
control 类型发送 | 同上,用 control 通道 | 返回 0 |
| 24 | send_type_mismatch |
类型不匹配 | ① set_type(sensor) ② send_status(port, ...) |
返回 -3;handler 不被调用 |
| 25 | send_no_handler |
未注册 handler | ① set_type(sensor) ② 不设 handler ③ send_sensor(port, ...) |
返回 -4 |
| 26 | send_same_type_multiple_ports |
同类型多端口 | ① 两端口均设 sensor + handler ② 依次发送 | 各调一次 handler |
| 27 | send_switch_handler_runtime |
运行时切换 handler | ① 注册 h1 发送 ② 改注册 h2 发送 | 第一次用 h1,第二次用 h2 |
| 28 | send_zero_length |
零长度数据 | ① 正常端口 ② send_sensor(port, "x", 0) |
返回 0;handler 被调用,len=0 |
| 29 | send_null_data |
NULL 数据 | ① 正常端口 ② send_sensor(port, NULL, 0) |
返回 0;handler 被调用 |
6. 接收回调(1 个)
| # | 测试名 | 描述 | 步骤 | 预期 |
|---|---|---|---|---|
| 30 | recv_callback_user_impl |
用户实现接收回调 | ① on_recv_sensor(port, data, len) ② on_recv_status(port, data, len) |
sensor 回调触发,status 不触发;再触发 status 回调,sensor 不变 |
7. 错误处理(2 个)
| # | 测试名 | 描述 | 步骤 | 预期 |
|---|---|---|---|---|
| 30 | error_invalid_port |
无效端口 ID | ① send_sensor(999, ...) ② send_sensor(-1, ...) |
均返回 -2 |
| 31 | error_deleted_port |
已删除端口 | ① 创建设好类型 + handler ② 删除 ③ send_sensor(原ID, ...) |
返回 -2 |
覆盖矩阵
| 功能模块 | 用例数 | 主要验证点 |
|---|---|---|
| 未初始化 | 1 | 未 init 调用 send → -1 |
| 端口生命周期 | 7 | 创建/多创建/ID复用/删除异常/满容量 |
| 类型与 handler | 7 | set/get/clear/switch/无效端口 |
| 通道元数据 | 4 | count/names/descs/boundary |
| 发送触发 | 9 | 三通道/类型不匹配/无handler/多端口/切换handler/零长/NULL |
| 接收回调 | 1 | 用户实现覆盖弱符号 |
| 错误处理 | 2 | 无效端口/已删除端口 |
总计:31 个测试用例