Wed, 27 Feb 2013 10:09:23 +0100
comparator module
olaf@9 | 1 | /* |
universe@27 | 2 | * tests of dlist implementation |
olaf@9 | 3 | */ |
olaf@9 | 4 | |
universe@27 | 5 | #include "dlist_tests.h" |
universe@89 | 6 | #include "ucx/comparator.h" |
olaf@9 | 7 | |
universe@33 | 8 | UCX_TEST_IMPLEMENT(test_ucx_dlist_append) { |
universe@71 | 9 | UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello"); |
universe@33 | 10 | UCX_TEST_BEGIN |
universe@27 | 11 | |
universe@69 | 12 | UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, |
universe@69 | 13 | "failed"); |
universe@27 | 14 | |
universe@71 | 15 | list = ucx_dlist_append(list, (void*)" World!"); |
universe@27 | 16 | |
universe@69 | 17 | UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, |
universe@69 | 18 | "failed"); |
universe@40 | 19 | UCX_TEST_ASSERT(list->next->next == NULL, "failed"); |
universe@33 | 20 | UCX_TEST_END |
universe@27 | 21 | |
universe@27 | 22 | ucx_dlist_free(list); |
universe@24 | 23 | } |
universe@24 | 24 | |
universe@33 | 25 | UCX_TEST_IMPLEMENT(test_ucx_dlist_prepend) { |
universe@71 | 26 | UcxDlist *list = ucx_dlist_prepend(NULL, (void*)" World!"); |
universe@33 | 27 | UCX_TEST_BEGIN |
universe@33 | 28 | |
universe@71 | 29 | list = ucx_dlist_prepend(list, (void*)"Hello"); |
universe@27 | 30 | |
universe@69 | 31 | UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, |
universe@69 | 32 | "failed"); |
universe@69 | 33 | UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, |
universe@69 | 34 | "failed"); |
universe@40 | 35 | UCX_TEST_ASSERT(list->next->next == NULL, "failed"); |
universe@27 | 36 | |
universe@33 | 37 | UCX_TEST_END |
universe@27 | 38 | ucx_dlist_free(list); |
universe@18 | 39 | } |
universe@18 | 40 | |
universe@33 | 41 | UCX_TEST_IMPLEMENT(test_ucx_dlist_equals) { |
universe@71 | 42 | UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello"); |
universe@71 | 43 | list = ucx_dlist_append(list, (void*)" World!"); |
universe@71 | 44 | UcxDlist *list2 = ucx_dlist_prepend(NULL, (void*)" World!"); |
universe@71 | 45 | list2 = ucx_dlist_prepend(list2, (void*)"Hello"); |
universe@71 | 46 | UcxDlist *list3 = ucx_dlist_prepend(NULL, (void*)" Welt!"); |
universe@71 | 47 | list3 = ucx_dlist_prepend(list3, (void*)"Hallo"); |
universe@33 | 48 | UCX_TEST_BEGIN |
universe@27 | 49 | |
universe@89 | 50 | UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, ucx_strcmp, NULL), "failed"); |
universe@89 | 51 | UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, ucx_strcmp, NULL), "failed"); |
universe@27 | 52 | |
universe@33 | 53 | UCX_TEST_END |
universe@27 | 54 | ucx_dlist_free(list3); |
universe@27 | 55 | ucx_dlist_free(list2); |
universe@27 | 56 | ucx_dlist_free(list); |
universe@24 | 57 | } |
universe@24 | 58 | |
universe@33 | 59 | UCX_TEST_IMPLEMENT(test_ucx_dlist_concat) { |
universe@71 | 60 | UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello"); |
universe@71 | 61 | UcxDlist *list2 = ucx_dlist_prepend(NULL, (void*)" World!"); |
universe@33 | 62 | UCX_TEST_BEGIN |
universe@27 | 63 | |
universe@27 | 64 | list = ucx_dlist_concat(list, list2); |
universe@27 | 65 | |
universe@69 | 66 | UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, |
universe@69 | 67 | "failed"); |
universe@69 | 68 | UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, |
universe@69 | 69 | "failed"); |
universe@40 | 70 | UCX_TEST_ASSERT(list->next->next == NULL, "failed"); |
universe@27 | 71 | |
universe@33 | 72 | UCX_TEST_END |
universe@27 | 73 | ucx_dlist_free(list); |
olaf@9 | 74 | } |
olaf@9 | 75 | |
universe@33 | 76 | UCX_TEST_IMPLEMENT(test_ucx_dlist_size) { |
universe@71 | 77 | UcxDlist *list = ucx_dlist_append(NULL, (void*)"This "); |
universe@33 | 78 | UCX_TEST_BEGIN |
universe@71 | 79 | list = ucx_dlist_append(list, (void*)"list "); |
universe@71 | 80 | list = ucx_dlist_append(list, (void*)"has "); |
universe@71 | 81 | list = ucx_dlist_append(list, (void*)"size "); |
universe@71 | 82 | list = ucx_dlist_append(list, (void*)"5!"); |
universe@27 | 83 | |
universe@27 | 84 | UCX_TEST_ASSERT(ucx_dlist_size(list) == 5, "failed"); |
universe@27 | 85 | |
universe@33 | 86 | UCX_TEST_END |
universe@27 | 87 | ucx_dlist_free(list); |
olaf@9 | 88 | } |
olaf@11 | 89 | |
universe@33 | 90 | UCX_TEST_IMPLEMENT(test_ucx_dlist_first) { |
universe@71 | 91 | UcxDlist *list = ucx_dlist_append(NULL, (void*)"Find "); |
universe@33 | 92 | UCX_TEST_BEGIN |
universe@71 | 93 | list = ucx_dlist_append(list, (void*)"the "); |
universe@71 | 94 | list = ucx_dlist_append(list, (void*)"first!"); |
universe@27 | 95 | |
universe@69 | 96 | const char* first = (const char*) (ucx_dlist_first(list)->data); |
universe@27 | 97 | |
universe@27 | 98 | UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed"); |
universe@27 | 99 | |
universe@33 | 100 | UCX_TEST_END |
universe@27 | 101 | ucx_dlist_free(list); |
universe@27 | 102 | } |
universe@27 | 103 | |
universe@33 | 104 | UCX_TEST_IMPLEMENT(test_ucx_dlist_last) { |
universe@71 | 105 | UcxDlist *list = ucx_dlist_append(NULL, (void*)"Find "); |
universe@33 | 106 | UCX_TEST_BEGIN |
universe@71 | 107 | list = ucx_dlist_append(list, (void*)"the "); |
universe@71 | 108 | list = ucx_dlist_append(list, (void*)"last!"); |
universe@27 | 109 | |
universe@69 | 110 | const char* last = (const char*) (ucx_dlist_last(list)->data); |
universe@27 | 111 | |
universe@27 | 112 | UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed"); |
universe@27 | 113 | |
universe@33 | 114 | UCX_TEST_END |
universe@27 | 115 | ucx_dlist_free(list); |
universe@27 | 116 | } |
universe@27 | 117 | |
universe@33 | 118 | UCX_TEST_IMPLEMENT(test_ucx_dlist_get) { |
universe@71 | 119 | UcxDlist *list = ucx_dlist_append(NULL, (void*)"Find "); |
universe@33 | 120 | UCX_TEST_BEGIN |
universe@71 | 121 | list = ucx_dlist_append(list, (void*)"the "); |
universe@71 | 122 | list = ucx_dlist_append(list, (void*)"mid!"); |
universe@27 | 123 | |
universe@69 | 124 | const char* mid = (const char*) (ucx_dlist_get(list, 1)->data); |
universe@27 | 125 | |
universe@27 | 126 | UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed"); |
universe@27 | 127 | |
universe@33 | 128 | UCX_TEST_END |
universe@27 | 129 | ucx_dlist_free(list); |
universe@27 | 130 | } |
universe@27 | 131 | |
universe@33 | 132 | UCX_TEST_IMPLEMENT(test_ucx_dlist_remove) { |
universe@71 | 133 | UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello"); |
universe@33 | 134 | UCX_TEST_BEGIN |
universe@71 | 135 | list = ucx_dlist_append(list, (void*)" fucking"); |
universe@71 | 136 | list = ucx_dlist_append(list, (void*)" World!"); |
universe@27 | 137 | |
universe@27 | 138 | list = ucx_dlist_remove(list, ucx_dlist_get(list, 1)); |
universe@27 | 139 | |
universe@69 | 140 | UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, |
universe@69 | 141 | "failed"); |
universe@69 | 142 | UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, |
universe@69 | 143 | "failed"); |
universe@40 | 144 | UCX_TEST_ASSERT(list->next->next == NULL, "failed"); |
universe@27 | 145 | |
universe@33 | 146 | UCX_TEST_END |
universe@27 | 147 | ucx_dlist_free(list); |
universe@27 | 148 | } |
universe@27 | 149 | |
universe@33 | 150 | UCX_TEST_IMPLEMENT(test_ucx_dlist_clone) { |
universe@27 | 151 | |
universe@27 | 152 | char *hello = (char*)malloc(6); |
universe@27 | 153 | char *world = (char*)malloc(8); |
universe@27 | 154 | |
universe@27 | 155 | memcpy(hello, "Hello", 6); |
universe@27 | 156 | memcpy(world, " World!", 8); |
universe@27 | 157 | |
universe@27 | 158 | UcxDlist *list = ucx_dlist_append(NULL, hello); |
universe@27 | 159 | list = ucx_dlist_append(list, world); |
universe@27 | 160 | |
universe@27 | 161 | UcxDlist *copy = ucx_dlist_clone(list, copy_string, NULL); |
universe@33 | 162 | UCX_TEST_BEGIN |
universe@27 | 163 | |
universe@89 | 164 | UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, ucx_strcmp, NULL), "failed"); |
universe@40 | 165 | UCX_TEST_ASSERT(hello != copy->data, "first element is no copy"); |
universe@40 | 166 | UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy"); |
universe@27 | 167 | |
universe@33 | 168 | UCX_TEST_END |
universe@27 | 169 | free(copy->next->data); |
universe@27 | 170 | free(copy->data); |
universe@27 | 171 | |
universe@27 | 172 | free(world); |
universe@27 | 173 | free(hello); |
olaf@30 | 174 | ucx_dlist_free(list); |
olaf@30 | 175 | ucx_dlist_free(copy); |
universe@27 | 176 | } |
universe@35 | 177 | |
universe@36 | 178 | UCX_TEST_IMPLEMENT(test_ucx_dlist_sort) { |
universe@71 | 179 | UcxDlist *list = ucx_dlist_append(NULL, (void*)"this"); |
universe@71 | 180 | list = ucx_dlist_append(list, (void*)"is"); |
universe@71 | 181 | list = ucx_dlist_append(list, (void*)"a"); |
universe@71 | 182 | list = ucx_dlist_append(list, (void*)"test"); |
universe@71 | 183 | list = ucx_dlist_append(list, (void*)"for"); |
universe@71 | 184 | list = ucx_dlist_append(list, (void*)"partial"); |
universe@71 | 185 | list = ucx_dlist_append(list, (void*)"correctness"); |
universe@35 | 186 | |
universe@71 | 187 | UcxDlist *expected = ucx_dlist_append(NULL, (void*)"a"); |
universe@71 | 188 | expected = ucx_dlist_append(expected, (void*)"correctness"); |
universe@71 | 189 | expected = ucx_dlist_append(expected, (void*)"for"); |
universe@71 | 190 | expected = ucx_dlist_append(expected, (void*)"is"); |
universe@71 | 191 | expected = ucx_dlist_append(expected, (void*)"partial"); |
universe@71 | 192 | expected = ucx_dlist_append(expected, (void*)"test"); |
universe@71 | 193 | expected = ucx_dlist_append(expected, (void*)"this"); |
universe@35 | 194 | |
universe@89 | 195 | list = ucx_dlist_sort(list, ucx_strcmp, NULL); |
universe@35 | 196 | |
universe@35 | 197 | UCX_TEST_BEGIN |
universe@35 | 198 | UCX_TEST_ASSERT( |
universe@89 | 199 | ucx_dlist_equals(list, expected, ucx_strcmp, NULL), "failed"); |
universe@35 | 200 | UcxDlist *l = list; |
universe@35 | 201 | UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null"); |
universe@35 | 202 | while (l->next != NULL) { |
universe@35 | 203 | UCX_TEST_ASSERT(l->next->prev == l, "prev pointer corrupted"); |
universe@35 | 204 | l = l->next; |
universe@35 | 205 | } |
universe@35 | 206 | UCX_TEST_END |
universe@35 | 207 | |
universe@35 | 208 | ucx_dlist_free(expected); |
universe@35 | 209 | ucx_dlist_free(list); |
universe@35 | 210 | } |