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