test/list_tests.c

Thu, 28 Feb 2013 08:17:26 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 28 Feb 2013 08:17:26 +0100
changeset 102
7c8ebad4d973
parent 94
57ea041df22f
child 103
08018864fb91
permissions
-rw-r--r--

added headers that are missing under visual studio

/*
 * tests of list implementation
 */

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

UCX_TEST_IMPLEMENT(test_ucx_list_append) {
    UcxList *list = ucx_list_append(NULL, (void*)"Hello");
    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
            "failed");
    
    list = ucx_list_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_list_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_list_prepend) {
    UcxList *list = ucx_list_prepend(NULL, (void*)" World!");
    UCX_TEST_BEGIN
    list = ucx_list_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_list_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_list_equals) {
    UcxList *list = ucx_list_append(NULL, (void*)"Hello");
    list = ucx_list_append(list, (void*)" World!");
    UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
    list2 = ucx_list_prepend(list2, (void*)"Hello");
    UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!");
    list3 = ucx_list_prepend(list3, (void*)"Hallo");
    
    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(ucx_list_equals(list, list2, ucx_strcmp, NULL), "failed");
    UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
    UCX_TEST_END
    
    ucx_list_free(list3);
    ucx_list_free(list2);
    ucx_list_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_list_concat) {
    UcxList *list = ucx_list_append(NULL, (void*)"Hello");
    UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
    
    list = ucx_list_concat(list, list2);
    UCX_TEST_BEGIN
    
    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
    if (list->next == NULL) {
        ucx_list_free(list2);
    }
    ucx_list_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_list_size) {
    UcxList *list = ucx_list_append(NULL, (void*)"This ");
    UCX_TEST_BEGIN
    list = ucx_list_append(list, (void*)"list ");
    list = ucx_list_append(list, (void*)"has ");
    list = ucx_list_append(list, (void*)"size ");
    list = ucx_list_append(list, (void*)"5!");
    
    UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
    
    UCX_TEST_END
    ucx_list_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_list_last) {
    UcxList *list = ucx_list_append(NULL, (void*)"Find ");
    UCX_TEST_BEGIN
    list = ucx_list_append(list, (void*)"the ");
    list = ucx_list_append(list, (void*)"last!");
    
    const char* last = (const char*) (ucx_list_last(list)->data);
    
    UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
    
    UCX_TEST_END
    ucx_list_free(list);
    
}

UCX_TEST_IMPLEMENT(test_ucx_list_get) {
    UcxList *list = ucx_list_append(NULL, (void*)"Find ");
    UCX_TEST_BEGIN
    list = ucx_list_append(list, (void*)"the ");
    list = ucx_list_append(list, (void*)"mid!");
    
    const char* mid = (const char*) (ucx_list_get(list, 1)->data);
    
    UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
    
    UCX_TEST_END
    ucx_list_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_list_contains) {
    UcxList *l = ucx_list_append(NULL, (void*)"Contains ");
    UCX_TEST_BEGIN
    l = ucx_list_append(l, (void*)"a ");
    l = ucx_list_append(l, (void*)"string!");
    
    UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL), "failed");
    UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL), "failed");
    
    UCX_TEST_END
    ucx_list_free(l);
}

UCX_TEST_IMPLEMENT(test_ucx_list_remove) {
    UcxList *list = ucx_list_append(NULL, (void*)"Hello");
    UCX_TEST_BEGIN
    list = ucx_list_append(list, (void*)" fucking");
    list = ucx_list_append(list, (void*)" World!");
    
    list = ucx_list_remove(list, ucx_list_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_list_free(list);
}

UCX_TEST_IMPLEMENT(test_ucx_list_clone) {
   
    char *hello = (char*)malloc(6);
    char *world = (char*)malloc(8);
    
    memcpy(hello, "Hello", 6);
    memcpy(world, " World!", 8);
    
    UcxList *list = ucx_list_append(NULL, hello);
    list = ucx_list_append(list, world);
    
    UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL);
    UCX_TEST_BEGIN

    UCX_TEST_ASSERT(ucx_list_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_list_free(list);
    ucx_list_free(copy);
}

UCX_TEST_IMPLEMENT(test_ucx_list_sort) {
    UcxList *list = ucx_list_append(NULL, (void*)"this");
    list = ucx_list_append(list, (void*)"is");
    list = ucx_list_append(list, (void*)"a");
    list = ucx_list_append(list, (void*)"test");
    list = ucx_list_append(list, (void*)"for");
    list = ucx_list_append(list, (void*)"partial");
    list = ucx_list_append(list, (void*)"correctness");

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

    list = ucx_list_sort(list, ucx_strcmp, NULL);

    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(
            ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
    UCX_TEST_END

    ucx_list_free(expected);
    ucx_list_free(list);
}

mercurial