test/list_tests.c

Sat, 18 Feb 2012 18:36:30 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 18 Feb 2012 18:36:30 +0100
changeset 27
22644e2572bc
parent 24
e04822101291
child 30
23bb65cbf7a4
permissions
-rw-r--r--

removed old foreach + refactored list tests

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@27 7 UCX_TEST_BEGIN(test_ucx_list_append) {
universe@27 8 UcxList *list = ucx_list_append(NULL, "Hello");
universe@27 9
universe@27 10 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
universe@27 11
universe@27 12 list = ucx_list_append(list, " World!");
universe@27 13
universe@27 14 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
universe@27 15 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
universe@27 16
universe@27 17 ucx_list_free(list);
universe@27 18
universe@27 19 UCX_TEST_END
universe@24 20 }
universe@24 21
universe@27 22 UCX_TEST_BEGIN(test_ucx_list_prepend) {
universe@27 23 UcxList *list = ucx_list_prepend(NULL, " World!");
universe@27 24 list = ucx_list_prepend(list, "Hello");
universe@27 25
universe@27 26 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
universe@27 27 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
universe@27 28 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
universe@27 29
universe@27 30 ucx_list_free(list);
universe@27 31
universe@27 32 UCX_TEST_END
universe@18 33 }
universe@18 34
universe@27 35 UCX_TEST_BEGIN(test_ucx_list_equals) {
universe@27 36 UcxList *list = ucx_list_append(NULL, "Hello");
universe@27 37 list = ucx_list_append(list, " World!");
universe@27 38 UcxList *list2 = ucx_list_prepend(NULL, " World!");
universe@27 39 list2 = ucx_list_prepend(list2, "Hello");
universe@27 40 UcxList *list3 = ucx_list_prepend(NULL, " Welt!");
universe@27 41 list3 = ucx_list_prepend(list3, "Hallo");
universe@27 42
universe@27 43 UCX_TEST_ASSERT(ucx_list_equals(list, list2, cmp_string, NULL), "failed")
universe@27 44 UCX_TEST_ASSERT(!ucx_list_equals(list, list3, cmp_string, NULL), "failed")
universe@27 45
universe@27 46 ucx_list_free(list3);
universe@27 47 ucx_list_free(list2);
universe@27 48 ucx_list_free(list);
universe@27 49
universe@27 50 UCX_TEST_END
universe@24 51 }
universe@24 52
universe@27 53 UCX_TEST_BEGIN(test_ucx_list_concat) {
universe@27 54 UcxList *list = ucx_list_append(NULL, "Hello");
universe@27 55 UcxList *list2 = ucx_list_prepend(NULL, " World!");
universe@27 56
universe@27 57 list = ucx_list_concat(list, list2);
universe@27 58
universe@27 59 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
universe@27 60 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
universe@27 61 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
universe@27 62
universe@27 63 ucx_list_free(list2);
universe@27 64 ucx_list_free(list);
universe@27 65
universe@27 66 UCX_TEST_END
olaf@9 67 }
olaf@9 68
universe@27 69 UCX_TEST_BEGIN(test_ucx_list_size) {
universe@27 70 UcxList *list = ucx_list_append(NULL, "This ");
universe@27 71 list = ucx_list_append(list, "list ");
universe@27 72 list = ucx_list_append(list, "has ");
universe@27 73 list = ucx_list_append(list, "size ");
universe@27 74 list = ucx_list_append(list, "5!");
universe@27 75
universe@27 76 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
universe@27 77
universe@27 78 ucx_list_free(list);
universe@27 79
universe@27 80 UCX_TEST_END
olaf@9 81 }
olaf@11 82
universe@27 83 UCX_TEST_BEGIN(test_ucx_list_last) {
universe@27 84 UcxList *list = ucx_list_append(NULL, "Find ");
universe@27 85 list = ucx_list_append(list, "the ");
universe@27 86 list = ucx_list_append(list, "last!");
universe@27 87
universe@27 88 char* last = (char*) (ucx_list_last(list)->data);
universe@27 89
universe@27 90 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
universe@27 91
universe@27 92 ucx_list_free(list);
universe@27 93
universe@27 94 UCX_TEST_END
universe@27 95 }
universe@27 96
universe@27 97 UCX_TEST_BEGIN(test_ucx_list_get) {
universe@27 98 UcxList *list = ucx_list_append(NULL, "Find ");
universe@27 99 list = ucx_list_append(list, "the ");
universe@27 100 list = ucx_list_append(list, "mid!");
universe@27 101
universe@27 102 char* mid = (char*) (ucx_list_get(list, 1)->data);
universe@27 103
universe@27 104 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
universe@27 105
universe@27 106 ucx_list_free(list);
universe@27 107
universe@27 108 UCX_TEST_END
universe@27 109 }
universe@27 110
universe@27 111 UCX_TEST_BEGIN(test_ucx_list_remove) {
universe@27 112 UcxList *list = ucx_list_append(NULL, "Hello");
universe@27 113 list = ucx_list_append(list, " fucking");
universe@27 114 list = ucx_list_append(list, " World!");
universe@27 115
universe@27 116 list = ucx_list_remove(list, ucx_list_get(list, 1));
universe@27 117
universe@27 118 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
universe@27 119 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
universe@27 120 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
universe@27 121
universe@27 122 ucx_list_free(list);
universe@27 123
universe@27 124 UCX_TEST_END
universe@27 125 }
universe@27 126
universe@27 127 UCX_TEST_BEGIN(test_ucx_list_clone) {
universe@27 128
universe@27 129 char *hello = (char*)malloc(6);
universe@27 130 char *world = (char*)malloc(8);
universe@27 131
universe@27 132 memcpy(hello, "Hello", 6);
universe@27 133 memcpy(world, " World!", 8);
universe@27 134
universe@27 135 UcxList *list = ucx_list_append(NULL, hello);
universe@27 136 list = ucx_list_append(list, world);
universe@27 137
universe@27 138 UcxList *copy = ucx_list_clone(list, copy_string, NULL);
universe@27 139
universe@27 140 UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed")
universe@27 141 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
universe@27 142 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
universe@27 143
universe@27 144 free(copy->next->data);
universe@27 145 free(copy->data);
universe@27 146
universe@27 147 free(world);
universe@27 148 free(hello);
universe@27 149 free(list);
universe@27 150 free(copy);
universe@27 151
universe@27 152 UCX_TEST_END
universe@27 153 }

mercurial