major refactoring of test framework

Thu, 31 May 2012 12:51:22 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 31 May 2012 12:51:22 +0200
changeset 33
9c219a62070d
parent 32
c7af4ec56e19
child 34
0dcd2ca2a223

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
     1.1 --- a/test/dlist_tests.c	Thu May 31 09:18:26 2012 +0200
     1.2 +++ b/test/dlist_tests.c	Thu May 31 12:51:22 2012 +0200
     1.3 @@ -4,8 +4,9 @@
     1.4  
     1.5  #include "dlist_tests.h"
     1.6  
     1.7 -UCX_TEST_BEGIN(test_ucx_dlist_append) {
     1.8 +UCX_TEST_IMPLEMENT(test_ucx_dlist_append) {
     1.9      UcxDlist *list = ucx_dlist_append(NULL, "Hello");
    1.10 +    UCX_TEST_BEGIN
    1.11      
    1.12      UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    1.13      
    1.14 @@ -13,46 +14,47 @@
    1.15      
    1.16      UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    1.17      UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    1.18 +    UCX_TEST_END
    1.19      
    1.20      ucx_dlist_free(list);
    1.21 -    
    1.22 -    UCX_TEST_END
    1.23  }
    1.24  
    1.25 -UCX_TEST_BEGIN(test_ucx_dlist_prepend) {
    1.26 +UCX_TEST_IMPLEMENT(test_ucx_dlist_prepend) {
    1.27      UcxDlist *list = ucx_dlist_prepend(NULL, " World!");
    1.28 +    UCX_TEST_BEGIN
    1.29 +
    1.30      list = ucx_dlist_prepend(list, "Hello");
    1.31      
    1.32      UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    1.33      UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    1.34      UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    1.35      
    1.36 +    UCX_TEST_END
    1.37      ucx_dlist_free(list);
    1.38 -    
    1.39 -    UCX_TEST_END
    1.40  }
    1.41  
    1.42 -UCX_TEST_BEGIN(test_ucx_dlist_equals) {
    1.43 +UCX_TEST_IMPLEMENT(test_ucx_dlist_equals) {
    1.44      UcxDlist *list = ucx_dlist_append(NULL, "Hello");
    1.45      list = ucx_dlist_append(list, " World!");
    1.46      UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!");
    1.47      list2 = ucx_dlist_prepend(list2, "Hello");
    1.48      UcxDlist *list3 = ucx_dlist_prepend(NULL, " Welt!");
    1.49      list3 = ucx_dlist_prepend(list3, "Hallo");
    1.50 +    UCX_TEST_BEGIN
    1.51      
    1.52      UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, cmp_string, NULL), "failed")
    1.53      UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, cmp_string, NULL), "failed")
    1.54      
    1.55 +    UCX_TEST_END
    1.56      ucx_dlist_free(list3);
    1.57      ucx_dlist_free(list2);
    1.58      ucx_dlist_free(list);
    1.59 -    
    1.60 -    UCX_TEST_END
    1.61  }
    1.62  
    1.63 -UCX_TEST_BEGIN(test_ucx_dlist_concat) {
    1.64 +UCX_TEST_IMPLEMENT(test_ucx_dlist_concat) {
    1.65      UcxDlist *list = ucx_dlist_append(NULL, "Hello");
    1.66      UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!");
    1.67 +    UCX_TEST_BEGIN
    1.68      
    1.69      list = ucx_dlist_concat(list, list2);
    1.70      
    1.71 @@ -60,13 +62,13 @@
    1.72      UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    1.73      UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    1.74      
    1.75 +    UCX_TEST_END
    1.76      ucx_dlist_free(list);
    1.77 -    
    1.78 -    UCX_TEST_END
    1.79  }
    1.80  
    1.81 -UCX_TEST_BEGIN(test_ucx_dlist_size) {
    1.82 +UCX_TEST_IMPLEMENT(test_ucx_dlist_size) {
    1.83      UcxDlist *list = ucx_dlist_append(NULL, "This ");
    1.84 +    UCX_TEST_BEGIN
    1.85      list = ucx_dlist_append(list, "list ");
    1.86      list = ucx_dlist_append(list, "has ");
    1.87      list = ucx_dlist_append(list, "size ");
    1.88 @@ -74,13 +76,13 @@
    1.89      
    1.90      UCX_TEST_ASSERT(ucx_dlist_size(list) == 5, "failed");
    1.91      
    1.92 +    UCX_TEST_END
    1.93      ucx_dlist_free(list);
    1.94 -    
    1.95 -    UCX_TEST_END
    1.96  }
    1.97  
    1.98 -UCX_TEST_BEGIN(test_ucx_dlist_first) {
    1.99 +UCX_TEST_IMPLEMENT(test_ucx_dlist_first) {
   1.100      UcxDlist *list = ucx_dlist_append(NULL, "Find ");
   1.101 +    UCX_TEST_BEGIN
   1.102      list = ucx_dlist_append(list, "the ");
   1.103      list = ucx_dlist_append(list, "first!");
   1.104      
   1.105 @@ -88,13 +90,13 @@
   1.106      
   1.107      UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
   1.108      
   1.109 +    UCX_TEST_END
   1.110      ucx_dlist_free(list);
   1.111 -    
   1.112 -    UCX_TEST_END
   1.113  }
   1.114  
   1.115 -UCX_TEST_BEGIN(test_ucx_dlist_last) {
   1.116 +UCX_TEST_IMPLEMENT(test_ucx_dlist_last) {
   1.117      UcxDlist *list = ucx_dlist_append(NULL, "Find ");
   1.118 +    UCX_TEST_BEGIN
   1.119      list = ucx_dlist_append(list, "the ");
   1.120      list = ucx_dlist_append(list, "last!");
   1.121      
   1.122 @@ -102,13 +104,13 @@
   1.123      
   1.124      UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
   1.125      
   1.126 +    UCX_TEST_END
   1.127      ucx_dlist_free(list);
   1.128 -    
   1.129 -    UCX_TEST_END
   1.130  }
   1.131  
   1.132 -UCX_TEST_BEGIN(test_ucx_dlist_get) {
   1.133 +UCX_TEST_IMPLEMENT(test_ucx_dlist_get) {
   1.134      UcxDlist *list = ucx_dlist_append(NULL, "Find ");
   1.135 +    UCX_TEST_BEGIN
   1.136      list = ucx_dlist_append(list, "the ");
   1.137      list = ucx_dlist_append(list, "mid!");
   1.138      
   1.139 @@ -116,13 +118,13 @@
   1.140      
   1.141      UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
   1.142      
   1.143 +    UCX_TEST_END
   1.144      ucx_dlist_free(list);
   1.145 -    
   1.146 -    UCX_TEST_END
   1.147  }
   1.148  
   1.149 -UCX_TEST_BEGIN(test_ucx_dlist_remove) {
   1.150 +UCX_TEST_IMPLEMENT(test_ucx_dlist_remove) {
   1.151      UcxDlist *list = ucx_dlist_append(NULL, "Hello");
   1.152 +    UCX_TEST_BEGIN
   1.153      list = ucx_dlist_append(list, " fucking");
   1.154      list = ucx_dlist_append(list, " World!");
   1.155      
   1.156 @@ -132,12 +134,11 @@
   1.157      UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
   1.158      UCX_TEST_ASSERT(list->next->next == NULL, "failed")
   1.159      
   1.160 +    UCX_TEST_END
   1.161      ucx_dlist_free(list);
   1.162 -    
   1.163 -    UCX_TEST_END
   1.164  }
   1.165  
   1.166 -UCX_TEST_BEGIN(test_ucx_dlist_clone) {
   1.167 +UCX_TEST_IMPLEMENT(test_ucx_dlist_clone) {
   1.168     
   1.169      char *hello = (char*)malloc(6);
   1.170      char *world = (char*)malloc(8);
   1.171 @@ -149,11 +150,13 @@
   1.172      list = ucx_dlist_append(list, world);
   1.173      
   1.174      UcxDlist *copy = ucx_dlist_clone(list, copy_string, NULL);
   1.175 +    UCX_TEST_BEGIN
   1.176  
   1.177      UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, cmp_string, NULL), "failed")
   1.178      UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
   1.179      UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
   1.180  
   1.181 +    UCX_TEST_END
   1.182      free(copy->next->data);
   1.183      free(copy->data);
   1.184  
   1.185 @@ -161,6 +164,4 @@
   1.186      free(hello);
   1.187      ucx_dlist_free(list);
   1.188      ucx_dlist_free(copy);
   1.189 -    
   1.190 -    UCX_TEST_END
   1.191  }
     2.1 --- a/test/list_tests.c	Thu May 31 09:18:26 2012 +0200
     2.2 +++ b/test/list_tests.c	Thu May 31 12:51:22 2012 +0200
     2.3 @@ -4,35 +4,34 @@
     2.4  
     2.5  #include "list_tests.h"
     2.6  
     2.7 -UCX_TEST_BEGIN(test_ucx_list_append) {
     2.8 +UCX_TEST_IMPLEMENT(test_ucx_list_append) {
     2.9      UcxList *list = ucx_list_append(NULL, "Hello");
    2.10 -    
    2.11 +    UCX_TEST_BEGIN
    2.12      UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    2.13      
    2.14      list = ucx_list_append(list, " World!");
    2.15      
    2.16      UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    2.17      UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    2.18 -    
    2.19 +
    2.20 +    UCX_TEST_END
    2.21      ucx_list_free(list);
    2.22 -    
    2.23 -    UCX_TEST_END
    2.24  }
    2.25  
    2.26 -UCX_TEST_BEGIN(test_ucx_list_prepend) {
    2.27 +UCX_TEST_IMPLEMENT(test_ucx_list_prepend) {
    2.28      UcxList *list = ucx_list_prepend(NULL, " World!");
    2.29 +    UCX_TEST_BEGIN
    2.30      list = ucx_list_prepend(list, "Hello");
    2.31      
    2.32      UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    2.33      UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    2.34      UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    2.35      
    2.36 +    UCX_TEST_END
    2.37      ucx_list_free(list);
    2.38 -    
    2.39 -    UCX_TEST_END
    2.40  }
    2.41  
    2.42 -UCX_TEST_BEGIN(test_ucx_list_equals) {
    2.43 +UCX_TEST_IMPLEMENT(test_ucx_list_equals) {
    2.44      UcxList *list = ucx_list_append(NULL, "Hello");
    2.45      list = ucx_list_append(list, " World!");
    2.46      UcxList *list2 = ucx_list_prepend(NULL, " World!");
    2.47 @@ -40,33 +39,37 @@
    2.48      UcxList *list3 = ucx_list_prepend(NULL, " Welt!");
    2.49      list3 = ucx_list_prepend(list3, "Hallo");
    2.50      
    2.51 +    UCX_TEST_BEGIN
    2.52      UCX_TEST_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed")
    2.53      UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, NULL), "failed")
    2.54 +    UCX_TEST_END
    2.55      
    2.56      ucx_list_free(list3);
    2.57      ucx_list_free(list2);
    2.58      ucx_list_free(list);
    2.59 -    
    2.60 -    UCX_TEST_END
    2.61  }
    2.62  
    2.63 -UCX_TEST_BEGIN(test_ucx_list_concat) {
    2.64 +UCX_TEST_IMPLEMENT(test_ucx_list_concat) {
    2.65      UcxList *list = ucx_list_append(NULL, "Hello");
    2.66      UcxList *list2 = ucx_list_prepend(NULL, " World!");
    2.67      
    2.68      list = ucx_list_concat(list, list2);
    2.69 +    UCX_TEST_BEGIN
    2.70      
    2.71      UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    2.72      UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    2.73      UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    2.74      
    2.75 +    UCX_TEST_END
    2.76 +    if (list->next == NULL) {
    2.77 +        ucx_list_free(list2);
    2.78 +    }
    2.79      ucx_list_free(list);
    2.80 -    
    2.81 -    UCX_TEST_END
    2.82  }
    2.83  
    2.84 -UCX_TEST_BEGIN(test_ucx_list_size) {
    2.85 +UCX_TEST_IMPLEMENT(test_ucx_list_size) {
    2.86      UcxList *list = ucx_list_append(NULL, "This ");
    2.87 +    UCX_TEST_BEGIN
    2.88      list = ucx_list_append(list, "list ");
    2.89      list = ucx_list_append(list, "has ");
    2.90      list = ucx_list_append(list, "size ");
    2.91 @@ -74,13 +77,13 @@
    2.92      
    2.93      UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
    2.94      
    2.95 +    UCX_TEST_END
    2.96      ucx_list_free(list);
    2.97 -    
    2.98 -    UCX_TEST_END
    2.99  }
   2.100  
   2.101 -UCX_TEST_BEGIN(test_ucx_list_last) {
   2.102 +UCX_TEST_IMPLEMENT(test_ucx_list_last) {
   2.103      UcxList *list = ucx_list_append(NULL, "Find ");
   2.104 +    UCX_TEST_BEGIN
   2.105      list = ucx_list_append(list, "the ");
   2.106      list = ucx_list_append(list, "last!");
   2.107      
   2.108 @@ -88,13 +91,14 @@
   2.109      
   2.110      UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
   2.111      
   2.112 +    UCX_TEST_END
   2.113      ucx_list_free(list);
   2.114      
   2.115 -    UCX_TEST_END
   2.116  }
   2.117  
   2.118 -UCX_TEST_BEGIN(test_ucx_list_get) {
   2.119 +UCX_TEST_IMPLEMENT(test_ucx_list_get) {
   2.120      UcxList *list = ucx_list_append(NULL, "Find ");
   2.121 +    UCX_TEST_BEGIN
   2.122      list = ucx_list_append(list, "the ");
   2.123      list = ucx_list_append(list, "mid!");
   2.124      
   2.125 @@ -102,13 +106,13 @@
   2.126      
   2.127      UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
   2.128      
   2.129 +    UCX_TEST_END
   2.130      ucx_list_free(list);
   2.131 -    
   2.132 -    UCX_TEST_END
   2.133  }
   2.134  
   2.135 -UCX_TEST_BEGIN(test_ucx_list_remove) {
   2.136 +UCX_TEST_IMPLEMENT(test_ucx_list_remove) {
   2.137      UcxList *list = ucx_list_append(NULL, "Hello");
   2.138 +    UCX_TEST_BEGIN
   2.139      list = ucx_list_append(list, " fucking");
   2.140      list = ucx_list_append(list, " World!");
   2.141      
   2.142 @@ -117,13 +121,12 @@
   2.143      UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
   2.144      UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
   2.145      UCX_TEST_ASSERT(list->next->next == NULL, "failed")
   2.146 +    UCX_TEST_END
   2.147      
   2.148      ucx_list_free(list);
   2.149 -    
   2.150 -    UCX_TEST_END
   2.151  }
   2.152  
   2.153 -UCX_TEST_BEGIN(test_ucx_list_clone) {
   2.154 +UCX_TEST_IMPLEMENT(test_ucx_list_clone) {
   2.155     
   2.156      char *hello = (char*)malloc(6);
   2.157      char *world = (char*)malloc(8);
   2.158 @@ -135,11 +138,13 @@
   2.159      list = ucx_list_append(list, world);
   2.160      
   2.161      UcxList *copy = ucx_list_clone(list, copy_string, NULL);
   2.162 +    UCX_TEST_BEGIN
   2.163  
   2.164      UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed")
   2.165      UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
   2.166      UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
   2.167 -            
   2.168 +
   2.169 +    UCX_TEST_END
   2.170      free(copy->next->data);
   2.171      free(copy->data);
   2.172  
   2.173 @@ -147,6 +152,4 @@
   2.174      free(hello);
   2.175      ucx_list_free(list);
   2.176      ucx_list_free(copy);
   2.177 -    
   2.178 -    UCX_TEST_END
   2.179  }
     3.1 --- a/test/main.c	Thu May 31 09:18:26 2012 +0200
     3.2 +++ b/test/main.c	Thu May 31 12:51:22 2012 +0200
     3.3 @@ -51,25 +51,57 @@
     3.4      return cpy;
     3.5  }
     3.6  
     3.7 -UCX_TEST_BEGIN(testTestSuitePositive) {
     3.8 +UCX_TEST_IMPLEMENT(testTestSuitePositive) {
     3.9 +    UCX_TEST_BEGIN
    3.10      UCX_TEST_ASSERT(2*2 == 4, "the test framework fails")
    3.11      UCX_TEST_END
    3.12  }
    3.13  
    3.14 -UCX_TEST_BEGIN(testTestSuiteNegative) {
    3.15 +UCX_TEST_IMPLEMENT(testTestSuiteNegative) {
    3.16 +    UCX_TEST_BEGIN
    3.17      UCX_TEST_ASSERT(2*(-2) == 4, "the test framework works")
    3.18      UCX_TEST_END
    3.19  }
    3.20  
    3.21 +UCX_TEST_SUBROUTINE(testTestSuiteRoutineSuccess,field) {
    3.22 +    int* i = (int*) field;
    3.23 +    *i += 2;
    3.24 +    UCX_TEST_ASSERT(*i==4, "the test framework fails");
    3.25 +}
    3.26 +
    3.27 +UCX_TEST_SUBROUTINE(testTestSuiteRoutineFailure,field) {
    3.28 +    int* i = (int*) field;
    3.29 +    *i += 2;
    3.30 +    UCX_TEST_ASSERT(*i==4, "the test framework works");
    3.31 +}
    3.32 +
    3.33 +UCX_TEST_IMPLEMENT(testTestSuiteRoutinePositive) {
    3.34 +    int i = 2;
    3.35 +    UCX_TEST_BEGIN
    3.36 +    UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutineSuccess, &i);
    3.37 +    UCX_TEST_ASSERT(i==4, "the test framework fails");
    3.38 +    UCX_TEST_END
    3.39 +}
    3.40 +
    3.41 +UCX_TEST_IMPLEMENT(testTestSuiteRoutineNegative) {
    3.42 +    int i = 0;
    3.43 +    UCX_TEST_BEGIN
    3.44 +    UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutineFailure, &i);
    3.45 +    UCX_TEST_ASSERT(1, "the test framework fails");
    3.46 +    UCX_TEST_END
    3.47 +}
    3.48 +
    3.49  int main(int argc, char **argv) {
    3.50      printf("UCX Tests\n---------\n");
    3.51  
    3.52 -    printf("\nUcxTestSuite tests (1 failure is intended!)\n");
    3.53 +    printf("\nUcxTestSuite tests (2 failures are intended!)\n");
    3.54      UcxTestSuite* suite = ucx_test_suite_new();
    3.55      ucx_test_register(suite, testTestSuitePositive);
    3.56      ucx_test_register(suite, testTestSuiteNegative);
    3.57 +    ucx_test_register(suite, testTestSuiteRoutinePositive);
    3.58 +    ucx_test_register(suite, testTestSuiteRoutineNegative);
    3.59      ucx_test_run(suite, stdout);
    3.60 -    if (suite->failure == 1 && suite->success == 1) {
    3.61 +    if (suite->failure == 2 && suite->success == 2) {
    3.62          ucx_test_suite_free(suite);
    3.63  
    3.64          printf("\nLibrary function tests\n");
    3.65 @@ -112,6 +144,7 @@
    3.66          ucx_test_register(suite, test_ucx_map_put);
    3.67          ucx_test_register(suite, test_ucx_map_get);
    3.68          ucx_test_register(suite, test_ucx_map_iterator);
    3.69 +        ucx_test_register(suite, test_ucx_map_iterator_chain);
    3.70          
    3.71          ucx_test_run(suite, stdout);
    3.72          ucx_test_suite_free(suite);
     4.1 --- a/test/map_tests.c	Thu May 31 09:18:26 2012 +0200
     4.2 +++ b/test/map_tests.c	Thu May 31 12:51:22 2012 +0200
     4.3 @@ -4,20 +4,19 @@
     4.4  
     4.5  #include "map_tests.h"
     4.6  
     4.7 -UCX_TEST_BEGIN(test_ucx_map_new) {
     4.8 +UCX_TEST_IMPLEMENT(test_ucx_map_new) {
     4.9      UcxMap *map = ucx_map_new(16);
    4.10 -    
    4.11 +    UCX_TEST_BEGIN
    4.12      UCX_TEST_ASSERT(map->size == 16, "wrong size")
    4.13      UCX_TEST_ASSERT(map->map != NULL, "failed")
    4.14      
    4.15 +    UCX_TEST_END
    4.16      ucx_map_free(map);
    4.17 -    
    4.18 -    UCX_TEST_END
    4.19  }
    4.20  
    4.21 -UCX_TEST_BEGIN(test_ucx_key) {
    4.22 -    
    4.23 +UCX_TEST_IMPLEMENT(test_ucx_key) {
    4.24      UcxKey key = ucx_key("This is a text.", 15);
    4.25 +    UCX_TEST_BEGIN
    4.26      UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed")
    4.27      UCX_TEST_ASSERT(key.len == 15, "failed")
    4.28      UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed")
    4.29 @@ -25,13 +24,14 @@
    4.30      UCX_TEST_END
    4.31  }
    4.32  
    4.33 -UCX_TEST_BEGIN(test_ucx_map_put) {
    4.34 +UCX_TEST_IMPLEMENT(test_ucx_map_put) {
    4.35      
    4.36      UcxMap *map = ucx_map_new(4);
    4.37      
    4.38      int td[5];
    4.39      td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
    4.40  
    4.41 +    UCX_TEST_BEGIN
    4.42      ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
    4.43      ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
    4.44      ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
    4.45 @@ -63,19 +63,18 @@
    4.46              "overwrite failed")
    4.47      UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed")
    4.48      
    4.49 +    UCX_TEST_END
    4.50      ucx_map_free(map);
    4.51 -    
    4.52 +}
    4.53 +
    4.54 +UCX_TEST_IMPLEMENT(test_ucx_map_get) {
    4.55 +    UCX_TEST_BEGIN
    4.56 +    UCX_TEST_ASSERT(0, "not implemented");
    4.57      UCX_TEST_END
    4.58  }
    4.59  
    4.60 -UCX_TEST_BEGIN(test_ucx_map_get) {
    4.61 -    // TODO:
    4.62 -    UCX_TEST_END
    4.63 -}
    4.64 -
    4.65 -UCX_TEST_BEGIN(test_ucx_map_iterator) {
    4.66 -    UcxMap *map = ucx_map_new(16);
    4.67 -    
    4.68 +UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) {
    4.69 +    UcxMap *map = (UcxMap*) mapptr;
    4.70      int v1 = 10;
    4.71      int v2 = 15;
    4.72      int v3 = 7;
    4.73 @@ -97,29 +96,20 @@
    4.74  
    4.75      UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
    4.76      UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
    4.77 +}
    4.78  
    4.79 +UCX_TEST_IMPLEMENT(test_ucx_map_iterator) {
    4.80 +    UcxMap *map = ucx_map_new(16);
    4.81 +    UCX_TEST_BEGIN
    4.82 +    UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
    4.83 +    UCX_TEST_END
    4.84      ucx_map_free(map);
    4.85 -    
    4.86 -    map = ucx_map_new(1);
    4.87 -    ucx_map_cstr_put(map, "v1", &v1);
    4.88 -    ucx_map_cstr_put(map, "v2", &v2);
    4.89 -    ucx_map_cstr_put(map, "v3", &v3);
    4.90 -    ucx_map_cstr_put(map, "v4", &v4);
    4.91 -    
    4.92 -    i = ucx_map_iterator(map);
    4.93 -    check = 0;
    4.94 -    hit = 0;
    4.95 -    
    4.96 -    UCX_MAP_FOREACH(int*, v, map, i) {
    4.97 -        check += *v;
    4.98 -        hit++;
    4.99 -    }
   4.100 -    
   4.101 -    UCX_TEST_ASSERT(hit == 4, "test2: wrong number of hits");
   4.102 -    UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test2: wrong result");
   4.103 -    
   4.104 -    
   4.105 +}
   4.106 +
   4.107 +UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) {
   4.108 +    UcxMap *map = ucx_map_new(1);
   4.109 +    UCX_TEST_BEGIN
   4.110 +    UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
   4.111 +    UCX_TEST_END
   4.112      ucx_map_free(map);
   4.113 -    
   4.114 -    UCX_TEST_END
   4.115  }
     5.1 --- a/test/map_tests.h	Thu May 31 09:18:26 2012 +0200
     5.2 +++ b/test/map_tests.h	Thu May 31 12:51:22 2012 +0200
     5.3 @@ -17,6 +17,7 @@
     5.4  UCX_TEST_DECLARE(test_ucx_map_put)
     5.5  UCX_TEST_DECLARE(test_ucx_map_get)
     5.6  UCX_TEST_DECLARE(test_ucx_map_iterator)
     5.7 +UCX_TEST_DECLARE(test_ucx_map_iterator_chain)
     5.8  
     5.9  
    5.10  #ifdef	__cplusplus
     6.1 --- a/test/mpool_tests.c	Thu May 31 09:18:26 2012 +0200
     6.2 +++ b/test/mpool_tests.c	Thu May 31 12:51:22 2012 +0200
     6.3 @@ -6,22 +6,20 @@
     6.4  
     6.5  #include "mpool_tests.h"
     6.6  
     6.7 -UCX_TEST_BEGIN(test_ucx_mempool_new) {
     6.8 +UCX_TEST_IMPLEMENT(test_ucx_mempool_new) {
     6.9      UcxMempool *pool = ucx_mempool_new(16);
    6.10 -    
    6.11 +    UCX_TEST_BEGIN
    6.12      UCX_TEST_ASSERT(pool->size == 16, "wrong size")
    6.13      UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter")
    6.14      UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed")
    6.15 -    
    6.16 +    UCX_TEST_END
    6.17      ucx_mempool_free(pool);
    6.18 -    
    6.19 -    UCX_TEST_END
    6.20  }
    6.21  
    6.22 -UCX_TEST_BEGIN(test_ucx_mempool_malloc) {
    6.23 +UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc) {
    6.24      
    6.25      UcxMempool *pool = ucx_mempool_new(1);
    6.26 -    
    6.27 +    UCX_TEST_BEGIN
    6.28      intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
    6.29      
    6.30      UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented")
    6.31 @@ -33,15 +31,14 @@
    6.32      
    6.33      UCX_TEST_ASSERT(*test == 5, "wrong pointer")
    6.34      
    6.35 +    UCX_TEST_END
    6.36      ucx_mempool_free(pool);
    6.37 -    
    6.38 -    UCX_TEST_END
    6.39  }
    6.40  
    6.41 -UCX_TEST_BEGIN(test_ucx_mempool_malloc_with_chcap) {
    6.42 +UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc_with_chcap) {
    6.43      
    6.44      UcxMempool *pool = ucx_mempool_new(1);
    6.45 -    
    6.46 +    UCX_TEST_BEGIN
    6.47      ucx_mempool_malloc(pool, sizeof(int));
    6.48      intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
    6.49      
    6.50 @@ -54,23 +51,22 @@
    6.51      
    6.52      UCX_TEST_ASSERT(*test == 5, "wrong pointer")
    6.53      
    6.54 +    UCX_TEST_END
    6.55      ucx_mempool_free(pool);
    6.56 -    
    6.57 -    UCX_TEST_END
    6.58  }
    6.59  
    6.60 -UCX_TEST_BEGIN(test_ucx_mempool_calloc) {
    6.61 +UCX_TEST_IMPLEMENT(test_ucx_mempool_calloc) {
    6.62      
    6.63      UcxMempool *pool = ucx_mempool_new(1);
    6.64 +    UCX_TEST_BEGIN
    6.65      
    6.66      intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
    6.67      
    6.68      UCX_TEST_ASSERT(test != NULL, "no memory for test data")
    6.69      UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed")
    6.70      
    6.71 +    UCX_TEST_END
    6.72      ucx_mempool_free(pool);
    6.73 -    
    6.74 -    UCX_TEST_END
    6.75  }
    6.76  
    6.77  void test_setdestr(void* elem) {
    6.78 @@ -78,14 +74,15 @@
    6.79      *cb = 42;
    6.80  }
    6.81  
    6.82 -UCX_TEST_BEGIN(test_ucx_mempool_set_destr) {
    6.83 +UCX_TEST_IMPLEMENT(test_ucx_mempool_set_destr) {
    6.84      
    6.85 +    intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
    6.86 +    UCX_TEST_BEGIN
    6.87      UcxMempool *pool = ucx_mempool_new(2);
    6.88      
    6.89      ucx_mempool_malloc(pool, sizeof(intptr_t));
    6.90      intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
    6.91      
    6.92 -    intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
    6.93      UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
    6.94      
    6.95      test[0] = 5; test[1] = (intptr_t) cb;
    6.96 @@ -100,20 +97,19 @@
    6.97      ucx_mempool_free(pool);
    6.98      
    6.99      UCX_TEST_ASSERT(*cb == 42, "destructor not called")
   6.100 -
   6.101 -    free(cb);
   6.102      
   6.103      UCX_TEST_END
   6.104 +    if (cb != NULL) free(cb);
   6.105  }
   6.106  
   6.107  
   6.108 -UCX_TEST_BEGIN(test_ucx_mempool_reg_destr) {
   6.109 +UCX_TEST_IMPLEMENT(test_ucx_mempool_reg_destr) {
   6.110      
   6.111 +    intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
   6.112 +    intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   6.113 +    UCX_TEST_BEGIN
   6.114      UcxMempool *pool = ucx_mempool_new(1);
   6.115      
   6.116 -    intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
   6.117 -    
   6.118 -    intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   6.119      UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
   6.120      
   6.121      test[0] = 5; test[1] = (intptr_t) cb;
   6.122 @@ -127,23 +123,22 @@
   6.123      UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed")
   6.124      
   6.125      ucx_mempool_free(pool);
   6.126 -    free(test);
   6.127 -    
   6.128      UCX_TEST_ASSERT(*cb == 42, "destructor not called")
   6.129 +    UCX_TEST_END
   6.130  
   6.131 -    free(cb);
   6.132 -    
   6.133 -    UCX_TEST_END
   6.134 +    if (test != NULL) free(test);
   6.135 +    if (cb != NULL) free(cb);
   6.136  }
   6.137  
   6.138 -UCX_TEST_BEGIN(test_ucx_mempool_realloc) {
   6.139 -    
   6.140 +UCX_TEST_IMPLEMENT(test_ucx_mempool_realloc) {
   6.141 +
   6.142 +    intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   6.143 +    UCX_TEST_BEGIN
   6.144      UcxMempool *pool = ucx_mempool_new(2);
   6.145      
   6.146      ucx_mempool_malloc(pool, sizeof(intptr_t));
   6.147      intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
   6.148 -    
   6.149 -    intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   6.150 +
   6.151      UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
   6.152      
   6.153      test[0] = 5; test[1] = (intptr_t) cb;
   6.154 @@ -167,8 +162,7 @@
   6.155      ucx_mempool_free(pool);
   6.156      
   6.157      UCX_TEST_ASSERT(*cb == 42, "destructor not called")
   6.158 -
   6.159 -    free(cb);
   6.160      
   6.161      UCX_TEST_END
   6.162 +    if (cb != NULL) free(cb);
   6.163  }
     7.1 --- a/ucx/test.h	Thu May 31 09:18:26 2012 +0200
     7.2 +++ b/ucx/test.h	Thu May 31 12:51:22 2012 +0200
     7.3 @@ -3,6 +3,31 @@
     7.4   * Author: Mike
     7.5   *
     7.6   * Created on 18. Februar 2012, 14:15
     7.7 + *
     7.8 + *
     7.9 + *
    7.10 + * Usage of this test framework:
    7.11 + *
    7.12 + * **** IN HEADER FILE: ****
    7.13 + *
    7.14 + * UCX_TEST_DECLARE(function_name)
    7.15 + *
    7.16 + * **** IN SOURCE FILE: ****
    7.17 + *
    7.18 + * UCX_TEST_IMPLEMENT(function_name) {
    7.19 + *  <memory allocation and other stuff here>
    7.20 + *  UCX_TEST_BEGIN
    7.21 + *  <tests with UCX_TEST_ASSERT here>
    7.22 + *  UCX_TEST_END
    7.23 + *  <cleanup of memory here>
    7.24 + * }
    7.25 + *
    7.26 + * PLEASE NOTE: if a test fails, a longjump is performed
    7.27 + * back to the UCX_TEST_BEGIN macro!
    7.28 + *
    7.29 + * You may use multiple BEGIN-END blocks if you are aware of the
    7.30 + * longjmp behaviour.
    7.31 + *
    7.32   */
    7.33  
    7.34  #ifndef TEST_H
    7.35 @@ -10,6 +35,7 @@
    7.36  
    7.37  #include <stdio.h>
    7.38  #include <string.h>
    7.39 +#include <setjmp.h>
    7.40  #include "list.h"
    7.41  
    7.42  #ifdef	__cplusplus
    7.43 @@ -31,16 +57,25 @@
    7.44  void ucx_test_run(UcxTestSuite*, FILE*);
    7.45  
    7.46  #define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *);
    7.47 -#define UCX_TEST_BEGIN(name) void name(UcxTestSuite* _suite_,FILE *_output_) {\
    7.48 -    fwrite("Running "#name"... ", 1, 12+strlen(#name), _output_);
    7.49 +#define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_)
    7.50 +
    7.51 +#define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\
    7.52 +        fwrite(__func__, 1, strlen(__func__), _output_);\
    7.53 +        fwrite("... ", 1, 4, _output_);\
    7.54 +        jmp_buf _env_; \
    7.55 +        if (!setjmp(_env_)) {
    7.56  
    7.57  #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \
    7.58          fwrite(message".\n", 1, 2+strlen(message), _output_); \
    7.59          _suite_->failure++; \
    7.60 -        return;\
    7.61 +        longjmp(_env_, 1);\
    7.62      }
    7.63  
    7.64 -#define UCX_TEST_END } fwrite("success.\n", 1, 9, _output_); _suite_->success++;
    7.65 +#define UCX_TEST_SUBROUTINE(name,data) void name(UcxTestSuite* _suite_,\
    7.66 +        FILE *_output_, jmp_buf _env_, void* data)
    7.67 +#define UCX_TEST_CALL_SUBROUTINE(name,data) name(_suite_,_output_,_env_,data);
    7.68 +
    7.69 +#define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;}
    7.70  
    7.71  #ifdef	__cplusplus
    7.72  }

mercurial