test/mpool_tests.c

Fri, 25 May 2012 17:39:27 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 25 May 2012 17:39:27 +0200
changeset 31
91ac86557290
parent 30
23bb65cbf7a4
child 32
c7af4ec56e19
permissions
-rw-r--r--

added map iterator

/*
 *
 */

#include <inttypes.h>

#include "mpool_tests.h"

UCX_TEST_BEGIN(test_ucx_mempool_new) {
    UcxMempool *pool = ucx_mempool_new(16);
    
    UCX_TEST_ASSERT(pool->size == 16, "wrong size")
    UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter")
    UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed")
    
    ucx_mempool_free(pool);
    
    UCX_TEST_END
}

UCX_TEST_BEGIN(test_ucx_mempool_malloc) {
    
    UcxMempool *pool = ucx_mempool_new(1);
    
    int *test = (int*) ucx_mempool_malloc(pool, sizeof(int));
    
    UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented")
    UCX_TEST_ASSERT(pool->size == 1, "chcap called")
    
    int *pooladdr = (int*)((char*)pool->data[0] + sizeof(ucx_destructor));
    *pooladdr = 5;
    
    UCX_TEST_ASSERT(*test == 5, "wrong pointer")
    
    ucx_mempool_free(pool);
    
    UCX_TEST_END
}

UCX_TEST_BEGIN(test_ucx_mempool_malloc_with_chcap) {
    
    UcxMempool *pool = ucx_mempool_new(1);
    
    ucx_mempool_malloc(pool, sizeof(int));
    int *test = (int*) ucx_mempool_malloc(pool, sizeof(int));
    
    UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented")
    UCX_TEST_ASSERT(pool->size == 17, "chcap not called")
    
    int *pooladdr = (int*)((char*)pool->data[1] + sizeof(ucx_destructor));
    *pooladdr = 5;
    
    UCX_TEST_ASSERT(*test == 5, "wrong pointer")
    
    ucx_mempool_free(pool);
    
    UCX_TEST_END
}

UCX_TEST_BEGIN(test_ucx_mempool_calloc) {
    
    UcxMempool *pool = ucx_mempool_new(1);
    
    int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
    
    UCX_TEST_ASSERT(test != NULL, "no memory for test data")
    UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed")
    
    ucx_mempool_free(pool);
    
    UCX_TEST_END
}

void test_setdestr(void* elem) {
    intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
    *cb = 42;
}

UCX_TEST_BEGIN(test_ucx_mempool_set_destr) {
    
    UcxMempool *pool = ucx_mempool_new(2);
    
    ucx_mempool_malloc(pool, sizeof(int));
    intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
    
    int *cb = (intptr_t*) malloc(sizeof(intptr_t));
    UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
    
    test[0] = 5; test[1] = (intptr_t) cb;
    *cb = 13;
    
    ucx_mempool_set_destr(test, test_setdestr);
    UCX_TEST_ASSERT(
            *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
    UCX_TEST_ASSERT(
            test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
    
    ucx_mempool_free(pool);
    
    UCX_TEST_ASSERT(*cb == 42, "destructor not called")

    free(cb);
    
    UCX_TEST_END
}


UCX_TEST_BEGIN(test_ucx_mempool_reg_destr) {
    
    UcxMempool *pool = ucx_mempool_new(1);
    
    intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
    
    intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
    UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
    
    test[0] = 5; test[1] = (intptr_t) cb;
    *cb = 13;
    
    ucx_mempool_reg_destr(pool, test, test_setdestr);
    
    ucx_destructor *pooladdr = (ucx_destructor*)
            ((char*)pool->data[0] + sizeof(ucx_destructor));
    
    UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed")
    
    ucx_mempool_free(pool);
    free(test);
    
    UCX_TEST_ASSERT(*cb == 42, "destructor not called")

    free(cb);
    
    UCX_TEST_END
}

UCX_TEST_BEGIN(test_ucx_mempool_realloc) {
    
    UcxMempool *pool = ucx_mempool_new(2);
    
    ucx_mempool_malloc(pool, sizeof(int));
    intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
    
    intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
    UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
    
    test[0] = 5; test[1] = (intptr_t) cb;
    *cb = 13;
    
    ucx_mempool_set_destr(test, test_setdestr);
    
    int *rtest, n = 2;
    do {
        n *= 2;
        UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc")
        rtest = ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
    } while (rtest == test);
    test = rtest;
    
    UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
            "realloc killed destructor")
    UCX_TEST_ASSERT(
            test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
    
    ucx_mempool_free(pool);
    
    UCX_TEST_ASSERT(*cb == 42, "destructor not called")

    free(cb);
    
    UCX_TEST_END
}

mercurial