test/mpool_tests.c

changeset 30
23bb65cbf7a4
parent 28
1666cbeb1db8
child 32
c7af4ec56e19
equal deleted inserted replaced
29:bce0d7f2912b 30:23bb65cbf7a4
1 /* 1 /*
2 * 2 *
3 */ 3 */
4
5 #include <inttypes.h>
4 6
5 #include "mpool_tests.h" 7 #include "mpool_tests.h"
6 8
7 UCX_TEST_BEGIN(test_ucx_mempool_new) { 9 UCX_TEST_BEGIN(test_ucx_mempool_new) {
8 UcxMempool *pool = ucx_mempool_new(16); 10 UcxMempool *pool = ucx_mempool_new(16);
68 70
69 UCX_TEST_END 71 UCX_TEST_END
70 } 72 }
71 73
72 void test_setdestr(void* elem) { 74 void test_setdestr(void* elem) {
73 int *cb = (int*) ((int*) elem)[1]; 75 intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
74 *cb = 42; 76 *cb = 42;
75 } 77 }
76 78
77 UCX_TEST_BEGIN(test_ucx_mempool_set_destr) { 79 UCX_TEST_BEGIN(test_ucx_mempool_set_destr) {
78 80
79 UcxMempool *pool = ucx_mempool_new(2); 81 UcxMempool *pool = ucx_mempool_new(2);
80 82
81 ucx_mempool_malloc(pool, sizeof(int)); 83 ucx_mempool_malloc(pool, sizeof(int));
82 int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int)); 84 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
83 85
84 int *cb = (int*) malloc(sizeof(int)); 86 int *cb = (intptr_t*) malloc(sizeof(intptr_t));
85 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") 87 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
86 88
87 test[0] = 5; test[1] = (int) cb; 89 test[0] = 5; test[1] = (intptr_t) cb;
88 *cb = 13; 90 *cb = 13;
89 91
90 ucx_mempool_set_destr(test, test_setdestr); 92 ucx_mempool_set_destr(test, test_setdestr);
91
92 UCX_TEST_ASSERT( 93 UCX_TEST_ASSERT(
93 *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed") 94 *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
94 UCX_TEST_ASSERT( 95 UCX_TEST_ASSERT(
95 test[0] == 5 && test[1] == (int) cb, "setdestr destroyed data") 96 test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
96 97
97 ucx_mempool_free(pool); 98 ucx_mempool_free(pool);
98 99
99 UCX_TEST_ASSERT(*cb == 42, "destructor not called") 100 UCX_TEST_ASSERT(*cb == 42, "destructor not called")
100 101
106 107
107 UCX_TEST_BEGIN(test_ucx_mempool_reg_destr) { 108 UCX_TEST_BEGIN(test_ucx_mempool_reg_destr) {
108 109
109 UcxMempool *pool = ucx_mempool_new(1); 110 UcxMempool *pool = ucx_mempool_new(1);
110 111
111 int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int)); 112 intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
112 113
113 int *cb = (int*) malloc(sizeof(int)); 114 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
114 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") 115 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
115 116
116 test[0] = 5; test[1] = (int) cb; 117 test[0] = 5; test[1] = (intptr_t) cb;
117 *cb = 13; 118 *cb = 13;
118 119
119 ucx_mempool_reg_destr(pool, test, test_setdestr); 120 ucx_mempool_reg_destr(pool, test, test_setdestr);
120 121
121 ucx_destructor *pooladdr = (ucx_destructor*) 122 ucx_destructor *pooladdr = (ucx_destructor*)
122 ((char*)pool->data[1] + sizeof(ucx_destructor)); 123 ((char*)pool->data[0] + sizeof(ucx_destructor));
123 124
124 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed") 125 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed")
125 126
126 ucx_mempool_free(pool); 127 ucx_mempool_free(pool);
128 free(test);
127 129
128 UCX_TEST_ASSERT(*cb == 42, "destructor not called") 130 UCX_TEST_ASSERT(*cb == 42, "destructor not called")
129 131
130 free(cb); 132 free(cb);
131 133
135 UCX_TEST_BEGIN(test_ucx_mempool_realloc) { 137 UCX_TEST_BEGIN(test_ucx_mempool_realloc) {
136 138
137 UcxMempool *pool = ucx_mempool_new(2); 139 UcxMempool *pool = ucx_mempool_new(2);
138 140
139 ucx_mempool_malloc(pool, sizeof(int)); 141 ucx_mempool_malloc(pool, sizeof(int));
140 int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int)); 142 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
141 143
142 int *cb = (int*) malloc(sizeof(int)); 144 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
143 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") 145 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
144 146
145 test[0] = 5; test[1] = (int) cb; 147 test[0] = 5; test[1] = (intptr_t) cb;
146 *cb = 13; 148 *cb = 13;
147 149
148 ucx_mempool_set_destr(test, test_setdestr); 150 ucx_mempool_set_destr(test, test_setdestr);
149 151
150 int *rtest, n = 2; 152 int *rtest, n = 2;
151 do { 153 do {
152 n *= 2; 154 n *= 2;
153 UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc") 155 UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc")
154 rtest = ucx_mempool_realloc(pool, test, n*sizeof(int)); 156 rtest = ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
155 } while (rtest == test); 157 } while (rtest == test);
156 test = rtest; 158 test = rtest;
157 159
158 UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr, 160 UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
159 "realloc killed destructor") 161 "realloc killed destructor")
160 UCX_TEST_ASSERT( 162 UCX_TEST_ASSERT(
161 test[0] == 5 && test[1] == (int) cb, "realloc destroyed data") 163 test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
162 164
163 ucx_mempool_free(pool); 165 ucx_mempool_free(pool);
164 166
165 UCX_TEST_ASSERT(*cb == 42, "destructor not called") 167 UCX_TEST_ASSERT(*cb == 42, "destructor not called")
166 168

mercurial