test/list_tests.c

Fri, 24 Feb 2012 15:53:50 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 24 Feb 2012 15:53:50 +0100
changeset 30
23bb65cbf7a4
parent 27
22644e2572bc
child 33
9c219a62070d
permissions
-rw-r--r--

some fixes

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(list);
universe@27 64
universe@27 65 UCX_TEST_END
olaf@9 66 }
olaf@9 67
universe@27 68 UCX_TEST_BEGIN(test_ucx_list_size) {
universe@27 69 UcxList *list = ucx_list_append(NULL, "This ");
universe@27 70 list = ucx_list_append(list, "list ");
universe@27 71 list = ucx_list_append(list, "has ");
universe@27 72 list = ucx_list_append(list, "size ");
universe@27 73 list = ucx_list_append(list, "5!");
universe@27 74
universe@27 75 UCX_TEST_ASSERT(ucx_list_size(list) == 5, "failed");
universe@27 76
universe@27 77 ucx_list_free(list);
universe@27 78
universe@27 79 UCX_TEST_END
olaf@9 80 }
olaf@11 81
universe@27 82 UCX_TEST_BEGIN(test_ucx_list_last) {
universe@27 83 UcxList *list = ucx_list_append(NULL, "Find ");
universe@27 84 list = ucx_list_append(list, "the ");
universe@27 85 list = ucx_list_append(list, "last!");
universe@27 86
universe@27 87 char* last = (char*) (ucx_list_last(list)->data);
universe@27 88
universe@27 89 UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
universe@27 90
universe@27 91 ucx_list_free(list);
universe@27 92
universe@27 93 UCX_TEST_END
universe@27 94 }
universe@27 95
universe@27 96 UCX_TEST_BEGIN(test_ucx_list_get) {
universe@27 97 UcxList *list = ucx_list_append(NULL, "Find ");
universe@27 98 list = ucx_list_append(list, "the ");
universe@27 99 list = ucx_list_append(list, "mid!");
universe@27 100
universe@27 101 char* mid = (char*) (ucx_list_get(list, 1)->data);
universe@27 102
universe@27 103 UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
universe@27 104
universe@27 105 ucx_list_free(list);
universe@27 106
universe@27 107 UCX_TEST_END
universe@27 108 }
universe@27 109
universe@27 110 UCX_TEST_BEGIN(test_ucx_list_remove) {
universe@27 111 UcxList *list = ucx_list_append(NULL, "Hello");
universe@27 112 list = ucx_list_append(list, " fucking");
universe@27 113 list = ucx_list_append(list, " World!");
universe@27 114
universe@27 115 list = ucx_list_remove(list, ucx_list_get(list, 1));
universe@27 116
universe@27 117 UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
universe@27 118 UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
universe@27 119 UCX_TEST_ASSERT(list->next->next == NULL, "failed")
universe@27 120
universe@27 121 ucx_list_free(list);
universe@27 122
universe@27 123 UCX_TEST_END
universe@27 124 }
universe@27 125
universe@27 126 UCX_TEST_BEGIN(test_ucx_list_clone) {
universe@27 127
universe@27 128 char *hello = (char*)malloc(6);
universe@27 129 char *world = (char*)malloc(8);
universe@27 130
universe@27 131 memcpy(hello, "Hello", 6);
universe@27 132 memcpy(world, " World!", 8);
universe@27 133
universe@27 134 UcxList *list = ucx_list_append(NULL, hello);
universe@27 135 list = ucx_list_append(list, world);
universe@27 136
universe@27 137 UcxList *copy = ucx_list_clone(list, copy_string, NULL);
universe@27 138
universe@27 139 UCX_TEST_ASSERT(ucx_list_equals(list, copy, cmp_string, NULL), "failed")
universe@27 140 UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
universe@27 141 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
olaf@30 142
universe@27 143 free(copy->next->data);
universe@27 144 free(copy->data);
universe@27 145
universe@27 146 free(world);
universe@27 147 free(hello);
olaf@30 148 ucx_list_free(list);
olaf@30 149 ucx_list_free(copy);
universe@27 150
universe@27 151 UCX_TEST_END
universe@27 152 }

mercurial