diff options
-rw-r--r-- | common/linked_list.c | 8 | ||||
-rw-r--r-- | libseat/backend/seatd.c | 1 | ||||
-rw-r--r-- | tests/linked_list.c | 28 |
3 files changed, 32 insertions, 5 deletions
diff --git a/common/linked_list.c b/common/linked_list.c index 3d2ee7d..7383de0 100644 --- a/common/linked_list.c +++ b/common/linked_list.c @@ -35,14 +35,12 @@ bool linked_list_empty(struct linked_list *list) { void linked_list_take(struct linked_list *target, struct linked_list *source) { if (linked_list_empty(source)) { - linked_list_init(target); return; } + source->next->prev = target; + source->prev->next = target->next; + target->next->prev = source->prev; target->next = source->next; - target->prev = source->prev; - target->next->prev = target; - target->prev->next = target; - linked_list_init(source); } diff --git a/libseat/backend/seatd.c b/libseat/backend/seatd.c index 46e354d..400b357 100644 --- a/libseat/backend/seatd.c +++ b/libseat/backend/seatd.c @@ -205,6 +205,7 @@ static int queue_event(struct backend_seatd *backend, int opcode) { static void execute_events(struct backend_seatd *backend) { struct linked_list list; + linked_list_init(&list); linked_list_take(&list, &backend->pending_events); while (!linked_list_empty(&list)) { struct pending_event *ev = (struct pending_event *)list.next; diff --git a/tests/linked_list.c b/tests/linked_list.c index a29cff6..71322b8 100644 --- a/tests/linked_list.c +++ b/tests/linked_list.c @@ -204,6 +204,7 @@ static void test_linked_list_loop_iterate(void) { static void test_linked_list_take_empty(void) { struct linked_list list1, list2; linked_list_init(&list1); + linked_list_init(&list2); linked_list_take(&list2, &list1); @@ -214,6 +215,7 @@ static void test_linked_list_take_empty(void) { static void test_linked_list_take_single(void) { struct linked_list list1, list2; linked_list_init(&list1); + linked_list_init(&list2); struct list_elem elem1 = {{0}, NULL}; linked_list_insert(&list1, &elem1.link); @@ -228,6 +230,7 @@ static void test_linked_list_take_single(void) { static void test_linked_list_take_many(void) { struct linked_list list1, list2; linked_list_init(&list1); + linked_list_init(&list2); struct list_elem elem1 = {{0}, NULL}; struct list_elem elem2 = {{0}, NULL}; @@ -242,6 +245,30 @@ static void test_linked_list_take_many(void) { assert(elem2.link.next == &list2 && elem2.link.prev == &elem1.link); } +static void test_linked_list_take_concat(void) { + struct linked_list list1, list2; + linked_list_init(&list1); + linked_list_init(&list2); + + struct list_elem elem1 = {{0}, NULL}; + struct list_elem elem2 = {{0}, NULL}; + struct list_elem elem3 = {{0}, NULL}; + struct list_elem elem4 = {{0}, NULL}; + linked_list_insert(&list1, &elem2.link); + linked_list_insert(&list1, &elem1.link); + linked_list_insert(&list2, &elem4.link); + linked_list_insert(&list2, &elem3.link); + + linked_list_take(&list2, &list1); + + assert(linked_list_empty(&list1)); + assert(list2.next == &elem1.link && list2.prev == &elem4.link); + assert(elem1.link.next == &elem2.link && elem1.link.prev == &list2); + assert(elem2.link.next == &elem3.link && elem2.link.prev == &elem1.link); + assert(elem3.link.next == &elem4.link && elem3.link.prev == &elem2.link); + assert(elem4.link.next == &list2 && elem4.link.prev == &elem3.link); +} + int main(int argc, char *argv[]) { (void)argc; (void)argv; @@ -258,6 +285,7 @@ int main(int argc, char *argv[]) { test_linked_list_take_empty(); test_linked_list_take_single(); test_linked_list_take_many(); + test_linked_list_take_concat(); return 0; } |