chiark / gitweb /
test: Add list testcase
authorJan Janssen <medhefgo@web.de>
Wed, 26 Jun 2013 11:43:16 +0000 (13:43 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 27 Jun 2013 05:38:47 +0000 (01:38 -0400)
.gitignore
Makefile.am
src/shared/list.h
src/test/test-list.c [new file with mode: 0644]

index d1e2ae991533371af4494bc0264bf59e1d4fb4be..866d8eba0b4fe3b5daa70df0a783444b23ef8eb2 100644 (file)
 /test-journal-syslog
 /test-journal-verify
 /test-libudev
 /test-journal-syslog
 /test-journal-verify
 /test-libudev
+/test-list
 /test-log
 /test-login
 /test-loopback
 /test-log
 /test-login
 /test-loopback
index 12254e39a8d64f30808ff7619f33685cba271e6e..c64934efa5cd61ae8ee215cb276a317e143ba8ab 100644 (file)
@@ -1098,7 +1098,8 @@ tests += \
        test-prioq \
        test-fileio \
        test-time \
        test-prioq \
        test-fileio \
        test-time \
-       test-hashmap
+       test-hashmap \
+       test-list
 
 EXTRA_DIST += \
        test/sched_idle_bad.service \
 
 EXTRA_DIST += \
        test/sched_idle_bad.service \
@@ -1202,6 +1203,15 @@ test_hashmap_CFLAGS = \
 test_hashmap_LDADD = \
        libsystemd-core.la
 
 test_hashmap_LDADD = \
        libsystemd-core.la
 
+test_list_SOURCES = \
+       src/test/test-list.c
+
+test_list_CFLAGS = \
+       $(AM_CFLAGS)
+
+test_list_LDADD = \
+       libsystemd-core.la
+
 test_prioq_SOURCES = \
        src/test/test-prioq.c
 
 test_prioq_SOURCES = \
        src/test/test-prioq.c
 
index 96d6237974726693512b71cbc12c12e9df7bcf4e..476757460aa6145c55ccc2873f016d70862582f0 100644 (file)
@@ -81,7 +81,7 @@
                 (head) = _item;                                         \
         } while (false)
 
                 (head) = _item;                                         \
         } while (false)
 
-/* Find the head of the list */
+/* Find the tail of the list */
 #define LIST_FIND_TAIL(t,name,item,tail)                                \
         do {                                                            \
                 t *_item = (item);                                      \
 #define LIST_FIND_TAIL(t,name,item,tail)                                \
         do {                                                            \
                 t *_item = (item);                                      \
diff --git a/src/test/test-list.c b/src/test/test-list.c
new file mode 100644 (file)
index 0000000..2710504
--- /dev/null
@@ -0,0 +1,109 @@
+/***
+  This file is part of systemd
+
+  Copyright 2013 Jan Janssen
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "list.h"
+#include "util.h"
+
+int main(int argc, const char *argv[]) {
+        size_t i;
+        typedef struct list_item {
+                LIST_FIELDS(struct list_item, item);
+        } list_item;
+        LIST_HEAD(list_item, head);
+        list_item items[4];
+        list_item *cursor;
+
+        LIST_HEAD_INIT(list_item, head);
+        assert_se(head == NULL);
+
+        for (i = 0; i < ELEMENTSOF(items); i++) {
+                LIST_INIT(list_item, item, &items[i]);
+                assert_se(LIST_JUST_US(item, &items[i]));
+                LIST_PREPEND(list_item, item, head, &items[i]);
+        }
+
+        assert_se(!LIST_JUST_US(item, head));
+
+        assert_se(items[0].item_next == NULL);
+        assert_se(items[1].item_next == &items[0]);
+        assert_se(items[2].item_next == &items[1]);
+        assert_se(items[3].item_next == &items[2]);
+
+        assert_se(items[0].item_prev == &items[1]);
+        assert_se(items[1].item_prev == &items[2]);
+        assert_se(items[2].item_prev == &items[3]);
+        assert_se(items[3].item_prev == NULL);
+
+        LIST_FIND_HEAD(list_item, item, &items[0], cursor);
+        assert_se(cursor == &items[3]);
+
+        LIST_FIND_TAIL(list_item, item, &items[3], cursor);
+        assert_se(cursor == &items[0]);
+
+        LIST_REMOVE(list_item, item, head, &items[1]);
+        assert_se(LIST_JUST_US(item, &items[1]));
+
+        assert_se(items[0].item_next == NULL);
+        assert_se(items[2].item_next == &items[0]);
+        assert_se(items[3].item_next == &items[2]);
+
+        assert_se(items[0].item_prev == &items[2]);
+        assert_se(items[2].item_prev == &items[3]);
+        assert_se(items[3].item_prev == NULL);
+
+        LIST_INSERT_AFTER(list_item, item, head, &items[3], &items[1]);
+        assert_se(items[0].item_next == NULL);
+        assert_se(items[2].item_next == &items[0]);
+        assert_se(items[1].item_next == &items[2]);
+        assert_se(items[3].item_next == &items[1]);
+
+        assert_se(items[0].item_prev == &items[2]);
+        assert_se(items[2].item_prev == &items[1]);
+        assert_se(items[1].item_prev == &items[3]);
+        assert_se(items[3].item_prev == NULL);
+
+        LIST_REMOVE(list_item, item, head, &items[0]);
+        assert_se(LIST_JUST_US(item, &items[0]));
+
+        assert_se(items[2].item_next == NULL);
+        assert_se(items[1].item_next == &items[2]);
+        assert_se(items[3].item_next == &items[1]);
+
+        assert_se(items[2].item_prev == &items[1]);
+        assert_se(items[1].item_prev == &items[3]);
+        assert_se(items[3].item_prev == NULL);
+
+        LIST_REMOVE(list_item, item, head, &items[1]);
+        assert_se(LIST_JUST_US(item, &items[1]));
+
+        assert_se(items[2].item_next == NULL);
+        assert_se(items[3].item_next == &items[2]);
+
+        assert_se(items[2].item_prev == &items[3]);
+        assert_se(items[3].item_prev == NULL);
+
+        LIST_REMOVE(list_item, item, head, &items[2]);
+        assert_se(LIST_JUST_US(item, &items[2]));
+        assert_se(LIST_JUST_US(item, head));
+
+        LIST_REMOVE(list_item, item, head, &items[3]);
+        assert_se(LIST_JUST_US(item, &items[3]));
+
+        return 0;
+}