Wed, 27 Feb 2013 10:28:02 +0100
added tests for ucx_(d)list_contains
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@90 | 132 | UCX_TEST_IMPLEMENT(test_ucx_dlist_contains) { |
universe@90 | 133 | UcxDlist *l = ucx_dlist_append(NULL, (void*)"Contains "); |
universe@90 | 134 | UCX_TEST_BEGIN |
universe@90 | 135 | l = ucx_dlist_append(l, (void*)"a "); |
universe@90 | 136 | l = ucx_dlist_append(l, (void*)"string!"); |
universe@90 | 137 | |
universe@90 | 138 | UCX_TEST_ASSERT(ucx_dlist_contains(l,(void*)"a ",ucx_strcmp,NULL),"failed"); |
universe@90 | 139 | UCX_TEST_ASSERT(!ucx_dlist_contains(l,(void*)"a",ucx_strcmp,NULL),"failed"); |
universe@90 | 140 | |
universe@90 | 141 | UCX_TEST_END |
universe@90 | 142 | ucx_dlist_free(l); |
universe@90 | 143 | } |
universe@90 | 144 | |
universe@33 | 145 | UCX_TEST_IMPLEMENT(test_ucx_dlist_remove) { |
universe@71 | 146 | UcxDlist *list = ucx_dlist_append(NULL, (void*)"Hello"); |
universe@33 | 147 | UCX_TEST_BEGIN |
universe@71 | 148 | list = ucx_dlist_append(list, (void*)" fucking"); |
universe@71 | 149 | list = ucx_dlist_append(list, (void*)" World!"); |
universe@27 | 150 | |
universe@27 | 151 | list = ucx_dlist_remove(list, ucx_dlist_get(list, 1)); |
universe@27 | 152 | |
universe@69 | 153 | UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, |
universe@69 | 154 | "failed"); |
universe@69 | 155 | UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, |
universe@69 | 156 | "failed"); |
universe@40 | 157 | UCX_TEST_ASSERT(list->next->next == NULL, "failed"); |
universe@27 | 158 | |
universe@33 | 159 | UCX_TEST_END |
universe@27 | 160 | ucx_dlist_free(list); |
universe@27 | 161 | } |
universe@27 | 162 | |
universe@33 | 163 | UCX_TEST_IMPLEMENT(test_ucx_dlist_clone) { |
universe@27 | 164 | |
universe@27 | 165 | char *hello = (char*)malloc(6); |
universe@27 | 166 | char *world = (char*)malloc(8); |
universe@27 | 167 | |
universe@27 | 168 | memcpy(hello, "Hello", 6); |
universe@27 | 169 | memcpy(world, " World!", 8); |
universe@27 | 170 | |
universe@27 | 171 | UcxDlist *list = ucx_dlist_append(NULL, hello); |
universe@27 | 172 | list = ucx_dlist_append(list, world); |
universe@27 | 173 | |
universe@27 | 174 | UcxDlist *copy = ucx_dlist_clone(list, copy_string, NULL); |
universe@33 | 175 | UCX_TEST_BEGIN |
universe@27 | 176 | |
universe@89 | 177 | UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, ucx_strcmp, NULL), "failed"); |
universe@40 | 178 | UCX_TEST_ASSERT(hello != copy->data, "first element is no copy"); |
universe@40 | 179 | UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy"); |
universe@27 | 180 | |
universe@33 | 181 | UCX_TEST_END |
universe@27 | 182 | free(copy->next->data); |
universe@27 | 183 | free(copy->data); |
universe@27 | 184 | |
universe@27 | 185 | free(world); |
universe@27 | 186 | free(hello); |
olaf@30 | 187 | ucx_dlist_free(list); |
olaf@30 | 188 | ucx_dlist_free(copy); |
universe@27 | 189 | } |
universe@35 | 190 | |
universe@36 | 191 | UCX_TEST_IMPLEMENT(test_ucx_dlist_sort) { |
universe@71 | 192 | UcxDlist *list = ucx_dlist_append(NULL, (void*)"this"); |
universe@71 | 193 | list = ucx_dlist_append(list, (void*)"is"); |
universe@71 | 194 | list = ucx_dlist_append(list, (void*)"a"); |
universe@71 | 195 | list = ucx_dlist_append(list, (void*)"test"); |
universe@71 | 196 | list = ucx_dlist_append(list, (void*)"for"); |
universe@71 | 197 | list = ucx_dlist_append(list, (void*)"partial"); |
universe@71 | 198 | list = ucx_dlist_append(list, (void*)"correctness"); |
universe@35 | 199 | |
universe@71 | 200 | UcxDlist *expected = ucx_dlist_append(NULL, (void*)"a"); |
universe@71 | 201 | expected = ucx_dlist_append(expected, (void*)"correctness"); |
universe@71 | 202 | expected = ucx_dlist_append(expected, (void*)"for"); |
universe@71 | 203 | expected = ucx_dlist_append(expected, (void*)"is"); |
universe@71 | 204 | expected = ucx_dlist_append(expected, (void*)"partial"); |
universe@71 | 205 | expected = ucx_dlist_append(expected, (void*)"test"); |
universe@71 | 206 | expected = ucx_dlist_append(expected, (void*)"this"); |
universe@35 | 207 | |
universe@89 | 208 | list = ucx_dlist_sort(list, ucx_strcmp, NULL); |
universe@35 | 209 | |
universe@35 | 210 | UCX_TEST_BEGIN |
universe@35 | 211 | UCX_TEST_ASSERT( |
universe@89 | 212 | ucx_dlist_equals(list, expected, ucx_strcmp, NULL), "failed"); |
universe@35 | 213 | UcxDlist *l = list; |
universe@35 | 214 | UCX_TEST_ASSERT(l->prev == NULL, "prev field of first entry is not null"); |
universe@35 | 215 | while (l->next != NULL) { |
universe@35 | 216 | UCX_TEST_ASSERT(l->next->prev == l, "prev pointer corrupted"); |
universe@35 | 217 | l = l->next; |
universe@35 | 218 | } |
universe@35 | 219 | UCX_TEST_END |
universe@35 | 220 | |
universe@35 | 221 | ucx_dlist_free(expected); |
universe@35 | 222 | ucx_dlist_free(list); |
universe@35 | 223 | } |