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