diff -r 9c1fccda16bc -r 31bf97fdbf71 test/test_list.c --- a/test/test_list.c Sat Oct 09 11:12:48 2021 +0200 +++ b/test/test_list.c Sat Dec 04 17:38:23 2021 +0100 @@ -88,8 +88,8 @@ cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]); CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) CU_ASSERT_PTR_EQUAL(end, &nodes[0]) - CU_ASSERT_PTR_EQUAL(nodes[0].prev, NULL) - CU_ASSERT_PTR_EQUAL(nodes[0].next, NULL) + CU_ASSERT_PTR_NULL(nodes[0].prev) + CU_ASSERT_PTR_NULL(nodes[0].next) cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]); CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) @@ -113,6 +113,23 @@ CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]) CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]) + // test with end only / prev, next + memset(nodes, 0, 4 * sizeof(struct node)); + begin = NULL; + end = NULL; + + cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[0]); + CU_ASSERT_PTR_EQUAL(end, &nodes[0]) + cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[1]); + CU_ASSERT_PTR_EQUAL(end, &nodes[1]) + CU_ASSERT_PTR_EQUAL(nodes[0].next, &nodes[1]) + CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[0]) + + cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[2]); + CU_ASSERT_PTR_EQUAL(end, &nodes[2]) + CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[2]) + CU_ASSERT_PTR_EQUAL(nodes[2].prev, &nodes[1]) + // test with begin, end / next memset(nodes, 0, 4 * sizeof(struct node)); begin = NULL; @@ -127,6 +144,104 @@ CU_ASSERT_PTR_NULL(nodes[1].prev) } +void test_linked_list_prepend(void) { + struct node { + void *prev; + void *next; + }; + + struct node nodes[4]; + + // test with begin, end / prev, next + memset(nodes, 0, 4 * sizeof(struct node)); + void *begin = NULL; + void *end = NULL; + + ptrdiff_t loc_prev = offsetof(struct node, prev); + ptrdiff_t loc_next = offsetof(struct node, next); + + cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[0]); + CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) + CU_ASSERT_PTR_EQUAL(end, &nodes[0]) + CU_ASSERT_PTR_NULL(nodes[0].prev) + CU_ASSERT_PTR_NULL(nodes[0].next) + + cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[1]); + CU_ASSERT_PTR_EQUAL(begin, &nodes[1]) + CU_ASSERT_PTR_EQUAL(end, &nodes[0]) + CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0]) + CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1]) + + // test with begin only / prev, next + memset(nodes, 0, 4 * sizeof(struct node)); + begin = NULL; + end = NULL; + + cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[0]); + CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) + cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[1]); + CU_ASSERT_PTR_EQUAL(begin, &nodes[1]) + CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0]) + CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1]) + + cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[2]); + CU_ASSERT_PTR_EQUAL(begin, &nodes[2]) + CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1]) + CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2]) + + // test with end only / prev, next + memset(nodes, 0, 4 * sizeof(struct node)); + begin = NULL; + end = NULL; + + cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[0]); + CU_ASSERT_PTR_EQUAL(end, &nodes[0]) + cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[1]); + CU_ASSERT_PTR_EQUAL(end, &nodes[0]) + CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0]) + CU_ASSERT_PTR_EQUAL(nodes[0].prev, &nodes[1]) + + cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[2]); + CU_ASSERT_PTR_EQUAL(end, &nodes[0]) + CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1]) + CU_ASSERT_PTR_EQUAL(nodes[1].prev, &nodes[2]) + + // test with begin, end / next + memset(nodes, 0, 4 * sizeof(struct node)); + begin = NULL; + end = NULL; + + cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[0]); + CU_ASSERT_PTR_EQUAL(begin, &nodes[0]) + CU_ASSERT_PTR_EQUAL(end, &nodes[0]) + cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[1]); + cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[2]); + CU_ASSERT_PTR_EQUAL(begin, &nodes[2]) + CU_ASSERT_PTR_EQUAL(end, &nodes[0]) + CU_ASSERT_PTR_EQUAL(nodes[1].next, &nodes[0]) + CU_ASSERT_PTR_EQUAL(nodes[2].next, &nodes[1]) + CU_ASSERT_PTR_NULL(nodes[1].prev) + CU_ASSERT_PTR_NULL(nodes[0].prev) +} + +void test_linked_list_first(void) { + CU_ASSERT_PTR_NULL(cx_linked_list_first(NULL, 0)) + + struct node { + int data; + void *prev; + }; + ptrdiff_t loc = offsetof(struct node, prev); + + struct node first = {1, NULL}; + struct node second = {2, &first}; + struct node third = {3, &second}; + + CU_ASSERT_PTR_EQUAL(cx_linked_list_first(&first, loc), &first) + CU_ASSERT_PTR_EQUAL(cx_linked_list_first(&second, loc), &first) + CU_ASSERT_PTR_EQUAL(cx_linked_list_first(&third, loc), &first) +} + void test_linked_list_last(void) { CU_ASSERT_PTR_NULL(cx_linked_list_last(NULL, 0)) @@ -738,7 +853,9 @@ suite = CU_add_suite("low level linked list", NULL, NULL); cu_add_test(suite, test_linked_list_at); + cu_add_test(suite, test_linked_list_prepend); cu_add_test(suite, test_linked_list_add); + cu_add_test(suite, test_linked_list_first); cu_add_test(suite, test_linked_list_last); cu_add_test(suite, test_linked_list_prev); cu_add_test(suite, test_linked_list_remove);