test/dlist_tests.c

Wed, 27 Feb 2013 10:09:23 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 27 Feb 2013 10:09:23 +0100
changeset 89
47f7fdbddb62
parent 71
303dabadff1c
child 90
ef3163857e88
permissions
-rw-r--r--

comparator module

     1 /*
     2  * tests of dlist implementation
     3  */
     5 #include "dlist_tests.h"
     6 #include "ucx/comparator.h"
     8 UCX_TEST_IMPLEMENT(test_ucx_dlist_append) {
     9     UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello");
    10     UCX_TEST_BEGIN
    12     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    13             "failed");
    15     list = ucx_dlist_append(list, (void*)" World!");
    17     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    18             "failed");
    19     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    20     UCX_TEST_END
    22     ucx_dlist_free(list);
    23 }
    25 UCX_TEST_IMPLEMENT(test_ucx_dlist_prepend) {
    26     UcxDlist *list = ucx_dlist_prepend(NULL, (void*)" World!");
    27     UCX_TEST_BEGIN
    29     list = ucx_dlist_prepend(list, (void*)"Hello");
    31     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    32             "failed");
    33     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    34             "failed");
    35     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    37     UCX_TEST_END
    38     ucx_dlist_free(list);
    39 }
    41 UCX_TEST_IMPLEMENT(test_ucx_dlist_equals) {
    42     UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello");
    43     list = ucx_dlist_append(list, (void*)" World!");
    44     UcxDlist *list2 = ucx_dlist_prepend(NULL, (void*)" World!");
    45     list2 = ucx_dlist_prepend(list2, (void*)"Hello");
    46     UcxDlist *list3 = ucx_dlist_prepend(NULL, (void*)" Welt!");
    47     list3 = ucx_dlist_prepend(list3, (void*)"Hallo");
    48     UCX_TEST_BEGIN
    50     UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, ucx_strcmp, NULL), "failed");
    51     UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, ucx_strcmp, NULL), "failed");
    53     UCX_TEST_END
    54     ucx_dlist_free(list3);
    55     ucx_dlist_free(list2);
    56     ucx_dlist_free(list);
    57 }
    59 UCX_TEST_IMPLEMENT(test_ucx_dlist_concat) {
    60     UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello");
    61     UcxDlist *list2 = ucx_dlist_prepend(NULL, (void*)" World!");
    62     UCX_TEST_BEGIN
    64     list = ucx_dlist_concat(list, list2);
    66     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    67             "failed");
    68     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    69             "failed");
    70     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    72     UCX_TEST_END
    73     ucx_dlist_free(list);
    74 }
    76 UCX_TEST_IMPLEMENT(test_ucx_dlist_size) {
    77     UcxDlist *list = ucx_dlist_append(NULL, (void*)"This ");
    78     UCX_TEST_BEGIN
    79     list = ucx_dlist_append(list, (void*)"list ");
    80     list = ucx_dlist_append(list, (void*)"has ");
    81     list = ucx_dlist_append(list, (void*)"size ");
    82     list = ucx_dlist_append(list, (void*)"5!");
    84     UCX_TEST_ASSERT(ucx_dlist_size(list) == 5, "failed");
    86     UCX_TEST_END
    87     ucx_dlist_free(list);
    88 }
    90 UCX_TEST_IMPLEMENT(test_ucx_dlist_first) {
    91     UcxDlist *list = ucx_dlist_append(NULL, (void*)"Find ");
    92     UCX_TEST_BEGIN
    93     list = ucx_dlist_append(list, (void*)"the ");
    94     list = ucx_dlist_append(list, (void*)"first!");
    96     const char* first = (const char*) (ucx_dlist_first(list)->data);
    98     UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
   100     UCX_TEST_END
   101     ucx_dlist_free(list);
   102 }
   104 UCX_TEST_IMPLEMENT(test_ucx_dlist_last) {
   105     UcxDlist *list = ucx_dlist_append(NULL, (void*)"Find ");
   106     UCX_TEST_BEGIN
   107     list = ucx_dlist_append(list, (void*)"the ");
   108     list = ucx_dlist_append(list, (void*)"last!");
   110     const char* last = (const char*) (ucx_dlist_last(list)->data);
   112     UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
   114     UCX_TEST_END
   115     ucx_dlist_free(list);
   116 }
   118 UCX_TEST_IMPLEMENT(test_ucx_dlist_get) {
   119     UcxDlist *list = ucx_dlist_append(NULL, (void*)"Find ");
   120     UCX_TEST_BEGIN
   121     list = ucx_dlist_append(list, (void*)"the ");
   122     list = ucx_dlist_append(list, (void*)"mid!");
   124     const char* mid = (const char*) (ucx_dlist_get(list, 1)->data);
   126     UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
   128     UCX_TEST_END
   129     ucx_dlist_free(list);
   130 }
   132 UCX_TEST_IMPLEMENT(test_ucx_dlist_remove) {
   133     UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello");
   134     UCX_TEST_BEGIN
   135     list = ucx_dlist_append(list, (void*)" fucking");
   136     list = ucx_dlist_append(list, (void*)" World!");
   138     list = ucx_dlist_remove(list, ucx_dlist_get(list, 1));
   140     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
   141             "failed");
   142     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
   143             "failed");
   144     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
   146     UCX_TEST_END
   147     ucx_dlist_free(list);
   148 }
   150 UCX_TEST_IMPLEMENT(test_ucx_dlist_clone) {
   152     char *hello = (char*)malloc(6);
   153     char *world = (char*)malloc(8);
   155     memcpy(hello, "Hello", 6);
   156     memcpy(world, " World!", 8);
   158     UcxDlist *list = ucx_dlist_append(NULL, hello);
   159     list = ucx_dlist_append(list, world);
   161     UcxDlist *copy = ucx_dlist_clone(list, copy_string, NULL);
   162     UCX_TEST_BEGIN
   164     UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, ucx_strcmp, NULL), "failed");
   165     UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
   166     UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
   168     UCX_TEST_END
   169     free(copy->next->data);
   170     free(copy->data);
   172     free(world);
   173     free(hello);
   174     ucx_dlist_free(list);
   175     ucx_dlist_free(copy);
   176 }
   178 UCX_TEST_IMPLEMENT(test_ucx_dlist_sort) {
   179     UcxDlist *list = ucx_dlist_append(NULL, (void*)"this");
   180     list = ucx_dlist_append(list, (void*)"is");
   181     list = ucx_dlist_append(list, (void*)"a");
   182     list = ucx_dlist_append(list, (void*)"test");
   183     list = ucx_dlist_append(list, (void*)"for");
   184     list = ucx_dlist_append(list, (void*)"partial");
   185     list = ucx_dlist_append(list, (void*)"correctness");
   187     UcxDlist *expected = ucx_dlist_append(NULL, (void*)"a");
   188     expected = ucx_dlist_append(expected, (void*)"correctness");
   189     expected = ucx_dlist_append(expected, (void*)"for");
   190     expected = ucx_dlist_append(expected, (void*)"is");
   191     expected = ucx_dlist_append(expected, (void*)"partial");
   192     expected = ucx_dlist_append(expected, (void*)"test");
   193     expected = ucx_dlist_append(expected, (void*)"this");
   195     list = ucx_dlist_sort(list, ucx_strcmp, NULL);
   197     UCX_TEST_BEGIN
   198     UCX_TEST_ASSERT(
   199             ucx_dlist_equals(list, expected, ucx_strcmp, NULL), "failed");
   200     UcxDlist *l = list;
   201     UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
   202     while (l->next != NULL) {
   203         UCX_TEST_ASSERT(l->next->prev == l, "prev pointer corrupted");
   204         l = l->next;
   205     }
   206     UCX_TEST_END
   208     ucx_dlist_free(expected);
   209     ucx_dlist_free(list);
   210 }

mercurial