diff -r 18f964adad1b -r 1bd4b8c28722 test/test_list.c --- a/test/test_list.c Fri Oct 08 18:58:49 2021 +0200 +++ b/test/test_list.c Fri Oct 08 19:47:31 2021 +0200 @@ -128,7 +128,6 @@ } void test_linked_list_last(void) { - CU_ASSERT_PTR_NULL(cx_linked_list_last(NULL, -1)) CU_ASSERT_PTR_NULL(cx_linked_list_last(NULL, 0)) struct node { @@ -146,6 +145,97 @@ CU_ASSERT_PTR_EQUAL(cx_linked_list_last(&third, loc), &third) } +void test_linked_list_prev(void) { + struct node { + void *next; + }; + ptrdiff_t loc = offsetof(struct node, next); + + struct node third = {NULL}; + struct node second = {&third}; + struct node first = {&second}; + + CU_ASSERT_PTR_NULL(cx_linked_list_prev(&first, loc, &first)) + CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(&first, loc, &second), &first) + CU_ASSERT_PTR_EQUAL(cx_linked_list_prev(&first, loc, &third), &second) +} + +void test_linked_list_remove(void) { + struct node { + void *next; + }; + struct dnode { + void *next; + void *prev; + }; + ptrdiff_t loc = offsetof(struct node, next); + ptrdiff_t ploc = offsetof(struct dnode, prev); + + void *begin; + void *end; + void *result; + + // single linked list + struct node third = {NULL}; + struct node second = {&third}; + struct node first = {&second}; + begin = &first; + + result = cx_linked_list_remove(&begin, NULL, -1, loc, &second); + CU_ASSERT_PTR_EQUAL(result, &first) + CU_ASSERT_PTR_EQUAL(begin, &first) + CU_ASSERT_PTR_EQUAL(first.next, &third) + CU_ASSERT_PTR_NULL(second.next) + CU_ASSERT_PTR_NULL(third.next) + + result = cx_linked_list_remove(&begin, NULL, -1, loc, &first); + CU_ASSERT_PTR_EQUAL(result, &third) + CU_ASSERT_PTR_EQUAL(begin, &third) + CU_ASSERT_PTR_NULL(first.next) + CU_ASSERT_PTR_NULL(third.next) + + result = cx_linked_list_remove(&begin, NULL, -1, loc, &third); + CU_ASSERT_PTR_NULL(result) + CU_ASSERT_PTR_NULL(begin) + CU_ASSERT_PTR_NULL(third.next) + + // doubly linked list + struct dnode dthird = {NULL , NULL}; + struct dnode dsecond = {&dthird, NULL}; + struct dnode dfirst = {&dsecond, NULL}; + dthird.prev = &dsecond; + dsecond.prev = &dfirst; + begin = &dfirst; + end = &dthird; + + result = cx_linked_list_remove(&begin, &end, ploc, loc, &dsecond); + CU_ASSERT_PTR_EQUAL(result, &dfirst) + CU_ASSERT_PTR_EQUAL(begin, &dfirst) + CU_ASSERT_PTR_EQUAL(end, &dthird) + CU_ASSERT_PTR_NULL(dfirst.prev) + CU_ASSERT_PTR_EQUAL(dfirst.next, &dthird) + CU_ASSERT_PTR_NULL(dsecond.prev) + CU_ASSERT_PTR_NULL(dsecond.next) + CU_ASSERT_PTR_EQUAL(dthird.prev, &dfirst) + CU_ASSERT_PTR_NULL(dthird.next) + + result = cx_linked_list_remove(&begin, &end, ploc, loc, &dthird); + CU_ASSERT_PTR_EQUAL(result, &dfirst) + CU_ASSERT_PTR_EQUAL(begin, &dfirst) + CU_ASSERT_PTR_EQUAL(end, &dfirst) + CU_ASSERT_PTR_NULL(dfirst.prev) + CU_ASSERT_PTR_NULL(dfirst.next) + CU_ASSERT_PTR_NULL(dthird.prev) + CU_ASSERT_PTR_NULL(dthird.next) + + result = cx_linked_list_remove(&begin, &end, ploc, loc, &dfirst); + CU_ASSERT_PTR_NULL(result) + CU_ASSERT_PTR_NULL(begin) + CU_ASSERT_PTR_NULL(end) + CU_ASSERT_PTR_NULL(dfirst.next) + CU_ASSERT_PTR_NULL(dfirst.prev) +} + void test_linked_list_size(void) { struct node { void *next; @@ -209,7 +299,7 @@ CU_ASSERT_EQUAL(begin->data, expected[0]) struct node *check = begin; struct node *check_last = NULL; - for (int i = 0 ; i < 100 ; i++) { + for (int i = 0; i < 100; i++) { CU_ASSERT_EQUAL(check->data, expected[i]) CU_ASSERT_PTR_EQUAL(check->prev, check_last) if (i < 99) { @@ -222,6 +312,51 @@ CU_ASSERT_EQUAL(end->data, expected[99]) } +void test_linked_list_reverse(void) { + struct node { + void *next; + }; + struct dnode { + void *next; + void *prev; + }; + ptrdiff_t loc = offsetof(struct node, next); + ptrdiff_t ploc = offsetof(struct dnode, prev); + + void *begin; + void *end; + + // single linked list + struct node third = {NULL}; + struct node second = {&third}; + struct node first = {&second}; + begin = &first; + + cx_linked_list_reverse(&begin, NULL, -1, loc); + CU_ASSERT_PTR_EQUAL(begin, &third) + CU_ASSERT_PTR_EQUAL(third.next, &second) + CU_ASSERT_PTR_EQUAL(second.next, &first) + CU_ASSERT_PTR_NULL(first.next) + + // doubly linked list + struct dnode dthird = {NULL , NULL}; + struct dnode dsecond = {&dthird, NULL}; + struct dnode dfirst = {&dsecond, NULL}; + dthird.prev = &dsecond; + dsecond.prev = &dfirst; + begin = &dfirst; + end = &dthird; + + cx_linked_list_reverse(&begin, &end, ploc, loc); + CU_ASSERT_PTR_EQUAL(begin, &dthird) + CU_ASSERT_PTR_EQUAL(end, &dfirst) + CU_ASSERT_PTR_EQUAL(dthird.next, &dsecond) + CU_ASSERT_PTR_EQUAL(dsecond.next, &dfirst) + CU_ASSERT_PTR_NULL(dfirst.next) + CU_ASSERT_PTR_NULL(dthird.prev) + CU_ASSERT_PTR_EQUAL(dsecond.prev, &dthird) + CU_ASSERT_PTR_EQUAL(dfirst.prev, &dsecond) +} void test_hl_linked_list_create(void) { cxTestingAllocatorReset(); @@ -415,14 +550,14 @@ CxList list = cxLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int, sizeof(int)); - for (int i = 0 ; i < 100 ; i++) { + for (int i = 0; i < 100; i++) { cxListAdd(list, &scrambled[i]); } cxListSort(list); - for (int i = 0 ; i < 100 ; i++) { - CU_ASSERT_EQUAL(*(int*)cxListAt(list, i), expected[i]) + for (int i = 0; i < 100; i++) { + CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i]) } cxLinkedListDestroy(list); @@ -616,14 +751,14 @@ CxList list = cxPointerLinkedListCreate(cxTestingAllocator, (CxListComparator) cmp_int); - for (int i = 0 ; i < 100 ; i++) { + for (int i = 0; i < 100; i++) { cxListAdd(list, &scrambled[i]); } cxListSort(list); - for (int i = 0 ; i < 100 ; i++) { - CU_ASSERT_EQUAL(*(int*)cxListAt(list, i), expected[i]) + for (int i = 0; i < 100; i++) { + CU_ASSERT_EQUAL(*(int *) cxListAt(list, i), expected[i]) } cxLinkedListDestroy(list); @@ -642,8 +777,11 @@ cu_add_test(suite, test_linked_list_at); cu_add_test(suite, test_linked_list_add); cu_add_test(suite, test_linked_list_last); + cu_add_test(suite, test_linked_list_prev); + cu_add_test(suite, test_linked_list_remove); cu_add_test(suite, test_linked_list_size); cu_add_test(suite, test_linked_list_sort); + cu_add_test(suite, test_linked_list_reverse); suite = CU_add_suite("high level linked list", NULL, NULL);