7 #include "mpool_tests.h" |
7 #include "mpool_tests.h" |
8 |
8 |
9 UCX_TEST_IMPLEMENT(test_ucx_mempool_new) { |
9 UCX_TEST_IMPLEMENT(test_ucx_mempool_new) { |
10 UcxMempool *pool = ucx_mempool_new(16); |
10 UcxMempool *pool = ucx_mempool_new(16); |
11 UCX_TEST_BEGIN |
11 UCX_TEST_BEGIN |
12 UCX_TEST_ASSERT(pool->size == 16, "wrong size") |
12 UCX_TEST_ASSERT(pool->size == 16, "wrong size"); |
13 UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter") |
13 UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter"); |
14 UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed") |
14 UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed"); |
15 UCX_TEST_END |
15 UCX_TEST_END |
16 ucx_mempool_free(pool); |
16 ucx_mempool_free(pool); |
17 } |
17 } |
18 |
18 |
19 UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc) { |
19 UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc) { |
20 |
20 |
21 UcxMempool *pool = ucx_mempool_new(1); |
21 UcxMempool *pool = ucx_mempool_new(1); |
22 UCX_TEST_BEGIN |
22 UCX_TEST_BEGIN |
23 intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t)); |
23 intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t)); |
24 |
24 |
25 UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented") |
25 UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented"); |
26 UCX_TEST_ASSERT(pool->size == 1, "chcap called") |
26 UCX_TEST_ASSERT(pool->size == 1, "chcap called"); |
27 |
27 |
28 intptr_t *pooladdr = |
28 intptr_t *pooladdr = |
29 (intptr_t*)((char*)pool->data[0] + sizeof(ucx_destructor)); |
29 (intptr_t*)((char*)pool->data[0] + sizeof(ucx_destructor)); |
30 *pooladdr = 5; |
30 *pooladdr = 5; |
31 |
31 |
32 UCX_TEST_ASSERT(*test == 5, "wrong pointer") |
32 UCX_TEST_ASSERT(*test == 5, "wrong pointer"); |
33 |
33 |
34 UCX_TEST_END |
34 UCX_TEST_END |
35 ucx_mempool_free(pool); |
35 ucx_mempool_free(pool); |
36 } |
36 } |
37 |
37 |
40 UcxMempool *pool = ucx_mempool_new(1); |
40 UcxMempool *pool = ucx_mempool_new(1); |
41 UCX_TEST_BEGIN |
41 UCX_TEST_BEGIN |
42 ucx_mempool_malloc(pool, sizeof(int)); |
42 ucx_mempool_malloc(pool, sizeof(int)); |
43 intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t)); |
43 intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t)); |
44 |
44 |
45 UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented") |
45 UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented"); |
46 UCX_TEST_ASSERT(pool->size == 17, "chcap not called") |
46 UCX_TEST_ASSERT(pool->size == 17, "chcap not called"); |
47 |
47 |
48 intptr_t *pooladdr = |
48 intptr_t *pooladdr = |
49 (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor)); |
49 (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor)); |
50 *pooladdr = 5; |
50 *pooladdr = 5; |
51 |
51 |
52 UCX_TEST_ASSERT(*test == 5, "wrong pointer") |
52 UCX_TEST_ASSERT(*test == 5, "wrong pointer"); |
53 |
53 |
54 UCX_TEST_END |
54 UCX_TEST_END |
55 ucx_mempool_free(pool); |
55 ucx_mempool_free(pool); |
56 } |
56 } |
57 |
57 |
60 UcxMempool *pool = ucx_mempool_new(1); |
60 UcxMempool *pool = ucx_mempool_new(1); |
61 UCX_TEST_BEGIN |
61 UCX_TEST_BEGIN |
62 |
62 |
63 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
63 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
64 |
64 |
65 UCX_TEST_ASSERT(test != NULL, "no memory for test data") |
65 UCX_TEST_ASSERT(test != NULL, "no memory for test data"); |
66 UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed") |
66 UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed"); |
67 |
67 |
68 UCX_TEST_END |
68 UCX_TEST_END |
69 ucx_mempool_free(pool); |
69 ucx_mempool_free(pool); |
70 } |
70 } |
71 |
71 |
81 UcxMempool *pool = ucx_mempool_new(2); |
81 UcxMempool *pool = ucx_mempool_new(2); |
82 |
82 |
83 ucx_mempool_malloc(pool, sizeof(intptr_t)); |
83 ucx_mempool_malloc(pool, sizeof(intptr_t)); |
84 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
84 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
85 |
85 |
86 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") |
86 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data"); |
87 |
87 |
88 test[0] = 5; test[1] = (intptr_t) cb; |
88 test[0] = 5; test[1] = (intptr_t) cb; |
89 *cb = 13; |
89 *cb = 13; |
90 |
90 |
91 ucx_mempool_set_destr(test, test_setdestr); |
91 ucx_mempool_set_destr(test, test_setdestr); |
94 UCX_TEST_ASSERT( |
94 UCX_TEST_ASSERT( |
95 test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data") |
95 test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data") |
96 |
96 |
97 ucx_mempool_free(pool); |
97 ucx_mempool_free(pool); |
98 |
98 |
99 UCX_TEST_ASSERT(*cb == 42, "destructor not called") |
99 UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
100 |
100 |
101 UCX_TEST_END |
101 UCX_TEST_END |
102 if (cb != NULL) free(cb); |
102 if (cb != NULL) free(cb); |
103 } |
103 } |
104 |
104 |
108 intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t)); |
108 intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t)); |
109 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
109 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
110 UCX_TEST_BEGIN |
110 UCX_TEST_BEGIN |
111 UcxMempool *pool = ucx_mempool_new(1); |
111 UcxMempool *pool = ucx_mempool_new(1); |
112 |
112 |
113 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") |
113 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data"); |
114 |
114 |
115 test[0] = 5; test[1] = (intptr_t) cb; |
115 test[0] = 5; test[1] = (intptr_t) cb; |
116 *cb = 13; |
116 *cb = 13; |
117 |
117 |
118 ucx_mempool_reg_destr(pool, test, test_setdestr); |
118 ucx_mempool_reg_destr(pool, test, test_setdestr); |
119 |
119 |
120 ucx_destructor *pooladdr = (ucx_destructor*) |
120 ucx_destructor *pooladdr = (ucx_destructor*) |
121 ((char*)pool->data[0] + sizeof(ucx_destructor)); |
121 ((char*)pool->data[0] + sizeof(ucx_destructor)); |
122 |
122 |
123 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed") |
123 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed"); |
124 |
124 |
125 ucx_mempool_free(pool); |
125 ucx_mempool_free(pool); |
126 UCX_TEST_ASSERT(*cb == 42, "destructor not called") |
126 UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
127 UCX_TEST_END |
127 UCX_TEST_END |
128 |
128 |
129 if (test != NULL) free(test); |
129 if (test != NULL) free(test); |
130 if (cb != NULL) free(cb); |
130 if (cb != NULL) free(cb); |
131 } |
131 } |
137 UcxMempool *pool = ucx_mempool_new(2); |
137 UcxMempool *pool = ucx_mempool_new(2); |
138 |
138 |
139 ucx_mempool_malloc(pool, sizeof(intptr_t)); |
139 ucx_mempool_malloc(pool, sizeof(intptr_t)); |
140 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
140 intptr_t *test = (intptr_t*) ucx_mempool_calloc(pool, 2, sizeof(intptr_t)); |
141 |
141 |
142 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data") |
142 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data"); |
143 |
143 |
144 test[0] = 5; test[1] = (intptr_t) cb; |
144 test[0] = 5; test[1] = (intptr_t) cb; |
145 *cb = 13; |
145 *cb = 13; |
146 |
146 |
147 ucx_mempool_set_destr(test, test_setdestr); |
147 ucx_mempool_set_destr(test, test_setdestr); |
148 |
148 |
149 int *rtest, n = 2; |
149 int *rtest, n = 2; |
150 do { |
150 do { |
151 n *= 2; |
151 n *= 2; |
152 UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc") |
152 UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc"); |
153 rtest = ucx_mempool_realloc(pool, test, n*sizeof(intptr_t)); |
153 rtest = ucx_mempool_realloc(pool, test, n*sizeof(intptr_t)); |
154 } while (rtest == test); |
154 } while (rtest == test); |
155 test = rtest; |
155 test = rtest; |
156 |
156 |
157 UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr, |
157 UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr, |
159 UCX_TEST_ASSERT( |
159 UCX_TEST_ASSERT( |
160 test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data") |
160 test[0] == 5 && test[1] == (intptr_t) cb, "realloc destroyed data") |
161 |
161 |
162 ucx_mempool_free(pool); |
162 ucx_mempool_free(pool); |
163 |
163 |
164 UCX_TEST_ASSERT(*cb == 42, "destructor not called") |
164 UCX_TEST_ASSERT(*cb == 42, "destructor not called"); |
165 |
165 |
166 UCX_TEST_END |
166 UCX_TEST_END |
167 if (cb != NULL) free(cb); |
167 if (cb != NULL) free(cb); |
168 } |
168 } |