Skip to content

Commit c2f831c

Browse files
Chandra Pratapgitster
authored andcommitted
t-reftable-pq: add test for index based comparison
When comparing two entries, the priority queue as defined by reftable/pq.{c, h} first compares the entries on the basis of their ref-record's keys. If the keys turn out to be equal, the comparison is then made on the basis of their update indices (which are never equal). In the current testing setup, only the case for comparison on the basis of ref-record's keys is exercised. Add a test for index-based comparison as well. Rename the existing test to reflect its nature of only testing record-based comparison. While at it, replace 'strbuf_detach' with 'xstrfmt' to assign refnames in the existing test. This makes the test conciser. 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 b37b71b commit c2f831c

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

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

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

21-
static void t_pq(void)
21+
static void t_pq_record(void)
2222
{
2323
struct merged_iter_pqueue pq = { 0 };
2424
struct reftable_record recs[54];
2525
size_t N = ARRAY_SIZE(recs) - 1, i;
2626
char *last = NULL;
2727

2828
for (i = 0; i < N; i++) {
29-
struct strbuf refname = STRBUF_INIT;
30-
strbuf_addf(&refname, "%02"PRIuMAX, (uintmax_t)i);
31-
3229
reftable_record_init(&recs[i], BLOCK_TYPE_REF);
33-
recs[i].u.ref.refname = strbuf_detach(&refname, NULL);
30+
recs[i].u.ref.refname = xstrfmt("%02"PRIuMAX, (uintmax_t)i);
3431
}
3532

3633
i = 1;
@@ -59,9 +56,48 @@ static void t_pq(void)
5956
merged_iter_pqueue_release(&pq);
6057
}
6158

59+
static void t_pq_index(void)
60+
{
61+
struct merged_iter_pqueue pq = { 0 };
62+
struct reftable_record recs[13];
63+
char *last = NULL;
64+
size_t N = ARRAY_SIZE(recs), i;
65+
66+
for (i = 0; i < N; i++) {
67+
reftable_record_init(&recs[i], BLOCK_TYPE_REF);
68+
recs[i].u.ref.refname = (char *) "refs/heads/master";
69+
}
70+
71+
i = 1;
72+
do {
73+
struct pq_entry e = {
74+
.rec = &recs[i],
75+
.index = i,
76+
};
77+
78+
merged_iter_pqueue_add(&pq, &e);
79+
merged_iter_pqueue_check(&pq);
80+
i = (i * 7) % N;
81+
} while (i != 1);
82+
83+
for (i = N - 1; i > 0; i--) {
84+
struct pq_entry e = merged_iter_pqueue_remove(&pq);
85+
merged_iter_pqueue_check(&pq);
86+
87+
check(reftable_record_type(e.rec) == BLOCK_TYPE_REF);
88+
check_int(e.index, ==, i);
89+
if (last)
90+
check_str(last, e.rec->u.ref.refname);
91+
last = e.rec->u.ref.refname;
92+
}
93+
94+
merged_iter_pqueue_release(&pq);
95+
}
96+
6297
int cmd_main(int argc, const char *argv[])
6398
{
64-
TEST(t_pq(), "pq works");
99+
TEST(t_pq_record(), "pq works with record-based comparison");
100+
TEST(t_pq_index(), "pq works with index-based comparison");
65101

66102
return test_done();
67103
}

0 commit comments

Comments
 (0)