/* 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: $ */ #include #include #include #include static CUTER_LIST_HEAD(_tests); void cuter_register_test(struct test_t *test) { cuter_list_add(&(test->node), &_tests); } /* XXX: add sorting to the run all to ensure runs in lexically sorted order */ int cuter_driver(int argc, char **argv) { struct test_t *t = NULL; int ret = 0; unsigned long count = 0; if (argc > 1) { /* Run each test name specified */ if (!strcmp(argv[1], "-a")) { /* Run all */ cuter_list_for_each_entry(t, &(_tests), node) { const signed int result = t->fn(t->name); /* Output simple test report */ printf("[%ld] %s:%s\n", count, t->name, !result?"Passed":"Failed"); /* Track number of errors */ if (result != 0) ++ret; ++count; } } else if (!strcmp(argv[1], "-l")) { /* Bare bones list */ cuter_list_for_each_entry(t, &_tests, node) { printf("%s ", t->name); } } else { /* Run matching test */ cuter_list_for_each_entry(t, &_tests, node) { if (!strcmp(t->name, argv[1])) { const signed int result = t->fn(t->name); /* Output simple test report */ printf("[0] %s:%s\n", t->name, !result?"Passed":"Failed"); /* Track number of errors */ if (result != 0) ++ret; break; } } } } else { /* List all */ printf("Usage:\n%s [-a|-l|testname]\n\n", argv[0]); printf("Available tests:\n"); cuter_list_for_each_entry(t, &_tests, node) { printf("\t%s\n", t->name); } } return ret; }