Sun, 14 Feb 2021 15:13:53 +0100
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 |
1.1 --- a/src/cx/linked_list.h Sun Feb 14 11:31:13 2021 +0100 1.2 +++ b/src/cx/linked_list.h Sun Feb 14 15:13:53 2021 +0100 1.3 @@ -38,17 +38,8 @@ 1.4 1.5 extern cx_list_class cx_linked_list_class; 1.6 1.7 -typedef struct { 1.8 - void *begin; 1.9 - ptrdiff_t loc_prev; 1.10 - ptrdiff_t loc_next; 1.11 - size_t item_size; 1.12 -} CxLinkedListDesc; 1.13 - 1.14 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size); 1.15 1.16 -CxList cxLinkedListWrap(CxAllocator allocator, CxListComparator comparator, CxLinkedListDesc desc); 1.17 - 1.18 void cxLinkedListDestroy(CxList list); 1.19 1.20 size_t cxLinkedListRecalculateSize(CxList list);
2.1 --- a/src/cx/list.h Sun Feb 14 11:31:13 2021 +0100 2.2 +++ b/src/cx/list.h Sun Feb 14 15:13:53 2021 +0100 2.3 @@ -32,7 +32,7 @@ 2.4 #include <stdlib.h> 2.5 #include "allocator.h" 2.6 2.7 -typedef int(*CxListComparator)(void *left, void *right); 2.8 +typedef int(*CxListComparator)(void const *left, void const *right); 2.9 2.10 typedef struct { 2.11 CxAllocator allocator; 2.12 @@ -51,12 +51,12 @@ 2.13 void *(*last)(cx_list *list); 2.14 } cx_list_class; 2.15 2.16 -struct cx_list_s { 2.17 +typedef struct { 2.18 cx_list_class *cl; 2.19 cx_list data; 2.20 -}; 2.21 +} cx_list_s; 2.22 2.23 -typedef struct cx_list_s *CxList; 2.24 +typedef cx_list_s *CxList; 2.25 2.26 int cxListAdd(CxList list, void *elem); 2.27
3.1 --- a/src/linked_list.c Sun Feb 14 11:31:13 2021 +0100 3.2 +++ b/src/linked_list.c Sun Feb 14 15:13:53 2021 +0100 3.3 @@ -156,30 +156,20 @@ 3.4 }; 3.5 3.6 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) { 3.7 - CxLinkedListDesc desc; 3.8 - desc.item_size = item_size; 3.9 - desc.begin = NULL; 3.10 - desc.loc_prev = offsetof(struct cx_linked_list_node, prev); 3.11 - desc.loc_next = offsetof(struct cx_linked_list_node, next); 3.12 - 3.13 - return cxLinkedListWrap(allocator, comparator, desc); 3.14 -} 3.15 - 3.16 -CxList cxLinkedListWrap(CxAllocator allocator, CxListComparator comparator, CxLinkedListDesc desc) { 3.17 - CxList list = cxMalloc(allocator, sizeof(list) + sizeof(cx_linked_list)); 3.18 + CxList list = cxMalloc(allocator, sizeof(cx_list_s) + sizeof(cx_linked_list)); 3.19 if (list == NULL) 3.20 return NULL; 3.21 3.22 list->cl = &cx_linked_list_class; 3.23 list->data.allocator = allocator; 3.24 list->data.cmpfunc = comparator; 3.25 - list->data.itemsize = desc.item_size; 3.26 + list->data.itemsize = item_size; 3.27 list->data.capacity = SIZE_MAX; 3.28 3.29 cx_linked_list *ll = (cx_linked_list *) list->data.listdata; 3.30 - ll->begin = desc.begin; 3.31 - ll->loc_prev = desc.loc_prev; 3.32 - ll->loc_next = desc.loc_next; 3.33 + ll->begin = NULL; 3.34 + ll->loc_prev = offsetof(struct cx_linked_list_node, prev); 3.35 + ll->loc_next = offsetof(struct cx_linked_list_node, next); 3.36 cxLinkedListRecalculateSize(list); 3.37 3.38 return list;
4.1 --- a/test/test_linked_list.c Sun Feb 14 11:31:13 2021 +0100 4.2 +++ b/test/test_linked_list.c Sun Feb 14 15:13:53 2021 +0100 4.3 @@ -29,8 +29,31 @@ 4.4 #include "cx/linked_list.h" 4.5 #include "test_config.h" 4.6 4.7 -void test_linked_list_wrap() { 4.8 - CU_FAIL("test not implemented") 4.9 +int cmp_int(int const *l, int const *r) { 4.10 + int left = *l, right = *r; 4.11 + return left == right ? 0 : (left < right ? -1 : 1); 4.12 +} 4.13 + 4.14 +void test_linked_list_create() { 4.15 + CxList list = cxLinkedListCreate(cxDefaultAllocator, (CxListComparator) cmp_int, sizeof(int)); 4.16 + 4.17 + CU_ASSERT_EQUAL(list->data.size, 0) 4.18 + CU_ASSERT_EQUAL(list->data.capacity, (size_t) -1) 4.19 + CU_ASSERT_PTR_EQUAL(list->data.allocator, cxDefaultAllocator) 4.20 + CU_ASSERT_EQUAL(list->data.itemsize, sizeof(int)) 4.21 + CU_ASSERT_PTR_EQUAL(list->data.cmpfunc, cmp_int) 4.22 + 4.23 + struct node { 4.24 + void* begin; void* end; ptrdiff_t ploc; ptrdiff_t nloc; 4.25 + }; 4.26 + 4.27 + struct node* actual = (struct node*) list->data.listdata; 4.28 + CU_ASSERT_PTR_NULL(actual->begin) 4.29 + CU_ASSERT_PTR_NULL(actual->end) 4.30 + CU_ASSERT_EQUAL(0, actual->ploc) 4.31 + CU_ASSERT_EQUAL(sizeof(void*), actual->nloc) 4.32 + 4.33 + cxLinkedListDestroy(list); 4.34 } 4.35 4.36 int main() { 4.37 @@ -40,14 +63,14 @@ 4.38 return CU_get_error(); 4.39 } 4.40 4.41 - suite = CU_add_suite("linked list creation", NULL, NULL); 4.42 + suite = CU_add_suite("linked list memory management", NULL, NULL); 4.43 if (NULL == suite) { 4.44 CU_cleanup_registry(); 4.45 return CU_get_error(); 4.46 } 4.47 4.48 if ( 4.49 - !CU_add_test(suite, "wrapping of custom linked list", test_linked_list_wrap) 4.50 + !CU_add_test(suite, "create linked list", test_linked_list_create) 4.51 ) { 4.52 CU_cleanup_registry(); 4.53 return CU_get_error();