test/mpool_tests.c

Mon, 20 Feb 2012 15:30:45 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 20 Feb 2012 15:30:45 +0100
changeset 28
1666cbeb1db8
parent 19
cdd7a3173249
child 30
23bb65cbf7a4
permissions
-rw-r--r--

new mempool tests

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

mercurial