From 0790398dc124362900f736d0564a6804dd4c95fb Mon Sep 17 00:00:00 2001 From: Jason Graham Date: Thu, 14 Feb 2019 23:11:51 -0500 Subject: [PATCH] Updated the test programs. The test data structure now includes canary data for use during debugging cleanup. Also, test_dtor() now performs a call to autoptr_zero_obj() as the last statement. --- tests/test_autoptr1.c | 13 +++++---- tests/test_autoptr3.c | 38 +++++++++++++------------- tests/test_autoptr4.c | 52 ++++++++++++++++++------------------ tests/test_autoptr5.c | 62 +++++++++++++++++++++---------------------- tests/test_common.h | 57 ++++++++++++++++++++++----------------- 5 files changed, 117 insertions(+), 105 deletions(-) diff --git a/tests/test_autoptr1.c b/tests/test_autoptr1.c index 43bb98b..dbc5867 100644 --- a/tests/test_autoptr1.c +++ b/tests/test_autoptr1.c @@ -6,16 +6,19 @@ int main(int argc, char **argv) { - struct test *t = test_alloc(); - struct test *p = autoptr_bind(t); + struct test t; + struct test *p = NULL; + + test_ctor(&t); + p = autoptr_bind(&t); - assert( ! autoptr_destroy_ok(t) ); + assert( ! autoptr_destroy_ok(&t) ); // Release ownership of primary test object - autoptr_release(t); + autoptr_release(&t); // The test object should be destroyable (i.e., single owner) - assert( autoptr_destroy_ok(t) ); + assert( autoptr_destroy_ok(&t) ); autoptr_unbind((void **)&p); assert( p == NULL ); diff --git a/tests/test_autoptr3.c b/tests/test_autoptr3.c index 5e30151..5a0c613 100644 --- a/tests/test_autoptr3.c +++ b/tests/test_autoptr3.c @@ -1,32 +1,32 @@ #include #include -#include #include "test_common.h" +#include int main(int argc, char **argv) { - struct test *t = test_valloc(3); - struct test *p0 = autoptr_bind(&t[0]); - struct test *p1 = autoptr_bind(&t[1]); - struct test *p2 = autoptr_bind(&t[2]); - - assert( ! autoptr_destroy_ok(t) ); + struct test *t = test_valloc(3); + struct test *p0 = autoptr_bind(&t[0]); + struct test *p1 = autoptr_bind(&t[1]); + struct test *p2 = autoptr_bind(&t[2]); + + assert(!autoptr_destroy_ok(t)); - autoptr_unbind((void **)&p0); - assert( p0 == NULL ); - assert(test_initd); + autoptr_unbind((void **)&p0); + assert(p0 == NULL); + assert(test_initd); - autoptr_unbind((void **)&p1); - assert( p1 == NULL ); - assert(test_initd); + autoptr_unbind((void **)&p1); + assert(p1 == NULL); + assert(test_initd); - autoptr_unbind((void **)&p2); - assert( p2 == NULL ); - assert(test_initd); + autoptr_unbind((void **)&p2); + assert(p2 == NULL); + assert(test_initd); - autoptr_vfree_obj((void **)&t, 3); - assert(!test_initd); + autoptr_vfree_obj((void **)&t, 3); + assert(!test_initd); - return 0; + return 0; } diff --git a/tests/test_autoptr4.c b/tests/test_autoptr4.c index b142364..251dca2 100644 --- a/tests/test_autoptr4.c +++ b/tests/test_autoptr4.c @@ -1,43 +1,43 @@ #include #include -#include #include "test_common.h" +#include int main(int argc, char **argv) { - struct test *t = test_valloc(3); - struct test *p[3]; + struct test *t = test_valloc(3); + struct test *p[3]; - // Bind the vector to a list of pointers - autoptr_vbindl(t, 3, (void **)p); + // Bind the vector to a list of pointers + autoptr_vbindl(t, 3, (void **)p); - // Bind each pointer in the list - struct test *p0 = autoptr_bind(p[0]); - struct test *p1 = autoptr_bind(p[1]); - struct test *p2 = autoptr_bind(p[2]); + // Bind each pointer in the list + struct test *p0 = autoptr_bind(p[0]); + struct test *p1 = autoptr_bind(p[1]); + struct test *p2 = autoptr_bind(p[2]); - // Transfer ownership - autoptr_release(t); - assert( ! autoptr_destroy_ok(t) ); + // Transfer ownership + autoptr_release(t); + assert(!autoptr_destroy_ok(t)); - // Unbind the list of pointers - autoptr_lunbind((void **)p, 3); - assert(test_initd); + // Unbind the list of pointers + autoptr_lunbind((void **)p, 3); + assert(test_initd); - autoptr_unbind((void **)&p0); - assert( p0 == NULL ); - assert(test_initd); + autoptr_unbind((void **)&p0); + assert(p0 == NULL); + assert(test_initd); - autoptr_unbind((void **)&p1); - assert( p1 == NULL ); - assert(test_initd); + autoptr_unbind((void **)&p1); + assert(p1 == NULL); + assert(test_initd); - autoptr_unbind((void **)&p2); - assert( p2 == NULL ); + autoptr_unbind((void **)&p2); + assert(p2 == NULL); - // Ensure that destructor callback was called - assert(!test_initd); + // Ensure that destructor callback was called + assert(!test_initd); - return 0; + return 0; } diff --git a/tests/test_autoptr5.c b/tests/test_autoptr5.c index 47ecabe..bcdc26c 100644 --- a/tests/test_autoptr5.c +++ b/tests/test_autoptr5.c @@ -1,49 +1,49 @@ #include #include -#include #include "test_common.h" +#include int main(int argc, char **argv) { - struct test *t[3]; + struct test *t[3]; + + for (size_t n = 0; n < 3; ++n) + t[n] = test_alloc(); - for( size_t n=0; n<3; ++n ) - t[n] = test_alloc(); - - struct test *p[3]; + struct test *p[3]; - // Bind the list of pointers to another list - autoptr_lbindl((void **)t, 3, (void **)p); + // Bind the list of pointers to another list + autoptr_lbindl((void **)t, 3, (void **)p); - // Bind each pointer in the list - struct test *p0 = autoptr_bind(p[0]); - struct test *p1 = autoptr_bind(p[1]); - struct test *p2 = autoptr_bind(p[2]); + // Bind each pointer in the list + struct test *p0 = autoptr_bind(p[0]); + struct test *p1 = autoptr_bind(p[1]); + struct test *p2 = autoptr_bind(p[2]); - // Transfer ownership - for( size_t n=0; n<3; ++n ) { - autoptr_release(t[n]); - assert( ! autoptr_destroy_ok(t[n]) ); - } + // Transfer ownership + for (size_t n = 0; n < 3; ++n) { + autoptr_release(t[n]); + assert(!autoptr_destroy_ok(t[n])); + } - // Unbind the list of pointers - autoptr_lunbind((void **)p, 3); - assert(test_initd); + // Unbind the list of pointers + autoptr_lunbind((void **)p, 3); + assert(test_initd); - autoptr_unbind((void **)&p0); - assert( p0 == NULL ); - assert(test_initd); + autoptr_unbind((void **)&p0); + assert(p0 == NULL); + assert(test_initd); - autoptr_unbind((void **)&p1); - assert( p1 == NULL ); - assert(test_initd); + autoptr_unbind((void **)&p1); + assert(p1 == NULL); + assert(test_initd); - autoptr_unbind((void **)&p2); - assert( p2 == NULL ); + autoptr_unbind((void **)&p2); + assert(p2 == NULL); - // Ensure that destructor callback was called - assert(!test_initd); + // Ensure that destructor callback was called + assert(!test_initd); - return 0; + return 0; } diff --git a/tests/test_common.h b/tests/test_common.h index 087bf24..13464ec 100644 --- a/tests/test_common.h +++ b/tests/test_common.h @@ -1,49 +1,58 @@ #ifndef __TEST_COMMON_H__ #define __TEST_COMMON_H__ +#include + #include static int test_initd = 0; struct test { - struct autoptr __autoptr; + struct autoptr __autoptr; + int data; }; -static void test_dtor(struct test *t) +static void test_dtor(struct test *t); +static void test_ctor(struct test *t) { - if( !autoptr_destroy_ok(t) ) { - autoptr_release(t); - return; - } - - assert( --test_initd >= 0 ); + autoptr_ctor(t, sizeof(*t), (void (*)(void *))test_dtor); + ++test_initd; + + t->data = 42; } -__attribute__((unused)) -static struct test *test_alloc() +static void test_dtor(struct test *t) { - struct test *t = calloc(1, sizeof(*t)); + if (!autoptr_destroy_ok(t)) { + autoptr_release(t); + return; + } - autoptr_ctor((struct autoptr *)t, sizeof(*t), (void (*)(void *))test_dtor); - autoptr_set_allocd(t, true); + assert(--test_initd >= 0); + autoptr_zero_obj(t); +} + +__attribute__((unused)) static struct test *test_alloc() +{ + struct test *t = calloc(1, sizeof(*t)); + test_ctor(t); - ++test_initd; + autoptr_set_allocd(t, true); - return t; + return t; } -__attribute__((unused)) -static struct test *test_valloc(size_t n) +__attribute__((unused)) static struct test *test_valloc(size_t n) { - struct test *t = calloc(n, sizeof(*t)); - - autoptr_ctor((struct autoptr *)t, sizeof(*t), (void (*)(void *))test_dtor); - autoptr_set_allocd(t, true); - autoptr_set_managed(t, n); + struct test *t = calloc(n, sizeof(*t)); - test_initd += n; + for (size_t i = 0; i < n; ++i) { + test_ctor(t + i); + } + autoptr_set_allocd(t, true); + autoptr_set_managed(t, n); - return t; + return t; } #endif // __TEST_COMMON_H__