Files
DataRouter/tests/test_datarouter.c
T

344 lines
12 KiB
C

/**
* @file test_datarouter.c
*/
#include "datarouter.h"
#include <stdio.h>
#include <string.h>
/* ================================================================
* 框架
* ================================================================ */
static int _total_run, _total_pass, _total_fail;
static int _err;
static const char *_cur_name;
#define FIXTURE_MAX 16
typedef struct {
datarouter_port_t ports[FIXTURE_MAX];
int count;
} fixture_t;
#define FIXTURE_INIT(f) memset((f), 0, sizeof(*(f)))
#define FIXTURE_PUSH(f, p) do { if ((f)->count < FIXTURE_MAX) (f)->ports[(f)->count++] = (p); } while (0)
#define FIXTURE_CLEAN(f) do { for (int _i = 0; _i < (f)->count; _i++) datarouter_port_delete((f)->ports[_i]); } while (0)
#define FIXTURE_CREATE(f) ({ datarouter_port_t _p = datarouter_port_create(); FIXTURE_PUSH(f, _p); _p; })
#define TEST_START(name) \
static void test_##name(void) \
{ \
fixture_t _fix; \
FIXTURE_INIT(&_fix); \
_cur_name = #name; \
_err = 0;
#define TEST_END() \
cleanup: \
FIXTURE_CLEAN(&_fix); \
if (_err) { _total_fail++; printf(" FAIL %s\n", _cur_name); } \
else { _total_pass++; printf(" PASS %s\n", _cur_name); } \
_total_run++; \
}
#define CHECK(cond, ...) do { \
if (!(cond)) { \
printf(" %s [line %d]: ", _cur_name, __LINE__); \
printf(__VA_ARGS__); \
printf("\n"); \
_err = 1; \
goto cleanup; \
} \
} while (0)
/* ================================================================
* 测试用 handler
* ================================================================ */
static int _h_cnt;
static int _h_port[16];
static int _h_pos;
static void _h(uint32_t p, const void *d, size_t l) { (void)d; (void)l; _h_cnt++; if (_h_pos<16) _h_port[_h_pos++]=p; }
static void _h2(uint32_t p, const void *d, size_t l) { (void)d; (void)l; _h_cnt++; if (_h_pos<16) _h_port[_h_pos++]=p; }
static void _h3(uint32_t p, const void *d, size_t l) { (void)d; (void)l; _h_cnt++; if (_h_pos<16) _h_port[_h_pos++]=p; }
static void reset_h(void) { _h_cnt = 0; _h_pos = 0; }
/* ================================================================
* 1. 未初始化
* ================================================================ */
TEST_START(error_uninit)
CHECK(datarouter_send_sensor("d", 1) == -1, "expected -1");
TEST_END()
/* ================================================================
* 2. 端口管理
* ================================================================ */
TEST_START(port_create_basic)
CHECK(FIXTURE_CREATE(&_fix) >= 0, NULL);
TEST_END()
TEST_START(port_create_max)
int c = 0;
for (int i = 0; i < 64; i++) {
datarouter_port_t p = datarouter_port_create();
if (p == DATAROUTER_INVALID_PORT) break;
FIXTURE_PUSH(&_fix, p); c++;
}
CHECK(c >= 64, "got %d", c);
CHECK(datarouter_port_create() == DATAROUTER_INVALID_PORT, NULL);
TEST_END()
TEST_START(port_delete_invalid)
datarouter_port_delete(-1); datarouter_port_delete(999);
TEST_END()
TEST_START(port_default_state)
datarouter_port_t p = FIXTURE_CREATE(&_fix);
CHECK(datarouter_port_get_send_type(p) == -1, NULL);
CHECK(datarouter_port_handler_count(p) == 0, NULL);
TEST_END()
/* ================================================================
* 3. send_type 与 handler
* ================================================================ */
TEST_START(send_type_set_get)
datarouter_port_t p = FIXTURE_CREATE(&_fix);
datarouter_port_set_send_type(p, DATAROUTER_SEND_sensor);
CHECK(datarouter_port_get_send_type(p) == DATAROUTER_SEND_sensor, NULL);
datarouter_port_set_send_type(p, -1);
CHECK(datarouter_port_get_send_type(p) == -1, NULL);
TEST_END()
TEST_START(handler_add_remove)
datarouter_port_t p = FIXTURE_CREATE(&_fix);
CHECK(datarouter_port_add_handler(p, _h) == 0, NULL);
CHECK(datarouter_port_handler_count(p) == 1, NULL);
CHECK(datarouter_port_remove_handler(p, _h) == 0, NULL);
CHECK(datarouter_port_handler_count(p) == 0, NULL);
TEST_END()
TEST_START(handler_clear)
datarouter_port_t p = FIXTURE_CREATE(&_fix);
datarouter_port_add_handler(p, _h);
datarouter_port_clear_handlers(p);
CHECK(datarouter_port_handler_count(p) == 0, NULL);
TEST_END()
/* ================================================================
* 4. 发送 — 自动广播
* ================================================================ */
TEST_START(send_no_match_port)
/* 没有端口配置该 send_type */
CHECK(datarouter_send_sensor("d", 1) == -2, "expected -2");
TEST_END()
TEST_START(send_one_port_one_handler)
datarouter_port_t p = FIXTURE_CREATE(&_fix);
datarouter_port_set_send_type(p, DATAROUTER_SEND_sensor);
datarouter_port_add_handler(p, _h);
reset_h();
CHECK(datarouter_send_sensor("d", 1) == 0, NULL);
CHECK(_h_cnt == 1, "cnt=%d", _h_cnt);
CHECK(_h_port[0] == (int)p, "port=%d", _h_port[0]);
TEST_END()
TEST_START(send_one_port_multi_handler)
datarouter_port_t p = FIXTURE_CREATE(&_fix);
datarouter_port_set_send_type(p, DATAROUTER_SEND_sensor);
datarouter_port_add_handler(p, _h);
datarouter_port_add_handler(p, _h2);
reset_h();
CHECK(datarouter_send_sensor("d", 1) == 0, NULL);
CHECK(_h_cnt == 2, "cnt=%d", _h_cnt);
TEST_END()
TEST_START(send_multi_ports_same_type)
/* 两个端口同类型,各一个 handler */
datarouter_port_t p1 = FIXTURE_CREATE(&_fix);
datarouter_port_t p2 = FIXTURE_CREATE(&_fix);
datarouter_port_set_send_type(p1, DATAROUTER_SEND_sensor);
datarouter_port_set_send_type(p2, DATAROUTER_SEND_sensor);
datarouter_port_add_handler(p1, _h);
datarouter_port_add_handler(p2, _h);
reset_h();
CHECK(datarouter_send_sensor("d", 1) == 0, NULL);
CHECK(_h_cnt == 2, "cnt=%d", _h_cnt);
/* p1 和 p2 各触发一次 */
CHECK((_h_port[0]==p1 && _h_port[1]==p2) ||
(_h_port[0]==p2 && _h_port[1]==p1), "ports %d,%d", _h_port[0], _h_port[1]);
TEST_END()
TEST_START(send_multi_port_multi_handler)
datarouter_port_t p1 = FIXTURE_CREATE(&_fix);
datarouter_port_t p2 = FIXTURE_CREATE(&_fix);
datarouter_port_set_send_type(p1, DATAROUTER_SEND_sensor);
datarouter_port_set_send_type(p2, DATAROUTER_SEND_sensor);
datarouter_port_add_handler(p1, _h);
datarouter_port_add_handler(p1, _h2); /* p1 两个 */
datarouter_port_add_handler(p2, _h3); /* p2 一个 */
reset_h();
CHECK(datarouter_send_sensor("d", 1) == 0, NULL);
CHECK(_h_cnt == 3, "cnt=%d", _h_cnt);
TEST_END()
TEST_START(send_type_filter)
/* 两个端口不同类型,只触发匹配的 */
datarouter_port_t p1 = FIXTURE_CREATE(&_fix);
datarouter_port_t p2 = FIXTURE_CREATE(&_fix);
datarouter_port_set_send_type(p1, DATAROUTER_SEND_sensor);
datarouter_port_set_send_type(p2, DATAROUTER_SEND_status);
datarouter_port_add_handler(p1, _h);
datarouter_port_add_handler(p2, _h);
reset_h();
CHECK(datarouter_send_sensor("d", 1) == 0, NULL);
CHECK(_h_cnt == 1, "cnt=%d", _h_cnt);
CHECK(_h_port[0] == (int)p1, "only p1 should fire");
TEST_END()
TEST_START(send_after_remove_handler)
datarouter_port_t p = FIXTURE_CREATE(&_fix);
datarouter_port_set_send_type(p, DATAROUTER_SEND_sensor);
datarouter_port_add_handler(p, _h);
reset_h(); datarouter_send_sensor("d", 1); CHECK(_h_cnt == 1, NULL);
datarouter_port_remove_handler(p, _h);
CHECK(datarouter_send_sensor("d", 1) == -2, "expected -2 after remove");
TEST_END()
TEST_START(send_after_delete_port)
datarouter_port_t p = FIXTURE_CREATE(&_fix);
datarouter_port_set_send_type(p, DATAROUTER_SEND_sensor);
datarouter_port_add_handler(p, _h);
datarouter_port_delete(p);
CHECK(datarouter_send_sensor("d", 1) == -2, "expected -2");
_fix.count--;
TEST_END()
TEST_START(send_zero_length)
datarouter_port_t p = FIXTURE_CREATE(&_fix);
datarouter_port_set_send_type(p, DATAROUTER_SEND_sensor);
datarouter_port_add_handler(p, _h);
reset_h();
CHECK(datarouter_send_sensor("x", 0) == 0, NULL);
CHECK(_h_cnt == 1, NULL);
TEST_END()
TEST_START(send_null_data)
datarouter_port_t p = FIXTURE_CREATE(&_fix);
datarouter_port_set_send_type(p, DATAROUTER_SEND_sensor);
datarouter_port_add_handler(p, _h);
reset_h();
CHECK(datarouter_send_sensor(NULL, 0) == 0, NULL);
CHECK(_h_cnt == 1, NULL);
TEST_END()
TEST_START(send_multi_cap)
/* 发送 status 不影响 sensor 端口 */
datarouter_port_t p1 = FIXTURE_CREATE(&_fix);
datarouter_port_t p2 = FIXTURE_CREATE(&_fix);
datarouter_port_set_send_type(p1, DATAROUTER_SEND_sensor);
datarouter_port_set_send_type(p2, DATAROUTER_SEND_status);
datarouter_port_add_handler(p1, _h);
datarouter_port_add_handler(p2, _h);
reset_h();
CHECK(datarouter_send_sensor("d", 1) == 0, NULL);
CHECK(_h_cnt == 1, "sensor: cnt=%d", _h_cnt);
CHECK(datarouter_send_status("d", 1) == 0, NULL);
CHECK(_h_cnt == 2, "status: cnt=%d", _h_cnt);
TEST_END()
/* ================================================================
* 5. 接收回调
* ================================================================ */
static int _rx_r;
void datarouter_on_recv_rtcm(uint32_t p, const void *d, size_t l)
{ (void)p; (void)d; (void)l; _rx_r++; }
TEST_START(recv_callback)
_rx_r = 0;
datarouter_on_recv_rtcm(0, "d", 1);
CHECK(_rx_r == 1, "cnt=%d", _rx_r);
TEST_END()
/* ================================================================
* 6. 能力元数据
* ================================================================ */
TEST_START(send_cap_meta)
CHECK(datarouter_send_cap_count() == 2, NULL);
CHECK(strcmp(datarouter_send_cap_name(0), "sensor") == 0, NULL);
CHECK(strcmp(datarouter_send_cap_desc(0), "Sensor data report") == 0, NULL);
CHECK(datarouter_send_cap_name(-1) == NULL, NULL);
CHECK(datarouter_send_cap_name(99) == NULL, NULL);
TEST_END()
TEST_START(recv_cap_meta)
CHECK(datarouter_recv_cap_count() == 2, NULL);
CHECK(strcmp(datarouter_recv_cap_name(0), "rtcm") == 0, NULL);
CHECK(strcmp(datarouter_recv_cap_desc(0), "RTCM correction data") == 0, NULL);
CHECK(datarouter_recv_cap_name(-1) == NULL, NULL);
CHECK(datarouter_recv_cap_desc(99) == NULL, NULL);
TEST_END()
/* ================================================================
* 主函数
* ================================================================ */
#define RUN(name) do { reset_h(); _rx_r = 0; test_##name(); } while (0)
int main(void)
{
printf("==== DataRouter Unit Tests ====\n\n");
printf("[uninitialized]\n");
RUN(error_uninit);
datarouter_init();
printf("\n[port management]\n");
RUN(port_create_basic);
RUN(port_create_max);
RUN(port_delete_invalid);
RUN(port_default_state);
printf("\n");
datarouter_reset(); datarouter_init();
printf("[send_type & handler]\n");
RUN(send_type_set_get);
RUN(handler_add_remove);
RUN(handler_clear);
printf("\n");
printf("[send — auto broadcast]\n");
RUN(send_no_match_port);
RUN(send_one_port_one_handler);
RUN(send_one_port_multi_handler);
RUN(send_multi_ports_same_type);
RUN(send_multi_port_multi_handler);
RUN(send_type_filter);
RUN(send_after_remove_handler);
RUN(send_after_delete_port);
RUN(send_zero_length);
RUN(send_null_data);
RUN(send_multi_cap);
printf("\n");
printf("[receive callback]\n");
RUN(recv_callback);
printf("\n");
printf("[capability metadata]\n");
RUN(send_cap_meta);
RUN(recv_cap_meta);
printf("\n");
printf("==============================\n");
printf(" total: %d | pass: %d | fail: %d\n\n",
_total_run, _total_pass, _total_fail);
return _total_fail > 0 ? 1 : 0;
}