Files
DataRouter/TEST_CASES.md
T

107 lines
4.9 KiB
Markdown
Raw 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.
# DataRouter 单元测试用例文档
## 测试环境
| 项目 | 内容 |
|------|------|
| 语言标准 | C11 |
| 编译器 | GCC 11.4.0 |
| 测试框架 | 自定义轻量框架(宏 + goto cleanup) |
| 构建系统 | CMake ≥ 3.10 |
| 发送能力 | `sensor` / `status` |
| 接收能力 | `rtcm` / `command` |
## 运行方式
```bash
cmake -S . -B build
cmake --build build
ctest --test-dir build
# 或直接运行:
./build/test_datarouter
```
## 测试用例列表
### 1. 未初始化(1 个)
| # | 测试名 | 描述 | 步骤 | 预期 |
|---|--------|------|------|------|
| 1 | `error_uninit` | 未 init 时发送 | 不调 init 直接 `send_sensor(0, ...)` | 返回 -1 |
### 2. 端口管理(4 个)
| # | 测试名 | 描述 | 步骤 | 预期 |
|---|--------|------|------|------|
| 2 | `port_create_basic` | 基本创建 | `port_create()` | 返回 ≥ 0 |
| 3 | `port_create_max` | 满容量(64) | 循环创建到满再尝试 | 64 个成功,第 65 个 INVALID |
| 4 | `port_delete_invalid` | 删除无效 ID | `delete(-1)` + `delete(999)` | 不崩溃 |
| 5 | `port_default_state` | 默认状态 | 创建端口,查 send/recv/handler | send_type=-1, recv_type=-1, count=0 |
### 3. send_type / recv_type 独立设置(6 个)
| # | 测试名 | 描述 | 步骤 | 预期 |
|---|--------|------|------|------|
| 6 | `send_type_set_get` | 设置发送类型 | `set_send_type(sensor)` → `get_send_type()` | 返回 sensor |
| 7 | `recv_type_set_get` | 设置接收类型 | `set_recv_type(rtcm)` → `get_recv_type()` | 返回 rtcm |
| 8 | `types_independent` | 收发互不干扰 | 同时 `set_send_type(sensor)` + `set_recv_type(command)` | get_send=sensor, get_recv=command |
| 9 | `send_type_clear` | 清除发送类型 | `set_send_type(sensor)` → `set_send_type(-1)` | `get_send_type()` == -1 |
| 10 | `recv_type_clear` | 清除接收类型 | `set_recv_type(rtcm)` → `set_recv_type(-1)` | `get_recv_type()` == -1 |
| 11 | `types_from_invalid` | 无效端口查类型 | `get_send_type(-1/999)` / `get_recv_type(-1/999)` | 均返回 -1 |
### 4. handler 增删(2 个)
| # | 测试名 | 描述 | 步骤 | 预期 |
|---|--------|------|------|------|
| 12 | `handler_add_remove` | 添加后移除 | ① add(a) add(b) count=2 ② remove(a) count=1 | count 正确 |
| 13 | `handler_clear` | 清除全部 | add 后 clear | count == 0 |
### 5. 发送触发(9 个)
| # | 测试名 | 描述 | 步骤 | 预期 |
|---|--------|------|------|------|
| 14 | `send_one_handler` | 单 handler 发送 | send_type + add_handler → send_sensor | 返回 0;handler 被调用 1 次 |
| 15 | `send_multi_handlers` | 三个 handler 广播 | 注册 3 个 handler 后 send | 返回 0;handler 被调用 3 次 |
| 16 | `send_type_mismatch` | 发送类型不匹配 | set_send_type(sensor) 但调用 send_status | 返回 -3;handler 不被调用 |
| 17 | `send_no_handler` | 未注册 handler | 只 set_send_type 不 add_handler | 返回 -4 |
| 18 | `send_no_send_type` | 没有 send_type | 端口只有 recv_type,调 send_sensor | 返回 -3 |
| 19 | `send_remove_handler` | 移除后少广播 | 注册 2 handler → send count=2 → remove 1 → send count=1 | 数量正确 |
| 20 | `send_clear_handlers` | 清空后无法发送 | add → clear → send | 返回 -4 |
| 21 | `send_zero_length` | 零长度数据 | send(port, "x", 0) | 返回 0;handler 被调用 |
| 22 | `send_null_data` | NULL 数据 | send(port, NULL, 0) | 返回 0;handler 被调用 |
### 6. 接收回调(1 个)
| # | 测试名 | 描述 | 步骤 | 预期 |
|---|--------|------|------|------|
| 23 | `recv_callbacks` | 用户实现接收回调 | ① `on_recv_rtcm(0, ...)` ② `on_recv_command(1, ...)` | 各自触发,互不干扰 |
### 7. 能力元数据(2 个)
| # | 测试名 | 描述 | 步骤 | 预期 |
|---|--------|------|------|------|
| 24 | `send_cap_meta` | 发送能力元数据 | 查 count / name / desc / 边界 | SEND 两组正确 |
| 25 | `recv_cap_meta` | 接收能力元数据 | 查 count / name / desc / 边界 | RECV 两组正确 |
### 8. 错误处理(2 个)
| # | 测试名 | 描述 | 步骤 | 预期 |
|---|--------|------|------|------|
| 26 | `error_invalid_port` | 无效端口 | `send_sensor(999/-1, ...)` | 均返回 -2 |
| 27 | `error_deleted_port` | 已删除端口 | 创建→设好→删除→send | 返回 -2 |
## 覆盖矩阵
| 功能模块 | 用例数 | 主要验证点 |
|----------|:------:|------------|
| 未初始化 | 1 | send → -1 |
| 端口管理 | 4 | 创建/满容量/删除异常/默认状态 |
| send_type / recv_type | 6 | 设置/清除/独立/无效端口 |
| handler 增删 | 2 | 添加/移除/清空 |
| 发送触发 | 9 | 单播/多播/类型不匹配/无handler/无send_type/移除后/清空后/零长/NULL |
| 接收回调 | 1 | 用户实现覆盖弱符号 |
| 能力元数据 | 2 | SEND 与 RECV 两组独立查询 |
| 错误处理 | 2 | 无效端口/已删除端口 |
**总计:27 个测试用例**