test/list_tests.c

Thu, 10 Apr 2014 11:32:59 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 10 Apr 2014 11:32:59 +0200
changeset 162
52dfe5f4ecd7
parent 134
4d320dc3a7af
child 172
7084e8e8433c
permissions
-rw-r--r--

added more tests for ucx_list_remove

     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(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(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(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(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(test_ucx_list_size) {
   101     UcxList *list = ucx_list_append(NULL, (void*)"This ");
   102     list = ucx_list_append(list, (void*)"list ");
   103     list = ucx_list_append(list, (void*)"has ");
   104     list = ucx_list_append(list, (void*)"size ");
   105     list = ucx_list_append(list, (void*)"5!");
   107     UCX_TEST_BEGIN
   109     UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
   111     UCX_TEST_END
   112     ucx_list_free(list);
   113 }
   115 UCX_TEST(test_ucx_list_first) {
   116     UcxList *list = ucx_list_append(NULL, (void*)"Find ");
   117     list = ucx_list_append(list, (void*)"the ");
   118     list = ucx_list_append(list, (void*)"first!");
   120     UCX_TEST_BEGIN
   122     const char* first = (const char*) (ucx_list_first(list)->data);
   124     UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
   126     UCX_TEST_END
   127     ucx_list_free(list);
   128 }
   130 UCX_TEST(test_ucx_list_last) {
   131     UcxList *list = ucx_list_append(NULL, (void*)"Find ");
   132     list = ucx_list_append(list, (void*)"the ");
   133     list = ucx_list_append(list, (void*)"last!");
   135     UCX_TEST_BEGIN
   137     const char* last = (const char*) (ucx_list_last(list)->data);
   139     UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
   141     UCX_TEST_END
   142     ucx_list_free(list);
   143 }
   145 UCX_TEST(test_ucx_list_get) {
   146     UcxList *list = ucx_list_append(NULL, (void*)"Find ");
   147     list = ucx_list_append(list, (void*)"the ");
   148     list = ucx_list_append(list, (void*)"mid!");
   150     UCX_TEST_BEGIN
   152     const char* mid = (const char*) (ucx_list_get(list, 1)->data);
   154     UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
   156     UCX_TEST_END
   157     ucx_list_free(list);
   158 }
   160 UCX_TEST(test_ucx_list_indexof) {
   161     UcxList *list = ucx_list_append(NULL, (void*)"Find ");
   162     list = ucx_list_append(list, (void*)"the ");
   163     list = ucx_list_append(list, (void*)"mid!");
   165     UCX_TEST_BEGIN
   167     UCX_TEST_ASSERT(ucx_list_indexof(list, list) == 0, "failed");
   168     UCX_TEST_ASSERT(ucx_list_indexof(list, list->next) == 1, "failed");
   169     UCX_TEST_ASSERT(ucx_list_indexof(list, ucx_list_get(list, 2)) == 2,
   170         "failed");
   172     UcxList *otherlist = ucx_list_append(NULL, (void*) "foobar");
   173     UCX_TEST_ASSERT(ucx_list_indexof(list, otherlist) == -1, "failed");
   174     ucx_list_free(otherlist);
   176     UCX_TEST_END
   177     ucx_list_free(list);
   178 }
   180 UCX_TEST(test_ucx_list_find) {
   181     UcxList *l = ucx_list_append(NULL, (void*)"find ");
   182     l = ucx_list_append(l, (void*)"some ");
   183     l = ucx_list_append(l, (void*)"string!");
   185     UCX_TEST_BEGIN
   187     UCX_TEST_ASSERT(ucx_list_find(l,(void*)"some ",ucx_strcmp,NULL) == 1,
   188         "doesn't find string");
   189     UCX_TEST_ASSERT(ucx_list_find(l,(void*)"a",ucx_strcmp,NULL) == -1,
   190         "finds non-existing string");
   192     UCX_TEST_END
   193     ucx_list_free(l);
   194 }
   196 UCX_TEST(test_ucx_list_contains) {
   197     UcxList *l = ucx_list_append(NULL, (void*)"Contains ");
   198     l = ucx_list_append(l, (void*)"a ");
   199     l = ucx_list_append(l, (void*)"string!");
   201     UCX_TEST_BEGIN
   203     UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL),
   204         "false negative");
   205     UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL),
   206         "false positive");
   208     UCX_TEST_END
   209     ucx_list_free(l);
   210 }
   212 UCX_TEST(test_ucx_list_remove) {
   213     UcxList *list = ucx_list_append(NULL, (void*)"Hello");
   214     list = ucx_list_append(list, (void*)"fucking");
   215     list = ucx_list_append(list, (void*)"World!");
   217     UcxList *list2 = ucx_list_append(NULL, (void*)"A");
   218     list2 = ucx_list_append(list2, (void*)"B");
   219     list2 = ucx_list_append(list2, (void*)"C");
   220     list2 = ucx_list_append(list2, (void*)"D");
   221     list2 = ucx_list_append(list2, (void*)"E");
   222     list2 = ucx_list_append(list2, (void*)"F");
   223     list2 = ucx_list_append(list2, (void*)"G");
   225     UCX_TEST_BEGIN
   227     list = ucx_list_remove(list, ucx_list_get(list, 1));
   229     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
   230             "failed");
   231     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, "World!", 7) == 0,
   232             "failed");
   233     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
   235     // remove first element: B, C, D, E, F, G
   236     list2 = ucx_list_remove(list2, list2);
   238     UCX_TEST_ASSERT(ucx_list_size(list2) == 6, "list2 has wrong size");
   239     UCX_TEST_ASSERT(strncmp((const char*)list2->data, "B", 1) == 0,
   240             "wrong first element");
   241     UCX_TEST_ASSERT(strncmp((const char*)ucx_list_get(list2, 5)->data, "G", 1)
   242             == 0, "wrong last element");
   244     // remove second element: B, D, E, F, G
   245     list2 = ucx_list_remove(list2, list2->next);
   247     UCX_TEST_ASSERT(ucx_list_size(list2) == 5, "list2 has wrong size");
   248     UCX_TEST_ASSERT(strncmp((const char*)list2->next->data, "D", 1) == 0,
   249             "wrong second element");
   251     UcxList *last = ucx_list_get(list2, 4);
   252     list2 = ucx_list_remove(list2, last->prev);
   254     UCX_TEST_ASSERT(ucx_list_size(list2) == 4, "list2 has wrong size");
   255     UCX_TEST_ASSERT(strncmp((const char*)last->prev->data, "E", 1) == 0,
   256             "wrong element");
   258     // remove last element: B, D, E, F
   259     list2 = ucx_list_remove(list2, last);
   260     UCX_TEST_ASSERT(ucx_list_size(list2) == 3, "list2 has wrong size");
   261     UCX_TEST_ASSERT(strncmp((const char*)ucx_list_get(list2, 2)->data, "E", 1)
   262             == 0, "wrong last element");
   264     UCX_TEST_ASSERT(strncmp((const char*)list2->data, "B", 1) == 0,
   265             "wrong element");
   267     list2 = ucx_list_remove(list2, list2);
   268     UCX_TEST_ASSERT(ucx_list_size(list2) == 2, "list2 has wrong size");
   269     list2 = ucx_list_remove(list2, list2);
   270     UCX_TEST_ASSERT(ucx_list_size(list2) == 1, "list2 has wrong size");
   271     list2 = ucx_list_remove(list2, list2);
   272     UCX_TEST_ASSERT(list2 == NULL, "list2 is not null");
   274     UCX_TEST_END
   275     ucx_list_free(list);
   276 }
   278 UCX_TEST(test_ucx_list_clone) {
   280     char *hello = (char*)malloc(6);
   281     char *world = (char*)malloc(8);
   283     memcpy(hello, "Hello", 6);
   284     memcpy(world, " World!", 8);
   286     UcxList *list = ucx_list_append(NULL, hello);
   287     list = ucx_list_append(list, world);
   289     UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL);
   290     UCX_TEST_BEGIN
   292     UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed");
   293     UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
   294     UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
   296     UCX_TEST_END
   297     free(copy->next->data);
   298     free(copy->data);
   300     free(world);
   301     free(hello);
   302     ucx_list_free(list);
   303     ucx_list_free(copy);
   304 }
   306 UCX_TEST(test_ucx_list_sort) {
   307     UcxList *list = ucx_list_append(NULL, (void*)"this");
   308     list = ucx_list_append(list, (void*)"is");
   309     list = ucx_list_append(list, (void*)"a");
   310     list = ucx_list_append(list, (void*)"test");
   311     list = ucx_list_append(list, (void*)"for");
   312     list = ucx_list_append(list, (void*)"partial");
   313     list = ucx_list_append(list, (void*)"correctness");
   315     UcxList *expected = ucx_list_append(NULL, (void*)"a");
   316     expected = ucx_list_append(expected, (void*)"correctness");
   317     expected = ucx_list_append(expected, (void*)"for");
   318     expected = ucx_list_append(expected, (void*)"is");
   319     expected = ucx_list_append(expected, (void*)"partial");
   320     expected = ucx_list_append(expected, (void*)"test");
   321     expected = ucx_list_append(expected, (void*)"this");
   323     list = ucx_list_sort(list, ucx_strcmp, NULL);
   325     UCX_TEST_BEGIN
   326     UCX_TEST_ASSERT(
   327             ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
   328     UcxList *l = list;
   329     UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
   330     while (l->next != NULL) {
   331         UCX_TEST_ASSERT(l->next->prev == l, "prev pointer corrupted");
   332         l = l->next;
   333     }
   334     UCX_TEST_END
   336     ucx_list_free(expected);
   337     ucx_list_free(list);
   338 }

mercurial