Skip to content

Commit 0dc84a8

Browse files
Chandra Pratapgitster
authored andcommitted
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 <[email protected]> Mentored-by: Christian Couder <[email protected]> Signed-off-by: Chandra Pratap <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c2f831c commit 0dc84a8

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

t/unit-tests/t-reftable-pq.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ static void merged_iter_pqueue_check(const struct merged_iter_pqueue *pq)
1818
}
1919
}
2020

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+
2126
static void t_pq_record(void)
2227
{
2328
struct merged_iter_pqueue pq = { 0 };
@@ -42,9 +47,11 @@ static void t_pq_record(void)
4247
} while (i != 1);
4348

4449
while (!merged_iter_pqueue_is_empty(pq)) {
50+
struct pq_entry top = merged_iter_pqueue_top(pq);
4551
struct pq_entry e = merged_iter_pqueue_remove(&pq);
4652
merged_iter_pqueue_check(&pq);
4753

54+
check(pq_entry_equal(&top, &e));
4855
check(reftable_record_type(e.rec) == BLOCK_TYPE_REF);
4956
if (last)
5057
check_int(strcmp(last, e.rec->u.ref.refname), <, 0);
@@ -81,9 +88,11 @@ static void t_pq_index(void)
8188
} while (i != 1);
8289

8390
for (i = N - 1; i > 0; i--) {
91+
struct pq_entry top = merged_iter_pqueue_top(pq);
8492
struct pq_entry e = merged_iter_pqueue_remove(&pq);
8593
merged_iter_pqueue_check(&pq);
8694

95+
check(pq_entry_equal(&top, &e));
8796
check(reftable_record_type(e.rec) == BLOCK_TYPE_REF);
8897
check_int(e.index, ==, i);
8998
if (last)
@@ -94,10 +103,50 @@ static void t_pq_index(void)
94103
merged_iter_pqueue_release(&pq);
95104
}
96105

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+
97145
int cmd_main(int argc, const char *argv[])
98146
{
99147
TEST(t_pq_record(), "pq works with record-based comparison");
100148
TEST(t_pq_index(), "pq works with index-based comparison");
149+
TEST(t_merged_iter_pqueue_top(), "merged_iter_pqueue_top works");
101150

102151
return test_done();
103152
}

0 commit comments

Comments
 (0)