Wed, 27 Feb 2013 10:28:02 +0100
added tests for ucx_(d)list_contains
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@90 | 120 | UCX_TEST_IMPLEMENT(test_ucx_list_contains) { |
universe@90 | 121 | UcxList *l = ucx_list_append(NULL, (void*)"Contains "); |
universe@90 | 122 | UCX_TEST_BEGIN |
universe@90 | 123 | l = ucx_list_append(l, (void*)"a "); |
universe@90 | 124 | l = ucx_list_append(l, (void*)"string!"); |
universe@90 | 125 | |
universe@90 | 126 | UCX_TEST_ASSERT(ucx_list_contains(l,(void*)"a ",ucx_strcmp,NULL), "failed"); |
universe@90 | 127 | UCX_TEST_ASSERT(!ucx_list_contains(l,(void*)"a",ucx_strcmp,NULL), "failed"); |
universe@90 | 128 | |
universe@90 | 129 | UCX_TEST_END |
universe@90 | 130 | ucx_list_free(l); |
universe@90 | 131 | } |
universe@90 | 132 | |
universe@33 | 133 | UCX_TEST_IMPLEMENT(test_ucx_list_remove) { |
universe@71 | 134 | UcxList *list = ucx_list_append(NULL, (void*)"Hello"); |
universe@33 | 135 | UCX_TEST_BEGIN |
universe@71 | 136 | list = ucx_list_append(list, (void*)" fucking"); |
universe@71 | 137 | list = ucx_list_append(list, (void*)" World!"); |
universe@27 | 138 | |
universe@27 | 139 | list = ucx_list_remove(list, ucx_list_get(list, 1)); |
universe@27 | 140 | |
universe@69 | 141 | UCX_TEST_ASSERT(strncmp((const char*)list->data, "Hello", 5) == 0, |
universe@69 | 142 | "failed"); |
universe@69 | 143 | UCX_TEST_ASSERT(strncmp((const char*)list->next->data, " World!", 7) == 0, |
universe@69 | 144 | "failed"); |
universe@40 | 145 | UCX_TEST_ASSERT(list->next->next == NULL, "failed"); |
universe@33 | 146 | UCX_TEST_END |
universe@27 | 147 | |
universe@27 | 148 | ucx_list_free(list); |
universe@27 | 149 | } |
universe@27 | 150 | |
universe@33 | 151 | UCX_TEST_IMPLEMENT(test_ucx_list_clone) { |
universe@27 | 152 | |
universe@27 | 153 | char *hello = (char*)malloc(6); |
universe@27 | 154 | char *world = (char*)malloc(8); |
universe@27 | 155 | |
universe@27 | 156 | memcpy(hello, "Hello", 6); |
universe@27 | 157 | memcpy(world, " World!", 8); |
universe@27 | 158 | |
universe@27 | 159 | UcxList *list = ucx_list_append(NULL, hello); |
universe@27 | 160 | list = ucx_list_append(list, world); |
universe@27 | 161 | |
universe@27 | 162 | UcxList *copy = ucx_list_clone(list, copy_string, NULL); |
universe@33 | 163 | UCX_TEST_BEGIN |
universe@27 | 164 | |
universe@89 | 165 | UCX_TEST_ASSERT(ucx_list_equals(list, copy, ucx_strcmp, NULL), "failed"); |
universe@40 | 166 | UCX_TEST_ASSERT(hello != copy->data, "first element is no copy"); |
universe@40 | 167 | UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy"); |
universe@33 | 168 | |
universe@33 | 169 | UCX_TEST_END |
universe@27 | 170 | free(copy->next->data); |
universe@27 | 171 | free(copy->data); |
universe@27 | 172 | |
universe@27 | 173 | free(world); |
universe@27 | 174 | free(hello); |
olaf@30 | 175 | ucx_list_free(list); |
olaf@30 | 176 | ucx_list_free(copy); |
universe@27 | 177 | } |
universe@35 | 178 | |
universe@36 | 179 | UCX_TEST_IMPLEMENT(test_ucx_list_sort) { |
universe@71 | 180 | UcxList *list = ucx_list_append(NULL, (void*)"this"); |
universe@71 | 181 | list = ucx_list_append(list, (void*)"is"); |
universe@71 | 182 | list = ucx_list_append(list, (void*)"a"); |
universe@71 | 183 | list = ucx_list_append(list, (void*)"test"); |
universe@71 | 184 | list = ucx_list_append(list, (void*)"for"); |
universe@71 | 185 | list = ucx_list_append(list, (void*)"partial"); |
universe@71 | 186 | list = ucx_list_append(list, (void*)"correctness"); |
universe@35 | 187 | |
universe@71 | 188 | UcxList *expected = ucx_list_append(NULL, (void*)"a"); |
universe@71 | 189 | expected = ucx_list_append(expected, (void*)"correctness"); |
universe@71 | 190 | expected = ucx_list_append(expected, (void*)"for"); |
universe@71 | 191 | expected = ucx_list_append(expected, (void*)"is"); |
universe@71 | 192 | expected = ucx_list_append(expected, (void*)"partial"); |
universe@71 | 193 | expected = ucx_list_append(expected, (void*)"test"); |
universe@71 | 194 | expected = ucx_list_append(expected, (void*)"this"); |
universe@35 | 195 | |
universe@89 | 196 | list = ucx_list_sort(list, ucx_strcmp, NULL); |
universe@35 | 197 | |
universe@35 | 198 | UCX_TEST_BEGIN |
universe@35 | 199 | UCX_TEST_ASSERT( |
universe@89 | 200 | ucx_list_equals(list, expected, ucx_strcmp, NULL), "failed"); |
universe@35 | 201 | UCX_TEST_END |
universe@35 | 202 | |
universe@35 | 203 | ucx_list_free(expected); |
universe@35 | 204 | ucx_list_free(list); |
universe@35 | 205 | } |