test/list_tests.c

changeset 172
7084e8e8433c
parent 162
52dfe5f4ecd7
child 177
11ad03783baf
     1.1 --- a/test/list_tests.c	Tue May 06 14:35:29 2014 +0200
     1.2 +++ b/test/list_tests.c	Mon Jun 02 16:04:11 2014 +0200
     1.3 @@ -30,7 +30,8 @@
     1.4  #include "ucx/utils.h"
     1.5  
     1.6  UCX_TEST(test_ucx_list_append) {
     1.7 -    UcxList *list = ucx_list_append(NULL, (void*)"Hello");
     1.8 +    UcxList *list, *first;
     1.9 +    list = first = ucx_list_append(NULL, (void*)"Hello");
    1.10      UCX_TEST_BEGIN
    1.11      
    1.12      UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    1.13 @@ -38,8 +39,10 @@
    1.14      
    1.15      list = ucx_list_append(list, (void*)" World!");
    1.16      
    1.17 +    UCX_TEST_ASSERT(list == first, "does not return first element");
    1.18      UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    1.19              "failed");
    1.20 +    UCX_TEST_ASSERT(list->next->prev == list, "failed");
    1.21      UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    1.22      UCX_TEST_END
    1.23      
    1.24 @@ -47,7 +50,8 @@
    1.25  }
    1.26  
    1.27  UCX_TEST(test_ucx_list_prepend) {
    1.28 -    UcxList *list = ucx_list_prepend(NULL, (void*)" World!");
    1.29 +    UcxList *list, *last;
    1.30 +    list = last = ucx_list_prepend(NULL, (void*)" World!");
    1.31      UCX_TEST_BEGIN
    1.32  
    1.33      list = ucx_list_prepend(list, (void*)"Hello");
    1.34 @@ -56,25 +60,33 @@
    1.35              "failed");
    1.36      UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    1.37              "failed");
    1.38 +    UCX_TEST_ASSERT(list == last->prev, "does not return first element");
    1.39      UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    1.40 +    UCX_TEST_ASSERT(list->prev == NULL, "failed");
    1.41      
    1.42      UCX_TEST_END
    1.43      ucx_list_free(list);
    1.44  }
    1.45  
    1.46  UCX_TEST(test_ucx_list_equals) {
    1.47 -    UcxList *list = ucx_list_append(NULL, (void*)"Hello");
    1.48 -    list = ucx_list_append(list, (void*)" World!");
    1.49 -    UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
    1.50 -    list2 = ucx_list_prepend(list2, (void*)"Hello");
    1.51 +    const char *hello = "Hello";
    1.52 +    const char *world = " World!";
    1.53 +    UcxList *list = ucx_list_append(NULL, (void*)hello);
    1.54 +    list = ucx_list_append(list, (void*)world);
    1.55 +    UcxList *list2 = ucx_list_prepend(NULL, (void*)world);
    1.56 +    list2 = ucx_list_prepend(list2, (void*)hello);
    1.57      UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!");
    1.58      list3 = ucx_list_prepend(list3, (void*)"Hallo");
    1.59 +    UcxList *list4 = ucx_list_prepend(NULL, (void*)" World!");
    1.60 +    list4 = ucx_list_prepend(list4, (void*)"Hello");
    1.61      UCX_TEST_BEGIN
    1.62      
    1.63 -    UCX_TEST_ASSERT(ucx_list_equals(list, list2, ucx_strcmp, NULL), "failed");
    1.64 +    UCX_TEST_ASSERT(ucx_list_equals(list, list4, ucx_strcmp, NULL), "failed");
    1.65      UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
    1.66 +    UCX_TEST_ASSERT(ucx_list_equals(list, list2, NULL, NULL), "failed");
    1.67      
    1.68      UCX_TEST_END
    1.69 +    ucx_list_free(list4);
    1.70      ucx_list_free(list3);
    1.71      ucx_list_free(list2);
    1.72      ucx_list_free(list);
    1.73 @@ -82,18 +94,28 @@
    1.74  
    1.75  UCX_TEST(test_ucx_list_concat) {
    1.76      UcxList *list = ucx_list_append(NULL, (void*)"Hello");
    1.77 +    list = ucx_list_append(list, (void*)" my ");
    1.78      UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
    1.79 +    list2 = ucx_list_prepend(list2, (void*)" sweet ");
    1.80      UCX_TEST_BEGIN
    1.81      
    1.82      list = ucx_list_concat(list, list2);
    1.83 +    list = ucx_list_concat(list, NULL);
    1.84 +    list = ucx_list_concat(NULL, list);
    1.85      
    1.86 -    UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
    1.87 +    UCX_TEST_ASSERT(!strncmp((const char*)list->data, "Hello", 5),
    1.88              "failed");
    1.89 -    UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
    1.90 +    UCX_TEST_ASSERT(!strncmp((const char*)list->next->data, " my ", 4),
    1.91              "failed");
    1.92 -    UCX_TEST_ASSERT(list->next->next == NULL, "failed");
    1.93 +    UCX_TEST_ASSERT(!strncmp((const char*)list->next->next->data, " sweet ", 7),
    1.94 +            "failed");
    1.95 +    UCX_TEST_ASSERT(!strncmp((const char*)ucx_list_last(list)->data,
    1.96 +            " World!", 7), "failed");
    1.97 +
    1.98 +    UCX_TEST_ASSERT(list->prev == NULL, "failed");
    1.99      
   1.100      UCX_TEST_END
   1.101 +    // don't free list2, as it is freed by freeing list;
   1.102      ucx_list_free(list);
   1.103  }
   1.104  
   1.105 @@ -107,6 +129,8 @@
   1.106      UCX_TEST_BEGIN
   1.107      
   1.108      UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
   1.109 +    list = ucx_list_remove(list, ucx_list_get(list, 2));
   1.110 +    UCX_TEST_ASSERT(ucx_list_size(list) == 4, "failed after removal");
   1.111      
   1.112      UCX_TEST_END
   1.113      ucx_list_free(list);
   1.114 @@ -122,6 +146,9 @@
   1.115      const char* first = (const char*) (ucx_list_first(list)->data);
   1.116      
   1.117      UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
   1.118 +    UCX_TEST_ASSERT(ucx_list_first(list->next->next) == list, "failed");
   1.119 +    UCX_TEST_ASSERT(!ucx_list_first(NULL),
   1.120 +        "does not return NULL on an empty list");
   1.121      
   1.122      UCX_TEST_END
   1.123      ucx_list_free(list);
   1.124 @@ -134,9 +161,12 @@
   1.125      
   1.126      UCX_TEST_BEGIN
   1.127      
   1.128 -    const char* last = (const char*) (ucx_list_last(list)->data);
   1.129 +    const char* last = (const char*) (ucx_list_last(list->next->next)->data);
   1.130      
   1.131      UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
   1.132 +    UCX_TEST_ASSERT(ucx_list_last(list) == list->next->next, "failed");
   1.133 +    UCX_TEST_ASSERT(!ucx_list_last(NULL),
   1.134 +        "does not return NULL on an empty list");
   1.135      
   1.136      UCX_TEST_END
   1.137      ucx_list_free(list);
   1.138 @@ -149,9 +179,16 @@
   1.139      
   1.140      UCX_TEST_BEGIN
   1.141      
   1.142 +    const char* first = (const char*) (ucx_list_get(list, 0)->data);
   1.143      const char* mid = (const char*) (ucx_list_get(list, 1)->data);
   1.144 +    const char* last = (const char*) (ucx_list_get(list, 2)->data);
   1.145      
   1.146 +    UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
   1.147      UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
   1.148 +    UCX_TEST_ASSERT(strncmp(last, "mid!", 4) == 0, "failed");
   1.149 +    UCX_TEST_ASSERT(!ucx_list_get(list, -1), "out of bounds (neg)");
   1.150 +    UCX_TEST_ASSERT(!ucx_list_get(list, 3), "out of bounds");
   1.151 +    UCX_TEST_ASSERT(!ucx_list_get(NULL, 0), "empty list");
   1.152      
   1.153      UCX_TEST_END
   1.154      ucx_list_free(list);
   1.155 @@ -169,8 +206,10 @@
   1.156      UCX_TEST_ASSERT(ucx_list_indexof(list, ucx_list_get(list, 2)) == 2,
   1.157          "failed");
   1.158      
   1.159 -    UcxList *otherlist = ucx_list_append(NULL, (void*) "foobar");
   1.160 +    UcxList *otherlist = ucx_list_append(NULL, (void*) "the ");
   1.161      UCX_TEST_ASSERT(ucx_list_indexof(list, otherlist) == -1, "failed");
   1.162 +    UCX_TEST_ASSERT(ucx_list_indexof(NULL, otherlist) == -1, "empty list");
   1.163 +
   1.164      ucx_list_free(otherlist);
   1.165      
   1.166      UCX_TEST_END
   1.167 @@ -178,9 +217,10 @@
   1.168  }
   1.169  
   1.170  UCX_TEST(test_ucx_list_find) {
   1.171 +    const char* teststr = "string!";
   1.172      UcxList *l = ucx_list_append(NULL, (void*)"find ");
   1.173      l = ucx_list_append(l, (void*)"some ");
   1.174 -    l = ucx_list_append(l, (void*)"string!");
   1.175 +    l = ucx_list_append(l, (void*)teststr);
   1.176      
   1.177      UCX_TEST_BEGIN
   1.178      
   1.179 @@ -189,6 +229,12 @@
   1.180      UCX_TEST_ASSERT(ucx_list_find(l,(void*)"a",ucx_strcmp,NULL) == -1,
   1.181          "finds non-existing string");
   1.182      
   1.183 +    UCX_TEST_ASSERT(ucx_list_find(l,(void*)teststr,NULL,NULL) == 2,
   1.184 +        "doesn't find integer without cmp_func");
   1.185 +    
   1.186 +    UCX_TEST_ASSERT(ucx_list_find(NULL, (void*)"some ",ucx_strcmp,NULL) == -1,
   1.187 +        "empty list");
   1.188 +    
   1.189      UCX_TEST_END
   1.190      ucx_list_free(l);
   1.191  }
   1.192 @@ -311,13 +357,31 @@
   1.193      list = ucx_list_append(list, (void*)"for");
   1.194      list = ucx_list_append(list, (void*)"partial");
   1.195      list = ucx_list_append(list, (void*)"correctness");
   1.196 +    list = ucx_list_append(list, (void*)"of");
   1.197 +    list = ucx_list_append(list, (void*)"the");
   1.198 +    list = ucx_list_append(list, (void*)"sort");
   1.199 +    list = ucx_list_append(list, (void*)"function");
   1.200 +    list = ucx_list_append(list, (void*)"that");
   1.201 +    list = ucx_list_append(list, (void*)"shall");
   1.202 +    list = ucx_list_append(list, (void*)"pass");
   1.203 +    list = ucx_list_append(list, (void*)"this");
   1.204 +    list = ucx_list_append(list, (void*)"test");
   1.205  
   1.206      UcxList *expected = ucx_list_append(NULL, (void*)"a");
   1.207      expected = ucx_list_append(expected, (void*)"correctness");
   1.208      expected = ucx_list_append(expected, (void*)"for");
   1.209 +    expected = ucx_list_append(expected, (void*)"function");
   1.210      expected = ucx_list_append(expected, (void*)"is");
   1.211 +    expected = ucx_list_append(expected, (void*)"of");
   1.212      expected = ucx_list_append(expected, (void*)"partial");
   1.213 +    expected = ucx_list_append(expected, (void*)"pass");
   1.214 +    expected = ucx_list_append(expected, (void*)"shall");
   1.215 +    expected = ucx_list_append(expected, (void*)"sort");
   1.216      expected = ucx_list_append(expected, (void*)"test");
   1.217 +    expected = ucx_list_append(expected, (void*)"test");
   1.218 +    expected = ucx_list_append(expected, (void*)"that");
   1.219 +    expected = ucx_list_append(expected, (void*)"the");
   1.220 +    expected = ucx_list_append(expected, (void*)"this");
   1.221      expected = ucx_list_append(expected, (void*)"this");
   1.222  
   1.223      list = ucx_list_sort(list, ucx_strcmp, NULL);
   1.224 @@ -325,12 +389,15 @@
   1.225      UCX_TEST_BEGIN
   1.226      UCX_TEST_ASSERT(
   1.227              ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
   1.228 +    UCX_TEST_ASSERT(ucx_list_size(list) == 16, "list has now a wrong size");
   1.229      UcxList *l = list;
   1.230      UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
   1.231      while (l->next != NULL) {
   1.232 -        UCX_TEST_ASSERT(l->next->prev == l, "prev pointer corrupted");
   1.233 +        UCX_TEST_ASSERT(l->next->prev == l, "next or prev pointer corrupted");
   1.234          l = l->next;
   1.235      }
   1.236 +    UCX_TEST_ASSERT(!ucx_list_sort(NULL, ucx_strcmp, NULL),
   1.237 +        "failed to sort empty list");
   1.238      UCX_TEST_END
   1.239  
   1.240      ucx_list_free(expected);

mercurial