test/mpool_tests.c

Fri, 12 Oct 2012 10:54:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Oct 2012 10:54:55 +0200
changeset 69
fb59270b1de3
parent 40
583718dd4cf3
child 103
08018864fb91
permissions
-rw-r--r--

made the code work with VC++ compiler (use make CONF=windows)

     1 /*
     2  *
     3  */
     5 #include <stdint.h>
     7 #include "mpool_tests.h"
     9 UCX_TEST_IMPLEMENT(test_ucx_mempool_new) {
    10     UcxMempool *pool = ucx_mempool_new(16);
    11     UCX_TEST_BEGIN
    12     UCX_TEST_ASSERT(pool->size == 16, "wrong size");
    13     UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter");
    14     UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed");
    15     UCX_TEST_END
    16     ucx_mempool_free(pool);
    17 }
    19 UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc) {
    21     UcxMempool *pool = ucx_mempool_new(1);
    22     UCX_TEST_BEGIN
    23     intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
    25     UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented");
    26     UCX_TEST_ASSERT(pool->size == 1, "chcap called");
    28     intptr_t *pooladdr =
    29             (intptr_t*)((char*)pool->data[0] + sizeof(ucx_destructor));
    30     *pooladdr = 5;
    32     UCX_TEST_ASSERT(*test == 5, "wrong pointer");
    34     UCX_TEST_END
    35     ucx_mempool_free(pool);
    36 }
    38 UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc_with_chcap) {
    40     UcxMempool *pool = ucx_mempool_new(1);
    41     UCX_TEST_BEGIN
    42     ucx_mempool_malloc(pool, sizeof(int));
    43     intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
    45     UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented");
    46     UCX_TEST_ASSERT(pool->size == 17, "chcap not called");
    48     intptr_t *pooladdr =
    49             (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor));
    50     *pooladdr = 5;
    52     UCX_TEST_ASSERT(*test == 5, "wrong pointer");
    54     UCX_TEST_END
    55     ucx_mempool_free(pool);
    56 }
    58 UCX_TEST_IMPLEMENT(test_ucx_mempool_calloc) {
    60     UcxMempool *pool = ucx_mempool_new(1);
    61     UCX_TEST_BEGIN
    63     intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
    65     UCX_TEST_ASSERT(test != NULL, "no memory for test data");
    66     UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed");
    68     UCX_TEST_END
    69     ucx_mempool_free(pool);
    70 }
    72 void test_setdestr(void* elem) {
    73     intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
    74     *cb = 42;
    75 }
    77 UCX_TEST_IMPLEMENT(test_ucx_mempool_set_destr) {
    79     intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
    80     UCX_TEST_BEGIN
    81     UcxMempool *pool = ucx_mempool_new(2);
    83     ucx_mempool_malloc(pool, sizeof(intptr_t));
    84     intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
    86     UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
    88     test[0] = 5; test[1] = (intptr_t) cb;
    89     *cb = 13;
    91     ucx_mempool_set_destr(test, test_setdestr);
    92     UCX_TEST_ASSERT(
    93             *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
    94     UCX_TEST_ASSERT(
    95             test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
    97     ucx_mempool_free(pool);
    99     UCX_TEST_ASSERT(*cb == 42, "destructor not called");
   101     UCX_TEST_END
   102     if (cb != NULL) free(cb);
   103 }
   106 UCX_TEST_IMPLEMENT(test_ucx_mempool_reg_destr) {
   108     intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
   109     intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   110     UCX_TEST_BEGIN
   111     UcxMempool *pool = ucx_mempool_new(1);
   113     UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
   115     test[0] = 5; test[1] = (intptr_t) cb;
   116     *cb = 13;
   118     ucx_mempool_reg_destr(pool, test, test_setdestr);
   120     ucx_destructor *pooladdr = (ucx_destructor*)
   121             ((char*)pool->data[0] + sizeof(ucx_destructor));
   123     UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed");
   125     ucx_mempool_free(pool);
   126     UCX_TEST_ASSERT(*cb == 42, "destructor not called");
   127     UCX_TEST_END
   129     if (test != NULL) free(test);
   130     if (cb != NULL) free(cb);
   131 }
   133 UCX_TEST_IMPLEMENT(test_ucx_mempool_realloc) {
   135     intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   136     UCX_TEST_BEGIN
   137     UcxMempool *pool = ucx_mempool_new(2);
   139     ucx_mempool_malloc(pool, sizeof(intptr_t));
   140     intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
   142     UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
   144     test[0] = 5; test[1] = (intptr_t) cb;
   145     *cb = 13;
   147     ucx_mempool_set_destr(test, test_setdestr);
   149     intptr_t *rtest, n = 2;
   150     do {
   151         n *= 2;
   152         UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc");
   153         rtest = (intptr_t*) ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
   154     } while (rtest == test);
   155     test = rtest;
   157     UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
   158             "realloc killed destructor")
   159     UCX_TEST_ASSERT(
   160             test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
   162     ucx_mempool_free(pool);
   164     UCX_TEST_ASSERT(*cb == 42, "destructor not called");
   166     UCX_TEST_END
   167     if (cb != NULL) free(cb);
   168 }

mercurial