git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3 2/7] t: harmonize t-reftable-merged.c with coding guidelines

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 11, 2024, 20:38 UTC
Message-ID
<xmqq7cdrr7f2.fsf@gitster.g>
In-Reply-To
<20240711040854.4602-3-chandrapratap3519@gmail.com>
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.
Show 15 quoted lines
> @@ -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.

Show 10 quoted lines
> @@ -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.
Show 20 quoted lines
> @@ -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.

Show 12 quoted lines
> @@ -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.

Show 12 quoted lines
> @@ -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.
Previous: Chandra PratapNext: Chandra Pratap
Message 27 of 41 in “t: port reftable/merged_test.c to the unit testing framework”
  1. Chandra PratapJul 3, 2024
  2. 1/5 t: move reftable/merged_test.c to the unit testing frameworkChandra Pratap, Jul 3, 2024
  3. Karthik NayakJul 5, 2024
  4. Chandra PratapJul 6, 2024
  5. 2/5 t: harmonize t-reftable-merged.c with coding guidelinesChandra Pratap, Jul 3, 2024
  6. Karthik NayakJul 5, 2024
  7. 3/5 t-reftable-merged: add tests for reftable_merged_table_max_update_indexChandra Pratap, Jul 3, 2024
  8. 4/5 t-reftable-merged: use reftable_ref_record_equal to compare ref recordsChandra Pratap, Jul 3, 2024
  9. Karthik NayakJul 5, 2024
  10. 5/5 t-reftable-merged: add test for REFTABLE_FORMAT_ERRORChandra Pratap, Jul 3, 2024
  11. Karthik NayakJul 5, 2024
  12. [GSoC][PATCH v2 0/7] t: port reftable/merged_test.c to the unit testing frameworkChandra Pratap, Jul 9, 2024
  13. 1/7 t: move reftable/merged_test.c to the unit testing frameworkChandra Pratap, Jul 9, 2024
  14. Justin ToblerJul 9, 2024
  15. 2/7 t: harmonize t-reftable-merged.c with coding guidelinesChandra Pratap, Jul 9, 2024
  16. 3/7 t-reftable-merged: improve the test t_merged_single_record()Chandra Pratap, Jul 9, 2024
  17. 4/7 t-reftable-merged: improve the const-correctness of helper functionsChandra Pratap, Jul 9, 2024
  18. 5/7 t-reftable-merged: add tests for reftable_merged_table_max_update_indexChandra Pratap, Jul 9, 2024
  19. 6/7 t-reftable-merged: use reftable_ref_record_equal to compare ref recordsChandra Pratap, Jul 9, 2024
  20. 7/7 t-reftable-merged: add test for REFTABLE_FORMAT_ERRORChandra Pratap, Jul 9, 2024
  21. Karthik NayakJul 10, 2024
  22. Karthik NayakJul 10, 2024
  23. Junio C HamanoJul 11, 2024
  24. [GSoC][PATCH v3 0/7] t: port reftable/merged_test.c to the unit testing frameworkChandra Pratap, Jul 11, 2024
  25. 1/7 t: move reftable/merged_test.c to the unit testing frameworkChandra Pratap, Jul 11, 2024
  26. 2/7 t: harmonize t-reftable-merged.c with coding guidelinesChandra Pratap, Jul 11, 2024
  27. Junio C HamanoJul 11, 2024
  28. 3/7 t-reftable-merged: improve the test t_merged_single_record()Chandra Pratap, Jul 11, 2024
  29. 4/7 t-reftable-merged: improve the const-correctness of helper functionsChandra Pratap, Jul 11, 2024
  30. 5/7 t-reftable-merged: add tests for reftable_merged_table_max_update_indexChandra Pratap, Jul 11, 2024
  31. 6/7 t-reftable-merged: use reftable_ref_record_equal to compare ref recordsChandra Pratap, Jul 11, 2024
  32. 7/7 t-reftable-merged: add test for REFTABLE_FORMAT_ERRORChandra Pratap, Jul 11, 2024
  33. [GSoC][PATCH v4 0/7] t: port reftable/merged_test.c to the unit testing frameworkChandra Pratap, Jul 12, 2024
  34. 1/7 t: move reftable/merged_test.c to the unit testing frameworkChandra Pratap, Jul 12, 2024
  35. 2/7 t: harmonize t-reftable-merged.c with coding guidelinesChandra Pratap, Jul 12, 2024
  36. 3/7 t-reftable-merged: improve the test t_merged_single_record()Chandra Pratap, Jul 12, 2024
  37. 4/7 t-reftable-merged: improve the const-correctness of helper functionsChandra Pratap, Jul 12, 2024
  38. Patrick SteinhardtJul 24, 2024
  39. 5/7 t-reftable-merged: add tests for reftable_merged_table_max_update_indexChandra Pratap, Jul 12, 2024
  40. 6/7 t-reftable-merged: use reftable_ref_record_equal to compare ref recordsChandra Pratap, Jul 12, 2024
  41. 7/7 t-reftable-merged: add test for REFTABLE_FORMAT_ERRORChandra Pratap, Jul 12, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.