[GSoC][PATCH v2 00/11] t: port reftable/block_test.c to the unit testing framework

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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/block_test.c to the unit testing framework and
improve upon the ported test. The first patch in the series 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 v2:
- Split the first patch into two, one that moves the test to the
  unit testing framework and another that adds improvements.
- Fix a comma-error in the commit message of  patch 4
  (previously patch 3).
- Use 'ret' as the name for a variable that stores return codes
  rather than 'n' in all the tests.
- Add a check for 'i == N' in the block iterator loop in all
  the tests.
- Modify the obj test (patch 10) to no longer use magic numbers.
- Rebase the branch on top of the latest 'master' branch.

CI/PR: https://github.com/gitgitgadget/git/pull/1749

Chandra Pratap(11):
t: move reftable/block_test.c to the unit testing framework
t: harmonize t-reftable-block.c with coding guidelines
t-reftable-block: release used block reader
t-reftable-block: use reftable_record_equal() instead of check_str()
t-reftable-block: use reftable_record_key() instead of strbuf_addstr()
t-reftable-block: use block_iter_reset() instead of block_iter_close()
t-reftable-block: use xstrfmt() instead of xstrdup()
t-reftable-block: remove unnecessary variable 'j'
t-reftable-block: add tests for log blocks
t-reftable-block: add tests for obj blocks
t-reftable-block: add tests for index blocks

Makefile                        |   2 +-
reftable/block_test.c           | 123 ------------------------------
reftable/reftable-tests.h       |   1 -
t/helper/test-reftable.c        |   1 -
t/unit-tests/t-reftable-block.c | 368 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 369 insertions(+), 126 deletions(-)

