/* A simple unit testing framework for C * * This is a simple testing framework. * Look at test_this_framework.h for an example test definition. * * $Author: $ * $Revision: $ */ #ifndef _CUTER_H #define _CUTER_H #define _GNU_SOURCE #include #include /* Utilities exposed to the test definitions */ /* XXX: make this less dumb. Integrate better in reporting */ #ifndef CUTER_LOG_STREAM #define CUTER_LOG_STREAM stderr #endif #ifndef CUTER_LOG_ENABLED #define CUTER_LOG_ENABLED 1 #endif #define LOG(fmt, ...) { \ if (CUTER_LOG_ENABLED) \ fprintf(CUTER_LOG_STREAM, "%s:%d:%s:" fmt "\n", \ __FILE__, __LINE__, _name, ##__VA_ARGS__); \ } #define TEST_OK 0 #define TEST_FAIL 1 #define ASSERT(test, msg) { \ if (!(test)) { \ LOG("(%s): %s", #test, msg); \ return TEST_FAIL; \ } \ } /* Bookkeeping for test code generation */ struct cuter_list_head { struct cuter_list_head *next, *prev; }; struct test_t { const char *name; /* 0 == success */ signed int (*fn)(const char *_name); struct cuter_list_head node; }; #define START_TEST(test_name) \ static signed int test_name(const char *_name); \ static struct test_t _##test_name##_object = { name: #test_name, fn: &test_name }; \ void __attribute__((constructor)) __attribute__((visibility ("hidden"))) _register_##test_name(void) { \ cuter_register_test(&_##test_name##_object); \ } \ static signed int test_name(const char *_name) { #define END_TEST } #define STANDALONE \ int main(int argc, char **argv) { return cuter_driver(argc, argv); } void cuter_register_test(struct test_t *test); int cuter_driver(int argc, char **argv); #endif /* _CUTER_H */