olaf@13: /* olaf@13: * olaf@13: */ olaf@13: olaf@30: #include olaf@30: olaf@13: #include "mpool_tests.h" olaf@13: universe@28: UCX_TEST_BEGIN(test_ucx_mempool_new) { universe@28: UcxMempool *pool = ucx_mempool_new(16); universe@28: universe@28: UCX_TEST_ASSERT(pool->size == 16, "wrong size") universe@28: UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter") universe@28: UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed") universe@28: universe@28: ucx_mempool_free(pool); universe@28: universe@28: UCX_TEST_END olaf@13: } olaf@13: universe@28: UCX_TEST_BEGIN(test_ucx_mempool_malloc) { universe@28: universe@28: UcxMempool *pool = ucx_mempool_new(1); universe@28: universe@32: intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t)); universe@28: universe@28: UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented") universe@28: UCX_TEST_ASSERT(pool->size == 1, "chcap called") universe@28: universe@32: intptr_t *pooladdr = universe@32: (intptr_t*)((char*)pool->data[0] + sizeof(ucx_destructor)); universe@28: *pooladdr = 5; universe@28: universe@28: UCX_TEST_ASSERT(*test == 5, "wrong pointer") universe@28: universe@28: ucx_mempool_free(pool); universe@28: universe@28: UCX_TEST_END olaf@13: } olaf@13: universe@28: UCX_TEST_BEGIN(test_ucx_mempool_malloc_with_chcap) { universe@28: universe@28: UcxMempool *pool = ucx_mempool_new(1); universe@28: universe@28: ucx_mempool_malloc(pool, sizeof(int)); universe@32: intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t)); universe@28: universe@28: UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented") universe@28: UCX_TEST_ASSERT(pool->size == 17, "chcap not called") universe@28: universe@32: intptr_t *pooladdr = universe@32: (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor)); universe@28: *pooladdr = 5; universe@28: universe@28: UCX_TEST_ASSERT(*test == 5, "wrong pointer") universe@28: universe@28: ucx_mempool_free(pool); universe@28: universe@28: UCX_TEST_END universe@28: } universe@19: universe@28: UCX_TEST_BEGIN(test_ucx_mempool_calloc) { universe@28: universe@28: UcxMempool *pool = ucx_mempool_new(1); universe@28: universe@32: intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); universe@28: universe@28: UCX_TEST_ASSERT(test != NULL, "no memory for test data") universe@28: UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed") universe@28: universe@28: ucx_mempool_free(pool); universe@28: universe@28: UCX_TEST_END universe@28: } olaf@13: universe@28: void test_setdestr(void* elem) { olaf@30: intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1]; universe@28: *cb = 42; universe@28: } olaf@13: universe@28: UCX_TEST_BEGIN(test_ucx_mempool_set_destr) { universe@28: universe@28: UcxMempool *pool = ucx_mempool_new(2); universe@28: universe@32: ucx_mempool_malloc(pool, sizeof(intptr_t)); olaf@30: intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); universe@28: universe@32: intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); universe@28: UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") universe@28: olaf@30: test[0] = 5; test[1] = (intptr_t) cb; universe@28: *cb = 13; universe@28: universe@28: ucx_mempool_set_destr(test, test_setdestr); universe@28: UCX_TEST_ASSERT( universe@28: *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed") universe@28: UCX_TEST_ASSERT( olaf@30: test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data") universe@28: universe@28: ucx_mempool_free(pool); universe@28: universe@28: UCX_TEST_ASSERT(*cb == 42, "destructor not called") olaf@13: universe@28: free(cb); universe@28: universe@28: UCX_TEST_END universe@28: } olaf@13: olaf@13: universe@28: UCX_TEST_BEGIN(test_ucx_mempool_reg_destr) { olaf@14: universe@28: UcxMempool *pool = ucx_mempool_new(1); universe@28: olaf@30: intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t)); universe@28: olaf@30: intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); universe@28: UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") universe@28: olaf@30: test[0] = 5; test[1] = (intptr_t) cb; universe@28: *cb = 13; universe@28: universe@28: ucx_mempool_reg_destr(pool, test, test_setdestr); universe@28: universe@28: ucx_destructor *pooladdr = (ucx_destructor*) olaf@30: ((char*)pool->data[0] + sizeof(ucx_destructor)); universe@28: universe@28: UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed") universe@28: universe@28: ucx_mempool_free(pool); olaf@30: free(test); universe@28: universe@28: UCX_TEST_ASSERT(*cb == 42, "destructor not called") olaf@13: universe@28: free(cb); olaf@13: universe@28: UCX_TEST_END universe@28: } olaf@13: universe@28: UCX_TEST_BEGIN(test_ucx_mempool_realloc) { universe@28: universe@28: UcxMempool *pool = ucx_mempool_new(2); universe@28: universe@32: ucx_mempool_malloc(pool, sizeof(intptr_t)); olaf@30: intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); universe@28: olaf@30: intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); universe@28: UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") universe@28: olaf@30: test[0] = 5; test[1] = (intptr_t) cb; universe@28: *cb = 13; universe@28: universe@28: ucx_mempool_set_destr(test, test_setdestr); universe@28: universe@28: int *rtest, n = 2; universe@28: do { universe@28: n *= 2; universe@28: UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc") olaf@30: rtest = ucx_mempool_realloc(pool, test, n*sizeof(intptr_t)); universe@28: } while (rtest == test); universe@28: test = rtest; universe@28: universe@28: UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr, universe@28: "realloc killed destructor") universe@28: UCX_TEST_ASSERT( olaf@30: test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data") universe@28: universe@28: ucx_mempool_free(pool); universe@28: universe@28: UCX_TEST_ASSERT(*cb == 42, "destructor not called") universe@28: universe@28: free(cb); universe@28: universe@28: UCX_TEST_END olaf@13: }