diff -r 59f147baea31 -r 22644e2572bc test/list_tests.c --- a/test/list_tests.c Sat Feb 18 15:50:43 2012 +0100 +++ b/test/list_tests.c Sat Feb 18 18:36:30 2012 +0100 @@ -2,177 +2,152 @@ * tests of list implementation */ -#include -#include +#include "list_tests.h" -#include "ucx/list.h" -#include "ucx/dlist.h" - -struct foreach_testdata { - int values[3]; - int i; -}; - -void* int_cpy(void* source, void* data) { - void *dest = malloc(sizeof(int)); - if (dest != NULL) { - *((int*)dest) = *((int*)source); - } - return dest; +UCX_TEST_BEGIN(test_ucx_list_append) { + UcxList *list = ucx_list_append(NULL, "Hello"); + + 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_list_free(list); + + UCX_TEST_END } -int int_cmp(void* e1, void *e2, void *data) { - if (e1 == NULL || e2 == NULL) return (e1 == e2) ? 0 : -1; - - int *i1 = (int*)e1, *i2 = (int*)e2; - int r = (*i1) - (*i2); - return (r < 0) ? -1 : (r == 0 ? 0 : 1); +UCX_TEST_BEGIN(test_ucx_list_prepend) { + UcxList *list = ucx_list_prepend(NULL, " World!"); + 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_list_free(list); + + UCX_TEST_END } -int dlist_tests_foreach(void *v, void *custom) { - UcxDlist *dl = (UcxDlist*)v; - struct foreach_testdata *tdata = (struct foreach_testdata*)custom; - - tdata->values[tdata->i] = *(int*)dl->data; - tdata->i++; - - return 0; +UCX_TEST_BEGIN(test_ucx_list_equals) { + UcxList *list = ucx_list_append(NULL, "Hello"); + list = ucx_list_append(list, " World!"); + UcxList *list2 = ucx_list_prepend(NULL, " World!"); + list2 = ucx_list_prepend(list2, "Hello"); + UcxList *list3 = ucx_list_prepend(NULL, " Welt!"); + list3 = ucx_list_prepend(list3, "Hallo"); + + 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_list_free(list3); + ucx_list_free(list2); + ucx_list_free(list); + + UCX_TEST_END } -int dlist_free_content(void *v, void *custom) { - UcxDlist *dl = (UcxDlist*)v; - free(dl->data); - return 0; +UCX_TEST_BEGIN(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_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_list_free(list2); + ucx_list_free(list); + + UCX_TEST_END } -int dlist_tests() { - int r = 0; - int v[8]; - UcxDlist *dl = NULL; - // build list 0,1,2,3,4,5,6,7 - printf(" Test ucx_dlist_append\n"); - fflush(stdout); - for(int i=0;i<8;i++) { - v[i] = i; - dl = ucx_dlist_append(dl, &v[i]); - } - - printf(" Test ucx_dlist_get\n"); - fflush(stdout); - for(int i=0;i<8;i++) { - UcxDlist *elm = ucx_dlist_get(dl, i); - if(elm == NULL) { - fprintf(stderr, "ucx_dlist_get failed: element is NULL\n"); - r--; - } else if(elm->data == NULL) { - fprintf(stderr, "ucx_dlist_get failed: data is NULL\n"); - r--; - } else { - int *data = (int*)elm->data; - if(*data != i) { - fprintf(stderr, "ucx_dlist_get failed with index %d\n", i); - r--; - } - } - } - - printf(" Test ucx_dlist_free\n"); - fflush(stdout); - ucx_dlist_free(dl); - - dl = NULL; - // build list 4,0,4 - printf(" Test ucx_dlist_prepend\n"); - dl = ucx_dlist_prepend(dl, &v[0]); - dl = ucx_dlist_prepend(dl, &v[4]); - dl = ucx_dlist_append(dl, &v[4]); - - struct foreach_testdata tdata; - tdata.i = 0; - ucx_dlist_foreach(dl, dlist_tests_foreach, &tdata); - - if(tdata.values[0] != 4 || tdata.values[1] != 0 || tdata.values[2] != 4) { - fprintf(stderr, "prepend/append test failed\n"); - fprintf(stderr, "content: [%d, %d, %d]\n", - tdata.values[0], tdata.values[1], tdata.values[2]); - r--; - } - - printf(" Test ucx_dlist_equals\n"); - UcxDlist *dl2 = NULL; - dl2 = ucx_dlist_append(dl2, &v[4]); - dl2 = ucx_dlist_append(dl2, &v[0]); - dl2 = ucx_dlist_append(dl2, &v[4]); - if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { - fprintf(stderr, "ucx_dlist_equals failed (false negative)\n"); - r--; - } - dl2->next->data = NULL; - if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { - fprintf(stderr, "ucx_dlist_equals failed (false positive)\n"); - r--; - } - dl2->next->data = &(tdata.values[1]); - if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) { - fprintf(stderr, "ucx_dlist_equals failed (cmp_func false negative)\n"); - r--; - } - if (ucx_dlist_equals(dl, dl2, NULL, NULL)) { - fprintf(stderr, "ucx_dlist_equals failed (cmp_func false positive)\n"); - r--; - } - ucx_dlist_free(dl2); - - printf(" Test ucx_dlist_clone\n"); - dl2 = ucx_dlist_clone(dl, NULL, NULL); - if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { - fprintf(stderr, "ucx_dlist_clone (without copy) failed\n"); - r--; - } - ucx_dlist_free(dl2); - - printf(" Test ucx_dlist_clone with copy\n"); - dl2 = ucx_dlist_clone(dl, int_cpy, NULL); - if (!ucx_dlist_equals(dl, dl2, NULL, NULL)) { - if (!ucx_dlist_equals(dl, dl2, int_cmp, NULL)) { - fprintf(stderr, "ucx_dlist_clone (with copy) failed (compare)\n"); - r--; - } - } else { - fprintf(stderr, "ucx_dlist_clone (with copy) failed (identity)\n"); - r--; - } - ucx_dlist_foreach(dl, dlist_free_content, NULL); - ucx_dlist_free(dl2); - - ucx_dlist_free(dl); - - dl = NULL; - printf(" Test ucx_dlist_remove\n"); - dl = ucx_dlist_append(dl, &v[4]); - dl = ucx_dlist_append(dl, &v[0]); - dl = ucx_dlist_append(dl, &v[3]); - dl = ucx_dlist_remove(dl, dl->next); - if (ucx_dlist_size(dl) == 2) { - if ((*((int*)(dl->data)) != 4) || (*((int*)(dl->next->data)) != 3)) { - fprintf(stderr, "ucx_dlist_remove failed (wrong data)\n"); - r--; - } - } else { - fprintf(stderr, "ucx_dlist_remove failed (wrong size)\n"); - r--; - } - dl = ucx_dlist_remove(dl, dl); - if (ucx_dlist_size(dl) == 1) { - if ((*((int*)(dl->data)) != 3)) { - fprintf(stderr, "ucx_dlist_remove first failed (wrong data)\n"); - r--; - } - } else { - fprintf(stderr, "ucx_dlist_remove first failed (wrong size)\n"); - r--; - } - - return r; +UCX_TEST_BEGIN(test_ucx_list_size) { + UcxList *list = ucx_list_append(NULL, "This "); + list = ucx_list_append(list, "list "); + list = ucx_list_append(list, "has "); + list = ucx_list_append(list, "size "); + list = ucx_list_append(list, "5!"); + + UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed"); + + ucx_list_free(list); + + UCX_TEST_END } +UCX_TEST_BEGIN(test_ucx_list_last) { + UcxList *list = ucx_list_append(NULL, "Find "); + list = ucx_list_append(list, "the "); + list = ucx_list_append(list, "last!"); + + char* last = (char*) (ucx_list_last(list)->data); + + UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed"); + + ucx_list_free(list); + + UCX_TEST_END +} + +UCX_TEST_BEGIN(test_ucx_list_get) { + UcxList *list = ucx_list_append(NULL, "Find "); + list = ucx_list_append(list, "the "); + list = ucx_list_append(list, "mid!"); + + char* mid = (char*) (ucx_list_get(list, 1)->data); + + UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed"); + + ucx_list_free(list); + + UCX_TEST_END +} + +UCX_TEST_BEGIN(test_ucx_list_remove) { + UcxList *list = ucx_list_append(NULL, "Hello"); + list = ucx_list_append(list, " fucking"); + list = ucx_list_append(list, " World!"); + + list = ucx_list_remove(list, ucx_list_get(list, 1)); + + 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_list_free(list); + + UCX_TEST_END +} + +UCX_TEST_BEGIN(test_ucx_list_clone) { + + char *hello = (char*)malloc(6); + char *world = (char*)malloc(8); + + memcpy(hello, "Hello", 6); + memcpy(world, " World!", 8); + + UcxList *list = ucx_list_append(NULL, hello); + list = ucx_list_append(list, world); + + UcxList *copy = ucx_list_clone(list, copy_string, NULL); + + 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") + + free(copy->next->data); + free(copy->data); + + free(world); + free(hello); + free(list); + free(copy); + + UCX_TEST_END +}