test/dlist_tests.c

Fri, 12 Oct 2012 10:54:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Oct 2012 10:54:55 +0200
changeset 69
fb59270b1de3
parent 40
583718dd4cf3
child 71
303dabadff1c
permissions
-rw-r--r--

made the code work with VC++ compiler (use make CONF=windows)

     1 /*
     2  * tests of dlist implementation
     3  */
     5 #include "dlist_tests.h"
     7 UCX_TEST_IMPLEMENT(test_ucx_dlist_append) {
     8     UcxDlist *list = ucx_dlist_append(NULL, "Hello");
     9     UCX_TEST_BEGIN
    11     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    12             "failed");
    14     list = ucx_dlist_append(list, " World!");
    16     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    17             "failed");
    18     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    19     UCX_TEST_END
    21     ucx_dlist_free(list);
    22 }
    24 UCX_TEST_IMPLEMENT(test_ucx_dlist_prepend) {
    25     UcxDlist *list = ucx_dlist_prepend(NULL, " World!");
    26     UCX_TEST_BEGIN
    28     list = ucx_dlist_prepend(list, "Hello");
    30     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    31             "failed");
    32     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    33             "failed");
    34     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    36     UCX_TEST_END
    37     ucx_dlist_free(list);
    38 }
    40 UCX_TEST_IMPLEMENT(test_ucx_dlist_equals) {
    41     UcxDlist *list = ucx_dlist_append(NULL, "Hello");
    42     list = ucx_dlist_append(list, " World!");
    43     UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!");
    44     list2 = ucx_dlist_prepend(list2, "Hello");
    45     UcxDlist *list3 = ucx_dlist_prepend(NULL, " Welt!");
    46     list3 = ucx_dlist_prepend(list3, "Hallo");
    47     UCX_TEST_BEGIN
    49     UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, cmp_string, NULL), "failed");
    50     UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, cmp_string, NULL), "failed");
    52     UCX_TEST_END
    53     ucx_dlist_free(list3);
    54     ucx_dlist_free(list2);
    55     ucx_dlist_free(list);
    56 }
    58 UCX_TEST_IMPLEMENT(test_ucx_dlist_concat) {
    59     UcxDlist *list = ucx_dlist_append(NULL, "Hello");
    60     UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!");
    61     UCX_TEST_BEGIN
    63     list = ucx_dlist_concat(list, list2);
    65     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    66             "failed");
    67     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    68             "failed");
    69     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    71     UCX_TEST_END
    72     ucx_dlist_free(list);
    73 }
    75 UCX_TEST_IMPLEMENT(test_ucx_dlist_size) {
    76     UcxDlist *list = ucx_dlist_append(NULL, "This ");
    77     UCX_TEST_BEGIN
    78     list = ucx_dlist_append(list, "list ");
    79     list = ucx_dlist_append(list, "has ");
    80     list = ucx_dlist_append(list, "size ");
    81     list = ucx_dlist_append(list, "5!");
    83     UCX_TEST_ASSERT(ucx_dlist_size(list) == 5, "failed");
    85     UCX_TEST_END
    86     ucx_dlist_free(list);
    87 }
    89 UCX_TEST_IMPLEMENT(test_ucx_dlist_first) {
    90     UcxDlist *list = ucx_dlist_append(NULL, "Find ");
    91     UCX_TEST_BEGIN
    92     list = ucx_dlist_append(list, "the ");
    93     list = ucx_dlist_append(list, "first!");
    95     const char* first = (const char*) (ucx_dlist_first(list)->data);
    97     UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
    99     UCX_TEST_END
   100     ucx_dlist_free(list);
   101 }
   103 UCX_TEST_IMPLEMENT(test_ucx_dlist_last) {
   104     UcxDlist *list = ucx_dlist_append(NULL, "Find ");
   105     UCX_TEST_BEGIN
   106     list = ucx_dlist_append(list, "the ");
   107     list = ucx_dlist_append(list, "last!");
   109     const char* last = (const char*) (ucx_dlist_last(list)->data);
   111     UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
   113     UCX_TEST_END
   114     ucx_dlist_free(list);
   115 }
   117 UCX_TEST_IMPLEMENT(test_ucx_dlist_get) {
   118     UcxDlist *list = ucx_dlist_append(NULL, "Find ");
   119     UCX_TEST_BEGIN
   120     list = ucx_dlist_append(list, "the ");
   121     list = ucx_dlist_append(list, "mid!");
   123     const char* mid = (const char*) (ucx_dlist_get(list, 1)->data);
   125     UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
   127     UCX_TEST_END
   128     ucx_dlist_free(list);
   129 }
   131 UCX_TEST_IMPLEMENT(test_ucx_dlist_remove) {
   132     UcxDlist *list = ucx_dlist_append(NULL, "Hello");
   133     UCX_TEST_BEGIN
   134     list = ucx_dlist_append(list, " fucking");
   135     list = ucx_dlist_append(list, " World!");
   137     list = ucx_dlist_remove(list, ucx_dlist_get(list, 1));
   139     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
   140             "failed");
   141     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
   142             "failed");
   143     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
   145     UCX_TEST_END
   146     ucx_dlist_free(list);
   147 }
   149 UCX_TEST_IMPLEMENT(test_ucx_dlist_clone) {
   151     char *hello = (char*)malloc(6);
   152     char *world = (char*)malloc(8);
   154     memcpy(hello, "Hello", 6);
   155     memcpy(world, " World!", 8);
   157     UcxDlist *list = ucx_dlist_append(NULL, hello);
   158     list = ucx_dlist_append(list, world);
   160     UcxDlist *copy = ucx_dlist_clone(list, copy_string, NULL);
   161     UCX_TEST_BEGIN
   163     UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, cmp_string, NULL), "failed");
   164     UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
   165     UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
   167     UCX_TEST_END
   168     free(copy->next->data);
   169     free(copy->data);
   171     free(world);
   172     free(hello);
   173     ucx_dlist_free(list);
   174     ucx_dlist_free(copy);
   175 }
   177 UCX_TEST_IMPLEMENT(test_ucx_dlist_sort) {
   178     UcxDlist *list = ucx_dlist_append(NULL, "this");
   179     list = ucx_dlist_append(list, "is");
   180     list = ucx_dlist_append(list, "a");
   181     list = ucx_dlist_append(list, "test");
   182     list = ucx_dlist_append(list, "for");
   183     list = ucx_dlist_append(list, "partial");
   184     list = ucx_dlist_append(list, "correctness");
   186     UcxDlist *expected = ucx_dlist_append(NULL, "a");
   187     expected = ucx_dlist_append(expected, "correctness");
   188     expected = ucx_dlist_append(expected, "for");
   189     expected = ucx_dlist_append(expected, "is");
   190     expected = ucx_dlist_append(expected, "partial");
   191     expected = ucx_dlist_append(expected, "test");
   192     expected = ucx_dlist_append(expected, "this");
   194     list = ucx_dlist_sort(list, cmp_string, NULL);
   196     UCX_TEST_BEGIN
   197     UCX_TEST_ASSERT(
   198             ucx_dlist_equals(list, expected, cmp_string, NULL), "failed");
   199     UcxDlist *l = list;
   200     UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
   201     while (l->next != NULL) {
   202         UCX_TEST_ASSERT(l->next->prev == l, "prev pointer corrupted");
   203         l = l->next;
   204     }
   205     UCX_TEST_END
   207     ucx_dlist_free(expected);
   208     ucx_dlist_free(list);
   209 }

mercurial