Skip to content

Commit

Permalink
Build: improve alloc/realloc/dealloc tests (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxwolf authored Aug 30, 2016
1 parent 4045234 commit f02e19c
Showing 1 changed file with 191 additions and 49 deletions.
240 changes: 191 additions & 49 deletions test/check_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,93 +6,235 @@
#include <stdlib.h>
#include "include/util_int.h"

static size_t _test_alloc_in_amt;
static void *_test_alloc_in_ptr;
static void *_test_alloc_out_ptr;
static void *test_alloc(size_t amt)
{
// TODO: verify amount requested
return malloc(amt);
_test_alloc_in_amt = amt;
_test_alloc_out_ptr = malloc(amt);
return _test_alloc_out_ptr;
}

static void *test_realloc(void *ptr, size_t amt)
{
// TODO: verify pointer to change & amount requested
return realloc(ptr, amt);
_test_alloc_in_ptr = ptr;
_test_alloc_in_amt = amt;
_test_alloc_out_ptr = realloc(ptr, amt);
return _test_alloc_out_ptr;
}

static void test_dealloc(void *ptr)
{
// TODO: verify pointer requested
_test_alloc_in_ptr = ptr;
free(ptr);
}

START_TEST(test_cjose_set_allocators)
static void test_alloc_reset()
{
ck_assert(malloc == cjose_get_alloc());
ck_assert(realloc == cjose_get_realloc());
ck_assert(free == cjose_get_dealloc());
ck_assert(cjose_alloc3_default == cjose_get_alloc3());
ck_assert(cjose_realloc3_default == cjose_get_realloc3());
ck_assert(cjose_dealloc3_default == cjose_get_dealloc3());
_test_alloc_in_amt = 0;
_test_alloc_in_ptr = _test_alloc_out_ptr = NULL;
}

START_TEST(test_cjose_set_allocators)
{
// Simply verify return ptr is not NULL
ck_assert(NULL != cjose_get_alloc());
ck_assert(NULL != cjose_get_realloc());
ck_assert(NULL != cjose_get_dealloc());
ck_assert(NULL != cjose_get_alloc3());
ck_assert(NULL != cjose_get_realloc3());
ck_assert(NULL != cjose_get_dealloc3());

// Simply verify return ptr is not NULL
cjose_set_alloc_funcs(test_alloc, test_realloc, test_dealloc);
ck_assert(test_alloc == cjose_get_alloc());
ck_assert(test_realloc == cjose_get_realloc());
ck_assert(test_dealloc == cjose_get_dealloc());
ck_assert(cjose_alloc3_default == cjose_get_alloc3());
ck_assert(cjose_realloc3_default == cjose_get_realloc3());
ck_assert(cjose_dealloc3_default == cjose_get_dealloc3());

cjose_set_alloc_funcs(NULL, NULL, NULL);
ck_assert(malloc == cjose_get_alloc());
ck_assert(realloc == cjose_get_realloc());
ck_assert(free == cjose_get_dealloc());
ck_assert(cjose_alloc3_default == cjose_get_alloc3());
ck_assert(cjose_realloc3_default == cjose_get_realloc3());
ck_assert(cjose_dealloc3_default == cjose_get_dealloc3());
ck_assert(NULL != cjose_get_alloc());
ck_assert(NULL != cjose_get_realloc());
ck_assert(NULL != cjose_get_dealloc());
ck_assert(NULL != cjose_get_alloc3());
ck_assert(NULL != cjose_get_realloc3());
ck_assert(NULL != cjose_get_dealloc3());

// test simple allocation/reallocation/deallocation redirect
size_t amt;
void *ptr;
void *re_ptr;

test_alloc_reset();

amt = 129;
ptr = cjose_get_alloc()(amt);
ck_assert(amt == _test_alloc_in_amt);
ck_assert(ptr == _test_alloc_out_ptr);

amt = 319;
re_ptr = cjose_get_realloc()(ptr, amt);
ck_assert(amt == _test_alloc_in_amt);
ck_assert(ptr == _test_alloc_in_ptr);
ck_assert(re_ptr == _test_alloc_out_ptr);

ptr = re_ptr;
cjose_get_dealloc()(ptr);
ck_assert(ptr == _test_alloc_out_ptr);

// test extended allocation/reallocation/deallocation redirect
test_alloc_reset();

amt = 129;
ptr = cjose_get_alloc3()(amt, __FILE__, __LINE__);
ck_assert(amt == _test_alloc_in_amt);
ck_assert(ptr == _test_alloc_out_ptr);

amt = 319;
re_ptr = cjose_get_realloc3()(ptr, amt, __FILE__, __LINE__);
ck_assert(amt == _test_alloc_in_amt);
ck_assert(ptr == _test_alloc_in_ptr);
ck_assert(re_ptr == _test_alloc_out_ptr);

ptr = re_ptr;
cjose_get_dealloc3()(ptr, __FILE__, __LINE__);
ck_assert(ptr == _test_alloc_out_ptr);

// Simply verify return ptr is not NULL
ck_assert(NULL != cjose_get_alloc());
ck_assert(NULL != cjose_get_realloc());
ck_assert(NULL != cjose_get_dealloc());
ck_assert(NULL != cjose_get_alloc3());
ck_assert(NULL != cjose_get_realloc3());
ck_assert(NULL != cjose_get_dealloc3());
}
END_TEST

static size_t _test_alloc3_in_amt;
static void *_test_alloc3_in_ptr;
static const char *_test_alloc3_in_file;
static int _test_alloc3_in_line;
static void *_test_alloc3_out_ptr;
static void test_alloc3_reset()
{
test_alloc_reset();
_test_alloc3_in_amt = 0;
_test_alloc3_in_ptr = _test_alloc3_out_ptr = NULL;
_test_alloc3_in_file = NULL;
_test_alloc3_in_line = 0;
}

static void *test_alloc3(size_t amt, const char *file, int line)
{
// TODO: verify amount requested
return malloc(amt);
_test_alloc3_in_amt = amt;
_test_alloc3_in_file = file;
_test_alloc3_in_line = line;
_test_alloc3_out_ptr = malloc(amt);
return _test_alloc3_out_ptr;
}

static void *test_realloc3(void *ptr, size_t amt, const char *file, int line)
{
// TODO: verify pointer to change & amount requested
return realloc(ptr, amt);
_test_alloc3_in_ptr = ptr;
_test_alloc3_in_amt = amt;
_test_alloc3_in_file = file;
_test_alloc3_in_line = line;
_test_alloc3_out_ptr = realloc(ptr, amt);
return _test_alloc3_out_ptr;
}

static void test_dealloc3(void *ptr, const char *file, int line)
{
// TODO: verify pointer requested
_test_alloc3_in_ptr = ptr;
_test_alloc3_in_file = file;
_test_alloc3_in_line = line;
free(ptr);
}

START_TEST(test_cjose_set_allocators_ex)
{
ck_assert(malloc == cjose_get_alloc());
ck_assert(realloc == cjose_get_realloc());
ck_assert(free == cjose_get_dealloc());
ck_assert(cjose_alloc3_default == cjose_get_alloc3());
ck_assert(cjose_realloc3_default == cjose_get_realloc3());
ck_assert(cjose_dealloc3_default == cjose_get_dealloc3());
// Simply verify return ptr is not NULL
ck_assert(NULL != cjose_get_alloc());
ck_assert(NULL != cjose_get_realloc());
ck_assert(NULL != cjose_get_dealloc());
ck_assert(NULL != cjose_get_alloc3());
ck_assert(NULL != cjose_get_realloc3());
ck_assert(NULL != cjose_get_dealloc3());

cjose_set_alloc_ex_funcs(test_alloc3, test_realloc3, test_dealloc3);
ck_assert(cjose_alloc_wrapped == cjose_get_alloc());
ck_assert(cjose_realloc_wrapped == cjose_get_realloc());
ck_assert(cjose_dealloc_wrapped == cjose_get_dealloc());
ck_assert(test_alloc3 == cjose_get_alloc3());
ck_assert(test_realloc3 == cjose_get_realloc3());
ck_assert(test_dealloc3 == cjose_get_dealloc3());
// Simply verify return ptr is not NULL
ck_assert(NULL != cjose_get_alloc());
ck_assert(NULL != cjose_get_realloc());
ck_assert(NULL != cjose_get_dealloc());
ck_assert(NULL != cjose_get_alloc3());
ck_assert(NULL != cjose_get_realloc3());
ck_assert(NULL != cjose_get_dealloc3());

size_t amt;
void *ptr;
void *re_ptr;
const char *file;
int line;

// test extended allocation/reallocation/deallocation redirect
test_alloc3_reset();

amt = 129;
file = __FILE__;
line = __LINE__;
ptr = cjose_get_alloc3()(amt, file, line);
ck_assert(amt == _test_alloc3_in_amt);
ck_assert(ptr == _test_alloc3_out_ptr);
ck_assert(file == _test_alloc3_in_file);
ck_assert(line == _test_alloc3_in_line);

amt = 319;
file = __FILE__;
line = __LINE__;
re_ptr = cjose_get_realloc3()(ptr, amt, file, line);
ck_assert(amt == _test_alloc3_in_amt);
ck_assert(ptr == _test_alloc3_in_ptr);
ck_assert(re_ptr == _test_alloc3_out_ptr);
ck_assert(file == _test_alloc3_in_file);
ck_assert(line == _test_alloc3_in_line);

ptr = re_ptr;
file = __FILE__;
line = __LINE__;
cjose_get_dealloc3()(ptr, file, line);
ck_assert(ptr == _test_alloc3_in_ptr);
ck_assert(file == _test_alloc3_in_file);
ck_assert(line == _test_alloc3_in_line);

// test simple allocation/reallocation/deallocation redirect
test_alloc3_reset();

amt = 129;
ptr = cjose_get_alloc()(amt);
ck_assert(amt == _test_alloc3_in_amt);
ck_assert(ptr == _test_alloc3_out_ptr);
ck_assert(NULL != _test_alloc3_in_file);
ck_assert(0 != _test_alloc3_in_line);

amt = 319;
re_ptr = cjose_get_realloc()(ptr, amt);
ck_assert(amt == _test_alloc3_in_amt);
ck_assert(ptr == _test_alloc3_in_ptr);
ck_assert(re_ptr == _test_alloc3_out_ptr);
ck_assert(NULL != _test_alloc3_in_file);
ck_assert(0 != _test_alloc3_in_line);

ptr = re_ptr;
cjose_get_dealloc()(ptr);
ck_assert(ptr == _test_alloc3_in_ptr);
ck_assert(NULL != _test_alloc3_in_file);
ck_assert(0 != _test_alloc3_in_line);

test_alloc3_reset();

cjose_set_alloc_ex_funcs(NULL, NULL, NULL);
ck_assert(malloc == cjose_get_alloc());
ck_assert(realloc == cjose_get_realloc());
ck_assert(free == cjose_get_dealloc());
ck_assert(cjose_alloc3_default == cjose_get_alloc3());
ck_assert(cjose_realloc3_default == cjose_get_realloc3());
ck_assert(cjose_dealloc3_default == cjose_get_dealloc3());
// Simply verify return ptr is not NULL
ck_assert(NULL != cjose_get_alloc());
ck_assert(NULL != cjose_get_realloc());
ck_assert(NULL != cjose_get_dealloc());
ck_assert(NULL != cjose_get_alloc3());
ck_assert(NULL != cjose_get_realloc3());
ck_assert(NULL != cjose_get_dealloc3());
}
END_TEST

Expand Down

0 comments on commit f02e19c

Please sign in to comment.