Skip to content

Commit e4e384f

Browse files
Chandra Pratapgitster
authored andcommitted
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 <[email protected]> Mentored-by: Christian Couder <[email protected]> Signed-off-by: Chandra Pratap <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 15e29ea commit e4e384f

File tree

1 file changed

+53
-57
lines changed

1 file changed

+53
-57
lines changed

t/unit-tests/t-reftable-stack.c

Lines changed: 53 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ static void t_read_file(void)
8181
int n, err;
8282
char **names = NULL;
8383
const char *want[] = { "line1", "line2", "line3" };
84-
int i = 0;
8584

8685
check_int(fd, >, 0);
8786
n = write_in_full(fd, out, strlen(out));
@@ -92,9 +91,8 @@ static void t_read_file(void)
9291
err = read_lines(fn, &names);
9392
check(!err);
9493

95-
for (i = 0; names[i]; i++) {
94+
for (size_t i = 0; names[i]; i++)
9695
check_str(want[i], names[i]);
97-
}
9896
free_names(names);
9997
(void) remove(fn);
10098
}
@@ -123,7 +121,7 @@ static void write_n_ref_tables(struct reftable_stack *st,
123121
};
124122

125123
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);
127125
ref.refname = buf.buf;
128126
set_test_hash(ref.value.val1, i);
129127

@@ -164,12 +162,12 @@ static void t_reftable_stack_add_one(void)
164162
.value_type = REFTABLE_REF_SYMREF,
165163
.value.symref = (char *) "master",
166164
};
167-
struct reftable_ref_record dest = { NULL };
165+
struct reftable_ref_record dest = { 0 };
168166
struct stat stat_result = { 0 };
169167
err = reftable_new_stack(&st, dir, &opts);
170168
check(!err);
171169

172-
err = reftable_stack_add(st, &write_test_ref, &ref);
170+
err = reftable_stack_add(st, write_test_ref, &ref);
173171
check(!err);
174172

175173
err = reftable_stack_read_ref(st, ref.refname, &dest);
@@ -234,16 +232,16 @@ static void t_reftable_stack_uptodate(void)
234232
err = reftable_new_stack(&st2, dir, &opts);
235233
check(!err);
236234

237-
err = reftable_stack_add(st1, &write_test_ref, &ref1);
235+
err = reftable_stack_add(st1, write_test_ref, &ref1);
238236
check(!err);
239237

240-
err = reftable_stack_add(st2, &write_test_ref, &ref2);
238+
err = reftable_stack_add(st2, write_test_ref, &ref2);
241239
check_int(err, ==, REFTABLE_OUTDATED_ERROR);
242240

243241
err = reftable_stack_reload(st2);
244242
check(!err);
245243

246-
err = reftable_stack_add(st2, &write_test_ref, &ref2);
244+
err = reftable_stack_add(st2, write_test_ref, &ref2);
247245
check(!err);
248246
reftable_stack_destroy(st1);
249247
reftable_stack_destroy(st2);
@@ -264,7 +262,7 @@ static void t_reftable_stack_transaction_api(void)
264262
.value_type = REFTABLE_REF_SYMREF,
265263
.value.symref = (char *) "master",
266264
};
267-
struct reftable_ref_record dest = { NULL };
265+
struct reftable_ref_record dest = { 0 };
268266

269267
err = reftable_new_stack(&st, dir, &opts);
270268
check(!err);
@@ -274,7 +272,7 @@ static void t_reftable_stack_transaction_api(void)
274272
err = reftable_stack_new_addition(&add, st);
275273
check(!err);
276274

277-
err = reftable_addition_add(add, &write_test_ref, &ref);
275+
err = reftable_addition_add(add, write_test_ref, &ref);
278276
check(!err);
279277

