mccu/include/mcutil/test.h
2025-02-04 15:32:09 +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.
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]; \
const char* sn = test_case->short_name; \
printf("Running test case: %s\n", test_case->short_name); \
int ret = (test_case->test_fn)(); \
if (ret) { \
printf("(%s) FAIL - Returned non zero (%d)\n", sn, ret); \
printf("(%s) REASON: %s\n", sn, test_case->error_types[ret]); \
exit(1); \
} else { \
printf("(%s) PASS\n", sn); \
} \
}
#endif // _H_MCU_TEST