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