Mon, 20 Feb 2012 15:30:45 +0100
new mempool tests
1 /*
2 *
3 */
5 #include "mpool_tests.h"
7 UCX_TEST_BEGIN(test_ucx_mempool_new) {
8 UcxMempool *pool = ucx_mempool_new(16);
10 UCX_TEST_ASSERT(pool->size == 16, "wrong size")
11 UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter")
12 UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed")
14 ucx_mempool_free(pool);
16 UCX_TEST_END
17 }
19 UCX_TEST_BEGIN(test_ucx_mempool_malloc) {
21 UcxMempool *pool = ucx_mempool_new(1);
23 int *test = (int*) ucx_mempool_malloc(pool, sizeof(int));
25 UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented")
26 UCX_TEST_ASSERT(pool->size == 1, "chcap called")
28 int *pooladdr = (int*)((char*)pool->data[0] + sizeof(ucx_destructor));
29 *pooladdr = 5;
31 UCX_TEST_ASSERT(*test == 5, "wrong pointer")
33 ucx_mempool_free(pool);
35 UCX_TEST_END
36 }
38 UCX_TEST_BEGIN(test_ucx_mempool_malloc_with_chcap) {
40 UcxMempool *pool = ucx_mempool_new(1);
42 ucx_mempool_malloc(pool, sizeof(int));
43 int *test = (int*) ucx_mempool_malloc(pool, sizeof(int));
45 UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented")
46 UCX_TEST_ASSERT(pool->size == 17, "chcap not called")
48 int *pooladdr = (int*)((char*)pool->data[1] + sizeof(ucx_destructor));
49 *pooladdr = 5;
51 UCX_TEST_ASSERT(*test == 5, "wrong pointer")
53 ucx_mempool_free(pool);
55 UCX_TEST_END
56 }
58 UCX_TEST_BEGIN(test_ucx_mempool_calloc) {
60 UcxMempool *pool = ucx_mempool_new(1);
62 int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
64 UCX_TEST_ASSERT(test != NULL, "no memory for test data")
65 UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed")
67 ucx_mempool_free(pool);
69 UCX_TEST_END
70 }
72 void test_setdestr(void* elem) {
73 int *cb = (int*) ((int*) elem)[1];
74 *cb = 42;
75 }
77 UCX_TEST_BEGIN(test_ucx_mempool_set_destr) {
79 UcxMempool *pool = ucx_mempool_new(2);
81 ucx_mempool_malloc(pool, sizeof(int));
82 int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
84 int *cb = (int*) malloc(sizeof(int));
85 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
87 test[0] = 5; test[1] = (int) cb;
88 *cb = 13;
90 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] == (int) cb, "setdestr destroyed data")
97 ucx_mempool_free(pool);
99 UCX_TEST_ASSERT(*cb == 42, "destructor not called")
101 free(cb);
103 UCX_TEST_END
104 }
107 UCX_TEST_BEGIN(test_ucx_mempool_reg_destr) {
109 UcxMempool *pool = ucx_mempool_new(1);
111 int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
113 int *cb = (int*) malloc(sizeof(int));
114 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
116 test[0] = 5; test[1] = (int) cb;
117 *cb = 13;
119 ucx_mempool_reg_destr(pool, test, test_setdestr);
121 ucx_destructor *pooladdr = (ucx_destructor*)
122 ((char*)pool->data[1] + sizeof(ucx_destructor));
124 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed")
126 ucx_mempool_free(pool);
128 UCX_TEST_ASSERT(*cb == 42, "destructor not called")
130 free(cb);
132 UCX_TEST_END
133 }
135 UCX_TEST_BEGIN(test_ucx_mempool_realloc) {
137 UcxMempool *pool = ucx_mempool_new(2);
139 ucx_mempool_malloc(pool, sizeof(int));
140 int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
142 int *cb = (int*) malloc(sizeof(int));
143 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
145 test[0] = 5; test[1] = (int) cb;
146 *cb = 13;
148 ucx_mempool_set_destr(test, test_setdestr);
150 int *rtest, n = 2;
151 do {
152 n *= 2;
153 UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc")
154 rtest = ucx_mempool_realloc(pool, test, n*sizeof(int));
155 } while (rtest == test);
156 test = rtest;
158 UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
159 "realloc killed destructor")
160 UCX_TEST_ASSERT(
161 test[0] == 5 && test[1] == (int) cb, "realloc destroyed data")
163 ucx_mempool_free(pool);
165 UCX_TEST_ASSERT(*cb == 42, "destructor not called")
167 free(cb);
169 UCX_TEST_END
170 }