t: harmonize t-reftable-stack.c with coding guidelines

Harmonize the newly ported test unit-tests/t-reftable-stack.c with the following guidelines: - Single line 'for' statements must omit curly braces. - Structs must be 0-initialized with '= { 0 }' instead of '= { NULL }'. - Array sizes and indices should preferably be of type 'size_t' and not 'int'. - Function pointers should be passed as 'func' and not '&func'. While at it, remove initialization for those variables that are re-used multiple times, like loop variables. 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>

Chandra Pratap committed Sep 8, 2024 at 09:35 UTC e4e384f68d2e9c2fd726bfc9d16f2359c0dda69c
1 file changed +53 -57
t/unit-tests/t-reftable-stack.c
+53 -57
@@ -81,7 +81,6 @@ static void t_read_file(void)
81 int n, err;
82 char **names = NULL;
83 const char *want[] = { "line1", "line2", "line3" };
84 - int i = 0;
84
85 check_int(fd, >, 0);
86 n = write_in_full(fd, out, strlen(out));
@@ -92,9 +91,8 @@ static void t_read_file(void)
91 err = read_lines(fn, &names);
92 check(!err);
93
95 - for (i = 0; names[i]; i++) {
94 + for (size_t i = 0; names[i]; i++)
95 check_str(want[i], names[i]);
97 - }
96 free_names(names);
97 (void) remove(fn);
98 }
@@ -123,7 +121,7 @@ static void write_n_ref_tables(struct reftable_stack *st,
121 };
122
123 strbuf_reset(&buf);
126 - strbuf_addf(&buf, "refs/heads/branch-%04u", (unsigned) i);
124 + strbuf_addf(&buf, "refs/heads/branch-%04"PRIuMAX, (uintmax_t)i);
125 ref.refname = buf.buf;
126 set_test_hash(ref.value.val1, i);
127
@@ -164,12 +162,12 @@ static void t_reftable_stack_add_one(void)
162 .value_type = REFTABLE_REF_SYMREF,
163 .value.symref = (char *) "master",
164 };
167 - struct reftable_ref_record dest = { NULL };
165 + struct reftable_ref_record dest = { 0 };
166 struct stat stat_result = { 0 };
167 err = reftable_new_stack(&st, dir, &opts);
168 check(!err);
169
172 - err = reftable_stack_add(st, &write_test_ref, &ref);
170 + err = reftable_stack_add(st, write_test_ref, &ref);
171 check(!err);
172
173 err = reftable_stack_read_ref(st, ref.refname, &dest);
@@ -234,16 +232,16 @@ static void t_reftable_stack_uptodate(void)
232 err = reftable_new_stack(&st2, dir, &opts);
233 check(!err);
234
237 - err = reftable_stack_add(st1, &write_test_ref, &ref1);
235 + err = reftable_stack_add(st1, write_test_ref, &ref1);
236 check(!err);
237
240 - err = reftable_stack_add(st2, &write_test_ref, &ref2);
238 + err = reftable_stack_add(st2, write_test_ref, &ref2);
239 check_int(err, ==, REFTABLE_OUTDATED_ERROR);
240
241 err = reftable_stack_reload(st2);
242 check(!err);
243
246 - err = reftable_stack_add(st2, &write_test_ref, &ref2);
244 + err = reftable_stack_add(st2, write_test_ref, &ref2);
245 check(!err);
246 reftable_stack_destroy(st1);
247 reftable_stack_destroy(st2);
@@ -264,7 +262,7 @@ static void t_reftable_stack_transaction_api(void)
262 .value_type = REFTABLE_REF_SYMREF,
263 .value.symref = (char *) "master",
264 };
267 - struct reftable_ref_record dest = { NULL };
265 + struct reftable_ref_record dest = { 0 };
266
267 err = reftable_new_stack(&st, dir, &opts);
268 check(!err);
@@ -274,7 +272,7 @@ static void t_reftable_stack_transaction_api(void)
272 err = reftable_stack_new_addition(&add, st);
273 check(!err);
274
277 - err = reftable_addition_add(add, &write_test_ref, &ref);
275 + err = reftable_addition_add(add, write_test_ref, &ref);
276 check(!err);
277
278 err = reftable_addition_commit(add);
@@ -298,12 +296,13 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void)
296 struct reftable_write_options opts = {0};
297 struct reftable_addition *add = NULL;
298 struct reftable_stack *st = NULL;
301 - int i, n = 20, err;
299 + size_t n = 20;
300 + int err;
301
302 err = reftable_new_stack(&st, dir, &opts);
303 check(!err);
304
306 - for (i = 0; i <= n; i++) {
305 + for (size_t i = 0; i <= n; i++) {
306 struct reftable_ref_record ref = {
307 .update_index = reftable_stack_next_update_index(st),
308 .value_type = REFTABLE_REF_SYMREF,
@@ -311,7 +310,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void)
310 };
311 char name[100];
312
314 - snprintf(name, sizeof(name), "branch%04d", i);
313 + snprintf(name, sizeof(name), "branch%04"PRIuMAX, (uintmax_t)i);
314 ref.refname = name;
315
316 /*
@@ -324,7 +323,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void)
323 err = reftable_stack_new_addition(&add, st);
324 check(!err);
325
327 - err = reftable_addition_add(add, &write_test_ref, &ref);
326 + err = reftable_addition_add(add, write_test_ref, &ref);
327 check(!err);
328
329 err = reftable_addition_commit(add);
@@ -355,7 +354,7 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void)
354 .value_type = REFTABLE_REF_VAL1,
355 .value.val1 = {0x01},
356 };
358 - struct reftable_write_options opts = {0};
357 + struct reftable_write_options opts = { 0 };
358 struct reftable_stack *st;
359 struct strbuf table_path = STRBUF_INIT;
360 char *dir = get_tmp_dir(__LINE__);
@@ -417,10 +416,10 @@ static void t_reftable_stack_update_index_check(void)
416 err = reftable_new_stack(&st, dir, &opts);
417 check(!err);
418
420 - err = reftable_stack_add(st, &write_test_ref, &ref1);
419 + err = reftable_stack_add(st, write_test_ref, &ref1);
420 check(!err);
421
423 - err = reftable_stack_add(st, &write_test_ref, &ref2);
422 + err = reftable_stack_add(st, write_test_ref, &ref2);
423 check_int(err, ==, REFTABLE_API_ERROR);
424 reftable_stack_destroy(st);
425 clear_dir(dir);
@@ -436,7 +435,7 @@ static void t_reftable_stack_lock_failure(void)
435 err = reftable_new_stack(&st, dir, &opts);
436 check(!err);
437 for (i = -1; i != REFTABLE_EMPTY_TABLE_ERROR; i--) {
439 - err = reftable_stack_add(st, &write_error, &i);
438 + err = reftable_stack_add(st, write_error, &i);
439 check_int(err, ==, i);
440 }
441
@@ -446,7 +445,6 @@ static void t_reftable_stack_lock_failure(void)
445
446 static void t_reftable_stack_add(void)
447 {
449 - int i = 0;
448 int err = 0;
449 struct reftable_write_options opts = {
450 .exact_log_message = 1,
@@ -455,18 +453,18 @@ static void t_reftable_stack_add(void)
453 };
454 struct reftable_stack *st = NULL;
455 char *dir = get_tmp_dir(__LINE__);
458 - struct reftable_ref_record refs[2] = { { NULL } };
459 - struct reftable_log_record logs[2] = { { NULL } };
456 + struct reftable_ref_record refs[2] = { 0 };
457 + struct reftable_log_record logs[2] = { 0 };
458 struct strbuf path = STRBUF_INIT;
459 struct stat stat_result;
462 - int N = ARRAY_SIZE(refs);
460 + size_t i, N = ARRAY_SIZE(refs);
461
462 err = reftable_new_stack(&st, dir, &opts);
463 check(!err);
464
465 for (i = 0; i < N; i++) {
466 char buf[256];
469 - snprintf(buf, sizeof(buf), "branch%02d", i);
467 + snprintf(buf, sizeof(buf), "branch%02"PRIuMAX, (uintmax_t)i);
468 refs[i].refname = xstrdup(buf);
469 refs[i].update_index = i + 1;
470 refs[i].value_type = REFTABLE_REF_VAL1;
@@ -480,7 +478,7 @@ static void t_reftable_stack_add(void)
478 }
479
480 for (i = 0; i < N; i++) {
483 - int err = reftable_stack_add(st, &write_test_ref, &refs[i]);
481 + int err = reftable_stack_add(st, write_test_ref, &refs[i]);
482 check(!err);
483 }
484
@@ -489,7 +487,7 @@ static void t_reftable_stack_add(void)
487 .log = &logs[i],
488 .update_index = reftable_stack_next_update_index(st),
489 };
492 - int err = reftable_stack_add(st, &write_test_log, &arg);
490 + int err = reftable_stack_add(st, write_test_log, &arg);
491 check(!err);
492 }
493
@@ -497,7 +495,7 @@ static void t_reftable_stack_add(void)
495 check(!err);
496
497 for (i = 0; i < N; i++) {
500 - struct reftable_ref_record dest = { NULL };
498 + struct reftable_ref_record dest = { 0 };
499
500 int err = reftable_stack_read_ref(st, refs[i].refname, &dest);
501 check(!err);
@@ -507,7 +505,7 @@ static void t_reftable_stack_add(void)
505 }
506
507 for (i = 0; i < N; i++) {
510 - struct reftable_log_record dest = { NULL };
508 + struct reftable_log_record dest = { 0 };
509 int err = reftable_stack_read_log(st, refs[i].refname, &dest);
510 check(!err);
511 check(reftable_log_record_equal(&dest, logs + i,
@@ -575,11 +573,11 @@ static void t_reftable_stack_log_normalize(void)
573 check(!err);
574
575 input.value.update.message = (char *) "one\ntwo";
578 - err = reftable_stack_add(st, &write_test_log, &arg);
576 + err = reftable_stack_add(st, write_test_log, &arg);
577 check_int(err, ==, REFTABLE_API_ERROR);
578
579 input.value.update.message = (char *) "one";
582 - err = reftable_stack_add(st, &write_test_log, &arg);
580 + err = reftable_stack_add(st, write_test_log, &arg);
581 check(!err);
582
583 err = reftable_stack_read_log(st, input.refname, &dest);
@@ -588,7 +586,7 @@ static void t_reftable_stack_log_normalize(void)
586
587 input.value.update.message = (char *) "two\n";
588 arg.update_index = 2;
591 - err = reftable_stack_add(st, &write_test_log, &arg);
589 + err = reftable_stack_add(st, write_test_log, &arg);
590 check(!err);
591 err = reftable_stack_read_log(st, input.refname, &dest);
592 check(!err);
@@ -602,16 +600,15 @@ static void t_reftable_stack_log_normalize(void)
600
601 static void t_reftable_stack_tombstone(void)
602 {
605 - int i = 0;
603 char *dir = get_tmp_dir(__LINE__);
604 struct reftable_write_options opts = { 0 };
605 struct reftable_stack *st = NULL;
606 int err;
610 - struct reftable_ref_record refs[2] = { { NULL } };
611 - struct reftable_log_record logs[2] = { { NULL } };
612 - int N = ARRAY_SIZE(refs);
613 - struct reftable_ref_record dest = { NULL };
614 - struct reftable_log_record log_dest = { NULL };
607 + struct reftable_ref_record refs[2] = { 0 };
608 + struct reftable_log_record logs[2] = { 0 };
609 + size_t i, N = ARRAY_SIZE(refs);
610 + struct reftable_ref_record dest = { 0 };
611 + struct reftable_log_record log_dest = { 0 };
612
613 err = reftable_new_stack(&st, dir, &opts);
614 check(!err);
@@ -637,7 +634,7 @@ static void t_reftable_stack_tombstone(void)
634 }
635 }
636 for (i = 0; i < N; i++) {
640 - int err = reftable_stack_add(st, &write_test_ref, &refs[i]);
637 + int err = reftable_stack_add(st, write_test_ref, &refs[i]);
638 check(!err);
639 }
640
@@ -646,7 +643,7 @@ static void t_reftable_stack_tombstone(void)
643 .log = &logs[i],
644 .update_index = reftable_stack_next_update_index(st),
645 };
649 - int err = reftable_stack_add(st, &write_test_log, &arg);
646 + int err = reftable_stack_add(st, write_test_log, &arg);
647 check(!err);
648 }
649
@@ -695,12 +692,12 @@ static void t_reftable_stack_hash_id(void)
692 struct reftable_stack *st32 = NULL;
693 struct reftable_write_options opts_default = { 0 };
694 struct reftable_stack *st_default = NULL;
698 - struct reftable_ref_record dest = { NULL };
695 + struct reftable_ref_record dest = { 0 };
696
697 err = reftable_new_stack(&st, dir, &opts);
698 check(!err);
699
703 - err = reftable_stack_add(st, &write_test_ref, &ref);
700 + err = reftable_stack_add(st, write_test_ref, &ref);
701 check(!err);
702
703 /* can't read it with the wrong hash ID. */
@@ -743,21 +740,20 @@ static void t_reflog_expire(void)
740 char *dir = get_tmp_dir(__LINE__);
741 struct reftable_write_options opts = { 0 };
742 struct reftable_stack *st = NULL;
746 - struct reftable_log_record logs[20] = { { NULL } };
747 - int N = ARRAY_SIZE(logs) - 1;
748 - int i = 0;
743 + struct reftable_log_record logs[20] = { 0 };
744 + size_t i, N = ARRAY_SIZE(logs) - 1;
745 int err;
746 struct reftable_log_expiry_config expiry = {
747 .time = 10,
748 };
753 - struct reftable_log_record log = { NULL };
749 + struct reftable_log_record log = { 0 };
750
751 err = reftable_new_stack(&st, dir, &opts);
752 check(!err);
753
754 for (i = 1; i <= N; i++) {
755 char buf[256];
760 - snprintf(buf, sizeof(buf), "branch%02d", i);
756 + snprintf(buf, sizeof(buf), "branch%02"PRIuMAX, (uintmax_t)i);
757
758 logs[i].refname = xstrdup(buf);
759 logs[i].update_index = i;
@@ -772,7 +768,7 @@ static void t_reflog_expire(void)
768 .log = &logs[i],
769 .update_index = reftable_stack_next_update_index(st),
770 };
775 - int err = reftable_stack_add(st, &write_test_log, &arg);
771 + int err = reftable_stack_add(st, write_test_log, &arg);
772 check(!err);
773 }
774
@@ -800,9 +796,8 @@ static void t_reflog_expire(void)
796
797 /* cleanup */
798 reftable_stack_destroy(st);
803 - for (i = 0; i <= N; i++) {
799 + for (i = 0; i <= N; i++)
800 reftable_log_record_release(&logs[i]);
805 - }
801 clear_dir(dir);
802 reftable_log_record_release(&log);
803 }
@@ -824,7 +819,7 @@ static void t_empty_add(void)
819 err = reftable_new_stack(&st, dir, &opts);
820 check(!err);
821
827 - err = reftable_stack_add(st, &write_nothing, NULL);
822 + err = reftable_stack_add(st, write_nothing, NULL);
823 check(!err);
824
825 err = reftable_new_stack(&st2, dir, &opts);
@@ -851,8 +846,8 @@ static void t_reftable_stack_auto_compaction(void)
846 };
847 struct reftable_stack *st = NULL;
848 char *dir = get_tmp_dir(__LINE__);
854 - int err, i;
855 - int N = 100;
849 + int err;
850 + size_t i, N = 100;
851
852 err = reftable_new_stack(&st, dir, &opts);
853 check(!err);
@@ -865,9 +860,9 @@ static void t_reftable_stack_auto_compaction(void)
860 .value_type = REFTABLE_REF_SYMREF,
861 .value.symref = (char *) "master",
862 };
868 - snprintf(name, sizeof(name), "branch%04d", i);
863 + snprintf(name, sizeof(name), "branch%04"PRIuMAX, (uintmax_t)i);
864
870 - err = reftable_stack_add(st, &write_test_ref, &ref);
865 + err = reftable_stack_add(st, write_test_ref, &ref);
866 check(!err);
867
868 err = reftable_stack_auto_compact(st);
@@ -929,7 +924,8 @@ static void t_reftable_stack_add_performs_auto_compaction(void)
924 struct reftable_stack *st = NULL;
925 struct strbuf refname = STRBUF_INIT;
926 char *dir = get_tmp_dir(__LINE__);
932 - int err, i, n = 20;
927 + int err;
928 + size_t i, n = 20;
929
930 err = reftable_new_stack(&st, dir, &opts);
931 check(!err);
@@ -949,10 +945,10 @@ static void t_reftable_stack_add_performs_auto_compaction(void)
945 st->opts.disable_auto_compact = i != n;
946
947 strbuf_reset(&refname);
952 - strbuf_addf(&refname, "branch-%04d", i);
948 + strbuf_addf(&refname, "branch-%04"PRIuMAX, (uintmax_t)i);
949 ref.refname = refname.buf;
950
955 - err = reftable_stack_add(st, &write_test_ref, &ref);
951 + err = reftable_stack_add(st, write_test_ref, &ref);
952 check(!err);
953
954 /*