4 |
4 |
5 #include <inttypes.h> |
5 #include <inttypes.h> |
6 |
6 |
7 #include "mpool_tests.h" |
7 #include "mpool_tests.h" |
8 |
8 |
9 UCX_TEST_BEGIN(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 |
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 |
15 UCX_TEST_END |
16 ucx_mempool_free(pool); |
16 ucx_mempool_free(pool); |
17 |
|
18 UCX_TEST_END |
|
19 } |
17 } |
20 |
18 |
21 UCX_TEST_BEGIN(test_ucx_mempool_malloc) { |
19 UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc) { |
22 |
20 |
23 UcxMempool *pool = ucx_mempool_new(1); |
21 UcxMempool *pool = ucx_mempool_new(1); |
24 |
22 UCX_TEST_BEGIN |
25 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)); |
26 |
24 |
27 UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented") |
25 UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented") |
28 UCX_TEST_ASSERT(pool->size == 1, "chcap called") |
26 UCX_TEST_ASSERT(pool->size == 1, "chcap called") |
29 |
27 |
31 (intptr_t*)((char*)pool->data[0] + sizeof(ucx_destructor)); |
29 (intptr_t*)((char*)pool->data[0] + sizeof(ucx_destructor)); |
32 *pooladdr = 5; |
30 *pooladdr = 5; |
33 |
31 |
34 UCX_TEST_ASSERT(*test == 5, "wrong pointer") |
32 UCX_TEST_ASSERT(*test == 5, "wrong pointer") |
35 |
33 |
|
34 UCX_TEST_END |
36 ucx_mempool_free(pool); |
35 ucx_mempool_free(pool); |
37 |
|
38 UCX_TEST_END |
|
39 } |
36 } |
40 |
37 |
41 UCX_TEST_BEGIN(test_ucx_mempool_malloc_with_chcap) { |
38 UCX_TEST_IMPLEMENT(test_ucx_mempool_malloc_with_chcap) { |
42 |
39 |
43 UcxMempool *pool = ucx_mempool_new(1); |
40 UcxMempool *pool = ucx_mempool_new(1); |
44 |
41 UCX_TEST_BEGIN |
45 ucx_mempool_malloc(pool, sizeof(int)); |
42 ucx_mempool_malloc(pool, sizeof(int)); |
46 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)); |
47 |
44 |
48 UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented") |
45 UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented") |
49 UCX_TEST_ASSERT(pool->size == 17, "chcap not called") |
46 UCX_TEST_ASSERT(pool->size == 17, "chcap not called") |
52 (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor)); |
49 (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor)); |
53 *pooladdr = 5; |
50 *pooladdr = 5; |
54 |
51 |
55 UCX_TEST_ASSERT(*test == 5, "wrong pointer") |
52 UCX_TEST_ASSERT(*test == 5, "wrong pointer") |
56 |
53 |
|
54 UCX_TEST_END |
57 ucx_mempool_free(pool); |
55 ucx_mempool_free(pool); |
58 |
|
59 UCX_TEST_END |
|
60 } |
56 } |
61 |
57 |
62 UCX_TEST_BEGIN(test_ucx_mempool_calloc) { |
58 UCX_TEST_IMPLEMENT(test_ucx_mempool_calloc) { |
63 |
59 |
64 UcxMempool *pool = ucx_mempool_new(1); |
60 UcxMempool *pool = ucx_mempool_new(1); |
|
61 UCX_TEST_BEGIN |
65 |
62 |
66 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)); |
67 |
64 |
68 UCX_TEST_ASSERT(test != NULL, "no memory for test data") |
65 UCX_TEST_ASSERT(test != NULL, "no memory for test data") |
69 UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed") |
66 UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed") |
70 |
67 |
|
68 UCX_TEST_END |
71 ucx_mempool_free(pool); |
69 ucx_mempool_free(pool); |
72 |
|
73 UCX_TEST_END |
|
74 } |
70 } |
75 |
71 |
76 void test_setdestr(void* elem) { |
72 void test_setdestr(void* elem) { |
77 intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1]; |
73 intptr_t *cb = (intptr_t*) ((intptr_t*) elem)[1]; |
78 *cb = 42; |
74 *cb = 42; |
79 } |
75 } |
80 |
76 |
81 UCX_TEST_BEGIN(test_ucx_mempool_set_destr) { |
77 UCX_TEST_IMPLEMENT(test_ucx_mempool_set_destr) { |
82 |
78 |
|
79 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
|
80 UCX_TEST_BEGIN |
83 UcxMempool *pool = ucx_mempool_new(2); |
81 UcxMempool *pool = ucx_mempool_new(2); |
84 |
82 |
85 ucx_mempool_malloc(pool, sizeof(intptr_t)); |
83 ucx_mempool_malloc(pool, sizeof(intptr_t)); |
86 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)); |
87 |
85 |
88 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
|
89 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") |
90 |
87 |
91 test[0] = 5; test[1] = (intptr_t) cb; |
88 test[0] = 5; test[1] = (intptr_t) cb; |
92 *cb = 13; |
89 *cb = 13; |
93 |
90 |
98 test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data") |
95 test[0] == 5 && test[1] == (intptr_t) cb, "setdestr destroyed data") |
99 |
96 |
100 ucx_mempool_free(pool); |
97 ucx_mempool_free(pool); |
101 |
98 |
102 UCX_TEST_ASSERT(*cb == 42, "destructor not called") |
99 UCX_TEST_ASSERT(*cb == 42, "destructor not called") |
103 |
|
104 free(cb); |
|
105 |
100 |
106 UCX_TEST_END |
101 UCX_TEST_END |
|
102 if (cb != NULL) free(cb); |
107 } |
103 } |
108 |
104 |
109 |
105 |
110 UCX_TEST_BEGIN(test_ucx_mempool_reg_destr) { |
106 UCX_TEST_IMPLEMENT(test_ucx_mempool_reg_destr) { |
111 |
107 |
|
108 intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t)); |
|
109 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
|
110 UCX_TEST_BEGIN |
112 UcxMempool *pool = ucx_mempool_new(1); |
111 UcxMempool *pool = ucx_mempool_new(1); |
113 |
112 |
114 intptr_t *test = (intptr_t*) calloc(2, sizeof(intptr_t)); |
|
115 |
|
116 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
|
117 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") |
118 |
114 |
119 test[0] = 5; test[1] = (intptr_t) cb; |
115 test[0] = 5; test[1] = (intptr_t) cb; |
120 *cb = 13; |
116 *cb = 13; |
121 |
117 |
125 ((char*)pool->data[0] + sizeof(ucx_destructor)); |
121 ((char*)pool->data[0] + sizeof(ucx_destructor)); |
126 |
122 |
127 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed") |
123 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed") |
128 |
124 |
129 ucx_mempool_free(pool); |
125 ucx_mempool_free(pool); |
130 free(test); |
|
131 |
|
132 UCX_TEST_ASSERT(*cb == 42, "destructor not called") |
126 UCX_TEST_ASSERT(*cb == 42, "destructor not called") |
|
127 UCX_TEST_END |
133 |
128 |
134 free(cb); |
129 if (test != NULL) free(test); |
135 |
130 if (cb != NULL) free(cb); |
136 UCX_TEST_END |
|
137 } |
131 } |
138 |
132 |
139 UCX_TEST_BEGIN(test_ucx_mempool_realloc) { |
133 UCX_TEST_IMPLEMENT(test_ucx_mempool_realloc) { |
140 |
134 |
|
135 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
|
136 UCX_TEST_BEGIN |
141 UcxMempool *pool = ucx_mempool_new(2); |
137 UcxMempool *pool = ucx_mempool_new(2); |
142 |
138 |
143 ucx_mempool_malloc(pool, sizeof(intptr_t)); |
139 ucx_mempool_malloc(pool, sizeof(intptr_t)); |
144 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)); |
145 |
141 |
146 intptr_t *cb = (intptr_t*) malloc(sizeof(intptr_t)); |
|
147 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") |
148 |
143 |
149 test[0] = 5; test[1] = (intptr_t) cb; |
144 test[0] = 5; test[1] = (intptr_t) cb; |
150 *cb = 13; |
145 *cb = 13; |
151 |
146 |