mccu/include/mcutil/test.h
2025-01-29 23:21:18 +02:00

31 lines
1.3 KiB
C

#ifndef _H_MCU_TEST
#define _H_MCU_TEST
#include <stddef.h>
typedef struct mcu_test_case_s {
const char* short_name;
int (*test_fn)(void);
// If `test_fn` returns non zero, it will get the (ret_val - 1) value of this array,
// if this is null it will show up as `(none)`, so return 1 for the first error type.
const char* error_types[];
} mcu_test_case_t;
#define RUN_TEST_CASES(cases) \
for (size_t i = 0; i < sizeof(cases)/sizeof(mcu_test_case_t); i++) { \
mcu_test_case_t* test_case = &(cases)[i]; \
int ret = (test_case->test_fn)(); \
if (!ret) { \
printf("(%s): FAIL - Returned non zero (%d)\n", \
test_case->short_name, ret); \
printf("(%s): REASON: %s\n", \
test_case->short_name, test_case->error_types[ret-1]); \
exit(1); \
} else { \
printf("(%s): FAIL\n", test_case->short_name); \
} \
}
#endif // _H_MCU_TEST