Range-diff against v2:
<rebase commits>
 1:  0712c3df22 !  1:  c0d8c4149f t: move reftable/block_test.c to the unit testing framework
    @@ Commit message
         framework and renaming the tests to follow the unit-tests'
         naming conventions.

    -    While at it, ensure structs are 0-initialized with '= { 0 }'
    -    instead of '= { NULL }' and array indices have type 'size_t'
    -    instead of 'int'.
    -
         Mentored-by: Patrick Steinhardt <ps@xxxxxx>
         Mentored-by: Christian Couder <chriscool@xxxxxxxxxxxxx>
         Signed-off-by: Chandra Pratap <chandrapratap3519@xxxxxxxxx>
    @@ t/unit-tests/t-reftable-block.c: license that can be found in the LICENSE file o
      {
      	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 };
    -+	const size_t N = ARRAY_SIZE(names);
    -+	const size_t block_size = 1024;
    -+	struct reftable_block block = { 0 };
    - 	struct block_writer bw = {
    - 		.last_key = STRBUF_INIT,
    - 	};
    - 	struct reftable_record rec = {
    - 		.type = BLOCK_TYPE_REF,
    - 	};
    --	int i = 0;
    -+	size_t i = 0;
    - 	int n;
    - 	struct block_reader br = { 0 };
    - 	struct block_iter it = BLOCK_ITER_INIT;
    --	int j = 0;
    -+	size_t j = 0;
    - 	struct strbuf want = STRBUF_INIT;
    -
    - 	REFTABLE_CALLOC_ARRAY(block.data, block_size);
     @@ t/unit-tests/t-reftable-block.c: static void test_block_read_write(void)
      	rec.u.ref.refname = (char *) "";
      	rec.u.ref.value_type = REFTABLE_REF_DELETION;
    @@ t/unit-tests/t-reftable-block.c: static void test_block_read_write(void)

      	for (i = 0; i < N; i++) {
      		char name[100];
    --		snprintf(name, sizeof(name), "branch%02d", i);
    -+		snprintf(name, sizeof(name), "branch%02"PRIuMAX , (uintmax_t)i);
    -
    - 		rec.u.ref.refname = name;
    - 		rec.u.ref.value_type = REFTABLE_REF_VAL1;
     @@ t/unit-tests/t-reftable-block.c: static void test_block_read_write(void)
      		n = block_writer_add(&bw, &rec);
      		rec.u.ref.refname = NULL;
    @@ t/unit-tests/t-reftable-block.c: static void test_block_read_write(void)
      	while (1) {
      		int r = block_iter_next(&it, &rec);
     -		EXPECT(r >= 0);
    --		if (r > 0) {
     +		check_int(r, >=, 0);
    -+		if (r > 0)
    + 		if (r > 0) {
      			break;
    --		}
    + 		}
     -		EXPECT_STREQ(names[j], rec.u.ref.refname);
     +		check_str(names[j], rec.u.ref.refname);
      		j++;
    @@ t/unit-tests/t-reftable-block.c: static void test_block_read_write(void)
      		block_iter_close(&it);
      	}
     @@ t/unit-tests/t-reftable-block.c: static void test_block_read_write(void)
    - 	reftable_record_release(&rec);
    - 	reftable_block_done(&br.block);
    - 	strbuf_release(&want);
    --	for (i = 0; i < N; i++) {
    -+	for (i = 0; i < N; i++)
    - 		reftable_free(names[i]);
    --	}
    + 	}
      }

     -int block_test_main(int argc, const char *argv[])
 -:  ---------- >  2:  017658ddb3 t: harmonize t-reftable-block.c with coding guidelines
 2:  3282dbeba1 =  3:  2042e2fde0 t-reftable-block: release used block reader
 3:  5a3fea003a !  4:  853efb0c0b t-reftable-block: use reftable_record_equal() instead of check_str()
    @@ Commit message
         comparing only the keys of input and output reftable records. This is
         not ideal because there can exist inequal reftable records with the
         same key. Use the dedicated function for record comparison,
    -    reftable_record_equal() instead of key-based comparison.
    +    reftable_record_equal(), instead of key-based comparison.

         Mentored-by: Patrick Steinhardt <ps@xxxxxx>
         Mentored-by: Christian Couder <chriscool@xxxxxxxxxxxxx>
    @@ t/unit-tests/t-reftable-block.c: license that can be found in the LICENSE file o
      	struct block_writer bw = {
     @@ t/unit-tests/t-reftable-block.c: static void t_block_read_write(void)
      		char name[100];
    - 		snprintf(name, sizeof(name), "branch%02"PRIuMAX , (uintmax_t)i);
    + 		snprintf(name, sizeof(name), "branch%02"PRIuMAX, (uintmax_t)i);

     -		rec.u.ref.refname = name;
     +		rec.u.ref.refname = xstrdup(name);
    @@ t/unit-tests/t-reftable-block.c: static void t_block_read_write(void)

     -		names[i] = xstrdup(name);
     +		recs[i] = rec;
    - 		n = block_writer_add(&bw, &rec);
    + 		ret = block_writer_add(&bw, &rec);
      		rec.u.ref.refname = NULL;
      		rec.u.ref.value_type = REFTABLE_REF_DELETION;
     @@ t/unit-tests/t-reftable-block.c: static void t_block_read_write(void)
    - 		check_int(r, >=, 0);
    - 		if (r > 0)
    + 			check_int(i, ==, N);
      			break;
    + 		}
     -		check_str(names[j], rec.u.ref.refname);
     +		check(reftable_record_equal(&recs[j], &rec, GIT_SHA1_RAWSZ));
      		j++;
    @@ t/unit-tests/t-reftable-block.c: static void t_block_read_write(void)
     -		strbuf_addstr(&want, names[i]);
     +		strbuf_addstr(&want, recs[i].u.ref.refname);

    - 		n = block_iter_seek_key(&it, &br, &want);
    - 		check_int(n, ==, 0);
    + 		ret = block_iter_seek_key(&it, &br, &want);
    + 		check_int(ret, ==, 0);
     @@ t/unit-tests/t-reftable-block.c: static void t_block_read_write(void)
    - 		n = block_iter_next(&it, &rec);
    - 		check_int(n, ==, 0);
    + 		ret = block_iter_next(&it, &rec);
    + 		check_int(ret, ==, 0);

     -		check_str(names[i], rec.u.ref.refname);
     +		check(reftable_record_equal(&recs[i], &rec, GIT_SHA1_RAWSZ));

      		want.len--;
    - 		n = block_iter_seek_key(&it, &br, &want);
    + 		ret = block_iter_seek_key(&it, &br, &want);
     @@ t/unit-tests/t-reftable-block.c: static void t_block_read_write(void)

    - 		n = block_iter_next(&it, &rec);
    - 		check_int(n, ==, 0);
    + 		ret = block_iter_next(&it, &rec);
    + 		check_int(ret, ==, 0);
     -		check_str(names[10 * (i / 10)], rec.u.ref.refname);
     +		check(reftable_record_equal(&recs[10 * (i / 10)], &rec, GIT_SHA1_RAWSZ));

 4:  7ca77be75c !  5:  0de1e0cbe2 t-reftable-block: use reftable_record_key() instead of strbuf_addstr()
    @@ t/unit-tests/t-reftable-block.c: static void t_block_read_write(void)
     -		strbuf_addstr(&want, recs[i].u.ref.refname);
     +		reftable_record_key(&recs[i], &want);

    - 		n = block_iter_seek_key(&it, &br, &want);
    - 		check_int(n, ==, 0);
    + 		ret = block_iter_seek_key(&it, &br, &want);
    + 		check_int(ret, ==, 0);
 5:  0016d8828a !  6:  74187586c9 t-reftable-block: use block_iter_reset() instead of block_iter_close()
    @@ t/unit-tests/t-reftable-block.c: static void t_block_read_write(void)
     +		block_iter_reset(&it);
      		reftable_record_key(&recs[i], &want);

    - 		n = block_iter_seek_key(&it, &br, &want);
    + 		ret = block_iter_seek_key(&it, &br, &want);
     @@ t/unit-tests/t-reftable-block.c: static void t_block_read_write(void)
    - 		n = block_iter_next(&it, &rec);
    - 		check_int(n, ==, 0);
    + 		ret = block_iter_next(&it, &rec);
    + 		check_int(ret, ==, 0);
      		check(reftable_record_equal(&recs[10 * (i / 10)], &rec, GIT_SHA1_RAWSZ));
     -
     -		block_iter_close(&it);
 6:  49fe881f0e <  -:  ---------- t-reftable-block: use xstrfmt() instead of xstrdup()
 -:  ---------- >  7:  5e35cae987 t-reftable-block: use xstrfmt() instead of xstrdup()
 7:  5dbc756799 !  8:  bacd52a236 t-reftable-block: remove unnecessary variable 'j'
    @@ Commit message

      ## t/unit-tests/t-reftable-block.c ##
     @@ t/unit-tests/t-reftable-block.c: static void t_block_read_write(void)
    - 	int n;
    + 	int ret;
      	struct block_reader br = { 0 };
      	struct block_iter it = BLOCK_ITER_INIT;
     -	size_t j = 0;
    @@ t/unit-tests/t-reftable-block.c: static void t_block_read_write(void)

     -	while (1) {
     +	for (i = 0; ; i++) {
    - 		int r = block_iter_next(&it, &rec);
    - 		check_int(r, >=, 0);
    - 		if (r > 0)
    + 		ret = block_iter_next(&it, &rec);
    + 		check_int(ret, >=, 0);
    + 		if (ret > 0) {
    + 			check_int(i, ==, N);
      			break;
    + 		}
     -		check(reftable_record_equal(&recs[j], &rec, GIT_SHA1_RAWSZ));
     -		j++;
     +		check(reftable_record_equal(&recs[i], &rec, GIT_SHA1_RAWSZ));
 8:  0916b8b2ca !  9:  6e8632d3e6 t-reftable-block: add tests for log blocks
    @@ t/unit-tests/t-reftable-block.c: static void t_block_read_write(void)
     +		.type = BLOCK_TYPE_LOG,
     +	};
     +	size_t i = 0;
    -+	int n;
    ++	int ret;
     +	struct block_reader br = { 0 };
     +	struct block_iter it = BLOCK_ITER_INIT;
     +	struct strbuf want = STRBUF_INIT;
    @@ t/unit-tests/t-reftable-block.c: static void t_block_read_write(void)
     +		rec.u.log.value_type = REFTABLE_LOG_UPDATE;
     +
     +		recs[i] = rec;
    -+		n = block_writer_add(&bw, &rec);
    ++		ret = block_writer_add(&bw, &rec);
     +		rec.u.log.refname = NULL;
     +		rec.u.log.value_type = REFTABLE_LOG_DELETION;
    -+		check_int(n, ==, 0);
    ++		check_int(ret, ==, 0);
     +	}
     +
    -+	n = block_writer_finish(&bw);
    -+	check_int(n, >, 0);
    ++	ret = block_writer_finish(&bw);
    ++	check_int(ret, >, 0);
     +
     +	block_writer_release(&bw);
     +
    @@ t/unit-tests/t-reftable-block.c: static void t_block_read_write(void)
     +	block_iter_seek_start(&it, &br);
     +
     +	for (i = 0; ; i++) {
    -+		int r = block_iter_next(&it, &rec);
    -+		check_int(r, >=, 0);
    -+		if (r > 0)
    ++		ret = block_iter_next(&it, &rec);
    ++		check_int(ret, >=, 0);
    ++		if (ret > 0) {
    ++			check_int(i, ==, N);
     +			break;
    ++		}
     +		check(reftable_record_equal(&recs[i], &rec, GIT_SHA1_RAWSZ));
     +	}
     +
    @@ t/unit-tests/t-reftable-block.c: static void t_block_read_write(void)
     +		strbuf_reset(&want);
     +		strbuf_addstr(&want, recs[i].u.log.refname);
     +
    -+		n = block_iter_seek_key(&it, &br, &want);
    -+		check_int(n, ==, 0);
    ++		ret = block_iter_seek_key(&it, &br, &want);
    ++		check_int(ret, ==, 0);
     +
    -+		n = block_iter_next(&it, &rec);
    -+		check_int(n, ==, 0);
    ++		ret = block_iter_next(&it, &rec);
    ++		check_int(ret, ==, 0);
     +
     +		check(reftable_record_equal(&recs[i], &rec, GIT_SHA1_RAWSZ));
     +
     +		want.len--;
    -+		n = block_iter_seek_key(&it, &br, &want);
    -+		check_int(n, ==, 0);
    ++		ret = block_iter_seek_key(&it, &br, &want);
    ++		check_int(ret, ==, 0);
     +
    -+		n = block_iter_next(&it, &rec);
    -+		check_int(n, ==, 0);
    ++		ret = block_iter_next(&it, &rec);
    ++		check_int(ret, ==, 0);
     +		check(reftable_record_equal(&recs[10 * (i / 10)], &rec, GIT_SHA1_RAWSZ));
     +	}
     +
 9:  4794f2d610 ! 10:  d147d5e9a9 t-reftable-block: add tests for obj blocks
    @@ t/unit-tests/t-reftable-block.c: static void t_log_block_read_write(void)
     +		.type = BLOCK_TYPE_OBJ,
     +	};
     +	size_t i = 0;
    -+	int n;
    ++	int ret;
     +	struct block_reader br = { 0 };
     +	struct block_iter it = BLOCK_ITER_INIT;
     +	struct strbuf want = STRBUF_INIT;
    @@ t/unit-tests/t-reftable-block.c: static void t_log_block_read_write(void)
     +			  header_off, hash_size(GIT_SHA1_FORMAT_ID));
     +
     +	for (i = 0; i < N; i++) {
    -+		uint8_t *bytes = reftable_malloc(sizeof(uint8_t[5]));
    -+		memcpy(bytes, (uint8_t[]){i, i+1, i+2, i+3, i+5}, sizeof(uint8_t[5]));
    ++		uint8_t bytes[] = { i, i + 1, i + 2, i + 3, i + 5 }, *allocated;
    ++		allocated = reftable_malloc(ARRAY_SIZE(bytes));
    ++		DUP_ARRAY(allocated, bytes, ARRAY_SIZE(bytes));
     +
    -+		rec.u.obj.hash_prefix = bytes;
    ++		rec.u.obj.hash_prefix = allocated;
     +		rec.u.obj.hash_prefix_len = 5;
     +
     +		recs[i] = rec;
    -+		n = block_writer_add(&bw, &rec);
    ++		ret = block_writer_add(&bw, &rec);
     +		rec.u.obj.hash_prefix = NULL;
     +		rec.u.obj.hash_prefix_len = 0;
    -+		check_int(n, ==, 0);
    ++		check_int(ret, ==, 0);
     +	}
     +
    -+	n = block_writer_finish(&bw);
    -+	check_int(n, >, 0);
    ++	ret = block_writer_finish(&bw);
    ++	check_int(ret, >, 0);
     +
     +	block_writer_release(&bw);
     +
    @@ t/unit-tests/t-reftable-block.c: static void t_log_block_read_write(void)
     +	block_iter_seek_start(&it, &br);
     +
     +	for (i = 0; ; i++) {
    -+		int r = block_iter_next(&it, &rec);
    -+		check_int(r, >=, 0);
    -+		if (r > 0)
    ++		ret = block_iter_next(&it, &rec);
    ++		check_int(ret, >=, 0);
    ++		if (ret > 0) {
    ++			check_int(i, ==, N);
     +			break;
    ++		}
     +		check(reftable_record_equal(&recs[i], &rec, GIT_SHA1_RAWSZ));
     +	}
     +
    @@ t/unit-tests/t-reftable-block.c: static void t_log_block_read_write(void)
     +		block_iter_reset(&it);
     +		reftable_record_key(&recs[i], &want);
     +
    -+		n = block_iter_seek_key(&it, &br, &want);
    -+		check_int(n, ==, 0);
    ++		ret = block_iter_seek_key(&it, &br, &want);
    ++		check_int(ret, ==, 0);
     +
    -+		n = block_iter_next(&it, &rec);
    -+		check_int(n, ==, 0);
    ++		ret = block_iter_next(&it, &rec);
    ++		check_int(ret, ==, 0);
     +
     +		check(reftable_record_equal(&recs[i], &rec, GIT_SHA1_RAWSZ));
     +	}
