test/mpool_tests.c

changeset 390
d345541018fa
parent 270
3d80d425543b
equal deleted inserted replaced
389:92e482410453 390:d345541018fa
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 */
28
29 #include <inttypes.h>
30
31 #include "mpool_tests.h"
32
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 }
42
43 UCX_TEST(test_ucx_mempool_malloc) {
44
45 UcxMempool *pool = ucx_mempool_new(1);
46 UCX_TEST_BEGIN
47 intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
48
49 UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented");
50 UCX_TEST_ASSERT(pool->size == 1, "chcap called");
51
52 intptr_t *pooladdr =
53 (intptr_t*)((char*)pool->data[0] + sizeof(ucx_destructor));
54 *pooladdr = 5;
55
56 UCX_TEST_ASSERT(*test == 5, "wrong pointer");
57
58 UCX_TEST_END
59 ucx_mempool_destroy(pool);
60 }
61
62 UCX_TEST(test_ucx_mempool_malloc_with_chcap) {
63
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));
68
69 UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented");
70 UCX_TEST_ASSERT(pool->size == 2, "chcap not called");
71
72 intptr_t *pooladdr =
73 (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor));
74 *pooladdr = 5;
75
76 UCX_TEST_ASSERT(*test == 5, "wrong pointer");
77
78 // overflow test
79 void *n0 = ucx_mempool_malloc(pool, (size_t)-1);
80 void *n1 = ucx_mempool_malloc(pool, ((size_t)-1) - sizeof(void*)/2);
81
82 UCX_TEST_ASSERT(n0 == NULL, "should not allocate SIZE_MAX bytes");
83 UCX_TEST_ASSERT(n1 == NULL, "should detect integer overflow");
84
85 UCX_TEST_END
86 ucx_mempool_destroy(pool);
87 }
88
89 UCX_TEST(test_ucx_mempool_calloc) {
90
91 UcxMempool *pool = ucx_mempool_new(1);
92 UCX_TEST_BEGIN
93
94 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
95
96 UCX_TEST_ASSERT(test != NULL, "no memory for test data");
97 UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed");
98
99 // overflow test
100 void *n0 = ucx_mempool_calloc(pool, (size_t)-1, 1);
101 void *n1 = ucx_mempool_calloc(pool, ((size_t)-1)/2, 3);
102
103 UCX_TEST_ASSERT(n0 == NULL, "should not allocate SIZE_MAX bytes");
104 UCX_TEST_ASSERT(n1 == NULL, "should detect integer overflow");
105
106 UCX_TEST_END
107 ucx_mempool_destroy(pool);
108 }
109
110 UCX_TEST(test_ucx_mempool_free) {
111 UcxMempool *pool = ucx_mempool_new(16);
112 void *mem1;
113 void *mem2;
114
115 UCX_TEST_BEGIN
116
117 mem1 = ucx_mempool_malloc(pool, 16);
118 ucx_mempool_free(pool, mem1);
119
120 UCX_TEST_ASSERT(pool->ndata == 0, "mempool not empty");
121
122 ucx_mempool_malloc(pool, 16);
123 ucx_mempool_malloc(pool, 16);
124 mem1 = ucx_mempool_malloc(pool, 16);
125 ucx_mempool_malloc(pool, 16);
126 mem2 = ucx_mempool_malloc(pool, 16);
127
128 ucx_mempool_free(pool, mem1);
129
130 UCX_TEST_ASSERT(pool->ndata == 4, "wrong mempool size");
131
132 ucx_mempool_free(pool, mem2);
133
134 UCX_TEST_ASSERT(pool->ndata == 3, "wrong mempool size");
135
136 UCX_TEST_END
137 ucx_mempool_destroy(pool);
138 }
139
140 #ifdef __cplusplus
141 extern "C"
142 #endif
143 void test_setdestr(void* elem) {
144 intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
145 *cb = 42;
146 }
147
148 UCX_TEST(test_ucx_mempool_set_destr) {
149
150 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
151 UCX_TEST_BEGIN
152 UcxMempool *pool = ucx_mempool_new(2);
153
154 ucx_mempool_malloc(pool, sizeof(intptr_t));
155 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
156
157 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
158
159 test[0] = 5; test[1] = (intptr_t) cb;
160 *cb = 13;
161
162 ucx_mempool_set_destr(test, test_setdestr);
163 UCX_TEST_ASSERT(
164 *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
165 UCX_TEST_ASSERT(
166 test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
167
168 ucx_mempool_destroy(pool);
169
170 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
171
172 UCX_TEST_END
173 if (cb != NULL) free(cb);
174 }
175
176
177 UCX_TEST(test_ucx_mempool_reg_destr) {
178
179 intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
180 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
181 UCX_TEST_BEGIN
182 UcxMempool *pool = ucx_mempool_new(1);
183
184 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
185
186 test[0] = 5; test[1] = (intptr_t) cb;
187 *cb = 13;
188
189 ucx_mempool_reg_destr(pool, test, test_setdestr);
190
191 ucx_destructor *pooladdr = (ucx_destructor*)
192 ((char*)pool->data[0] + sizeof(ucx_destructor));
193
194 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed");
195
196 ucx_mempool_destroy(pool);
197 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
198 UCX_TEST_END
199
200 if (test != NULL) free(test);
201 if (cb != NULL) free(cb);
202 }
203
204 UCX_TEST(test_ucx_mempool_realloc) {
205
206 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
207 UCX_TEST_BEGIN
208 UcxMempool *pool = ucx_mempool_new(2);
209
210 ucx_mempool_malloc(pool, sizeof(intptr_t));
211 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
212
213 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
214
215 test[0] = 5; test[1] = (intptr_t) cb;
216 *cb = 13;
217
218 ucx_mempool_set_destr(test, test_setdestr);
219
220 intptr_t *rtest, n = 2;
221 do {
222 n *= 2;
223 UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc");
224 rtest = (intptr_t*) ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
225 } while (rtest == test);
226 test = rtest;
227
228 UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
229 "realloc killed destructor")
230 UCX_TEST_ASSERT(
231 test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
232
233 ucx_mempool_destroy(pool);
234
235 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
236
237 UCX_TEST_END
238 if (cb != NULL) free(cb);
239 }

mercurial