[PATCH v2 libinput 03/14] util: add a list_append()

Peter Hutterer peter.hutterer at who-t.net
Fri Jun 8 06:00:10 UTC 2018


Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 src/libinput-util.c | 14 ++++++++++++
 src/libinput-util.h |  1 +
 test/test-misc.c    | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 79 insertions(+)

diff --git a/src/libinput-util.c b/src/libinput-util.c
index eb6e3ecf..a475c465 100644
--- a/src/libinput-util.c
+++ b/src/libinput-util.c
@@ -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)
 {
diff --git a/src/libinput-util.h b/src/libinput-util.h
index 92e7cf73..6524d940 100644
--- a/src/libinput-util.h
+++ b/src/libinput-util.h
@@ -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);
 
diff --git a/test/test-misc.c b/test/test-misc.c
index ea262f65..779baf31 100644
--- a/test/test-misc.c
+++ b/test/test-misc.c
@@ -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);
 }
-- 
2.14.4



More information about the wayland-devel mailing list