Thu, 28 Feb 2013 08:50:24 +0100
added license and copyright notice to all files
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 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 <stdint.h>
31 #include "mpool_tests.h"
33 UCX_TEST_IMPLEMENT(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_free(pool);
41 }
43 UCX_TEST_IMPLEMENT(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_free(pool);
60 }
62 UCX_TEST_IMPLEMENT(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 == 17, "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_free(pool);
80 }
82 UCX_TEST_IMPLEMENT(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_free(pool);
94 }
96 void test_setdestr(void* elem) {
97 intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1];
98 *cb = 42;
99 }
101 UCX_TEST_IMPLEMENT(test_ucx_mempool_set_destr) {
103 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
104 UCX_TEST_BEGIN
105 UcxMempool *pool = ucx_mempool_new(2);
107 ucx_mempool_malloc(pool, sizeof(intptr_t));
108 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
110 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
112 test[0] = 5; test[1] = (intptr_t) cb;
113 *cb = 13;
115 ucx_mempool_set_destr(test, test_setdestr);
116 UCX_TEST_ASSERT(
117 *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
118 UCX_TEST_ASSERT(
119 test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data")
121 ucx_mempool_free(pool);
123 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
125 UCX_TEST_END
126 if (cb != NULL) free(cb);
127 }
130 UCX_TEST_IMPLEMENT(test_ucx_mempool_reg_destr) {
132 intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t));
133 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
134 UCX_TEST_BEGIN
135 UcxMempool *pool = ucx_mempool_new(1);
137 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
139 test[0] = 5; test[1] = (intptr_t) cb;
140 *cb = 13;
142 ucx_mempool_reg_destr(pool, test, test_setdestr);
144 ucx_destructor *pooladdr = (ucx_destructor*)
145 ((char*)pool->data[0] + sizeof(ucx_destructor));
147 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed");
149 ucx_mempool_free(pool);
150 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
151 UCX_TEST_END
153 if (test != NULL) free(test);
154 if (cb != NULL) free(cb);
155 }
157 UCX_TEST_IMPLEMENT(test_ucx_mempool_realloc) {
159 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t));
160 UCX_TEST_BEGIN
161 UcxMempool *pool = ucx_mempool_new(2);
163 ucx_mempool_malloc(pool, sizeof(intptr_t));
164 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t));
166 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data");
168 test[0] = 5; test[1] = (intptr_t) cb;
169 *cb = 13;
171 ucx_mempool_set_destr(test, test_setdestr);
173 intptr_t *rtest, n = 2;
174 do {
175 n *= 2;
176 UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc");
177 rtest = (intptr_t*) ucx_mempool_realloc(pool, test, n*sizeof(intptr_t));
178 } while (rtest == test);
179 test = rtest;
181 UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
182 "realloc killed destructor")
183 UCX_TEST_ASSERT(
184 test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data")
186 ucx_mempool_free(pool);
188 UCX_TEST_ASSERT(*cb == 42, "destructor not called");
190 UCX_TEST_END
191 if (cb != NULL) free(cb);
192 }