test/list_tests.c

Mon, 22 Jul 2013 13:45:49 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 22 Jul 2013 13:45:49 +0200
changeset 123
7fb0f74517c5
parent 122
540d99722f1f
child 134
4d320dc3a7af
permissions
-rw-r--r--

changed signature of sstrncat + some documentation for UcxList + new features for UcxList

olaf@9 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@103 4 * Copyright 2013 Olaf Wintermann. All rights reserved.
universe@103 5 *
universe@103 6 * Redistribution and use in source and binary forms, with or without
universe@103 7 * modification, are permitted provided that the following conditions are met:
universe@103 8 *
universe@103 9 * 1. Redistributions of source code must retain the above copyright
universe@103 10 * notice, this list of conditions and the following disclaimer.
universe@103 11 *
universe@103 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@103 13 * notice, this list of conditions and the following disclaimer in the
universe@103 14 * documentation and/or other materials provided with the distribution.
universe@103 15 *
universe@103 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@103 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@103 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@103 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@103 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@103 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@103 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@103 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@103 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@103 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@103 26 * POSSIBILITY OF SUCH DAMAGE.
olaf@9 27 */
olaf@9 28
universe@122 29 #include "list_tests.h"
universe@94 30 #include "ucx/utils.h"
olaf@9 31
universe@122 32 UCX_TEST_IMPLEMENT(test_ucx_list_append) {
universe@122 33 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
universe@33 34 UCX_TEST_BEGIN
universe@27 35
universe@69 36 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
universe@69 37 "failed");
universe@27 38
universe@122 39 list = ucx_list_append(list, (void*)" World!");
universe@27 40
universe@69 41 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
universe@69 42 "failed");
universe@40 43 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
universe@33 44 UCX_TEST_END
universe@27 45
universe@122 46 ucx_list_free(list);
universe@24 47 }
universe@24 48
universe@122 49 UCX_TEST_IMPLEMENT(test_ucx_list_prepend) {
universe@122 50 UcxList *list = ucx_list_prepend(NULL, (void*)" World!");
universe@33 51 UCX_TEST_BEGIN
universe@33 52
universe@122 53 list = ucx_list_prepend(list, (void*)"Hello");
universe@27 54
universe@69 55 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
universe@69 56 "failed");
universe@69 57 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
universe@69 58 "failed");
universe@40 59 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
universe@27 60
universe@33 61 UCX_TEST_END
universe@122 62 ucx_list_free(list);
universe@18 63 }
universe@18 64
universe@122 65 UCX_TEST_IMPLEMENT(test_ucx_list_equals) {
universe@122 66 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
universe@122 67 list = ucx_list_append(list, (void*)" World!");
universe@122 68 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
universe@122 69 list2 = ucx_list_prepend(list2, (void*)"Hello");
universe@122 70 UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!");
universe@122 71 list3 = ucx_list_prepend(list3, (void*)"Hallo");
universe@33 72 UCX_TEST_BEGIN
universe@27 73
universe@122 74 UCX_TEST_ASSERT(ucx_list_equals(list, list2, ucx_strcmp, NULL), "failed");
universe@122 75 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
universe@27 76
universe@33 77 UCX_TEST_END
universe@122 78 ucx_list_free(list3);
universe@122 79 ucx_list_free(list2);
universe@122 80 ucx_list_free(list);
universe@24 81 }
universe@24 82
universe@122 83 UCX_TEST_IMPLEMENT(test_ucx_list_concat) {
universe@122 84 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
universe@122 85 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
universe@33 86 UCX_TEST_BEGIN
universe@27 87
universe@122 88 list = ucx_list_concat(list, list2);
universe@27 89
universe@69 90 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
universe@69 91 "failed");
universe@69 92 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
universe@69 93 "failed");
universe@40 94 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
universe@27 95
universe@33 96 UCX_TEST_END
universe@122 97 ucx_list_free(list);
olaf@9 98 }
olaf@9 99
universe@122 100 UCX_TEST_IMPLEMENT(test_ucx_list_size) {
universe@122 101 UcxList *list = ucx_list_append(NULL, (void*)"This ");
universe@122 102 list = ucx_list_append(list, (void*)"list ");
universe@122 103 list = ucx_list_append(list, (void*)"has ");
universe@122 104 list = ucx_list_append(list, (void*)"size ");
universe@122 105 list = ucx_list_append(list, (void*)"5!");
universe@27 106
universe@123 107 UCX_TEST_BEGIN
universe@123 108
universe@122 109 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
universe@27 110
universe@33 111 UCX_TEST_END
universe@122 112 ucx_list_free(list);
olaf@9 113 }
olaf@11 114
universe@122 115 UCX_TEST_IMPLEMENT(test_ucx_list_first) {
universe@122 116 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
universe@122 117 list = ucx_list_append(list, (void*)"the ");
universe@122 118 list = ucx_list_append(list, (void*)"first!");
universe@27 119
universe@123 120 UCX_TEST_BEGIN
universe@123 121
universe@122 122 const char* first = (const char*) (ucx_list_first(list)->data);
universe@27 123
universe@27 124 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
universe@27 125
universe@33 126 UCX_TEST_END
universe@122 127 ucx_list_free(list);
universe@27 128 }
universe@27 129
universe@122 130 UCX_TEST_IMPLEMENT(test_ucx_list_last) {
universe@122 131 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
universe@122 132 list = ucx_list_append(list, (void*)"the ");
universe@122 133 list = ucx_list_append(list, (void*)"last!");
universe@27 134
universe@123 135 UCX_TEST_BEGIN
universe@123 136
universe@122 137 const char* last = (const char*) (ucx_list_last(list)->data);
universe@27 138
universe@27 139 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
universe@27 140
universe@33 141 UCX_TEST_END
universe@122 142 ucx_list_free(list);
universe@27 143 }
universe@27 144
universe@122 145 UCX_TEST_IMPLEMENT(test_ucx_list_get) {
universe@122 146 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
universe@122 147 list = ucx_list_append(list, (void*)"the ");
universe@122 148 list = ucx_list_append(list, (void*)"mid!");
universe@27 149
universe@123 150 UCX_TEST_BEGIN
universe@123 151
universe@122 152 const char* mid = (const char*) (ucx_list_get(list, 1)->data);
universe@27 153
universe@27 154 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
universe@27 155
universe@33 156 UCX_TEST_END
universe@122 157 ucx_list_free(list);
universe@27 158 }
universe@27 159
universe@123 160 UCX_TEST_IMPLEMENT(test_ucx_list_indexof) {
universe@123 161 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
universe@123 162 list = ucx_list_append(list, (void*)"the ");
universe@123 163 list = ucx_list_append(list, (void*)"mid!");
universe@123 164
universe@123 165 UCX_TEST_BEGIN
universe@123 166
universe@123 167 UCX_TEST_ASSERT(ucx_list_indexof(list, list) == 0, "failed");
universe@123 168 UCX_TEST_ASSERT(ucx_list_indexof(list, list->next) == 1, "failed");
universe@123 169 UCX_TEST_ASSERT(ucx_list_indexof(list, ucx_list_get(list, 2)) == 2,
universe@123 170 "failed");
universe@123 171
universe@123 172 UcxList *otherlist = ucx_list_append(NULL, (void*) "foobar");
universe@123 173 UCX_TEST_ASSERT(ucx_list_indexof(list, otherlist) == -1, "failed");
universe@123 174 ucx_list_free(otherlist);
universe@123 175
universe@123 176 UCX_TEST_END
universe@123 177 ucx_list_free(list);
universe@123 178 }
universe@123 179
universe@123 180 UCX_TEST_IMPLEMENT(test_ucx_list_find) {
universe@123 181 UcxList *l = ucx_list_append(NULL, (void*)"find ");
universe@123 182 l = ucx_list_append(l, (void*)"some ");
universe@123 183 l = ucx_list_append(l, (void*)"string!");
universe@123 184
universe@123 185 UCX_TEST_BEGIN
universe@123 186
universe@123 187 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"some ",ucx_strcmp,NULL) == 1,
universe@123 188 "doesn't find string");
universe@123 189 UCX_TEST_ASSERT(ucx_list_find(l,(void*)"a",ucx_strcmp,NULL) == -1,
universe@123 190 "finds non-existing string");
universe@123 191
universe@123 192 UCX_TEST_END
universe@123 193 ucx_list_free(l);
universe@123 194 }
universe@123 195
universe@122 196 UCX_TEST_IMPLEMENT(test_ucx_list_contains) {
universe@122 197 UcxList *l = ucx_list_append(NULL, (void*)"Contains ");
universe@122 198 l = ucx_list_append(l, (void*)"a ");
universe@122 199 l = ucx_list_append(l, (void*)"string!");
universe@90 200
universe@123 201 UCX_TEST_BEGIN
universe@123 202
universe@123 203 UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL),
universe@123 204 "false negative");
universe@123 205 UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL),
universe@123 206 "false positive");
universe@90 207
universe@90 208 UCX_TEST_END
universe@122 209 ucx_list_free(l);
universe@90 210 }
universe@90 211
universe@122 212 UCX_TEST_IMPLEMENT(test_ucx_list_remove) {
universe@122 213 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
universe@122 214 list = ucx_list_append(list, (void*)" fucking");
universe@122 215 list = ucx_list_append(list, (void*)" World!");
universe@27 216
universe@123 217 UCX_TEST_BEGIN
universe@123 218
universe@122 219 list = ucx_list_remove(list, ucx_list_get(list, 1));
universe@27 220
universe@69 221 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
universe@69 222 "failed");
universe@69 223 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
universe@69 224 "failed");
universe@40 225 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
universe@27 226
universe@33 227 UCX_TEST_END
universe@122 228 ucx_list_free(list);
universe@27 229 }
universe@27 230
universe@122 231 UCX_TEST_IMPLEMENT(test_ucx_list_clone) {
universe@27 232
universe@27 233 char *hello = (char*)malloc(6);
universe@27 234 char *world = (char*)malloc(8);
universe@27 235
universe@27 236 memcpy(hello, "Hello", 6);
universe@27 237 memcpy(world, " World!", 8);
universe@27 238
universe@122 239 UcxList *list = ucx_list_append(NULL, hello);
universe@122 240 list = ucx_list_append(list, world);
universe@27 241
universe@122 242 UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL);
universe@33 243 UCX_TEST_BEGIN
universe@27 244
universe@122 245 UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed");
universe@40 246 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
universe@40 247 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
universe@27 248
universe@33 249 UCX_TEST_END
universe@27 250 free(copy->next->data);
universe@27 251 free(copy->data);
universe@27 252
universe@27 253 free(world);
universe@27 254 free(hello);
universe@122 255 ucx_list_free(list);
universe@122 256 ucx_list_free(copy);
universe@27 257 }
universe@35 258
universe@122 259 UCX_TEST_IMPLEMENT(test_ucx_list_sort) {
universe@122 260 UcxList *list = ucx_list_append(NULL, (void*)"this");
universe@122 261 list = ucx_list_append(list, (void*)"is");
universe@122 262 list = ucx_list_append(list, (void*)"a");
universe@122 263 list = ucx_list_append(list, (void*)"test");
universe@122 264 list = ucx_list_append(list, (void*)"for");
universe@122 265 list = ucx_list_append(list, (void*)"partial");
universe@122 266 list = ucx_list_append(list, (void*)"correctness");
universe@35 267
universe@122 268 UcxList *expected = ucx_list_append(NULL, (void*)"a");
universe@122 269 expected = ucx_list_append(expected, (void*)"correctness");
universe@122 270 expected = ucx_list_append(expected, (void*)"for");
universe@122 271 expected = ucx_list_append(expected, (void*)"is");
universe@122 272 expected = ucx_list_append(expected, (void*)"partial");
universe@122 273 expected = ucx_list_append(expected, (void*)"test");
universe@122 274 expected = ucx_list_append(expected, (void*)"this");
universe@35 275
universe@122 276 list = ucx_list_sort(list, ucx_strcmp, NULL);
universe@35 277
universe@35 278 UCX_TEST_BEGIN
universe@35 279 UCX_TEST_ASSERT(
universe@122 280 ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
universe@122 281 UcxList *l = list;
universe@35 282 UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
universe@35 283 while (l->next != NULL) {
universe@35 284 UCX_TEST_ASSERT(l->next->prev == l, "prev pointer corrupted");
universe@35 285 l = l->next;
universe@35 286 }
universe@35 287 UCX_TEST_END
universe@35 288
universe@122 289 ucx_list_free(expected);
universe@122 290 ucx_list_free(list);
universe@35 291 }

mercurial