Skip to content

Commit fa0d358

Browse files
author
Chandra Pratap
committed
t-reftable-stack: add test for stack iterators
reftable_stack_init_ref_iterator and reftable_stack_init_log_iterator as defined by reftable/stack.{c,h} initialize a stack iterator to iterate over the ref and log records in a reftable stack respectively. Since these functions are not exercised by any of the existing tests, add a test for them. Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
1 parent 028fa6f commit fa0d358

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,85 @@ static void t_reftable_stack_add(void)
550550
clear_dir(dir);
551551
}
552552

553+
static void t_reftable_stack_iterator(void)
554+
{
555+
struct reftable_write_options opts = { 0 };
556+
struct reftable_stack *st = NULL;
557+
char *dir = get_tmp_dir(__LINE__);
558+
struct reftable_ref_record refs[10] = { 0 };
559+
struct reftable_log_record logs[10] = { 0 };
560+
struct reftable_iterator it = { 0 };
561+
size_t N = ARRAY_SIZE(refs), i;
562+
int err;
563+
564+
err = reftable_new_stack(&st, dir, &opts);
565+
check(!err);
566+
567+
for (i = 0; i < N; i++) {
568+
refs[i].refname = xstrfmt("branch%02"PRIuMAX, (uintmax_t)i);
569+
refs[i].update_index = i + 1;
570+
refs[i].value_type = REFTABLE_REF_VAL1;
571+
set_test_hash(refs[i].value.val1, i);
572+
573+
logs[i].refname = xstrfmt("branch%02"PRIuMAX, (uintmax_t)i);
574+
logs[i].update_index = i + 1;
575+
logs[i].value_type = REFTABLE_LOG_UPDATE;
576+
logs[i].value.update.email = xstrdup("johndoe@invalid");
577+
logs[i].value.update.message = xstrdup("commit\n");
578+
set_test_hash(logs[i].value.update.new_hash, i);
579+
}
580+
581+
for (i = 0; i < N; i++) {
582+
err = reftable_stack_add(st, write_test_ref, &refs[i]);
583+
check(!err);
584+
}
585+
586+
for (i = 0; i < N; i++) {
587+
struct write_log_arg arg = {
588+
.log = &logs[i],
589+
.update_index = reftable_stack_next_update_index(st),
590+
};
591+
err = reftable_stack_add(st, write_test_log, &arg);
592+
check(!err);
593+
}
594+
595+
reftable_stack_init_ref_iterator(st, &it);
596+
reftable_iterator_seek_ref(&it, refs[0].refname);
597+
for (i = 0; ; i++) {
598+
struct reftable_ref_record ref = { 0 };
599+
err = reftable_iterator_next_ref(&it, &ref);
600+
if (err > 0)
601+
break;
602+
check(!err);
603+
check(reftable_ref_record_equal(&ref, &refs[i], GIT_SHA1_RAWSZ));
604+
reftable_ref_record_release(&ref);
605+
}
606+
check_int(i, ==, N);
607+
608+
reftable_iterator_destroy(&it);
609+
610+
reftable_stack_init_log_iterator(st, &it);
611+
reftable_iterator_seek_log(&it, logs[0].refname);
612+
for (i = 0; ; i++) {
613+
struct reftable_log_record log = { 0 };
614+
err = reftable_iterator_next_log(&it, &log);
615+
if (err > 0)
616+
break;
617+
check(!err);
618+
check(reftable_log_record_equal(&log, &logs[i], GIT_SHA1_RAWSZ));
619+
reftable_log_record_release(&log);
620+
}
621+
check_int(i, ==, N);
622+
623+
reftable_stack_destroy(st);
624+
reftable_iterator_destroy(&it);
625+
for (i = 0; i < N; i++) {
626+
reftable_ref_record_release(&refs[i]);
627+
reftable_log_record_release(&logs[i]);
628+
}
629+
clear_dir(dir);
630+
}
631+
553632
static void t_reftable_stack_log_normalize(void)
554633
{
555634
int err = 0;
@@ -1125,6 +1204,7 @@ int cmd_main(int argc, const char *argv[])
11251204
TEST(t_reftable_stack_compaction_concurrent_clean(), "compaction with unclean stack shutdown");
11261205
TEST(t_reftable_stack_compaction_with_locked_tables(), "compaction with locked tables");
11271206
TEST(t_reftable_stack_hash_id(), "read stack with wrong hash ID");
1207+
TEST(t_reftable_stack_iterator(), "log and ref iterator for reftable stack");
11281208
TEST(t_reftable_stack_lock_failure(), "stack addition with lockfile failure");
11291209
TEST(t_reftable_stack_log_normalize(), "log messages should be normalized");
11301210
TEST(t_reftable_stack_tombstone(), "'tombstone' refs in stack");

0 commit comments

Comments
 (0)