test/dlist_tests.c

changeset 27
22644e2572bc
parent 24
e04822101291
child 30
23bb65cbf7a4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/dlist_tests.c	Sat Feb 18 18:36:30 2012 +0100
@@ -0,0 +1,167 @@
+/*
+ * tests of dlist implementation
+ */
+
+#include "dlist_tests.h"
+
+UCX_TEST_BEGIN(test_ucx_dlist_append) {
+    UcxDlist *list = ucx_dlist_append(NULL, "Hello");
+    
+    UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
+    
+    list = ucx_dlist_append(list, " World!");
+    
+    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
+    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
+    
+    ucx_dlist_free(list);
+    
+    UCX_TEST_END
+}
+
+UCX_TEST_BEGIN(test_ucx_dlist_prepend) {
+    UcxDlist *list = ucx_dlist_prepend(NULL, " World!");
+    list = ucx_dlist_prepend(list, "Hello");
+    
+    UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
+    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
+    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
+    
+    ucx_dlist_free(list);
+    
+    UCX_TEST_END
+}
+
+UCX_TEST_BEGIN(test_ucx_dlist_equals) {
+    UcxDlist *list = ucx_dlist_append(NULL, "Hello");
+    list = ucx_dlist_append(list, " World!");
+    UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!");
+    list2 = ucx_dlist_prepend(list2, "Hello");
+    UcxDlist *list3 = ucx_dlist_prepend(NULL, " Welt!");
+    list3 = ucx_dlist_prepend(list3, "Hallo");
+    
+    UCX_TEST_ASSERT(ucx_dlist_equals(list, list2, cmp_string, NULL), "failed")
+    UCX_TEST_ASSERT(!ucx_dlist_equals(list, list3, cmp_string, NULL), "failed")
+    
+    ucx_dlist_free(list3);
+    ucx_dlist_free(list2);
+    ucx_dlist_free(list);
+    
+    UCX_TEST_END
+}
+
+UCX_TEST_BEGIN(test_ucx_dlist_concat) {
+    UcxDlist *list = ucx_dlist_append(NULL, "Hello");
+    UcxDlist *list2 = ucx_dlist_prepend(NULL, " World!");
+    
+    list = ucx_dlist_concat(list, list2);
+    
+    UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
+    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
+    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
+    
+    ucx_dlist_free(list2);
+    ucx_dlist_free(list);
+    
+    UCX_TEST_END
+}
+
+UCX_TEST_BEGIN(test_ucx_dlist_size) {
+    UcxDlist *list = ucx_dlist_append(NULL, "This ");
+    list = ucx_dlist_append(list, "list ");
+    list = ucx_dlist_append(list, "has ");
+    list = ucx_dlist_append(list, "size ");
+    list = ucx_dlist_append(list, "5!");
+    
+    UCX_TEST_ASSERT(ucx_dlist_size(list) == 5, "failed");
+    
+    ucx_dlist_free(list);
+    
+    UCX_TEST_END
+}
+
+UCX_TEST_BEGIN(test_ucx_dlist_first) {
+    UcxDlist *list = ucx_dlist_append(NULL, "Find ");
+    list = ucx_dlist_append(list, "the ");
+    list = ucx_dlist_append(list, "first!");
+    
+    char* first = (char*) (ucx_dlist_first(list)->data);
+    
+    UCX_TEST_ASSERT(strncmp(first, "Find ", 5) == 0, "failed");
+    
+    ucx_dlist_free(list);
+    
+    UCX_TEST_END
+}
+
+UCX_TEST_BEGIN(test_ucx_dlist_last) {
+    UcxDlist *list = ucx_dlist_append(NULL, "Find ");
+    list = ucx_dlist_append(list, "the ");
+    list = ucx_dlist_append(list, "last!");
+    
+    char* last = (char*) (ucx_dlist_last(list)->data);
+    
+    UCX_TEST_ASSERT(strncmp(last, "last!", 5) == 0, "failed");
+    
+    ucx_dlist_free(list);
+    
+    UCX_TEST_END
+}
+
+UCX_TEST_BEGIN(test_ucx_dlist_get) {
+    UcxDlist *list = ucx_dlist_append(NULL, "Find ");
+    list = ucx_dlist_append(list, "the ");
+    list = ucx_dlist_append(list, "mid!");
+    
+    char* mid = (char*) (ucx_dlist_get(list, 1)->data);
+    
+    UCX_TEST_ASSERT(strncmp(mid, "the ", 4) == 0, "failed");
+    
+    ucx_dlist_free(list);
+    
+    UCX_TEST_END
+}
+
+UCX_TEST_BEGIN(test_ucx_dlist_remove) {
+    UcxDlist *list = ucx_dlist_append(NULL, "Hello");
+    list = ucx_dlist_append(list, " fucking");
+    list = ucx_dlist_append(list, " World!");
+    
+    list = ucx_dlist_remove(list, ucx_dlist_get(list, 1));
+    
+    UCX_TEST_ASSERT(strncmp(list->data, "Hello", 5) == 0, "failed")
+    UCX_TEST_ASSERT(strncmp(list->next->data, " World!", 7) == 0, "failed")
+    UCX_TEST_ASSERT(list->next->next == NULL, "failed")
+    
+    ucx_dlist_free(list);
+    
+    UCX_TEST_END
+}
+
+UCX_TEST_BEGIN(test_ucx_dlist_clone) {
+   
+    char *hello = (char*)malloc(6);
+    char *world = (char*)malloc(8);
+    
+    memcpy(hello, "Hello", 6);
+    memcpy(world, " World!", 8);
+    
+    UcxDlist *list = ucx_dlist_append(NULL, hello);
+    list = ucx_dlist_append(list, world);
+    
+    UcxDlist *copy = ucx_dlist_clone(list, copy_string, NULL);
+
+    UCX_TEST_ASSERT(ucx_dlist_equals(list, copy, cmp_string, NULL), "failed")
+    UCX_TEST_ASSERT(hello != copy->data, "first element is no copy")
+    UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy")
+
+    free(copy->next->data);
+    free(copy->data);
+
+    free(world);
+    free(hello);
+    free(list);
+    free(copy);
+    
+    UCX_TEST_END
+}

mercurial