diff options
author | 3gg <3gg@shellblade.net> | 2023-07-13 08:24:25 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2023-07-13 08:24:25 -0700 |
commit | de8bbfdfa0f9e95ec2f9847762f5c009765b92f4 (patch) | |
tree | 5c4852ba216b0f04e4c90aa64843cd9dba114c0a /mempool | |
parent | 9f254f0c7b03236be615b1235cf3fc765d6000ea (diff) |
Add tests for dyn and clear.
Diffstat (limited to 'mempool')
-rw-r--r-- | mempool/test/mempool_test.c | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/mempool/test/mempool_test.c b/mempool/test/mempool_test.c index d257922..e6c24a4 100644 --- a/mempool/test/mempool_test.c +++ b/mempool/test/mempool_test.c | |||
@@ -18,12 +18,20 @@ static int sum(test_pool* pool) { | |||
18 | return sum; | 18 | return sum; |
19 | } | 19 | } |
20 | 20 | ||
21 | // Create a pool. | 21 | // Create a statically-backed pool. |
22 | TEST_CASE(mempool_create) { | 22 | TEST_CASE(mempool_create) { |
23 | test_pool pool; | 23 | test_pool pool; |
24 | mempool_make(&pool); | 24 | mempool_make(&pool); |
25 | } | 25 | } |
26 | 26 | ||
27 | // Create a dynamically-backed pool. | ||
28 | TEST_CASE(mem_create_dyn) { | ||
29 | DEF_MEMPOOL_DYN(dyn_pool, int); | ||
30 | |||
31 | dyn_pool pool; | ||
32 | mempool_make_dyn(&pool, NUM_BLOCKS, sizeof(int)); | ||
33 | } | ||
34 | |||
27 | // Allocate all N blocks. | 35 | // Allocate all N blocks. |
28 | TEST_CASE(mempool_allocate_until_full) { | 36 | TEST_CASE(mempool_allocate_until_full) { |
29 | test_pool pool; | 37 | test_pool pool; |
@@ -154,4 +162,27 @@ TEST_CASE(mempool_get_block) { | |||
154 | } | 162 | } |
155 | } | 163 | } |
156 | 164 | ||
165 | // Clear and re-use an allocator. | ||
166 | TEST_CASE(mem_clear_then_reuse) { | ||
167 | test_pool mem; | ||
168 | mempool_make(&mem); | ||
169 | |||
170 | // Allocate chunks, contents not important. | ||
171 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
172 | int* chunk = mempool_alloc(&mem); | ||
173 | TEST_TRUE(chunk != 0); | ||
174 | } | ||
175 | |||
176 | mempool_clear(&mem); | ||
177 | |||
178 | // Allocate chunks and assign values 0..N. | ||
179 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
180 | int* chunk = mempool_alloc(&mem); | ||
181 | TEST_TRUE(chunk != 0); | ||
182 | *chunk = i + 1; | ||
183 | } | ||
184 | |||
185 | TEST_EQUAL(sum(&mem), NUM_BLOCKS * (NUM_BLOCKS + 1) / 2); | ||
186 | } | ||
187 | |||
157 | int main() { return 0; } | 188 | int main() { return 0; } |