Subject: [PATCH v4 05/10] t/unit-tests: convert reftable pq test to use clar

From: Seyi Kuforiji <[email-masked]>
Date: Thu, 5 Jun 2025 15:06:39 +0100

Adapt reftable priority queue test file to use clar by using clar
assertions where necessary.

Signed-off-by: Seyi Kuforiji <[email-masked]>
---
 Makefile                                      |  2 +-
 t/meson.build                                 |  3 +-
 .../{t-reftable-pq.c => u-reftable-pq.c}      | 59 +++++++++----------
 3 files changed, 30 insertions(+), 34 deletions(-)
 rename t/unit-tests/{t-reftable-pq.c => u-reftable-pq.c} (64%)

diff --git a/Makefile b/Makefile
index fb0ef10ff2..3cccc73073 100644
--- a/Makefile
+++ b/Makefile
@@ -1367,6 +1367,7 @@ CLAR_TEST_SUITES += u-prio-queue
 CLAR_TEST_SUITES += u-reftable-basics
 CLAR_TEST_SUITES += u-reftable-block
 CLAR_TEST_SUITES += u-reftable-merged
+CLAR_TEST_SUITES += u-reftable-pq
 CLAR_TEST_SUITES += u-reftable-tree
 CLAR_TEST_SUITES += u-strbuf
 CLAR_TEST_SUITES += u-strcmp-offset
@@ -1380,7 +1381,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-reftable-clar.o
 
-UNIT_TEST_PROGRAMS += t-reftable-pq
 UNIT_TEST_PROGRAMS += t-reftable-readwrite
 UNIT_TEST_PROGRAMS += t-reftable-record
 UNIT_TEST_PROGRAMS += t-reftable-stack
