]> git.ozlabs.org Git - ccan/blobdiff - ccan/list/test/run.c
list.h: opaque list iteration functionality
[ccan] / ccan / list / test / run.c
index 4ac2dcda34e3eb5ebdc50aca20ea8ac1e379505a..1d02acd5b763123ae365f4e185c676dd6c35453d 100644 (file)
@@ -1,6 +1,7 @@
 #include <ccan/list/list.h>
 #include <ccan/tap/tap.h>
 #include <ccan/list/list.c>
+#include "helper.h"
 
 struct parent {
        const char *name;
@@ -21,8 +22,10 @@ int main(int argc, char *argv[])
        struct child c1, c2, c3, *c, *n;
        unsigned int i;
        struct list_head list = LIST_HEAD_INIT(list);
+       opaque_t *q, *nq;
+       struct list_head opaque_list = LIST_HEAD_INIT(opaque_list);
 
-       plan_tests(53);
+       plan_tests(65);
        /* Test LIST_HEAD, LIST_HEAD_INIT, list_empty and check_list */
        ok1(list_empty(&static_list));
        ok1(list_check(&static_list, NULL));
@@ -147,6 +150,49 @@ int main(int argc, char *argv[])
        ok1(i == 3);
        ok1(list_empty(&parent.children));
 
+       /* Test list_for_each_off. */
+       list_add_tail(&opaque_list,
+                     (struct list_node *)create_opaque_blob());
+       list_add_tail(&opaque_list,
+                     (struct list_node *)create_opaque_blob());
+       list_add_tail(&opaque_list,
+                     (struct list_node *)create_opaque_blob());
+
+       i = 0;
+
+       list_for_each_off(&opaque_list, q, 0) {
+         i++;
+         ok1(if_blobs_know_the_secret(q));
+       }
+       ok1(i == 3);
+
+       /* Test list_for_each_safe_off, list_del_off and list_del_from_off. */
+       i = 0;
+       list_for_each_safe_off(&opaque_list, q, nq, 0) {
+               switch (i++) {
+               case 0:
+                       ok1(if_blobs_know_the_secret(q));
+                       list_del_off(q, 0);
+                       destroy_opaque_blob(q);
+                       break;
+               case 1:
+                       ok1(if_blobs_know_the_secret(q));
+                       list_del_from_off(&opaque_list, q, 0);
+                       destroy_opaque_blob(q);
+                       break;
+               case 2:
+                       ok1(c == &c3);
+                       list_del_from_off(&opaque_list, q, 0);
+                       destroy_opaque_blob(q);
+                       break;
+               }
+               ok1(list_check(&opaque_list, NULL));
+               if (i > 2)
+                       break;
+       }
+       ok1(i == 3);
+       ok1(list_empty(&opaque_list));
+
        /* Test list_top/list_tail on empty list. */
        ok1(list_top(&parent.children, struct child, list) == NULL);
        ok1(list_tail(&parent.children, struct child, list) == NULL);