280278
err = reftable_addition_commit(add);
@@ -298,20 +296,21 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void)
298296
struct reftable_write_options opts = {0};
299297
struct reftable_addition *add = NULL;
300298
struct reftable_stack *st = NULL;
301-
int i, n = 20, err;
299+
size_t n = 20;
300+
int err;
302301

303302
err = reftable_new_stack(&st, dir, &opts);
304303
check(!err);
305304

306-
for (i = 0; i <= n; i++) {
305+
for (size_t i = 0; i <= n; i++) {
307306
struct reftable_ref_record ref = {
308307
.update_index = reftable_stack_next_update_index(st),
309308
.value_type = REFTABLE_REF_SYMREF,
310309
.value.symref = (char *) "master",
311310
};
312311
char name[100];
313312

314-
snprintf(name, sizeof(name), "branch%04d", i);
313+
snprintf(name, sizeof(name), "branch%04"PRIuMAX, (uintmax_t)i);
315314
ref.refname = name;
316315

317316
/*
@@ -324,7 +323,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void)
324323
err = reftable_stack_new_addition(&add, st);
325324
check(!err);
326325

327-
err = reftable_addition_add(add, &write_test_ref, &ref);
326+
err = reftable_addition_add(add, write_test_ref, &ref);
328327
check(!err);
329328

330329
err = reftable_addition_commit(add);
@@ -355,7 +354,7 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void)
355354
.value_type = REFTABLE_REF_VAL1,
356355
.value.val1 = {0x01},
357356
};
358-
struct reftable_write_options opts = {0};
357+
struct reftable_write_options opts = { 0 };
359358
struct reftable_stack *st;
360359
struct strbuf table_path = STRBUF_INIT;
361360
char *dir = get_tmp_dir(__LINE__);
@@ -417,10 +416,10 @@ static void t_reftable_stack_update_index_check(void)
417416
err = reftable_new_stack(&st, dir, &opts);
418417
check(!err);
419418

420-
err = reftable_stack_add(st, &write_test_ref, &ref1);
419+
err = reftable_stack_add(st, write_test_ref, &ref1);
421420
check(!err);
422421

423-
err = reftable_stack_add(st, &write_test_ref, &ref2);
422+
err = reftable_stack_add(st, write_test_ref, &ref2);
424423
check_int(err, ==, REFTABLE_API_ERROR);
425424
reftable_stack_destroy(st);
426425
clear_dir(dir);
@@ -436,7 +435,7 @@ static void t_reftable_stack_lock_failure(void)
436435
err = reftable_new_stack(&st, dir, &opts);
437436
check(!err);
438437
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);
440439
check_int(err, ==, i);
441440
}
442441

@@ -446,7 +445,6 @@ static void t_reftable_stack_lock_failure(void)
446445

447446
static void t_reftable_stack_add(void)
448447
{
449-
int i = 0;
450448
int err = 0;
451449
struct reftable_write_options opts = {
452450
.exact_log_message = 1,
@@ -455,18 +453,18 @@ static void t_reftable_stack_add(void)
455453
};
456454
struct reftable_stack *st = NULL;
457455
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 };
460458
struct strbuf path = STRBUF_INIT;
461459
struct stat stat_result;
462-
int N = ARRAY_SIZE(refs);
460+
size_t i, N = ARRAY_SIZE(refs);
463461

464462
err = reftable_new_stack(&st, dir, &opts);
465463
check(!err);
466464

467465
for (i = 0; i < N; i++) {
468466
char buf[256];
469-
snprintf(buf, sizeof(buf), "branch%02d", i);
467+
snprintf(buf, sizeof(buf), "branch%02"PRIuMAX, (uintmax_t)i);
470468
refs[i].refname = xstrdup(buf);
471469
refs[i].update_index = i + 1;
472470
refs[i].value_type = REFTABLE_REF_VAL1;
@@ -480,7 +478,7 @@ static void t_reftable_stack_add(void)
480478
}
481479

482480
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]);
484482
check(!err);
485483
}
486484

@@ -489,15 +487,15 @@ static void t_reftable_stack_add(void)
489487
.log = &logs[i],
490488
.update_index = reftable_stack_next_update_index(st),
491489
};
492-
int err = reftable_stack_add(st, &write_test_log, &arg);
490+
int err = reftable_stack_add(st, write_test_log, &arg);
493491
check(!err);
494492
}
495493

496494
err = reftable_stack_compact_all(st, NULL);
497495
check(!err);
498496

499497
for (i = 0; i < N; i++) {
500-
struct reftable_ref_record dest = { NULL };
498+
struct reftable_ref_record dest = { 0 };
501499

502500
int err = reftable_stack_read_ref(st, refs[i].refname, &dest);
503501
check(!err);
@@ -507,7 +505,7 @@ static void t_reftable_stack_add(void)
507505
}
508506

509507
for (i = 0; i < N; i++) {
510-
struct reftable_log_record dest = { NULL };
508+
struct reftable_log_record dest = { 0 };
511509
int err = reftable_stack_read_log(st, refs[i].refname, &dest);
512510
check(!err);
513511
check(reftable_log_record_equal(&dest, logs + i,
@@ -575,11 +573,11 @@ static void t_reftable_stack_log_normalize(void)
575573
check(!err);
576574

577575
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);
579577
check_int(err, ==, REFTABLE_API_ERROR);
580578

581579
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);
583581
check(!err);
584582

585583
err = reftable_stack_read_log(st, input.refname, &dest);
@@ -588,7 +586,7 @@ static void t_reftable_stack_log_normalize(void)
588586

589587
input.value.update.message = (char *) "two\n";
590588
arg.update_index = 2;
591-
err = reftable_stack_add(st, &write_test_log, &arg);
589+
err = reftable_stack_add(st, write_test_log, &arg);
592590
check(!err);
593591
err = reftable_stack_read_log(st, input.refname, &dest);
594592
check(!err);
@@ -602,16 +600,15 @@ static void t_reftable_stack_log_normalize(void)
602600

603601
static void t_reftable_stack_tombstone(void)
604602
{
605-
int i = 0;
606603
char *dir = get_tmp_dir(__LINE__);
607604
struct reftable_write_options opts = { 0 };
608605
struct reftable_stack *st = NULL;
609606
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 };
615612

616613
err = reftable_new_stack(&st, dir, &opts);
617614
check(!err);
@@ -637,7 +634,7 @@ static void t_reftable_stack_tombstone(void)
637634
}
638635
}
639636
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]);
641638
check(!err);
642639
}
643640

@@ -646,7 +643,7 @@ static void t_reftable_stack_tombstone(void)
646643
.log = &logs[i],
647644
.update_index = reftable_stack_next_update_index(st),
648645
};
649-
int err = reftable_stack_add(st, &write_test_log, &arg);
646+
int err = reftable_stack_add(st, write_test_log, &arg);
650647
check(!err);
651648
}
652649

@@ -695,12 +692,12 @@ static void t_reftable_stack_hash_id(void)
695692
struct reftable_stack *st32 = NULL;
696693
struct reftable_write_options opts_default = { 0 };
697694
struct reftable_stack *st_default = NULL;
698-
struct reftable_ref_record dest = { NULL };
695+
struct reftable_ref_record dest = { 0 };
699696

700697
err = reftable_new_stack(&st, dir, &opts);
701698
check(!err);
702699

703-
err = reftable_stack_add(st, &write_test_ref, &ref);
700+
err = reftable_stack_add(st, write_test_ref, &ref);
704701
check(!err);
705702

706703
/* can't read it with the wrong hash ID. */
@@ -743,21 +740,20 @@ static void t_reflog_expire(void)
743740
char *dir = get_tmp_dir(__LINE__);
744741
struct reftable_write_options opts = { 0 };
745742
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;
749745
int err;
750746
struct reftable_log_expiry_config expiry = {
751747
.time = 10,
752748
};
753-
struct reftable_log_record log = { NULL };
749+
struct reftable_log_record log = { 0 };
754750

755751
err = reftable_new_stack(&st, dir, &opts);
756752
check(!err);
757753

758754
for (i = 1; i <= N; i++) {
759755
char buf[256];
760-
snprintf(buf, sizeof(buf), "branch%02d", i);
756+
snprintf(buf, sizeof(buf), "branch%02"PRIuMAX, (uintmax_t)i);
761757

762758
logs[i].refname = xstrdup(buf);
763759
logs[i].update_index = i;
@@ -772,7 +768,7 @@ static void t_reflog_expire(void)
772768
.log = &logs[i],
773769
.update_index = reftable_stack_next_update_index(st),
774770
};
775-
int err = reftable_stack_add(st, &write_test_log, &arg);
771+
int err = reftable_stack_add(st, write_test_log, &arg);
776772
check(!err);
777773
}
778774

@@ -800,9 +796,8 @@ static void t_reflog_expire(void)
800796

801797
/* cleanup */
802798
reftable_stack_destroy(st);
803-
for (i = 0; i <= N; i++) {
799+
for (i = 0; i <= N; i++)
804800
reftable_log_record_release(&logs[i]);
805-
}
806801
clear_dir(dir);
807802
reftable_log_record_release(&log);
808803
}
@@ -824,7 +819,7 @@ static void t_empty_add(void)
824819
err = reftable_new_stack(&st, dir, &opts);
825820
check(!err);
826821

