test/list_tests.c

Mon, 22 Jul 2013 11:53:39 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 22 Jul 2013 11:53:39 +0200
changeset 122
540d99722f1f
parent 103
test/dlist_tests.c@08018864fb91
child 123
7fb0f74517c5
permissions
-rw-r--r--

removal of single linked list implemenation - step 2: renamed UcxDlist to UcxList (new single implementation)

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013 Olaf Wintermann. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "list_tests.h"
    30 #include "ucx/utils.h"
    32 UCX_TEST_IMPLEMENT(test_ucx_list_append) {
    33     UcxList *list = ucx_list_append(NULL, (void*)"Hello");
    34     UCX_TEST_BEGIN
    36     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    37             "failed");
    39     list = ucx_list_append(list, (void*)" World!");
    41     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    42             "failed");
    43     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    44     UCX_TEST_END
    46     ucx_list_free(list);
    47 }
    49 UCX_TEST_IMPLEMENT(test_ucx_list_prepend) {
    50     UcxList *list = ucx_list_prepend(NULL, (void*)" World!");
    51     UCX_TEST_BEGIN
    53     list = ucx_list_prepend(list, (void*)"Hello");
    55     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    56             "failed");
    57     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    58             "failed");
    59     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    61     UCX_TEST_END
    62     ucx_list_free(list);
    63 }
    65 UCX_TEST_IMPLEMENT(test_ucx_list_equals) {
    66     UcxList *list = ucx_list_append(NULL, (void*)"Hello");
    67     list = ucx_list_append(list, (void*)" World!");
    68     UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
    69     list2 = ucx_list_prepend(list2, (void*)"Hello");
    70     UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!");
    71     list3 = ucx_list_prepend(list3, (void*)"Hallo");
    72     UCX_TEST_BEGIN
    74     UCX_TEST_ASSERT(ucx_list_equals(list, list2, ucx_strcmp, NULL), "failed");
    75     UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
    77     UCX_TEST_END
    78     ucx_list_free(list3);
    79     ucx_list_free(list2);
    80     ucx_list_free(list);
    81 }
    83 UCX_TEST_IMPLEMENT(test_ucx_list_concat) {
    84     UcxList *list = ucx_list_append(NULL, (void*)"Hello");
    85     UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
    86     UCX_TEST_BEGIN
    88     list = ucx_list_concat(list, list2);
    90     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    91             "failed");
    92     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    93             "failed");
    94     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    96     UCX_TEST_END
    97     ucx_list_free(list);
    98 }
   100 UCX_TEST_IMPLEMENT(test_ucx_list_size) {
   101     UcxList *list = ucx_list_append(NULL, (void*)"This ");
   102     UCX_TEST_BEGIN
   103     list = ucx_list_append(list, (void*)"list ");
   104     list = ucx_list_append(list, (void*)"has ");
   105     list = ucx_list_append(list, (void*)"size ");
   106     list = ucx_list_append(list, (void*)"5!");
   108     UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
   110     UCX_TEST_END
   111     ucx_list_free(list);
   112 }
   114 UCX_TEST_IMPLEMENT(test_ucx_list_first) {
   115     UcxList *list = ucx_list_append(NULL, (void*)"Find ");
   116     UCX_TEST_BEGIN
   117     list = ucx_list_append(list, (void*)"the ");
   118     list = ucx_list_append(list, (void*)"first!");
   120     const char* first = (const char*) (ucx_list_first(list)->data);
   122     UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
   124     UCX_TEST_END
   125     ucx_list_free(list);
   126 }
   128 UCX_TEST_IMPLEMENT(test_ucx_list_last) {
   129     UcxList *list = ucx_list_append(NULL, (void*)"Find ");
   130     UCX_TEST_BEGIN
   131     list = ucx_list_append(list, (void*)"the ");
   132     list = ucx_list_append(list, (void*)"last!");
   134     const char* last = (const char*) (ucx_list_last(list)->data);
   136     UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
   138     UCX_TEST_END
   139     ucx_list_free(list);
   140 }
   142 UCX_TEST_IMPLEMENT(test_ucx_list_get) {
   143     UcxList *list = ucx_list_append(NULL, (void*)"Find ");
   144     UCX_TEST_BEGIN
   145     list = ucx_list_append(list, (void*)"the ");
   146     list = ucx_list_append(list, (void*)"mid!");
   148     const char* mid = (const char*) (ucx_list_get(list, 1)->data);
   150     UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
   152     UCX_TEST_END
   153     ucx_list_free(list);
   154 }
   156 UCX_TEST_IMPLEMENT(test_ucx_list_contains) {
   157     UcxList *l = ucx_list_append(NULL, (void*)"Contains ");
   158     UCX_TEST_BEGIN
   159     l = ucx_list_append(l, (void*)"a ");
   160     l = ucx_list_append(l, (void*)"string!");
   162     UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL),"failed");
   163     UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL),"failed");
   165     UCX_TEST_END
   166     ucx_list_free(l);
   167 }
   169 UCX_TEST_IMPLEMENT(test_ucx_list_remove) {
   170     UcxList *list = ucx_list_append(NULL, (void*)"Hello");
   171     UCX_TEST_BEGIN
   172     list = ucx_list_append(list, (void*)" fucking");
   173     list = ucx_list_append(list, (void*)" World!");
   175     list = ucx_list_remove(list, ucx_list_get(list, 1));
   177     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
   178             "failed");
   179     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
   180             "failed");
   181     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
   183     UCX_TEST_END
   184     ucx_list_free(list);
   185 }
   187 UCX_TEST_IMPLEMENT(test_ucx_list_clone) {
   189     char *hello = (char*)malloc(6);
   190     char *world = (char*)malloc(8);
   192     memcpy(hello, "Hello", 6);
   193     memcpy(world, " World!", 8);
   195     UcxList *list = ucx_list_append(NULL, hello);
   196     list = ucx_list_append(list, world);
   198     UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL);
   199     UCX_TEST_BEGIN
   201     UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed");
   202     UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
   203     UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
   205     UCX_TEST_END
   206     free(copy->next->data);
   207     free(copy->data);
   209     free(world);
   210     free(hello);
   211     ucx_list_free(list);
   212     ucx_list_free(copy);
   213 }
   215 UCX_TEST_IMPLEMENT(test_ucx_list_sort) {
   216     UcxList *list = ucx_list_append(NULL, (void*)"this");
   217     list = ucx_list_append(list, (void*)"is");
   218     list = ucx_list_append(list, (void*)"a");
   219     list = ucx_list_append(list, (void*)"test");
   220     list = ucx_list_append(list, (void*)"for");
   221     list = ucx_list_append(list, (void*)"partial");
   222     list = ucx_list_append(list, (void*)"correctness");
   224     UcxList *expected = ucx_list_append(NULL, (void*)"a");
   225     expected = ucx_list_append(expected, (void*)"correctness");
   226     expected = ucx_list_append(expected, (void*)"for");
   227     expected = ucx_list_append(expected, (void*)"is");
   228     expected = ucx_list_append(expected, (void*)"partial");
   229     expected = ucx_list_append(expected, (void*)"test");
   230     expected = ucx_list_append(expected, (void*)"this");
   232     list = ucx_list_sort(list, ucx_strcmp, NULL);
   234     UCX_TEST_BEGIN
   235     UCX_TEST_ASSERT(
   236             ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
   237     UcxList *l = list;
   238     UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
   239     while (l->next != NULL) {
   240         UCX_TEST_ASSERT(l->next->prev == l, "prev pointer corrupted");
   241         l = l->next;
   242     }
   243     UCX_TEST_END
   245     ucx_list_free(expected);
   246     ucx_list_free(list);
   247 }

mercurial