Initial simple block allocator tests, not being run by default because they're

failing. I suspect it has to do with my lack of understanding of glib's unit
test framework, not the code being tested.

svn path=/trunk/; revision=48519
This commit is contained in:
Evan Huus 2013-03-24 00:45:32 +00:00
parent fc5b86f6d4
commit 8ccea72f10
3 changed files with 93 additions and 25 deletions

View File

@ -167,15 +167,12 @@ typedef struct _wmem_block_allocator_t {
wmem_block_chunk_t *free_insert_point;
} wmem_block_allocator_t;
/* DEBUG */
#ifdef WMEM_DEBUG
#include <stdio.h>
/* DEBUG AND TEST */
static void
wmem_block_debug_chunk_chain(wmem_block_chunk_t *chunk)
wmem_block_verify_chunk_chain(wmem_block_chunk_t *chunk)
{
guint32 total_len = 0;
printf("VERIFYING CHUNK CHAIN\n");
g_assert(chunk->prev == 0);
do {
@ -192,13 +189,12 @@ wmem_block_debug_chunk_chain(wmem_block_chunk_t *chunk)
}
static void
wmem_block_debug_free_list(wmem_block_allocator_t *allocator)
wmem_block_verify_free_list(wmem_block_allocator_t *allocator)
{
gboolean seen_insert_point = FALSE;
wmem_block_chunk_t *free_list;
wmem_block_free_t *free_head;
printf("VERIFYING FREE LIST\n");
if (allocator->free_insert_point == NULL) {
seen_insert_point = TRUE;
}
@ -223,7 +219,34 @@ wmem_block_debug_free_list(wmem_block_allocator_t *allocator)
g_assert(seen_insert_point);
}
#endif
void
wmem_block_verify(wmem_allocator_t *allocator)
{
GSList *tmp;
wmem_block_allocator_t *private_allocator;
/* Normally it would be bad for an allocator helper function to depend
* on receiving the right type of allocator, but this is for testing only
* and is not part of any real API. */
g_assert(allocator->type == WMEM_ALLOCATOR_BLOCK);
private_allocator = (wmem_block_allocator_t*) allocator->private_data;
if (private_allocator->block_list == NULL) {
g_assert(! private_allocator->free_list_head);
g_assert(! private_allocator->free_insert_point);
return;
}
wmem_block_verify_free_list(private_allocator);
tmp = private_allocator->block_list;
while (tmp) {
wmem_block_verify_chunk_chain((wmem_block_chunk_t *)tmp->data);
tmp = tmp->next;
}
}
/* HELPERS */
@ -746,10 +769,6 @@ wmem_block_free_all(void *private_data)
wmem_block_allocator_t *allocator = (wmem_block_allocator_t*) private_data;
GSList *tmp;
#ifdef WMEM_DEBUG
wmem_block_debug_free_list(allocator);
#endif
/* the existing free list is entirely irrelevant */
allocator->free_list_head = NULL;
allocator->free_insert_point = NULL;
@ -758,9 +777,6 @@ wmem_block_free_all(void *private_data)
tmp = allocator->block_list;
while (tmp) {
#ifdef WMEM_DEBUG
wmem_block_debug_chunk_chain((wmem_block_chunk_t *) tmp->data);
#endif
wmem_block_init_block(allocator, tmp->data);
tmp = tmp->next;
}
@ -778,17 +794,9 @@ wmem_block_gc(void *private_data)
* block list. */
tmp = allocator->block_list;
#ifdef WMEM_DEBUG
wmem_block_debug_free_list(allocator);
#endif
while (tmp) {
chunk = (wmem_block_chunk_t *) tmp->data;
#ifdef WMEM_DEBUG
wmem_block_debug_chunk_chain(chunk);
#endif
if (!chunk->used && chunk->last) {
/* if the first chunk is also the last, and is unused, then
* the block as a whole is entirely unused, so remove it from the

View File

@ -35,6 +35,10 @@ extern "C" {
wmem_allocator_t *
wmem_block_allocator_new(void);
/* Exposed only for testing purposes */
void
wmem_block_verify(wmem_allocator_t *allocator);
#ifdef __cplusplus
}
#endif /* __cplusplus */

View File

@ -23,10 +23,66 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
int main(void);
#include <glib.h>
#include "wmem.h"
#include "wmem_allocator.h"
#include "wmem_allocator_block.h"
#include "config.h"
int main(void)
typedef struct _wmem_test_fixture_t {
wmem_allocator_t *allocator;
} wmem_test_fixture_t;
static void
wmem_test_block_allocator_setup(wmem_test_fixture_t *fixture,
const void *extra _U_)
{
/* we call the functions direct to ensure our type doesn't get overridden */
fixture->allocator = wmem_block_allocator_new();
fixture->allocator->type = WMEM_ALLOCATOR_BLOCK;
}
static void
wmem_test_teardown(wmem_test_fixture_t *fixture, const void *extra _U_)
{
wmem_destroy_allocator(fixture->allocator);
}
static void
wmem_test_block_allocator(wmem_test_fixture_t *fixture, const void *extra _U_)
{
char *ptrs[1024];
int i;
wmem_block_verify(fixture->allocator);
for (i=0; i<1024; i++) ptrs[i] = wmem_alloc(fixture->allocator, 8);
for (i=0; i<1024; i++) wmem_free(fixture->allocator, ptrs[i]);
wmem_block_verify(fixture->allocator);
for (i=0; i<1024; i++) ptrs[i] = wmem_alloc(fixture->allocator, 64);
for (i=0; i<1024; i++) wmem_free(fixture->allocator, ptrs[i]);
wmem_block_verify(fixture->allocator);
for (i=0; i<1024; i++) ptrs[i] = wmem_alloc(fixture->allocator, 512);
for (i=0; i<1024; i++) wmem_free(fixture->allocator, ptrs[i]);
wmem_block_verify(fixture->allocator);
}
int
main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
g_test_add("/wmem/block-allocator", wmem_test_fixture_t, NULL,
wmem_test_block_allocator_setup,
wmem_test_block_allocator,
wmem_test_teardown);
/* return g_test_run(); */
return 0;
}