test/list_tests.c

changeset 122
540d99722f1f
parent 103
08018864fb91
child 123
7fb0f74517c5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/list_tests.c	Mon Jul 22 11:53:39 2013 +0200
     1.3 @@ -0,0 +1,247 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 2013 Olaf Wintermann. All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions are met:
    1.11 + *
    1.12 + *   1. Redistributions of source code must retain the above copyright
    1.13 + *      notice, this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + *   2. Redistributions in binary form must reproduce the above copyright
    1.16 + *      notice, this list of conditions and the following disclaimer in the
    1.17 + *      documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 + * POSSIBILITY OF SUCH DAMAGE.
    1.30 + */
    1.31 +
    1.32 +#include "list_tests.h"
    1.33 +#include "ucx/utils.h"
    1.34 +
    1.35 +UCX_TEST_IMPLEMENT(test_ucx_list_append) {
    1.36 +    UcxList *list = ucx_list_append(NULL, (void*)"Hello");
    1.37 +    UCX_TEST_BEGIN
    1.38 +    
    1.39 +    UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    1.40 +            "failed");
    1.41 +    
    1.42 +    list = ucx_list_append(list, (void*)" World!");
    1.43 +    
    1.44 +    UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    1.45 +            "failed");
    1.46 +    UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    1.47 +    UCX_TEST_END
    1.48 +    
    1.49 +    ucx_list_free(list);
    1.50 +}
    1.51 +
    1.52 +UCX_TEST_IMPLEMENT(test_ucx_list_prepend) {
    1.53 +    UcxList *list = ucx_list_prepend(NULL, (void*)" World!");
    1.54 +    UCX_TEST_BEGIN
    1.55 +
    1.56 +    list = ucx_list_prepend(list, (void*)"Hello");
    1.57 +    
    1.58 +    UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    1.59 +            "failed");
    1.60 +    UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    1.61 +            "failed");
    1.62 +    UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    1.63 +    
    1.64 +    UCX_TEST_END
    1.65 +    ucx_list_free(list);
    1.66 +}
    1.67 +
    1.68 +UCX_TEST_IMPLEMENT(test_ucx_list_equals) {
    1.69 +    UcxList *list = ucx_list_append(NULL, (void*)"Hello");
    1.70 +    list = ucx_list_append(list, (void*)" World!");
    1.71 +    UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
    1.72 +    list2 = ucx_list_prepend(list2, (void*)"Hello");
    1.73 +    UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!");
    1.74 +    list3 = ucx_list_prepend(list3, (void*)"Hallo");
    1.75 +    UCX_TEST_BEGIN
    1.76 +    
    1.77 +    UCX_TEST_ASSERT(ucx_list_equals(list, list2, ucx_strcmp, NULL), "failed");
    1.78 +    UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
    1.79 +    
    1.80 +    UCX_TEST_END
    1.81 +    ucx_list_free(list3);
    1.82 +    ucx_list_free(list2);
    1.83 +    ucx_list_free(list);
    1.84 +}
    1.85 +
    1.86 +UCX_TEST_IMPLEMENT(test_ucx_list_concat) {
    1.87 +    UcxList *list = ucx_list_append(NULL, (void*)"Hello");
    1.88 +    UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
    1.89 +    UCX_TEST_BEGIN
    1.90 +    
    1.91 +    list = ucx_list_concat(list, list2);
    1.92 +    
    1.93 +    UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    1.94 +            "failed");
    1.95 +    UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    1.96 +            "failed");
    1.97 +    UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    1.98 +    
    1.99 +    UCX_TEST_END
   1.100 +    ucx_list_free(list);
   1.101 +}
   1.102 +
   1.103 +UCX_TEST_IMPLEMENT(test_ucx_list_size) {
   1.104 +    UcxList *list = ucx_list_append(NULL, (void*)"This ");
   1.105 +    UCX_TEST_BEGIN
   1.106 +    list = ucx_list_append(list, (void*)"list ");
   1.107 +    list = ucx_list_append(list, (void*)"has ");
   1.108 +    list = ucx_list_append(list, (void*)"size ");
   1.109 +    list = ucx_list_append(list, (void*)"5!");
   1.110 +    
   1.111 +    UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
   1.112 +    
   1.113 +    UCX_TEST_END
   1.114 +    ucx_list_free(list);
   1.115 +}
   1.116 +
   1.117 +UCX_TEST_IMPLEMENT(test_ucx_list_first) {
   1.118 +    UcxList *list = ucx_list_append(NULL, (void*)"Find ");
   1.119 +    UCX_TEST_BEGIN
   1.120 +    list = ucx_list_append(list, (void*)"the ");
   1.121 +    list = ucx_list_append(list, (void*)"first!");
   1.122 +    
   1.123 +    const char* first = (const char*) (ucx_list_first(list)->data);
   1.124 +    
   1.125 +    UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
   1.126 +    
   1.127 +    UCX_TEST_END
   1.128 +    ucx_list_free(list);
   1.129 +}
   1.130 +
   1.131 +UCX_TEST_IMPLEMENT(test_ucx_list_last) {
   1.132 +    UcxList *list = ucx_list_append(NULL, (void*)"Find ");
   1.133 +    UCX_TEST_BEGIN
   1.134 +    list = ucx_list_append(list, (void*)"the ");
   1.135 +    list = ucx_list_append(list, (void*)"last!");
   1.136 +    
   1.137 +    const char* last = (const char*) (ucx_list_last(list)->data);
   1.138 +    
   1.139 +    UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
   1.140 +    
   1.141 +    UCX_TEST_END
   1.142 +    ucx_list_free(list);
   1.143 +}
   1.144 +
   1.145 +UCX_TEST_IMPLEMENT(test_ucx_list_get) {
   1.146 +    UcxList *list = ucx_list_append(NULL, (void*)"Find ");
   1.147 +    UCX_TEST_BEGIN
   1.148 +    list = ucx_list_append(list, (void*)"the ");
   1.149 +    list = ucx_list_append(list, (void*)"mid!");
   1.150 +    
   1.151 +    const char* mid = (const char*) (ucx_list_get(list, 1)->data);
   1.152 +    
   1.153 +    UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
   1.154 +    
   1.155 +    UCX_TEST_END
   1.156 +    ucx_list_free(list);
   1.157 +}
   1.158 +
   1.159 +UCX_TEST_IMPLEMENT(test_ucx_list_contains) {
   1.160 +    UcxList *l = ucx_list_append(NULL, (void*)"Contains ");
   1.161 +    UCX_TEST_BEGIN
   1.162 +    l = ucx_list_append(l, (void*)"a ");
   1.163 +    l = ucx_list_append(l, (void*)"string!");
   1.164 +    
   1.165 +    UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL),"failed");
   1.166 +    UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL),"failed");
   1.167 +    
   1.168 +    UCX_TEST_END
   1.169 +    ucx_list_free(l);
   1.170 +}
   1.171 +
   1.172 +UCX_TEST_IMPLEMENT(test_ucx_list_remove) {
   1.173 +    UcxList *list = ucx_list_append(NULL, (void*)"Hello");
   1.174 +    UCX_TEST_BEGIN
   1.175 +    list = ucx_list_append(list, (void*)" fucking");
   1.176 +    list = ucx_list_append(list, (void*)" World!");
   1.177 +    
   1.178 +    list = ucx_list_remove(list, ucx_list_get(list, 1));
   1.179 +    
   1.180 +    UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
   1.181 +            "failed");
   1.182 +    UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
   1.183 +            "failed");
   1.184 +    UCX_TEST_ASSERT(list->next->next == NULL, "failed");
   1.185 +    
   1.186 +    UCX_TEST_END
   1.187 +    ucx_list_free(list);
   1.188 +}
   1.189 +
   1.190 +UCX_TEST_IMPLEMENT(test_ucx_list_clone) {
   1.191 +   
   1.192 +    char *hello = (char*)malloc(6);
   1.193 +    char *world = (char*)malloc(8);
   1.194 +    
   1.195 +    memcpy(hello, "Hello", 6);
   1.196 +    memcpy(world, " World!", 8);
   1.197 +    
   1.198 +    UcxList *list = ucx_list_append(NULL, hello);
   1.199 +    list = ucx_list_append(list, world);
   1.200 +    
   1.201 +    UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL);
   1.202 +    UCX_TEST_BEGIN
   1.203 +
   1.204 +    UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed");
   1.205 +    UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
   1.206 +    UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
   1.207 +
   1.208 +    UCX_TEST_END
   1.209 +    free(copy->next->data);
   1.210 +    free(copy->data);
   1.211 +
   1.212 +    free(world);
   1.213 +    free(hello);
   1.214 +    ucx_list_free(list);
   1.215 +    ucx_list_free(copy);
   1.216 +}
   1.217 +
   1.218 +UCX_TEST_IMPLEMENT(test_ucx_list_sort) {
   1.219 +    UcxList *list = ucx_list_append(NULL, (void*)"this");
   1.220 +    list = ucx_list_append(list, (void*)"is");
   1.221 +    list = ucx_list_append(list, (void*)"a");
   1.222 +    list = ucx_list_append(list, (void*)"test");
   1.223 +    list = ucx_list_append(list, (void*)"for");
   1.224 +    list = ucx_list_append(list, (void*)"partial");
   1.225 +    list = ucx_list_append(list, (void*)"correctness");
   1.226 +
   1.227 +    UcxList *expected = ucx_list_append(NULL, (void*)"a");
   1.228 +    expected = ucx_list_append(expected, (void*)"correctness");
   1.229 +    expected = ucx_list_append(expected, (void*)"for");
   1.230 +    expected = ucx_list_append(expected, (void*)"is");
   1.231 +    expected = ucx_list_append(expected, (void*)"partial");
   1.232 +    expected = ucx_list_append(expected, (void*)"test");
   1.233 +    expected = ucx_list_append(expected, (void*)"this");
   1.234 +
   1.235 +    list = ucx_list_sort(list, ucx_strcmp, NULL);
   1.236 +
   1.237 +    UCX_TEST_BEGIN
   1.238 +    UCX_TEST_ASSERT(
   1.239 +            ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
   1.240 +    UcxList *l = list;
   1.241 +    UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
   1.242 +    while (l->next != NULL) {
   1.243 +        UCX_TEST_ASSERT(l->next->prev == l, "prev pointer corrupted");
   1.244 +        l = l->next;
   1.245 +    }
   1.246 +    UCX_TEST_END
   1.247 +
   1.248 +    ucx_list_free(expected);
   1.249 +    ucx_list_free(list);
   1.250 +}

mercurial