Sat, 18 Feb 2012 15:50:43 +0100
added test framework and foreach macro
test/main.c | file | annotate | diff | comparison | revisions | |
ucx/Makefile | file | annotate | diff | comparison | revisions | |
ucx/list.h | file | annotate | diff | comparison | revisions | |
ucx/test.c | file | annotate | diff | comparison | revisions | |
ucx/test.h | file | annotate | diff | comparison | revisions | |
ucx/ucx.h | file | annotate | diff | comparison | revisions |
--- a/test/main.c Thu Feb 09 10:40:19 2012 +0100 +++ b/test/main.c Sat Feb 18 15:50:43 2012 +0100 @@ -29,30 +29,55 @@ #include <stdio.h> #include <stdlib.h> +#include "ucx/test.h" + #include "list_tests.h" #include "mpool_tests.h" #include "map_tests.h" +UCX_TEST_BEGIN(testTestSuitePositive) { + UCX_TEST_ASSERT(2*2 == 4, "the test framework fails") + UCX_TEST_END +} + +UCX_TEST_BEGIN(testTestSuiteNegative) { + UCX_TEST_ASSERT(2*(-2) == 4, "the test framework works") + UCX_TEST_END +} + int main(int argc, char **argv) { printf("UCX Tests\n---------\n"); - printf("\nUcxDlist Tests\n"); - if(dlist_tests()) { - fprintf(stderr, "dlist_tests failed\n"); - } + printf("\nUcxTestSuite Tests\n"); + UcxTestSuite* suite = ucx_test_suite_new(); + ucx_test_register(suite, testTestSuitePositive); + ucx_test_register(suite, testTestSuiteNegative); + ucx_test_run(suite, stdout); + if (suite->failure == 1 && suite->success == 1) { + ucx_test_suite_free(suite); + + /* TODO: replace these tests with "real" tests */ + printf("\nUcxDlist Tests\n"); + if(dlist_tests()) { + fprintf(stderr, "dlist_tests failed\n"); + } - printf("\nUcxList Tests\n Assumed to be correct\n"); + printf("\nUcxList Tests\n Assumed to be correct\n"); - printf("\nUcxMemPool Tests\n"); - if(mpool_tests()) { - fprintf(stderr, "mpool_tests failed\n"); - } + printf("\nUcxMemPool Tests\n"); + if(mpool_tests()) { + fprintf(stderr, "mpool_tests failed\n"); + } - printf("\nUcxMap Tests\n"); - if(map_tests()) { - fprintf(stderr, "map_tests failed\n"); + printf("\nUcxMap Tests\n"); + if(map_tests()) { + fprintf(stderr, "map_tests failed\n"); + } + + return EXIT_SUCCESS; + } else { + ucx_test_suite_free(suite); + return EXIT_FAILURE; } - - return EXIT_SUCCESS; }
--- a/ucx/Makefile Thu Feb 09 10:40:19 2012 +0100 +++ b/ucx/Makefile Sat Feb 18 15:50:43 2012 +0100 @@ -29,7 +29,7 @@ include ../$(CONF).mk # list of source files -SRC = list.c dlist.c map.c mempool.c string.c +SRC = list.c dlist.c map.c mempool.c string.c test.c OBJ = $(SRC:%.c=../build/%.$(OBJ_EXT))
--- a/ucx/list.h Thu Feb 09 10:40:19 2012 +0100 +++ b/ucx/list.h Sat Feb 18 15:50:43 2012 +0100 @@ -11,7 +11,7 @@ #ifdef __cplusplus extern "C" { #endif - + typedef struct UcxList UcxList; struct UcxList { void *data;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ucx/test.c Sat Feb 18 15:50:43 2012 +0100 @@ -0,0 +1,39 @@ +/* + * File: test.c + * Author: Mike + * + * Created on 18. Februar 2012, 14:15 + */ + +#include "test.h" + +UcxTestSuite* ucx_test_suite_new() { + UcxTestSuite* suite = (UcxTestSuite*) malloc(sizeof(UcxTestSuite)); + if (suite != NULL) { + suite->success = 0; + suite->failure = 0; + suite->tests = NULL; + } + return suite; +} + +void ucx_test_suite_free(UcxTestSuite* suite) { + ucx_list_free(suite->tests); + free(suite); +} + +void ucx_test_register(UcxTestSuite* suite, UcxTest test) { + suite->tests = ucx_list_append(suite->tests, (void*) test); +} + +void ucx_test_run(UcxTestSuite* suite, FILE* output) { + suite->success = 0; + suite->failure = 0; + UCX_FOREACH (UcxList*, suite->tests, e) { + UcxTest test = (UcxTest) (e->data); + test(suite, output); + } + fwrite("\nAll test completed.\n", 1, 21, output); + fprintf(output, " Total: %d\n Success: %d\n Failure: %d\n", + suite->success+suite->failure, suite->success, suite->failure); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ucx/test.h Sat Feb 18 15:50:43 2012 +0100 @@ -0,0 +1,49 @@ +/* + * File: test.h + * Author: Mike + * + * Created on 18. Februar 2012, 14:15 + */ + +#ifndef TEST_H +#define TEST_H + +#include <stdio.h> +#include <string.h> +#include "list.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + unsigned int success; + unsigned int failure; + UcxList *tests; +} UcxTestSuite; + +typedef void(*UcxTest)(UcxTestSuite*,FILE*); + +UcxTestSuite* ucx_test_suite_new(); +void ucx_test_suite_free(UcxTestSuite*); + +void ucx_test_register(UcxTestSuite*, UcxTest); +void ucx_test_run(UcxTestSuite*, FILE*); + +#define UCX_TEST_BEGIN(name) void name(UcxTestSuite* _suite_,FILE *_output_) {\ + fwrite("Running "#name"... ", 1, 12+strlen(#name), _output_); + +#define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \ + fwrite(message".\n", 1, 2+strlen(message), _output_); \ + _suite_->failure++; \ + return;\ + } + +#define UCX_TEST_END } fwrite("success.\n", 1, 9, _output_); _suite_->success++; + +#ifdef __cplusplus +} +#endif + +#endif /* TEST_H */ +
--- a/ucx/ucx.h Thu Feb 09 10:40:19 2012 +0100 +++ b/ucx/ucx.h Sat Feb 18 15:50:43 2012 +0100 @@ -14,6 +14,9 @@ extern "C" { #endif +#define UCX_FOREACH(type,list,elem) \ + for (type elem = list ; elem != NULL ; elem = elem->next) + /* source,data -> errno */ typedef int(*ucx_callback)(void*,void*);