test/mpool_tests.c

changeset 28
1666cbeb1db8
parent 19
cdd7a3173249
child 30
23bb65cbf7a4
equal deleted inserted replaced
27:22644e2572bc 28:1666cbeb1db8
1 /* 1 /*
2 * 2 *
3 */ 3 */
4 4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "mpool_tests.h" 5 #include "mpool_tests.h"
10 6
11 int cmp_value = 2; 7 UCX_TEST_BEGIN(test_ucx_mempool_new) {
12 8 UcxMempool *pool = ucx_mempool_new(16);
13 void int_destructor(int *ptr) { 9
14 if(*ptr != cmp_value) { 10 UCX_TEST_ASSERT(pool->size == 16, "wrong size")
15 fprintf(stderr, "ucx_mempool_free failed: wrong order\n"); 11 UCX_TEST_ASSERT(pool->ndata == 0, "uninitialized counter")
16 return; 12 UCX_TEST_ASSERT(pool->data != NULL, "no memory addressed")
17 } 13
18 cmp_value += 2; 14 ucx_mempool_free(pool);
15
16 UCX_TEST_END
19 } 17 }
20 18
21 void hello_destructor(char *str) { 19 UCX_TEST_BEGIN(test_ucx_mempool_malloc) {
22 if(strcmp(str, "Hello World!") != 0) { 20
23 fprintf(stderr, "ucx_mempool_reg_destr failed\n"); 21 UcxMempool *pool = ucx_mempool_new(1);
24 } 22
23 int *test = (int*) ucx_mempool_malloc(pool, sizeof(int));
24
25 UCX_TEST_ASSERT(pool->ndata == 1, "counter not incremented")
26 UCX_TEST_ASSERT(pool->size == 1, "chcap called")
27
28 int *pooladdr = (int*)((char*)pool->data[0] + sizeof(ucx_destructor));
29 *pooladdr = 5;
30
31 UCX_TEST_ASSERT(*test == 5, "wrong pointer")
32
33 ucx_mempool_free(pool);
34
35 UCX_TEST_END
25 } 36 }
26 37
27 int mpool_tests() { 38 UCX_TEST_BEGIN(test_ucx_mempool_malloc_with_chcap) {
28 int r = 0; 39
40 UcxMempool *pool = ucx_mempool_new(1);
41
42 ucx_mempool_malloc(pool, sizeof(int));
43 int *test = (int*) ucx_mempool_malloc(pool, sizeof(int));
44
45 UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented")
46 UCX_TEST_ASSERT(pool->size == 17, "chcap not called")
47
48 int *pooladdr = (int*)((char*)pool->data[1] + sizeof(ucx_destructor));
49 *pooladdr = 5;
50
51 UCX_TEST_ASSERT(*test == 5, "wrong pointer")
52
53 ucx_mempool_free(pool);
54
55 UCX_TEST_END
56 }
29 57
30 printf(" Test ucx_mempool_new\n"); 58 UCX_TEST_BEGIN(test_ucx_mempool_calloc) {
31 UcxMempool *pool = ucx_mempool_new(16); 59
60 UcxMempool *pool = ucx_mempool_new(1);
61
62 int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
63
64 UCX_TEST_ASSERT(test != NULL, "no memory for test data")
65 UCX_TEST_ASSERT(test[0] == 0 && test[1] == 0, "failed")
66
67 ucx_mempool_free(pool);
68
69 UCX_TEST_END
70 }
32 71
33 printf(" Test ucx_mempool_malloc\n"); 72 void test_setdestr(void* elem) {
34 int *ptr1 = (int*)ucx_mempool_malloc(pool, sizeof(int)); 73 int *cb = (int*) ((int*) elem)[1];
35 for(int i=0;i<256;i++) { 74 *cb = 42;
36 ucx_mempool_malloc(pool, i+1); 75 }
37 }
38 int *ptr2 = (int*)ucx_mempool_malloc(pool, sizeof(int));
39 int *ptr3 = (int*)ucx_mempool_malloc(pool, sizeof(int));
40 for(int i=0;i<256;i++) {
41 ucx_mempool_malloc(pool, i+1);
42 }
43 int *ptr4 = (int*)ucx_mempool_malloc(pool, sizeof(int));
44 76
45 *ptr1 = 2; 77 UCX_TEST_BEGIN(test_ucx_mempool_set_destr) {
46 *ptr2 = 4; 78
47 *ptr3 = 6; 79 UcxMempool *pool = ucx_mempool_new(2);
48 *ptr4 = 8; 80
81 ucx_mempool_malloc(pool, sizeof(int));
82 int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
83
84 int *cb = (int*) malloc(sizeof(int));
85 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
86
87 test[0] = 5; test[1] = (int) cb;
88 *cb = 13;
89
90 ucx_mempool_set_destr(test, test_setdestr);
91
92 UCX_TEST_ASSERT(
93 *(ucx_destructor*)(pool->data[1]) == test_setdestr, "failed")
94 UCX_TEST_ASSERT(
95 test[0] == 5 && test[1] == (int) cb, "setdestr destroyed data")
96
97 ucx_mempool_free(pool);
98
99 UCX_TEST_ASSERT(*cb == 42, "destructor not called")
49 100
50 printf(" Test ucx_mempool_set_destr\n"); 101 free(cb);
51 ucx_mempool_set_destr(ptr1, (ucx_destructor)int_destructor); 102
52 ucx_mempool_set_destr(ptr2, (ucx_destructor)int_destructor); 103 UCX_TEST_END
53 ucx_mempool_set_destr(ptr3, (ucx_destructor)int_destructor); 104 }
54 ucx_mempool_set_destr(ptr4, (ucx_destructor)int_destructor);
55 105
56 printf(" Test ucx_mempool_calloc\n");
57 char *str = ucx_mempool_calloc(pool, 1, 3);
58 if(str[0] != 0 || str[1] != 0 || str[2] != 0) {
59 fprintf(stderr, "ucx_mempool_calloc failed\n");
60 r--;
61 }
62 str[0] = 'O';
63 str[1] = 'K';
64 106
65 printf(" Test ucx_mempool_realloc\n"); 107 UCX_TEST_BEGIN(test_ucx_mempool_reg_destr) {
66 str = ucx_mempool_realloc(pool, str, 4);
67 str[2] = '!';
68 str[3] = 0;
69 if(strcmp(str, "OK!") != 0) {
70 fprintf(stderr, "Test ucx_mempool_realloc failed!\n");
71 r--;
72 }
73 108
74 printf(" Test ucx_mempool_reg_destr\n"); 109 UcxMempool *pool = ucx_mempool_new(1);
75 char *hello = "Hello World!"; 110
76 ucx_mempool_reg_destr(pool, hello, (ucx_destructor)hello_destructor); 111 int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
112
113 int *cb = (int*) malloc(sizeof(int));
114 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
115
116 test[0] = 5; test[1] = (int) cb;
117 *cb = 13;
118
119 ucx_mempool_reg_destr(pool, test, test_setdestr);
120
121 ucx_destructor *pooladdr = (ucx_destructor*)
122 ((char*)pool->data[1] + sizeof(ucx_destructor));
123
124 UCX_TEST_ASSERT(*pooladdr == test_setdestr, "failed")
125
126 ucx_mempool_free(pool);
127
128 UCX_TEST_ASSERT(*cb == 42, "destructor not called")
77 129
78 printf(" Test ucx_mempool_free\n"); 130 free(cb);
79 //ucx_mempool_free(pool);
80 131
132 UCX_TEST_END
133 }
81 134
82 return r; 135 UCX_TEST_BEGIN(test_ucx_mempool_realloc) {
136
137 UcxMempool *pool = ucx_mempool_new(2);
138
139 ucx_mempool_malloc(pool, sizeof(int));
140 int *test = (int*) ucx_mempool_calloc(pool, 2, sizeof(int));
141
142 int *cb = (int*) malloc(sizeof(int));
143 UCX_TEST_ASSERT(cb != NULL && test != NULL, "no memory for test data")
144
145 test[0] = 5; test[1] = (int) cb;
146 *cb = 13;
147
148 ucx_mempool_set_destr(test, test_setdestr);
149
150 int *rtest, n = 2;
151 do {
152 n *= 2;
153 UCX_TEST_ASSERT(n < 65536, "test corrupt - no movement for realloc")
154 rtest = ucx_mempool_realloc(pool, test, n*sizeof(int));
155 } while (rtest == test);
156 test = rtest;
157
158 UCX_TEST_ASSERT(*(ucx_destructor*)(pool->data[1]) == test_setdestr,
159 "realloc killed destructor")
160 UCX_TEST_ASSERT(
161 test[0] == 5 && test[1] == (int) cb, "realloc destroyed data")
162
163 ucx_mempool_free(pool);
164
165 UCX_TEST_ASSERT(*cb == 42, "destructor not called")
166
167 free(cb);
168
169 UCX_TEST_END
83 } 170 }

mercurial