Sat, 28 Oct 2017 15:43:51 +0200
modules documentation
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 #include <inttypes.h>
31 #include "mpool_tests.h"
33 UCX_TEST(test_ucx_mempool_new) {
34 UcxMempool *pool = ucx_mempool_new(16);
35 UCX_TEST_BEGIN
36 UCX_TEST_ASSERT(pool->size == 16, "wrong size");
37 UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter");
38 UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed");
39 UCX_TEST_END
40 ucx_mempool_destroy(pool);
41 }
43 UCX_TEST(test_ucx_mempool_malloc) {
45 UcxMempool *pool = ucx_mempool_new(1);
46 UCX_TEST_BEGIN
47 intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
49 UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented");
50 UCX_TEST_ASSERT(pool->size == 1, "chcap called");
52 intptr_t *pooladdr =
53 (intptr_t*)((char*)pool->data[0] + sizeof(ucx_destructor));
54 *pooladdr = 5;
56 UCX_TEST_ASSERT(*test == 5, "wrong pointer");
58 UCX_TEST_END
59 ucx_mempool_destroy(pool);
60 }
62 UCX_TEST(test_ucx_mempool_malloc_with_chcap) {
64 UcxMempool *pool = ucx_mempool_new(1);
65 UCX_TEST_BEGIN
66 ucx_mempool_malloc(pool, sizeof(int));
67 intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
69 UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented");
70 UCX_TEST_ASSERT(pool->size == 2, "chcap not called");
72 intptr_t *pooladdr =
73 (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor));
74 *pooladdr = 5;
76 UCX_TEST_ASSERT(*test == 5, "wrong pointer");
78 UCX_TEST_END
79 ucx_mempool_destroy(pool);
80 }
82 UCX_TEST(test_ucx_mempool_calloc) {
84 UcxMempool *pool = ucx_mempool_new(1);
85 UCX_TEST_BEGIN
87 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
89 UCX_TEST_ASSERT(test != NULL, "no memory for test data");
90 UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed");
92 UCX_TEST_END
93 ucx_mempool_destroy(pool);
94 }
96 UCX_TEST(test_ucx_mempool_free) {
97 UcxMempool *pool = ucx_mempool_new(16);
98 void *mem1;
99 void *mem2;
101 UCX_TEST_BEGIN
103 mem1 = ucx_mempool_malloc(pool, 16);
104 ucx_mempool_free(pool, mem1);
106 UCX_TEST_ASSERT(pool->ndata == 0, "mempool not empty");
108 ucx_mempool_malloc(pool, 16);
109 ucx_mempool_malloc(pool, 16);
110 mem1 = ucx_mempool_malloc(pool, 16);
111 ucx_mempool_malloc(pool, 16);
112 mem2 = ucx_mempool_malloc(pool, 16);
114 ucx_mempool_free(pool, mem1);
116 UCX_TEST_ASSERT(pool->ndata == 4, "wrong mempool size");
118 ucx_mempool_free(pool, mem2);
120 UCX_TEST_ASSERT(pool->ndata == 3, "wrong mempool size");
122 UCX_TEST_END
123 ucx_mempool_destroy(pool);
124 }
126 #ifdef __cplusplus
127 extern "C"
128 #endif
129 void test_setdestr(void* elem) {
130 intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
131 *cb = 42;
132 }
134 UCX_TEST(test_ucx_mempool_set_destr) {
136 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
137 UCX_TEST_BEGIN
138 UcxMempool *pool = ucx_mempool_new(2);
140 ucx_mempool_malloc(pool, sizeof(intptr_t));
141 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
143 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
145 test[0] = 5; test[1] = (intptr_t) cb;
146 *cb = 13;
148 ucx_mempool_set_destr(test, test_setdestr);
149 UCX_TEST_ASSERT(
150 *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
151 UCX_TEST_ASSERT(
152 test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
154 ucx_mempool_destroy(pool);
156 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
158 UCX_TEST_END
159 if (cb != NULL) free(cb);
160 }
163 UCX_TEST(test_ucx_mempool_reg_destr) {
165 intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
166 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
167 UCX_TEST_BEGIN
168 UcxMempool *pool = ucx_mempool_new(1);
170 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
172 test[0] = 5; test[1] = (intptr_t) cb;
173 *cb = 13;
175 ucx_mempool_reg_destr(pool, test, test_setdestr);
177 ucx_destructor *pooladdr = (ucx_destructor*)
178 ((char*)pool->data[0] + sizeof(ucx_destructor));
180 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed");
182 ucx_mempool_destroy(pool);
183 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
184 UCX_TEST_END
186 if (test != NULL) free(test);
187 if (cb != NULL) free(cb);
188 }
190 UCX_TEST(test_ucx_mempool_realloc) {
192 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
193 UCX_TEST_BEGIN
194 UcxMempool *pool = ucx_mempool_new(2);
196 ucx_mempool_malloc(pool, sizeof(intptr_t));
197 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
199 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
201 test[0] = 5; test[1] = (intptr_t) cb;
202 *cb = 13;
204 ucx_mempool_set_destr(test, test_setdestr);
206 intptr_t *rtest, n = 2;
207 do {
208 n *= 2;
209 UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc");
210 rtest = (intptr_t*) ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
211 } while (rtest == test);
212 test = rtest;
214 UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
215 "realloc killed destructor")
216 UCX_TEST_ASSERT(
217 test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
219 ucx_mempool_destroy(pool);
221 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
223 UCX_TEST_END
224 if (cb != NULL) free(cb);
225 }