10:  40fcc98173 ! 11:  2b43b1ac9a t-reftable-block: add tests for index blocks
    @@ t/unit-tests/t-reftable-block.c: static void t_obj_block_read_write(void)
     +		.u.idx.last_key = STRBUF_INIT,
     +	};
     +	size_t i = 0;
    -+	int n;
    ++	int ret;
     +	struct block_reader br = { 0 };
     +	struct block_iter it = BLOCK_ITER_INIT;
     +	struct strbuf want = STRBUF_INIT;
    @@ t/unit-tests/t-reftable-block.c: static void t_obj_block_read_write(void)
     +		strbuf_addf(&recs[i].u.idx.last_key, "branch%02"PRIuMAX, (uintmax_t)i);
     +		recs[i].u.idx.offset = i;
     +
    -+		n = block_writer_add(&bw, &recs[i]);
    -+		check_int(n, ==, 0);
    ++		ret = block_writer_add(&bw, &recs[i]);
    ++		check_int(ret, ==, 0);
     +	}
     +
    -+	n = block_writer_finish(&bw);
    -+	check_int(n, >, 0);
    ++	ret = block_writer_finish(&bw);
    ++	check_int(ret, >, 0);
     +
     +	block_writer_release(&bw);
     +
    @@ t/unit-tests/t-reftable-block.c: static void t_obj_block_read_write(void)
     +	block_iter_seek_start(&it, &br);
     +
     +	for (i = 0; ; i++) {
    -+		int r = block_iter_next(&it, &rec);
    -+		check_int(r, >=, 0);
    -+		if (r > 0)
    ++		ret = block_iter_next(&it, &rec);
    ++		check_int(ret, >=, 0);
    ++		if (ret > 0) {
    ++			check_int(i, ==, N);
     +			break;
    ++		}
     +		check(reftable_record_equal(&recs[i], &rec, GIT_SHA1_RAWSZ));
     +	}
     +
    @@ t/unit-tests/t-reftable-block.c: static void t_obj_block_read_write(void)
     +		block_iter_reset(&it);
     +		reftable_record_key(&recs[i], &want);
     +
    -+		n = block_iter_seek_key(&it, &br, &want);
    -+		check_int(n, ==, 0);
    ++		ret = block_iter_seek_key(&it, &br, &want);
    ++		check_int(ret, ==, 0);
     +
    -+		n = block_iter_next(&it, &rec);
    -+		check_int(n, ==, 0);
    ++		ret = block_iter_next(&it, &rec);
    ++		check_int(ret, ==, 0);
     +
     +		check(reftable_record_equal(&recs[i], &rec, GIT_SHA1_RAWSZ));
     +
     +		want.len--;
    -+		n = block_iter_seek_key(&it, &br, &want);
    -+		check_int(n, ==, 0);
    ++		ret = block_iter_seek_key(&it, &br, &want);
    ++		check_int(ret, ==, 0);
     +
    -+		n = block_iter_next(&it, &rec);
    -+		check_int(n, ==, 0);
    ++		ret = block_iter_next(&it, &rec);
    ++		check_int(ret, ==, 0);
     +		check(reftable_record_equal(&recs[10 * (i / 10)], &rec, GIT_SHA1_RAWSZ));
     +	}
     +




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux