mbox series

[GSoC,v4,0/7] t: port reftable/merged_test.c to the unit testing framework

Message ID 20240712055041.6476-1-chandrapratap3519@gmail.com (mailing list archive)
Headers show
Series t: port reftable/merged_test.c to the unit testing framework | expand

Message

Chandra Pratap July 12, 2024, 5:38 a.m. UTC
In the recent codebase update (commit 8bf6fbd, 2023-12-09), a new unit
testing framework written entirely in C was introduced to the Git project
aimed at simplifying testing and reducing test run times.
Currently, tests for the reftable refs-backend are performed by a custom
testing framework defined by reftable/test_framework.{c, h}. Port
reftable/merged_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@pks.im>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>

---
Changes in v4:
- Make more variables 'size_t' and remove redundant initialization
  in patch 2 in-line with Junio's comments on v3.
- Use ARRAY_SIZE in patch 2 instead of hardcoding arrays' element counts.

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

Chandra Pratap (7):
[PATCH 1/7] t: move reftable/merged_test.c to the unit testing framework
[PATCH 2/7] t: harmonize t-reftable-merged.c with coding guidelines
[PATCH 3/7] t-reftable-merged: improve the test for t_merged_single_record()
[PATCH 4/7] t-reftable-merged: improve the const-correctness of helper functions
[PATCH 5/7] t-reftable-merged: add tests for reftable_merged_table_max_update_index
[PATCH 6/7] t-reftable-merged: use reftable_ref_record_equal to compare ref records
[PATCH 7/7] t-reftable-merged: add test for REFTABLE_FORMAT_ERROR

Makefile                                                   |   2 +-
t/helper/test-reftable.c                                   |   1 -
reftable/reftable-tests.h				   |   1 -
reftable/merged_test.c => t/unit-tests/t-reftable-merged.c | 208 +++++++++++++++----------------
4 files changed, 106 insertions(+), 106 deletions(-)

