Mon, 20 Feb 2017 16:04:14 +0100
adds new test case for sstrsplit: string ends with delimiter but empty string exceeds list bound
olaf@9 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@103 | 3 | * |
universe@225 | 4 | * Copyright 2016 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@134 | 32 | UCX_TEST(test_ucx_list_append) { |
universe@172 | 33 | UcxList *list, *first; |
universe@172 | 34 | list = first = ucx_list_append(NULL, (void*)"Hello"); |
universe@33 | 35 | UCX_TEST_BEGIN |
universe@27 | 36 | |
universe@69 | 37 | UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, |
universe@69 | 38 | "failed"); |
universe@27 | 39 | |
universe@122 | 40 | list = ucx_list_append(list, (void*)" World!"); |
universe@27 | 41 | |
universe@172 | 42 | UCX_TEST_ASSERT(list == first, "does not return first element"); |
universe@69 | 43 | UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, |
universe@69 | 44 | "failed"); |
universe@172 | 45 | UCX_TEST_ASSERT(list->next->prev == list, "failed"); |
universe@40 | 46 | UCX_TEST_ASSERT(list->next->next == NULL, "failed"); |
universe@33 | 47 | UCX_TEST_END |
universe@27 | 48 | |
universe@122 | 49 | ucx_list_free(list); |
universe@24 | 50 | } |
universe@24 | 51 | |
universe@134 | 52 | UCX_TEST(test_ucx_list_prepend) { |
universe@172 | 53 | UcxList *list, *last; |
universe@172 | 54 | list = last = ucx_list_prepend(NULL, (void*)" World!"); |
universe@33 | 55 | UCX_TEST_BEGIN |
universe@33 | 56 | |
universe@122 | 57 | list = ucx_list_prepend(list, (void*)"Hello"); |
universe@27 | 58 | |
universe@69 | 59 | UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, |
universe@69 | 60 | "failed"); |
universe@69 | 61 | UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, |
universe@69 | 62 | "failed"); |
universe@172 | 63 | UCX_TEST_ASSERT(list == last->prev, "does not return first element"); |
universe@40 | 64 | UCX_TEST_ASSERT(list->next->next == NULL, "failed"); |
universe@172 | 65 | UCX_TEST_ASSERT(list->prev == NULL, "failed"); |
universe@27 | 66 | |
universe@33 | 67 | UCX_TEST_END |
universe@122 | 68 | ucx_list_free(list); |
universe@18 | 69 | } |
universe@18 | 70 | |
universe@228 | 71 | UCX_TEST(test_ucx_list_append_once) { |
universe@228 | 72 | UcxList *list, *first; |
universe@228 | 73 | list = first = ucx_list_append_once(NULL, (void*)"Hello", ucx_strcmp, NULL); |
universe@228 | 74 | UCX_TEST_BEGIN |
universe@228 | 75 | |
universe@228 | 76 | UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, |
universe@228 | 77 | "failed"); |
universe@228 | 78 | |
universe@228 | 79 | list = ucx_list_append_once(list, (void*)"Hello", ucx_strcmp, NULL); |
universe@228 | 80 | list = ucx_list_append_once(list, (void*)" World!", ucx_strcmp, NULL); |
universe@228 | 81 | |
universe@228 | 82 | UCX_TEST_ASSERT(list == first, "does not return first element"); |
universe@228 | 83 | UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, |
universe@228 | 84 | "'Hello' was not inserted _once_"); |
universe@228 | 85 | UCX_TEST_ASSERT(list->next->prev == list, "failed"); |
universe@228 | 86 | UCX_TEST_ASSERT(list->next->next == NULL, "right not terminated"); |
universe@228 | 87 | UCX_TEST_END |
universe@228 | 88 | |
universe@228 | 89 | ucx_list_free(list); |
universe@228 | 90 | } |
universe@228 | 91 | |
universe@134 | 92 | UCX_TEST(test_ucx_list_equals) { |
universe@172 | 93 | const char *hello = "Hello"; |
universe@172 | 94 | const char *world = " World!"; |
universe@172 | 95 | UcxList *list = ucx_list_append(NULL, (void*)hello); |
universe@172 | 96 | list = ucx_list_append(list, (void*)world); |
universe@172 | 97 | UcxList *list2 = ucx_list_prepend(NULL, (void*)world); |
universe@172 | 98 | list2 = ucx_list_prepend(list2, (void*)hello); |
universe@122 | 99 | UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!"); |
universe@122 | 100 | list3 = ucx_list_prepend(list3, (void*)"Hallo"); |
universe@172 | 101 | UcxList *list4 = ucx_list_prepend(NULL, (void*)" World!"); |
universe@172 | 102 | list4 = ucx_list_prepend(list4, (void*)"Hello"); |
universe@33 | 103 | UCX_TEST_BEGIN |
universe@27 | 104 | |
universe@172 | 105 | UCX_TEST_ASSERT(ucx_list_equals(list, list4, ucx_strcmp, NULL), "failed"); |
universe@122 | 106 | UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed"); |
universe@172 | 107 | UCX_TEST_ASSERT(ucx_list_equals(list, list2, NULL, NULL), "failed"); |
universe@27 | 108 | |
universe@33 | 109 | UCX_TEST_END |
universe@172 | 110 | ucx_list_free(list4); |
universe@122 | 111 | ucx_list_free(list3); |
universe@122 | 112 | ucx_list_free(list2); |
universe@122 | 113 | ucx_list_free(list); |
universe@24 | 114 | } |
universe@24 | 115 | |
universe@134 | 116 | UCX_TEST(test_ucx_list_concat) { |
universe@122 | 117 | UcxList *list = ucx_list_append(NULL, (void*)"Hello"); |
universe@172 | 118 | list = ucx_list_append(list, (void*)" my "); |
universe@122 | 119 | UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!"); |
universe@172 | 120 | list2 = ucx_list_prepend(list2, (void*)" sweet "); |
universe@33 | 121 | UCX_TEST_BEGIN |
universe@27 | 122 | |
universe@122 | 123 | list = ucx_list_concat(list, list2); |
universe@172 | 124 | list = ucx_list_concat(list, NULL); |
universe@172 | 125 | list = ucx_list_concat(NULL, list); |
universe@27 | 126 | |
universe@172 | 127 | UCX_TEST_ASSERT(!strncmp((const char*)list->data, "Hello", 5), |
universe@69 | 128 | "failed"); |
universe@172 | 129 | UCX_TEST_ASSERT(!strncmp((const char*)list->next->data, " my ", 4), |
universe@69 | 130 | "failed"); |
universe@172 | 131 | UCX_TEST_ASSERT(!strncmp((const char*)list->next->next->data, " sweet ", 7), |
universe@172 | 132 | "failed"); |
universe@172 | 133 | UCX_TEST_ASSERT(!strncmp((const char*)ucx_list_last(list)->data, |
universe@172 | 134 | " World!", 7), "failed"); |
universe@172 | 135 | |
universe@172 | 136 | UCX_TEST_ASSERT(list->prev == NULL, "failed"); |
universe@27 | 137 | |
universe@33 | 138 | UCX_TEST_END |
universe@172 | 139 | // don't free list2, as it is freed by freeing list; |
universe@122 | 140 | ucx_list_free(list); |
olaf@9 | 141 | } |
olaf@9 | 142 | |
universe@134 | 143 | UCX_TEST(test_ucx_list_size) { |
universe@122 | 144 | UcxList *list = ucx_list_append(NULL, (void*)"This "); |
universe@122 | 145 | list = ucx_list_append(list, (void*)"list "); |
universe@122 | 146 | list = ucx_list_append(list, (void*)"has "); |
universe@122 | 147 | list = ucx_list_append(list, (void*)"size "); |
universe@122 | 148 | list = ucx_list_append(list, (void*)"5!"); |
universe@27 | 149 | |
universe@123 | 150 | UCX_TEST_BEGIN |
universe@123 | 151 | |
universe@122 | 152 | UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed"); |
universe@172 | 153 | list = ucx_list_remove(list, ucx_list_get(list, 2)); |
universe@172 | 154 | UCX_TEST_ASSERT(ucx_list_size(list) == 4, "failed after removal"); |
universe@27 | 155 | |
universe@33 | 156 | UCX_TEST_END |
universe@122 | 157 | ucx_list_free(list); |
olaf@9 | 158 | } |
olaf@11 | 159 | |
universe@134 | 160 | UCX_TEST(test_ucx_list_first) { |
universe@122 | 161 | UcxList *list = ucx_list_append(NULL, (void*)"Find "); |
universe@122 | 162 | list = ucx_list_append(list, (void*)"the "); |
universe@122 | 163 | list = ucx_list_append(list, (void*)"first!"); |
universe@27 | 164 | |
universe@123 | 165 | UCX_TEST_BEGIN |
universe@123 | 166 | |
universe@122 | 167 | const char* first = (const char*) (ucx_list_first(list)->data); |
universe@27 | 168 | |
universe@27 | 169 | UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed"); |
universe@172 | 170 | UCX_TEST_ASSERT(ucx_list_first(list->next->next) == list, "failed"); |
universe@172 | 171 | UCX_TEST_ASSERT(!ucx_list_first(NULL), |
universe@172 | 172 | "does not return NULL on an empty list"); |
universe@27 | 173 | |
universe@33 | 174 | UCX_TEST_END |
universe@122 | 175 | ucx_list_free(list); |
universe@27 | 176 | } |
universe@27 | 177 | |
universe@134 | 178 | UCX_TEST(test_ucx_list_last) { |
universe@122 | 179 | UcxList *list = ucx_list_append(NULL, (void*)"Find "); |
universe@122 | 180 | list = ucx_list_append(list, (void*)"the "); |
universe@122 | 181 | list = ucx_list_append(list, (void*)"last!"); |
universe@27 | 182 | |
universe@123 | 183 | UCX_TEST_BEGIN |
universe@123 | 184 | |
universe@172 | 185 | const char* last = (const char*) (ucx_list_last(list->next->next)->data); |
universe@27 | 186 | |
universe@27 | 187 | UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed"); |
universe@172 | 188 | UCX_TEST_ASSERT(ucx_list_last(list) == list->next->next, "failed"); |
universe@172 | 189 | UCX_TEST_ASSERT(!ucx_list_last(NULL), |
universe@172 | 190 | "does not return NULL on an empty list"); |
universe@27 | 191 | |
universe@33 | 192 | UCX_TEST_END |
universe@122 | 193 | ucx_list_free(list); |
universe@27 | 194 | } |
universe@27 | 195 | |
universe@134 | 196 | UCX_TEST(test_ucx_list_get) { |
universe@122 | 197 | UcxList *list = ucx_list_append(NULL, (void*)"Find "); |
universe@122 | 198 | list = ucx_list_append(list, (void*)"the "); |
universe@122 | 199 | list = ucx_list_append(list, (void*)"mid!"); |
universe@27 | 200 | |
universe@123 | 201 | UCX_TEST_BEGIN |
universe@123 | 202 | |
universe@172 | 203 | const char* first = (const char*) (ucx_list_get(list, 0)->data); |
universe@122 | 204 | const char* mid = (const char*) (ucx_list_get(list, 1)->data); |
universe@172 | 205 | const char* last = (const char*) (ucx_list_get(list, 2)->data); |
universe@27 | 206 | |
universe@172 | 207 | UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed"); |
universe@27 | 208 | UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed"); |
universe@172 | 209 | UCX_TEST_ASSERT(strncmp(last, "mid!", 4) == 0, "failed"); |
universe@172 | 210 | UCX_TEST_ASSERT(!ucx_list_get(list, -1), "out of bounds (neg)"); |
universe@172 | 211 | UCX_TEST_ASSERT(!ucx_list_get(list, 3), "out of bounds"); |
universe@172 | 212 | UCX_TEST_ASSERT(!ucx_list_get(NULL, 0), "empty list"); |
universe@27 | 213 | |
universe@33 | 214 | UCX_TEST_END |
universe@122 | 215 | ucx_list_free(list); |
universe@27 | 216 | } |
universe@27 | 217 | |
universe@134 | 218 | UCX_TEST(test_ucx_list_indexof) { |
universe@123 | 219 | UcxList *list = ucx_list_append(NULL, (void*)"Find "); |
universe@123 | 220 | list = ucx_list_append(list, (void*)"the "); |
universe@123 | 221 | list = ucx_list_append(list, (void*)"mid!"); |
universe@123 | 222 | |
universe@123 | 223 | UCX_TEST_BEGIN |
universe@123 | 224 | |
universe@123 | 225 | UCX_TEST_ASSERT(ucx_list_indexof(list, list) == 0, "failed"); |
universe@123 | 226 | UCX_TEST_ASSERT(ucx_list_indexof(list, list->next) == 1, "failed"); |
universe@123 | 227 | UCX_TEST_ASSERT(ucx_list_indexof(list, ucx_list_get(list, 2)) == 2, |
universe@123 | 228 | "failed"); |
universe@123 | 229 | |
universe@172 | 230 | UcxList *otherlist = ucx_list_append(NULL, (void*) "the "); |
universe@123 | 231 | UCX_TEST_ASSERT(ucx_list_indexof(list, otherlist) == -1, "failed"); |
universe@172 | 232 | UCX_TEST_ASSERT(ucx_list_indexof(NULL, otherlist) == -1, "empty list"); |
universe@172 | 233 | |
universe@123 | 234 | ucx_list_free(otherlist); |
universe@123 | 235 | |
universe@123 | 236 | UCX_TEST_END |
universe@123 | 237 | ucx_list_free(list); |
universe@123 | 238 | } |
universe@123 | 239 | |
universe@134 | 240 | UCX_TEST(test_ucx_list_find) { |
universe@172 | 241 | const char* teststr = "string!"; |
universe@123 | 242 | UcxList *l = ucx_list_append(NULL, (void*)"find "); |
universe@123 | 243 | l = ucx_list_append(l, (void*)"some "); |
universe@172 | 244 | l = ucx_list_append(l, (void*)teststr); |
universe@123 | 245 | |
universe@123 | 246 | UCX_TEST_BEGIN |
universe@123 | 247 | |
universe@123 | 248 | UCX_TEST_ASSERT(ucx_list_find(l,(void*)"some ",ucx_strcmp,NULL) == 1, |
universe@123 | 249 | "doesn't find string"); |
universe@123 | 250 | UCX_TEST_ASSERT(ucx_list_find(l,(void*)"a",ucx_strcmp,NULL) == -1, |
universe@123 | 251 | "finds non-existing string"); |
universe@123 | 252 | |
universe@172 | 253 | UCX_TEST_ASSERT(ucx_list_find(l,(void*)teststr,NULL,NULL) == 2, |
universe@172 | 254 | "doesn't find integer without cmp_func"); |
universe@172 | 255 | |
universe@172 | 256 | UCX_TEST_ASSERT(ucx_list_find(NULL, (void*)"some ",ucx_strcmp,NULL) == -1, |
universe@172 | 257 | "empty list"); |
universe@172 | 258 | |
universe@123 | 259 | UCX_TEST_END |
universe@123 | 260 | ucx_list_free(l); |
universe@123 | 261 | } |
universe@123 | 262 | |
universe@134 | 263 | UCX_TEST(test_ucx_list_contains) { |
universe@122 | 264 | UcxList *l = ucx_list_append(NULL, (void*)"Contains "); |
universe@122 | 265 | l = ucx_list_append(l, (void*)"a "); |
universe@122 | 266 | l = ucx_list_append(l, (void*)"string!"); |
universe@90 | 267 | |
universe@123 | 268 | UCX_TEST_BEGIN |
universe@123 | 269 | |
universe@123 | 270 | UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL), |
universe@123 | 271 | "false negative"); |
universe@123 | 272 | UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL), |
universe@123 | 273 | "false positive"); |
universe@90 | 274 | |
universe@90 | 275 | UCX_TEST_END |
universe@122 | 276 | ucx_list_free(l); |
universe@90 | 277 | } |
universe@90 | 278 | |
universe@134 | 279 | UCX_TEST(test_ucx_list_remove) { |
universe@122 | 280 | UcxList *list = ucx_list_append(NULL, (void*)"Hello"); |
olaf@162 | 281 | list = ucx_list_append(list, (void*)"fucking"); |
olaf@162 | 282 | list = ucx_list_append(list, (void*)"World!"); |
olaf@162 | 283 | |
olaf@162 | 284 | UcxList *list2 = ucx_list_append(NULL, (void*)"A"); |
olaf@162 | 285 | list2 = ucx_list_append(list2, (void*)"B"); |
olaf@162 | 286 | list2 = ucx_list_append(list2, (void*)"C"); |
olaf@162 | 287 | list2 = ucx_list_append(list2, (void*)"D"); |
olaf@162 | 288 | list2 = ucx_list_append(list2, (void*)"E"); |
olaf@162 | 289 | list2 = ucx_list_append(list2, (void*)"F"); |
olaf@162 | 290 | list2 = ucx_list_append(list2, (void*)"G"); |
universe@27 | 291 | |
universe@123 | 292 | UCX_TEST_BEGIN |
universe@123 | 293 | |
universe@122 | 294 | list = ucx_list_remove(list, ucx_list_get(list, 1)); |
universe@27 | 295 | |
universe@69 | 296 | UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, |
universe@69 | 297 | "failed"); |
olaf@162 | 298 | UCX_TEST_ASSERT(strncmp((const char*)list->next->data, "World!", 7) == 0, |
universe@69 | 299 | "failed"); |
universe@40 | 300 | UCX_TEST_ASSERT(list->next->next == NULL, "failed"); |
universe@27 | 301 | |
olaf@162 | 302 | // remove first element: B, C, D, E, F, G |
olaf@162 | 303 | list2 = ucx_list_remove(list2, list2); |
olaf@162 | 304 | |
olaf@162 | 305 | UCX_TEST_ASSERT(ucx_list_size(list2) == 6, "list2 has wrong size"); |
olaf@162 | 306 | UCX_TEST_ASSERT(strncmp((const char*)list2->data, "B", 1) == 0, |
olaf@162 | 307 | "wrong first element"); |
olaf@162 | 308 | UCX_TEST_ASSERT(strncmp((const char*)ucx_list_get(list2, 5)->data, "G", 1) |
olaf@162 | 309 | == 0, "wrong last element"); |
olaf@162 | 310 | |
olaf@162 | 311 | // remove second element: B, D, E, F, G |
olaf@162 | 312 | list2 = ucx_list_remove(list2, list2->next); |
olaf@162 | 313 | |
olaf@162 | 314 | UCX_TEST_ASSERT(ucx_list_size(list2) == 5, "list2 has wrong size"); |
olaf@162 | 315 | UCX_TEST_ASSERT(strncmp((const char*)list2->next->data, "D", 1) == 0, |
olaf@162 | 316 | "wrong second element"); |
olaf@162 | 317 | |
olaf@162 | 318 | UcxList *last = ucx_list_get(list2, 4); |
olaf@162 | 319 | list2 = ucx_list_remove(list2, last->prev); |
olaf@162 | 320 | |
olaf@162 | 321 | UCX_TEST_ASSERT(ucx_list_size(list2) == 4, "list2 has wrong size"); |
olaf@162 | 322 | UCX_TEST_ASSERT(strncmp((const char*)last->prev->data, "E", 1) == 0, |
olaf@162 | 323 | "wrong element"); |
olaf@162 | 324 | |
olaf@162 | 325 | // remove last element: B, D, E, F |
olaf@162 | 326 | list2 = ucx_list_remove(list2, last); |
olaf@162 | 327 | UCX_TEST_ASSERT(ucx_list_size(list2) == 3, "list2 has wrong size"); |
olaf@162 | 328 | UCX_TEST_ASSERT(strncmp((const char*)ucx_list_get(list2, 2)->data, "E", 1) |
olaf@162 | 329 | == 0, "wrong last element"); |
olaf@162 | 330 | |
olaf@162 | 331 | UCX_TEST_ASSERT(strncmp((const char*)list2->data, "B", 1) == 0, |
olaf@162 | 332 | "wrong element"); |
olaf@162 | 333 | |
olaf@162 | 334 | list2 = ucx_list_remove(list2, list2); |
olaf@162 | 335 | UCX_TEST_ASSERT(ucx_list_size(list2) == 2, "list2 has wrong size"); |
olaf@162 | 336 | list2 = ucx_list_remove(list2, list2); |
olaf@162 | 337 | UCX_TEST_ASSERT(ucx_list_size(list2) == 1, "list2 has wrong size"); |
olaf@162 | 338 | list2 = ucx_list_remove(list2, list2); |
olaf@162 | 339 | UCX_TEST_ASSERT(list2 == NULL, "list2 is not null"); |
olaf@162 | 340 | |
universe@33 | 341 | UCX_TEST_END |
universe@122 | 342 | ucx_list_free(list); |
universe@27 | 343 | } |
universe@27 | 344 | |
universe@134 | 345 | UCX_TEST(test_ucx_list_clone) { |
universe@27 | 346 | |
universe@27 | 347 | char *hello = (char*)malloc(6); |
universe@27 | 348 | char *world = (char*)malloc(8); |
universe@27 | 349 | |
universe@27 | 350 | memcpy(hello, "Hello", 6); |
universe@27 | 351 | memcpy(world, " World!", 8); |
universe@27 | 352 | |
universe@122 | 353 | UcxList *list = ucx_list_append(NULL, hello); |
universe@122 | 354 | list = ucx_list_append(list, world); |
universe@27 | 355 | |
universe@122 | 356 | UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL); |
universe@33 | 357 | UCX_TEST_BEGIN |
universe@27 | 358 | |
universe@122 | 359 | UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed"); |
universe@40 | 360 | UCX_TEST_ASSERT(hello != copy->data, "first element is no copy"); |
universe@40 | 361 | UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy"); |
universe@27 | 362 | |
universe@33 | 363 | UCX_TEST_END |
universe@211 | 364 | |
universe@212 | 365 | ucx_list_free_content(copy, free); |
universe@27 | 366 | |
universe@27 | 367 | free(world); |
universe@27 | 368 | free(hello); |
universe@122 | 369 | ucx_list_free(list); |
universe@122 | 370 | ucx_list_free(copy); |
universe@27 | 371 | } |
universe@35 | 372 | |
universe@134 | 373 | UCX_TEST(test_ucx_list_sort) { |
universe@122 | 374 | UcxList *list = ucx_list_append(NULL, (void*)"this"); |
universe@122 | 375 | list = ucx_list_append(list, (void*)"is"); |
universe@122 | 376 | list = ucx_list_append(list, (void*)"a"); |
universe@122 | 377 | list = ucx_list_append(list, (void*)"test"); |
universe@122 | 378 | list = ucx_list_append(list, (void*)"for"); |
universe@122 | 379 | list = ucx_list_append(list, (void*)"partial"); |
universe@122 | 380 | list = ucx_list_append(list, (void*)"correctness"); |
universe@172 | 381 | list = ucx_list_append(list, (void*)"of"); |
universe@172 | 382 | list = ucx_list_append(list, (void*)"the"); |
universe@172 | 383 | list = ucx_list_append(list, (void*)"sort"); |
universe@172 | 384 | list = ucx_list_append(list, (void*)"function"); |
universe@172 | 385 | list = ucx_list_append(list, (void*)"that"); |
universe@172 | 386 | list = ucx_list_append(list, (void*)"shall"); |
universe@172 | 387 | list = ucx_list_append(list, (void*)"pass"); |
universe@172 | 388 | list = ucx_list_append(list, (void*)"this"); |
universe@172 | 389 | list = ucx_list_append(list, (void*)"test"); |
universe@35 | 390 | |
universe@122 | 391 | UcxList *expected = ucx_list_append(NULL, (void*)"a"); |
universe@122 | 392 | expected = ucx_list_append(expected, (void*)"correctness"); |
universe@122 | 393 | expected = ucx_list_append(expected, (void*)"for"); |
universe@172 | 394 | expected = ucx_list_append(expected, (void*)"function"); |
universe@122 | 395 | expected = ucx_list_append(expected, (void*)"is"); |
universe@172 | 396 | expected = ucx_list_append(expected, (void*)"of"); |
universe@122 | 397 | expected = ucx_list_append(expected, (void*)"partial"); |
universe@172 | 398 | expected = ucx_list_append(expected, (void*)"pass"); |
universe@172 | 399 | expected = ucx_list_append(expected, (void*)"shall"); |
universe@172 | 400 | expected = ucx_list_append(expected, (void*)"sort"); |
universe@122 | 401 | expected = ucx_list_append(expected, (void*)"test"); |
universe@172 | 402 | expected = ucx_list_append(expected, (void*)"test"); |
universe@172 | 403 | expected = ucx_list_append(expected, (void*)"that"); |
universe@172 | 404 | expected = ucx_list_append(expected, (void*)"the"); |
universe@172 | 405 | expected = ucx_list_append(expected, (void*)"this"); |
universe@122 | 406 | expected = ucx_list_append(expected, (void*)"this"); |
universe@35 | 407 | |
universe@122 | 408 | list = ucx_list_sort(list, ucx_strcmp, NULL); |
universe@35 | 409 | |
universe@35 | 410 | UCX_TEST_BEGIN |
universe@35 | 411 | UCX_TEST_ASSERT( |
universe@122 | 412 | ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed"); |
universe@172 | 413 | UCX_TEST_ASSERT(ucx_list_size(list) == 16, "list has now a wrong size"); |
universe@122 | 414 | UcxList *l = list; |
universe@35 | 415 | UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null"); |
universe@35 | 416 | while (l->next != NULL) { |
universe@172 | 417 | UCX_TEST_ASSERT(l->next->prev == l, "next or prev pointer corrupted"); |
universe@35 | 418 | l = l->next; |
universe@35 | 419 | } |
universe@172 | 420 | UCX_TEST_ASSERT(!ucx_list_sort(NULL, ucx_strcmp, NULL), |
universe@172 | 421 | "failed to sort empty list"); |
universe@35 | 422 | UCX_TEST_END |
universe@35 | 423 | |
universe@122 | 424 | ucx_list_free(expected); |
universe@122 | 425 | ucx_list_free(list); |
universe@35 | 426 | } |