test/list_tests.c

Fri, 12 Oct 2012 12:08:34 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Oct 2012 12:08:34 +0200
changeset 71
303dabadff1c
parent 69
fb59270b1de3
child 89
47f7fdbddb62
permissions
-rw-r--r--

made the code work with g++ without errors (but warnings)

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

mercurial