test/list_tests.c

Sat, 18 Feb 2012 18:36:30 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 18 Feb 2012 18:36:30 +0100
changeset 27
22644e2572bc
parent 24
e04822101291
child 30
23bb65cbf7a4
permissions
-rw-r--r--

removed old foreach + refactored list tests

     1 /*
     2  * tests of list implementation
     3  */
     5 #include "list_tests.h"
     7 UCX_TEST_BEGIN(test_ucx_list_append) {
     8     UcxList *list = ucx_list_append(NULL, "Hello");
    10     UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    12     list = ucx_list_append(list, " World!");
    14     UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    15     UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    17     ucx_list_free(list);
    19     UCX_TEST_END
    20 }
    22 UCX_TEST_BEGIN(test_ucx_list_prepend) {
    23     UcxList *list = ucx_list_prepend(NULL, " World!");
    24     list = ucx_list_prepend(list, "Hello");
    26     UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    27     UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    28     UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    30     ucx_list_free(list);
    32     UCX_TEST_END
    33 }
    35 UCX_TEST_BEGIN(test_ucx_list_equals) {
    36     UcxList *list = ucx_list_append(NULL, "Hello");
    37     list = ucx_list_append(list, " World!");
    38     UcxList *list2 = ucx_list_prepend(NULL, " World!");
    39     list2 = ucx_list_prepend(list2, "Hello");
    40     UcxList *list3 = ucx_list_prepend(NULL, " Welt!");
    41     list3 = ucx_list_prepend(list3, "Hallo");
    43     UCX_TEST_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed")
    44     UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, NULL), "failed")
    46     ucx_list_free(list3);
    47     ucx_list_free(list2);
    48     ucx_list_free(list);
    50     UCX_TEST_END
    51 }
    53 UCX_TEST_BEGIN(test_ucx_list_concat) {
    54     UcxList *list = ucx_list_append(NULL, "Hello");
    55     UcxList *list2 = ucx_list_prepend(NULL, " World!");
    57     list = ucx_list_concat(list, list2);
    59     UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
    60     UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
    61     UCX_TEST_ASSERT(list->next->next == NULL, "failed")
    63     ucx_list_free(list2);
    64     ucx_list_free(list);
    66     UCX_TEST_END
    67 }
    69 UCX_TEST_BEGIN(test_ucx_list_size) {
    70     UcxList *list = ucx_list_append(NULL, "This ");
    71     list = ucx_list_append(list, "list ");
    72     list = ucx_list_append(list, "has ");
    73     list = ucx_list_append(list, "size ");
    74     list = ucx_list_append(list, "5!");
    76     UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
    78     ucx_list_free(list);
    80     UCX_TEST_END
    81 }
    83 UCX_TEST_BEGIN(test_ucx_list_last) {
    84     UcxList *list = ucx_list_append(NULL, "Find ");
    85     list = ucx_list_append(list, "the ");
    86     list = ucx_list_append(list, "last!");
    88     char* last = (char*) (ucx_list_last(list)->data);
    90     UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
    92     ucx_list_free(list);
    94     UCX_TEST_END
    95 }
    97 UCX_TEST_BEGIN(test_ucx_list_get) {
    98     UcxList *list = ucx_list_append(NULL, "Find ");
    99     list = ucx_list_append(list, "the ");
   100     list = ucx_list_append(list, "mid!");
   102     char* mid = (char*) (ucx_list_get(list, 1)->data);
   104     UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
   106     ucx_list_free(list);
   108     UCX_TEST_END
   109 }
   111 UCX_TEST_BEGIN(test_ucx_list_remove) {
   112     UcxList *list = ucx_list_append(NULL, "Hello");
   113     list = ucx_list_append(list, " fucking");
   114     list = ucx_list_append(list, " World!");
   116     list = ucx_list_remove(list, ucx_list_get(list, 1));
   118     UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
   119     UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
   120     UCX_TEST_ASSERT(list->next->next == NULL, "failed")
   122     ucx_list_free(list);
   124     UCX_TEST_END
   125 }
   127 UCX_TEST_BEGIN(test_ucx_list_clone) {
   129     char *hello = (char*)malloc(6);
   130     char *world = (char*)malloc(8);
   132     memcpy(hello, "Hello", 6);
   133     memcpy(world, " World!", 8);
   135     UcxList *list = ucx_list_append(NULL, hello);
   136     list = ucx_list_append(list, world);
   138     UcxList *copy = ucx_list_clone(list, copy_string, NULL);
   140     UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed")
   141     UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
   142     UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
   144     free(copy->next->data);
   145     free(copy->data);
   147     free(world);
   148     free(hello);
   149     free(list);
   150     free(copy);
   152     UCX_TEST_END
   153 }

mercurial