You can use a self inclusion trick to get a list of function pointers:
#ifndef LIST_TESTS
#define TEST(name, ...) static int name() __VA_ARGS__
/* all includes go here */
#endif // ifndef LIST_TESTS
TEST(test1, {
/* some tests here */
return 0;
})
TEST(test2, {
/* some tests here */
return 0;
})
#undef TEST
#ifndef LIST_TESTS
int main(void) {
int (*tests[])() = {
#define LIST_TESTS
#define TEST(name, ...) name,
#include __FILE__
};
int num_tests = sizeof(tests) / sizeof(tests[0]);
int i;
for (i = 0; i < num_tests; ++i) {
/* stuff before test */
(tests[i])();
/* stuff after test */
}
return 0;
}
#endif // ifndef LIST_TESTS