Skip to content

Commit

Permalink
Updated the test programs.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jgraha8 committed Feb 15, 2019
1 parent e8465a2 commit 0790398
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 105 deletions.
13 changes: 8 additions & 5 deletions tests/test_autoptr1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
38 changes: 19 additions & 19 deletions tests/test_autoptr3.c
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
#include <assert.h>
#include <stdlib.h>

#include <libautoptr/autoptr.h>
#include "test_common.h"
#include <libautoptr/autoptr.h>

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;
}
52 changes: 26 additions & 26 deletions tests/test_autoptr4.c
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
#include <assert.h>
#include <stdlib.h>

#include <libautoptr/autoptr.h>
#include "test_common.h"
#include <libautoptr/autoptr.h>

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;
}
62 changes: 31 additions & 31 deletions tests/test_autoptr5.c
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
#include <assert.h>
#include <stdlib.h>

#include <libautoptr/autoptr.h>
#include "test_common.h"
#include <libautoptr/autoptr.h>

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;
}
57 changes: 33 additions & 24 deletions tests/test_common.h
Original file line number Diff line number Diff line change
@@ -1,49 +1,58 @@
#ifndef __TEST_COMMON_H__
#define __TEST_COMMON_H__

#include <string.h>

#include <libautoptr/autoptr.h>

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__

0 comments on commit 0790398

Please sign in to comment.