diff --git a/t/meson.build b/t/meson.build
index d25dfb0c92..ac4394b7a2 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -11,6 +11,7 @@ clar_test_suites = [
   'unit-tests/u-reftable-basics.c',
   'unit-tests/u-reftable-block.c',
   'unit-tests/u-reftable-merged.c',
+  'unit-tests/u-reftable-pq.c',
   'unit-tests/u-reftable-tree.c',
   'unit-tests/u-strbuf.c',
   'unit-tests/u-strcmp-offset.c',
@@ -58,7 +59,7 @@ clar_unit_tests = executable('unit-tests',
 test('unit-tests', clar_unit_tests)
 
 unit_test_programs = [
-  'unit-tests/t-reftable-pq.c',
+  'unit-tests/t-reftable-reader.c',
   'unit-tests/t-reftable-readwrite.c',
   'unit-tests/t-reftable-record.c',
   'unit-tests/t-reftable-stack.c',
diff --git a/t/unit-tests/t-reftable-pq.c b/t/unit-tests/u-reftable-pq.c
similarity index 64%
rename from t/unit-tests/t-reftable-pq.c
rename to t/unit-tests/u-reftable-pq.c
index fb5a4eb187..d55792ca6f 100644
--- a/t/unit-tests/t-reftable-pq.c
+++ b/t/unit-tests/u-reftable-pq.c
@@ -6,7 +6,8 @@ license that can be found in the LICENSE file or at
 https://developers.google.com/open-source/licenses/bsd
 */
 
-#include "test-lib.h"
+#include "unit-test.h"
+#include "lib-reftable-clar.h"
 #include "reftable/constants.h"
 #include "reftable/pq.h"
 #include "strbuf.h"
@@ -15,18 +16,18 @@ static void merged_iter_pqueue_check(const struct merged_iter_pqueue *pq)
 {
 	for (size_t i = 1; i < pq->len; i++) {
 		size_t parent = (i - 1) / 2;
-		check(pq_less(&pq->heap[parent], &pq->heap[i]));
+		cl_assert(pq_less(&pq->heap[parent], &pq->heap[i]) != 0);
 	}
 }
 
 static int pq_entry_equal(struct pq_entry *a, struct pq_entry *b)
 {
 	int cmp;
-	check(!reftable_record_cmp(a->rec, b->rec, &cmp));
+	cl_assert_equal_i(reftable_record_cmp(a->rec, b->rec, &cmp), 0);
 	return !cmp && (a->index == b->index);
 }
 
-static void t_pq_record(void)
+void test_reftable_pq__record(void)
 {
 	struct merged_iter_pqueue pq = { 0 };
 	struct reftable_record recs[54];
@@ -34,7 +35,8 @@ static void t_pq_record(void)
 	char *last = NULL;
 
 	for (i = 0; i < N; i++) {
-		check(!reftable_record_init(&recs[i], REFTABLE_BLOCK_TYPE_REF));
+		cl_assert(!reftable_record_init(&recs[i],
+						REFTABLE_BLOCK_TYPE_REF));
 		recs[i].u.ref.refname = xstrfmt("%02"PRIuMAX, (uintmax_t)i);
 	}
 
@@ -53,13 +55,13 @@ static void t_pq_record(void)
 		struct pq_entry top = merged_iter_pqueue_top(pq);
 		struct pq_entry e;
 
-		check(!merged_iter_pqueue_remove(&pq, &e));
+		cl_assert_equal_i(merged_iter_pqueue_remove(&pq, &e), 0);
 		merged_iter_pqueue_check(&pq);
 
-		check(pq_entry_equal(&top, &e));
-		check(reftable_record_type(e.rec) == REFTABLE_BLOCK_TYPE_REF);
+		cl_assert(pq_entry_equal(&top, &e));
+		cl_assert(reftable_record_type(e.rec) == REFTABLE_BLOCK_TYPE_REF);
 		if (last)
-			check_int(strcmp(last, e.rec->u.ref.refname), <, 0);
+			cl_assert(strcmp(last, e.rec->u.ref.refname) < 0);
 		last = e.rec->u.ref.refname;
 	}
 
@@ -68,7 +70,7 @@ static void t_pq_record(void)
 	merged_iter_pqueue_release(&pq);
 }
 
-static void t_pq_index(void)
+void test_reftable_pq__index(void)
 {
 	struct merged_iter_pqueue pq = { 0 };
 	struct reftable_record recs[13];
@@ -76,7 +78,8 @@ static void t_pq_index(void)
 	size_t N = ARRAY_SIZE(recs), i;
 
 	for (i = 0; i < N; i++) {
-		check(!reftable_record_init(&recs[i], REFTABLE_BLOCK_TYPE_REF));
+		cl_assert(!reftable_record_init(&recs[i],
+						REFTABLE_BLOCK_TYPE_REF));
 		recs[i].u.ref.refname = (char *) "refs/heads/master";
 	}
 
@@ -96,28 +99,29 @@ static void t_pq_index(void)
 		struct pq_entry top = merged_iter_pqueue_top(pq);
 		struct pq_entry e;
 
-		check(!merged_iter_pqueue_remove(&pq, &e));
+		cl_assert_equal_i(merged_iter_pqueue_remove(&pq, &e), 0);
 		merged_iter_pqueue_check(&pq);
 
-		check(pq_entry_equal(&top, &e));
-		check(reftable_record_type(e.rec) == REFTABLE_BLOCK_TYPE_REF);
-		check_int(e.index, ==, i);
+		cl_assert(pq_entry_equal(&top, &e));
+		cl_assert(reftable_record_type(e.rec) == REFTABLE_BLOCK_TYPE_REF);
+		cl_assert_equal_i(e.index, i);
 		if (last)
-			check_str(last, e.rec->u.ref.refname);
+			cl_assert_equal_s(last, e.rec->u.ref.refname);
 		last = e.rec->u.ref.refname;
 	}
 
 	merged_iter_pqueue_release(&pq);
 }
 
-static void t_merged_iter_pqueue_top(void)
+void test_reftable_pq__merged_iter_pqueue_top(void)
 {
 	struct merged_iter_pqueue pq = { 0 };
 	struct reftable_record recs[13];
 	size_t N = ARRAY_SIZE(recs), i;
 
 	for (i = 0; i < N; i++) {
-		check(!reftable_record_init(&recs[i], REFTABLE_BLOCK_TYPE_REF));
+		cl_assert(!reftable_record_init(&recs[i],
+						REFTABLE_BLOCK_TYPE_REF));
 		recs[i].u.ref.refname = (char *) "refs/heads/master";
 	}
 
@@ -137,25 +141,16 @@ static void t_merged_iter_pqueue_top(void)
 		struct pq_entry top = merged_iter_pqueue_top(pq);
 		struct pq_entry e;
 
-		check(!merged_iter_pqueue_remove(&pq, &e));
+		cl_assert_equal_i(merged_iter_pqueue_remove(&pq, &e), 0);
 
 		merged_iter_pqueue_check(&pq);
-		check(pq_entry_equal(&top, &e));
-		check(reftable_record_equal(top.rec, &recs[i], REFTABLE_HASH_SIZE_SHA1));
+		cl_assert(pq_entry_equal(&top, &e) != 0);
+		cl_assert(reftable_record_equal(top.rec, &recs[i], REFTABLE_HASH_SIZE_SHA1) != 0);
 		for (size_t j = 0; i < pq.len; j++) {
-			check(pq_less(&top, &pq.heap[j]));
-			check_int(top.index, >, j);
+			cl_assert(pq_less(&top, &pq.heap[j]) != 0);
+			cl_assert(top.index > j);
 		}
 	}
 
 	merged_iter_pqueue_release(&pq);
 }
-
-int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
-{
-	TEST(t_pq_record(), "pq works with record-based comparison");
-	TEST(t_pq_index(), "pq works with index-based comparison");
-	TEST(t_merged_iter_pqueue_top(), "merged_iter_pqueue_top works");
-
-	return test_done();
-}
-- 
2.43.0