summaryrefslogtreecommitdiff
path: root/lib/ccan/tlist/test
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ccan/tlist/test')
-rw-r--r--lib/ccan/tlist/test/compile_fail-tlist_add.c35
-rw-r--r--lib/ccan/tlist/test/compile_fail-tlist_add_tail.c35
-rw-r--r--lib/ccan/tlist/test/compile_fail-tlist_del_from.c34
-rw-r--r--lib/ccan/tlist/test/compile_fail-tlist_for_each.c34
-rw-r--r--lib/ccan/tlist/test/compile_fail-tlist_for_each_safe.c33
-rw-r--r--lib/ccan/tlist/test/compile_fail-tlist_tail.c31
-rw-r--r--lib/ccan/tlist/test/compile_fail-tlist_top.c31
-rw-r--r--lib/ccan/tlist/test/run.c147
8 files changed, 380 insertions, 0 deletions
diff --git a/lib/ccan/tlist/test/compile_fail-tlist_add.c b/lib/ccan/tlist/test/compile_fail-tlist_add.c
new file mode 100644
index 0000000000..1b87bfd119
--- /dev/null
+++ b/lib/ccan/tlist/test/compile_fail-tlist_add.c
@@ -0,0 +1,35 @@
+#include <ccan/tlist/tlist.h>
+
+TLIST_TYPE(children, struct child);
+TLIST_TYPE(cousins, struct cousin);
+
+struct child {
+ const char *name;
+ struct list_node list;
+};
+
+struct cousin {
+ const char *name;
+ struct list_node list;
+};
+
+int main(int argc, char *argv[])
+{
+ struct tlist_children children;
+ struct tlist_cousins cousins;
+ struct child child = { "child" };
+ struct cousin cousin = { "cousin" };
+
+ tlist_init(&children);
+ tlist_init(&cousins);
+ tlist_add(&children, &child, list);
+ tlist_add(&cousins, &cousin, list);
+ tlist_del_from(&cousins, &cousin, list);
+#ifdef FAIL
+#if !HAVE_FLEXIBLE_ARRAY_MEMBER
+#error Need flexible array members to check type
+#endif
+ tlist_add(&children, &cousin, list);
+#endif
+ return 0;
+}
diff --git a/lib/ccan/tlist/test/compile_fail-tlist_add_tail.c b/lib/ccan/tlist/test/compile_fail-tlist_add_tail.c
new file mode 100644
index 0000000000..33dff3d8eb
--- /dev/null
+++ b/lib/ccan/tlist/test/compile_fail-tlist_add_tail.c
@@ -0,0 +1,35 @@
+#include <ccan/tlist/tlist.h>
+
+TLIST_TYPE(children, struct child);
+TLIST_TYPE(cousins, struct cousin);
+
+struct child {
+ const char *name;
+ struct list_node list;
+};
+
+struct cousin {
+ const char *name;
+ struct list_node list;
+};
+
+int main(int argc, char *argv[])
+{
+ struct tlist_children children;
+ struct tlist_cousins cousins;
+ struct child child = { "child" };
+ struct cousin cousin = { "cousin" };
+
+ tlist_init(&children);
+ tlist_init(&cousins);
+ tlist_add(&children, &child, list);
+ tlist_add(&cousins, &cousin, list);
+ tlist_del_from(&cousins, &cousin, list);
+#ifdef FAIL
+#if !HAVE_FLEXIBLE_ARRAY_MEMBER
+#error Need flexible array members to check type
+#endif
+ tlist_add_tail(&children, &cousin, list);
+#endif
+ return 0;
+}
diff --git a/lib/ccan/tlist/test/compile_fail-tlist_del_from.c b/lib/ccan/tlist/test/compile_fail-tlist_del_from.c
new file mode 100644
index 0000000000..d06a72fbfa
--- /dev/null
+++ b/lib/ccan/tlist/test/compile_fail-tlist_del_from.c
@@ -0,0 +1,34 @@
+#include <ccan/tlist/tlist.h>
+
+TLIST_TYPE(children, struct child);
+TLIST_TYPE(cousins, struct cousin);
+
+struct child {
+ const char *name;
+ struct list_node list;
+};
+
+struct cousin {
+ const char *name;
+ struct list_node list;
+};
+
+int main(int argc, char *argv[])
+{
+ struct tlist_children children;
+ struct tlist_cousins cousins;
+ struct child child = { "child" };
+ struct cousin cousin = { "cousin" };
+
+ tlist_init(&children);
+ tlist_init(&cousins);
+ tlist_add(&children, &child, list);
+ tlist_add(&cousins, &cousin, list);
+#ifdef FAIL
+#if !HAVE_FLEXIBLE_ARRAY_MEMBER
+#error Need flexible array members to check type
+#endif
+ tlist_del_from(&children, &cousin, list);
+#endif
+ return 0;
+}
diff --git a/lib/ccan/tlist/test/compile_fail-tlist_for_each.c b/lib/ccan/tlist/test/compile_fail-tlist_for_each.c
new file mode 100644
index 0000000000..1b2fb6882f
--- /dev/null
+++ b/lib/ccan/tlist/test/compile_fail-tlist_for_each.c
@@ -0,0 +1,34 @@
+#include <ccan/tlist/tlist.h>
+
+TLIST_TYPE(children, struct child);
+
+struct child {
+ const char *name;
+ struct list_node list;
+};
+
+struct cousin {
+ const char *name;
+ struct list_node list;
+};
+
+int main(int argc, char *argv[])
+{
+ struct tlist_children children;
+ struct child child = { "child" };
+#ifdef FAIL
+#if !HAVE_FLEXIBLE_ARRAY_MEMBER
+#error Need flexible array members to check type
+#endif
+ struct cousin *c;
+#else
+ struct child *c;
+#endif
+
+ tlist_init(&children);
+ tlist_add(&children, &child, list);
+
+ tlist_for_each(&children, c, list)
+ (void) c; /* Suppress unused-but-set-variable warning. */
+ return 0;
+}
diff --git a/lib/ccan/tlist/test/compile_fail-tlist_for_each_safe.c b/lib/ccan/tlist/test/compile_fail-tlist_for_each_safe.c
new file mode 100644
index 0000000000..651c6cefd6
--- /dev/null
+++ b/lib/ccan/tlist/test/compile_fail-tlist_for_each_safe.c
@@ -0,0 +1,33 @@
+#include <ccan/tlist/tlist.h>
+
+TLIST_TYPE(children, struct child);
+
+struct child {
+ const char *name;
+ struct list_node list;
+};
+
+struct cousin {
+ const char *name;
+ struct list_node list;
+};
+
+int main(int argc, char *argv[])
+{
+ struct tlist_children children;
+ struct child child = { "child" };
+#ifdef FAIL
+#if !HAVE_FLEXIBLE_ARRAY_MEMBER
+#error Need flexible array members to check type
+#endif
+ struct cousin *c, *n;
+#else
+ struct child *c, *n;
+#endif
+
+ tlist_init(&children);
+ tlist_add(&children, &child, list);
+
+ tlist_for_each_safe(&children, c, n, list);
+ return 0;
+}
diff --git a/lib/ccan/tlist/test/compile_fail-tlist_tail.c b/lib/ccan/tlist/test/compile_fail-tlist_tail.c
new file mode 100644
index 0000000000..48f394446e
--- /dev/null
+++ b/lib/ccan/tlist/test/compile_fail-tlist_tail.c
@@ -0,0 +1,31 @@
+#include <ccan/tlist/tlist.h>
+
+TLIST_TYPE(children, struct child);
+
+struct child {
+ const char *name;
+ struct list_node list;
+};
+
+struct cousin {
+ const char *name;
+ struct list_node list;
+};
+
+int main(int argc, char *argv[])
+{
+ struct tlist_children children;
+ struct child child = { "child" };
+#ifdef FAIL
+ struct cousin *c;
+#else
+ struct child *c;
+#endif
+
+ tlist_init(&children);
+ tlist_add(&children, &child, list);
+
+ c = tlist_tail(&children, list);
+ (void) c; /* Suppress unused-but-set-variable warning. */
+ return 0;
+}
diff --git a/lib/ccan/tlist/test/compile_fail-tlist_top.c b/lib/ccan/tlist/test/compile_fail-tlist_top.c
new file mode 100644
index 0000000000..21651400ef
--- /dev/null
+++ b/lib/ccan/tlist/test/compile_fail-tlist_top.c
@@ -0,0 +1,31 @@
+#include <ccan/tlist/tlist.h>
+
+TLIST_TYPE(children, struct child);
+
+struct child {
+ const char *name;
+ struct list_node list;
+};
+
+struct cousin {
+ const char *name;
+ struct list_node list;
+};
+
+int main(int argc, char *argv[])
+{
+ struct tlist_children children;
+ struct child child = { "child" };
+#ifdef FAIL
+ struct cousin *c;
+#else
+ struct child *c;
+#endif
+
+ tlist_init(&children);
+ tlist_add(&children, &child, list);
+
+ c = tlist_top(&children, list);
+ (void) c; /* Suppress unused-but-set-variable warning. */
+ return 0;
+}
diff --git a/lib/ccan/tlist/test/run.c b/lib/ccan/tlist/test/run.c
new file mode 100644
index 0000000000..95b02ebe21
--- /dev/null
+++ b/lib/ccan/tlist/test/run.c
@@ -0,0 +1,147 @@
+#define CCAN_LIST_DEBUG 1
+#include <ccan/tlist/tlist.h>
+#include <ccan/tap/tap.h>
+
+TLIST_TYPE(children, struct child);
+
+struct parent {
+ const char *name;
+ struct tlist_children children;
+ unsigned int num_children;
+};
+
+struct child {
+ const char *name;
+ struct list_node list;
+};
+
+int main(int argc, char *argv[])
+{
+ struct parent parent;
+ struct child c1, c2, c3, *c, *n;
+ unsigned int i;
+ struct tlist_children tlist = TLIST_INIT(tlist);
+
+ plan_tests(48);
+ /* Test TLIST_INIT, and tlist_empty */
+ ok1(tlist_empty(&tlist));
+ ok1(tlist_check(&tlist, NULL));
+
+ parent.num_children = 0;
+ tlist_init(&parent.children);
+ /* Test tlist_init */
+ ok1(tlist_empty(&parent.children));
+ ok1(tlist_check(&parent.children, NULL));
+
+ c2.name = "c2";
+ tlist_add(&parent.children, &c2, list);
+ /* Test tlist_add and !tlist_empty. */
+ ok1(!tlist_empty(&parent.children));
+ ok1(c2.list.next == &parent.children.raw.n);
+ ok1(c2.list.prev == &parent.children.raw.n);
+ ok1(parent.children.raw.n.next == &c2.list);
+ ok1(parent.children.raw.n.prev == &c2.list);
+ /* Test tlist_check */
+ ok1(tlist_check(&parent.children, NULL));
+
+ c1.name = "c1";
+ tlist_add(&parent.children, &c1, list);
+ /* Test list_add and !list_empty. */
+ ok1(!tlist_empty(&parent.children));
+ ok1(c2.list.next == &parent.children.raw.n);
+ ok1(c2.list.prev == &c1.list);
+ ok1(parent.children.raw.n.next == &c1.list);
+ ok1(parent.children.raw.n.prev == &c2.list);
+ ok1(c1.list.next == &c2.list);
+ ok1(c1.list.prev == &parent.children.raw.n);
+ /* Test tlist_check */
+ ok1(tlist_check(&parent.children, NULL));
+
+ c3.name = "c3";
+ tlist_add_tail(&parent.children, &c3, list);
+ /* Test list_add_tail and !list_empty. */
+ ok1(!tlist_empty(&parent.children));
+ ok1(parent.children.raw.n.next == &c1.list);
+ ok1(parent.children.raw.n.prev == &c3.list);
+ ok1(c1.list.next == &c2.list);
+ ok1(c1.list.prev == &parent.children.raw.n);
+ ok1(c2.list.next == &c3.list);
+ ok1(c2.list.prev == &c1.list);
+ ok1(c3.list.next == &parent.children.raw.n);
+ ok1(c3.list.prev == &c2.list);
+ /* Test tlist_check */
+ ok1(tlist_check(&parent.children, NULL));
+
+ /* Test tlist_top */
+ ok1(tlist_top(&parent.children, list) == &c1);
+
+ /* Test list_tail */
+ ok1(tlist_tail(&parent.children, list) == &c3);
+
+ /* Test tlist_for_each. */
+ i = 0;
+ tlist_for_each(&parent.children, c, list) {
+ switch (i++) {
+ case 0:
+ ok1(c == &c1);
+ break;
+ case 1:
+ ok1(c == &c2);
+ break;
+ case 2:
+ ok1(c == &c3);
+ break;
+ }
+ if (i > 2)
+ break;
+ }
+ ok1(i == 3);
+
+ /* Test tlist_for_each_rev. */
+ i = 0;
+ tlist_for_each_rev(&parent.children, c, list) {
+ switch (i++) {
+ case 0:
+ ok1(c == &c3);
+ break;
+ case 1:
+ ok1(c == &c2);
+ break;
+ case 2:
+ ok1(c == &c1);
+ break;
+ }
+ if (i > 2)
+ break;
+ }
+ ok1(i == 3);
+
+ /* Test tlist_for_each_safe, tlist_del and tlist_del_from. */
+ i = 0;
+ tlist_for_each_safe(&parent.children, c, n, list) {
+ switch (i++) {
+ case 0:
+ ok1(c == &c1);
+ tlist_del(c, list);
+ break;
+ case 1:
+ ok1(c == &c2);
+ tlist_del_from(&parent.children, c, list);
+ break;
+ case 2:
+ ok1(c == &c3);
+ tlist_del_from(&parent.children, c, list);
+ break;
+ }
+ ok1(tlist_check(&parent.children, NULL));
+ if (i > 2)
+ break;
+ }
+ ok1(i == 3);
+ ok1(tlist_empty(&parent.children));
+
+ /* Test list_top/list_tail on empty list. */
+ ok1(tlist_top(&parent.children, list) == (struct child *)NULL);
+ ok1(tlist_tail(&parent.children, list) == (struct child *)NULL);
+ return exit_status();
+}