]> git.ozlabs.org Git - petitboot/blobdiff - test/lib/list-test.c
test/lib: Use talloc in list test
[petitboot] / test / lib / list-test.c
index de629eede7da6d80ae45f6ac72f9c6ce7efdca79..3f7952ab888d10178256b3529a35cc01352b6f2b 100644 (file)
@@ -25,6 +25,7 @@
 #include <stdlib.h>
 
 #include <list/list.h>
+#include <talloc/talloc.h>
 
 
 int main(void)
@@ -36,10 +37,13 @@ int main(void)
        STATIC_LIST(tester);
        struct item *item;
        struct item *tmp;
+       void *ctx;
        int i;
 
+       ctx = talloc_new(NULL);
+
        for (i = 0; i < 5; i++) {
-               struct item *item = malloc(sizeof(struct item));
+               struct item *item = talloc(ctx, struct item);
 
                item->value = i;
 
@@ -60,12 +64,31 @@ int main(void)
                list_remove(&item->list);
        }
 
+       /* we should see that the list is empty */
        i = 0;
-       fprintf(stderr, "-- list_for_each_entry --\n");
+       fprintf(stderr, "-- list_for_each_entry(empty) --\n");
        list_for_each_entry(&tester, item, list) {
                fprintf(stderr, "%d: %d: %p -> %p\n", i++, item->value, item, item->list.next);
        }
 
+       if (i) {
+               fprintf(stderr, "Error: list should be empty\n");
+               return EXIT_FAILURE;
+       }
+
+       i = 0;
+       fprintf(stderr, "-- list_for_each_entry_safe(empty) --\n");
+       list_for_each_entry_safe(&tester, item, tmp, list) {
+               fprintf(stderr, "%d: %d: %p -> %p\n", i++, item->value, item, item->list.next);
+       }
+
+       if (i) {
+               fprintf(stderr, "Error: list should be empty\n");
+               return EXIT_FAILURE;
+       }
        fprintf(stderr, "-- done --\n");
+
+       talloc_free(ctx);
+
        return EXIT_SUCCESS;
 }