test/list_tests.c

Thu, 16 Aug 2012 11:31:16 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 16 Aug 2012 11:31:16 +0200
changeset 36
a9d656e4f7ce
parent 35
fdabd1240b69
child 40
583718dd4cf3
permissions
-rw-r--r--

changed API of sort algorithms (no further hint for the algorithms used in preparation for the upcomming change from qsort to natural merge sort)

     1 /*
     2  * tests of list implementation
     3  */
     5 #include "list_tests.h"
     7 UCX_TEST_IMPLEMENT(test_ucx_list_append) {
     8     UcxList *list = ucx_list_append(NULL, "Hello");
     9     UCX_TEST_BEGIN
    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_TEST_END
    18     ucx_list_free(list);
    19 }
    21 UCX_TEST_IMPLEMENT(test_ucx_list_prepend) {
    22     UcxList *list = ucx_list_prepend(NULL, " World!");
    23     UCX_TEST_BEGIN
    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_TEST_END
    31     ucx_list_free(list);
    32 }
    34 UCX_TEST_IMPLEMENT(test_ucx_list_equals) {
    35     UcxList *list = ucx_list_append(NULL, "Hello");
    36     list = ucx_list_append(list, " World!");
    37     UcxList *list2 = ucx_list_prepend(NULL, " World!");
    38     list2 = ucx_list_prepend(list2, "Hello");
    39     UcxList *list3 = ucx_list_prepend(NULL, " Welt!");
    40     list3 = ucx_list_prepend(list3, "Hallo");
    42     UCX_TEST_BEGIN
    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")
    45     UCX_TEST_END
    47     ucx_list_free(list3);
    48     ucx_list_free(list2);
    49     ucx_list_free(list);
    50 }
    52 UCX_TEST_IMPLEMENT(test_ucx_list_concat) {
    53     UcxList *list = ucx_list_append(NULL, "Hello");
    54     UcxList *list2 = ucx_list_prepend(NULL, " World!");
    56     list = ucx_list_concat(list, list2);
    57     UCX_TEST_BEGIN
    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_TEST_END
    64     if (list->next == NULL) {
    65         ucx_list_free(list2);
    66     }
    67     ucx_list_free(list);
    68 }
    70 UCX_TEST_IMPLEMENT(test_ucx_list_size) {
    71     UcxList *list = ucx_list_append(NULL, "This ");
    72     UCX_TEST_BEGIN
    73     list = ucx_list_append(list, "list ");
    74     list = ucx_list_append(list, "has ");
    75     list = ucx_list_append(list, "size ");
    76     list = ucx_list_append(list, "5!");
    78     UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
    80     UCX_TEST_END
    81     ucx_list_free(list);
    82 }
    84 UCX_TEST_IMPLEMENT(test_ucx_list_last) {
    85     UcxList *list = ucx_list_append(NULL, "Find ");
    86     UCX_TEST_BEGIN
    87     list = ucx_list_append(list, "the ");
    88     list = ucx_list_append(list, "last!");
    90     char* last = (char*) (ucx_list_last(list)->data);
    92     UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
    94     UCX_TEST_END
    95     ucx_list_free(list);
    97 }
    99 UCX_TEST_IMPLEMENT(test_ucx_list_get) {
   100     UcxList *list = ucx_list_append(NULL, "Find ");
   101     UCX_TEST_BEGIN
   102     list = ucx_list_append(list, "the ");
   103     list = ucx_list_append(list, "mid!");
   105     char* mid = (char*) (ucx_list_get(list, 1)->data);
   107     UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
   109     UCX_TEST_END
   110     ucx_list_free(list);
   111 }
   113 UCX_TEST_IMPLEMENT(test_ucx_list_remove) {
   114     UcxList *list = ucx_list_append(NULL, "Hello");
   115     UCX_TEST_BEGIN
   116     list = ucx_list_append(list, " fucking");
   117     list = ucx_list_append(list, " World!");
   119     list = ucx_list_remove(list, ucx_list_get(list, 1));
   121     UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
   122     UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
   123     UCX_TEST_ASSERT(list->next->next == NULL, "failed")
   124     UCX_TEST_END
   126     ucx_list_free(list);
   127 }
   129 UCX_TEST_IMPLEMENT(test_ucx_list_clone) {
   131     char *hello = (char*)malloc(6);
   132     char *world = (char*)malloc(8);
   134     memcpy(hello, "Hello", 6);
   135     memcpy(world, " World!", 8);
   137     UcxList *list = ucx_list_append(NULL, hello);
   138     list = ucx_list_append(list, world);
   140     UcxList *copy = ucx_list_clone(list, copy_string, NULL);
   141     UCX_TEST_BEGIN
   143     UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed")
   144     UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
   145     UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
   147     UCX_TEST_END
   148     free(copy->next->data);
   149     free(copy->data);
   151     free(world);
   152     free(hello);
   153     ucx_list_free(list);
   154     ucx_list_free(copy);
   155 }
   157 UCX_TEST_IMPLEMENT(test_ucx_list_sort) {
   158     UcxList *list = ucx_list_append(NULL, "this");
   159     list = ucx_list_append(list, "is");
   160     list = ucx_list_append(list, "a");
   161     list = ucx_list_append(list, "test");
   162     list = ucx_list_append(list, "for");
   163     list = ucx_list_append(list, "partial");
   164     list = ucx_list_append(list, "correctness");
   166     UcxList *expected = ucx_list_append(NULL, "a");
   167     expected = ucx_list_append(expected, "correctness");
   168     expected = ucx_list_append(expected, "for");
   169     expected = ucx_list_append(expected, "is");
   170     expected = ucx_list_append(expected, "partial");
   171     expected = ucx_list_append(expected, "test");
   172     expected = ucx_list_append(expected, "this");
   174     list = ucx_list_sort(list, cmp_string, NULL);
   176     UCX_TEST_BEGIN
   177     UCX_TEST_ASSERT(
   178             ucx_list_equals(list, expected, cmp_string, NULL), "failed");
   179     UCX_TEST_END
   181     ucx_list_free(expected);
   182     ucx_list_free(list);
   183 }

mercurial