1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#ifndef UNIT_TEST_H
#define UNIT_TEST_H
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define ASSERT(statement) \
if (!(statement)) { \
sprintf(assert_fail, "line %d: assertion `%s' failed", __LINE__, #statement); \
return EXIT_FAILURE; \
}
#define CMD(foo) {#foo, foo},
#define APPROX_MARGIN (1e-100)
#define LENGTH(arr) (sizeof arr / sizeof *arr)
static char assert_fail[256];
typedef struct command command_t;
struct command {
const char *str;
int (*exe)(void);
};
bool approx(double x, double y) {
return ((x - y) <= APPROX_MARGIN &&
(x - y) >= -APPROX_MARGIN);
}
int run_tests(struct command *tests, int num_tests) {
int ret = EXIT_SUCCESS;
int n_passed = 0;
for (int i = 0; i < num_tests; ++ i) {
//fprintf(stderr, "%s\n", tests[i].str); // TESTING
if (tests[i].exe() == EXIT_FAILURE) {
fprintf(stderr, "%s\t\t%s\n", tests[i].str, assert_fail);
ret = EXIT_FAILURE;
} else {
++ n_passed;
}
}
// Print number of tests passed
fprintf(stderr, "%d/%d\n", n_passed, num_tests);
return ret;
}
#define RUN_TESTS(tests) run_tests(tests, sizeof tests / sizeof *tests);
#endif
|