summaryrefslogtreecommitdiffstats
path: root/src/test/test-list.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 20:49:52 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 20:49:52 +0000
commit55944e5e40b1be2afc4855d8d2baf4b73d1876b5 (patch)
tree33f869f55a1b149e9b7c2b7e201867ca5dd52992 /src/test/test-list.c
parentInitial commit. (diff)
downloadsystemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.tar.xz
systemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.zip
Adding upstream version 255.4.upstream/255.4
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/test-list.c')
-rw-r--r--src/test/test-list.c286
1 files changed, 286 insertions, 0 deletions
diff --git a/src/test/test-list.c b/src/test/test-list.c
new file mode 100644
index 0000000..87f7c7b
--- /dev/null
+++ b/src/test/test-list.c
@@ -0,0 +1,286 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "alloc-util.h"
+#include "list.h"
+#include "tests.h"
+
+int main(int argc, const char *argv[]) {
+ test_setup_logging(LOG_DEBUG);
+
+ size_t i;
+ typedef struct list_item {
+ LIST_FIELDS(struct list_item, item_list);
+ } list_item;
+ LIST_HEAD(list_item, head);
+ LIST_HEAD(list_item, head2);
+ list_item items[4];
+
+ LIST_HEAD_INIT(head);
+ LIST_HEAD_INIT(head2);
+ assert_se(head == NULL);
+ assert_se(head2 == NULL);
+
+ for (i = 0; i < ELEMENTSOF(items); i++) {
+ LIST_INIT(item_list, &items[i]);
+ assert_se(LIST_JUST_US(item_list, &items[i]));
+ assert_se(LIST_PREPEND(item_list, head, &items[i]) == &items[i]);
+ }
+
+ i = 0;
+ LIST_FOREACH_OTHERS(item_list, cursor, &items[2]) {
+ i++;
+ assert_se(cursor != &items[2]);
+ }
+ assert_se(i == ELEMENTSOF(items)-1);
+
+ i = 0;
+ LIST_FOREACH_OTHERS(item_list, cursor, &items[0]) {
+ i++;
+ assert_se(cursor != &items[0]);
+ }
+ assert_se(i == ELEMENTSOF(items)-1);
+
+ i = 0;
+ LIST_FOREACH_OTHERS(item_list, cursor, &items[3]) {
+ i++;
+ assert_se(cursor != &items[3]);
+ }
+ assert_se(i == ELEMENTSOF(items)-1);
+
+ assert_se(!LIST_JUST_US(item_list, head));
+
+ assert_se(items[0].item_list_next == NULL);
+ assert_se(items[1].item_list_next == &items[0]);
+ assert_se(items[2].item_list_next == &items[1]);
+ assert_se(items[3].item_list_next == &items[2]);
+
+ assert_se(items[0].item_list_prev == &items[1]);
+ assert_se(items[1].item_list_prev == &items[2]);
+ assert_se(items[2].item_list_prev == &items[3]);
+ assert_se(items[3].item_list_prev == NULL);
+
+ list_item *cursor = LIST_FIND_HEAD(item_list, &items[0]);
+ assert_se(cursor == &items[3]);
+
+ cursor = LIST_FIND_TAIL(item_list, &items[3]);
+ assert_se(cursor == &items[0]);
+
+ assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]);
+ assert_se(LIST_JUST_US(item_list, &items[1]));
+
+ assert_se(items[0].item_list_next == NULL);
+ assert_se(items[2].item_list_next == &items[0]);
+ assert_se(items[3].item_list_next == &items[2]);
+
+ assert_se(items[0].item_list_prev == &items[2]);
+ assert_se(items[2].item_list_prev == &items[3]);
+ assert_se(items[3].item_list_prev == NULL);
+
+ assert_se(LIST_INSERT_AFTER(item_list, head, &items[3], &items[1]) == &items[1]);
+ assert_se(items[0].item_list_next == NULL);
+ assert_se(items[2].item_list_next == &items[0]);
+ assert_se(items[1].item_list_next == &items[2]);
+ assert_se(items[3].item_list_next == &items[1]);
+
+ assert_se(items[0].item_list_prev == &items[2]);
+ assert_se(items[2].item_list_prev == &items[1]);
+ assert_se(items[1].item_list_prev == &items[3]);
+ assert_se(items[3].item_list_prev == NULL);
+
+ assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]);
+ assert_se(LIST_JUST_US(item_list, &items[1]));
+
+ assert_se(items[0].item_list_next == NULL);
+ assert_se(items[2].item_list_next == &items[0]);
+ assert_se(items[3].item_list_next == &items[2]);
+
+ assert_se(items[0].item_list_prev == &items[2]);
+ assert_se(items[2].item_list_prev == &items[3]);
+ assert_se(items[3].item_list_prev == NULL);
+
+ assert_se(LIST_INSERT_BEFORE(item_list, head, &items[2], &items[1]) == &items[1]);
+ assert_se(items[0].item_list_next == NULL);
+ assert_se(items[2].item_list_next == &items[0]);
+ assert_se(items[1].item_list_next == &items[2]);
+ assert_se(items[3].item_list_next == &items[1]);
+
+ assert_se(items[0].item_list_prev == &items[2]);
+ assert_se(items[2].item_list_prev == &items[1]);
+ assert_se(items[1].item_list_prev == &items[3]);
+ assert_se(items[3].item_list_prev == NULL);
+
+ assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]);
+ assert_se(LIST_JUST_US(item_list, &items[0]));
+
+ assert_se(items[2].item_list_next == NULL);
+ assert_se(items[1].item_list_next == &items[2]);
+ assert_se(items[3].item_list_next == &items[1]);
+
+ assert_se(items[2].item_list_prev == &items[1]);
+ assert_se(items[1].item_list_prev == &items[3]);
+ assert_se(items[3].item_list_prev == NULL);
+
+ assert_se(LIST_INSERT_BEFORE(item_list, head, &items[3], &items[0]) == &items[0]);
+ assert_se(items[2].item_list_next == NULL);
+ assert_se(items[1].item_list_next == &items[2]);
+ assert_se(items[3].item_list_next == &items[1]);
+ assert_se(items[0].item_list_next == &items[3]);
+
+ assert_se(items[2].item_list_prev == &items[1]);
+ assert_se(items[1].item_list_prev == &items[3]);
+ assert_se(items[3].item_list_prev == &items[0]);
+ assert_se(items[0].item_list_prev == NULL);
+ assert_se(head == &items[0]);
+
+ assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]);
+ assert_se(LIST_JUST_US(item_list, &items[0]));
+
+ assert_se(items[2].item_list_next == NULL);
+ assert_se(items[1].item_list_next == &items[2]);
+ assert_se(items[3].item_list_next == &items[1]);
+
+ assert_se(items[2].item_list_prev == &items[1]);
+ assert_se(items[1].item_list_prev == &items[3]);
+ assert_se(items[3].item_list_prev == NULL);
+
+ assert_se(LIST_INSERT_BEFORE(item_list, head, NULL, &items[0]) == &items[0]);
+ assert_se(items[0].item_list_next == NULL);
+ assert_se(items[2].item_list_next == &items[0]);
+ assert_se(items[1].item_list_next == &items[2]);
+ assert_se(items[3].item_list_next == &items[1]);
+
+ assert_se(items[0].item_list_prev == &items[2]);
+ assert_se(items[2].item_list_prev == &items[1]);
+ assert_se(items[1].item_list_prev == &items[3]);
+ assert_se(items[3].item_list_prev == NULL);
+
+ assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]);
+ assert_se(LIST_JUST_US(item_list, &items[0]));
+
+ assert_se(items[2].item_list_next == NULL);
+ assert_se(items[1].item_list_next == &items[2]);
+ assert_se(items[3].item_list_next == &items[1]);
+
+ assert_se(items[2].item_list_prev == &items[1]);
+ assert_se(items[1].item_list_prev == &items[3]);
+ assert_se(items[3].item_list_prev == NULL);
+
+ assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]);
+ assert_se(LIST_JUST_US(item_list, &items[1]));
+
+ assert_se(items[2].item_list_next == NULL);
+ assert_se(items[3].item_list_next == &items[2]);
+
+ assert_se(items[2].item_list_prev == &items[3]);
+ assert_se(items[3].item_list_prev == NULL);
+
+ assert_se(LIST_REMOVE(item_list, head, &items[2]) == &items[2]);
+ assert_se(LIST_JUST_US(item_list, &items[2]));
+ assert_se(LIST_JUST_US(item_list, head));
+
+ assert_se(LIST_REMOVE(item_list, head, &items[3]) == &items[3]);
+ assert_se(LIST_JUST_US(item_list, &items[3]));
+
+ assert_se(head == NULL);
+
+ for (i = 0; i < ELEMENTSOF(items); i++) {
+ assert_se(LIST_JUST_US(item_list, &items[i]));
+ assert_se(LIST_APPEND(item_list, head, &items[i]) == &items[i]);
+ }
+
+ assert_se(!LIST_JUST_US(item_list, head));
+
+ assert_se(items[0].item_list_next == &items[1]);
+ assert_se(items[1].item_list_next == &items[2]);
+ assert_se(items[2].item_list_next == &items[3]);
+ assert_se(items[3].item_list_next == NULL);
+
+ assert_se(items[0].item_list_prev == NULL);
+ assert_se(items[1].item_list_prev == &items[0]);
+ assert_se(items[2].item_list_prev == &items[1]);
+ assert_se(items[3].item_list_prev == &items[2]);
+
+ for (i = 0; i < ELEMENTSOF(items); i++)
+ assert_se(LIST_REMOVE(item_list, head, &items[i]) == &items[i]);
+
+ assert_se(head == NULL);
+
+ for (i = 0; i < ELEMENTSOF(items) / 2; i++) {
+ LIST_INIT(item_list, &items[i]);
+ assert_se(LIST_JUST_US(item_list, &items[i]));
+ assert_se(LIST_PREPEND(item_list, head, &items[i]) == &items[i]);
+ }
+
+ for (i = ELEMENTSOF(items) / 2; i < ELEMENTSOF(items); i++) {
+ LIST_INIT(item_list, &items[i]);
+ assert_se(LIST_JUST_US(item_list, &items[i]));
+ assert_se(LIST_PREPEND(item_list, head2, &items[i]) == &items[i]);
+ }
+
+ assert_se(items[0].item_list_next == NULL);
+ assert_se(items[1].item_list_next == &items[0]);
+ assert_se(items[2].item_list_next == NULL);
+ assert_se(items[3].item_list_next == &items[2]);
+
+ assert_se(items[0].item_list_prev == &items[1]);
+ assert_se(items[1].item_list_prev == NULL);
+ assert_se(items[2].item_list_prev == &items[3]);
+ assert_se(items[3].item_list_prev == NULL);
+
+ assert_se(LIST_JOIN(item_list, head2, head) == head2);
+ assert_se(head == NULL);
+
+ assert_se(items[0].item_list_next == NULL);
+ assert_se(items[1].item_list_next == &items[0]);
+ assert_se(items[2].item_list_next == &items[1]);
+ assert_se(items[3].item_list_next == &items[2]);
+
+ assert_se(items[0].item_list_prev == &items[1]);
+ assert_se(items[1].item_list_prev == &items[2]);
+ assert_se(items[2].item_list_prev == &items[3]);
+ assert_se(items[3].item_list_prev == NULL);
+
+ assert_se(LIST_JOIN(item_list, head, head2) == head);
+ assert_se(head2 == NULL);
+ assert_se(head);
+
+ for (i = 0; i < ELEMENTSOF(items); i++)
+ assert_se(LIST_REMOVE(item_list, head, &items[i]) == &items[i]);
+
+ assert_se(head == NULL);
+
+ assert_se(LIST_PREPEND(item_list, head, items + 0) == items + 0);
+ assert_se(LIST_PREPEND(item_list, head, items + 1) == items + 1);
+ assert_se(LIST_PREPEND(item_list, head, items + 2) == items + 2);
+
+ assert_se(LIST_POP(item_list, head) == items + 2);
+ assert_se(LIST_POP(item_list, head) == items + 1);
+ assert_se(LIST_POP(item_list, head) == items + 0);
+ assert_se(LIST_POP(item_list, head) == NULL);
+
+ /* No-op on an empty list */
+
+ LIST_CLEAR(item_list, head, free);
+
+ /* A non-empty list is cleared */
+
+ assert_se(LIST_PREPEND(item_list, head, new0(list_item, 1)));
+ assert_se(LIST_PREPEND(item_list, head, new0(list_item, 1)));
+
+ LIST_CLEAR(item_list, head, free);
+
+ assert_se(head == NULL);
+
+ /* A list can be cleared partially */
+
+ assert_se(LIST_PREPEND(item_list, head, new0(list_item, 1)));
+ assert_se(LIST_PREPEND(item_list, head, new0(list_item, 1)));
+ assert_se(LIST_PREPEND(item_list, head, items + 0) == items + 0);
+
+ LIST_CLEAR(item_list, head->item_list_next, free);
+
+ assert_se(head == items + 0);
+ assert_se(head->item_list_next == NULL);
+
+ return 0;
+}