mccu/include/mcutil/test.h

30 lines
1.3 KiB
C
Raw Normal View History

2025-01-29 21:21:18 +00:00
#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.
2025-02-04 13:03:27 +00:00
char** error_types;
2025-01-29 21:21:18 +00:00
} 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]; \
2025-02-04 13:03:27 +00:00
printf("(%s): ", test_case->short_name); \
2025-01-29 21:21:18 +00:00
int ret = (test_case->test_fn)(); \
2025-02-04 13:03:27 +00:00
if (ret) { \
printf("FAIL - Returned non zero (%d)\n", ret); \
printf("REASON: %s\n", test_case->error_types[ret]); \
2025-01-29 21:21:18 +00:00
exit(1); \
} else { \
2025-02-04 13:03:27 +00:00
printf("PASS\n"); \
2025-01-29 21:21:18 +00:00
} \
}
#endif // _H_MCU_TEST