Thu, 31 May 2012 12:51:22 +0200
major refactoring of test framework
test/dlist_tests.c | file | annotate | diff | comparison | revisions | |
test/list_tests.c | file | annotate | diff | comparison | revisions | |
test/main.c | file | annotate | diff | comparison | revisions | |
test/map_tests.c | file | annotate | diff | comparison | revisions | |
test/map_tests.h | file | annotate | diff | comparison | revisions | |
test/mpool_tests.c | file | annotate | diff | comparison | revisions | |
ucx/test.h | file | annotate | diff | comparison | revisions |
--- a/test/dlist_tests.c Thu May 31 09:18:26 2012 +0200 +++ b/test/dlist_tests.c Thu May 31 12:51:22 2012 +0200 @@ -4,8 +4,9 @@ #include "dlist_tests.h" -UCX_TEST_BEGIN(test_ucx_dlist_append) { +UCX_TEST_IMPLEMENT(test_ucx_dlist_append) { UcxDlist *list = ucx_dlist_append(NULL, "Hello"); + UCX_TEST_BEGIN UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") @@ -13,46 +14,47 @@ UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") UCX_TEST_ASSERT(list->next->next == NULL, "failed") + UCX_TEST_END ucx_dlist_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_dlist_prepend) { +UCX_TEST_IMPLEMENT(test_ucx_dlist_prepend) { UcxDlist *list = ucx_dlist_prepend(NULL, " World!"); + UCX_TEST_BEGIN + list = ucx_dlist_prepend(list, "Hello"); UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") UCX_TEST_ASSERT(list->next->next == NULL, "failed") + UCX_TEST_END ucx_dlist_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_dlist_equals) { +UCX_TEST_IMPLEMENT(test_ucx_dlist_equals) { UcxDlist *list = ucx_dlist_append(NULL, "Hello"); list = ucx_dlist_append(list, " World!"); UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!"); list2 = ucx_dlist_prepend(list2, "Hello"); UcxDlist *list3 = ucx_dlist_prepend(NULL, " Welt!"); list3 = ucx_dlist_prepend(list3, "Hallo"); + UCX_TEST_BEGIN UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, cmp_string, NULL), "failed") UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, cmp_string, NULL), "failed") + UCX_TEST_END ucx_dlist_free(list3); ucx_dlist_free(list2); ucx_dlist_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_dlist_concat) { +UCX_TEST_IMPLEMENT(test_ucx_dlist_concat) { UcxDlist *list = ucx_dlist_append(NULL, "Hello"); UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!"); + UCX_TEST_BEGIN list = ucx_dlist_concat(list, list2); @@ -60,13 +62,13 @@ UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") UCX_TEST_ASSERT(list->next->next == NULL, "failed") + UCX_TEST_END ucx_dlist_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_dlist_size) { +UCX_TEST_IMPLEMENT(test_ucx_dlist_size) { UcxDlist *list = ucx_dlist_append(NULL, "This "); + UCX_TEST_BEGIN list = ucx_dlist_append(list, "list "); list = ucx_dlist_append(list, "has "); list = ucx_dlist_append(list, "size "); @@ -74,13 +76,13 @@ UCX_TEST_ASSERT(ucx_dlist_size(list) == 5, "failed"); + UCX_TEST_END ucx_dlist_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_dlist_first) { +UCX_TEST_IMPLEMENT(test_ucx_dlist_first) { UcxDlist *list = ucx_dlist_append(NULL, "Find "); + UCX_TEST_BEGIN list = ucx_dlist_append(list, "the "); list = ucx_dlist_append(list, "first!"); @@ -88,13 +90,13 @@ UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed"); + UCX_TEST_END ucx_dlist_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_dlist_last) { +UCX_TEST_IMPLEMENT(test_ucx_dlist_last) { UcxDlist *list = ucx_dlist_append(NULL, "Find "); + UCX_TEST_BEGIN list = ucx_dlist_append(list, "the "); list = ucx_dlist_append(list, "last!"); @@ -102,13 +104,13 @@ UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed"); + UCX_TEST_END ucx_dlist_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_dlist_get) { +UCX_TEST_IMPLEMENT(test_ucx_dlist_get) { UcxDlist *list = ucx_dlist_append(NULL, "Find "); + UCX_TEST_BEGIN list = ucx_dlist_append(list, "the "); list = ucx_dlist_append(list, "mid!"); @@ -116,13 +118,13 @@ UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed"); + UCX_TEST_END ucx_dlist_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_dlist_remove) { +UCX_TEST_IMPLEMENT(test_ucx_dlist_remove) { UcxDlist *list = ucx_dlist_append(NULL, "Hello"); + UCX_TEST_BEGIN list = ucx_dlist_append(list, " fucking"); list = ucx_dlist_append(list, " World!"); @@ -132,12 +134,11 @@ UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") UCX_TEST_ASSERT(list->next->next == NULL, "failed") + UCX_TEST_END ucx_dlist_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_dlist_clone) { +UCX_TEST_IMPLEMENT(test_ucx_dlist_clone) { char *hello = (char*)malloc(6); char *world = (char*)malloc(8); @@ -149,11 +150,13 @@ list = ucx_dlist_append(list, world); UcxDlist *copy = ucx_dlist_clone(list, copy_string, NULL); + UCX_TEST_BEGIN UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, cmp_string, NULL), "failed") UCX_TEST_ASSERT(hello != copy->data, "first element is no copy") UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy") + UCX_TEST_END free(copy->next->data); free(copy->data); @@ -161,6 +164,4 @@ free(hello); ucx_dlist_free(list); ucx_dlist_free(copy); - - UCX_TEST_END }
--- a/test/list_tests.c Thu May 31 09:18:26 2012 +0200 +++ b/test/list_tests.c Thu May 31 12:51:22 2012 +0200 @@ -4,35 +4,34 @@ #include "list_tests.h" -UCX_TEST_BEGIN(test_ucx_list_append) { +UCX_TEST_IMPLEMENT(test_ucx_list_append) { UcxList *list = ucx_list_append(NULL, "Hello"); - + UCX_TEST_BEGIN UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") list = ucx_list_append(list, " World!"); UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") UCX_TEST_ASSERT(list->next->next == NULL, "failed") - + + UCX_TEST_END ucx_list_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_list_prepend) { +UCX_TEST_IMPLEMENT(test_ucx_list_prepend) { UcxList *list = ucx_list_prepend(NULL, " World!"); + UCX_TEST_BEGIN list = ucx_list_prepend(list, "Hello"); UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") UCX_TEST_ASSERT(list->next->next == NULL, "failed") + UCX_TEST_END ucx_list_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_list_equals) { +UCX_TEST_IMPLEMENT(test_ucx_list_equals) { UcxList *list = ucx_list_append(NULL, "Hello"); list = ucx_list_append(list, " World!"); UcxList *list2 = ucx_list_prepend(NULL, " World!"); @@ -40,33 +39,37 @@ UcxList *list3 = ucx_list_prepend(NULL, " Welt!"); list3 = ucx_list_prepend(list3, "Hallo"); + UCX_TEST_BEGIN UCX_TEST_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed") UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, NULL), "failed") + UCX_TEST_END ucx_list_free(list3); ucx_list_free(list2); ucx_list_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_list_concat) { +UCX_TEST_IMPLEMENT(test_ucx_list_concat) { UcxList *list = ucx_list_append(NULL, "Hello"); UcxList *list2 = ucx_list_prepend(NULL, " World!"); list = ucx_list_concat(list, list2); + UCX_TEST_BEGIN UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") UCX_TEST_ASSERT(list->next->next == NULL, "failed") + UCX_TEST_END + if (list->next == NULL) { + ucx_list_free(list2); + } ucx_list_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_list_size) { +UCX_TEST_IMPLEMENT(test_ucx_list_size) { UcxList *list = ucx_list_append(NULL, "This "); + UCX_TEST_BEGIN list = ucx_list_append(list, "list "); list = ucx_list_append(list, "has "); list = ucx_list_append(list, "size "); @@ -74,13 +77,13 @@ UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed"); + UCX_TEST_END ucx_list_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_list_last) { +UCX_TEST_IMPLEMENT(test_ucx_list_last) { UcxList *list = ucx_list_append(NULL, "Find "); + UCX_TEST_BEGIN list = ucx_list_append(list, "the "); list = ucx_list_append(list, "last!"); @@ -88,13 +91,14 @@ UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed"); + UCX_TEST_END ucx_list_free(list); - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_list_get) { +UCX_TEST_IMPLEMENT(test_ucx_list_get) { UcxList *list = ucx_list_append(NULL, "Find "); + UCX_TEST_BEGIN list = ucx_list_append(list, "the "); list = ucx_list_append(list, "mid!"); @@ -102,13 +106,13 @@ UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed"); + UCX_TEST_END ucx_list_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_list_remove) { +UCX_TEST_IMPLEMENT(test_ucx_list_remove) { UcxList *list = ucx_list_append(NULL, "Hello"); + UCX_TEST_BEGIN list = ucx_list_append(list, " fucking"); list = ucx_list_append(list, " World!"); @@ -117,13 +121,12 @@ UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") UCX_TEST_ASSERT(list->next->next == NULL, "failed") + UCX_TEST_END ucx_list_free(list); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_list_clone) { +UCX_TEST_IMPLEMENT(test_ucx_list_clone) { char *hello = (char*)malloc(6); char *world = (char*)malloc(8); @@ -135,11 +138,13 @@ list = ucx_list_append(list, world); UcxList *copy = ucx_list_clone(list, copy_string, NULL); + UCX_TEST_BEGIN UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed") UCX_TEST_ASSERT(hello != copy->data, "first element is no copy") UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy") - + + UCX_TEST_END free(copy->next->data); free(copy->data); @@ -147,6 +152,4 @@ free(hello); ucx_list_free(list); ucx_list_free(copy); - - UCX_TEST_END }
--- a/test/main.c Thu May 31 09:18:26 2012 +0200 +++ b/test/main.c Thu May 31 12:51:22 2012 +0200 @@ -51,25 +51,57 @@ return cpy; } -UCX_TEST_BEGIN(testTestSuitePositive) { +UCX_TEST_IMPLEMENT(testTestSuitePositive) { + UCX_TEST_BEGIN UCX_TEST_ASSERT(2*2 == 4, "the test framework fails") UCX_TEST_END } -UCX_TEST_BEGIN(testTestSuiteNegative) { +UCX_TEST_IMPLEMENT(testTestSuiteNegative) { + UCX_TEST_BEGIN UCX_TEST_ASSERT(2*(-2) == 4, "the test framework works") UCX_TEST_END } +UCX_TEST_SUBROUTINE(testTestSuiteRoutineSuccess,field) { + int* i = (int*) field; + *i += 2; + UCX_TEST_ASSERT(*i==4, "the test framework fails"); +} + +UCX_TEST_SUBROUTINE(testTestSuiteRoutineFailure,field) { + int* i = (int*) field; + *i += 2; + UCX_TEST_ASSERT(*i==4, "the test framework works"); +} + +UCX_TEST_IMPLEMENT(testTestSuiteRoutinePositive) { + int i = 2; + UCX_TEST_BEGIN + UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutineSuccess, &i); + UCX_TEST_ASSERT(i==4, "the test framework fails"); + UCX_TEST_END +} + +UCX_TEST_IMPLEMENT(testTestSuiteRoutineNegative) { + int i = 0; + UCX_TEST_BEGIN + UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutineFailure, &i); + UCX_TEST_ASSERT(1, "the test framework fails"); + UCX_TEST_END +} + int main(int argc, char **argv) { printf("UCX Tests\n---------\n"); - printf("\nUcxTestSuite tests (1 failure is intended!)\n"); + printf("\nUcxTestSuite tests (2 failures are intended!)\n"); UcxTestSuite* suite = ucx_test_suite_new(); ucx_test_register(suite, testTestSuitePositive); ucx_test_register(suite, testTestSuiteNegative); + ucx_test_register(suite, testTestSuiteRoutinePositive); + ucx_test_register(suite, testTestSuiteRoutineNegative); ucx_test_run(suite, stdout); - if (suite->failure == 1 && suite->success == 1) { + if (suite->failure == 2 && suite->success == 2) { ucx_test_suite_free(suite); printf("\nLibrary function tests\n"); @@ -112,6 +144,7 @@ ucx_test_register(suite, test_ucx_map_put); ucx_test_register(suite, test_ucx_map_get); ucx_test_register(suite, test_ucx_map_iterator); + ucx_test_register(suite, test_ucx_map_iterator_chain); ucx_test_run(suite, stdout); ucx_test_suite_free(suite);
--- a/test/map_tests.c Thu May 31 09:18:26 2012 +0200 +++ b/test/map_tests.c Thu May 31 12:51:22 2012 +0200 @@ -4,20 +4,19 @@ #include "map_tests.h" -UCX_TEST_BEGIN(test_ucx_map_new) { +UCX_TEST_IMPLEMENT(test_ucx_map_new) { UcxMap *map = ucx_map_new(16); - + UCX_TEST_BEGIN UCX_TEST_ASSERT(map->size == 16, "wrong size") UCX_TEST_ASSERT(map->map != NULL, "failed") + UCX_TEST_END ucx_map_free(map); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_key) { - +UCX_TEST_IMPLEMENT(test_ucx_key) { UcxKey key = ucx_key("This is a text.", 15); + UCX_TEST_BEGIN UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed") UCX_TEST_ASSERT(key.len == 15, "failed") UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed") @@ -25,13 +24,14 @@ UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_map_put) { +UCX_TEST_IMPLEMENT(test_ucx_map_put) { UcxMap *map = ucx_map_new(4); int td[5]; td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000; + UCX_TEST_BEGIN ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */ ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */ ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */ @@ -63,19 +63,18 @@ "overwrite failed") UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed") + UCX_TEST_END ucx_map_free(map); - +} + +UCX_TEST_IMPLEMENT(test_ucx_map_get) { + UCX_TEST_BEGIN + UCX_TEST_ASSERT(0, "not implemented"); UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_map_get) { - // TODO: - UCX_TEST_END -} - -UCX_TEST_BEGIN(test_ucx_map_iterator) { - UcxMap *map = ucx_map_new(16); - +UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) { + UcxMap *map = (UcxMap*) mapptr; int v1 = 10; int v2 = 15; int v3 = 7; @@ -97,29 +96,20 @@ UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits"); UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result"); +} +UCX_TEST_IMPLEMENT(test_ucx_map_iterator) { + UcxMap *map = ucx_map_new(16); + UCX_TEST_BEGIN + UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map) + UCX_TEST_END ucx_map_free(map); - - map = ucx_map_new(1); - ucx_map_cstr_put(map, "v1", &v1); - ucx_map_cstr_put(map, "v2", &v2); - ucx_map_cstr_put(map, "v3", &v3); - ucx_map_cstr_put(map, "v4", &v4); - - i = ucx_map_iterator(map); - check = 0; - hit = 0; - - UCX_MAP_FOREACH(int*, v, map, i) { - check += *v; - hit++; - } - - UCX_TEST_ASSERT(hit == 4, "test2: wrong number of hits"); - UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test2: wrong result"); - - +} + +UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) { + UcxMap *map = ucx_map_new(1); + UCX_TEST_BEGIN + UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map) + UCX_TEST_END ucx_map_free(map); - - UCX_TEST_END }
--- a/test/map_tests.h Thu May 31 09:18:26 2012 +0200 +++ b/test/map_tests.h Thu May 31 12:51:22 2012 +0200 @@ -17,6 +17,7 @@ UCX_TEST_DECLARE(test_ucx_map_put) UCX_TEST_DECLARE(test_ucx_map_get) UCX_TEST_DECLARE(test_ucx_map_iterator) +UCX_TEST_DECLARE(test_ucx_map_iterator_chain) #ifdef __cplusplus
--- a/test/mpool_tests.c Thu May 31 09:18:26 2012 +0200 +++ b/test/mpool_tests.c Thu May 31 12:51:22 2012 +0200 @@ -6,22 +6,20 @@ #include "mpool_tests.h" -UCX_TEST_BEGIN(test_ucx_mempool_new) { +UCX_TEST_IMPLEMENT(test_ucx_mempool_new) { UcxMempool *pool = ucx_mempool_new(16); - + UCX_TEST_BEGIN UCX_TEST_ASSERT(pool->size == 16, "wrong size") UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter") UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed") - + UCX_TEST_END ucx_mempool_free(pool); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_mempool_malloc) { +UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc) { UcxMempool *pool = ucx_mempool_new(1); - + UCX_TEST_BEGIN intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t)); UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented") @@ -33,15 +31,14 @@ UCX_TEST_ASSERT(*test == 5, "wrong pointer") + UCX_TEST_END ucx_mempool_free(pool); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_mempool_malloc_with_chcap) { +UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc_with_chcap) { UcxMempool *pool = ucx_mempool_new(1); - + UCX_TEST_BEGIN ucx_mempool_malloc(pool, sizeof(int)); intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t)); @@ -54,23 +51,22 @@ UCX_TEST_ASSERT(*test == 5, "wrong pointer") + UCX_TEST_END ucx_mempool_free(pool); - - UCX_TEST_END } -UCX_TEST_BEGIN(test_ucx_mempool_calloc) { +UCX_TEST_IMPLEMENT(test_ucx_mempool_calloc) { UcxMempool *pool = ucx_mempool_new(1); + UCX_TEST_BEGIN intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); UCX_TEST_ASSERT(test != NULL, "no memory for test data") UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed") + UCX_TEST_END ucx_mempool_free(pool); - - UCX_TEST_END } void test_setdestr(void* elem) { @@ -78,14 +74,15 @@ *cb = 42; } -UCX_TEST_BEGIN(test_ucx_mempool_set_destr) { +UCX_TEST_IMPLEMENT(test_ucx_mempool_set_destr) { + intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); + UCX_TEST_BEGIN UcxMempool *pool = ucx_mempool_new(2); ucx_mempool_malloc(pool, sizeof(intptr_t)); intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); - intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") test[0] = 5; test[1] = (intptr_t) cb; @@ -100,20 +97,19 @@ ucx_mempool_free(pool); UCX_TEST_ASSERT(*cb == 42, "destructor not called") - - free(cb); UCX_TEST_END + if (cb != NULL) free(cb); } -UCX_TEST_BEGIN(test_ucx_mempool_reg_destr) { +UCX_TEST_IMPLEMENT(test_ucx_mempool_reg_destr) { + intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t)); + intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); + UCX_TEST_BEGIN UcxMempool *pool = ucx_mempool_new(1); - intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t)); - - intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") test[0] = 5; test[1] = (intptr_t) cb; @@ -127,23 +123,22 @@ UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed") ucx_mempool_free(pool); - free(test); - UCX_TEST_ASSERT(*cb == 42, "destructor not called") + UCX_TEST_END - free(cb); - - UCX_TEST_END + if (test != NULL) free(test); + if (cb != NULL) free(cb); } -UCX_TEST_BEGIN(test_ucx_mempool_realloc) { - +UCX_TEST_IMPLEMENT(test_ucx_mempool_realloc) { + + intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); + UCX_TEST_BEGIN UcxMempool *pool = ucx_mempool_new(2); ucx_mempool_malloc(pool, sizeof(intptr_t)); intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); - - intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); + UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") test[0] = 5; test[1] = (intptr_t) cb; @@ -167,8 +162,7 @@ ucx_mempool_free(pool); UCX_TEST_ASSERT(*cb == 42, "destructor not called") - - free(cb); UCX_TEST_END + if (cb != NULL) free(cb); }
--- a/ucx/test.h Thu May 31 09:18:26 2012 +0200 +++ b/ucx/test.h Thu May 31 12:51:22 2012 +0200 @@ -3,6 +3,31 @@ * Author: Mike * * Created on 18. Februar 2012, 14:15 + * + * + * + * Usage of this test framework: + * + * **** IN HEADER FILE: **** + * + * UCX_TEST_DECLARE(function_name) + * + * **** IN SOURCE FILE: **** + * + * UCX_TEST_IMPLEMENT(function_name) { + * <memory allocation and other stuff here> + * UCX_TEST_BEGIN + * <tests with UCX_TEST_ASSERT here> + * UCX_TEST_END + * <cleanup of memory here> + * } + * + * PLEASE NOTE: if a test fails, a longjump is performed + * back to the UCX_TEST_BEGIN macro! + * + * You may use multiple BEGIN-END blocks if you are aware of the + * longjmp behaviour. + * */ #ifndef TEST_H @@ -10,6 +35,7 @@ #include <stdio.h> #include <string.h> +#include <setjmp.h> #include "list.h" #ifdef __cplusplus @@ -31,16 +57,25 @@ void ucx_test_run(UcxTestSuite*, FILE*); #define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *); -#define UCX_TEST_BEGIN(name) void name(UcxTestSuite* _suite_,FILE *_output_) {\ - fwrite("Running "#name"... ", 1, 12+strlen(#name), _output_); +#define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_) + +#define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\ + fwrite(__func__, 1, strlen(__func__), _output_);\ + fwrite("... ", 1, 4, _output_);\ + jmp_buf _env_; \ + if (!setjmp(_env_)) { #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \ fwrite(message".\n", 1, 2+strlen(message), _output_); \ _suite_->failure++; \ - return;\ + longjmp(_env_, 1);\ } -#define UCX_TEST_END } fwrite("success.\n", 1, 9, _output_); _suite_->success++; +#define UCX_TEST_SUBROUTINE(name,data) void name(UcxTestSuite* _suite_,\ + FILE *_output_, jmp_buf _env_, void* data) +#define UCX_TEST_CALL_SUBROUTINE(name,data) name(_suite_,_output_,_env_,data); + +#define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;} #ifdef __cplusplus }