Thu, 31 May 2012 09:18:26 +0200
consequently used intptr_t in mpool tests
13 | 1 | /* |
2 | * | |
3 | */ | |
4 | ||
30 | 5 | #include <inttypes.h> |
6 | ||
13 | 7 | #include "mpool_tests.h" |
8 | ||
28 | 9 | UCX_TEST_BEGIN(test_ucx_mempool_new) { |
10 | UcxMempool *pool = ucx_mempool_new(16); | |
11 | ||
12 | UCX_TEST_ASSERT(pool->size == 16, "wrong size") | |
13 | UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter") | |
14 | UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed") | |
15 | ||
16 | ucx_mempool_free(pool); | |
17 | ||
18 | UCX_TEST_END | |
19 | } | |
13 | 20 | |
28 | 21 | UCX_TEST_BEGIN(test_ucx_mempool_malloc) { |
22 | ||
23 | UcxMempool *pool = ucx_mempool_new(1); | |
24 | ||
32
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
25 | intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t)); |
28 | 26 | |
27 | UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented") | |
28 | UCX_TEST_ASSERT(pool->size == 1, "chcap called") | |
29 | ||
32
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
30 | intptr_t *pooladdr = |
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
31 | (intptr_t*)((char*)pool->data[0] + sizeof(ucx_destructor)); |
28 | 32 | *pooladdr = 5; |
33 | ||
34 | UCX_TEST_ASSERT(*test == 5, "wrong pointer") | |
35 | ||
36 | ucx_mempool_free(pool); | |
37 | ||
38 | UCX_TEST_END | |
13 | 39 | } |
40 | ||
28 | 41 | UCX_TEST_BEGIN(test_ucx_mempool_malloc_with_chcap) { |
42 | ||
43 | UcxMempool *pool = ucx_mempool_new(1); | |
44 | ||
45 | 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
|
46 | intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t)); |
28 | 47 | |
48 | UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented") | |
49 | UCX_TEST_ASSERT(pool->size == 17, "chcap not called") | |
50 | ||
32
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
51 | intptr_t *pooladdr = |
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
52 | (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor)); |
28 | 53 | *pooladdr = 5; |
54 | ||
55 | UCX_TEST_ASSERT(*test == 5, "wrong pointer") | |
56 | ||
57 | ucx_mempool_free(pool); | |
58 | ||
59 | UCX_TEST_END | |
60 | } | |
61 | ||
62 | UCX_TEST_BEGIN(test_ucx_mempool_calloc) { | |
63 | ||
64 | UcxMempool *pool = ucx_mempool_new(1); | |
65 | ||
32
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
66 | intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
28 | 67 | |
68 | UCX_TEST_ASSERT(test != NULL, "no memory for test data") | |
69 | UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed") | |
70 | ||
71 | ucx_mempool_free(pool); | |
72 | ||
73 | UCX_TEST_END | |
74 | } | |
75 | ||
76 | void test_setdestr(void* elem) { | |
30 | 77 | intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1]; |
28 | 78 | *cb = 42; |
13 | 79 | } |
80 | ||
28 | 81 | UCX_TEST_BEGIN(test_ucx_mempool_set_destr) { |
82 | ||
83 | UcxMempool *pool = ucx_mempool_new(2); | |
84 | ||
32
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
85 | ucx_mempool_malloc(pool, sizeof(intptr_t)); |
30 | 86 | intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
28 | 87 | |
32
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
88 | intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
28 | 89 | UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") |
90 | ||
30 | 91 | test[0] = 5; test[1] = (intptr_t) cb; |
28 | 92 | *cb = 13; |
93 | ||
94 | ucx_mempool_set_destr(test, test_setdestr); | |
95 | UCX_TEST_ASSERT( | |
96 | *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed") | |
97 | UCX_TEST_ASSERT( | |
30 | 98 | test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data") |
28 | 99 | |
100 | ucx_mempool_free(pool); | |
101 | ||
102 | UCX_TEST_ASSERT(*cb == 42, "destructor not called") | |
13 | 103 | |
28 | 104 | free(cb); |
105 | ||
106 | UCX_TEST_END | |
107 | } | |
13 | 108 | |
109 | ||
28 | 110 | UCX_TEST_BEGIN(test_ucx_mempool_reg_destr) { |
111 | ||
112 | UcxMempool *pool = ucx_mempool_new(1); | |
113 | ||
30 | 114 | intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t)); |
28 | 115 | |
30 | 116 | intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
28 | 117 | UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") |
118 | ||
30 | 119 | test[0] = 5; test[1] = (intptr_t) cb; |
28 | 120 | *cb = 13; |
121 | ||
122 | ucx_mempool_reg_destr(pool, test, test_setdestr); | |
123 | ||
124 | ucx_destructor *pooladdr = (ucx_destructor*) | |
30 | 125 | ((char*)pool->data[0] + sizeof(ucx_destructor)); |
28 | 126 | |
127 | UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed") | |
128 | ||
129 | ucx_mempool_free(pool); | |
30 | 130 | free(test); |
28 | 131 | |
132 | UCX_TEST_ASSERT(*cb == 42, "destructor not called") | |
133 | ||
134 | free(cb); | |
135 | ||
136 | UCX_TEST_END | |
137 | } | |
13 | 138 | |
28 | 139 | UCX_TEST_BEGIN(test_ucx_mempool_realloc) { |
140 | ||
141 | UcxMempool *pool = ucx_mempool_new(2); | |
142 | ||
32
c7af4ec56e19
consequently used intptr_t in mpool tests
Mike Becker <universe@uap-core.de>
parents:
30
diff
changeset
|
143 | ucx_mempool_malloc(pool, sizeof(intptr_t)); |
30 | 144 | intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
28 | 145 | |
30 | 146 | intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
28 | 147 | UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") |
148 | ||
30 | 149 | test[0] = 5; test[1] = (intptr_t) cb; |
28 | 150 | *cb = 13; |
151 | ||
152 | ucx_mempool_set_destr(test, test_setdestr); | |
14 | 153 | |
28 | 154 | int *rtest, n = 2; |
155 | do { | |
156 | n *= 2; | |
157 | UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc") | |
30 | 158 | rtest = ucx_mempool_realloc(pool, test, n*sizeof(intptr_t)); |
28 | 159 | } while (rtest == test); |
160 | test = rtest; | |
161 | ||
162 | UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr, | |
163 | "realloc killed destructor") | |
164 | UCX_TEST_ASSERT( | |
30 | 165 | test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data") |
28 | 166 | |
167 | ucx_mempool_free(pool); | |
168 | ||
169 | UCX_TEST_ASSERT(*cb == 42, "destructor not called") | |
13 | 170 | |
28 | 171 | free(cb); |
13 | 172 | |
28 | 173 | UCX_TEST_END |
13 | 174 | } |