test/list_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 "list_tests.h"
universe@94 30 #include "ucx/utils.h"
olaf@9 31
universe@33 32 UCX_TEST_IMPLEMENT(test_ucx_list_append) {
universe@71 33 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
universe@33 34 UCX_TEST_BEGIN
universe@69 35 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
universe@69 36 "failed");
universe@27 37
universe@71 38 list = ucx_list_append(list, (void*)" World!");
universe@27 39
universe@69 40 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
universe@69 41 "failed");
universe@40 42 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
universe@33 43
universe@33 44 UCX_TEST_END
universe@27 45 ucx_list_free(list);
universe@24 46 }
universe@24 47
universe@33 48 UCX_TEST_IMPLEMENT(test_ucx_list_prepend) {
universe@71 49 UcxList *list = ucx_list_prepend(NULL, (void*)" World!");
universe@33 50 UCX_TEST_BEGIN
universe@71 51 list = ucx_list_prepend(list, (void*)"Hello");
universe@27 52
universe@69 53 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
universe@69 54 "failed");
universe@69 55 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
universe@69 56 "failed");
universe@40 57 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
universe@27 58
universe@33 59 UCX_TEST_END
universe@27 60 ucx_list_free(list);
universe@18 61 }
universe@18 62
universe@33 63 UCX_TEST_IMPLEMENT(test_ucx_list_equals) {
universe@71 64 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
universe@71 65 list = ucx_list_append(list, (void*)" World!");
universe@71 66 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
universe@71 67 list2 = ucx_list_prepend(list2, (void*)"Hello");
universe@71 68 UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!");
universe@71 69 list3 = ucx_list_prepend(list3, (void*)"Hallo");
universe@27 70
universe@33 71 UCX_TEST_BEGIN
universe@89 72 UCX_TEST_ASSERT(ucx_list_equals(list, list2, ucx_strcmp, NULL), "failed");
universe@89 73 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed");
universe@33 74 UCX_TEST_END
universe@27 75
universe@27 76 ucx_list_free(list3);
universe@27 77 ucx_list_free(list2);
universe@27 78 ucx_list_free(list);
universe@24 79 }
universe@24 80
universe@33 81 UCX_TEST_IMPLEMENT(test_ucx_list_concat) {
universe@71 82 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
universe@71 83 UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!");
universe@27 84
universe@27 85 list = ucx_list_concat(list, list2);
universe@33 86 UCX_TEST_BEGIN
universe@27 87
universe@69 88 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
universe@69 89 "failed");
universe@69 90 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
universe@69 91 "failed");
universe@40 92 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
universe@27 93
universe@33 94 UCX_TEST_END
universe@33 95 if (list->next == NULL) {
universe@33 96 ucx_list_free(list2);
universe@33 97 }
universe@27 98 ucx_list_free(list);
olaf@9 99 }
olaf@9 100
universe@33 101 UCX_TEST_IMPLEMENT(test_ucx_list_size) {
universe@71 102 UcxList *list = ucx_list_append(NULL, (void*)"This ");
universe@33 103 UCX_TEST_BEGIN
universe@71 104 list = ucx_list_append(list, (void*)"list ");
universe@71 105 list = ucx_list_append(list, (void*)"has ");
universe@71 106 list = ucx_list_append(list, (void*)"size ");
universe@71 107 list = ucx_list_append(list, (void*)"5!");
universe@27 108
universe@27 109 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
universe@27 110
universe@33 111 UCX_TEST_END
universe@27 112 ucx_list_free(list);
olaf@9 113 }
olaf@11 114
universe@33 115 UCX_TEST_IMPLEMENT(test_ucx_list_last) {
universe@71 116 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
universe@33 117 UCX_TEST_BEGIN
universe@71 118 list = ucx_list_append(list, (void*)"the ");
universe@71 119 list = ucx_list_append(list, (void*)"last!");
universe@27 120
universe@69 121 const char* last = (const char*) (ucx_list_last(list)->data);
universe@27 122
universe@27 123 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
universe@27 124
universe@33 125 UCX_TEST_END
universe@27 126 ucx_list_free(list);
universe@27 127
universe@27 128 }
universe@27 129
universe@33 130 UCX_TEST_IMPLEMENT(test_ucx_list_get) {
universe@71 131 UcxList *list = ucx_list_append(NULL, (void*)"Find ");
universe@33 132 UCX_TEST_BEGIN
universe@71 133 list = ucx_list_append(list, (void*)"the ");
universe@71 134 list = ucx_list_append(list, (void*)"mid!");
universe@27 135
universe@69 136 const char* mid = (const char*) (ucx_list_get(list, 1)->data);
universe@27 137
universe@27 138 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
universe@27 139
universe@33 140 UCX_TEST_END
universe@27 141 ucx_list_free(list);
universe@27 142 }
universe@27 143
universe@90 144 UCX_TEST_IMPLEMENT(test_ucx_list_contains) {
universe@90 145 UcxList *l = ucx_list_append(NULL, (void*)"Contains ");
universe@90 146 UCX_TEST_BEGIN
universe@90 147 l = ucx_list_append(l, (void*)"a ");
universe@90 148 l = ucx_list_append(l, (void*)"string!");
universe@90 149
universe@90 150 UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL), "failed");
universe@90 151 UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL), "failed");
universe@90 152
universe@90 153 UCX_TEST_END
universe@90 154 ucx_list_free(l);
universe@90 155 }
universe@90 156
universe@33 157 UCX_TEST_IMPLEMENT(test_ucx_list_remove) {
universe@71 158 UcxList *list = ucx_list_append(NULL, (void*)"Hello");
universe@33 159 UCX_TEST_BEGIN
universe@71 160 list = ucx_list_append(list, (void*)" fucking");
universe@71 161 list = ucx_list_append(list, (void*)" World!");
universe@27 162
universe@27 163 list = ucx_list_remove(list, ucx_list_get(list, 1));
universe@27 164
universe@69 165 UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0,
universe@69 166 "failed");
universe@69 167 UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0,
universe@69 168 "failed");
universe@40 169 UCX_TEST_ASSERT(list->next->next == NULL, "failed");
universe@33 170 UCX_TEST_END
universe@27 171
universe@27 172 ucx_list_free(list);
universe@27 173 }
universe@27 174
universe@33 175 UCX_TEST_IMPLEMENT(test_ucx_list_clone) {
universe@27 176
universe@27 177 char *hello = (char*)malloc(6);
universe@27 178 char *world = (char*)malloc(8);
universe@27 179
universe@27 180 memcpy(hello, "Hello", 6);
universe@27 181 memcpy(world, " World!", 8);
universe@27 182
universe@27 183 UcxList *list = ucx_list_append(NULL, hello);
universe@27 184 list = ucx_list_append(list, world);
universe@27 185
universe@94 186 UcxList *copy = ucx_list_clone(list, ucx_strcpy, NULL);
universe@33 187 UCX_TEST_BEGIN
universe@27 188
universe@89 189 UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed");
universe@40 190 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy");
universe@40 191 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
universe@33 192
universe@33 193 UCX_TEST_END
universe@27 194 free(copy->next->data);
universe@27 195 free(copy->data);
universe@27 196
universe@27 197 free(world);
universe@27 198 free(hello);
olaf@30 199 ucx_list_free(list);
olaf@30 200 ucx_list_free(copy);
universe@27 201 }
universe@35 202
universe@36 203 UCX_TEST_IMPLEMENT(test_ucx_list_sort) {
universe@71 204 UcxList *list = ucx_list_append(NULL, (void*)"this");
universe@71 205 list = ucx_list_append(list, (void*)"is");
universe@71 206 list = ucx_list_append(list, (void*)"a");
universe@71 207 list = ucx_list_append(list, (void*)"test");
universe@71 208 list = ucx_list_append(list, (void*)"for");
universe@71 209 list = ucx_list_append(list, (void*)"partial");
universe@71 210 list = ucx_list_append(list, (void*)"correctness");
universe@35 211
universe@71 212 UcxList *expected = ucx_list_append(NULL, (void*)"a");
universe@71 213 expected = ucx_list_append(expected, (void*)"correctness");
universe@71 214 expected = ucx_list_append(expected, (void*)"for");
universe@71 215 expected = ucx_list_append(expected, (void*)"is");
universe@71 216 expected = ucx_list_append(expected, (void*)"partial");
universe@71 217 expected = ucx_list_append(expected, (void*)"test");
universe@71 218 expected = ucx_list_append(expected, (void*)"this");
universe@35 219
universe@89 220 list = ucx_list_sort(list, ucx_strcmp, NULL);
universe@35 221
universe@35 222 UCX_TEST_BEGIN
universe@35 223 UCX_TEST_ASSERT(
universe@89 224 ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed");
universe@35 225 UCX_TEST_END
universe@35 226
universe@35 227 ucx_list_free(expected);
universe@35 228 ucx_list_free(list);
universe@35 229 }

mercurial