From de8bbfdfa0f9e95ec2f9847762f5c009765b92f4 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 13 Jul 2023 08:24:25 -0700 Subject: Add tests for dyn and clear. --- mempool/test/mempool_test.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'mempool/test') 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) { return sum; } -// Create a pool. +// Create a statically-backed pool. TEST_CASE(mempool_create) { test_pool pool; mempool_make(&pool); } +// Create a dynamically-backed pool. +TEST_CASE(mem_create_dyn) { + DEF_MEMPOOL_DYN(dyn_pool, int); + + dyn_pool pool; + mempool_make_dyn(&pool, NUM_BLOCKS, sizeof(int)); +} + // Allocate all N blocks. TEST_CASE(mempool_allocate_until_full) { test_pool pool; @@ -154,4 +162,27 @@ TEST_CASE(mempool_get_block) { } } +// Clear and re-use an allocator. +TEST_CASE(mem_clear_then_reuse) { + test_pool mem; + mempool_make(&mem); + + // Allocate chunks, contents not important. + for (int i = 0; i < NUM_BLOCKS; ++i) { + int* chunk = mempool_alloc(&mem); + TEST_TRUE(chunk != 0); + } + + mempool_clear(&mem); + + // Allocate chunks and assign values 0..N. + for (int i = 0; i < NUM_BLOCKS; ++i) { + int* chunk = mempool_alloc(&mem); + TEST_TRUE(chunk != 0); + *chunk = i + 1; + } + + TEST_EQUAL(sum(&mem), NUM_BLOCKS * (NUM_BLOCKS + 1) / 2); +} + int main() { return 0; } -- cgit v1.2.3