removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate

Sun, 14 Feb 2021 15:13:53 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 14 Feb 2021 15:13:53 +0100
changeset 412
af766caea48d
parent 411
2842f729caab
child 413
0f4aa9fc75d9

removes stupid high level wrapper for linked lists + adds test for cxLinkedListCreate

src/cx/linked_list.h file | annotate | diff | comparison | revisions
src/cx/list.h file | annotate | diff | comparison | revisions
src/linked_list.c file | annotate | diff | comparison | revisions
test/test_linked_list.c file | annotate | diff | comparison | revisions
--- a/src/cx/linked_list.h	Sun Feb 14 11:31:13 2021 +0100
+++ b/src/cx/linked_list.h	Sun Feb 14 15:13:53 2021 +0100
@@ -38,17 +38,8 @@
 
 extern cx_list_class cx_linked_list_class;
 
-typedef struct {
-    void *begin;
-    ptrdiff_t loc_prev;
-    ptrdiff_t loc_next;
-    size_t item_size;
-} CxLinkedListDesc;
-
 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size);
 
-CxList cxLinkedListWrap(CxAllocator allocator, CxListComparator comparator, CxLinkedListDesc desc);
-
 void cxLinkedListDestroy(CxList list);
 
 size_t cxLinkedListRecalculateSize(CxList list);
--- a/src/cx/list.h	Sun Feb 14 11:31:13 2021 +0100
+++ b/src/cx/list.h	Sun Feb 14 15:13:53 2021 +0100
@@ -32,7 +32,7 @@
 #include <stdlib.h>
 #include "allocator.h"
 
-typedef int(*CxListComparator)(void *left, void *right);
+typedef int(*CxListComparator)(void const *left, void const *right);
 
 typedef struct {
     CxAllocator allocator;
@@ -51,12 +51,12 @@
     void *(*last)(cx_list *list);
 } cx_list_class;
 
-struct cx_list_s {
+typedef struct {
     cx_list_class *cl;
     cx_list data;
-};
+} cx_list_s;
 
-typedef struct cx_list_s *CxList;
+typedef cx_list_s *CxList;
 
 int cxListAdd(CxList list, void *elem);
 
--- a/src/linked_list.c	Sun Feb 14 11:31:13 2021 +0100
+++ b/src/linked_list.c	Sun Feb 14 15:13:53 2021 +0100
@@ -156,30 +156,20 @@
 };
 
 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
-    CxLinkedListDesc desc;
-    desc.item_size = item_size;
-    desc.begin = NULL;
-    desc.loc_prev = offsetof(struct cx_linked_list_node, prev);
-    desc.loc_next = offsetof(struct cx_linked_list_node, next);
-
-    return cxLinkedListWrap(allocator, comparator, desc);
-}
-
-CxList cxLinkedListWrap(CxAllocator allocator, CxListComparator comparator, CxLinkedListDesc desc) {
-    CxList list = cxMalloc(allocator, sizeof(list) + sizeof(cx_linked_list));
+    CxList list = cxMalloc(allocator, sizeof(cx_list_s) + sizeof(cx_linked_list));
     if (list == NULL)
         return NULL;
 
     list->cl = &cx_linked_list_class;
     list->data.allocator = allocator;
     list->data.cmpfunc = comparator;
-    list->data.itemsize = desc.item_size;
+    list->data.itemsize = item_size;
     list->data.capacity = SIZE_MAX;
 
     cx_linked_list *ll = (cx_linked_list *) list->data.listdata;
-    ll->begin = desc.begin;
-    ll->loc_prev = desc.loc_prev;
-    ll->loc_next = desc.loc_next;
+    ll->begin = NULL;
+    ll->loc_prev = offsetof(struct cx_linked_list_node, prev);
+    ll->loc_next = offsetof(struct cx_linked_list_node, next);
     cxLinkedListRecalculateSize(list);
 
     return list;
--- a/test/test_linked_list.c	Sun Feb 14 11:31:13 2021 +0100
+++ b/test/test_linked_list.c	Sun Feb 14 15:13:53 2021 +0100
@@ -29,8 +29,31 @@
 #include "cx/linked_list.h"
 #include "test_config.h"
 
-void test_linked_list_wrap() {
-    CU_FAIL("test not implemented")
+int cmp_int(int const *l, int const *r) {
+    int left = *l, right = *r;
+    return left == right ? 0 : (left < right ? -1 : 1);
+}
+
+void test_linked_list_create() {
+    CxList list = cxLinkedListCreate(cxDefaultAllocator, (CxListComparator) cmp_int, sizeof(int));
+
+    CU_ASSERT_EQUAL(list->data.size, 0)
+    CU_ASSERT_EQUAL(list->data.capacity, (size_t) -1)
+    CU_ASSERT_PTR_EQUAL(list->data.allocator, cxDefaultAllocator)
+    CU_ASSERT_EQUAL(list->data.itemsize, sizeof(int))
+    CU_ASSERT_PTR_EQUAL(list->data.cmpfunc, cmp_int)
+
+    struct node {
+        void* begin; void* end; ptrdiff_t ploc; ptrdiff_t nloc;
+    };
+
+    struct node* actual = (struct node*) list->data.listdata;
+    CU_ASSERT_PTR_NULL(actual->begin)
+    CU_ASSERT_PTR_NULL(actual->end)
+    CU_ASSERT_EQUAL(0, actual->ploc)
+    CU_ASSERT_EQUAL(sizeof(void*), actual->nloc)
+
+    cxLinkedListDestroy(list);
 }
 
 int main() {
@@ -40,14 +63,14 @@
         return CU_get_error();
     }
 
-    suite = CU_add_suite("linked list creation", NULL, NULL);
+    suite = CU_add_suite("linked list memory management", NULL, NULL);
     if (NULL == suite) {
         CU_cleanup_registry();
         return CU_get_error();
     }
 
     if (
-            !CU_add_test(suite, "wrapping of custom linked list", test_linked_list_wrap)
+            !CU_add_test(suite, "create linked list", test_linked_list_create)
             ) {
         CU_cleanup_registry();
         return CU_get_error();

mercurial