test/list_tests.c

Fri, 01 Jun 2012 12:35:30 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 01 Jun 2012 12:35:30 +0200
changeset 34
0dcd2ca2a223
parent 33
9c219a62070d
child 35
fdabd1240b69
permissions
-rw-r--r--

completed map tests

/*
 * 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(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_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(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_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(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_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!");
    
    char* last = (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!");
    
    char* mid = (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(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_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);
}

mercurial