test/list_tests.c

Tue, 05 Oct 2021 16:22:48 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 05 Oct 2021 16:22:48 +0200
branch
support/2.x
changeset 471
e9ef2637e101
parent 371
365b24f20f98
permissions
-rw-r--r--

add new ucx_list_sort test

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 }
universe@371 406
universe@471 407 UCX_TEST(test_ucx_list_sort_int) {
universe@471 408 int expected[] = {
universe@471 409 14, 30, 151, 163, 227, 300, 315, 317, 363, 398, 417, 424, 438, 446, 508, 555, 605, 713, 716, 759, 761, 880,
universe@471 410 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 1707, 1734, 1771, 1874, 1894,
universe@471 411 1976, 2079, 2124, 2130, 2135, 2266, 2338, 2358, 2430, 2500, 2540, 2542, 2546, 2711, 2733, 2754, 2764, 2797,
universe@471 412 2888, 2900, 3020, 3053, 3109, 3244, 3275, 3302, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 3675, 3677,
universe@471 413 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681,
universe@471 414 4785, 4791, 4801, 4859, 4903, 4973
universe@471 415 };
universe@471 416 int scrambled[] = {
universe@471 417 759, 716, 880, 761, 2358, 2542, 2500, 2540, 2546, 2711, 2430, 1707, 1874, 1771, 1894, 1734, 1976, 2079,
universe@471 418 2124, 2130, 2135, 2266, 2338, 2733, 2754, 2764, 2797, 3362, 3363, 3364, 3441, 3515, 3539, 3579, 3655, 2888,
universe@471 419 2900, 3020, 3053, 3109, 3244, 3275, 3302, 438, 446, 508, 555, 605, 713, 14, 30, 151, 163, 227, 300,
universe@471 420 894, 1034, 1077, 1191, 1231, 1264, 1297, 1409, 1423, 1511, 1544, 1659, 1686, 315, 317, 363, 398, 417, 424,
universe@471 421 3675, 3677, 3718, 3724, 3757, 3866, 3896, 3906, 3941, 3984, 3994, 4785, 4791, 4801, 4859, 4903, 4973,
universe@471 422 4016, 4085, 4121, 4254, 4319, 4366, 4459, 4514, 4681
universe@471 423 };
universe@471 424
universe@471 425 UcxList *list = NULL;
universe@471 426 for (int i = 0 ; i < 100 ; i++) {
universe@471 427 list = ucx_list_append(list, scrambled+i);
universe@471 428 }
universe@471 429
universe@471 430 list = ucx_list_sort(list, ucx_cmp_int, NULL);
universe@471 431
universe@471 432 UCX_TEST_BEGIN
universe@471 433
universe@471 434 UCX_TEST_ASSERT(list->prev == NULL, "prev field of first entry is not null");
universe@471 435 UcxList *check = list;
universe@471 436 UcxList *check_last = NULL;
universe@471 437 for (int i = 0 ; i < 100 ; i++) {
universe@471 438 UCX_TEST_ASSERT(*(int*)check->data == expected[i], "data not correctly sorted");
universe@471 439 UCX_TEST_ASSERT(check->prev == check_last, "prev pointer not correct");
universe@471 440 if (i < 99) {
universe@471 441 UCX_TEST_ASSERT(check->next != NULL, "next pointer not correct");
universe@471 442 }
universe@471 443 check_last = check;
universe@471 444 check = check->next;
universe@471 445 }
universe@471 446 UCX_TEST_ASSERT(check == NULL, "next pointer of last element not null");
universe@471 447
universe@471 448 UCX_TEST_END
universe@471 449
universe@471 450 ucx_list_free(list);
universe@471 451 }
universe@471 452
universe@371 453 UCX_TEST(test_ucx_list_union) {
universe@371 454 UcxList *left = ucx_list_append(NULL, (void*)"this");
universe@371 455 left = ucx_list_append(left, (void*)"is");
universe@371 456 left = ucx_list_append(left, (void*)"a");
universe@371 457 left = ucx_list_append(left, (void*)"test");
universe@371 458
universe@371 459 UcxList *right = ucx_list_append(NULL, (void*)"to");
universe@371 460 right = ucx_list_append(right, (void*)"test");
universe@371 461 right = ucx_list_append(right, (void*)"set");
universe@371 462 right = ucx_list_append(right, (void*)"operations");
universe@371 463
universe@371 464 UcxList *expected = ucx_list_append(NULL, (void*)"this");
universe@371 465 expected = ucx_list_append(expected, (void*)"is");
universe@371 466 expected = ucx_list_append(expected, (void*)"a");
universe@371 467 expected = ucx_list_append(expected, (void*)"test");
universe@371 468 expected = ucx_list_append(expected, (void*)"to");
universe@371 469 expected = ucx_list_append(expected, (void*)"set");
universe@371 470 expected = ucx_list_append(expected, (void*)"operations");
universe@371 471
universe@371 472 UcxList* result = ucx_list_union(left, right, ucx_cmp_str,
universe@371 473 NULL, NULL, NULL);
universe@371 474
universe@371 475 UCX_TEST_BEGIN
universe@371 476 UCX_TEST_ASSERT(ucx_list_equals(result, expected,
universe@371 477 ucx_cmp_str, NULL), "failed");
universe@371 478 UCX_TEST_END
universe@371 479
universe@371 480 ucx_list_free(result);
universe@371 481 ucx_list_free(expected);
universe@371 482 ucx_list_free(right);
universe@371 483 ucx_list_free(left);
universe@371 484 }
universe@371 485
universe@371 486 UCX_TEST(test_ucx_list_intersection) {
universe@371 487 UcxList *left = ucx_list_append(NULL, (void*)"this");
universe@371 488 left = ucx_list_append(left, (void*)"is");
universe@371 489 left = ucx_list_append(left, (void*)"a");
universe@371 490 left = ucx_list_append(left, (void*)"test");
universe@371 491
universe@371 492 UcxList *right = ucx_list_append(NULL, (void*)"to");
universe@371 493 right = ucx_list_append(right, (void*)"test");
universe@371 494 right = ucx_list_append(right, (void*)"a");
universe@371 495 right = ucx_list_append(right, (void*)"set");
universe@371 496 right = ucx_list_append(right, (void*)"operation");
universe@371 497
universe@371 498 UcxList *expected = ucx_list_append(NULL, (void*)"a");
universe@371 499 expected = ucx_list_append(expected, (void*)"test");
universe@371 500
universe@371 501 UcxList* result = ucx_list_intersection(left, right, ucx_cmp_str,
universe@371 502 NULL, NULL, NULL);
universe@371 503
universe@371 504 UCX_TEST_BEGIN
universe@371 505 UCX_TEST_ASSERT(ucx_list_equals(result, expected,
universe@371 506 ucx_cmp_str, NULL), "failed");
universe@371 507 UCX_TEST_END
universe@371 508
universe@371 509 ucx_list_free(result);
universe@371 510 ucx_list_free(expected);
universe@371 511 ucx_list_free(right);
universe@371 512 ucx_list_free(left);
universe@371 513 }
universe@371 514
universe@371 515 UCX_TEST(test_ucx_list_difference) {
universe@371 516 UcxList *left = ucx_list_append(NULL, (void*)"this");
universe@371 517 left = ucx_list_append(left, (void*)"is");
universe@371 518 left = ucx_list_append(left, (void*)"a");
universe@371 519 left = ucx_list_append(left, (void*)"test");
universe@371 520
universe@371 521 UcxList *right = ucx_list_append(NULL, (void*)"to");
universe@371 522 right = ucx_list_append(right, (void*)"test");
universe@371 523 right = ucx_list_append(right, (void*)"this");
universe@371 524 right = ucx_list_append(right, (void*)"set");
universe@371 525 right = ucx_list_append(right, (void*)"operations");
universe@371 526
universe@371 527 UcxList *expected = ucx_list_append(NULL, (void*)"is");
universe@371 528 expected = ucx_list_append(expected, (void*)"a");
universe@371 529
universe@371 530 UcxList* result = ucx_list_difference(left, right, ucx_cmp_str,
universe@371 531 NULL, NULL, NULL);
universe@371 532
universe@371 533 UCX_TEST_BEGIN
universe@371 534 UCX_TEST_ASSERT(ucx_list_equals(result, expected,
universe@371 535 ucx_cmp_str, NULL), "failed");
universe@371 536 UCX_TEST_END
universe@371 537
universe@371 538 ucx_list_free(result);
universe@371 539 ucx_list_free(expected);
universe@371 540 ucx_list_free(right);
universe@371 541 ucx_list_free(left);
universe@371 542 }

mercurial