test/dlist_tests.c

Thu, 28 Feb 2013 08:50:24 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 28 Feb 2013 08:50:24 +0100
changeset 103
08018864fb91
parent 94
57ea041df22f
permissions
-rw-r--r--

added license and copyright notice to all files

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@27 29 #include "dlist_tests.h"
universe@94 30 #include "ucx/utils.h"
olaf@9 31
universe@33 32 UCX_TEST_IMPLEMENT(test_ucx_dlist_append) {
universe@71 33 UcxDlist *list = ucx_dlist_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@71 39 list = ucx_dlist_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@27 46 ucx_dlist_free(list);
universe@24 47 }
universe@24 48
universe@33 49 UCX_TEST_IMPLEMENT(test_ucx_dlist_prepend) {
universe@71 50 UcxDlist *list = ucx_dlist_prepend(NULL, (void*)" World!");
universe@33 51 UCX_TEST_BEGIN
universe@33 52
universe@71 53 list = ucx_dlist_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@27 62 ucx_dlist_free(list);
universe@18 63 }
universe@18 64
universe@33 65 UCX_TEST_IMPLEMENT(test_ucx_dlist_equals) {
universe@71 66 UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello");
universe@71 67 list = ucx_dlist_append(list, (void*)" World!");
universe@71 68 UcxDlist *list2 = ucx_dlist_prepend(NULL, (void*)" World!");
universe@71 69 list2 = ucx_dlist_prepend(list2, (void*)"Hello");
universe@71 70 UcxDlist *list3 = ucx_dlist_prepend(NULL, (void*)" Welt!");
universe@71 71 list3 = ucx_dlist_prepend(list3, (void*)"Hallo");
universe@33 72 UCX_TEST_BEGIN
universe@27 73
universe@89 74 UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, ucx_strcmp, NULL), "failed");
universe@89 75 UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, ucx_strcmp, NULL), "failed");
universe@27 76
universe@33 77 UCX_TEST_END
universe@27 78 ucx_dlist_free(list3);
universe@27 79 ucx_dlist_free(list2);
universe@27 80 ucx_dlist_free(list);
universe@24 81 }
universe@24 82
universe@33 83 UCX_TEST_IMPLEMENT(test_ucx_dlist_concat) {
universe@71 84 UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello");
universe@71 85 UcxDlist *list2 = ucx_dlist_prepend(NULL, (void*)" World!");
universe@33 86 UCX_TEST_BEGIN
universe@27 87
universe@27 88 list = ucx_dlist_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@27 97 ucx_dlist_free(list);
olaf@9 98 }
olaf@9 99
universe@33 100 UCX_TEST_IMPLEMENT(test_ucx_dlist_size) {
universe@71 101 UcxDlist *list = ucx_dlist_append(NULL, (void*)"This ");
universe@33 102 UCX_TEST_BEGIN
universe@71 103 list = ucx_dlist_append(list, (void*)"list ");
universe@71 104 list = ucx_dlist_append(list, (void*)"has ");
universe@71 105 list = ucx_dlist_append(list, (void*)"size ");
universe@71 106 list = ucx_dlist_append(list, (void*)"5!");
universe@27 107
universe@27 108 UCX_TEST_ASSERT(ucx_dlist_size(list) == 5, "failed");
universe@27 109
universe@33 110 UCX_TEST_END
universe@27 111 ucx_dlist_free(list);
olaf@9 112 }
olaf@11 113
universe@33 114 UCX_TEST_IMPLEMENT(test_ucx_dlist_first) {
universe@71 115 UcxDlist *list = ucx_dlist_append(NULL, (void*)"Find ");
universe@33 116 UCX_TEST_BEGIN
universe@71 117 list = ucx_dlist_append(list, (void*)"the ");
universe@71 118 list = ucx_dlist_append(list, (void*)"first!");
universe@27 119
universe@69 120 const char* first = (const char*) (ucx_dlist_first(list)->data);
universe@27 121
universe@27 122 UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
universe@27 123
universe@33 124 UCX_TEST_END
universe@27 125 ucx_dlist_free(list);
universe@27 126 }
universe@27 127
universe@33 128 UCX_TEST_IMPLEMENT(test_ucx_dlist_last) {
universe@71 129 UcxDlist *list = ucx_dlist_append(NULL, (void*)"Find ");
universe@33 130 UCX_TEST_BEGIN
universe@71 131 list = ucx_dlist_append(list, (void*)"the ");
universe@71 132 list = ucx_dlist_append(list, (void*)"last!");
universe@27 133
universe@69 134 const char* last = (const char*) (ucx_dlist_last(list)->data);
universe@27 135
universe@27 136 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
universe@27 137
universe@33 138 UCX_TEST_END
universe@27 139 ucx_dlist_free(list);
universe@27 140 }
universe@27 141
universe@33 142 UCX_TEST_IMPLEMENT(test_ucx_dlist_get) {
universe@71 143 UcxDlist *list = ucx_dlist_append(NULL, (void*)"Find ");
universe@33 144 UCX_TEST_BEGIN
universe@71 145 list = ucx_dlist_append(list, (void*)"the ");
universe@71 146 list = ucx_dlist_append(list, (void*)"mid!");
universe@27 147
universe@69 148 const char* mid = (const char*) (ucx_dlist_get(list, 1)->data);
universe@27 149
universe@27 150 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
universe@27 151
universe@33 152 UCX_TEST_END
universe@27 153 ucx_dlist_free(list);
universe@27 154 }
universe@27 155
universe@90 156 UCX_TEST_IMPLEMENT(test_ucx_dlist_contains) {
universe@90 157 UcxDlist *l = ucx_dlist_append(NULL, (void*)"Contains ");
universe@90 158 UCX_TEST_BEGIN
universe@90 159 l = ucx_dlist_append(l, (void*)"a ");
universe@90 160 l = ucx_dlist_append(l, (void*)"string!");
universe@90 161
universe@90 162 UCX_TEST_ASSERT(ucx_dlist_contains(l,(void*)"a ",ucx_strcmp,NULL),"failed");
universe@90 163 UCX_TEST_ASSERT(!ucx_dlist_contains(l,(void*)"a",ucx_strcmp,NULL),"failed");
universe@90 164
universe@90 165 UCX_TEST_END
universe@90 166 ucx_dlist_free(l);
universe@90 167 }
universe@90 168
universe@33 169 UCX_TEST_IMPLEMENT(test_ucx_dlist_remove) {
universe@71 170 UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello");
universe@33 171 UCX_TEST_BEGIN
universe@71 172 list = ucx_dlist_append(list, (void*)" fucking");
universe@71 173 list = ucx_dlist_append(list, (void*)" World!");
universe@27 174
universe@27 175 list = ucx_dlist_remove(list, ucx_dlist_get(list, 1));
universe@27 176
universe@69 177 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
universe@69 178 "failed");
universe@69 179 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
universe@69 180 "failed");
universe@40 181 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
universe@27 182
universe@33 183 UCX_TEST_END
universe@27 184 ucx_dlist_free(list);
universe@27 185 }
universe@27 186
universe@33 187 UCX_TEST_IMPLEMENT(test_ucx_dlist_clone) {
universe@27 188
universe@27 189 char *hello = (char*)malloc(6);
universe@27 190 char *world = (char*)malloc(8);
universe@27 191
universe@27 192 memcpy(hello, "Hello", 6);
universe@27 193 memcpy(world, " World!", 8);
universe@27 194
universe@27 195 UcxDlist *list = ucx_dlist_append(NULL, hello);
universe@27 196 list = ucx_dlist_append(list, world);
universe@27 197
universe@94 198 UcxDlist *copy = ucx_dlist_clone(list, ucx_strcpy, NULL);
universe@33 199 UCX_TEST_BEGIN
universe@27 200
universe@89 201 UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, ucx_strcmp, NULL), "failed");
universe@40 202 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
universe@40 203 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
universe@27 204
universe@33 205 UCX_TEST_END
universe@27 206 free(copy->next->data);
universe@27 207 free(copy->data);
universe@27 208
universe@27 209 free(world);
universe@27 210 free(hello);
olaf@30 211 ucx_dlist_free(list);
olaf@30 212 ucx_dlist_free(copy);
universe@27 213 }
universe@35 214
universe@36 215 UCX_TEST_IMPLEMENT(test_ucx_dlist_sort) {
universe@71 216 UcxDlist *list = ucx_dlist_append(NULL, (void*)"this");
universe@71 217 list = ucx_dlist_append(list, (void*)"is");
universe@71 218 list = ucx_dlist_append(list, (void*)"a");
universe@71 219 list = ucx_dlist_append(list, (void*)"test");
universe@71 220 list = ucx_dlist_append(list, (void*)"for");
universe@71 221 list = ucx_dlist_append(list, (void*)"partial");
universe@71 222 list = ucx_dlist_append(list, (void*)"correctness");
universe@35 223
universe@71 224 UcxDlist *expected = ucx_dlist_append(NULL, (void*)"a");
universe@71 225 expected = ucx_dlist_append(expected, (void*)"correctness");
universe@71 226 expected = ucx_dlist_append(expected, (void*)"for");
universe@71 227 expected = ucx_dlist_append(expected, (void*)"is");
universe@71 228 expected = ucx_dlist_append(expected, (void*)"partial");
universe@71 229 expected = ucx_dlist_append(expected, (void*)"test");
universe@71 230 expected = ucx_dlist_append(expected, (void*)"this");
universe@35 231
universe@89 232 list = ucx_dlist_sort(list, ucx_strcmp, NULL);
universe@35 233
universe@35 234 UCX_TEST_BEGIN
universe@35 235 UCX_TEST_ASSERT(
universe@89 236 ucx_dlist_equals(list, expected, ucx_strcmp, NULL), "failed");
universe@35 237 UcxDlist *l = list;
universe@35 238 UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null");
universe@35 239 while (l->next != NULL) {
universe@35 240 UCX_TEST_ASSERT(l->next->prev == l, "prev pointer corrupted");
universe@35 241 l = l->next;
universe@35 242 }
universe@35 243 UCX_TEST_END
universe@35 244
universe@35 245 ucx_dlist_free(expected);
universe@35 246 ucx_dlist_free(list);
universe@35 247 }

mercurial