Thu, 28 Feb 2013 08:50:24 +0100
added license and copyright notice to all files
13 | 1 | /* |
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
13 | 3 | * |
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
5 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
8 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
11 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
15 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
13 | 27 | */ |
28 | ||
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
29 | #include <stdint.h> |
30 | 30 | |
13 | 31 | #include "mpool_tests.h" |
32 | ||
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
33 | UCX_TEST_IMPLEMENT(test_ucx_mempool_new) { |
28 | 34 | UcxMempool *pool = ucx_mempool_new(16); |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
35 | UCX_TEST_BEGIN |
40
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
36 | UCX_TEST_ASSERT(pool->size == 16, "wrong size"); |
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
37 | UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter"); |
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
38 | UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed"); |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
39 | UCX_TEST_END |
28 | 40 | ucx_mempool_free(pool); |
41 | } | |
13 | 42 | |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
43 | UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc) { |
28 | 44 | |
45 | UcxMempool *pool = ucx_mempool_new(1); | |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
46 | UCX_TEST_BEGIN |
32
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
47 | intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t)); |
28 | 48 | |
40
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
49 | UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented"); |
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
50 | UCX_TEST_ASSERT(pool->size == 1, "chcap called"); |
28 | 51 | |
32
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
52 | intptr_t *pooladdr = |
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
53 | (intptr_t*)((char*)pool->data[0] + sizeof(ucx_destructor)); |
28 | 54 | *pooladdr = 5; |
55 | ||
40
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
56 | UCX_TEST_ASSERT(*test == 5, "wrong pointer"); |
28 | 57 | |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
58 | UCX_TEST_END |
28 | 59 | ucx_mempool_free(pool); |
13 | 60 | } |
61 | ||
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
62 | UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc_with_chcap) { |
28 | 63 | |
64 | UcxMempool *pool = ucx_mempool_new(1); | |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
65 | UCX_TEST_BEGIN |
28 | 66 | ucx_mempool_malloc(pool, sizeof(int)); |
32
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
67 | intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t)); |
28 | 68 | |
40
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
69 | UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented"); |
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
70 | UCX_TEST_ASSERT(pool->size == 17, "chcap not called"); |
28 | 71 | |
32
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
72 | intptr_t *pooladdr = |
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
73 | (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor)); |
28 | 74 | *pooladdr = 5; |
75 | ||
40
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
76 | UCX_TEST_ASSERT(*test == 5, "wrong pointer"); |
28 | 77 | |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
78 | UCX_TEST_END |
28 | 79 | ucx_mempool_free(pool); |
80 | } | |
81 | ||
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
82 | UCX_TEST_IMPLEMENT(test_ucx_mempool_calloc) { |
28 | 83 | |
84 | UcxMempool *pool = ucx_mempool_new(1); | |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
85 | UCX_TEST_BEGIN |
28 | 86 | |
32
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
87 | intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
28 | 88 | |
40
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
89 | UCX_TEST_ASSERT(test != NULL, "no memory for test data"); |
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
90 | UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed"); |
28 | 91 | |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
92 | UCX_TEST_END |
28 | 93 | ucx_mempool_free(pool); |
94 | } | |
95 | ||
96 | void test_setdestr(void* elem) { | |
30 | 97 | intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1]; |
28 | 98 | *cb = 42; |
13 | 99 | } |
100 | ||
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
101 | UCX_TEST_IMPLEMENT(test_ucx_mempool_set_destr) { |
28 | 102 | |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
103 | intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
104 | UCX_TEST_BEGIN |
28 | 105 | UcxMempool *pool = ucx_mempool_new(2); |
106 | ||
32
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
107 | ucx_mempool_malloc(pool, sizeof(intptr_t)); |
30 | 108 | intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
28 | 109 | |
40
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
110 | UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data"); |
28 | 111 | |
30 | 112 | test[0] = 5; test[1] = (intptr_t) cb; |
28 | 113 | *cb = 13; |
114 | ||
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( | |
30 | 119 | test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data") |
28 | 120 | |
121 | ucx_mempool_free(pool); | |
122 | ||
40
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
123 | UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
28 | 124 | |
125 | UCX_TEST_END | |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
126 | if (cb != NULL) free(cb); |
28 | 127 | } |
13 | 128 | |
129 | ||
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
130 | UCX_TEST_IMPLEMENT(test_ucx_mempool_reg_destr) { |
28 | 131 | |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
132 | intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t)); |
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
133 | intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
134 | UCX_TEST_BEGIN |
28 | 135 | UcxMempool *pool = ucx_mempool_new(1); |
136 | ||
40
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
137 | UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data"); |
28 | 138 | |
30 | 139 | test[0] = 5; test[1] = (intptr_t) cb; |
28 | 140 | *cb = 13; |
141 | ||
142 | ucx_mempool_reg_destr(pool, test, test_setdestr); | |
143 | ||
144 | ucx_destructor *pooladdr = (ucx_destructor*) | |
30 | 145 | ((char*)pool->data[0] + sizeof(ucx_destructor)); |
28 | 146 | |
40
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
147 | UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed"); |
28 | 148 | |
149 | ucx_mempool_free(pool); | |
40
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
150 | UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
151 | UCX_TEST_END |
28 | 152 | |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
153 | if (test != NULL) free(test); |
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
154 | if (cb != NULL) free(cb); |
28 | 155 | } |
13 | 156 | |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
157 | UCX_TEST_IMPLEMENT(test_ucx_mempool_realloc) { |
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
158 | |
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
159 | intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
160 | UCX_TEST_BEGIN |
28 | 161 | UcxMempool *pool = ucx_mempool_new(2); |
162 | ||
32
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
163 | ucx_mempool_malloc(pool, sizeof(intptr_t)); |
30 | 164 | intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
165 | |
40
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
166 | UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data"); |
28 | 167 | |
30 | 168 | test[0] = 5; test[1] = (intptr_t) cb; |
28 | 169 | *cb = 13; |
170 | ||
171 | ucx_mempool_set_destr(test, test_setdestr); | |
14 | 172 | |
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
173 | intptr_t *rtest, n = 2; |
28 | 174 | do { |
175 | n *= 2; | |
40
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
176 | UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc"); |
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
177 | rtest = (intptr_t*) ucx_mempool_realloc(pool, test, n*sizeof(intptr_t)); |
28 | 178 | } while (rtest == test); |
179 | test = rtest; | |
180 | ||
181 | UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr, | |
182 | "realloc killed destructor") | |
183 | UCX_TEST_ASSERT( | |
30 | 184 | test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data") |
28 | 185 | |
186 | ucx_mempool_free(pool); | |
187 | ||
40
583718dd4cf3
added semicolons to assert makros
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
188 | UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
13 | 189 | |
28 | 190 | UCX_TEST_END |
33
9c219a62070d
major refactoring of test framework
Mike Becker <universe@uap-core.de>
parents:
32
diff
changeset
|
191 | if (cb != NULL) free(cb); |
13 | 192 | } |