test/dlist_tests.c

Fri, 21 Jun 2013 10:50:50 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 21 Jun 2013 10:50:50 +0200
changeset 105
f08fa6308d1f
parent 103
08018864fb91
permissions
-rw-r--r--

fixed logging test

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "dlist_tests.h"
#include "ucx/utils.h"

UCX_TEST_IMPLEMENT(test_ucx_dlist_append) {
    UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello");
    UCX_TEST_BEGIN
    
    UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
            "failed");
    
    list = ucx_dlist_append(list, (void*)" World!");
    
    UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
            "failed");
    UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    UCX_TEST_END
    
    ucx_dlist_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_dlist_prepend) {
    UcxDlist *list = ucx_dlist_prepend(NULL, (void*)" World!");
    UCX_TEST_BEGIN

    list = ucx_dlist_prepend(list, (void*)"Hello");
    
    UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
            "failed");
    UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
            "failed");
    UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    
    UCX_TEST_END
    ucx_dlist_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_dlist_equals) {
    UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello");
    list = ucx_dlist_append(list, (void*)" World!");
    UcxDlist *list2 = ucx_dlist_prepend(NULL, (void*)" World!");
    list2 = ucx_dlist_prepend(list2, (void*)"Hello");
    UcxDlist *list3 = ucx_dlist_prepend(NULL, (void*)" Welt!");
    list3 = ucx_dlist_prepend(list3, (void*)"Hallo");
    UCX_TEST_BEGIN
    
    UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, ucx_strcmp, NULL), "failed");
    UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, ucx_strcmp, NULL), "failed");
    
    UCX_TEST_END
    ucx_dlist_free(list3);
    ucx_dlist_free(list2);
    ucx_dlist_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_dlist_concat) {
    UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello");
    UcxDlist *list2 = ucx_dlist_prepend(NULL, (void*)" World!");
    UCX_TEST_BEGIN
    
    list = ucx_dlist_concat(list, list2);
    
    UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
            "failed");
    UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
            "failed");
    UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    
    UCX_TEST_END
    ucx_dlist_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_dlist_size) {
    UcxDlist *list = ucx_dlist_append(NULL, (void*)"This ");
    UCX_TEST_BEGIN
    list = ucx_dlist_append(list, (void*)"list ");
    list = ucx_dlist_append(list, (void*)"has ");
    list = ucx_dlist_append(list, (void*)"size ");
    list = ucx_dlist_append(list, (void*)"5!");
    
    UCX_TEST_ASSERT(ucx_dlist_size(list) == 5, "failed");
    
    UCX_TEST_END
    ucx_dlist_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_dlist_first) {
    UcxDlist *list = ucx_dlist_append(NULL, (void*)"Find ");
    UCX_TEST_BEGIN
    list = ucx_dlist_append(list, (void*)"the ");
    list = ucx_dlist_append(list, (void*)"first!");
    
    const char* first = (const char*) (ucx_dlist_first(list)->data);
    
    UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
    
    UCX_TEST_END
    ucx_dlist_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_dlist_last) {
    UcxDlist *list = ucx_dlist_append(NULL, (void*)"Find ");
    UCX_TEST_BEGIN
    list = ucx_dlist_append(list, (void*)"the ");
    list = ucx_dlist_append(list, (void*)"last!");
    
    const char* last = (const char*) (ucx_dlist_last(list)->data);
    
    UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
    
    UCX_TEST_END
    ucx_dlist_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_dlist_get) {
    UcxDlist *list = ucx_dlist_append(NULL, (void*)"Find ");
    UCX_TEST_BEGIN
    list = ucx_dlist_append(list, (void*)"the ");
    list = ucx_dlist_append(list, (void*)"mid!");
    
    const char* mid = (const char*) (ucx_dlist_get(list, 1)->data);
    
    UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
    
    UCX_TEST_END
    ucx_dlist_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_dlist_contains) {
    UcxDlist *l = ucx_dlist_append(NULL, (void*)"Contains ");
    UCX_TEST_BEGIN
    l = ucx_dlist_append(l, (void*)"a ");
    l = ucx_dlist_append(l, (void*)"string!");
    
    UCX_TEST_ASSERT(ucx_dlist_contains(l,(void*)"a ",ucx_strcmp,NULL),"failed");
    UCX_TEST_ASSERT(!ucx_dlist_contains(l,(void*)"a",ucx_strcmp,NULL),"failed");
    
    UCX_TEST_END
    ucx_dlist_free(l);
}

UCX_TEST_IMPLEMENT(test_ucx_dlist_remove) {
    UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello");
    UCX_TEST_BEGIN
    list = ucx_dlist_append(list, (void*)" fucking");
    list = ucx_dlist_append(list, (void*)" World!");
    
    list = ucx_dlist_remove(list, ucx_dlist_get(list, 1));
    
    UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
            "failed");
    UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
            "failed");
    UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    
    UCX_TEST_END
    ucx_dlist_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_dlist_clone) {
   
    char *hello = (char*)malloc(6);
    char *world = (char*)malloc(8);
    
    memcpy(hello, "Hello", 6);
    memcpy(world, " World!", 8);
    
    UcxDlist *list = ucx_dlist_append(NULL, hello);
    list = ucx_dlist_append(list, world);
    
    UcxDlist *copy = ucx_dlist_clone(list, ucx_strcpy, NULL);
    UCX_TEST_BEGIN

    UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, ucx_strcmp, NULL), "failed");
    UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
    UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");

    UCX_TEST_END
    free(copy->next->data);
    free(copy->data);

    free(world);
    free(hello);
    ucx_dlist_free(list);
    ucx_dlist_free(copy);
}

UCX_TEST_IMPLEMENT(test_ucx_dlist_sort) {
    UcxDlist *list = ucx_dlist_append(NULL, (void*)"this");
    list = ucx_dlist_append(list, (void*)"is");
    list = ucx_dlist_append(list, (void*)"a");
    list = ucx_dlist_append(list, (void*)"test");
    list = ucx_dlist_append(list, (void*)"for");
    list = ucx_dlist_append(list, (void*)"partial");
    list = ucx_dlist_append(list, (void*)"correctness");

    UcxDlist *expected = ucx_dlist_append(NULL, (void*)"a");
    expected = ucx_dlist_append(expected, (void*)"correctness");
    expected = ucx_dlist_append(expected, (void*)"for");
    expected = ucx_dlist_append(expected, (void*)"is");
    expected = ucx_dlist_append(expected, (void*)"partial");
    expected = ucx_dlist_append(expected, (void*)"test");
    expected = ucx_dlist_append(expected, (void*)"this");

    list = ucx_dlist_sort(list, ucx_strcmp, NULL);

    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(
            ucx_dlist_equals(list, expected, ucx_strcmp, NULL), "failed");
    UcxDlist *l = list;
    UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
    while (l->next != NULL) {
        UCX_TEST_ASSERT(l->next->prev == l, "prev pointer corrupted");
        l = l->next;
    }
    UCX_TEST_END

    ucx_dlist_free(expected);
    ucx_dlist_free(list);
}

mercurial