test/list_tests.c

Fri, 24 Feb 2012 15:53:50 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 24 Feb 2012 15:53:50 +0100
changeset 30
23bb65cbf7a4
parent 27
22644e2572bc
child 33
9c219a62070d
permissions
-rw-r--r--

some fixes

/*
 * tests of list implementation
 */

#include "list_tests.h"

UCX_TEST_BEGIN(test_ucx_list_append) {
    UcxList *list = ucx_list_append(NULL, "Hello");
    
    UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    
    list = ucx_list_append(list, " World!");
    
    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    
    ucx_list_free(list);
    
    UCX_TEST_END
}

UCX_TEST_BEGIN(test_ucx_list_prepend) {
    UcxList *list = ucx_list_prepend(NULL, " World!");
    list = ucx_list_prepend(list, "Hello");
    
    UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    
    ucx_list_free(list);
    
    UCX_TEST_END
}

UCX_TEST_BEGIN(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_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed")
    UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, NULL), "failed")
    
    ucx_list_free(list3);
    ucx_list_free(list2);
    ucx_list_free(list);
    
    UCX_TEST_END
}

UCX_TEST_BEGIN(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_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    
    ucx_list_free(list);
    
    UCX_TEST_END
}

UCX_TEST_BEGIN(test_ucx_list_size) {
    UcxList *list = ucx_list_append(NULL, "This ");
    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_list_free(list);
    
    UCX_TEST_END
}

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

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

UCX_TEST_BEGIN(test_ucx_list_remove) {
    UcxList *list = ucx_list_append(NULL, "Hello");
    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(list->data, "Hello", 5) == 0, "failed")
    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    
    ucx_list_free(list);
    
    UCX_TEST_END
}

UCX_TEST_BEGIN(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_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")
            
    free(copy->next->data);
    free(copy->data);

    free(world);
    free(hello);
    ucx_list_free(list);
    ucx_list_free(copy);
    
    UCX_TEST_END
}

mercurial