Range-diff against v3:
1:  08c993f5f6 ! 1:  963b9397b2 t: harmonize t-reftable-merged.c with coding guidelines
    @@ Commit message
         - Single line control flow statements like 'for' and 'if'
           must omit curly braces.
         - Structs must be 0-initialized with '= { 0 }' instead of '= { NULL }'.
    -    - Array indices must be of type 'size_t', not 'int'.
    +    - Array indices should preferably be of type 'size_t', not 'int'.
         - It is fine to use C99 initial declaration in 'for' loop.
     
    +    While at it, use 'ARRAY_SIZE(x)' to store the number of elements
    +    in an array instead of hardcoding 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-merged.c ##
    +@@ t/unit-tests/t-reftable-merged.c: static int noop_flush(void *arg)
    + }
    + 
    + static void write_test_table(struct strbuf *buf,
    +-			     struct reftable_ref_record refs[], int n)
    ++			     struct reftable_ref_record refs[], size_t n)
    + {
    + 	uint64_t min = 0xffffffff;
    + 	uint64_t max = 0;
    +-	int i = 0;
    ++	size_t i;
    + 	int err;
    + 
    + 	struct reftable_write_options opts = {
     @@ t/unit-tests/t-reftable-merged.c: static void write_test_table(struct strbuf *buf,
      	struct reftable_writer *w = NULL;
      	for (i = 0; i < n; i++) {
    @@ t/unit-tests/t-reftable-merged.c: static void write_test_table(struct strbuf *bu
      	}
      
      	w = reftable_new_writer(&strbuf_add_void, &noop_flush, buf, &opts);
    -@@ t/unit-tests/t-reftable-merged.c: static void write_test_log_table(struct strbuf *buf,
    - 				 struct reftable_log_record logs[], int n,
    +@@ t/unit-tests/t-reftable-merged.c: static void write_test_table(struct strbuf *buf,
    + }
    + 
    + static void write_test_log_table(struct strbuf *buf,
    +-				 struct reftable_log_record logs[], int n,
    ++				 struct reftable_log_record logs[], size_t n,
      				 uint64_t update_index)
      {
     -	int i = 0;
    @@ t/unit-tests/t-reftable-merged.c: static void write_test_log_table(struct strbuf
      	reftable_writer_set_limits(w, update_index, update_index);
      
     -	for (i = 0; i < n; i++) {
    -+	for (int i = 0; i < n; i++) {
    ++	for (size_t i = 0; i < n; i++) {
      		int err = reftable_writer_add_log(w, &logs[i]);
      		check(!err);
      	}
    +@@ t/unit-tests/t-reftable-merged.c: static void write_test_log_table(struct strbuf *buf,
    + static struct reftable_merged_table *
    + merged_table_from_records(struct reftable_ref_record **refs,
    + 			  struct reftable_block_source **source,
    +-			  struct reftable_reader ***readers, int *sizes,
    ++			  struct reftable_reader ***readers, size_t *sizes,
    + 			  struct strbuf *buf, size_t n)
    + {
    + 	struct reftable_merged_table *mt = NULL;
     @@ t/unit-tests/t-reftable-merged.c: merged_table_from_records(struct reftable_ref_record **refs,
      
      static void readers_destroy(struct reftable_reader **readers, size_t n)
    @@ t/unit-tests/t-reftable-merged.c: merged_table_from_records(struct reftable_ref_
      	reftable_free(readers);
      }
     @@ t/unit-tests/t-reftable-merged.c: static void t_merged_single_record(void)
    + 	} };
    + 
    + 	struct reftable_ref_record *refs[] = { r1, r2 };
    +-	int sizes[] = { 1, 1 };
    ++	size_t sizes[] = { ARRAY_SIZE(r1), ARRAY_SIZE(r2) };
    + 	struct strbuf bufs[2] = { STRBUF_INIT, STRBUF_INIT };
    + 	struct reftable_block_source *bs = NULL;
      	struct reftable_reader **readers = NULL;
      	struct reftable_merged_table *mt =
      		merged_table_from_records(refs, &bs, &readers, sizes, bufs, 2);
    @@ t/unit-tests/t-reftable-merged.c: static void t_merged_single_record(void)
      }
      
     @@ t/unit-tests/t-reftable-merged.c: static void t_merged_refs(void)
    + 	};
    + 
    + 	struct reftable_ref_record *refs[] = { r1, r2, r3 };
    +-	int sizes[3] = { 3, 1, 2 };
    ++	size_t sizes[3] = { ARRAY_SIZE(r1), ARRAY_SIZE(r2), ARRAY_SIZE(r3) };
    + 	struct strbuf bufs[3] = { STRBUF_INIT, STRBUF_INIT, STRBUF_INIT };
    + 	struct reftable_block_source *bs = NULL;
      	struct reftable_reader **readers = NULL;
      	struct reftable_merged_table *mt =
      		merged_table_from_records(refs, &bs, &readers, sizes, bufs, 3);
    @@ t/unit-tests/t-reftable-merged.c: static void t_merged_refs(void)
      	readers_destroy(readers, 3);
      	reftable_merged_table_free(mt);
      	reftable_free(bs);
    +@@ t/unit-tests/t-reftable-merged.c: static void t_merged_refs(void)
    + static struct reftable_merged_table *
    + merged_table_from_log_records(struct reftable_log_record **logs,
    + 			      struct reftable_block_source **source,
    +-			      struct reftable_reader ***readers, int *sizes,
    ++			      struct reftable_reader ***readers, size_t *sizes,
    + 			      struct strbuf *buf, size_t n)
    + {
    + 	struct reftable_merged_table *mt = NULL;
     @@ t/unit-tests/t-reftable-merged.c: static void t_merged_logs(void)
    + 	};
    + 
    + 	struct reftable_log_record *logs[] = { r1, r2, r3 };
    +-	int sizes[3] = { 2, 1, 1 };
    ++	size_t sizes[3] = { ARRAY_SIZE(r1), ARRAY_SIZE(r2), ARRAY_SIZE(r3) };
    + 	struct strbuf bufs[3] = { STRBUF_INIT, STRBUF_INIT, STRBUF_INIT };
    + 	struct reftable_block_source *bs = NULL;
      	struct reftable_reader **readers = NULL;
      	struct reftable_merged_table *mt = merged_table_from_log_records(
      		logs, &bs, &readers, sizes, bufs, 3);
    @@ t/unit-tests/t-reftable-merged.c: static void t_merged_logs(void)
      	size_t len = 0;
      	size_t cap = 0;
     -	int i = 0;
    -+	size_t i = 0;
    ++	size_t i;
      
      	merged_table_init_iter(mt, &it, BLOCK_TYPE_LOG);
      	err = reftable_iterator_seek_log(&it, "a");
2:  fa3085bd9b ! 2:  b7a0bd8165 t-reftable-merged: improve the test t_merged_single_record()
    @@ t/unit-tests/t-reftable-merged.c: static void t_merged_single_record(void)
     +	} };
      
     -	struct reftable_ref_record *refs[] = { r1, r2 };
    --	int sizes[] = { 1, 1 };
    +-	size_t sizes[] = { ARRAY_SIZE(r1), ARRAY_SIZE(r2) };
     -	struct strbuf bufs[2] = { STRBUF_INIT, STRBUF_INIT };
     +	struct reftable_ref_record *refs[] = { r1, r2, r3 };
    -+	int sizes[] = { 1, 1, 1 };
    ++	size_t sizes[] = { ARRAY_SIZE(r1), ARRAY_SIZE(r2), ARRAY_SIZE(r3) };
     +	struct strbuf bufs[3] = { STRBUF_INIT, STRBUF_INIT, STRBUF_INIT };
      	struct reftable_block_source *bs = NULL;
      	struct reftable_reader **readers = NULL;
3:  d491c1f383 ! 3:  e86d19a1c8 t-reftable-merged: improve the const-correctness of helper functions
    @@ t/unit-tests/t-reftable-merged.c: static int noop_flush(void *arg)
      }
      
      static void write_test_table(struct strbuf *buf,
    --			     struct reftable_ref_record refs[], int n)
    -+			     struct reftable_ref_record refs[], const int n)
    +-			     struct reftable_ref_record refs[], size_t n)
    ++			     struct reftable_ref_record refs[], const size_t n)
      {
      	uint64_t min = 0xffffffff;
      	uint64_t max = 0;
    @@ t/unit-tests/t-reftable-merged.c: static void write_test_table(struct strbuf *bu
      }
      
     -static void write_test_log_table(struct strbuf *buf,
    --				 struct reftable_log_record logs[], int n,
    +-				 struct reftable_log_record logs[], size_t n,
     -				 uint64_t update_index)
     +static void write_test_log_table(struct strbuf *buf, struct reftable_log_record logs[],
    -+				 const int n, const uint64_t update_index)
    ++				 const size_t n, const uint64_t update_index)
      {
      	int err;
      
    @@ t/unit-tests/t-reftable-merged.c: static void write_test_log_table(struct strbuf
      static struct reftable_merged_table *
      merged_table_from_records(struct reftable_ref_record **refs,
      			  struct reftable_block_source **source,
    --			  struct reftable_reader ***readers, int *sizes,
    +-			  struct reftable_reader ***readers, size_t *sizes,
     -			  struct strbuf *buf, size_t n)
    -+			  struct reftable_reader ***readers, const int *sizes,
    ++			  struct reftable_reader ***readers, const size_t *sizes,
     +			  struct strbuf *buf, const size_t n)
      {
      	struct reftable_merged_table *mt = NULL;
    @@ t/unit-tests/t-reftable-merged.c: static void t_merged_refs(void)
      static struct reftable_merged_table *
      merged_table_from_log_records(struct reftable_log_record **logs,
      			      struct reftable_block_source **source,
    --			      struct reftable_reader ***readers, int *sizes,
    +-			      struct reftable_reader ***readers, size_t *sizes,
     -			      struct strbuf *buf, size_t n)
    -+			      struct reftable_reader ***readers, const int *sizes,
    ++			      struct reftable_reader ***readers, const size_t *sizes,
     +			      struct strbuf *buf, const size_t n)
      {
      	struct reftable_merged_table *mt = NULL;
4:  ee9909f7ce = 4:  561ea9c5e9 t-reftable-merged: add tests for reftable_merged_table_max_update_index
5:  5ce16e9cfc = 5:  7f1a329e94 t-reftable-merged: use reftable_ref_record_equal to compare ref records
6:  d090e9ca5b = 6:  feaf46b765 t-reftable-merged: add test for REFTABLE_FORMAT_ERROR