test/list_tests.c

Fri, 12 Oct 2012 10:54:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Oct 2012 10:54:55 +0200
changeset 69
fb59270b1de3
parent 40
583718dd4cf3
child 71
303dabadff1c
permissions
-rw-r--r--

made the code work with VC++ compiler (use make CONF=windows)

/*
 * tests of list implementation
 */

#include "list_tests.h"

UCX_TEST_IMPLEMENT(test_ucx_list_append) {
    UcxList *list = ucx_list_append(NULL, "Hello");
    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
            "failed");
    
    list = ucx_list_append(list, " 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, " World!");
    UCX_TEST_BEGIN
    list = ucx_list_prepend(list, "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, "Hello");
    list = ucx_list_append(list, " World!");
    UcxList *list2 = ucx_list_prepend(NULL, " World!");
    list2 = ucx_list_prepend(list2, "Hello");
    UcxList *list3 = ucx_list_prepend(NULL, " Welt!");
    list3 = ucx_list_prepend(list3, "Hallo");
    
    UCX_TEST_BEGIN
    UCX_TEST_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed");
    UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, 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, "Hello");
    UcxList *list2 = ucx_list_prepend(NULL, " 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, "This ");
    UCX_TEST_BEGIN
    list = ucx_list_append(list, "list ");
    list = ucx_list_append(list, "has ");
    list = ucx_list_append(list, "size ");
    list = ucx_list_append(list, "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, "Find ");
    UCX_TEST_BEGIN
    list = ucx_list_append(list, "the ");
    list = ucx_list_append(list, "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, "Find ");
    UCX_TEST_BEGIN
    list = ucx_list_append(list, "the ");
    list = ucx_list_append(list, "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_remove) {
    UcxList *list = ucx_list_append(NULL, "Hello");
    UCX_TEST_BEGIN
    list = ucx_list_append(list, " fucking");
    list = ucx_list_append(list, " 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, copy_string, NULL);
    UCX_TEST_BEGIN

    UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, 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, "this");
    list = ucx_list_append(list, "is");
    list = ucx_list_append(list, "a");
    list = ucx_list_append(list, "test");
    list = ucx_list_append(list, "for");
    list = ucx_list_append(list, "partial");
    list = ucx_list_append(list, "correctness");

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

    list = ucx_list_sort(list, cmp_string, NULL);

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

    ucx_list_free(expected);
    ucx_list_free(list);
}

mercurial