t: move reftable/pq_test.c to the unit testing framework

reftable/pq_test.c exercises a priority queue defined by reftable/pq.{c, h}. Migrate reftable/pq_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework, and renaming the tests to align with unit-tests' standards. Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Chandra Pratap committed Aug 1, 2024 at 16:29 UTC a08ea27cd0efcaa4268f29026edfa162b14c73ad
4 files changed +17 -26
Makefile
+1 -1
@@ -1340,6 +1340,7 @@ UNIT_TEST_PROGRAMS += t-oidmap
1340 UNIT_TEST_PROGRAMS += t-oidtree
1341 UNIT_TEST_PROGRAMS += t-prio-queue
1342 UNIT_TEST_PROGRAMS += t-reftable-basics
1343 +UNIT_TEST_PROGRAMS += t-reftable-pq
1344 UNIT_TEST_PROGRAMS += t-reftable-record
1345 UNIT_TEST_PROGRAMS += t-strbuf
1346 UNIT_TEST_PROGRAMS += t-strcmp-offset
@@ -2681,7 +2682,6 @@ REFTABLE_OBJS += reftable/writer.o
2682 REFTABLE_TEST_OBJS += reftable/block_test.o
2683 REFTABLE_TEST_OBJS += reftable/dump.o
2684 REFTABLE_TEST_OBJS += reftable/merged_test.o
2684 -REFTABLE_TEST_OBJS += reftable/pq_test.o
2685 REFTABLE_TEST_OBJS += reftable/readwrite_test.o
2686 REFTABLE_TEST_OBJS += reftable/stack_test.o
2687 REFTABLE_TEST_OBJS += reftable/test_framework.o
reftable/reftable-tests.h
-1
@@ -12,7 +12,6 @@ https://developers.google.com/open-source/licenses/bsd
12 int basics_test_main(int argc, const char **argv);
13 int block_test_main(int argc, const char **argv);
14 int merged_test_main(int argc, const char **argv);
15 -int pq_test_main(int argc, const char **argv);
15 int record_test_main(int argc, const char **argv);
16 int readwrite_test_main(int argc, const char **argv);
17 int stack_test_main(int argc, const char **argv);
t/helper/test-reftable.c
-1
@@ -7,7 +7,6 @@ int cmd__reftable(int argc, const char **argv)
7 /* test from simple to complex. */
8 block_test_main(argc, argv);
9 tree_test_main(argc, argv);
10 - pq_test_main(argc, argv);
10 readwrite_test_main(argc, argv);
11 merged_test_main(argc, argv);
12 stack_test_main(argc, argv);
t/unit-tests/t-reftable-pq.c renamed
+16 -23
@@ -6,35 +6,28 @@ license that can be found in the LICENSE file or at
6 https://developers.google.com/open-source/licenses/bsd
7 */
8
9 -#include "system.h"
10 -
11 -#include "basics.h"
12 -#include "constants.h"
13 -#include "pq.h"
14 -#include "record.h"
15 -#include "reftable-tests.h"
16 -#include "test_framework.h"
9 +#include "test-lib.h"
10 +#include "reftable/constants.h"
11 +#include "reftable/pq.h"
12
13 void merged_iter_pqueue_check(struct merged_iter_pqueue pq)
14 {
20 - int i;
21 - for (i = 1; i < pq.len; i++) {
22 - int parent = (i - 1) / 2;
23 -
24 - EXPECT(pq_less(&pq.heap[parent], &pq.heap[i]));
15 + for (size_t i = 1; i < pq.len; i++) {
16 + size_t parent = (i - 1) / 2;
17 + check(pq_less(&pq.heap[parent], &pq.heap[i]));
18 }
19 }
20
28 -static void test_pq(void)
21 +static void t_pq(void)
22 {
30 - struct merged_iter_pqueue pq = { NULL };
23 + struct merged_iter_pqueue pq = { 0 };
24 struct reftable_record recs[54];
32 - int N = ARRAY_SIZE(recs) - 1, i;
25 + size_t N = ARRAY_SIZE(recs) - 1, i;
26 char *last = NULL;
27
28 for (i = 0; i < N; i++) {
29 struct strbuf refname = STRBUF_INIT;
37 - strbuf_addf(&refname, "%02d", i);
30 + strbuf_addf(&refname, "%02"PRIuMAX, (uintmax_t)i);
31
32 reftable_record_init(&recs[i], BLOCK_TYPE_REF);
33 recs[i].u.ref.refname = strbuf_detach(&refname, NULL);
@@ -48,7 +41,6 @@ static void test_pq(void)
41
42 merged_iter_pqueue_add(&pq, &e);
43 merged_iter_pqueue_check(pq);
51 -
44 i = (i * 7) % N;
45 } while (i != 1);
46
@@ -56,9 +48,9 @@ static void test_pq(void)
48 struct pq_entry e = merged_iter_pqueue_remove(&pq);
49 merged_iter_pqueue_check(pq);
50
59 - EXPECT(reftable_record_type(e.rec) == BLOCK_TYPE_REF);
51 + check(reftable_record_type(e.rec) == BLOCK_TYPE_REF);
52 if (last)
61 - EXPECT(strcmp(last, e.rec->u.ref.refname) < 0);
53 + check_int(strcmp(last, e.rec->u.ref.refname), <, 0);
54 last = e.rec->u.ref.refname;
55 }
56
@@ -67,8 +59,9 @@ static void test_pq(void)
59 merged_iter_pqueue_release(&pq);
60 }
61
70 -int pq_test_main(int argc, const char *argv[])
62 +int cmd_main(int argc, const char *argv[])
63 {
72 - RUN_TEST(test_pq);
73 - return 0;
64 + TEST(t_pq(), "pq works");
65 +
66 + return test_done();
67 }