public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: Seyi Kuforiji <kuforiji98@gmail•com>
To: git@vger•kernel.org
Cc: ps@pks•im, phillip.wood@dunelm•org.uk,
	Seyi Kuforiji <kuforiji98@gmail•com>
Subject: [PATCH v2 2/4] t/unit-tests: convert mem-pool test to use clar test framework
Date: Thu, 16 Jan 2025 17:15:57 +0100	[thread overview]
Message-ID: <20250116161559.91038-3-kuforiji98@gmail.com> (raw)
In-Reply-To: <20250116161559.91038-1-kuforiji98@gmail.com>

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>
---
 Makefile                  |  2 +-
 t/meson.build             |  2 +-
 t/unit-tests/t-mem-pool.c | 31 -------------------------------
 t/unit-tests/u-mem-pool.c | 25 +++++++++++++++++++++++++
 4 files changed, 27 insertions(+), 33 deletions(-)
 delete mode 100644 t/unit-tests/t-mem-pool.c
 create mode 100644 t/unit-tests/u-mem-pool.c

diff --git a/Makefile b/Makefile
index 97e8385b66..49ada4169d 100644
--- a/Makefile
+++ b/Makefile
@@ -1338,6 +1338,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/%
 THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/%
 
 CLAR_TEST_SUITES += u-ctype
+CLAR_TEST_SUITES += u-mem-pool
 CLAR_TEST_SUITES += u-strvec
 CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
 CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
@@ -1347,7 +1348,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
 UNIT_TEST_PROGRAMS += t-example-decorate
 UNIT_TEST_PROGRAMS += t-hash
 UNIT_TEST_PROGRAMS += t-hashmap
-UNIT_TEST_PROGRAMS += t-mem-pool
 UNIT_TEST_PROGRAMS += t-oid-array
 UNIT_TEST_PROGRAMS += t-oidmap
 UNIT_TEST_PROGRAMS += t-oidtree
diff --git a/t/meson.build b/t/meson.build
index 602ebfe6a2..ffe951f9be 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -1,5 +1,6 @@
 clar_test_suites = [
   'unit-tests/u-ctype.c',
+  'unit-tests/u-mem-pool.c',
   'unit-tests/u-strvec.c',
 ]
 
@@ -43,7 +44,6 @@ unit_test_programs = [
   'unit-tests/t-example-decorate.c',
   'unit-tests/t-hash.c',
   'unit-tests/t-hashmap.c',
-  'unit-tests/t-mem-pool.c',
   'unit-tests/t-oid-array.c',
   'unit-tests/t-oidmap.c',
   'unit-tests/t-oidtree.c',
diff --git a/t/unit-tests/t-mem-pool.c b/t/unit-tests/t-mem-pool.c
deleted file mode 100644
index fe500c704b..0000000000
--- a/t/unit-tests/t-mem-pool.c
+++ /dev/null
@@ -1,31 +0,0 @@
-#include "test-lib.h"
-#include "mem-pool.h"
-
-static void setup_static(void (*f)(struct mem_pool *), size_t block_alloc)
-{
-	struct mem_pool pool = { .block_alloc = block_alloc };
-	f(&pool);
-	mem_pool_discard(&pool, 0);
-}
-
-static void t_calloc_100(struct mem_pool *pool)
-{
-	size_t size = 100;
-	char *buffer = mem_pool_calloc(pool, 1, size);
-	for (size_t i = 0; i < size; i++)
-		check_int(buffer[i], ==, 0);
-	if (!check(pool->mp_block != NULL))
-		return;
-	check(pool->mp_block->next_free != NULL);
-	check(pool->mp_block->end != NULL);
-}
-
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
-{
-	TEST(setup_static(t_calloc_100, 1024 * 1024),
-	     "mem_pool_calloc returns 100 zeroed bytes with big block");
-	TEST(setup_static(t_calloc_100, 1),
-	     "mem_pool_calloc returns 100 zeroed bytes with tiny block");
-
-	return test_done();
-}
diff --git a/t/unit-tests/u-mem-pool.c b/t/unit-tests/u-mem-pool.c
new file mode 100644
index 0000000000..2bc2493b7e
--- /dev/null
+++ b/t/unit-tests/u-mem-pool.c
@@ -0,0 +1,25 @@
+#include "unit-test.h"
+#include "mem-pool.h"
+
+static void test_many_pool_allocations(size_t block_alloc)
+{
+	struct mem_pool pool = { .block_alloc = block_alloc };
+	size_t size = 100;
+	char *buffer = mem_pool_calloc(&pool, 1, size);
+	for (size_t i = 0; i < size; i++)
+		cl_assert_equal_i(0, buffer[i]);
+	cl_assert(pool.mp_block != NULL);
+	cl_assert(pool.mp_block->next_free != NULL);
+	cl_assert(pool.mp_block->end != NULL);
+	mem_pool_discard(&pool, 0);
+}
+
+void test_mem_pool__big_block(void)
+{
+	test_many_pool_allocations(1024 * 1024);
+}
+
+void test_mem_pool__tiny_block(void)
+{
+	test_many_pool_allocations(1);
+}
-- 
2.34.1


  parent reply	other threads:[~2025-01-16 16:16 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-16 10:49 [PATCH 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji
2025-01-16 10:49 ` [PATCH 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji
2025-01-16 13:12   ` Patrick Steinhardt
2025-01-16 10:49 ` [PATCH 2/4] t/unit-tests: convert mem-pool test to use clar test framework Seyi Kuforiji
2025-01-16 13:12   ` Patrick Steinhardt
2025-01-16 17:45     ` Junio C Hamano
2025-01-16 10:49 ` [PATCH 3/4] t/unit-tests: adapt priority queue " Seyi Kuforiji
2025-01-16 13:13   ` Patrick Steinhardt
2025-01-16 10:49 ` [PATCH 4/4] t/unit-tests: convert reftable tree " Seyi Kuforiji
2025-01-16 16:15 ` [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji
2025-01-16 16:15   ` [PATCH v2 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji
2025-01-17  6:27     ` Patrick Steinhardt
2025-01-16 16:15   ` Seyi Kuforiji [this message]
2025-01-16 16:15   ` [PATCH v2 3/4] t/unit-tests: adapt priority queue test to use clar test framework Seyi Kuforiji
2025-01-16 16:15   ` [PATCH v2 4/4] t/unit-tests: convert reftable tree " Seyi Kuforiji
2025-01-17  6:27   ` [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar Patrick Steinhardt
2025-01-17 12:29   ` Seyi Kuforiji
2025-01-17 12:29     ` [PATCH v3 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji
2025-01-17 12:29     ` [PATCH v3 2/4] t/unit-tests: convert mem-pool test to use clar test framework Seyi Kuforiji
2025-01-20 10:18       ` Karthik Nayak
2025-01-17 12:29     ` [PATCH v3 3/4] t/unit-tests: adapt priority queue " Seyi Kuforiji
2025-01-20 10:22       ` Karthik Nayak
2025-01-17 12:29     ` [PATCH v3 4/4] t/unit-tests: convert reftable tree " Seyi Kuforiji
2025-01-17 13:36     ` t/unit-tests: convert unit-tests to use clar Patrick Steinhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250116161559.91038-3-kuforiji98@gmail.com \
    --to=kuforiji98@gmail$(echo .)com \
    --cc=git@vger$(echo .)kernel.org \
    --cc=phillip.wood@dunelm$(echo .)org.uk \
    --cc=ps@pks$(echo .)im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox