aboutsummaryrefslogtreecommitdiffstats
path: root/t/unit-tests
diff options
context:
space:
mode:
authorChandra Pratap <chandrapratap3519@gmail.com>2024-08-21 18:00:51 +0530
committerJunio C Hamano <gitster@pobox.com>2024-08-21 09:41:40 -0700
commit546cc0d64e3df8a012d785c6b423d03e16bfc0c5 (patch)
treea1dacedce4df7994baa2eb55225e4707570e0800 /t/unit-tests
parent80ccd8a2602820fdf896a8e8894305225f86f61d (diff)
downloadgit-546cc0d64e3df8a012d785c6b423d03e16bfc0c5.tar.gz
t: move reftable/block_test.c to the unit testing framework
reftable/block_test.c exercises the functions defined in reftable/block.{c, h}. Migrate reftable/block_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework and renaming the tests to follow the unit-tests' naming conventions. Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 't/unit-tests')
-rw-r--r--t/unit-tests/t-reftable-block.c120
1 files changed, 120 insertions, 0 deletions
diff --git a/t/unit-tests/t-reftable-block.c b/t/unit-tests/t-reftable-block.c
new file mode 100644
index 0000000000..f2b9a8a6f4
--- /dev/null
+++ b/t/unit-tests/t-reftable-block.c
@@ -0,0 +1,120 @@
+/*
+Copyright 2020 Google LLC
+
+Use of this source code is governed by a BSD-style
+license that can be found in the LICENSE file or at
+https://developers.google.com/open-source/licenses/bsd
+*/
+
+#include "test-lib.h"
+#include "reftable/block.h"
+#include "reftable/blocksource.h"
+#include "reftable/constants.h"
+#include "reftable/reftable-error.h"
+
+static void t_block_read_write(void)
+{
+ const int header_off = 21; /* random */
+ char *names[30];
+ const int N = ARRAY_SIZE(names);
+ const int block_size = 1024;
+ struct reftable_block block = { NULL };
+ struct block_writer bw = {
+ .last_key = STRBUF_INIT,
+ };
+ struct reftable_record rec = {
+ .type = BLOCK_TYPE_REF,
+ };
+ int i = 0;
+ int n;
+ struct block_reader br = { 0 };
+ struct block_iter it = BLOCK_ITER_INIT;
+ int j = 0;
+ struct strbuf want = STRBUF_INIT;
+
+ REFTABLE_CALLOC_ARRAY(block.data, block_size);
+ block.len = block_size;
+ block.source = malloc_block_source();
+ block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size,
+ header_off, hash_size(GIT_SHA1_FORMAT_ID));
+
+ rec.u.ref.refname = (char *) "";
+ rec.u.ref.value_type = REFTABLE_REF_DELETION;
+ n = block_writer_add(&bw, &rec);
+ check_int(n, ==, REFTABLE_API_ERROR);
+
+ for (i = 0; i < N; i++) {
+ char name[100];
+ snprintf(name, sizeof(name), "branch%02d", i);
+
+ rec.u.ref.refname = name;
+ rec.u.ref.value_type = REFTABLE_REF_VAL1;
+ memset(rec.u.ref.value.val1, i, GIT_SHA1_RAWSZ);
+
+ names[i] = xstrdup(name);
+ n = block_writer_add(&bw, &rec);
+ rec.u.ref.refname = NULL;
+ rec.u.ref.value_type = REFTABLE_REF_DELETION;
+ check_int(n, ==, 0);
+ }
+
+ n = block_writer_finish(&bw);
+ check_int(n, >, 0);
+
+ block_writer_release(&bw);
+
+ block_reader_init(&br, &block, header_off, block_size, GIT_SHA1_RAWSZ);
+
+ block_iter_seek_start(&it, &br);
+
+ while (1) {
+ int r = block_iter_next(&it, &rec);
+ check_int(r, >=, 0);
+ if (r > 0) {
+ break;
+ }
+ check_str(names[j], rec.u.ref.refname);
+ j++;
+ }
+
+ reftable_record_release(&rec);
+ block_iter_close(&it);
+
+ for (i = 0; i < N; i++) {
+ struct block_iter it = BLOCK_ITER_INIT;
+ strbuf_reset(&want);
+ strbuf_addstr(&want, names[i]);
+
+ n = block_iter_seek_key(&it, &br, &want);
+ check_int(n, ==, 0);
+
+ n = block_iter_next(&it, &rec);
+ check_int(n, ==, 0);
+
+ check_str(names[i], rec.u.ref.refname);
+
+ want.len--;
+ n = block_iter_seek_key(&it, &br, &want);
+ check_int(n, ==, 0);
+
+ n = block_iter_next(&it, &rec);
+ check_int(n, ==, 0);
+ check_str(names[10 * (i / 10)], rec.u.ref.refname);
+
+ block_iter_close(&it);
+ }
+
+ reftable_record_release(&rec);
+ reftable_block_done(&br.block);
+ strbuf_release(&want);
+ for (i = 0; i < N; i++) {
+ reftable_free(names[i]);
+ }
+}
+
+int cmd_main(int argc, const char *argv[])
+{
+ TEST(t_block_read_write(), "read-write operations on blocks work");
+
+ return test_done();
+}