The reftable library comes with self tests, which are exercised as part of the usual end-to-end tests and are designed to observe the end-user visible effects of Git commands. What it exercises, however, is a better match for the unit-testing framework, merged at 8bf6fbd0 (Merge branch 'js/doc-unit-tests', 2023-12-09), which is designed to observe how low level implementation details, at the level of sequences of individual function calls, behave. Hence, port reftable/pq_test.c to the unit testing framework and improve upon the ported test. The first two patches in the series are preparatory cleanup, the third patch moves the test to the unit testing framework, and the rest of the patches improve upon the ported test. Mentored-by: Patrick Steinhardt <ps@xxxxxx> Mentored-by: Christian Couder <chriscool@xxxxxxxxxxxxx> Signed-off-by: Chandra Pratap <chandrapratap3519@xxxxxxxxx> --- Changes in v6: - Use modulo loop to pseudo-randomly insert elements in t_pq_index() in patch 6. - Use 'xstrfmt' for refnames instead of using a 'strbuf' in t_pq_record() in patch 6. - Use string literals instead of 'xstrdup' for refnames in t_pq_index() and t_merged_iter_pqueue_top() in patch 6 & 7. - Use modulo loop to pseudo-randomly insert elements in t_merged_iter_pqueue_top() in patch 7 CI/PR for v6: https://github.com/gitgitgadget/git/pull/1745 Chandra Pratap(7): reftable: remove unncessary curly braces in reftable/pq.c reftable: change the type of array indices to 'size_t' in reftable/pq.c t: move reftable/pq_test.c to the unit testing framework t-reftable-pq: make merged_iter_pqueue_check() static t-reftable-pq: make merged_iter_pqueue_check() callable by reference t-reftable-pq: add test for index based comparison t-reftable-pq: add tests for merged_iter_pqueue_top() Makefile | 2 +- reftable/pq.c | 29 +++----- reftable/pq.h | 1 - reftable/pq_test.c | 74 --------------------- reftable/reftable-tests.h | 1 - t/helper/test-reftable.c | 1 - t/unit-tests/t-reftable-pq.c | 154 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 165 insertions(+), 97 deletions(-) Range-diff against v5: 1: 94a77f5a60 ! 1: f5c30187e1 t-reftable-pq: add test for index based comparison @@ Commit message 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 <ps@xxxxxx> Mentored-by: Christian Couder <chriscool@xxxxxxxxxxxxx> Signed-off-by: Chandra Pratap <chandrapratap3519@xxxxxxxxx> @@ t/unit-tests/t-reftable-pq.c: static void merged_iter_pqueue_check(const struct { struct merged_iter_pqueue pq = { 0 }; struct reftable_record recs[54]; +@@ t/unit-tests/t-reftable-pq.c: static void t_pq(void) + char *last = NULL; + + for (i = 0; i < N; i++) { +- struct strbuf refname = STRBUF_INIT; +- strbuf_addf(&refname, "%02"PRIuMAX, (uintmax_t)i); +- + reftable_record_init(&recs[i], BLOCK_TYPE_REF); +- recs[i].u.ref.refname = strbuf_detach(&refname, NULL); ++ recs[i].u.ref.refname = xstrfmt("%02"PRIuMAX, (uintmax_t)i); + } + + i = 1; @@ t/unit-tests/t-reftable-pq.c: static void t_pq(void) merged_iter_pqueue_release(&pq); } @@ t/unit-tests/t-reftable-pq.c: static void t_pq(void) +static void t_pq_index(void) +{ + struct merged_iter_pqueue pq = { 0 }; -+ struct reftable_record recs[14]; ++ struct reftable_record recs[13]; + char *last = NULL; + size_t N = ARRAY_SIZE(recs), i; + + for (i = 0; i < N; i++) { + reftable_record_init(&recs[i], BLOCK_TYPE_REF); -+ recs[i].u.ref.refname = xstrdup("refs/heads/master"); ++ recs[i].u.ref.refname = (char *) "refs/heads/master"; + } + -+ for (i = 0; i < N; i++) { ++ i = 1; ++ do { + struct pq_entry e = { + .rec = &recs[i], + .index = i, @@ t/unit-tests/t-reftable-pq.c: static void t_pq(void) + + merged_iter_pqueue_add(&pq, &e); + merged_iter_pqueue_check(&pq); ++ i = (i * 7) % N; + } ++ while (i != 1); + -+ for (i = N - 1; !merged_iter_pqueue_is_empty(pq); i--) { ++ for (i = N - 1; i > 0; i--) { + struct pq_entry e = merged_iter_pqueue_remove(&pq); + merged_iter_pqueue_check(&pq); + @@ t/unit-tests/t-reftable-pq.c: static void t_pq(void) + last = e.rec->u.ref.refname; + } + -+ for (i = 0; i < N; i++) -+ reftable_record_release(&recs[i]); + merged_iter_pqueue_release(&pq); +} + 2: 9a76f87bd1 ! 2: 394540a789 t-reftable-pq: add tests for merged_iter_pqueue_top() @@ t/unit-tests/t-reftable-pq.c: static void t_pq_record(void) if (last) check_int(strcmp(last, e.rec->u.ref.refname), <, 0); @@ t/unit-tests/t-reftable-pq.c: static void t_pq_index(void) - } + while (i != 1); - for (i = N - 1; !merged_iter_pqueue_is_empty(pq); i--) { + for (i = N - 1; i > 0; i--) { + struct pq_entry top = merged_iter_pqueue_top(pq); struct pq_entry e = merged_iter_pqueue_remove(&pq); merged_iter_pqueue_check(&pq); @@ t/unit-tests/t-reftable-pq.c: static void t_pq_index(void) +static void t_merged_iter_pqueue_top(void) +{ + struct merged_iter_pqueue pq = { 0 }; -+ struct reftable_record recs[14]; ++ struct reftable_record recs[13]; + size_t N = ARRAY_SIZE(recs), i; + + for (i = 0; i < N; i++) { + reftable_record_init(&recs[i], BLOCK_TYPE_REF); -+ recs[i].u.ref.refname = xstrdup("refs/heads/master"); ++ recs[i].u.ref.refname = (char *) "refs/heads/master"; + } + -+ for (i = 0; i < N; i++) { ++ i = 1; ++ do { + struct pq_entry e = { + .rec = &recs[i], + .index = i, @@ t/unit-tests/t-reftable-pq.c: static void t_pq_index(void) + + merged_iter_pqueue_add(&pq, &e); + merged_iter_pqueue_check(&pq); ++ i = (i * 7) % N; + } ++ while (i != 1); + -+ for (i = N - 1; !merged_iter_pqueue_is_empty(pq); i--) { ++ for (i = N - 1; i > 0; i--) { + struct pq_entry top = merged_iter_pqueue_top(pq); + struct pq_entry e = merged_iter_pqueue_remove(&pq); + @@ t/unit-tests/t-reftable-pq.c: static void t_pq_index(void) + } + } + -+ for (i = 0; i < N; i++) -+ reftable_record_release(&recs[i]); + merged_iter_pqueue_release(&pq); +} +