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)

olaf@13 1 /*
olaf@13 2 *
olaf@13 3 */
olaf@13 4
universe@69 5 #include <stdint.h>
olaf@30 6
olaf@13 7 #include "mpool_tests.h"
olaf@13 8
universe@33 9 UCX_TEST_IMPLEMENT(test_ucx_mempool_new) {
universe@28 10 UcxMempool *pool = ucx_mempool_new(16);
universe@33 11 UCX_TEST_BEGIN
universe@40 12 UCX_TEST_ASSERT(pool->size == 16, "wrong size");
universe@40 13 UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter");
universe@40 14 UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed");
universe@33 15 UCX_TEST_END
universe@28 16 ucx_mempool_free(pool);
olaf@13 17 }
olaf@13 18
universe@33 19 UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc) {
universe@28 20
universe@28 21 UcxMempool *pool = ucx_mempool_new(1);
universe@33 22 UCX_TEST_BEGIN
universe@32 23 intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
universe@28 24
universe@40 25 UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented");
universe@40 26 UCX_TEST_ASSERT(pool->size == 1, "chcap called");
universe@28 27
universe@32 28 intptr_t *pooladdr =
universe@32 29 (intptr_t*)((char*)pool->data[0] + sizeof(ucx_destructor));
universe@28 30 *pooladdr = 5;
universe@28 31
universe@40 32 UCX_TEST_ASSERT(*test == 5, "wrong pointer");
universe@28 33
universe@33 34 UCX_TEST_END
universe@28 35 ucx_mempool_free(pool);
olaf@13 36 }
olaf@13 37
universe@33 38 UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc_with_chcap) {
universe@28 39
universe@28 40 UcxMempool *pool = ucx_mempool_new(1);
universe@33 41 UCX_TEST_BEGIN
universe@28 42 ucx_mempool_malloc(pool, sizeof(int));
universe@32 43 intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
universe@28 44
universe@40 45 UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented");
universe@40 46 UCX_TEST_ASSERT(pool->size == 17, "chcap not called");
universe@28 47
universe@32 48 intptr_t *pooladdr =
universe@32 49 (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor));
universe@28 50 *pooladdr = 5;
universe@28 51
universe@40 52 UCX_TEST_ASSERT(*test == 5, "wrong pointer");
universe@28 53
universe@33 54 UCX_TEST_END
universe@28 55 ucx_mempool_free(pool);
universe@28 56 }
universe@19 57
universe@33 58 UCX_TEST_IMPLEMENT(test_ucx_mempool_calloc) {
universe@28 59
universe@28 60 UcxMempool *pool = ucx_mempool_new(1);
universe@33 61 UCX_TEST_BEGIN
universe@28 62
universe@32 63 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
universe@28 64
universe@40 65 UCX_TEST_ASSERT(test != NULL, "no memory for test data");
universe@40 66 UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed");
universe@28 67
universe@33 68 UCX_TEST_END
universe@28 69 ucx_mempool_free(pool);
universe@28 70 }
olaf@13 71
universe@28 72 void test_setdestr(void* elem) {
olaf@30 73 intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
universe@28 74 *cb = 42;
universe@28 75 }
olaf@13 76
universe@33 77 UCX_TEST_IMPLEMENT(test_ucx_mempool_set_destr) {
universe@28 78
universe@33 79 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
universe@33 80 UCX_TEST_BEGIN
universe@28 81 UcxMempool *pool = ucx_mempool_new(2);
universe@28 82
universe@32 83 ucx_mempool_malloc(pool, sizeof(intptr_t));
olaf@30 84 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
universe@28 85
universe@40 86 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
universe@28 87
olaf@30 88 test[0] = 5; test[1] = (intptr_t) cb;
universe@28 89 *cb = 13;
universe@28 90
universe@28 91 ucx_mempool_set_destr(test, test_setdestr);
universe@28 92 UCX_TEST_ASSERT(
universe@28 93 *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
universe@28 94 UCX_TEST_ASSERT(
olaf@30 95 test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
universe@28 96
universe@28 97 ucx_mempool_free(pool);
universe@28 98
universe@40 99 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
universe@28 100
universe@28 101 UCX_TEST_END
universe@33 102 if (cb != NULL) free(cb);
universe@28 103 }
olaf@13 104
olaf@13 105
universe@33 106 UCX_TEST_IMPLEMENT(test_ucx_mempool_reg_destr) {
olaf@14 107
universe@33 108 intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
universe@33 109 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
universe@33 110 UCX_TEST_BEGIN
universe@28 111 UcxMempool *pool = ucx_mempool_new(1);
universe@28 112
universe@40 113 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
universe@28 114
olaf@30 115 test[0] = 5; test[1] = (intptr_t) cb;
universe@28 116 *cb = 13;
universe@28 117
universe@28 118 ucx_mempool_reg_destr(pool, test, test_setdestr);
universe@28 119
universe@28 120 ucx_destructor *pooladdr = (ucx_destructor*)
olaf@30 121 ((char*)pool->data[0] + sizeof(ucx_destructor));
universe@28 122
universe@40 123 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed");
universe@28 124
universe@28 125 ucx_mempool_free(pool);
universe@40 126 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
universe@33 127 UCX_TEST_END
olaf@13 128
universe@33 129 if (test != NULL) free(test);
universe@33 130 if (cb != NULL) free(cb);
universe@28 131 }
olaf@13 132
universe@33 133 UCX_TEST_IMPLEMENT(test_ucx_mempool_realloc) {
universe@33 134
universe@33 135 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
universe@33 136 UCX_TEST_BEGIN
universe@28 137 UcxMempool *pool = ucx_mempool_new(2);
universe@28 138
universe@32 139 ucx_mempool_malloc(pool, sizeof(intptr_t));
olaf@30 140 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
universe@33 141
universe@40 142 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
universe@28 143
olaf@30 144 test[0] = 5; test[1] = (intptr_t) cb;
universe@28 145 *cb = 13;
universe@28 146
universe@28 147 ucx_mempool_set_destr(test, test_setdestr);
universe@28 148
universe@69 149 intptr_t *rtest, n = 2;
universe@28 150 do {
universe@28 151 n *= 2;
universe@40 152 UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc");
universe@69 153 rtest = (intptr_t*) ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
universe@28 154 } while (rtest == test);
universe@28 155 test = rtest;
universe@28 156
universe@28 157 UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
universe@28 158 "realloc killed destructor")
universe@28 159 UCX_TEST_ASSERT(
olaf@30 160 test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
universe@28 161
universe@28 162 ucx_mempool_free(pool);
universe@28 163
universe@40 164 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
universe@28 165
universe@28 166 UCX_TEST_END
universe@33 167 if (cb != NULL) free(cb);
olaf@13 168 }

mercurial