test/list_tests.c

Thu, 28 Feb 2013 08:50:24 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 28 Feb 2013 08:50:24 +0100
changeset 103
08018864fb91
parent 94
57ea041df22f
permissions
-rw-r--r--

added license and copyright notice to all files

     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
    35     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    36             "failed");
    38     list = ucx_list_append(list, (void*)" World!");
    40     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    41             "failed");
    42     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    44     UCX_TEST_END
    45     ucx_list_free(list);
    46 }
    48 UCX_TEST_IMPLEMENT(test_ucx_list_prepend) {
    49     UcxList *list = ucx_list_prepend(NULL, (void*)" World!");
    50     UCX_TEST_BEGIN
    51     list = ucx_list_prepend(list, (void*)"Hello");
    53     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    54             "failed");
    55     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    56             "failed");
    57     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    59     UCX_TEST_END
    60     ucx_list_free(list);
    61 }
    63 UCX_TEST_IMPLEMENT(test_ucx_list_equals) {
    64     UcxList *list = ucx_list_append(NULL, (void*)"Hello");
    65     list = ucx_list_append(list, (void*)" World!");
    66     UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
    67     list2 = ucx_list_prepend(list2, (void*)"Hello");
    68     UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!");
    69     list3 = ucx_list_prepend(list3, (void*)"Hallo");
    71     UCX_TEST_BEGIN
    72     UCX_TEST_ASSERT(ucx_list_equals(list, list2, ucx_strcmp, NULL), "failed");
    73     UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
    74     UCX_TEST_END
    76     ucx_list_free(list3);
    77     ucx_list_free(list2);
    78     ucx_list_free(list);
    79 }
    81 UCX_TEST_IMPLEMENT(test_ucx_list_concat) {
    82     UcxList *list = ucx_list_append(NULL, (void*)"Hello");
    83     UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
    85     list = ucx_list_concat(list, list2);
    86     UCX_TEST_BEGIN
    88     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    89             "failed");
    90     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    91             "failed");
    92     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    94     UCX_TEST_END
    95     if (list->next == NULL) {
    96         ucx_list_free(list2);
    97     }
    98     ucx_list_free(list);
    99 }
   101 UCX_TEST_IMPLEMENT(test_ucx_list_size) {
   102     UcxList *list = ucx_list_append(NULL, (void*)"This ");
   103     UCX_TEST_BEGIN
   104     list = ucx_list_append(list, (void*)"list ");
   105     list = ucx_list_append(list, (void*)"has ");
   106     list = ucx_list_append(list, (void*)"size ");
   107     list = ucx_list_append(list, (void*)"5!");
   109     UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
   111     UCX_TEST_END
   112     ucx_list_free(list);
   113 }
   115 UCX_TEST_IMPLEMENT(test_ucx_list_last) {
   116     UcxList *list = ucx_list_append(NULL, (void*)"Find ");
   117     UCX_TEST_BEGIN
   118     list = ucx_list_append(list, (void*)"the ");
   119     list = ucx_list_append(list, (void*)"last!");
   121     const char* last = (const char*) (ucx_list_last(list)->data);
   123     UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
   125     UCX_TEST_END
   126     ucx_list_free(list);
   128 }
   130 UCX_TEST_IMPLEMENT(test_ucx_list_get) {
   131     UcxList *list = ucx_list_append(NULL, (void*)"Find ");
   132     UCX_TEST_BEGIN
   133     list = ucx_list_append(list, (void*)"the ");
   134     list = ucx_list_append(list, (void*)"mid!");
   136     const char* mid = (const char*) (ucx_list_get(list, 1)->data);
   138     UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
   140     UCX_TEST_END
   141     ucx_list_free(list);
   142 }
   144 UCX_TEST_IMPLEMENT(test_ucx_list_contains) {
   145     UcxList *l = ucx_list_append(NULL, (void*)"Contains ");
   146     UCX_TEST_BEGIN
   147     l = ucx_list_append(l, (void*)"a ");
   148     l = ucx_list_append(l, (void*)"string!");
   150     UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL), "failed");
   151     UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL), "failed");
   153     UCX_TEST_END
   154     ucx_list_free(l);
   155 }
   157 UCX_TEST_IMPLEMENT(test_ucx_list_remove) {
   158     UcxList *list = ucx_list_append(NULL, (void*)"Hello");
   159     UCX_TEST_BEGIN
   160     list = ucx_list_append(list, (void*)" fucking");
   161     list = ucx_list_append(list, (void*)" World!");
   163     list = ucx_list_remove(list, ucx_list_get(list, 1));
   165     UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
   166             "failed");
   167     UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
   168             "failed");
   169     UCX_TEST_ASSERT(list->next->next == NULL, "failed");
   170     UCX_TEST_END
   172     ucx_list_free(list);
   173 }
   175 UCX_TEST_IMPLEMENT(test_ucx_list_clone) {
   177     char *hello = (char*)malloc(6);
   178     char *world = (char*)malloc(8);
   180     memcpy(hello, "Hello", 6);
   181     memcpy(world, " World!", 8);
   183     UcxList *list = ucx_list_append(NULL, hello);
   184     list = ucx_list_append(list, world);
   186     UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL);
   187     UCX_TEST_BEGIN
   189     UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed");
   190     UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
   191     UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
   193     UCX_TEST_END
   194     free(copy->next->data);
   195     free(copy->data);
   197     free(world);
   198     free(hello);
   199     ucx_list_free(list);
   200     ucx_list_free(copy);
   201 }
   203 UCX_TEST_IMPLEMENT(test_ucx_list_sort) {
   204     UcxList *list = ucx_list_append(NULL, (void*)"this");
   205     list = ucx_list_append(list, (void*)"is");
   206     list = ucx_list_append(list, (void*)"a");
   207     list = ucx_list_append(list, (void*)"test");
   208     list = ucx_list_append(list, (void*)"for");
   209     list = ucx_list_append(list, (void*)"partial");
   210     list = ucx_list_append(list, (void*)"correctness");
   212     UcxList *expected = ucx_list_append(NULL, (void*)"a");
   213     expected = ucx_list_append(expected, (void*)"correctness");
   214     expected = ucx_list_append(expected, (void*)"for");
   215     expected = ucx_list_append(expected, (void*)"is");
   216     expected = ucx_list_append(expected, (void*)"partial");
   217     expected = ucx_list_append(expected, (void*)"test");
   218     expected = ucx_list_append(expected, (void*)"this");
   220     list = ucx_list_sort(list, ucx_strcmp, NULL);
   222     UCX_TEST_BEGIN
   223     UCX_TEST_ASSERT(
   224             ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
   225     UCX_TEST_END
   227     ucx_list_free(expected);
   228     ucx_list_free(list);
   229 }

mercurial