Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/cfl/cfl_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
56 changes: 56 additions & 0 deletions tests/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
};

Loading