util: add a list_append()

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2018-05-23 15:21:56 +10:00
parent c81809d0aa
commit e05fa8444a
3 changed files with 79 additions and 0 deletions

View file

@ -61,6 +61,20 @@ list_insert(struct list *list, struct list *elm)
elm->next->prev = elm;
}
void
list_append(struct list *list, struct list *elm)
{
assert((list->next != NULL && list->prev != NULL) ||
!"list->next|prev is NULL, possibly missing list_init()");
assert(((elm->next == NULL && elm->prev == NULL) || list_empty(elm)) ||
!"elm->next|prev is not NULL, list node used twice?");
elm->next = list;
elm->prev = list->prev;
list->prev = elm;
elm->prev->next = elm;
}
void
list_remove(struct list *elm)
{

View file

@ -86,6 +86,7 @@ struct list {
void list_init(struct list *list);
void list_insert(struct list *list, struct list *elm);
void list_append(struct list *list, struct list *elm);
void list_remove(struct list *elm);
bool list_empty(const struct list *list);

View file

@ -1580,6 +1580,67 @@ START_TEST(timer_flush)
}
END_TEST
START_TEST(list_test_insert)
{
struct list_test {
int val;
struct list node;
} tests[] = {
{ .val = 1 },
{ .val = 2 },
{ .val = 3 },
{ .val = 4 },
};
struct list_test *t;
struct list head;
int val;
list_init(&head);
ARRAY_FOR_EACH(tests, t) {
list_insert(&head, &t->node);
}
val = 4;
list_for_each(t, &head, node) {
ck_assert_int_eq(t->val, val);
val--;
}
ck_assert_int_eq(val, 0);
}
END_TEST
START_TEST(list_test_append)
{
struct list_test {
int val;
struct list node;
} tests[] = {
{ .val = 1 },
{ .val = 2 },
{ .val = 3 },
{ .val = 4 },
};
struct list_test *t;
struct list head;
int val;
list_init(&head);
ARRAY_FOR_EACH(tests, t) {
list_append(&head, &t->node);
}
val = 1;
list_for_each(t, &head, node) {
ck_assert_int_eq(t->val, val);
val++;
}
ck_assert_int_eq(val, 5);
}
END_TEST
TEST_COLLECTION(misc)
{
litest_add_no_device("events:conversion", event_conversion_device_notify);
@ -1623,4 +1684,7 @@ TEST_COLLECTION(misc)
litest_add_no_device("misc:fd", fd_no_event_leak);
litest_add_no_device("misc:library_version", library_version);
litest_add_no_device("misc:list", list_test_insert);
litest_add_no_device("misc:list", list_test_append);
}