talloc: fix testsuite after no_owner changes
authorStefan Metzmacher <metze@samba.org>
Mon, 13 Jul 2009 18:49:05 +0000 (20:49 +0200)
committerStefan Metzmacher <metze@samba.org>
Mon, 13 Jul 2009 18:55:14 +0000 (20:55 +0200)
metze

lib/talloc/testsuite.c

index 0fc444b02712d3dcbba57e4718f934a616893eeb..13010030459152080eeca7447daa0e65da431c60 100644 (file)
@@ -99,6 +99,18 @@ static double timeval_elapsed(struct timeval *tv)
        } \
 } while (0)
 
+#define CHECK_NOT_PARENT(test, ptr, parent) do { \
+       if (talloc_parent(ptr) == (parent)) { \
+               printf("failed: %s [\n%s: '%s' has wrong parent: got %p  expected a difference\n]\n", \
+                      test, __location__, #ptr, \
+                      (parent)); \
+               talloc_report_full(ptr, stdout); \
+               talloc_report_full(parent, stdout); \
+               talloc_report_full(NULL, stdout); \
+               return false; \
+       } \
+} while (0)
+
 
 /*
   test references 
@@ -128,15 +140,15 @@ static bool test_ref1(void)
        talloc_free(p2);
        talloc_report_full(root, stderr);
 
-       CHECK_BLOCKS("ref1", p1, 5);
+       CHECK_BLOCKS("ref1", p1, 4);
        CHECK_BLOCKS("ref1", p2, 1);
-       CHECK_BLOCKS("ref1", r1, 1);
+       CHECK_BLOCKS("ref1", r1, 2);
 
        fprintf(stderr, "Freeing p1\n");
        talloc_free(p1);
        talloc_report_full(root, stderr);
 
-       CHECK_BLOCKS("ref1", r1, 1);
+       CHECK_BLOCKS("ref1", r1, 2);
 
        fprintf(stderr, "Freeing r1\n");
        talloc_free(r1);
@@ -180,7 +192,7 @@ static bool test_ref2(void)
        CHECK_BLOCKS("ref2", r1, 2);
 
        fprintf(stderr, "Freeing ref\n");
-       talloc_free(ref);
+       talloc_unlink(r1, ref);
        talloc_report_full(root, stderr);
 
        CHECK_BLOCKS("ref2", p1, 5);
@@ -370,12 +382,11 @@ static bool test_misc(void)
        talloc_increase_ref_count(p1);
        talloc_increase_ref_count(p1);
        talloc_increase_ref_count(p1);
-       CHECK_BLOCKS("misc", p1, 1);
-       CHECK_BLOCKS("misc", root, 2);
-       talloc_free(p1);
+       CHECK_BLOCKS("misc", NULL, 7);
        CHECK_BLOCKS("misc", p1, 1);
        CHECK_BLOCKS("misc", root, 2);
        talloc_unlink(NULL, p1);
+       CHECK_BLOCKS("misc", NULL, 6);
        CHECK_BLOCKS("misc", p1, 1);
        CHECK_BLOCKS("misc", root, 2);
        p2 = talloc_strdup(p1, "foo");
@@ -384,28 +395,38 @@ static bool test_misc(void)
        torture_assert("misc", talloc_unlink(p1, p2) == 0,
                "failed: talloc_unlink() of parent should succeed\n");
        talloc_free(p1);
+       CHECK_BLOCKS("misc", NULL, 6);
        CHECK_BLOCKS("misc", p1, 1);
-       CHECK_BLOCKS("misc", root, 2);
+       CHECK_BLOCKS("misc", root, 1);
+       talloc_unlink(NULL, p1);
+       CHECK_BLOCKS("misc", NULL, 5);
+       CHECK_BLOCKS("misc", p1, 1);
+       CHECK_BLOCKS("misc", root, 1);
 
        name = talloc_set_name(p1, "my name is %s", "foo");
        torture_assert_str_equal("misc", talloc_get_name(p1), "my name is foo",
                "failed: wrong name after talloc_set_name(my name is foo)");
+       CHECK_BLOCKS("misc", NULL, 6);
        CHECK_BLOCKS("misc", p1, 2);
-       CHECK_BLOCKS("misc", root, 3);
+       CHECK_BLOCKS("misc", root, 1);
 
        talloc_set_name_const(p1, NULL);
        torture_assert_str_equal ("misc", talloc_get_name(p1), "UNNAMED",
                "failed: wrong name after talloc_set_name(NULL)");
+       CHECK_BLOCKS("misc", NULL, 6);
        CHECK_BLOCKS("misc", p1, 2);
-       CHECK_BLOCKS("misc", root, 3);
+       CHECK_BLOCKS("misc", root, 1);
 
        torture_assert("misc", talloc_free(NULL) == -1, 
                                   "talloc_free(NULL) should give -1\n");
 
        talloc_set_destructor(p1, fail_destructor);
-       torture_assert("misc", talloc_free(p1) == -1, 
-               "Failed destructor should cause talloc_free to fail\n");
+       torture_assert("misc", talloc_unlink(NULL, p1) == -1,
+               "Failed destructor should cause talloc_unlink to fail\n");
        talloc_set_destructor(p1, NULL);
+       CHECK_BLOCKS("misc", NULL, 6);
+       CHECK_BLOCKS("misc", p1, 2);
+       CHECK_BLOCKS("misc", root, 1);
 
        talloc_report(root, stderr);
 
@@ -439,13 +460,18 @@ static bool test_misc(void)
        d = talloc_realloc(p1, d, double, 0x20000000);
        torture_assert("misc", !d, "failed: integer overflow not detected\n");
 
-       talloc_free(p1);
+       CHECK_BLOCKS("misc", NULL, 7);
+       talloc_unlink(NULL, p1);
+       CHECK_BLOCKS("misc", NULL, 3);
        CHECK_BLOCKS("misc", root, 1);
 
        p1 = talloc_named(root, 100, "%d bytes", 100);
+       CHECK_BLOCKS("misc", NULL, 5);
        CHECK_BLOCKS("misc", p1, 2);
        CHECK_BLOCKS("misc", root, 3);
        talloc_unlink(root, p1);
+       CHECK_BLOCKS("misc", NULL, 3);
+       CHECK_BLOCKS("misc", root, 1);
 
        p1 = talloc_init("%d bytes", 200);
        p2 = talloc_asprintf(p1, "my test '%s'", "string");
@@ -765,16 +791,25 @@ static bool test_unref_reparent(void)
        p2 = talloc_named_const(root, 1, "parent by reference");
 
        c1 = talloc_named_const(p1, 1, "child");
+       CHECK_BLOCKS("unref_reparent", root, 4);
+       CHECK_SIZE("unref_reparent", root, 3);
        talloc_reference(p2, c1);
+       CHECK_BLOCKS("unref_reparent", root, 5);
+       CHECK_SIZE("unref_reparent", root, 3);
 
+       CHECK_BLOCKS("unref_reparent", root, 5);
+       CHECK_SIZE("unref_reparent", root, 3);
        CHECK_PARENT("unref_reparent", c1, p1);
 
        talloc_free(p1);
 
-       CHECK_PARENT("unref_reparent", c1, p2);
+       CHECK_BLOCKS("unref_reparent", root, 3);
+       CHECK_SIZE("unref_reparent", root, 1);
+       CHECK_NOT_PARENT("unref_reparent", c1, p2);
 
        talloc_unlink(p2, c1);
 
+       CHECK_BLOCKS("unref_reparent", root, 2);
        CHECK_SIZE("unref_reparent", root, 1);
 
        talloc_free(p2);
@@ -856,23 +891,38 @@ static bool test_speed(void)
 
 static bool test_lifeless(void)
 {
-       void *top = talloc_new(NULL);
+       void *top = talloc_named_const(NULL, 0, "top");
        char *parent, *child; 
-       void *child_owner = talloc_new(NULL);
+       void *child_owner = talloc_named_const(NULL, 0, "child_owner");
 
        printf("test: lifeless\n# TALLOC_UNLINK LOOP\n");
 
        parent = talloc_strdup(top, "parent");
+       CHECK_BLOCKS("lifeless", NULL, 5);
        child = talloc_strdup(parent, "child");  
+       CHECK_BLOCKS("lifeless", NULL, 6);
        (void)talloc_reference(child, parent);
+       CHECK_BLOCKS("lifeless", NULL, 7);
        (void)talloc_reference(child_owner, child); 
-       talloc_report_full(top, stderr);
+       CHECK_BLOCKS("lifeless", NULL, 8);
+       CHECK_BLOCKS("lifeless", top, 4);
+       CHECK_BLOCKS("lifeless", child_owner, 2);
        talloc_unlink(top, parent);
+       CHECK_BLOCKS("lifeless", NULL, 8);
+       CHECK_BLOCKS("lifeless", top, 1);
+       CHECK_BLOCKS("lifeless", child_owner, 2);
+       CHECK_BLOCKS("lifeless", child, 2);
        talloc_free(child);
-       talloc_report_full(top, stderr);
+       CHECK_BLOCKS("lifeless", NULL, 8);
+       CHECK_BLOCKS("lifeless", top, 1);
+       CHECK_BLOCKS("lifeless", child_owner, 2);
+       CHECK_BLOCKS("lifeless", child, 2);
        talloc_free(top);
+       CHECK_BLOCKS("lifeless", NULL, 7);
+       CHECK_BLOCKS("lifeless", child_owner, 2);
+       CHECK_BLOCKS("lifeless", child, 2);
        talloc_free(child_owner);
-       talloc_free(child);
+       CHECK_BLOCKS("lifeless", NULL, 2);
 
        printf("success: lifeless\n");
        return true;