diff mbox series

[6/6] t-reftable-stack: add test for stack iterators

Message ID 20240806142020.4615-7-chandrapratap3519@gmail.com (mailing list archive)
State Superseded
Headers show
Series t: port reftable/stack_test.c to the unit testing framework | expand

Commit Message

Chandra Pratap Aug. 6, 2024, 2:13 p.m. UTC
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>
---
 t/unit-tests/t-reftable-stack.c | 82 +++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)

Comments

Patrick Steinhardt Aug. 7, 2024, 5:23 a.m. UTC | #1
On Tue, Aug 06, 2024 at 07:43:42PM +0530, Chandra Pratap wrote:
> reftable_stack_init_ref_iterator and reftable_stack_init_log_iterator
> as defined by reftable/stack.{c, h} initialize a stack iterator to

Same remark here that Junio posted on a preceding catch, please drop the
space in `stack.{c,h}`.

> @@ -521,6 +521,87 @@ static void t_reftable_stack_add(void)
>  	clear_dir(dir);
>  }
>  
> +static void t_reftable_stack_iterator(void)
> +{
> +	struct reftable_write_options opts = { 0 };
> +	struct reftable_stack *st = NULL;
> +	char *dir = get_tmp_dir(__LINE__);
> +	struct reftable_ref_record refs[10] = { { 0 } };
> +	struct reftable_log_record logs[10] = { { 0 } };

Again, these should be initialized with `= { 0 }`.

> +	struct reftable_iterator it = { 0 };
> +	size_t N = ARRAY_SIZE(refs), i;
> +	int err;
> +
> +	err = reftable_new_stack(&st, dir, &opts);
> +	check(!err);
> +
> +	for (i = 0; i < N; i++) {
> +		char buf[20];
> +		xsnprintf(buf, sizeof(buf), "branch%02"PRIuMAX, (uintmax_t)i);
> +		refs[i].refname = xstrdup(buf);

This can be simplified to a single call to `xstrfmt()`.

Patrick
diff mbox series

Patch

diff --git a/t/unit-tests/t-reftable-stack.c b/t/unit-tests/t-reftable-stack.c
index 5228872450..1ec8e4afc0 100644
--- a/t/unit-tests/t-reftable-stack.c
+++ b/t/unit-tests/t-reftable-stack.c
@@ -521,6 +521,87 @@  static void t_reftable_stack_add(void)
 	clear_dir(dir);
 }
 
+static void t_reftable_stack_iterator(void)
+{
+	struct reftable_write_options opts = { 0 };
+	struct reftable_stack *st = NULL;
+	char *dir = get_tmp_dir(__LINE__);
+	struct reftable_ref_record refs[10] = { { 0 } };
+	struct reftable_log_record logs[10] = { { 0 } };
+	struct reftable_iterator it = { 0 };
+	size_t N = ARRAY_SIZE(refs), i;
+	int err;
+
+	err = reftable_new_stack(&st, dir, &opts);
+	check(!err);
+
+	for (i = 0; i < N; i++) {
+		char buf[20];
+		xsnprintf(buf, sizeof(buf), "branch%02"PRIuMAX, (uintmax_t)i);
+		refs[i].refname = xstrdup(buf);
+		refs[i].update_index = i + 1;
+		refs[i].value_type = REFTABLE_REF_VAL1;
+		set_test_hash(refs[i].value.val1, i);
+
+		logs[i].refname = xstrdup(buf);
+		logs[i].update_index = i + 1;
+		logs[i].value_type = REFTABLE_LOG_UPDATE;
+		logs[i].value.update.email = xstrdup("johndoe@invalid");
+		logs[i].value.update.message = xstrdup("commit\n");
+		set_test_hash(logs[i].value.update.new_hash, i);
+	}
+
+	for (i = 0; i < N; i++) {
+		err = reftable_stack_add(st, &write_test_ref, &refs[i]);
+		check(!err);
+	}
+
+	for (i = 0; i < N; i++) {
+		struct write_log_arg arg = {
+			.log = &logs[i],
+			.update_index = reftable_stack_next_update_index(st),
+		};
+		err = reftable_stack_add(st, &write_test_log, &arg);
+		check(!err);
+	}
+
+	reftable_stack_init_ref_iterator(st, &it);
+	reftable_iterator_seek_ref(&it, refs[0].refname);
+	for (i = 0; ; i++) {
+		struct reftable_ref_record ref = { 0 };
+		err = reftable_iterator_next_ref(&it, &ref);
+		if (err > 0)
+			break;
+		check(!err);
+		check(reftable_ref_record_equal(&ref, &refs[i], GIT_SHA1_RAWSZ));
+		reftable_ref_record_release(&ref);
+	}
+	check_int(i, ==, N);
+
+	reftable_iterator_destroy(&it);
+
+	reftable_stack_init_log_iterator(st, &it);
+	reftable_iterator_seek_log(&it, logs[0].refname);
+	for (i = 0; ; i++) {
+		struct reftable_log_record log = { 0 };
+		err = reftable_iterator_next_log(&it, &log);
+		if (err > 0)
+			break;
+		check(!err);
+		check(reftable_log_record_equal(&log, &logs[i], GIT_SHA1_RAWSZ));
+		reftable_log_record_release(&log);
+	}
+	check_int(i, ==, N);
+
+	reftable_stack_destroy(st);
+	reftable_iterator_destroy(&it);
+	for (i = 0; i < N; i++) {
+		reftable_ref_record_release(&refs[i]);
+		reftable_log_record_release(&logs[i]);
+	}
+	clear_dir(dir);
+}
+
 static void t_reftable_stack_log_normalize(void)
 {
 	int err;
@@ -1046,6 +1127,7 @@  int cmd_main(int argc, const char *argv[])
 	TEST(t_reftable_stack_compaction_concurrent(), "compaction with concurrent stack");
 	TEST(t_reftable_stack_compaction_concurrent_clean(), "compaction with unclean stack shutdown");
 	TEST(t_reftable_stack_hash_id(), "read stack with wrong hash ID");
+	TEST(t_reftable_stack_iterator(), "log and ref iterator for reftable stack");
 	TEST(t_reftable_stack_lock_failure(), "stack addition with lockfile failure");
 	TEST(t_reftable_stack_log_normalize(), "log messages should be normalized");
 	TEST(t_reftable_stack_tombstone(), "'tombstone' refs in stack");