diff --git a/include/cfl/cfl_list.h b/include/cfl/cfl_list.h index 47d6ea1..43b9a2f 100644 --- a/include/cfl/cfl_list.h +++ b/include/cfl/cfl_list.h @@ -140,7 +140,7 @@ static inline void cfl_list_add_after(struct cfl_list *_new, } next = prev->next; - next->prev = prev; + next->prev = _new; _new->next = next; _new->prev = prev; prev->next = _new; diff --git a/tests/list.c b/tests/list.c index 9717be4..d45ce1a 100644 --- a/tests/list.c +++ b/tests/list.c @@ -91,9 +91,65 @@ static void add() free(nodes); } +static void add_after() +{ + int i; + int expected[] = {0, 1, 2, 3, 4}; + struct cfl_list list; + struct cfl_list *head; + struct cfl_list *tmp; + struct node { + int value; + struct cfl_list _head; + }; + struct node nodes[5]; + struct node *node; + + for (i = 0; i < 5; i++) { + nodes[i].value = i; + } + + cfl_list_init(&list); + cfl_list_add(&nodes[0]._head, &list); + cfl_list_add(&nodes[2]._head, &list); + + /* insert in the middle and after the tail */ + cfl_list_add_after(&nodes[1]._head, &nodes[0]._head, &list); + cfl_list_add_after(&nodes[3]._head, &nodes[2]._head, &list); + + /* the next entry must point back to the inserted one */ + TEST_CHECK(nodes[2]._head.prev == &nodes[1]._head); + TEST_CHECK(list.prev == &nodes[3]._head); + + /* a tail insert must keep the entry added after the old tail */ + cfl_list_add(&nodes[4]._head, &list); + + i = 0; + cfl_list_foreach(head, &list) { + node = cfl_list_entry(head, struct node, _head); + TEST_CHECK(i < 5 && node->value == expected[i]); + i++; + } + TEST_CHECK(i == 5); + + i = 4; + for (head = list.prev; head != &list; head = head->prev) { + node = cfl_list_entry(head, struct node, _head); + TEST_CHECK(i >= 0 && node->value == expected[i]); + i--; + } + TEST_CHECK(i == -1); + + cfl_list_foreach_safe(head, tmp, &list) { + cfl_list_del(head); + } + TEST_CHECK(cfl_list_is_empty(&list)); +} + TEST_LIST = { {"checks", checks}, {"add", add}, + {"add_after", add_after}, { 0 } };