t/unit-tests: convert mem-pool test to use clar test framework

Adapt the mem-pool test script to use clar framework by using clar assertions where necessary.Test functions are created as a standalone to test different test cases. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> Acked-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Seyi Kuforiji committed Jan 17, 2025 at 13:29 UTC c143dfa7ed4115d1fd7d23150a0314768377f108
4 files changed +27 -33
Makefile
+1 -1
@@ -1338,6 +1338,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/%
1338 THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/%
1339
1340 CLAR_TEST_SUITES += u-ctype
1341 +CLAR_TEST_SUITES += u-mem-pool
1342 CLAR_TEST_SUITES += u-strvec
1343 CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
1344 CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
@@ -1347,7 +1348,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
1348 UNIT_TEST_PROGRAMS += t-example-decorate
1349 UNIT_TEST_PROGRAMS += t-hash
1350 UNIT_TEST_PROGRAMS += t-hashmap
1350 -UNIT_TEST_PROGRAMS += t-mem-pool
1351 UNIT_TEST_PROGRAMS += t-oid-array
1352 UNIT_TEST_PROGRAMS += t-oidmap
1353 UNIT_TEST_PROGRAMS += t-oidtree
t/meson.build
+1 -1
@@ -1,5 +1,6 @@
1 clar_test_suites = [
2 'unit-tests/u-ctype.c',
3 + 'unit-tests/u-mem-pool.c',
4 'unit-tests/u-strvec.c',
5 ]
6
@@ -43,7 +44,6 @@ unit_test_programs = [
44 'unit-tests/t-example-decorate.c',
45 'unit-tests/t-hash.c',
46 'unit-tests/t-hashmap.c',
46 - 'unit-tests/t-mem-pool.c',
47 'unit-tests/t-oid-array.c',
48 'unit-tests/t-oidmap.c',
49 'unit-tests/t-oidtree.c',
t/unit-tests/t-mem-pool.c deleted
-31
@@ -1,31 +0,0 @@
1 -#include "test-lib.h"
2 -#include "mem-pool.h"
3 -
4 -static void setup_static(void (*f)(struct mem_pool *), size_t block_alloc)
5 -{
6 - struct mem_pool pool = { .block_alloc = block_alloc };
7 - f(&pool);
8 - mem_pool_discard(&pool, 0);
9 -}
10 -
11 -static void t_calloc_100(struct mem_pool *pool)
12 -{
13 - size_t size = 100;
14 - char *buffer = mem_pool_calloc(pool, 1, size);
15 - for (size_t i = 0; i < size; i++)
16 - check_int(buffer[i], ==, 0);
17 - if (!check(pool->mp_block != NULL))
18 - return;
19 - check(pool->mp_block->next_free != NULL);
20 - check(pool->mp_block->end != NULL);
21 -}
22 -
23 -int cmd_main(int argc UNUSED, const char **argv UNUSED)
24 -{
25 - TEST(setup_static(t_calloc_100, 1024 * 1024),
26 - "mem_pool_calloc returns 100 zeroed bytes with big block");
27 - TEST(setup_static(t_calloc_100, 1),
28 - "mem_pool_calloc returns 100 zeroed bytes with tiny block");
29 -
30 - return test_done();
31 -}
t/unit-tests/u-mem-pool.c new
+25
@@ -0,0 +1,25 @@
1 +#include "unit-test.h"
2 +#include "mem-pool.h"
3 +
4 +static void test_many_pool_allocations(size_t block_alloc)
5 +{
6 + struct mem_pool pool = { .block_alloc = block_alloc };
7 + size_t size = 100;
8 + char *buffer = mem_pool_calloc(&pool, 1, size);
9 + for (size_t i = 0; i < size; i++)
10 + cl_assert_equal_i(0, buffer[i]);
11 + cl_assert(pool.mp_block != NULL);
12 + cl_assert(pool.mp_block->next_free != NULL);
13 + cl_assert(pool.mp_block->end != NULL);
14 + mem_pool_discard(&pool, 0);
15 +}
16 +
17 +void test_mem_pool__big_block(void)
18 +{
19 + test_many_pool_allocations(1024 * 1024);
20 +}
21 +
22 +void test_mem_pool__tiny_block(void)
23 +{
24 + test_many_pool_allocations(1);
25 +}