Skip to content

Commit

Permalink
Validate that allocator was actually used
Browse files Browse the repository at this point in the history
  • Loading branch information
asweeney86 committed Dec 21, 2017
1 parent f9e77c5 commit bd73c13
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions test/check_an_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,49 @@ struct node {
AN_ARRAY(node, test);
AN_ARRAY_PRIMITIVE(int, test2);

static struct allocator_stats {
size_t n_calloc;
size_t n_realloc;
size_t n_free;
} allocator_stats;

static void *
check_calloc(const void *ctx, size_t nmemb, size_t size, void *return_addr)
{
(void)ctx;
(void)return_addr;

allocator_stats.n_calloc++;
return calloc(nmemb, size);
}

static void *
check_malloc(const void *ctx, size_t size, void *return_addr)
{
return check_calloc(ctx, 1, size, return_addr);
}

static void *
check_realloc(const void *ctx, void *address, size_t size_from, size_t size_to, void *return_addr)
{
(void)ctx;
(void)size_from;
(void)return_addr;

allocator_stats.n_realloc++;
return realloc(address, size_to);
}

static void
check_free(const void *ctx, void *ptr, void *return_addr)
{
(void)ctx;
(void)return_addr;

allocator_stats.n_free++;
free(ptr);
}

START_TEST(stack_generic)
{
AN_ARRAY_INSTANCE(test) array;
Expand Down Expand Up @@ -47,6 +90,8 @@ START_TEST(stack_generic)

AN_ARRAY_PUSH(test, &array, &entry[0]);
fail_if(array.capacity != 1024);

AN_ARRAY_DEINIT(test, &array);
}
END_TEST

Expand All @@ -72,6 +117,8 @@ START_TEST(stack)
int x = 0;
AN_ARRAY_PUSH(test2, &array, &x);
fail_if(array.capacity != 1024);

AN_ARRAY_DEINIT(test2, &array);
}
END_TEST

Expand All @@ -96,6 +143,8 @@ START_TEST(find_element)
fail_if(!AN_ARRAY_MEMBER(test2, &array, &i), "Could not find a valid member");
}
fail_if(AN_ARRAY_MEMBER(test2, &array, &x), "Found a member that does not belong");

AN_ARRAY_DEINIT(test2, &array);
}
END_TEST

Expand All @@ -113,6 +162,8 @@ START_TEST(remove_element)
fail_if(AN_ARRAY_MEMBER(test2, &array, &i), "Found a deleted member");
fail_if((int)AN_ARRAY_LENGTH(test2, &array) != 1023-i,"Remove member failed");
}

AN_ARRAY_DEINIT(test2, &array);
}
END_TEST

Expand Down Expand Up @@ -160,6 +211,9 @@ START_TEST(remove_order)
"Previous element [%d] = %d is incorrect", to_remove_order - 1, to_remove_order - 1);
fail_if(*AN_ARRAY_VALUE(test2, &array, to_remove_order) != (to_remove_order + 1),
"Next element [%d] = %d is incorrect", to_remove_order, to_remove_order + 1);


AN_ARRAY_DEINIT(test2, &array);
}
END_TEST

Expand All @@ -177,6 +231,26 @@ END_TEST

START_TEST(allocator)
{
struct an_allocator check_allocator = {
.malloc = check_malloc,
.calloc = check_calloc,
.realloc = check_realloc,
.free = check_free
};

memset(&allocator_stats, 0, sizeof(allocator_stats));

an_array_set_allocator(&check_allocator);

AN_ARRAY_INSTANCE(test2) array;
AN_ARRAY_INIT(test2, &array, 2);
AN_ARRAY_RESIZE(test2, &array, 128);
AN_ARRAY_DEINIT(test2, &array);

ck_assert(allocator_stats.n_calloc > 0);
ck_assert(allocator_stats.n_realloc > 0);
ck_assert(allocator_stats.n_free > 0);

an_array_set_allocator(an_default_allocator());
}
END_TEST
Expand Down

0 comments on commit bd73c13

Please sign in to comment.