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)

olaf@9 1 /*
olaf@9 2 * tests of list implementation
olaf@9 3 */
olaf@9 4
universe@27 5 #include "list_tests.h"
olaf@9 6
universe@33 7 UCX_TEST_IMPLEMENT(test_ucx_list_append) {
universe@27 8 UcxList *list = ucx_list_append(NULL, "Hello");
universe@33 9 UCX_TEST_BEGIN
universe@27 10 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
universe@27 11
universe@27 12 list = ucx_list_append(list, " World!");
universe@27 13
universe@27 14 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
universe@27 15 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
universe@33 16
universe@33 17 UCX_TEST_END
universe@27 18 ucx_list_free(list);
universe@24 19 }
universe@24 20
universe@33 21 UCX_TEST_IMPLEMENT(test_ucx_list_prepend) {
universe@27 22 UcxList *list = ucx_list_prepend(NULL, " World!");
universe@33 23 UCX_TEST_BEGIN
universe@27 24 list = ucx_list_prepend(list, "Hello");
universe@27 25
universe@27 26 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
universe@27 27 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
universe@27 28 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
universe@27 29
universe@33 30 UCX_TEST_END
universe@27 31 ucx_list_free(list);
universe@18 32 }
universe@18 33
universe@33 34 UCX_TEST_IMPLEMENT(test_ucx_list_equals) {
universe@27 35 UcxList *list = ucx_list_append(NULL, "Hello");
universe@27 36 list = ucx_list_append(list, " World!");
universe@27 37 UcxList *list2 = ucx_list_prepend(NULL, " World!");
universe@27 38 list2 = ucx_list_prepend(list2, "Hello");
universe@27 39 UcxList *list3 = ucx_list_prepend(NULL, " Welt!");
universe@27 40 list3 = ucx_list_prepend(list3, "Hallo");
universe@27 41
universe@33 42 UCX_TEST_BEGIN
universe@27 43 UCX_TEST_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed")
universe@27 44 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, NULL), "failed")
universe@33 45 UCX_TEST_END
universe@27 46
universe@27 47 ucx_list_free(list3);
universe@27 48 ucx_list_free(list2);
universe@27 49 ucx_list_free(list);
universe@24 50 }
universe@24 51
universe@33 52 UCX_TEST_IMPLEMENT(test_ucx_list_concat) {
universe@27 53 UcxList *list = ucx_list_append(NULL, "Hello");
universe@27 54 UcxList *list2 = ucx_list_prepend(NULL, " World!");
universe@27 55
universe@27 56 list = ucx_list_concat(list, list2);
universe@33 57 UCX_TEST_BEGIN
universe@27 58
universe@27 59 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
universe@27 60 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
universe@27 61 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
universe@27 62
universe@33 63 UCX_TEST_END
universe@33 64 if (list->next == NULL) {
universe@33 65 ucx_list_free(list2);
universe@33 66 }
universe@27 67 ucx_list_free(list);
olaf@9 68 }
olaf@9 69
universe@33 70 UCX_TEST_IMPLEMENT(test_ucx_list_size) {
universe@27 71 UcxList *list = ucx_list_append(NULL, "This ");
universe@33 72 UCX_TEST_BEGIN
universe@27 73 list = ucx_list_append(list, "list ");
universe@27 74 list = ucx_list_append(list, "has ");
universe@27 75 list = ucx_list_append(list, "size ");
universe@27 76 list = ucx_list_append(list, "5!");
universe@27 77
universe@27 78 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
universe@27 79
universe@33 80 UCX_TEST_END
universe@27 81 ucx_list_free(list);
olaf@9 82 }
olaf@11 83
universe@33 84 UCX_TEST_IMPLEMENT(test_ucx_list_last) {
universe@27 85 UcxList *list = ucx_list_append(NULL, "Find ");
universe@33 86 UCX_TEST_BEGIN
universe@27 87 list = ucx_list_append(list, "the ");
universe@27 88 list = ucx_list_append(list, "last!");
universe@27 89
universe@27 90 char* last = (char*) (ucx_list_last(list)->data);
universe@27 91
universe@27 92 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
universe@27 93
universe@33 94 UCX_TEST_END
universe@27 95 ucx_list_free(list);
universe@27 96
universe@27 97 }
universe@27 98
universe@33 99 UCX_TEST_IMPLEMENT(test_ucx_list_get) {
universe@27 100 UcxList *list = ucx_list_append(NULL, "Find ");
universe@33 101 UCX_TEST_BEGIN
universe@27 102 list = ucx_list_append(list, "the ");
universe@27 103 list = ucx_list_append(list, "mid!");
universe@27 104
universe@27 105 char* mid = (char*) (ucx_list_get(list, 1)->data);
universe@27 106
universe@27 107 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
universe@27 108
universe@33 109 UCX_TEST_END
universe@27 110 ucx_list_free(list);
universe@27 111 }
universe@27 112
universe@33 113 UCX_TEST_IMPLEMENT(test_ucx_list_remove) {
universe@27 114 UcxList *list = ucx_list_append(NULL, "Hello");
universe@33 115 UCX_TEST_BEGIN
universe@27 116 list = ucx_list_append(list, " fucking");
universe@27 117 list = ucx_list_append(list, " World!");
universe@27 118
universe@27 119 list = ucx_list_remove(list, ucx_list_get(list, 1));
universe@27 120
universe@27 121 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
universe@27 122 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
universe@27 123 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
universe@33 124 UCX_TEST_END
universe@27 125
universe@27 126 ucx_list_free(list);
universe@27 127 }
universe@27 128
universe@33 129 UCX_TEST_IMPLEMENT(test_ucx_list_clone) {
universe@27 130
universe@27 131 char *hello = (char*)malloc(6);
universe@27 132 char *world = (char*)malloc(8);
universe@27 133
universe@27 134 memcpy(hello, "Hello", 6);
universe@27 135 memcpy(world, " World!", 8);
universe@27 136
universe@27 137 UcxList *list = ucx_list_append(NULL, hello);
universe@27 138 list = ucx_list_append(list, world);
universe@27 139
universe@27 140 UcxList *copy = ucx_list_clone(list, copy_string, NULL);
universe@33 141 UCX_TEST_BEGIN
universe@27 142
universe@27 143 UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed")
universe@27 144 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
universe@27 145 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
universe@33 146
universe@33 147 UCX_TEST_END
universe@27 148 free(copy->next->data);
universe@27 149 free(copy->data);
universe@27 150
universe@27 151 free(world);
universe@27 152 free(hello);
olaf@30 153 ucx_list_free(list);
olaf@30 154 ucx_list_free(copy);
universe@27 155 }
universe@35 156
universe@36 157 UCX_TEST_IMPLEMENT(test_ucx_list_sort) {
universe@35 158 UcxList *list = ucx_list_append(NULL, "this");
universe@35 159 list = ucx_list_append(list, "is");
universe@35 160 list = ucx_list_append(list, "a");
universe@35 161 list = ucx_list_append(list, "test");
universe@35 162 list = ucx_list_append(list, "for");
universe@35 163 list = ucx_list_append(list, "partial");
universe@35 164 list = ucx_list_append(list, "correctness");
universe@35 165
universe@35 166 UcxList *expected = ucx_list_append(NULL, "a");
universe@35 167 expected = ucx_list_append(expected, "correctness");
universe@35 168 expected = ucx_list_append(expected, "for");
universe@35 169 expected = ucx_list_append(expected, "is");
universe@35 170 expected = ucx_list_append(expected, "partial");
universe@35 171 expected = ucx_list_append(expected, "test");
universe@35 172 expected = ucx_list_append(expected, "this");
universe@35 173
universe@36 174 list = ucx_list_sort(list, cmp_string, NULL);
universe@35 175
universe@35 176 UCX_TEST_BEGIN
universe@35 177 UCX_TEST_ASSERT(
universe@35 178 ucx_list_equals(list, expected, cmp_string, NULL), "failed");
universe@35 179 UCX_TEST_END
universe@35 180
universe@35 181 ucx_list_free(expected);
universe@35 182 ucx_list_free(list);
universe@35 183 }

mercurial