test/mpool_tests.c

Fri, 24 Feb 2012 15:53:50 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 24 Feb 2012 15:53:50 +0100
changeset 30
23bb65cbf7a4
parent 28
1666cbeb1db8
child 32
c7af4ec56e19
permissions
-rw-r--r--

some fixes

     1 /*
     2  *
     3  */
     5 #include <inttypes.h>
     7 #include "mpool_tests.h"
     9 UCX_TEST_BEGIN(test_ucx_mempool_new) {
    10     UcxMempool *pool = ucx_mempool_new(16);
    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")
    16     ucx_mempool_free(pool);
    18     UCX_TEST_END
    19 }
    21 UCX_TEST_BEGIN(test_ucx_mempool_malloc) {
    23     UcxMempool *pool = ucx_mempool_new(1);
    25     int *test = (int*) ucx_mempool_malloc(pool, sizeof(int));
    27     UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented")
    28     UCX_TEST_ASSERT(pool->size == 1, "chcap called")
    30     int *pooladdr = (int*)((char*)pool->data[0] + sizeof(ucx_destructor));
    31     *pooladdr = 5;
    33     UCX_TEST_ASSERT(*test == 5, "wrong pointer")
    35     ucx_mempool_free(pool);
    37     UCX_TEST_END
    38 }
    40 UCX_TEST_BEGIN(test_ucx_mempool_malloc_with_chcap) {
    42     UcxMempool *pool = ucx_mempool_new(1);
    44     ucx_mempool_malloc(pool, sizeof(int));
    45     int *test = (int*) ucx_mempool_malloc(pool, sizeof(int));
    47     UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented")
    48     UCX_TEST_ASSERT(pool->size == 17, "chcap not called")
    50     int *pooladdr = (int*)((char*)pool->data[1] + sizeof(ucx_destructor));
    51     *pooladdr = 5;
    53     UCX_TEST_ASSERT(*test == 5, "wrong pointer")
    55     ucx_mempool_free(pool);
    57     UCX_TEST_END
    58 }
    60 UCX_TEST_BEGIN(test_ucx_mempool_calloc) {
    62     UcxMempool *pool = ucx_mempool_new(1);
    64     int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
    66     UCX_TEST_ASSERT(test != NULL, "no memory for test data")
    67     UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed")
    69     ucx_mempool_free(pool);
    71     UCX_TEST_END
    72 }
    74 void test_setdestr(void* elem) {
    75     intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
    76     *cb = 42;
    77 }
    79 UCX_TEST_BEGIN(test_ucx_mempool_set_destr) {
    81     UcxMempool *pool = ucx_mempool_new(2);
    83     ucx_mempool_malloc(pool, sizeof(int));
    84     intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
    86     int *cb = (intptr_t*) malloc(sizeof(intptr_t));
    87     UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
    89     test[0] = 5; test[1] = (intptr_t) cb;
    90     *cb = 13;
    92     ucx_mempool_set_destr(test, test_setdestr);
    93     UCX_TEST_ASSERT(
    94             *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
    95     UCX_TEST_ASSERT(
    96             test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
    98     ucx_mempool_free(pool);
   100     UCX_TEST_ASSERT(*cb == 42, "destructor not called")
   102     free(cb);
   104     UCX_TEST_END
   105 }
   108 UCX_TEST_BEGIN(test_ucx_mempool_reg_destr) {
   110     UcxMempool *pool = ucx_mempool_new(1);
   112     intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
   114     intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   115     UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
   117     test[0] = 5; test[1] = (intptr_t) cb;
   118     *cb = 13;
   120     ucx_mempool_reg_destr(pool, test, test_setdestr);
   122     ucx_destructor *pooladdr = (ucx_destructor*)
   123             ((char*)pool->data[0] + sizeof(ucx_destructor));
   125     UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed")
   127     ucx_mempool_free(pool);
   128     free(test);
   130     UCX_TEST_ASSERT(*cb == 42, "destructor not called")
   132     free(cb);
   134     UCX_TEST_END
   135 }
   137 UCX_TEST_BEGIN(test_ucx_mempool_realloc) {
   139     UcxMempool *pool = ucx_mempool_new(2);
   141     ucx_mempool_malloc(pool, sizeof(int));
   142     intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
   144     intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
   145     UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
   147     test[0] = 5; test[1] = (intptr_t) cb;
   148     *cb = 13;
   150     ucx_mempool_set_destr(test, test_setdestr);
   152     int *rtest, n = 2;
   153     do {
   154         n *= 2;
   155         UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc")
   156         rtest = ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
   157     } while (rtest == test);
   158     test = rtest;
   160     UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
   161             "realloc killed destructor")
   162     UCX_TEST_ASSERT(
   163             test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
   165     ucx_mempool_free(pool);
   167     UCX_TEST_ASSERT(*cb == 42, "destructor not called")
   169     free(cb);
   171     UCX_TEST_END
   172 }

mercurial