olaf@9: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@103: * universe@259: * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. olaf@9: */ olaf@9: universe@122: #include "list_tests.h" universe@251: #include olaf@9: universe@134: UCX_TEST(test_ucx_list_append) { universe@172: UcxList *list, *first; universe@172: list = first = ucx_list_append(NULL, (void*)"Hello"); universe@33: UCX_TEST_BEGIN universe@27: universe@69: UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, universe@69: "failed"); universe@27: universe@122: list = ucx_list_append(list, (void*)" World!"); universe@27: universe@172: UCX_TEST_ASSERT(list == first, "does not return first element"); universe@69: UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, universe@69: "failed"); universe@172: UCX_TEST_ASSERT(list->next->prev == list, "failed"); universe@40: UCX_TEST_ASSERT(list->next->next == NULL, "failed"); universe@33: UCX_TEST_END universe@27: universe@122: ucx_list_free(list); universe@24: } universe@24: universe@134: UCX_TEST(test_ucx_list_prepend) { universe@172: UcxList *list, *last; universe@172: list = last = ucx_list_prepend(NULL, (void*)" World!"); universe@33: UCX_TEST_BEGIN universe@33: universe@122: list = ucx_list_prepend(list, (void*)"Hello"); universe@27: universe@69: UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, universe@69: "failed"); universe@69: UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, universe@69: "failed"); universe@172: UCX_TEST_ASSERT(list == last->prev, "does not return first element"); universe@40: UCX_TEST_ASSERT(list->next->next == NULL, "failed"); universe@172: UCX_TEST_ASSERT(list->prev == NULL, "failed"); universe@27: universe@33: UCX_TEST_END universe@122: ucx_list_free(list); universe@18: } universe@18: universe@228: UCX_TEST(test_ucx_list_append_once) { universe@228: UcxList *list, *first; universe@228: list = first = ucx_list_append_once(NULL, (void*)"Hello", ucx_strcmp, NULL); universe@228: UCX_TEST_BEGIN universe@228: universe@228: UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, universe@228: "failed"); universe@228: universe@228: list = ucx_list_append_once(list, (void*)"Hello", ucx_strcmp, NULL); universe@228: list = ucx_list_append_once(list, (void*)" World!", ucx_strcmp, NULL); universe@228: universe@228: UCX_TEST_ASSERT(list == first, "does not return first element"); universe@228: UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, universe@228: "'Hello' was not inserted _once_"); universe@228: UCX_TEST_ASSERT(list->next->prev == list, "failed"); universe@228: UCX_TEST_ASSERT(list->next->next == NULL, "right not terminated"); universe@228: UCX_TEST_END universe@228: universe@228: ucx_list_free(list); universe@228: } universe@228: universe@134: UCX_TEST(test_ucx_list_equals) { universe@172: const char *hello = "Hello"; universe@172: const char *world = " World!"; universe@172: UcxList *list = ucx_list_append(NULL, (void*)hello); universe@172: list = ucx_list_append(list, (void*)world); universe@172: UcxList *list2 = ucx_list_prepend(NULL, (void*)world); universe@172: list2 = ucx_list_prepend(list2, (void*)hello); universe@122: UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!"); universe@122: list3 = ucx_list_prepend(list3, (void*)"Hallo"); universe@172: UcxList *list4 = ucx_list_prepend(NULL, (void*)" World!"); universe@172: list4 = ucx_list_prepend(list4, (void*)"Hello"); universe@33: UCX_TEST_BEGIN universe@27: universe@172: UCX_TEST_ASSERT(ucx_list_equals(list, list4, ucx_strcmp, NULL), "failed"); universe@122: UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed"); universe@172: UCX_TEST_ASSERT(ucx_list_equals(list, list2, NULL, NULL), "failed"); universe@27: universe@33: UCX_TEST_END universe@172: ucx_list_free(list4); universe@122: ucx_list_free(list3); universe@122: ucx_list_free(list2); universe@122: ucx_list_free(list); universe@24: } universe@24: universe@134: UCX_TEST(test_ucx_list_concat) { universe@122: UcxList *list = ucx_list_append(NULL, (void*)"Hello"); universe@172: list = ucx_list_append(list, (void*)" my "); universe@122: UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!"); universe@172: list2 = ucx_list_prepend(list2, (void*)" sweet "); universe@33: UCX_TEST_BEGIN universe@27: universe@122: list = ucx_list_concat(list, list2); universe@172: list = ucx_list_concat(list, NULL); universe@172: list = ucx_list_concat(NULL, list); universe@27: universe@172: UCX_TEST_ASSERT(!strncmp((const char*)list->data, "Hello", 5), universe@69: "failed"); universe@172: UCX_TEST_ASSERT(!strncmp((const char*)list->next->data, " my ", 4), universe@69: "failed"); universe@172: UCX_TEST_ASSERT(!strncmp((const char*)list->next->next->data, " sweet ", 7), universe@172: "failed"); universe@172: UCX_TEST_ASSERT(!strncmp((const char*)ucx_list_last(list)->data, universe@172: " World!", 7), "failed"); universe@172: universe@172: UCX_TEST_ASSERT(list->prev == NULL, "failed"); universe@27: universe@33: UCX_TEST_END universe@172: // don't free list2, as it is freed by freeing list; universe@122: ucx_list_free(list); olaf@9: } olaf@9: universe@134: UCX_TEST(test_ucx_list_size) { universe@122: UcxList *list = ucx_list_append(NULL, (void*)"This "); universe@122: list = ucx_list_append(list, (void*)"list "); universe@122: list = ucx_list_append(list, (void*)"has "); universe@122: list = ucx_list_append(list, (void*)"size "); universe@122: list = ucx_list_append(list, (void*)"5!"); universe@27: universe@123: UCX_TEST_BEGIN universe@123: universe@122: UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed"); universe@172: list = ucx_list_remove(list, ucx_list_get(list, 2)); universe@172: UCX_TEST_ASSERT(ucx_list_size(list) == 4, "failed after removal"); universe@27: universe@33: UCX_TEST_END universe@122: ucx_list_free(list); olaf@9: } olaf@11: universe@134: UCX_TEST(test_ucx_list_first) { universe@122: UcxList *list = ucx_list_append(NULL, (void*)"Find "); universe@122: list = ucx_list_append(list, (void*)"the "); universe@122: list = ucx_list_append(list, (void*)"first!"); universe@27: universe@123: UCX_TEST_BEGIN universe@123: universe@122: const char* first = (const char*) (ucx_list_first(list)->data); universe@27: universe@27: UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed"); universe@172: UCX_TEST_ASSERT(ucx_list_first(list->next->next) == list, "failed"); universe@172: UCX_TEST_ASSERT(!ucx_list_first(NULL), universe@172: "does not return NULL on an empty list"); universe@27: universe@33: UCX_TEST_END universe@122: ucx_list_free(list); universe@27: } universe@27: universe@134: UCX_TEST(test_ucx_list_last) { universe@122: UcxList *list = ucx_list_append(NULL, (void*)"Find "); universe@122: list = ucx_list_append(list, (void*)"the "); universe@122: list = ucx_list_append(list, (void*)"last!"); universe@27: universe@123: UCX_TEST_BEGIN universe@123: universe@172: const char* last = (const char*) (ucx_list_last(list->next->next)->data); universe@27: universe@27: UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed"); universe@172: UCX_TEST_ASSERT(ucx_list_last(list) == list->next->next, "failed"); universe@172: UCX_TEST_ASSERT(!ucx_list_last(NULL), universe@172: "does not return NULL on an empty list"); universe@27: universe@33: UCX_TEST_END universe@122: ucx_list_free(list); universe@27: } universe@27: universe@134: UCX_TEST(test_ucx_list_get) { universe@122: UcxList *list = ucx_list_append(NULL, (void*)"Find "); universe@122: list = ucx_list_append(list, (void*)"the "); universe@122: list = ucx_list_append(list, (void*)"mid!"); universe@27: universe@123: UCX_TEST_BEGIN universe@123: universe@172: const char* first = (const char*) (ucx_list_get(list, 0)->data); universe@122: const char* mid = (const char*) (ucx_list_get(list, 1)->data); universe@172: const char* last = (const char*) (ucx_list_get(list, 2)->data); universe@27: universe@172: UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed"); universe@27: UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed"); universe@172: UCX_TEST_ASSERT(strncmp(last, "mid!", 4) == 0, "failed"); universe@172: UCX_TEST_ASSERT(!ucx_list_get(list, -1), "out of bounds (neg)"); universe@172: UCX_TEST_ASSERT(!ucx_list_get(list, 3), "out of bounds"); universe@172: UCX_TEST_ASSERT(!ucx_list_get(NULL, 0), "empty list"); universe@27: universe@33: UCX_TEST_END universe@122: ucx_list_free(list); universe@27: } universe@27: universe@134: UCX_TEST(test_ucx_list_indexof) { universe@123: UcxList *list = ucx_list_append(NULL, (void*)"Find "); universe@123: list = ucx_list_append(list, (void*)"the "); universe@123: list = ucx_list_append(list, (void*)"mid!"); universe@123: universe@123: UCX_TEST_BEGIN universe@123: universe@123: UCX_TEST_ASSERT(ucx_list_indexof(list, list) == 0, "failed"); universe@123: UCX_TEST_ASSERT(ucx_list_indexof(list, list->next) == 1, "failed"); universe@123: UCX_TEST_ASSERT(ucx_list_indexof(list, ucx_list_get(list, 2)) == 2, universe@123: "failed"); universe@123: universe@172: UcxList *otherlist = ucx_list_append(NULL, (void*) "the "); universe@123: UCX_TEST_ASSERT(ucx_list_indexof(list, otherlist) == -1, "failed"); universe@172: UCX_TEST_ASSERT(ucx_list_indexof(NULL, otherlist) == -1, "empty list"); universe@172: universe@123: ucx_list_free(otherlist); universe@123: universe@123: UCX_TEST_END universe@123: ucx_list_free(list); universe@123: } universe@123: universe@134: UCX_TEST(test_ucx_list_find) { universe@172: const char* teststr = "string!"; universe@123: UcxList *l = ucx_list_append(NULL, (void*)"find "); universe@123: l = ucx_list_append(l, (void*)"some "); universe@172: l = ucx_list_append(l, (void*)teststr); universe@123: universe@123: UCX_TEST_BEGIN universe@123: universe@123: UCX_TEST_ASSERT(ucx_list_find(l,(void*)"some ",ucx_strcmp,NULL) == 1, universe@123: "doesn't find string"); universe@123: UCX_TEST_ASSERT(ucx_list_find(l,(void*)"a",ucx_strcmp,NULL) == -1, universe@123: "finds non-existing string"); universe@123: universe@172: UCX_TEST_ASSERT(ucx_list_find(l,(void*)teststr,NULL,NULL) == 2, universe@172: "doesn't find integer without cmp_func"); universe@172: universe@172: UCX_TEST_ASSERT(ucx_list_find(NULL, (void*)"some ",ucx_strcmp,NULL) == -1, universe@172: "empty list"); universe@172: universe@123: UCX_TEST_END universe@123: ucx_list_free(l); universe@123: } universe@123: universe@134: UCX_TEST(test_ucx_list_contains) { universe@122: UcxList *l = ucx_list_append(NULL, (void*)"Contains "); universe@122: l = ucx_list_append(l, (void*)"a "); universe@122: l = ucx_list_append(l, (void*)"string!"); universe@90: universe@123: UCX_TEST_BEGIN universe@123: universe@123: UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL), universe@123: "false negative"); universe@123: UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL), universe@123: "false positive"); universe@90: universe@90: UCX_TEST_END universe@122: ucx_list_free(l); universe@90: } universe@90: universe@134: UCX_TEST(test_ucx_list_remove) { universe@122: UcxList *list = ucx_list_append(NULL, (void*)"Hello"); olaf@162: list = ucx_list_append(list, (void*)"fucking"); olaf@162: list = ucx_list_append(list, (void*)"World!"); olaf@162: olaf@162: UcxList *list2 = ucx_list_append(NULL, (void*)"A"); olaf@162: list2 = ucx_list_append(list2, (void*)"B"); olaf@162: list2 = ucx_list_append(list2, (void*)"C"); olaf@162: list2 = ucx_list_append(list2, (void*)"D"); olaf@162: list2 = ucx_list_append(list2, (void*)"E"); olaf@162: list2 = ucx_list_append(list2, (void*)"F"); olaf@162: list2 = ucx_list_append(list2, (void*)"G"); universe@27: universe@123: UCX_TEST_BEGIN universe@123: universe@122: list = ucx_list_remove(list, ucx_list_get(list, 1)); universe@27: universe@69: UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, universe@69: "failed"); olaf@162: UCX_TEST_ASSERT(strncmp((const char*)list->next->data, "World!", 7) == 0, universe@69: "failed"); universe@40: UCX_TEST_ASSERT(list->next->next == NULL, "failed"); universe@27: olaf@162: // remove first element: B, C, D, E, F, G olaf@162: list2 = ucx_list_remove(list2, list2); olaf@162: olaf@162: UCX_TEST_ASSERT(ucx_list_size(list2) == 6, "list2 has wrong size"); olaf@162: UCX_TEST_ASSERT(strncmp((const char*)list2->data, "B", 1) == 0, olaf@162: "wrong first element"); olaf@162: UCX_TEST_ASSERT(strncmp((const char*)ucx_list_get(list2, 5)->data, "G", 1) olaf@162: == 0, "wrong last element"); olaf@162: olaf@162: // remove second element: B, D, E, F, G olaf@162: list2 = ucx_list_remove(list2, list2->next); olaf@162: olaf@162: UCX_TEST_ASSERT(ucx_list_size(list2) == 5, "list2 has wrong size"); olaf@162: UCX_TEST_ASSERT(strncmp((const char*)list2->next->data, "D", 1) == 0, olaf@162: "wrong second element"); olaf@162: olaf@162: UcxList *last = ucx_list_get(list2, 4); olaf@162: list2 = ucx_list_remove(list2, last->prev); olaf@162: olaf@162: UCX_TEST_ASSERT(ucx_list_size(list2) == 4, "list2 has wrong size"); olaf@162: UCX_TEST_ASSERT(strncmp((const char*)last->prev->data, "E", 1) == 0, olaf@162: "wrong element"); olaf@162: olaf@162: // remove last element: B, D, E, F olaf@162: list2 = ucx_list_remove(list2, last); olaf@162: UCX_TEST_ASSERT(ucx_list_size(list2) == 3, "list2 has wrong size"); olaf@162: UCX_TEST_ASSERT(strncmp((const char*)ucx_list_get(list2, 2)->data, "E", 1) olaf@162: == 0, "wrong last element"); olaf@162: olaf@162: UCX_TEST_ASSERT(strncmp((const char*)list2->data, "B", 1) == 0, olaf@162: "wrong element"); olaf@162: olaf@162: list2 = ucx_list_remove(list2, list2); olaf@162: UCX_TEST_ASSERT(ucx_list_size(list2) == 2, "list2 has wrong size"); olaf@162: list2 = ucx_list_remove(list2, list2); olaf@162: UCX_TEST_ASSERT(ucx_list_size(list2) == 1, "list2 has wrong size"); olaf@162: list2 = ucx_list_remove(list2, list2); olaf@162: UCX_TEST_ASSERT(list2 == NULL, "list2 is not null"); olaf@162: universe@33: UCX_TEST_END universe@122: ucx_list_free(list); universe@27: } universe@27: universe@134: UCX_TEST(test_ucx_list_clone) { universe@27: universe@27: char *hello = (char*)malloc(6); universe@27: char *world = (char*)malloc(8); universe@27: universe@27: memcpy(hello, "Hello", 6); universe@27: memcpy(world, " World!", 8); universe@27: universe@122: UcxList *list = ucx_list_append(NULL, hello); universe@122: list = ucx_list_append(list, world); universe@27: universe@122: UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL); universe@33: UCX_TEST_BEGIN universe@27: universe@122: UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed"); universe@40: UCX_TEST_ASSERT(hello != copy->data, "first element is no copy"); universe@40: UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy"); universe@27: universe@33: UCX_TEST_END universe@211: universe@212: ucx_list_free_content(copy, free); universe@27: universe@27: free(world); universe@27: free(hello); universe@122: ucx_list_free(list); universe@122: ucx_list_free(copy); universe@27: } universe@35: universe@134: UCX_TEST(test_ucx_list_sort) { universe@122: UcxList *list = ucx_list_append(NULL, (void*)"this"); universe@122: list = ucx_list_append(list, (void*)"is"); universe@122: list = ucx_list_append(list, (void*)"a"); universe@122: list = ucx_list_append(list, (void*)"test"); universe@122: list = ucx_list_append(list, (void*)"for"); universe@122: list = ucx_list_append(list, (void*)"partial"); universe@122: list = ucx_list_append(list, (void*)"correctness"); universe@172: list = ucx_list_append(list, (void*)"of"); universe@172: list = ucx_list_append(list, (void*)"the"); universe@172: list = ucx_list_append(list, (void*)"sort"); universe@172: list = ucx_list_append(list, (void*)"function"); universe@172: list = ucx_list_append(list, (void*)"that"); universe@172: list = ucx_list_append(list, (void*)"shall"); universe@172: list = ucx_list_append(list, (void*)"pass"); universe@172: list = ucx_list_append(list, (void*)"this"); universe@172: list = ucx_list_append(list, (void*)"test"); universe@35: universe@122: UcxList *expected = ucx_list_append(NULL, (void*)"a"); universe@122: expected = ucx_list_append(expected, (void*)"correctness"); universe@122: expected = ucx_list_append(expected, (void*)"for"); universe@172: expected = ucx_list_append(expected, (void*)"function"); universe@122: expected = ucx_list_append(expected, (void*)"is"); universe@172: expected = ucx_list_append(expected, (void*)"of"); universe@122: expected = ucx_list_append(expected, (void*)"partial"); universe@172: expected = ucx_list_append(expected, (void*)"pass"); universe@172: expected = ucx_list_append(expected, (void*)"shall"); universe@172: expected = ucx_list_append(expected, (void*)"sort"); universe@122: expected = ucx_list_append(expected, (void*)"test"); universe@172: expected = ucx_list_append(expected, (void*)"test"); universe@172: expected = ucx_list_append(expected, (void*)"that"); universe@172: expected = ucx_list_append(expected, (void*)"the"); universe@172: expected = ucx_list_append(expected, (void*)"this"); universe@122: expected = ucx_list_append(expected, (void*)"this"); universe@35: universe@122: list = ucx_list_sort(list, ucx_strcmp, NULL); universe@35: universe@35: UCX_TEST_BEGIN universe@35: UCX_TEST_ASSERT( universe@122: ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed"); universe@172: UCX_TEST_ASSERT(ucx_list_size(list) == 16, "list has now a wrong size"); universe@122: UcxList *l = list; universe@35: UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null"); universe@35: while (l->next != NULL) { universe@172: UCX_TEST_ASSERT(l->next->prev == l, "next or prev pointer corrupted"); universe@35: l = l->next; universe@35: } universe@172: UCX_TEST_ASSERT(!ucx_list_sort(NULL, ucx_strcmp, NULL), universe@172: "failed to sort empty list"); universe@35: UCX_TEST_END universe@35: universe@122: ucx_list_free(expected); universe@122: ucx_list_free(list); universe@35: }