test/list_tests.c

Mon, 30 Dec 2019 09:52:07 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 30 Dec 2019 09:52:07 +0100
branch
feature/array
changeset 387
7e0f19fe23ff
parent 323
b8c49e7a1dba
child 371
365b24f20f98
permissions
-rw-r--r--

closes array branch towards ucx 2.1 release

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

mercurial