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