git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Chandra Pratap <chandrapratap3519@gmail.com>
Cc: git@vger.kernel.org,  karthik.188@gmail.com,  chriscool@tuxfamily.org
Subject: Re: [PATCH v3 2/7] t: harmonize t-reftable-merged.c with coding guidelines
Date: Thu, 11 Jul 2024 13:38:09 -0700	[thread overview]
Message-ID: <xmqq7cdrr7f2.fsf@gitster.g> (raw)
In-Reply-To: <20240711040854.4602-3-chandrapratap3519@gmail.com> (Chandra Pratap's message of "Thu, 11 Jul 2024 09:28:31 +0530")

Chandra Pratap <chandrapratap3519@gmail.com> writes:

It is very nice that the steps [1/7] and [2/7] are split this way.

> Harmonize the newly ported test unit-tests/t-reftable-merged.c
> with the following guidelines:
> - Single line control flow statements like 'for' and 'if'
>   must omit curly braces.

OK.

> - Structs must be 0-initialized with '= { 0 }' instead of '= { NULL }'.

Correct.

> - Array indices must be of type 'size_t', not 'int'.

OK, but "must be" is probably a bit too strong (see below).

> - It is fine to use C99 initial declaration in 'for' loop.

Yes, it is fine.  You do not have to, but you can.

> @@ -68,7 +66,6 @@ static void write_test_log_table(struct strbuf *buf,
>  				 struct reftable_log_record logs[], int n,
>  				 uint64_t update_index)
>  {
> -	int i = 0;
>  	int err;
>  
>  	struct reftable_write_options opts = {
> @@ -79,7 +76,7 @@ static void write_test_log_table(struct strbuf *buf,
>  	w = reftable_new_writer(&strbuf_add_void, &noop_flush, buf, &opts);
>  	reftable_writer_set_limits(w, update_index, update_index);
>  
> -	for (i = 0; i < n; i++) {
> +	for (int i = 0; i < n; i++) {
>  		int err = reftable_writer_add_log(w, &logs[i]);

Did you mean size_t instead of int here?  Probably not, because the
iteration goes up to "int n" that is supplied by the caller of this
test, so iterating with "int" is perfectly fine here.

So, "must be size_t" is already violated here.  You could update the
type of the incoming parameter "n", but given that this is a test
program that deals with a known logs[] array of a small bounded
size, that may be way overkill and "int" can be justified, too.
On the other hand, if it does not require too much investigation,
you may want to check the caller and if it can be updated to use
"size_t" instead of "int".

The general rule is probably "think twice before using 'int' as an
array index; otherwise use 'size_t'", which covers what I said in
the above paragraph.

> @@ -121,8 +118,7 @@ merged_table_from_records(struct reftable_ref_record **refs,
>  
>  static void readers_destroy(struct reftable_reader **readers, size_t n)
>  {
> -	int i = 0;
> -	for (; i < n; i++)
> +	for (size_t i = 0; i < n; i++)
>  		reftable_reader_free(readers[i]);
>  	reftable_free(readers);
>  }

Much better.

> @@ -148,9 +144,8 @@ static void t_merged_single_record(void)
>  	struct reftable_reader **readers = NULL;
>  	struct reftable_merged_table *mt =
>  		merged_table_from_records(refs, &bs, &readers, sizes, bufs, 2);
> -	int i;
> -	struct reftable_ref_record ref = { NULL };
> -	struct reftable_iterator it = { NULL };
> +	struct reftable_ref_record ref = { 0 };
> +	struct reftable_iterator it = { 0 };
>  	int err;
>  
>  	merged_table_init_iter(mt, &it, BLOCK_TYPE_REF);
> @@ -164,9 +159,8 @@ static void t_merged_single_record(void)
>  	reftable_iterator_destroy(&it);
>  	readers_destroy(readers, 2);
>  	reftable_merged_table_free(mt);
> -	for (i = 0; i < ARRAY_SIZE(bufs); i++) {
> +	for (size_t i = 0; i < ARRAY_SIZE(bufs); i++)
>  		strbuf_release(&bufs[i]);
> -	}

OK.  size_t is overkill here because bufs[] is a function local
array with only two elements in it, but once the patch to use
"size_t" (i.e., this one) is written, it is not worth to go in and
make it use "int" again.

> @@ -226,12 +220,12 @@ static void t_merged_refs(void)
>  	struct reftable_reader **readers = NULL;
>  	struct reftable_merged_table *mt =
>  		merged_table_from_records(refs, &bs, &readers, sizes, bufs, 3);
> -	struct reftable_iterator it = { NULL };
> +	struct reftable_iterator it = { 0 };
>  	int err;
>  	struct reftable_ref_record *out = NULL;
>  	size_t len = 0;
>  	size_t cap = 0;
> -	int i = 0;
> +	size_t i;

OK.  It is good that we got rid of useless initialization, as this
is used to drive more than one loops below.

> @@ -358,12 +349,12 @@ static void t_merged_logs(void)
>  	struct reftable_reader **readers = NULL;
>  	struct reftable_merged_table *mt = merged_table_from_log_records(
>  		logs, &bs, &readers, sizes, bufs, 3);
> -	struct reftable_iterator it = { NULL };
> +	struct reftable_iterator it = { 0 };
>  	int err;
>  	struct reftable_log_record *out = NULL;
>  	size_t len = 0;
>  	size_t cap = 0;
> -	int i = 0;
> +	size_t i = 0;

Lose the useless initialization here, too.

  reply	other threads:[~2024-07-11 20:38 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-03 17:01 [GSoC][PATCH 0/5] t: port reftable/merged_test.c to the unit testing framework Chandra Pratap
2024-07-03 17:01 ` [PATCH 1/5] t: move " Chandra Pratap
2024-07-05 17:40   ` Karthik Nayak
2024-07-06  7:13     ` Chandra Pratap
2024-07-03 17:01 ` [PATCH 2/5] t: harmonize t-reftable-merged.c with coding guidelines Chandra Pratap
2024-07-05 18:08   ` Karthik Nayak
2024-07-03 17:01 ` [PATCH 3/5] t-reftable-merged: add tests for reftable_merged_table_max_update_index Chandra Pratap
2024-07-03 17:01 ` [PATCH 4/5] t-reftable-merged: use reftable_ref_record_equal to compare ref records Chandra Pratap
2024-07-05 18:14   ` Karthik Nayak
2024-07-03 17:01 ` [PATCH 5/5] t-reftable-merged: add test for REFTABLE_FORMAT_ERROR Chandra Pratap
2024-07-05 18:24 ` [GSoC][PATCH 0/5] t: port reftable/merged_test.c to the unit testing framework Karthik Nayak
2024-07-09  5:28 ` [GSoC][PATCH v2 0/7] " Chandra Pratap
2024-07-09  5:28   ` [PATCH v2 1/7] t: move " Chandra Pratap
2024-07-09 23:05     ` Justin Tobler
2024-07-09  5:28   ` [PATCH v2 2/7] t: harmonize t-reftable-merged.c with coding guidelines Chandra Pratap
2024-07-09  5:28   ` [PATCH v2 3/7] t-reftable-merged: improve the test t_merged_single_record() Chandra Pratap
2024-07-09  5:28   ` [PATCH v2 4/7] t-reftable-merged: improve the const-correctness of helper functions Chandra Pratap
2024-07-09  5:28   ` [PATCH v2 5/7] t-reftable-merged: add tests for reftable_merged_table_max_update_index Chandra Pratap
2024-07-09  5:28   ` [PATCH v2 6/7] t-reftable-merged: use reftable_ref_record_equal to compare ref records Chandra Pratap
2024-07-09  5:28   ` [PATCH v2 7/7] t-reftable-merged: add test for REFTABLE_FORMAT_ERROR Chandra Pratap
2024-07-10  9:18     ` Karthik Nayak
2024-07-10  9:19   ` [GSoC][PATCH v2 0/7] t: port reftable/merged_test.c to the unit testing framework Karthik Nayak
2024-07-11 14:38     ` Junio C Hamano
2024-07-11  3:58   ` [GSoC][PATCH v3 " Chandra Pratap
2024-07-11  3:58     ` [PATCH v3 1/7] t: move " Chandra Pratap
2024-07-11  3:58     ` [PATCH v3 2/7] t: harmonize t-reftable-merged.c with coding guidelines Chandra Pratap
2024-07-11 20:38       ` Junio C Hamano [this message]
2024-07-11  3:58     ` [PATCH v3 3/7] t-reftable-merged: improve the test t_merged_single_record() Chandra Pratap
2024-07-11  3:58     ` [PATCH v3 4/7] t-reftable-merged: improve the const-correctness of helper functions Chandra Pratap
2024-07-11  3:58     ` [PATCH v3 5/7] t-reftable-merged: add tests for reftable_merged_table_max_update_index Chandra Pratap
2024-07-11  3:58     ` [PATCH v3 6/7] t-reftable-merged: use reftable_ref_record_equal to compare ref records Chandra Pratap
2024-07-11  3:58     ` [PATCH v3 7/7] t-reftable-merged: add test for REFTABLE_FORMAT_ERROR Chandra Pratap
2024-07-12  5:38     ` [GSoC][PATCH v4 0/7] t: port reftable/merged_test.c to the unit testing framework Chandra Pratap
2024-07-12  5:38       ` [PATCH v4 1/7] t: move " Chandra Pratap
2024-07-12  5:38       ` [PATCH v4 2/7] t: harmonize t-reftable-merged.c with coding guidelines Chandra Pratap
2024-07-12  5:38       ` [PATCH v4 3/7] t-reftable-merged: improve the test t_merged_single_record() Chandra Pratap
2024-07-12  5:39       ` [PATCH v4 4/7] t-reftable-merged: improve the const-correctness of helper functions Chandra Pratap
2024-07-24  9:12         ` Patrick Steinhardt
2024-07-12  5:39       ` [PATCH v4 5/7] t-reftable-merged: add tests for reftable_merged_table_max_update_index Chandra Pratap
2024-07-12  5:39       ` [PATCH v4 6/7] t-reftable-merged: use reftable_ref_record_equal to compare ref records Chandra Pratap
2024-07-12  5:39       ` [PATCH v4 7/7] t-reftable-merged: add test for REFTABLE_FORMAT_ERROR Chandra Pratap

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqq7cdrr7f2.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=chandrapratap3519@gmail.com \
    --cc=chriscool@tuxfamily.org \
    --cc=git@vger.kernel.org \
    --cc=karthik.188@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).