Wed, 27 Feb 2013 10:09:23 +0100
comparator module
olaf@9 | 1 | /* |
olaf@9 | 2 | * tests of list implementation |
olaf@9 | 3 | */ |
olaf@9 | 4 | |
universe@27 | 5 | #include "list_tests.h" |
universe@89 | 6 | #include "ucx/comparator.h" |
olaf@9 | 7 | |
universe@33 | 8 | UCX_TEST_IMPLEMENT(test_ucx_list_append) { |
universe@71 | 9 | UcxList *list = ucx_list_append(NULL, (void*)"Hello"); |
universe@33 | 10 | UCX_TEST_BEGIN |
universe@69 | 11 | UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, |
universe@69 | 12 | "failed"); |
universe@27 | 13 | |
universe@71 | 14 | list = ucx_list_append(list, (void*)" 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 | |
universe@33 | 20 | UCX_TEST_END |
universe@27 | 21 | ucx_list_free(list); |
universe@24 | 22 | } |
universe@24 | 23 | |
universe@33 | 24 | UCX_TEST_IMPLEMENT(test_ucx_list_prepend) { |
universe@71 | 25 | UcxList *list = ucx_list_prepend(NULL, (void*)" World!"); |
universe@33 | 26 | UCX_TEST_BEGIN |
universe@71 | 27 | list = ucx_list_prepend(list, (void*)"Hello"); |
universe@27 | 28 | |
universe@69 | 29 | UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, |
universe@69 | 30 | "failed"); |
universe@69 | 31 | UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, |
universe@69 | 32 | "failed"); |
universe@40 | 33 | UCX_TEST_ASSERT(list->next->next == NULL, "failed"); |
universe@27 | 34 | |
universe@33 | 35 | UCX_TEST_END |
universe@27 | 36 | ucx_list_free(list); |
universe@18 | 37 | } |
universe@18 | 38 | |
universe@33 | 39 | UCX_TEST_IMPLEMENT(test_ucx_list_equals) { |
universe@71 | 40 | UcxList *list = ucx_list_append(NULL, (void*)"Hello"); |
universe@71 | 41 | list = ucx_list_append(list, (void*)" World!"); |
universe@71 | 42 | UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!"); |
universe@71 | 43 | list2 = ucx_list_prepend(list2, (void*)"Hello"); |
universe@71 | 44 | UcxList *list3 = ucx_list_prepend(NULL, (void*)" Welt!"); |
universe@71 | 45 | list3 = ucx_list_prepend(list3, (void*)"Hallo"); |
universe@27 | 46 | |
universe@33 | 47 | UCX_TEST_BEGIN |
universe@89 | 48 | UCX_TEST_ASSERT(ucx_list_equals(list, list2, ucx_strcmp, NULL), "failed"); |
universe@89 | 49 | UCX_TEST_ASSERT(!ucx_list_equals(list, list3, ucx_strcmp, NULL), "failed"); |
universe@33 | 50 | UCX_TEST_END |
universe@27 | 51 | |
universe@27 | 52 | ucx_list_free(list3); |
universe@27 | 53 | ucx_list_free(list2); |
universe@27 | 54 | ucx_list_free(list); |
universe@24 | 55 | } |
universe@24 | 56 | |
universe@33 | 57 | UCX_TEST_IMPLEMENT(test_ucx_list_concat) { |
universe@71 | 58 | UcxList *list = ucx_list_append(NULL, (void*)"Hello"); |
universe@71 | 59 | UcxList *list2 = ucx_list_prepend(NULL, (void*)" World!"); |
universe@27 | 60 | |
universe@27 | 61 | list = ucx_list_concat(list, list2); |
universe@33 | 62 | UCX_TEST_BEGIN |
universe@27 | 63 | |
universe@69 | 64 | UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, |
universe@69 | 65 | "failed"); |
universe@69 | 66 | UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, |
universe@69 | 67 | "failed"); |
universe@40 | 68 | UCX_TEST_ASSERT(list->next->next == NULL, "failed"); |
universe@27 | 69 | |
universe@33 | 70 | UCX_TEST_END |
universe@33 | 71 | if (list->next == NULL) { |
universe@33 | 72 | ucx_list_free(list2); |
universe@33 | 73 | } |
universe@27 | 74 | ucx_list_free(list); |
olaf@9 | 75 | } |
olaf@9 | 76 | |
universe@33 | 77 | UCX_TEST_IMPLEMENT(test_ucx_list_size) { |
universe@71 | 78 | UcxList *list = ucx_list_append(NULL, (void*)"This "); |
universe@33 | 79 | UCX_TEST_BEGIN |
universe@71 | 80 | list = ucx_list_append(list, (void*)"list "); |
universe@71 | 81 | list = ucx_list_append(list, (void*)"has "); |
universe@71 | 82 | list = ucx_list_append(list, (void*)"size "); |
universe@71 | 83 | list = ucx_list_append(list, (void*)"5!"); |
universe@27 | 84 | |
universe@27 | 85 | UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed"); |
universe@27 | 86 | |
universe@33 | 87 | UCX_TEST_END |
universe@27 | 88 | ucx_list_free(list); |
olaf@9 | 89 | } |
olaf@11 | 90 | |
universe@33 | 91 | UCX_TEST_IMPLEMENT(test_ucx_list_last) { |
universe@71 | 92 | UcxList *list = ucx_list_append(NULL, (void*)"Find "); |
universe@33 | 93 | UCX_TEST_BEGIN |
universe@71 | 94 | list = ucx_list_append(list, (void*)"the "); |
universe@71 | 95 | list = ucx_list_append(list, (void*)"last!"); |
universe@27 | 96 | |
universe@69 | 97 | const char* last = (const char*) (ucx_list_last(list)->data); |
universe@27 | 98 | |
universe@27 | 99 | UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed"); |
universe@27 | 100 | |
universe@33 | 101 | UCX_TEST_END |
universe@27 | 102 | ucx_list_free(list); |
universe@27 | 103 | |
universe@27 | 104 | } |
universe@27 | 105 | |
universe@33 | 106 | UCX_TEST_IMPLEMENT(test_ucx_list_get) { |
universe@71 | 107 | UcxList *list = ucx_list_append(NULL, (void*)"Find "); |
universe@33 | 108 | UCX_TEST_BEGIN |
universe@71 | 109 | list = ucx_list_append(list, (void*)"the "); |
universe@71 | 110 | list = ucx_list_append(list, (void*)"mid!"); |
universe@27 | 111 | |
universe@69 | 112 | const char* mid = (const char*) (ucx_list_get(list, 1)->data); |
universe@27 | 113 | |
universe@27 | 114 | UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed"); |
universe@27 | 115 | |
universe@33 | 116 | UCX_TEST_END |
universe@27 | 117 | ucx_list_free(list); |
universe@27 | 118 | } |
universe@27 | 119 | |
universe@33 | 120 | UCX_TEST_IMPLEMENT(test_ucx_list_remove) { |
universe@71 | 121 | UcxList *list = ucx_list_append(NULL, (void*)"Hello"); |
universe@33 | 122 | UCX_TEST_BEGIN |
universe@71 | 123 | list = ucx_list_append(list, (void*)" fucking"); |
universe@71 | 124 | list = ucx_list_append(list, (void*)" World!"); |
universe@27 | 125 | |
universe@27 | 126 | list = ucx_list_remove(list, ucx_list_get(list, 1)); |
universe@27 | 127 | |
universe@69 | 128 | UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, |
universe@69 | 129 | "failed"); |
universe@69 | 130 | UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, |
universe@69 | 131 | "failed"); |
universe@40 | 132 | UCX_TEST_ASSERT(list->next->next == NULL, "failed"); |
universe@33 | 133 | UCX_TEST_END |
universe@27 | 134 | |
universe@27 | 135 | ucx_list_free(list); |
universe@27 | 136 | } |
universe@27 | 137 | |
universe@33 | 138 | UCX_TEST_IMPLEMENT(test_ucx_list_clone) { |
universe@27 | 139 | |
universe@27 | 140 | char *hello = (char*)malloc(6); |
universe@27 | 141 | char *world = (char*)malloc(8); |
universe@27 | 142 | |
universe@27 | 143 | memcpy(hello, "Hello", 6); |
universe@27 | 144 | memcpy(world, " World!", 8); |
universe@27 | 145 | |
universe@27 | 146 | UcxList *list = ucx_list_append(NULL, hello); |
universe@27 | 147 | list = ucx_list_append(list, world); |
universe@27 | 148 | |
universe@27 | 149 | UcxList *copy = ucx_list_clone(list, copy_string, NULL); |
universe@33 | 150 | UCX_TEST_BEGIN |
universe@27 | 151 | |
universe@89 | 152 | UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed"); |
universe@40 | 153 | UCX_TEST_ASSERT(hello != copy->data, "first element is no copy"); |
universe@40 | 154 | UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy"); |
universe@33 | 155 | |
universe@33 | 156 | UCX_TEST_END |
universe@27 | 157 | free(copy->next->data); |
universe@27 | 158 | free(copy->data); |
universe@27 | 159 | |
universe@27 | 160 | free(world); |
universe@27 | 161 | free(hello); |
olaf@30 | 162 | ucx_list_free(list); |
olaf@30 | 163 | ucx_list_free(copy); |
universe@27 | 164 | } |
universe@35 | 165 | |
universe@36 | 166 | UCX_TEST_IMPLEMENT(test_ucx_list_sort) { |
universe@71 | 167 | UcxList *list = ucx_list_append(NULL, (void*)"this"); |
universe@71 | 168 | list = ucx_list_append(list, (void*)"is"); |
universe@71 | 169 | list = ucx_list_append(list, (void*)"a"); |
universe@71 | 170 | list = ucx_list_append(list, (void*)"test"); |
universe@71 | 171 | list = ucx_list_append(list, (void*)"for"); |
universe@71 | 172 | list = ucx_list_append(list, (void*)"partial"); |
universe@71 | 173 | list = ucx_list_append(list, (void*)"correctness"); |
universe@35 | 174 | |
universe@71 | 175 | UcxList *expected = ucx_list_append(NULL, (void*)"a"); |
universe@71 | 176 | expected = ucx_list_append(expected, (void*)"correctness"); |
universe@71 | 177 | expected = ucx_list_append(expected, (void*)"for"); |
universe@71 | 178 | expected = ucx_list_append(expected, (void*)"is"); |
universe@71 | 179 | expected = ucx_list_append(expected, (void*)"partial"); |
universe@71 | 180 | expected = ucx_list_append(expected, (void*)"test"); |
universe@71 | 181 | expected = ucx_list_append(expected, (void*)"this"); |
universe@35 | 182 | |
universe@89 | 183 | list = ucx_list_sort(list, ucx_strcmp, NULL); |
universe@35 | 184 | |
universe@35 | 185 | UCX_TEST_BEGIN |
universe@35 | 186 | UCX_TEST_ASSERT( |
universe@89 | 187 | ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed"); |
universe@35 | 188 | UCX_TEST_END |
universe@35 | 189 | |
universe@35 | 190 | ucx_list_free(expected); |
universe@35 | 191 | ucx_list_free(list); |
universe@35 | 192 | } |