827-
err = reftable_stack_add(st, &write_nothing, NULL);
822+
err = reftable_stack_add(st, write_nothing, NULL);
828823
check(!err);
829824

830825
err = reftable_new_stack(&st2, dir, &opts);
@@ -851,8 +846,8 @@ static void t_reftable_stack_auto_compaction(void)
851846
};
852847
struct reftable_stack *st = NULL;
853848
char *dir = get_tmp_dir(__LINE__);
854-
int err, i;
855-
int N = 100;
849+
int err;
850+
size_t i, N = 100;
856851

857852
err = reftable_new_stack(&st, dir, &opts);
858853
check(!err);
@@ -865,9 +860,9 @@ static void t_reftable_stack_auto_compaction(void)
865860
.value_type = REFTABLE_REF_SYMREF,
866861
.value.symref = (char *) "master",
867862
};
868-
snprintf(name, sizeof(name), "branch%04d", i);
863+
snprintf(name, sizeof(name), "branch%04"PRIuMAX, (uintmax_t)i);
869864

870-
err = reftable_stack_add(st, &write_test_ref, &ref);
865+
err = reftable_stack_add(st, write_test_ref, &ref);
871866
check(!err);
872867

873868
err = reftable_stack_auto_compact(st);
@@ -929,7 +924,8 @@ static void t_reftable_stack_add_performs_auto_compaction(void)
929924
struct reftable_stack *st = NULL;
930925
struct strbuf refname = STRBUF_INIT;
931926
char *dir = get_tmp_dir(__LINE__);
932-
int err, i, n = 20;
927+
int err;
928+
size_t i, n = 20;
933929

934930
err = reftable_new_stack(&st, dir, &opts);
935931
check(!err);
@@ -949,10 +945,10 @@ static void t_reftable_stack_add_performs_auto_compaction(void)
949945
st->opts.disable_auto_compact = i != n;
950946

951947
strbuf_reset(&refname);
952-
strbuf_addf(&refname, "branch-%04d", i);
948+
strbuf_addf(&refname, "branch-%04"PRIuMAX, (uintmax_t)i);
953949
ref.refname = refname.buf;
954950

955-
err = reftable_stack_add(st, &write_test_ref, &ref);
951+
err = reftable_stack_add(st, write_test_ref, &ref);
956952
check(!err);
957953

958954
/*

0 commit comments

Comments
 (0)