1.1 --- a/test/list_tests.c Sat Feb 18 15:50:43 2012 +0100 1.2 +++ b/test/list_tests.c Sat Feb 18 18:36:30 2012 +0100 1.3 @@ -2,177 +2,152 @@ 1.4 * tests of list implementation 1.5 */ 1.6 1.7 -#include <stdio.h> 1.8 -#include <stdlib.h> 1.9 +#include "list_tests.h" 1.10 1.11 -#include "ucx/list.h" 1.12 -#include "ucx/dlist.h" 1.13 - 1.14 -struct foreach_testdata { 1.15 - int values[3]; 1.16 - int i; 1.17 -}; 1.18 - 1.19 -void* int_cpy(void* source, void* data) { 1.20 - void *dest = malloc(sizeof(int)); 1.21 - if (dest != NULL) { 1.22 - *((int*)dest) = *((int*)source); 1.23 - } 1.24 - return dest; 1.25 +UCX_TEST_BEGIN(test_ucx_list_append) { 1.26 + UcxList *list = ucx_list_append(NULL, "Hello"); 1.27 + 1.28 + UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") 1.29 + 1.30 + list = ucx_list_append(list, " World!"); 1.31 + 1.32 + UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") 1.33 + UCX_TEST_ASSERT(list->next->next == NULL, "failed") 1.34 + 1.35 + ucx_list_free(list); 1.36 + 1.37 + UCX_TEST_END 1.38 } 1.39 1.40 -int int_cmp(void* e1, void *e2, void *data) { 1.41 - if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1; 1.42 - 1.43 - int *i1 = (int*)e1, *i2 = (int*)e2; 1.44 - int r = (*i1) - (*i2); 1.45 - return (r < 0) ? -1 : (r == 0 ? 0 : 1); 1.46 +UCX_TEST_BEGIN(test_ucx_list_prepend) { 1.47 + UcxList *list = ucx_list_prepend(NULL, " World!"); 1.48 + list = ucx_list_prepend(list, "Hello"); 1.49 + 1.50 + UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") 1.51 + UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") 1.52 + UCX_TEST_ASSERT(list->next->next == NULL, "failed") 1.53 + 1.54 + ucx_list_free(list); 1.55 + 1.56 + UCX_TEST_END 1.57 } 1.58 1.59 -int dlist_tests_foreach(void *v, void *custom) { 1.60 - UcxDlist *dl = (UcxDlist*)v; 1.61 - struct foreach_testdata *tdata = (struct foreach_testdata*)custom; 1.62 - 1.63 - tdata->values[tdata->i] = *(int*)dl->data; 1.64 - tdata->i++; 1.65 - 1.66 - return 0; 1.67 +UCX_TEST_BEGIN(test_ucx_list_equals) { 1.68 + UcxList *list = ucx_list_append(NULL, "Hello"); 1.69 + list = ucx_list_append(list, " World!"); 1.70 + UcxList *list2 = ucx_list_prepend(NULL, " World!"); 1.71 + list2 = ucx_list_prepend(list2, "Hello"); 1.72 + UcxList *list3 = ucx_list_prepend(NULL, " Welt!"); 1.73 + list3 = ucx_list_prepend(list3, "Hallo"); 1.74 + 1.75 + UCX_TEST_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed") 1.76 + UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, NULL), "failed") 1.77 + 1.78 + ucx_list_free(list3); 1.79 + ucx_list_free(list2); 1.80 + ucx_list_free(list); 1.81 + 1.82 + UCX_TEST_END 1.83 } 1.84 1.85 -int dlist_free_content(void *v, void *custom) { 1.86 - UcxDlist *dl = (UcxDlist*)v; 1.87 - free(dl->data); 1.88 - return 0; 1.89 +UCX_TEST_BEGIN(test_ucx_list_concat) { 1.90 + UcxList *list = ucx_list_append(NULL, "Hello"); 1.91 + UcxList *list2 = ucx_list_prepend(NULL, " World!"); 1.92 + 1.93 + list = ucx_list_concat(list, list2); 1.94 + 1.95 + UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") 1.96 + UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") 1.97 + UCX_TEST_ASSERT(list->next->next == NULL, "failed") 1.98 + 1.99 + ucx_list_free(list2); 1.100 + ucx_list_free(list); 1.101 + 1.102 + UCX_TEST_END 1.103 } 1.104 1.105 -int dlist_tests() { 1.106 - int r = 0; 1.107 - int v[8]; 1.108 - UcxDlist *dl = NULL; 1.109 - // build list 0,1,2,3,4,5,6,7 1.110 - printf(" Test ucx_dlist_append\n"); 1.111 - fflush(stdout); 1.112 - for(int i=0;i<8;i++) { 1.113 - v[i] = i; 1.114 - dl = ucx_dlist_append(dl, &v[i]); 1.115 - } 1.116 - 1.117 - printf(" Test ucx_dlist_get\n"); 1.118 - fflush(stdout); 1.119 - for(int i=0;i<8;i++) { 1.120 - UcxDlist *elm = ucx_dlist_get(dl, i); 1.121 - if(elm == NULL) { 1.122 - fprintf(stderr, "ucx_dlist_get failed: element is NULL\n"); 1.123 - r--; 1.124 - } else if(elm->data == NULL) { 1.125 - fprintf(stderr, "ucx_dlist_get failed: data is NULL\n"); 1.126 - r--; 1.127 - } else { 1.128 - int *data = (int*)elm->data; 1.129 - if(*data != i) { 1.130 - fprintf(stderr, "ucx_dlist_get failed with index %d\n", i); 1.131 - r--; 1.132 - } 1.133 - } 1.134 - } 1.135 - 1.136 - printf(" Test ucx_dlist_free\n"); 1.137 - fflush(stdout); 1.138 - ucx_dlist_free(dl); 1.139 - 1.140 - dl = NULL; 1.141 - // build list 4,0,4 1.142 - printf(" Test ucx_dlist_prepend\n"); 1.143 - dl = ucx_dlist_prepend(dl, &v[0]); 1.144 - dl = ucx_dlist_prepend(dl, &v[4]); 1.145 - dl = ucx_dlist_append(dl, &v[4]); 1.146 - 1.147 - struct foreach_testdata tdata; 1.148 - tdata.i = 0; 1.149 - ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata); 1.150 - 1.151 - if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { 1.152 - fprintf(stderr, "prepend/append test failed\n"); 1.153 - fprintf(stderr, "content: [%d, %d, %d]\n", 1.154 - tdata.values[0], tdata.values[1], tdata.values[2]); 1.155 - r--; 1.156 - } 1.157 - 1.158 - printf(" Test ucx_dlist_equals\n"); 1.159 - UcxDlist *dl2 = NULL; 1.160 - dl2 = ucx_dlist_append(dl2, &v[4]); 1.161 - dl2 = ucx_dlist_append(dl2, &v[0]); 1.162 - dl2 = ucx_dlist_append(dl2, &v[4]); 1.163 - if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { 1.164 - fprintf(stderr, "ucx_dlist_equals failed (false negative)\n"); 1.165 - r--; 1.166 - } 1.167 - dl2->next->data = NULL; 1.168 - if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { 1.169 - fprintf(stderr, "ucx_dlist_equals failed (false positive)\n"); 1.170 - r--; 1.171 - } 1.172 - dl2->next->data = &(tdata.values[1]); 1.173 - if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) { 1.174 - fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n"); 1.175 - r--; 1.176 - } 1.177 - if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { 1.178 - fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n"); 1.179 - r--; 1.180 - } 1.181 - ucx_dlist_free(dl2); 1.182 - 1.183 - printf(" Test ucx_dlist_clone\n"); 1.184 - dl2 = ucx_dlist_clone(dl, NULL, NULL); 1.185 - if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { 1.186 - fprintf(stderr, "ucx_dlist_clone (without copy) failed\n"); 1.187 - r--; 1.188 - } 1.189 - ucx_dlist_free(dl2); 1.190 - 1.191 - printf(" Test ucx_dlist_clone with copy\n"); 1.192 - dl2 = ucx_dlist_clone(dl, int_cpy, NULL); 1.193 - if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { 1.194 - if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) { 1.195 - fprintf(stderr, "ucx_dlist_clone (with copy) failed (compare)\n"); 1.196 - r--; 1.197 - } 1.198 - } else { 1.199 - fprintf(stderr, "ucx_dlist_clone (with copy) failed (identity)\n"); 1.200 - r--; 1.201 - } 1.202 - ucx_dlist_foreach(dl, dlist_free_content, NULL); 1.203 - ucx_dlist_free(dl2); 1.204 - 1.205 - ucx_dlist_free(dl); 1.206 - 1.207 - dl = NULL; 1.208 - printf(" Test ucx_dlist_remove\n"); 1.209 - dl = ucx_dlist_append(dl, &v[4]); 1.210 - dl = ucx_dlist_append(dl, &v[0]); 1.211 - dl = ucx_dlist_append(dl, &v[3]); 1.212 - dl = ucx_dlist_remove(dl, dl->next); 1.213 - if (ucx_dlist_size(dl) == 2) { 1.214 - if ((*((int*)(dl->data)) != 4) || (*((int*)(dl->next->data)) != 3)) { 1.215 - fprintf(stderr, "ucx_dlist_remove failed (wrong data)\n"); 1.216 - r--; 1.217 - } 1.218 - } else { 1.219 - fprintf(stderr, "ucx_dlist_remove failed (wrong size)\n"); 1.220 - r--; 1.221 - } 1.222 - dl = ucx_dlist_remove(dl, dl); 1.223 - if (ucx_dlist_size(dl) == 1) { 1.224 - if ((*((int*)(dl->data)) != 3)) { 1.225 - fprintf(stderr, "ucx_dlist_remove first failed (wrong data)\n"); 1.226 - r--; 1.227 - } 1.228 - } else { 1.229 - fprintf(stderr, "ucx_dlist_remove first failed (wrong size)\n"); 1.230 - r--; 1.231 - } 1.232 - 1.233 - return r; 1.234 +UCX_TEST_BEGIN(test_ucx_list_size) { 1.235 + UcxList *list = ucx_list_append(NULL, "This "); 1.236 + list = ucx_list_append(list, "list "); 1.237 + list = ucx_list_append(list, "has "); 1.238 + list = ucx_list_append(list, "size "); 1.239 + list = ucx_list_append(list, "5!"); 1.240 + 1.241 + UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed"); 1.242 + 1.243 + ucx_list_free(list); 1.244 + 1.245 + UCX_TEST_END 1.246 } 1.247 1.248 +UCX_TEST_BEGIN(test_ucx_list_last) { 1.249 + UcxList *list = ucx_list_append(NULL, "Find "); 1.250 + list = ucx_list_append(list, "the "); 1.251 + list = ucx_list_append(list, "last!"); 1.252 + 1.253 + char* last = (char*) (ucx_list_last(list)->data); 1.254 + 1.255 + UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed"); 1.256 + 1.257 + ucx_list_free(list); 1.258 + 1.259 + UCX_TEST_END 1.260 +} 1.261 + 1.262 +UCX_TEST_BEGIN(test_ucx_list_get) { 1.263 + UcxList *list = ucx_list_append(NULL, "Find "); 1.264 + list = ucx_list_append(list, "the "); 1.265 + list = ucx_list_append(list, "mid!"); 1.266 + 1.267 + char* mid = (char*) (ucx_list_get(list, 1)->data); 1.268 + 1.269 + UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed"); 1.270 + 1.271 + ucx_list_free(list); 1.272 + 1.273 + UCX_TEST_END 1.274 +} 1.275 + 1.276 +UCX_TEST_BEGIN(test_ucx_list_remove) { 1.277 + UcxList *list = ucx_list_append(NULL, "Hello"); 1.278 + list = ucx_list_append(list, " fucking"); 1.279 + list = ucx_list_append(list, " World!"); 1.280 + 1.281 + list = ucx_list_remove(list, ucx_list_get(list, 1)); 1.282 + 1.283 + UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed") 1.284 + UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed") 1.285 + UCX_TEST_ASSERT(list->next->next == NULL, "failed") 1.286 + 1.287 + ucx_list_free(list); 1.288 + 1.289 + UCX_TEST_END 1.290 +} 1.291 + 1.292 +UCX_TEST_BEGIN(test_ucx_list_clone) { 1.293 + 1.294 + char *hello = (char*)malloc(6); 1.295 + char *world = (char*)malloc(8); 1.296 + 1.297 + memcpy(hello, "Hello", 6); 1.298 + memcpy(world, " World!", 8); 1.299 + 1.300 + UcxList *list = ucx_list_append(NULL, hello); 1.301 + list = ucx_list_append(list, world); 1.302 + 1.303 + UcxList *copy = ucx_list_clone(list, copy_string, NULL); 1.304 + 1.305 + UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed") 1.306 + UCX_TEST_ASSERT(hello != copy->data, "first element is no copy") 1.307 + UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy") 1.308 + 1.309 + free(copy->next->data); 1.310 + free(copy->data); 1.311 + 1.312 + free(world); 1.313 + free(hello); 1.314 + free(list); 1.315 + free(copy); 1.316 + 1.317 + UCX_TEST_END 1.318 +}