t-reftable-pq: add tests for merged_iter_pqueue_top()

merged_iter_pqueue_top() as defined by reftable/pq.{c, h} returns the element at the top of a priority-queue's heap without removing it. Since there are no tests for this function in the existing setup, add tests for the same. 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 0dc84a806c53b02b465616d26efbca73682c58dd
1 file changed +49
t/unit-tests/t-reftable-pq.c
+49
@@ -18,6 +18,11 @@ static void merged_iter_pqueue_check(const struct merged_iter_pqueue *pq)
18 }
19 }
20
21 +static int pq_entry_equal(struct pq_entry *a, struct pq_entry *b)
22 +{
23 + return !reftable_record_cmp(a->rec, b->rec) && (a->index == b->index);
24 +}
25 +
26 static void t_pq_record(void)
27 {
28 struct merged_iter_pqueue pq = { 0 };
@@ -42,9 +47,11 @@ static void t_pq_record(void)
47 } while (i != 1);
48
49 while (!merged_iter_pqueue_is_empty(pq)) {
50 + struct pq_entry top = merged_iter_pqueue_top(pq);
51 struct pq_entry e = merged_iter_pqueue_remove(&pq);
52 merged_iter_pqueue_check(&pq);
53
54 + check(pq_entry_equal(&top, &e));
55 check(reftable_record_type(e.rec) == BLOCK_TYPE_REF);
56 if (last)
57 check_int(strcmp(last, e.rec->u.ref.refname), <, 0);
@@ -81,9 +88,11 @@ static void t_pq_index(void)
88 } while (i != 1);
89
90 for (i = N - 1; i > 0; i--) {
91 + struct pq_entry top = merged_iter_pqueue_top(pq);
92 struct pq_entry e = merged_iter_pqueue_remove(&pq);
93 merged_iter_pqueue_check(&pq);
94
95 + check(pq_entry_equal(&top, &e));
96 check(reftable_record_type(e.rec) == BLOCK_TYPE_REF);
97 check_int(e.index, ==, i);
98 if (last)
@@ -94,10 +103,50 @@ static void t_pq_index(void)
103 merged_iter_pqueue_release(&pq);
104 }
105
106 +static void t_merged_iter_pqueue_top(void)
107 +{
108 + struct merged_iter_pqueue pq = { 0 };
109 + struct reftable_record recs[13];
110 + size_t N = ARRAY_SIZE(recs), i;
111 +
112 + for (i = 0; i < N; i++) {
113 + reftable_record_init(&recs[i], BLOCK_TYPE_REF);
114 + recs[i].u.ref.refname = (char *) "refs/heads/master";
115 + }
116 +
117 + i = 1;
118 + do {
119 + struct pq_entry e = {
120 + .rec = &recs[i],
121 + .index = i,
122 + };
123 +
124 + merged_iter_pqueue_add(&pq, &e);
125 + merged_iter_pqueue_check(&pq);
126 + i = (i * 7) % N;
127 + } while (i != 1);
128 +
129 + for (i = N - 1; i > 0; i--) {
130 + struct pq_entry top = merged_iter_pqueue_top(pq);
131 + struct pq_entry e = merged_iter_pqueue_remove(&pq);
132 +
133 + merged_iter_pqueue_check(&pq);
134 + check(pq_entry_equal(&top, &e));
135 + check(reftable_record_equal(top.rec, &recs[i], GIT_SHA1_RAWSZ));
136 + for (size_t j = 0; i < pq.len; j++) {
137 + check(pq_less(&top, &pq.heap[j]));
138 + check_int(top.index, >, j);
139 + }
140 + }
141 +
142 + merged_iter_pqueue_release(&pq);
143 +}
144 +
145 int cmd_main(int argc, const char *argv[])
146 {
147 TEST(t_pq_record(), "pq works with record-based comparison");
148 TEST(t_pq_index(), "pq works with index-based comparison");
149 + TEST(t_merged_iter_pqueue_top(), "merged_iter_pqueue_top works");
150
151 return test_done();
152 }