t: port reftable/stack_test.c to the unit testing framework
cover t: port reftable/stack_test.c to the unit testing framework
The reftable library comes with self tests, which are exercised as part of the usual end-to-end tests and are designed to observe the end-user visible effects of Git commands. What it exercises, however, is a better match for the unit-testing framework, merged at 8bf6fbd0 (Merge branch 'js/doc-unit-tests', 2023-12-09), which is designed to observe how low level implementation details, at the level of sequences of individual function calls, behave.
Hence, port reftable/stack_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.
---
CI/PR: https://github.com/gitgitgadget/git/pull/1762
Chandra Pratap(6):
t: move reftable/stack_test.c to the unit testing framework
t: harmonize t-reftable-stack.c with coding guidelines
t-reftable-stack: use Git's tempfile API instead of mkstemp()
t-reftable-stack: use reftable_ref_record_equal() to compare ref records
t-reftable-stack: add test for non-default compaction factor
t-reftable-stack: add test for stack iterators
Makefile | 2 +- reftable/reftable-tests.h | 1 - t/helper/test-reftable.c | 1 - reftable/stack_test.c => t/unit-tests/t-reftable-stack.c | 600 +++++++++++++++++++-------------- 4 files changed, 355 insertions(+), 249 deletions(-)
PATCH 1/6 t: move reftable/stack_test.c to the unit testing framework
reftable/stack_test.c exercises the functions defined in reftable/stack.{c, h}. Migrate reftable/stack_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework and renaming the tests to be in-line with unit-tests' standards.
Since some of the tests use set_test_hash() defined by reftable/test_framework.{c, h} but these files are not '#included' in the test file, copy this function in the ported test file.
--- Makefile | 2 +- reftable/reftable-tests.h | 1 - t/helper/test-reftable.c | 1 - .../unit-tests/t-reftable-stack.c | 350 +++++++++--------- 4 files changed, 175 insertions(+), 179 deletions(-) rename reftable/stack_test.c => t/unit-tests/t-reftable-stack.c (77%)
Makefile +1 -1
@@ -1342,6 +1342,7 @@ UNIT_TEST_PROGRAMS += t-prio-queue UNIT_TEST_PROGRAMS += t-reftable-basics UNIT_TEST_PROGRAMS += t-reftable-merged UNIT_TEST_PROGRAMS += t-reftable-record +UNIT_TEST_PROGRAMS += t-reftable-stack UNIT_TEST_PROGRAMS += t-strbuf UNIT_TEST_PROGRAMS += t-strcmp-offset UNIT_TEST_PROGRAMS += t-strvec
@@ -2683,7 +2684,6 @@ REFTABLE_TEST_OBJS += reftable/block_test.o REFTABLE_TEST_OBJS += reftable/dump.o REFTABLE_TEST_OBJS += reftable/pq_test.o REFTABLE_TEST_OBJS += reftable/readwrite_test.o -REFTABLE_TEST_OBJS += reftable/stack_test.o REFTABLE_TEST_OBJS += reftable/test_framework.o REFTABLE_TEST_OBJS += reftable/tree_test.o
reftable/reftable-tests.h +0 -1
@@ -14,7 +14,6 @@ int block_test_main(int argc, const char **argv);int pq_test_main (int argc,const char **argv);int record_test_main (int argc,const char **argv);int readwrite_test_main (int argc,const char **argv); -int stack_test_main (int argc,const char **argv);int tree_test_main (int argc,const char **argv);int reftable_dump_main (int argc,char *const *argv);
t/helper/test-reftable.c +0 -1
@@ -9,7 +9,6 @@ int cmd__reftable(int argc, const char **argv)tree_test_main (argc, argv);pq_test_main (argc, argv);readwrite_test_main (argc, argv); -stack_test_main (argc, argv);return 0 ; }
t/unit-tests/t-reftable-stack.c renamed from reftable/stack_test.c +174 -176
842 lines not on this page · raw message
Signature · 1 line
2.45.GIT
PATCH 2/6 t: harmonize t-reftable-stack.c with coding guidelines
In reply to Chandra PratapHarmonize 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.
--- t/unit-tests/t-reftable-stack.c | 128 +++++++++++++++----------------- 1 file changed, 61 insertions(+), 67 deletions(-)
t/unit-tests/t-reftable-stack.c +61 -67
@@ -81,7 +81,6 @@ static void t_read_file(void)int n, err;char **names =NULL ;const char *want[] = {"line1" ,"line2" ,"line3" }; -int i =0 ; check_int(fd, >, 0); n =write_in_full (fd, out,strlen (out));
@@ -92,9 +91,8 @@ static void t_read_file(void) err =read_lines (fn, &names);check (!err); -for (i =0 ; names[i]; i++) { +for (size_t i =0 ; names[i]; i++)check_str (want[i], names[i]); - }free_names (names); (void )remove (fn); }
@@ -135,12 +133,12 @@ static void t_reftable_stack_add_one(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 };struct stat stat_result = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err); err =reftable_stack_read_ref (st, ref.refname, &dest);
@@ -212,16 +210,16 @@ static void t_reftable_stack_uptodate(void) err =reftable_new_stack (&st2, dir, &opts);check (!err); - err =reftable_stack_add (st1, &write_test_ref, &ref1); + err =reftable_stack_add (st1, write_test_ref, &ref1);check (!err); - err =reftable_stack_add (st2, &write_test_ref, &ref2); + err =reftable_stack_add (st2, write_test_ref, &ref2); check_int(err, ==, REFTABLE_OUTDATED_ERROR); err =reftable_stack_reload (st2);check (!err); - err =reftable_stack_add (st2, &write_test_ref, &ref2); + err =reftable_stack_add (st2, write_test_ref, &ref2);check (!err);reftable_stack_destroy (st1);reftable_stack_destroy (st2);
@@ -242,7 +240,7 @@ static void t_reftable_stack_transaction_api(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -252,7 +250,7 @@ static void t_reftable_stack_transaction_api(void) err =reftable_stack_new_addition (&add, st);check (!err); - err =reftable_addition_add (add, &write_test_ref, &ref); + err =reftable_addition_add (add, write_test_ref, &ref);check (!err); err =reftable_addition_commit (add);
@@ -276,7 +274,8 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void)struct reftable_write_options opts = {0 };struct reftable_addition *add =NULL ;struct reftable_stack *st =NULL ; -int i, n =20 , err; +size_t i, n =20 ; +int err; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -289,7 +288,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void) };char name[100 ]; -snprintf (name,sizeof (name),"branch%04d" , i); +snprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); ref.refname = name;/*
@@ -302,7 +301,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void) err =reftable_stack_new_addition (&add, st);check (!err); - err =reftable_addition_add (add, &write_test_ref, &ref); + err =reftable_addition_add (add, write_test_ref, &ref);check (!err); err =reftable_addition_commit (add);
@@ -395,10 +394,10 @@ static void t_reftable_stack_update_index_check(void) err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref1); + err =reftable_stack_add (st, write_test_ref, &ref1);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref2); + err =reftable_stack_add (st, write_test_ref, &ref2); check_int(err, ==, REFTABLE_API_ERROR);reftable_stack_destroy (st);clear_dir (dir);
@@ -414,7 +413,7 @@ static void t_reftable_stack_lock_failure(void) err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =-1 ; i !=REFTABLE_EMPTY_TABLE_ERROR ; i--) { - err =reftable_stack_add (st, &write_error, &i); + err =reftable_stack_add (st, write_error, &i); check_int(err, ==, i); }
@@ -424,8 +423,7 @@ static void t_reftable_stack_lock_failure(void)static void t_reftable_stack_add (void ) { -int i =0 ; -int err =0 ; +int err;struct reftable_write_options opts = { .exact_log_message =1 , .default_permissions =0660 ,
@@ -433,18 +431,18 @@ static void t_reftable_stack_add(void) };struct reftable_stack *st =NULL ;char *dir =get_tmp_dir (__LINE__); -struct reftable_ref_record refs[2 ] = { {NULL } }; -struct reftable_log_record logs[2 ] = { {NULL } }; +struct reftable_ref_record refs[2 ] = { {0 } }; +struct reftable_log_record logs[2 ] = { {0 } };struct strbuf path =STRBUF_INIT ;struct stat stat_result; -int N =ARRAY_SIZE (refs); +size_t N =ARRAY_SIZE (refs), i; err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =0 ; i <N ; i++) {char buf[256 ]; -snprintf (buf,sizeof (buf),"branch%02d" , i); +snprintf (buf,sizeof (buf),"branch%02" PRIuMAX, (uintmax_t )i); refs[i].refname =xstrdup (buf); refs[i].update_index = i +1 ; refs[i].value_type =REFTABLE_REF_VAL1 ;
@@ -458,7 +456,7 @@ static void t_reftable_stack_add(void) }for (i =0 ; i <N ; i++) { -int err =reftable_stack_add (st, &write_test_ref, &refs[i]); +int err =reftable_stack_add (st, write_test_ref, &refs[i]);check (!err); }
@@ -467,7 +465,7 @@ static void t_reftable_stack_add(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -475,7 +473,7 @@ static void t_reftable_stack_add(void)check (!err);for (i =0 ; i <N ; i++) { -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 };int err =reftable_stack_read_ref (st, refs[i].refname, &dest);check (!err);
@@ -485,7 +483,7 @@ static void t_reftable_stack_add(void) }for (i =0 ; i <N ; i++) { -struct reftable_log_record dest = {NULL }; +struct reftable_log_record dest = {0 };int err =reftable_stack_read_log (st, refs[i].refname, &dest);check (!err); check(reftable_log_record_equal(&dest, logs + i,
@@ -524,7 +522,7 @@ static void t_reftable_stack_add(void)static void t_reftable_stack_log_normalize (void ) { -int err =0 ; +int err;struct reftable_write_options opts = {0 , };
@@ -553,11 +551,11 @@ static void t_reftable_stack_log_normalize(void)check (!err); input.value.update.message = (char *)"one\ntwo" ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg); check_int(err, ==, REFTABLE_API_ERROR); input.value.update.message = (char *)"one" ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg);check (!err); err =reftable_stack_read_log (st, input.refname, &dest);
@@ -566,7 +564,7 @@ static void t_reftable_stack_log_normalize(void) input.value.update.message = (char *)"two\n" ; arg.update_index =2 ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg);check (!err); err =reftable_stack_read_log (st, input.refname, &dest);check (!err);
@@ -580,16 +578,15 @@ static void t_reftable_stack_log_normalize(void)static void t_reftable_stack_tombstone (void ) { -int i =0 ;char *dir =get_tmp_dir (__LINE__);struct reftable_write_options opts = {0 };struct reftable_stack *st =NULL ;int err; -struct reftable_ref_record refs[2 ] = { {NULL } }; -struct reftable_log_record logs[2 ] = { {NULL } }; -int N =ARRAY_SIZE (refs); -struct reftable_ref_record dest = {NULL }; -struct reftable_log_record log_dest = {NULL }; +struct reftable_ref_record refs[2 ] = { {0 } }; +struct reftable_log_record logs[2 ] = { {0 } }; +size_t N =ARRAY_SIZE (refs), i; +struct reftable_ref_record dest = {0 }; +struct reftable_log_record log_dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -615,7 +612,7 @@ static void t_reftable_stack_tombstone(void) } }for (i =0 ; i <N ; i++) { -int err =reftable_stack_add (st, &write_test_ref, &refs[i]); +int err =reftable_stack_add (st, write_test_ref, &refs[i]);check (!err); }
@@ -624,7 +621,7 @@ static void t_reftable_stack_tombstone(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -673,12 +670,12 @@ static void t_reftable_stack_hash_id(void)struct reftable_stack *st32 =NULL ;struct reftable_write_options opts_default = {0 };struct reftable_stack *st_default =NULL ; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err);/* can't read it with the wrong hash ID. */
@@ -721,21 +718,20 @@ static void t_reflog_expire(void)char *dir =get_tmp_dir (__LINE__);struct reftable_write_options opts = {0 };struct reftable_stack *st =NULL ; -struct reftable_log_record logs[20 ] = { {NULL } }; -int N =ARRAY_SIZE (logs) -1 ; -int i =0 ; +struct reftable_log_record logs[20 ] = { {0 } }; +size_t N =ARRAY_SIZE (logs) -1 , i;int err;struct reftable_log_expiry_config expiry = { .time =10 , }; -struct reftable_log_record log = {NULL }; +struct reftable_log_record log = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =1 ; i <=N ; i++) {char buf[256 ]; -snprintf (buf,sizeof (buf),"branch%02d" , i); +snprintf (buf,sizeof (buf),"branch%02" PRIuMAX, (uintmax_t )i); logs[i].refname =xstrdup (buf); logs[i].update_index = i;
@@ -750,7 +746,7 @@ static void t_reflog_expire(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -778,9 +774,8 @@ static void t_reflog_expire(void)/* cleanup */ reftable_stack_destroy (st); -for (i =0 ; i <=N ; i++) { +for (i =0 ; i <=N ; i++)reftable_log_record_release (&logs[i]); - }clear_dir (dir);reftable_log_record_release (&log); }
@@ -802,7 +797,7 @@ static void t_empty_add(void) err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_nothing,NULL ); + err =reftable_stack_add (st, write_nothing,NULL );check (!err); err =reftable_new_stack (&st2, dir, &opts);
@@ -829,8 +824,8 @@ static void t_reftable_stack_auto_compaction(void) };struct reftable_stack *st =NULL ;char *dir =get_tmp_dir (__LINE__); -int err, i; -int N =100 ; +int err; +size_t N =100 , i; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -843,9 +838,9 @@ static void t_reftable_stack_auto_compaction(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -snprintf (name,sizeof (name),"branch%04d" , i); +snprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err); err =reftable_stack_auto_compact (st);
@@ -866,7 +861,8 @@ static void t_reftable_stack_add_performs_auto_compaction(void)struct reftable_stack *st =NULL ;struct strbuf refname =STRBUF_INIT ;char *dir =get_tmp_dir (__LINE__); -int err, i, n =20 ; +int err; +size_t i, n =20 ; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -886,10 +882,10 @@ static void t_reftable_stack_add_performs_auto_compaction(void) st->opts.disable_auto_compact = i != n;strbuf_reset (&refname); -strbuf_addf (&refname,"branch-%04d" , i); +strbuf_addf (&refname,"branch-%04" PRIuMAX, (uintmax_t )i); ref.refname = refname.buf; - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err);/*
@@ -913,8 +909,8 @@ static void t_reftable_stack_compaction_concurrent(void)struct reftable_write_options opts = {0 };struct reftable_stack *st1 =NULL , *st2 =NULL ;char *dir =get_tmp_dir (__LINE__); -int err, i; -int N =3 ; +int err; +size_t N =3 , i; err =reftable_new_stack (&st1, dir, &opts);check (!err);
@@ -927,9 +923,9 @@ static void t_reftable_stack_compaction_concurrent(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -snprintf (name,sizeof (name),"branch%04d" , i); +snprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); - err =reftable_stack_add (st1, &write_test_ref, &ref); + err =reftable_stack_add (st1, write_test_ref, &ref);check (!err); }
@@ -949,10 +945,8 @@ static void t_reftable_stack_compaction_concurrent(void)static void unclean_stack_close (struct reftable_stack *st) {/* break abstraction boundary to simulate unclean shutdown. */ -int i =0 ; -for (; i < st->readers_len; i++) { +for (size_t i =0 ; i < st->readers_len; i++)reftable_reader_free (st->readers[i]); - } st->readers_len =0 ;FREE_AND_NULL (st->readers); }
@@ -962,8 +956,8 @@ static void t_reftable_stack_compaction_concurrent_clean(void)struct reftable_write_options opts = {0 };struct reftable_stack *st1 =NULL , *st2 =NULL , *st3 =NULL ;char *dir =get_tmp_dir (__LINE__); -int err, i; -int N =3 ; +int err; +size_t N =3 , i; err =reftable_new_stack (&st1, dir, &opts);check (!err);
@@ -976,9 +970,9 @@ static void t_reftable_stack_compaction_concurrent_clean(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -snprintf (name,sizeof (name),"branch%04d" , i); +snprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); - err =reftable_stack_add (st1, &write_test_ref, &ref); + err =reftable_stack_add (st1, write_test_ref, &ref);check (!err); }
Signature · 1 line
2.45.GIT
Re: [PATCH 2/6] t: harmonize t-reftable-stack.c with coding guidelines
Quoted 5 lines · Chandra Pratap
Harmonize the newly ported test
unit-tests/t-reftable-stack.cwith 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
s/'size_t'and'/'size_t' and/
Quoted 7 lines
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.
Signed-off-by Chandra Pratap <[email protected]>
Re: [PATCH 2/6] t: harmonize t-reftable-stack.c with coding guidelines
In reply to Chandra Pratapt/unit-tests/t-reftable-stack.c static void t_reftable_stack_add(void) · line 435
Quoted 8 lines · Chandra Pratap
t/unit-tests/t-reftable-stack.c +2
-2@@ -433,18 +431,18 @@ static void t_reftable_stack_add(void) };struct reftable_stack *st =NULL ;char *dir =get_tmp_dir (__LINE__); -struct reftable_ref_record refs[2 ] = { {NULL } }; -struct reftable_log_record logs[2 ] = { {NULL } }; +struct reftable_ref_record refs[2 ] = { {0 } }; +struct reftable_log_record logs[2 ] = { {0 } };
These should simply be { 0 }, here and in a few other places further down.
Patrick
PATCH 3/6 t-reftable-stack: use Git's tempfile API instead of mkstemp()
In reply to Chandra PratapGit's tempfile API defined by $GIT_DIR/tempfile.{c, h} provides a unified interface for tempfile operations. Since reftable/stack.c uses this API for all its tempfile needs instead of raw functions like mkstemp(), make the ported stack test strictly use Git's tempfile API as well.
--- t/unit-tests/t-reftable-stack.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
t/unit-tests/t-reftable-stack.c +3 -1
@@ -76,7 +76,8 @@ static char *get_tmp_dir(int linenumber)static void t_read_file (void ) {char *fn =get_tmp_template (__LINE__); -int fd =mkstemp (fn); +struct tempfile *tmp =mks_tempfile (fn); +int fd =get_tempfile_fd (tmp);char out[1024 ] ="line1\n\nline2\nline3" ;int n, err;char **names =NULL ;
@@ -95,6 +96,7 @@ static void t_read_file(void)check_str (want[i], names[i]);free_names (names); (void )remove (fn); +delete_tempfile (&tmp); }static int write_test_ref (struct reftable_writer *wr,void *arg)
Signature · 1 line
2.45.GIT
Re: [PATCH 3/6] t-reftable-stack: use Git's tempfile API instead of mkstemp()
Quoted 1 line · Chandra Pratap
Git's tempfile API defined by $GIT_DIR/tempfile.{c, h} provides
You are using the syntax of (some) shells to express "repeat the string with 'c' and 'h' appended" here, so drop SP after the comma.
Re: [PATCH 3/6] t-reftable-stack: use Git's tempfile API instead of mkstemp()
In reply to Chandra PratapQuoted 28 lines · Chandra Pratap
Git's tempfile API defined by $GIT_DIR/tempfile.{c, h} provides a unified interface for tempfile operations. Since
reftable/stack.cuses this API for all its tempfile needs instead of raw functions likemkstemp(), make the ported stack test strictly use Git's tempfile API as well.Mentored-by Patrick Steinhardt <[email protected]>Mentored-by Christian Couder <[email protected]>Signed-off-by Chandra Pratap <[email protected]>--- t/unit-tests/t-reftable-stack.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)t/unit-tests/t-reftable-stack.c +2
-1@@ -76,7 +76,8 @@ static char *get_tmp_dir(int linenumber)static void t_read_file (void ) {char *fn =get_tmp_template (__LINE__); -int fd =mkstemp (fn); +struct tempfile *tmp =mks_tempfile (fn); +int fd =get_tempfile_fd (tmp);char out[1024 ] ="line1\n\nline2\nline3" ;int n, err;char **names =NULL ;
t/unit-tests/t-reftable-stack.c static void t_read_file(void) · line 100
Quoted 5 lines
t/unit-tests/t-reftable-stack.c +1
-0@@ -95,6 +96,7 @@ static void t_read_file(void)check_str (want[i], names[i]);free_names (names); (void )remove (fn); +delete_tempfile (&tmp); }
I'm a bit torn whether this is a good idea because we are using a higher level interface that doesn't have unit tests itself. As I see it, both low-level primitives and anything that is already verified via another set of unit tests can be used when writing unit tests. But as far as I know, the tempfile interface does not yet have any.
Maybe I'm being overthinking this though.
Ideally, we wouldn't have to care about the underlying issue at all, namely cleanup of the temporary file. This is something that the clar brings to us for free: it can create temporary directories that it also knows to clean up automatically once done.
Patrick
PATCH 4/6 t-reftable-stack: use reftable_ref_record_equal() to compare ref records
In reply to Chandra PratapIn the current stack tests, ref records are compared for equality by sometimes using the dedicated function for ref-record comparison, reftable_ref_record_equal(), and sometimes by explicity comparing contents of the ref records.
Replace the latter instances of ref-record comparison with the former to maintain uniformity throughout the test file.
--- t/unit-tests/t-reftable-stack.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
t/unit-tests/t-reftable-stack.c +2 -3
@@ -145,7 +145,7 @@ static void t_reftable_stack_add_one(void) err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ )); check_int(st->readers_len, >, 0);printf ("testing print functionality:\n" );
@@ -262,8 +262,7 @@ static void t_reftable_stack_transaction_api(void) err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); - check_int(REFTABLE_REF_SYMREF, ==, dest.value_type); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ ));reftable_ref_record_release (&dest);reftable_stack_destroy (st);
Signature · 1 line
2.45.GIT
Re: [PATCH 4/6] t-reftable-stack: use reftable_ref_record_equal() to compare ref records
t/unit-tests/t-reftable-stack.c static void t_reftable_stack_add_one(void) · line 149
Quoted 11 lines · Chandra Pratap
t/unit-tests/t-reftable-stack.c +1
-1@@ -145,7 +145,7 @@ static void t_reftable_stack_add_one(void) err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ )); check_int(st->readers_len, >, 0);
I think the change itself is sensible as long as we have tests that verify that reftable_ref_record_equal() itself behaves as expected. I don't think we have such tests anywhere though, uncovering a test gap.
Patrick
Re: [PATCH 4/6] t-reftable-stack: use reftable_ref_record_equal() to compare ref records
Quoted 8 lines · Patrick Steinhardt
Quoted 6 lines · Chandra Pratap
t/unit-tests/t-reftable-stack.c +0
-0@@ -145,7 +145,7 @@ static void t_reftable_stack_add_one(void)
Quoted 9 lines
t/unit-tests/t-reftable-stack.c static void t_reftable_stack_add_one(void) · line 149
Quoted 5 lines
t/unit-tests/t-reftable-stack.c +1
-1err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ )); check_int(st->readers_len, >, 0);I think the change itself is sensible as long as we have tests that verify that
reftable_ref_record_equal()itself behaves as expected. I don't think we have such tests anywhere though, uncovering a test gap.
We _do_ test reftable_record_equal (which is a wrapper for reftable_ref_record_equal in the case of ref records) in the recently ported t-reftable-record test. Here is the test exercising this function in unit-tests/t-reftable-record.c:
static void t_reftable_ref_record_comparison(void) {
struct reftable_record in[3] = {
{
.type = BLOCK_TYPE_REF,
.u.ref.refname = (char *) "refs/heads/master",
.u.ref.value_type = REFTABLE_REF_VAL1,
},
{
.type = BLOCK_TYPE_REF,
.u.ref.refname = (char *) "refs/heads/master",
.u.ref.value_type = REFTABLE_REF_DELETION,
},
{
.type = BLOCK_TYPE_REF,
.u.ref.refname = (char *) "HEAD",
.u.ref.value_type = REFTABLE_REF_SYMREF,
.u.ref.value.symref = (char *) "refs/heads/master",
},};
check(!reftable_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ)); check(!reftable_record_cmp(&in[0], &in[1])); check(!reftable_record_equal(&in[1], &in[2], GIT_SHA1_RAWSZ)); check_int(reftable_record_cmp(&in[1], &in[2]), >, 0);
in[1].u.ref.value_type = in[0].u.ref.value_type; check(reftable_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ)); check(!reftable_record_cmp(&in[0], &in[1]));
}
Re: [PATCH 4/6] t-reftable-stack: use reftable_ref_record_equal() to compare ref records
Quoted 19 lines · Chandra Pratap
Quoted 8 lines · Patrick Steinhardt
Quoted 6 lines · Chandra Pratap
t/unit-tests/t-reftable-stack.c +0
-0@@ -145,7 +145,7 @@ static void t_reftable_stack_add_one(void)Quoted 9 lines
t/unit-tests/t-reftable-stack.c static void t_reftable_stack_add_one(void) · line 149
Quoted 5 lines
t/unit-tests/t-reftable-stack.c +1
-1err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ )); check_int(st->readers_len, >, 0);I think the change itself is sensible as long as we have tests that verify that
reftable_ref_record_equal()itself behaves as expected. I don't think we have such tests anywhere though, uncovering a test gap.
Quoted 4 lines
We _do_ test
reftable_record_equal(which is a wrapper forreftable_ref_record_equalin the case of ref records) in the recently ported t-reftable-record test. Here is the test exercising this function inunit-tests/t-reftable-record.c:
Ah, great, never mind then! I didn't see this test because we are using reftable_ref_record_equal() here, whereas the test uses reftable_record_equal(). But the latter uses the former for ref records, so that's fine.
Patrick
PATCH 5/6 t-reftable-stack: add test for non-default compaction factor
In reply to Chandra PratapIn a recent codebase update (commit ae8e378430, merge branch 'ps/reftable-write-options', 2024/05/13) the geometric factor used in auto-compaction of reftable tables was made configurable. Add a test to verify the functionality introduced by this update.
--- t/unit-tests/t-reftable-stack.c | 41 +++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-)
t/unit-tests/t-reftable-stack.c +37 -4
@@ -808,12 +808,12 @@ static void t_empty_add(void)reftable_stack_destroy (st2); } -static int fastlog2 (uint64_t sz) +static int fastlogN (uint64_t sz,uint64_t N ) {int l =0 ;if (sz ==0 )return 0 ; -for (; sz; sz /=2 ) +for (; sz; sz /=N ) l++;return l -1 ; }
@@ -846,11 +846,43 @@ static void t_reftable_stack_auto_compaction(void) err =reftable_stack_auto_compact (st);check (!err); -check (i <3 || st->merged->stack_len <2 *fastlog2 (i)); +check (i <2 || st->merged->stack_len <2 *fastlogN (i,2 )); } check_int(reftable_stack_compaction_stats(st)->entries_written, <, - (uint64_t )(N *fastlog2 (N ))); + (uint64_t )(N *fastlogN (N ,2 ))); + +reftable_stack_destroy (st); +clear_dir (dir); +} + +static void t_reftable_stack_auto_compaction_factor (void ) +{ +struct reftable_write_options opts = { + .auto_compaction_factor =5 , + }; +struct reftable_stack *st =NULL ; +char *dir =get_tmp_dir (__LINE__); +int err; +size_t N =100 ; + + err =reftable_new_stack (&st, dir, &opts); +check (!err); + +for (size_t i =0 ; i <N ; i++) { +char name[20 ]; +struct reftable_ref_record ref = { + .refname = name, + .update_index =reftable_stack_next_update_index (st), + .value_type =REFTABLE_REF_VAL1 , + }; +xsnprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); + + err =reftable_stack_add (st, &write_test_ref, &ref); +check (!err); + +check (i <5 || st->merged->stack_len <5 *fastlogN (i,5 )); + }reftable_stack_destroy (st);clear_dir (dir);
@@ -1009,6 +1041,7 @@ int cmd_main(int argc, const char *argv[])TEST (t_reftable_stack_add_one (),"add a single ref record to stack" );TEST (t_reftable_stack_add_performs_auto_compaction (),"addition to stack triggers auto-compaction" );TEST (t_reftable_stack_auto_compaction (),"stack must form geometric sequence after compaction" ); +TEST (t_reftable_stack_auto_compaction_factor (),"auto-compaction with non-default geometric factor" );TEST (t_reftable_stack_auto_compaction_fails_gracefully (),"failure on auto-compaction" );TEST (t_reftable_stack_compaction_concurrent (),"compaction with concurrent stack" );TEST (t_reftable_stack_compaction_concurrent_clean (),"compaction with unclean stack shutdown" );
Signature · 1 line
2.45.GIT
PATCH 6/6 t-reftable-stack: add test for stack iterators
In reply to Chandra Pratapreftable_stack_init_ref_iterator and reftable_stack_init_log_iterator as defined by reftable/stack.{c, h} initialize a stack iterator to iterate over the ref and log records in a reftable stack respectively. Since these functions are not exercised by any of the existing tests, add a test for them.
--- t/unit-tests/t-reftable-stack.c | 82 +++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)
t/unit-tests/t-reftable-stack.c +82 -0
@@ -521,6 +521,87 @@ static void t_reftable_stack_add(void)clear_dir (dir); } +static void t_reftable_stack_iterator (void ) +{ +struct reftable_write_options opts = {0 }; +struct reftable_stack *st =NULL ; +char *dir =get_tmp_dir (__LINE__); +struct reftable_ref_record refs[10 ] = { {0 } }; +struct reftable_log_record logs[10 ] = { {0 } }; +struct reftable_iterator it = {0 }; +size_t N =ARRAY_SIZE (refs), i; +int err; + + err =reftable_new_stack (&st, dir, &opts); +check (!err); + +for (i =0 ; i <N ; i++) { +char buf[20 ]; +xsnprintf (buf,sizeof (buf),"branch%02" PRIuMAX, (uintmax_t )i); + refs[i].refname =xstrdup (buf); + refs[i].update_index = i +1 ; + refs[i].value_type =REFTABLE_REF_VAL1 ; +set_test_hash (refs[i].value.val1, i); + + logs[i].refname =xstrdup (buf); + logs[i].update_index = i +1 ; + logs[i].value_type =REFTABLE_LOG_UPDATE ; + logs[i].value.update.email =xstrdup ("johndoe@invalid" ); + logs[i].value.update.message =xstrdup ("commit\n" ); +set_test_hash (logs[i].value.update.new_hash, i); + } + +for (i =0 ; i <N ; i++) { + err =reftable_stack_add (st, &write_test_ref, &refs[i]); +check (!err); + } + +for (i =0 ; i <N ; i++) { +struct write_log_arg arg = { + .log = &logs[i], + .update_index =reftable_stack_next_update_index (st), + }; + err =reftable_stack_add (st, &write_test_log, &arg); +check (!err); + } + +reftable_stack_init_ref_iterator (st, &it); +reftable_iterator_seek_ref (&it, refs[0 ].refname); +for (i =0 ; ; i++) { +struct reftable_ref_record ref = {0 }; + err =reftable_iterator_next_ref (&it, &ref); +if (err >0 ) +break ; +check (!err); +check (reftable_ref_record_equal (&ref, &refs[i],GIT_SHA1_RAWSZ )); +reftable_ref_record_release (&ref); + } + check_int(i, ==, N); + +reftable_iterator_destroy (&it); + +reftable_stack_init_log_iterator (st, &it); +reftable_iterator_seek_log (&it, logs[0 ].refname); +for (i =0 ; ; i++) { +struct reftable_log_record log = {0 }; + err =reftable_iterator_next_log (&it, &log); +if (err >0 ) +break ; +check (!err); +check (reftable_log_record_equal (&log, &logs[i],GIT_SHA1_RAWSZ )); +reftable_log_record_release (&log); + } + check_int(i, ==, N); + +reftable_stack_destroy (st); +reftable_iterator_destroy (&it); +for (i =0 ; i <N ; i++) { +reftable_ref_record_release (&refs[i]); +reftable_log_record_release (&logs[i]); + } +clear_dir (dir); +} +static void t_reftable_stack_log_normalize (void ) {int err;
@@ -1046,6 +1127,7 @@ int cmd_main(int argc, const char *argv[])TEST (t_reftable_stack_compaction_concurrent (),"compaction with concurrent stack" );TEST (t_reftable_stack_compaction_concurrent_clean (),"compaction with unclean stack shutdown" );TEST (t_reftable_stack_hash_id (),"read stack with wrong hash ID" ); +TEST (t_reftable_stack_iterator (),"log and ref iterator for reftable stack" );TEST (t_reftable_stack_lock_failure (),"stack addition with lockfile failure" );TEST (t_reftable_stack_log_normalize (),"log messages should be normalized" );TEST (t_reftable_stack_tombstone (),"'tombstone' refs in stack" );
Signature · 1 line
2.45.GIT
Re: [PATCH 6/6] t-reftable-stack: add test for stack iterators
Quoted 2 lines · Chandra Pratap
reftable_stack_init_ref_iteratorandreftable_stack_init_log_iteratoras defined by reftable/stack.{c, h} initialize a stack iterator to
Same remark here that Junio posted on a preceding catch, please drop the space in stack.{c,h}.
t/unit-tests/t-reftable-stack.c static void t_reftable_stack_add(void) · line 530
Quoted 11 lines
t/unit-tests/t-reftable-stack.c +7
-0@@ -521,6 +521,87 @@ static void t_reftable_stack_add(void)clear_dir (dir); } +static void t_reftable_stack_iterator (void ) +{ +struct reftable_write_options opts = {0 }; +struct reftable_stack *st =NULL ; +char *dir =get_tmp_dir (__LINE__); +struct reftable_ref_record refs[10 ] = { {0 } }; +struct reftable_log_record logs[10 ] = { {0 } };
Again, these should be initialized with = { 0 }.
t/unit-tests/t-reftable-stack.c static void t_reftable_stack_add(void) · line 541
Quoted 11 lines
t/unit-tests/t-reftable-stack.c +11
-0+struct reftable_iterator it = {0 }; +size_t N =ARRAY_SIZE (refs), i; +int err; + + err =reftable_new_stack (&st, dir, &opts); +check (!err); + +for (i =0 ; i <N ; i++) { +char buf[20 ]; +xsnprintf (buf,sizeof (buf),"branch%02" PRIuMAX, (uintmax_t )i); + refs[i].refname =xstrdup (buf);
This can be simplified to a single call to xstrfmt().
Patrick
Quoted 9 lines · Chandra Pratap
The reftable library comes with self tests, which are exercised as part of the usual end-to-end tests and are designed to observe the end-user visible effects of Git commands. What it exercises, however, is a better match for the unit-testing framework, merged at
8bf6fbd0(Merge branch 'js/doc-unit-tests', 2023-12-09), which is designed to observe how low level implementation details, at the level of sequences of individual function calls, behave.
Quoted 4 lines
Hence, port
reftable/stack_test.cto 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.
However, reftable/stack_test.c currently is a moving target because there is an in-flight topic that improves the table compaction and that topic wants to add more tests there. So let's wait until the dust from the other topic settles before doing the first step of this topic to move the file to t/unit-tests/ hiearchy.
Thanks.
Quoted 20 lines
Mentored-by Patrick Steinhardt <[email protected]>Mentored-by Christian Couder <[email protected]>Signed-off-by Chandra Pratap <[email protected]>---
CI/PR: https://github.com/gitgitgadget/git/pull/1762Chandra Pratap(6):
t: movereftable/stack_test.cto the unit testing framework
t: harmonizet-reftable-stack.cwith coding guidelines
t-reftable-stack: use Git's tempfile API instead ofmkstemp()
t-reftable-stack: usereftable_ref_record_equal()to compare ref records
t-reftable-stack: add test for non-default compaction factor
t-reftable-stack: add test for stack iteratorsMakefile | 2 +- reftable/reftable-tests.h | 1 - t/helper/test-reftable.c | 1 - reftable/stack_test.c => t/unit-tests/t-reftable-stack.c | 600 +++++++++++++++++++-------------- 4 files changed, 355 insertions(+), 249 deletions(-)
Quoted 17 lines · Junio C Hamano
Quoted 9 lines · Chandra Pratap
The reftable library comes with self tests, which are exercised as part of the usual end-to-end tests and are designed to observe the end-user visible effects of Git commands. What it exercises, however, is a better match for the unit-testing framework, merged at
8bf6fbd0(Merge branch 'js/doc-unit-tests', 2023-12-09), which is designed to observe how low level implementation details, at the level of sequences of individual function calls, behave.Quoted 4 lines
Hence, port
reftable/stack_test.cto 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.
Quoted 8 lines
However,
reftable/stack_test.ccurrently is a moving target because there is an in-flight topic that improves the table compaction and that topic wants to add more tests there. So let's wait until the dust from the other topic settles before doing the first step of this topic to move the file to t/unit-tests/ hiearchy.Thanks.
Oh okay, I will focus on the other tests in the meantime.
--snip--
v2 cover t: port reftable/stack_test.c to the unit testing framework
In reply to Chandra PratapThe reftable library comes with self tests, which are exercised as part of the usual end-to-end tests and are designed to observe the end-user visible effects of Git commands. What it exercises, however, is a better match for the unit-testing framework, merged at 8bf6fbd0 (Merge branch 'js/doc-unit-tests', 2023-12-09), which is designed to observe how low level implementation details, at the level of sequences of individual function calls, behave.
Hence, port reftable/stack_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.
---
Changes in v2:
- Rebase the branch on top of latest 'master', this ensures that updates to reftable/stack_test.c are included in the ported test.
- Fix a typo in the commit message patch 2.
- Use ' = { 0 }' to 0-initialize arrays of structs instead of ' = { { 0 } }' in patch 2.
- use 'xstrfmt()' instead of a char buffer and 'xstrdup()' in the new test in patch 6.
CI/PR: https://github.com/gitgitgadget/git/pull/1762
Chandra Pratap(6):
t: move reftable/stack_test.c to the unit testing framework
t: harmonize t-reftable-stack.c with coding guidelines
t-reftable-stack: use Git's tempfile API instead of mkstemp()
t-reftable-stack: use reftable_ref_record_equal() to compare ref records
t-reftable-stack: add test for non-default compaction factor
t-reftable-stack: add test for stack iterators
Makefile | 2 +- reftable/reftable-tests.h | 1 - t/helper/test-reftable.c | 1 - reftable/stack_test.c => t/unit-tests/t-reftable-stack.c | 611 +++++++++++++++++++-------------- 4 files changed, 360 insertions(+), 255 deletions(-)
Range-diff against v1:
<rebase commits>
1: bd595290dc ! 167: b401ef765a t: move reftable/stack_test.c to the unit testing framework
@@ Commit message
Signed-off-by: Chandra Pratap <[email protected]>
## Makefile ##
-@@ Makefile: UNIT_TEST_PROGRAMS += t-prio-queue
- UNIT_TEST_PROGRAMS += t-reftable-basics
+@@ Makefile: UNIT_TEST_PROGRAMS += t-reftable-basics
UNIT_TEST_PROGRAMS += t-reftable-merged
+ UNIT_TEST_PROGRAMS += t-reftable-pq
UNIT_TEST_PROGRAMS += t-reftable-record
+UNIT_TEST_PROGRAMS += t-reftable-stack
+ UNIT_TEST_PROGRAMS += t-reftable-tree
UNIT_TEST_PROGRAMS += t-strbuf
UNIT_TEST_PROGRAMS += t-strcmp-offset
- UNIT_TEST_PROGRAMS += t-strvec
-@@ Makefile: REFTABLE_TEST_OBJS += reftable/block_test.o
+@@ Makefile: REFTABLE_OBJS += reftable/writer.o
+ REFTABLE_TEST_OBJS += reftable/block_test.o
REFTABLE_TEST_OBJS += reftable/dump.o
- REFTABLE_TEST_OBJS += reftable/pq_test.o
REFTABLE_TEST_OBJS += reftable/readwrite_test.o
-REFTABLE_TEST_OBJS += reftable/stack_test.o
REFTABLE_TEST_OBJS += reftable/test_framework.o
- REFTABLE_TEST_OBJS += reftable/tree_test.o
+ TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS)) $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS))
## reftable/reftable-tests.h ##
-@@ reftable/reftable-tests.h: int block_test_main(int argc, const char **argv);
- int pq_test_main(int argc, const char **argv);
+@@ reftable/reftable-tests.h: int basics_test_main(int argc, const char **argv);
+ int block_test_main(int argc, const char **argv);
int record_test_main(int argc, const char **argv);
int readwrite_test_main(int argc, const char **argv);
-int stack_test_main(int argc, const char **argv);
- int tree_test_main(int argc, const char **argv);
int reftable_dump_main(int argc, char *const *argv);
+ #endif
## t/helper/test-reftable.c ##
@@ t/helper/test-reftable.c: int cmd__reftable(int argc, const char **argv)
- tree_test_main(argc, argv);
- pq_test_main(argc, argv);
+ /* test from simple to complex. */
+ block_test_main(argc, argv);
readwrite_test_main(argc, argv);
- stack_test_main(argc, argv);
return 0;
@@ t/unit-tests/t-reftable-stack.c: license that can be found in the LICENSE file o
+#include "reftable/stack.h"
#include <dirent.h>
- static void clear_dir(const char *dirname)
-@@ t/unit-tests/t-reftable-stack.c: static void clear_dir(const char *dirname)
- strbuf_release(&path);
- }
-
+static void set_test_hash(uint8_t *p, int i)
+{
+ memset(p, (uint8_t)i, hash_size(GIT_SHA1_FORMAT_ID));
+}
+
- static int count_dir_entries(const char *dirname)
+ static void clear_dir(const char *dirname)
{
- DIR *dir = opendir(dirname);
+ struct strbuf path = STRBUF_INIT;
@@ t/unit-tests/t-reftable-stack.c: static char *get_tmp_template(int linenumber)
static char *get_tmp_dir(int linenumber)
{
@@ t/unit-tests/t-reftable-stack.c: static void test_read_file(void)
}
free_names(names);
(void) remove(fn);
+@@ t/unit-tests/t-reftable-stack.c: static void write_n_ref_tables(struct reftable_stack *st,
+ set_test_hash(ref.value.val1, i);
+
+ err = reftable_stack_add(st, &write_test_ref, &ref);
+- EXPECT_ERR(err);
++ check(!err);
+ }
+
+ st->opts.disable_auto_compact = disable_auto_compact;
@@ t/unit-tests/t-reftable-stack.c: static int write_test_log(struct reftable_writer *wr, void *arg)
return reftable_writer_add_log(wr, wla->log);
}
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_auto_compaction
}
- EXPECT(reftable_stack_compaction_stats(st)->entries_written <
-- (uint64_t)(N * fastlog2(N)));
+ check_int(reftable_stack_compaction_stats(st)->entries_written, <,
-+ (uint64_t)(N * fastlog2(N)));
+ (uint64_t)(N * fastlog2(N)));
+
+ reftable_stack_destroy(st);
+ clear_dir(dir);
+ }
+
+-static void test_reftable_stack_auto_compaction_with_locked_tables(void)
++static void t_reftable_stack_auto_compaction_with_locked_tables(void)
+ {
+ struct reftable_write_options opts = {
+ .disable_auto_compact = 1,
+@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_auto_compaction_with_locked_tables(void)
+ int err;
+
+ err = reftable_new_stack(&st, dir, &opts);
+- EXPECT_ERR(err);
++ check(!err);
+
+ write_n_ref_tables(st, 5);
+- EXPECT(st->merged->stack_len == 5);
++ check_int(st->merged->stack_len, ==, 5);
+
+ /*
+ * Given that all tables we have written should be roughly the same
+@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_auto_compaction_with_locked_tables(void)
+ * only compact the newest two tables.
+ */
+ err = reftable_stack_auto_compact(st);
+- EXPECT_ERR(err);
+- EXPECT(st->stats.failures == 0);
+- EXPECT(st->merged->stack_len == 4);
++ check(!err);
++ check_int(st->stats.failures, ==, 0);
++ check_int(st->merged->stack_len, ==, 4);
reftable_stack_destroy(st);
+ strbuf_release(&buf);
clear_dir(dir);
}
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_add_performs_au
clear_dir(dir);
}
+-static void test_reftable_stack_compaction_with_locked_tables(void)
++static void t_reftable_stack_compaction_with_locked_tables(void)
+ {
+ struct reftable_write_options opts = {
+ .disable_auto_compact = 1,
+@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_compaction_with_locked_tables(void)
+ int err;
+
+ err = reftable_new_stack(&st, dir, &opts);
+- EXPECT_ERR(err);
++ check(!err);
+
+ write_n_ref_tables(st, 3);
+- EXPECT(st->merged->stack_len == 3);
++ check_int(st->merged->stack_len, ==, 3);
+
+ /* Lock one of the tables that we're about to compact. */
+ strbuf_reset(&buf);
+@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_compaction_with_locked_tables(void)
+ * compact all tables.
+ */
+ err = reftable_stack_compact_all(st, NULL);
+- EXPECT(err == REFTABLE_LOCK_ERROR);
+- EXPECT(st->stats.failures == 1);
+- EXPECT(st->merged->stack_len == 3);
++ check_int(err, ==, REFTABLE_LOCK_ERROR);
++ check_int(st->stats.failures, ==, 1);
++ check_int(st->merged->stack_len, ==, 3);
+
+ reftable_stack_destroy(st);
+ strbuf_release(&buf);
+ clear_dir(dir);
+ }
+
-static void test_reftable_stack_compaction_concurrent(void)
+static void t_reftable_stack_compaction_concurrent(void)
{
struct reftable_write_options opts = { 0 };
struct reftable_stack *st1 = NULL, *st2 = NULL;
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_compaction_concurrent(void)
- int N = 3;
+ int err;
err = reftable_new_stack(&st1, dir, &opts);
- EXPECT_ERR(err);
+ check(!err);
-
- for (i = 0; i < N; i++) {
- char name[100];
-@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_compaction_concurrent(void)
- snprintf(name, sizeof(name), "branch%04d", i);
-
- err = reftable_stack_add(st1, &write_test_ref, &ref);
-- EXPECT_ERR(err);
-+ check(!err);
- }
+ write_n_ref_tables(st1, 3);
err = reftable_new_stack(&st2, dir, &opts);
- EXPECT_ERR(err);
@@ t/unit-tests/t-reftable-stack.c: static void unclean_stack_close(struct reftable
struct reftable_write_options opts = { 0 };
struct reftable_stack *st1 = NULL, *st2 = NULL, *st3 = NULL;
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_compaction_concurrent_clean(void)
- int N = 3;
+ int err;
err = reftable_new_stack(&st1, dir, &opts);
- EXPECT_ERR(err);
+ check(!err);
-
- for (i = 0; i < N; i++) {
- char name[100];
-@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_compaction_concurrent_clean(void)
- snprintf(name, sizeof(name), "branch%04d", i);
-
- err = reftable_stack_add(st1, &write_test_ref, &ref);
-- EXPECT_ERR(err);
-+ check(!err);
- }
+ write_n_ref_tables(st1, 3);
err = reftable_new_stack(&st2, dir, &opts);
- EXPECT_ERR(err);
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_compaction_conc
- RUN_TEST(test_reftable_stack_add);
- RUN_TEST(test_reftable_stack_add_one);
- RUN_TEST(test_reftable_stack_auto_compaction);
+- RUN_TEST(test_reftable_stack_auto_compaction_with_locked_tables);
- RUN_TEST(test_reftable_stack_add_performs_auto_compaction);
- RUN_TEST(test_reftable_stack_compaction_concurrent);
- RUN_TEST(test_reftable_stack_compaction_concurrent_clean);
+- RUN_TEST(test_reftable_stack_compaction_with_locked_tables);
- RUN_TEST(test_reftable_stack_hash_id);
- RUN_TEST(test_reftable_stack_lock_failure);
- RUN_TEST(test_reftable_stack_log_normalize);
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_compaction_conc
+ TEST(t_reftable_stack_add_performs_auto_compaction(), "addition to stack triggers auto-compaction");
+ TEST(t_reftable_stack_auto_compaction(), "stack must form geometric sequence after compaction");
+ TEST(t_reftable_stack_auto_compaction_fails_gracefully(), "failure on auto-compaction");
++ TEST(t_reftable_stack_auto_compaction_with_locked_tables(), "auto compaction with locked tables");
+ TEST(t_reftable_stack_compaction_concurrent(), "compaction with concurrent stack");
+ TEST(t_reftable_stack_compaction_concurrent_clean(), "compaction with unclean stack shutdown");
++ TEST(t_reftable_stack_compaction_with_locked_tables(), "compaction with locked tables");
+ TEST(t_reftable_stack_hash_id(), "read stack with wrong hash ID");
+ TEST(t_reftable_stack_lock_failure(), "stack addition with lockfile failure");
+ TEST(t_reftable_stack_log_normalize(), "log messages should be normalized");
2: 64d91d89a5 ! 168: e1704047cb t: harmonize t-reftable-stack.c with coding guidelines
@@ Commit message
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
+ - Array sizes and indices should preferably be of type 'size_t' and
not 'int'.
- Function pointers should be passed as 'func' and not '&func'.
@@ t/unit-tests/t-reftable-stack.c: static void t_read_file(void)
free_names(names);
(void) remove(fn);
}
+@@ t/unit-tests/t-reftable-stack.c: static void write_n_ref_tables(struct reftable_stack *st,
+ .value_type = REFTABLE_REF_VAL1,
+ };
+
+- strbuf_addf(&buf, "refs/heads/branch-%04u", (unsigned) i);
++ strbuf_addf(&buf, "refs/heads/branch-%04"PRIuMAX, (uintmax_t)i);
+ ref.refname = buf.buf;
+ set_test_hash(ref.value.val1, i);
+
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add_one(void)
.value_type = REFTABLE_REF_SYMREF,
.value.symref = (char *) "master",
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_transaction_api_pe
struct reftable_addition *add = NULL;
struct reftable_stack *st = NULL;
- int i, n = 20, err;
-+ size_t i, n = 20;
++ size_t n = 20;
+ int err;
err = reftable_new_stack(&st, dir, &opts);
check(!err);
+
+- for (i = 0; i <= n; i++) {
++ for (size_t i = 0; i <= n; i++) {
+ struct reftable_ref_record ref = {
+ .update_index = reftable_stack_next_update_index(st),
+ .value_type = REFTABLE_REF_SYMREF,
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_transaction_api_performs_auto_compaction(void)
};
char name[100];
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_transaction_api_pe
check(!err);
err = reftable_addition_commit(add);
+@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_auto_compaction_fails_gracefully(void)
+ .value_type = REFTABLE_REF_VAL1,
+ .value.val1 = {0x01},
+ };
+- struct reftable_write_options opts = {0};
++ struct reftable_write_options opts = { 0 };
+ struct reftable_stack *st;
+ struct strbuf table_path = STRBUF_INIT;
+ char *dir = get_tmp_dir(__LINE__);
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_update_index_check(void)
err = reftable_new_stack(&st, dir, &opts);
check(!err);
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_lock_failure(void)
static void t_reftable_stack_add(void)
{
- int i = 0;
-- int err = 0;
-+ int err;
+ int err = 0;
struct reftable_write_options opts = {
.exact_log_message = 1,
- .default_permissions = 0660,
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add(void)
};
struct reftable_stack *st = NULL;
char *dir = get_tmp_dir(__LINE__);
- struct reftable_ref_record refs[2] = { { NULL } };
- struct reftable_log_record logs[2] = { { NULL } };
-+ struct reftable_ref_record refs[2] = { { 0 } };
-+ struct reftable_log_record logs[2] = { { 0 } };
++ struct reftable_ref_record refs[2] = { 0 };
++ struct reftable_log_record logs[2] = { 0 };
struct strbuf path = STRBUF_INIT;
struct stat stat_result;
- int N = ARRAY_SIZE(refs);
-+ size_t N = ARRAY_SIZE(refs), i;
++ size_t i, N = ARRAY_SIZE(refs);
err = reftable_new_stack(&st, dir, &opts);
check(!err);
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add(void)
int err = reftable_stack_read_log(st, refs[i].refname, &dest);
check(!err);
check(reftable_log_record_equal(&dest, logs + i,
-@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add(void)
-
- static void t_reftable_stack_log_normalize(void)
- {
-- int err = 0;
-+ int err;
- struct reftable_write_options opts = {
- 0,
- };
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_log_normalize(void)
check(!err);
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_log_normalize(void
- int N = ARRAY_SIZE(refs);
- struct reftable_ref_record dest = { NULL };
- struct reftable_log_record log_dest = { NULL };
-+ struct reftable_ref_record refs[2] = { { 0 } };
-+ struct reftable_log_record logs[2] = { { 0 } };
-+ size_t N = ARRAY_SIZE(refs), i;
++ struct reftable_ref_record refs[2] = { 0 };
++ struct reftable_log_record logs[2] = { 0 };
++ size_t i, N = ARRAY_SIZE(refs);
+ struct reftable_ref_record dest = { 0 };
+ struct reftable_log_record log_dest = { 0 };
@@ t/unit-tests/t-reftable-stack.c: static void t_reflog_expire(void)
- struct reftable_log_record logs[20] = { { NULL } };
- int N = ARRAY_SIZE(logs) - 1;
- int i = 0;
-+ struct reftable_log_record logs[20] = { { 0 } };
-+ size_t N = ARRAY_SIZE(logs) - 1, i;
++ struct reftable_log_record logs[20] = { 0 };
++ size_t i, N = ARRAY_SIZE(logs) - 1;
int err;
struct reftable_log_expiry_config expiry = {
.time = 10,
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_auto_compaction(vo
- int err, i;
- int N = 100;
+ int err;
-+ size_t N = 100, i;
++ size_t i, N = 100;
err = reftable_new_stack(&st, dir, &opts);
check(!err);
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add_performs_auto_
check(!err);
/*
-@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_compaction_concurrent(void)
- struct reftable_write_options opts = { 0 };
- struct reftable_stack *st1 = NULL, *st2 = NULL;
- char *dir = get_tmp_dir(__LINE__);
-- int err, i;
-- int N = 3;
-+ int err;
-+ size_t N = 3, i;
-
- err = reftable_new_stack(&st1, dir, &opts);
- check(!err);
-@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_compaction_concurrent(void)
- .value_type = REFTABLE_REF_SYMREF,
- .value.symref = (char *) "master",
- };
-- snprintf(name, sizeof(name), "branch%04d", i);
-+ snprintf(name, sizeof(name), "branch%04"PRIuMAX, (uintmax_t)i);
-
-- err = reftable_stack_add(st1, &write_test_ref, &ref);
-+ err = reftable_stack_add(st1, write_test_ref, &ref);
- check(!err);
- }
-
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_compaction_concurrent(void)
static void unclean_stack_close(struct reftable_stack *st)
{
/* break abstraction boundary to simulate unclean shutdown. */
- int i = 0;
- for (; i < st->readers_len; i++) {
-+ for (size_t i = 0; i < st->readers_len; i++)
++ for (int i = 0; i < st->readers_len; i++)
reftable_reader_free(st->readers[i]);
- }
st->readers_len = 0;
FREE_AND_NULL(st->readers);
}
-@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_compaction_concurrent_clean(void)
- struct reftable_write_options opts = { 0 };
- struct reftable_stack *st1 = NULL, *st2 = NULL, *st3 = NULL;
- char *dir = get_tmp_dir(__LINE__);
-- int err, i;
-- int N = 3;
-+ int err;
-+ size_t N = 3, i;
-
- err = reftable_new_stack(&st1, dir, &opts);
- check(!err);
-@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_compaction_concurrent_clean(void)
- .value_type = REFTABLE_REF_SYMREF,
- .value.symref = (char *) "master",
- };
-- snprintf(name, sizeof(name), "branch%04d", i);
-+ snprintf(name, sizeof(name), "branch%04"PRIuMAX, (uintmax_t)i);
-
-- err = reftable_stack_add(st1, &write_test_ref, &ref);
-+ err = reftable_stack_add(st1, write_test_ref, &ref);
- check(!err);
- }
- 3: 4e769b826f ! 169: 792a7b1e62 t-reftable-stack: use Git's tempfile API instead of mkstemp()
@@ Metadata
## Commit message ##
t-reftable-stack: use Git's tempfile API instead of mkstemp()
- Git's tempfile API defined by $GIT_DIR/tempfile.{c, h} provides
+ Git's tempfile API defined by $GIT_DIR/tempfile.{c,h} provides
a unified interface for tempfile operations. Since reftable/stack.c
uses this API for all its tempfile needs instead of raw functions
like mkstemp(), make the ported stack test strictly use Git's
4: 8277e711af ! 170: d771dc74af t-reftable-stack: use reftable_ref_record_equal() to compare ref records
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add_one(void)
printf("testing print functionality:\n");
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_transaction_api(void)
-
err = reftable_stack_read_ref(st, ref.refname, &dest);
check(!err);
-- check_int(REFTABLE_REF_SYMREF, ==, dest.value_type);
+ check_int(REFTABLE_REF_SYMREF, ==, dest.value_type);
- check_str("master", dest.value.symref);
+ check(reftable_ref_record_equal(&ref, &dest, GIT_SHA1_RAWSZ)); 5: 9528f910ce ! 171: c38a96cd4e t-reftable-stack: add test for non-default compaction factor
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_auto_compaction(vo
} check_int(reftable_stack_compaction_stats(st)->entries_written, <,
-- (uint64_t)(N * fastlog2(N)));
-+ (uint64_t)(N * fastlogN(N, 2)));
+- (uint64_t)(N * fastlog2(N)));
++ (uint64_t)(N * fastlogN(N, 2)));
+
+ reftable_stack_destroy(st);
+ clear_dir(dir);
@@ t/unit-tests/t-reftable-stack.c: int cmd_main(int argc, const char *argv[])
TEST(t_reftable_stack_auto_compaction(), "stack must form geometric sequence after compaction");
+ TEST(t_reftable_stack_auto_compaction_factor(), "auto-compaction with non-default geometric factor");
TEST(t_reftable_stack_auto_compaction_fails_gracefully(), "failure on auto-compaction");
+ TEST(t_reftable_stack_auto_compaction_with_locked_tables(), "auto compaction with locked tables");
TEST(t_reftable_stack_compaction_concurrent(), "compaction with concurrent stack");
- TEST(t_reftable_stack_compaction_concurrent_clean(), "compaction with unclean stack shutdown");
6: bd997a27d7 ! 172: ea5267ca22 t-reftable-stack: add test for stack iterators
@@ Commit message
t-reftable-stack: add test for stack iterators
reftable_stack_init_ref_iterator and reftable_stack_init_log_iterator
- as defined by reftable/stack.{c, h} initialize a stack iterator to
+ as defined by reftable/stack.{c,h} initialize a stack iterator to
iterate over the ref and log records in a reftable stack respectively.
Since these functions are not exercised by any of the existing tests,
add a test for them.
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add(void)
+ struct reftable_write_options opts = { 0 };
+ struct reftable_stack *st = NULL;
+ char *dir = get_tmp_dir(__LINE__);
-+ struct reftable_ref_record refs[10] = { { 0 } };
-+ struct reftable_log_record logs[10] = { { 0 } };
++ struct reftable_ref_record refs[10] = { 0 };
++ struct reftable_log_record logs[10] = { 0 };
+ struct reftable_iterator it = { 0 };
+ size_t N = ARRAY_SIZE(refs), i;
+ int err;
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add(void)
+ check(!err);
+
+ for (i = 0; i < N; i++) {
-+ char buf[20];
-+ xsnprintf(buf, sizeof(buf), "branch%02"PRIuMAX, (uintmax_t)i);
-+ refs[i].refname = xstrdup(buf);
++ refs[i].refname = xstrfmt("branch%02"PRIuMAX, (uintmax_t)i);
+ refs[i].update_index = i + 1;
+ refs[i].value_type = REFTABLE_REF_VAL1;
+ set_test_hash(refs[i].value.val1, i);
+
-+ logs[i].refname = xstrdup(buf);
++ logs[i].refname = xstrfmt("branch%02"PRIuMAX, (uintmax_t)i);
+ logs[i].update_index = i + 1;
+ logs[i].value_type = REFTABLE_LOG_UPDATE;
+ logs[i].value.update.email = xstrdup("johndoe@invalid");
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add(void)
+
static void t_reftable_stack_log_normalize(void)
{
- int err;
+ int err = 0;
@@ t/unit-tests/t-reftable-stack.c: int cmd_main(int argc, const char *argv[])
- TEST(t_reftable_stack_compaction_concurrent(), "compaction with concurrent stack");
TEST(t_reftable_stack_compaction_concurrent_clean(), "compaction with unclean stack shutdown");
+ TEST(t_reftable_stack_compaction_with_locked_tables(), "compaction with locked tables");
TEST(t_reftable_stack_hash_id(), "read stack with wrong hash ID");
+ TEST(t_reftable_stack_iterator(), "log and ref iterator for reftable stack");
TEST(t_reftable_stack_lock_failure(), "stack addition with lockfile failure");PATCH v2 1/6 t: move reftable/stack_test.c to the unit testing framework
reftable/stack_test.c exercises the functions defined in reftable/stack.{c, h}. Migrate reftable/stack_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework and renaming the tests to be in-line with unit-tests' standards.
Since some of the tests use set_test_hash() defined by reftable/test_framework.{c, h} but these files are not '#included' in the test file, copy this function in the ported test file.
--- Makefile | 2 +- reftable/reftable-tests.h | 1 - t/helper/test-reftable.c | 1 - .../unit-tests/t-reftable-stack.c | 374 +++++++++--------- 4 files changed, 187 insertions(+), 191 deletions(-) rename reftable/stack_test.c => t/unit-tests/t-reftable-stack.c (77%)
Makefile +1 -1
@@ -1344,6 +1344,7 @@ UNIT_TEST_PROGRAMS += t-reftable-basics UNIT_TEST_PROGRAMS += t-reftable-merged UNIT_TEST_PROGRAMS += t-reftable-pq UNIT_TEST_PROGRAMS += t-reftable-record +UNIT_TEST_PROGRAMS += t-reftable-stack UNIT_TEST_PROGRAMS += t-reftable-tree UNIT_TEST_PROGRAMS += t-strbuf UNIT_TEST_PROGRAMS += t-strcmp-offset
@@ -2685,7 +2686,6 @@ REFTABLE_OBJS += reftable/writer.o REFTABLE_TEST_OBJS += reftable/block_test.o REFTABLE_TEST_OBJS += reftable/dump.o REFTABLE_TEST_OBJS += reftable/readwrite_test.o -REFTABLE_TEST_OBJS += reftable/stack_test.o REFTABLE_TEST_OBJS += reftable/test_framework.o TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS)) $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS))
reftable/reftable-tests.h +0 -1
@@ -13,7 +13,6 @@ int basics_test_main(int argc, const char **argv);int block_test_main (int argc,const char **argv);int record_test_main (int argc,const char **argv);int readwrite_test_main (int argc,const char **argv); -int stack_test_main (int argc,const char **argv);int reftable_dump_main (int argc,char *const *argv);#endif
t/helper/test-reftable.c +0 -1
@@ -7,7 +7,6 @@ int cmd__reftable(int argc, const char **argv)/* test from simple to complex. */ block_test_main (argc, argv);readwrite_test_main (argc, argv); -stack_test_main (argc, argv);return 0 ; }
t/unit-tests/t-reftable-stack.c renamed from reftable/stack_test.c +186 -188
897 lines not on this page · raw message
Signature · 1 line
2.45.GIT
PATCH v2 2/6 t: harmonize t-reftable-stack.c with coding guidelines
In reply to Chandra PratapHarmonize 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.
--- t/unit-tests/t-reftable-stack.c | 114 +++++++++++++++----------------- 1 file changed, 54 insertions(+), 60 deletions(-)
t/unit-tests/t-reftable-stack.c +54 -60
@@ -81,7 +81,6 @@ static void t_read_file(void)int n, err;char **names =NULL ;const char *want[] = {"line1" ,"line2" ,"line3" }; -int i =0 ; check_int(fd, >, 0); n =write_in_full (fd, out,strlen (out));
@@ -92,9 +91,8 @@ static void t_read_file(void) err =read_lines (fn, &names);check (!err); -for (i =0 ; names[i]; i++) { +for (size_t i =0 ; names[i]; i++)check_str (want[i], names[i]); - }free_names (names); (void )remove (fn); }
@@ -122,7 +120,7 @@ static void write_n_ref_tables(struct reftable_stack *st, .value_type = REFTABLE_REF_VAL1, }; -strbuf_addf (&buf,"refs/heads/branch-%04u" , (unsigned ) i); +strbuf_addf (&buf,"refs/heads/branch-%04" PRIuMAX, (uintmax_t )i); ref.refname = buf.buf;set_test_hash (ref.value.val1, i);
@@ -163,12 +161,12 @@ static void t_reftable_stack_add_one(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 };struct stat stat_result = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err); err =reftable_stack_read_ref (st, ref.refname, &dest);
@@ -240,16 +238,16 @@ static void t_reftable_stack_uptodate(void) err =reftable_new_stack (&st2, dir, &opts);check (!err); - err =reftable_stack_add (st1, &write_test_ref, &ref1); + err =reftable_stack_add (st1, write_test_ref, &ref1);check (!err); - err =reftable_stack_add (st2, &write_test_ref, &ref2); + err =reftable_stack_add (st2, write_test_ref, &ref2); check_int(err, ==, REFTABLE_OUTDATED_ERROR); err =reftable_stack_reload (st2);check (!err); - err =reftable_stack_add (st2, &write_test_ref, &ref2); + err =reftable_stack_add (st2, write_test_ref, &ref2);check (!err);reftable_stack_destroy (st1);reftable_stack_destroy (st2);
@@ -270,7 +268,7 @@ static void t_reftable_stack_transaction_api(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -280,7 +278,7 @@ static void t_reftable_stack_transaction_api(void) err =reftable_stack_new_addition (&add, st);check (!err); - err =reftable_addition_add (add, &write_test_ref, &ref); + err =reftable_addition_add (add, write_test_ref, &ref);check (!err); err =reftable_addition_commit (add);
@@ -304,12 +302,13 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void)struct reftable_write_options opts = {0 };struct reftable_addition *add =NULL ;struct reftable_stack *st =NULL ; -int i, n =20 , err; +size_t n =20 ; +int err; err =reftable_new_stack (&st, dir, &opts);check (!err); -for (i =0 ; i <= n; i++) { +for (size_t i =0 ; i <= n; i++) {struct reftable_ref_record ref = { .update_index =reftable_stack_next_update_index (st), .value_type =REFTABLE_REF_SYMREF ,
@@ -317,7 +316,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void) };char name[100 ]; -snprintf (name,sizeof (name),"branch%04d" , i); +snprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); ref.refname = name;/*
@@ -330,7 +329,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void) err =reftable_stack_new_addition (&add, st);check (!err); - err =reftable_addition_add (add, &write_test_ref, &ref); + err =reftable_addition_add (add, write_test_ref, &ref);check (!err); err =reftable_addition_commit (add);
@@ -361,7 +360,7 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void) .value_type = REFTABLE_REF_VAL1, .value.val1 = {0x01}, }; -struct reftable_write_options opts = {0 }; +struct reftable_write_options opts = {0 };struct reftable_stack *st;struct strbuf table_path =STRBUF_INIT ;char *dir =get_tmp_dir (__LINE__);
@@ -423,10 +422,10 @@ static void t_reftable_stack_update_index_check(void) err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref1); + err =reftable_stack_add (st, write_test_ref, &ref1);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref2); + err =reftable_stack_add (st, write_test_ref, &ref2); check_int(err, ==, REFTABLE_API_ERROR);reftable_stack_destroy (st);clear_dir (dir);
@@ -442,7 +441,7 @@ static void t_reftable_stack_lock_failure(void) err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =-1 ; i !=REFTABLE_EMPTY_TABLE_ERROR ; i--) { - err =reftable_stack_add (st, &write_error, &i); + err =reftable_stack_add (st, write_error, &i); check_int(err, ==, i); }
@@ -452,7 +451,6 @@ static void t_reftable_stack_lock_failure(void)static void t_reftable_stack_add (void ) { -int i =0 ;int err =0 ;struct reftable_write_options opts = { .exact_log_message =1 ,
@@ -461,18 +459,18 @@ static void t_reftable_stack_add(void) };struct reftable_stack *st =NULL ;char *dir =get_tmp_dir (__LINE__); -struct reftable_ref_record refs[2 ] = { {NULL } }; -struct reftable_log_record logs[2 ] = { {NULL } }; +struct reftable_ref_record refs[2 ] = {0 }; +struct reftable_log_record logs[2 ] = {0 };struct strbuf path =STRBUF_INIT ;struct stat stat_result; -int N =ARRAY_SIZE (refs); +size_t i,N =ARRAY_SIZE (refs); err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =0 ; i <N ; i++) {char buf[256 ]; -snprintf (buf,sizeof (buf),"branch%02d" , i); +snprintf (buf,sizeof (buf),"branch%02" PRIuMAX, (uintmax_t )i); refs[i].refname =xstrdup (buf); refs[i].update_index = i +1 ; refs[i].value_type =REFTABLE_REF_VAL1 ;
@@ -486,7 +484,7 @@ static void t_reftable_stack_add(void) }for (i =0 ; i <N ; i++) { -int err =reftable_stack_add (st, &write_test_ref, &refs[i]); +int err =reftable_stack_add (st, write_test_ref, &refs[i]);check (!err); }
@@ -495,7 +493,7 @@ static void t_reftable_stack_add(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -503,7 +501,7 @@ static void t_reftable_stack_add(void)check (!err);for (i =0 ; i <N ; i++) { -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 };int err =reftable_stack_read_ref (st, refs[i].refname, &dest);check (!err);
@@ -513,7 +511,7 @@ static void t_reftable_stack_add(void) }for (i =0 ; i <N ; i++) { -struct reftable_log_record dest = {NULL }; +struct reftable_log_record dest = {0 };int err =reftable_stack_read_log (st, refs[i].refname, &dest);check (!err); check(reftable_log_record_equal(&dest, logs + i,
@@ -581,11 +579,11 @@ static void t_reftable_stack_log_normalize(void)check (!err); input.value.update.message = (char *)"one\ntwo" ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg); check_int(err, ==, REFTABLE_API_ERROR); input.value.update.message = (char *)"one" ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg);check (!err); err =reftable_stack_read_log (st, input.refname, &dest);
@@ -594,7 +592,7 @@ static void t_reftable_stack_log_normalize(void) input.value.update.message = (char *)"two\n" ; arg.update_index =2 ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg);check (!err); err =reftable_stack_read_log (st, input.refname, &dest);check (!err);
@@ -608,16 +606,15 @@ static void t_reftable_stack_log_normalize(void)static void t_reftable_stack_tombstone (void ) { -int i =0 ;char *dir =get_tmp_dir (__LINE__);struct reftable_write_options opts = {0 };struct reftable_stack *st =NULL ;int err; -struct reftable_ref_record refs[2 ] = { {NULL } }; -struct reftable_log_record logs[2 ] = { {NULL } }; -int N =ARRAY_SIZE (refs); -struct reftable_ref_record dest = {NULL }; -struct reftable_log_record log_dest = {NULL }; +struct reftable_ref_record refs[2 ] = {0 }; +struct reftable_log_record logs[2 ] = {0 }; +size_t i,N =ARRAY_SIZE (refs); +struct reftable_ref_record dest = {0 }; +struct reftable_log_record log_dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -643,7 +640,7 @@ static void t_reftable_stack_tombstone(void) } }for (i =0 ; i <N ; i++) { -int err =reftable_stack_add (st, &write_test_ref, &refs[i]); +int err =reftable_stack_add (st, write_test_ref, &refs[i]);check (!err); }
@@ -652,7 +649,7 @@ static void t_reftable_stack_tombstone(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -701,12 +698,12 @@ static void t_reftable_stack_hash_id(void)struct reftable_stack *st32 =NULL ;struct reftable_write_options opts_default = {0 };struct reftable_stack *st_default =NULL ; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err);/* can't read it with the wrong hash ID. */
@@ -749,21 +746,20 @@ static void t_reflog_expire(void)char *dir =get_tmp_dir (__LINE__);struct reftable_write_options opts = {0 };struct reftable_stack *st =NULL ; -struct reftable_log_record logs[20 ] = { {NULL } }; -int N =ARRAY_SIZE (logs) -1 ; -int i =0 ; +struct reftable_log_record logs[20 ] = {0 }; +size_t i,N =ARRAY_SIZE (logs) -1 ;int err;struct reftable_log_expiry_config expiry = { .time =10 , }; -struct reftable_log_record log = {NULL }; +struct reftable_log_record log = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =1 ; i <=N ; i++) {char buf[256 ]; -snprintf (buf,sizeof (buf),"branch%02d" , i); +snprintf (buf,sizeof (buf),"branch%02" PRIuMAX, (uintmax_t )i); logs[i].refname =xstrdup (buf); logs[i].update_index = i;
@@ -778,7 +774,7 @@ static void t_reflog_expire(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -806,9 +802,8 @@ static void t_reflog_expire(void)/* cleanup */ reftable_stack_destroy (st); -for (i =0 ; i <=N ; i++) { +for (i =0 ; i <=N ; i++)reftable_log_record_release (&logs[i]); - }clear_dir (dir);reftable_log_record_release (&log); }
@@ -830,7 +825,7 @@ static void t_empty_add(void) err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_nothing,NULL ); + err =reftable_stack_add (st, write_nothing,NULL );check (!err); err =reftable_new_stack (&st2, dir, &opts);
@@ -857,8 +852,8 @@ static void t_reftable_stack_auto_compaction(void) };struct reftable_stack *st =NULL ;char *dir =get_tmp_dir (__LINE__); -int err, i; -int N =100 ; +int err; +size_t i,N =100 ; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -871,9 +866,9 @@ static void t_reftable_stack_auto_compaction(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -snprintf (name,sizeof (name),"branch%04d" , i); +snprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err); err =reftable_stack_auto_compact (st);
@@ -935,7 +930,8 @@ static void t_reftable_stack_add_performs_auto_compaction(void)struct reftable_stack *st =NULL ;struct strbuf refname =STRBUF_INIT ;char *dir =get_tmp_dir (__LINE__); -int err, i, n =20 ; +int err; +size_t i, n =20 ; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -955,10 +951,10 @@ static void t_reftable_stack_add_performs_auto_compaction(void) st->opts.disable_auto_compact = i != n;strbuf_reset (&refname); -strbuf_addf (&refname,"branch-%04d" , i); +strbuf_addf (&refname,"branch-%04" PRIuMAX, (uintmax_t )i); ref.refname = refname.buf; - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err);/*
@@ -1039,10 +1035,8 @@ static void t_reftable_stack_compaction_concurrent(void)static void unclean_stack_close (struct reftable_stack *st) {/* break abstraction boundary to simulate unclean shutdown. */ -int i =0 ; -for (; i < st->readers_len; i++) { +for (int i =0 ; i < st->readers_len; i++)reftable_reader_free (st->readers[i]); - } st->readers_len =0 ;FREE_AND_NULL (st->readers); }
Signature · 1 line
2.45.GIT
Re: [PATCH v2 2/6] t: harmonize t-reftable-stack.c with coding guidelines
t/unit-tests/t-reftable-stack.c static void t_reftable_stack_compaction_concurrent(void) · line 1038
Quoted 7 lines · Chandra Pratap
t/unit-tests/t-reftable-stack.c +1
-2@@ -1039,10 +1035,8 @@ static void t_reftable_stack_compaction_concurrent(void)static void unclean_stack_close (struct reftable_stack *st) {/* break abstraction boundary to simulate unclean shutdown. */ -int i =0 ; -for (; i < st->readers_len; i++) { +for (int i =0 ; i < st->readers_len; i++)
Nit: this should likely be size_t i. Not worth a reroll on its own though.
Patrick
PATCH v2 3/6 t-reftable-stack: use Git's tempfile API instead of mkstemp()
In reply to Chandra PratapGit's tempfile API defined by $GIT_DIR/tempfile.{c,h} provides a unified interface for tempfile operations. Since reftable/stack.c uses this API for all its tempfile needs instead of raw functions like mkstemp(), make the ported stack test strictly use Git's tempfile API as well.
--- t/unit-tests/t-reftable-stack.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
t/unit-tests/t-reftable-stack.c +3 -1
@@ -76,7 +76,8 @@ static char *get_tmp_dir(int linenumber)static void t_read_file (void ) {char *fn =get_tmp_template (__LINE__); -int fd =mkstemp (fn); +struct tempfile *tmp =mks_tempfile (fn); +int fd =get_tempfile_fd (tmp);char out[1024 ] ="line1\n\nline2\nline3" ;int n, err;char **names =NULL ;
@@ -95,6 +96,7 @@ static void t_read_file(void)check_str (want[i], names[i]);free_names (names); (void )remove (fn); +delete_tempfile (&tmp); }static int write_test_ref (struct reftable_writer *wr,void *arg)
Signature · 1 line
2.45.GIT
PATCH v2 4/6 t-reftable-stack: use reftable_ref_record_equal() to compare ref records
In reply to Chandra PratapIn the current stack tests, ref records are compared for equality by sometimes using the dedicated function for ref-record comparison, reftable_ref_record_equal(), and sometimes by explicity comparing contents of the ref records.
Replace the latter instances of ref-record comparison with the former to maintain uniformity throughout the test file.
--- t/unit-tests/t-reftable-stack.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
t/unit-tests/t-reftable-stack.c +2 -2
@@ -173,7 +173,7 @@ static void t_reftable_stack_add_one(void) err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ )); check_int(st->readers_len, >, 0);printf ("testing print functionality:\n" );
@@ -291,7 +291,7 @@ static void t_reftable_stack_transaction_api(void) err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); check_int(REFTABLE_REF_SYMREF, ==, dest.value_type); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ ));reftable_ref_record_release (&dest);reftable_stack_destroy (st);
Signature · 1 line
2.45.GIT
PATCH v2 5/6 t-reftable-stack: add test for non-default compaction factor
In reply to Chandra PratapIn a recent codebase update (commit ae8e378430, merge branch 'ps/reftable-write-options', 2024/05/13) the geometric factor used in auto-compaction of reftable tables was made configurable. Add a test to verify the functionality introduced by this update.
--- t/unit-tests/t-reftable-stack.c | 41 +++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-)
t/unit-tests/t-reftable-stack.c +37 -4
@@ -837,12 +837,12 @@ static void t_empty_add(void)reftable_stack_destroy (st2); } -static int fastlog2 (uint64_t sz) +static int fastlogN (uint64_t sz,uint64_t N ) {int l =0 ;if (sz ==0 )return 0 ; -for (; sz; sz /=2 ) +for (; sz; sz /=N ) l++;return l -1 ; }
@@ -875,11 +875,43 @@ static void t_reftable_stack_auto_compaction(void) err =reftable_stack_auto_compact (st);check (!err); -check (i <3 || st->merged->stack_len <2 *fastlog2 (i)); +check (i <2 || st->merged->stack_len <2 *fastlogN (i,2 )); } check_int(reftable_stack_compaction_stats(st)->entries_written, <, - (uint64_t )(N *fastlog2 (N ))); + (uint64_t )(N *fastlogN (N ,2 ))); + +reftable_stack_destroy (st); +clear_dir (dir); +} + +static void t_reftable_stack_auto_compaction_factor (void ) +{ +struct reftable_write_options opts = { + .auto_compaction_factor =5 , + }; +struct reftable_stack *st =NULL ; +char *dir =get_tmp_dir (__LINE__); +int err; +size_t N =100 ; + + err =reftable_new_stack (&st, dir, &opts); +check (!err); + +for (size_t i =0 ; i <N ; i++) { +char name[20 ]; +struct reftable_ref_record ref = { + .refname = name, + .update_index =reftable_stack_next_update_index (st), + .value_type =REFTABLE_REF_VAL1 , + }; +xsnprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); + + err =reftable_stack_add (st, &write_test_ref, &ref); +check (!err); + +check (i <5 || st->merged->stack_len <5 *fastlogN (i,5 )); + }reftable_stack_destroy (st);clear_dir (dir);
@@ -1086,6 +1118,7 @@ int cmd_main(int argc, const char *argv[])TEST (t_reftable_stack_add_one (),"add a single ref record to stack" );TEST (t_reftable_stack_add_performs_auto_compaction (),"addition to stack triggers auto-compaction" );TEST (t_reftable_stack_auto_compaction (),"stack must form geometric sequence after compaction" ); +TEST (t_reftable_stack_auto_compaction_factor (),"auto-compaction with non-default geometric factor" );TEST (t_reftable_stack_auto_compaction_fails_gracefully (),"failure on auto-compaction" );TEST (t_reftable_stack_auto_compaction_with_locked_tables (),"auto compaction with locked tables" );TEST (t_reftable_stack_compaction_concurrent (),"compaction with concurrent stack" );
Signature · 1 line
2.45.GIT
PATCH v2 6/6 t-reftable-stack: add test for stack iterators
In reply to Chandra Pratapreftable_stack_init_ref_iterator and reftable_stack_init_log_iterator as defined by reftable/stack.{c,h} initialize a stack iterator to iterate over the ref and log records in a reftable stack respectively. Since these functions are not exercised by any of the existing tests, add a test for them.
--- t/unit-tests/t-reftable-stack.c | 80 +++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+)
t/unit-tests/t-reftable-stack.c +80 -0
@@ -550,6 +550,85 @@ static void t_reftable_stack_add(void)clear_dir (dir); } +static void t_reftable_stack_iterator (void ) +{ +struct reftable_write_options opts = {0 }; +struct reftable_stack *st =NULL ; +char *dir =get_tmp_dir (__LINE__); +struct reftable_ref_record refs[10 ] = {0 }; +struct reftable_log_record logs[10 ] = {0 }; +struct reftable_iterator it = {0 }; +size_t N =ARRAY_SIZE (refs), i; +int err; + + err =reftable_new_stack (&st, dir, &opts); +check (!err); + +for (i =0 ; i <N ; i++) { + refs[i].refname =xstrfmt ("branch%02" PRIuMAX, (uintmax_t )i); + refs[i].update_index = i +1 ; + refs[i].value_type =REFTABLE_REF_VAL1 ; +set_test_hash (refs[i].value.val1, i); + + logs[i].refname =xstrfmt ("branch%02" PRIuMAX, (uintmax_t )i); + logs[i].update_index = i +1 ; + logs[i].value_type =REFTABLE_LOG_UPDATE ; + logs[i].value.update.email =xstrdup ("johndoe@invalid" ); + logs[i].value.update.message =xstrdup ("commit\n" ); +set_test_hash (logs[i].value.update.new_hash, i); + } + +for (i =0 ; i <N ; i++) { + err =reftable_stack_add (st, &write_test_ref, &refs[i]); +check (!err); + } + +for (i =0 ; i <N ; i++) { +struct write_log_arg arg = { + .log = &logs[i], + .update_index =reftable_stack_next_update_index (st), + }; + err =reftable_stack_add (st, &write_test_log, &arg); +check (!err); + } + +reftable_stack_init_ref_iterator (st, &it); +reftable_iterator_seek_ref (&it, refs[0 ].refname); +for (i =0 ; ; i++) { +struct reftable_ref_record ref = {0 }; + err =reftable_iterator_next_ref (&it, &ref); +if (err >0 ) +break ; +check (!err); +check (reftable_ref_record_equal (&ref, &refs[i],GIT_SHA1_RAWSZ )); +reftable_ref_record_release (&ref); + } + check_int(i, ==, N); + +reftable_iterator_destroy (&it); + +reftable_stack_init_log_iterator (st, &it); +reftable_iterator_seek_log (&it, logs[0 ].refname); +for (i =0 ; ; i++) { +struct reftable_log_record log = {0 }; + err =reftable_iterator_next_log (&it, &log); +if (err >0 ) +break ; +check (!err); +check (reftable_log_record_equal (&log, &logs[i],GIT_SHA1_RAWSZ )); +reftable_log_record_release (&log); + } + check_int(i, ==, N); + +reftable_stack_destroy (st); +reftable_iterator_destroy (&it); +for (i =0 ; i <N ; i++) { +reftable_ref_record_release (&refs[i]); +reftable_log_record_release (&logs[i]); + } +clear_dir (dir); +} +static void t_reftable_stack_log_normalize (void ) {int err =0 ;
@@ -1125,6 +1204,7 @@ int cmd_main(int argc, const char *argv[])TEST (t_reftable_stack_compaction_concurrent_clean (),"compaction with unclean stack shutdown" );TEST (t_reftable_stack_compaction_with_locked_tables (),"compaction with locked tables" );TEST (t_reftable_stack_hash_id (),"read stack with wrong hash ID" ); +TEST (t_reftable_stack_iterator (),"log and ref iterator for reftable stack" );TEST (t_reftable_stack_lock_failure (),"stack addition with lockfile failure" );TEST (t_reftable_stack_log_normalize (),"log messages should be normalized" );TEST (t_reftable_stack_tombstone (),"'tombstone' refs in stack" );
Signature · 1 line
2.45.GIT
Re: [PATCH v2 6/6] t-reftable-stack: add test for stack iterators
Quoted 30 lines · Chandra Pratap
t/unit-tests/t-reftable-stack.c +22
-0@@ -550,6 +550,85 @@ static void t_reftable_stack_add(void)clear_dir (dir); } +static void t_reftable_stack_iterator (void ) +{ +struct reftable_write_options opts = {0 }; +struct reftable_stack *st =NULL ; +char *dir =get_tmp_dir (__LINE__); +struct reftable_ref_record refs[10 ] = {0 }; +struct reftable_log_record logs[10 ] = {0 }; +struct reftable_iterator it = {0 }; +size_t N =ARRAY_SIZE (refs), i; +int err; + + err =reftable_new_stack (&st, dir, &opts); +check (!err); + +for (i =0 ; i <N ; i++) { + refs[i].refname =xstrfmt ("branch%02" PRIuMAX, (uintmax_t )i); + refs[i].update_index = i +1 ; + refs[i].value_type =REFTABLE_REF_VAL1 ; +set_test_hash (refs[i].value.val1, i); + + logs[i].refname =xstrfmt ("branch%02" PRIuMAX, (uintmax_t )i); + logs[i].update_index = i +1 ;
t/unit-tests/t-reftable-stack.c static void t_reftable_stack_add(void) · line 584
Quoted 10 lines
t/unit-tests/t-reftable-stack.c +10
-0+ logs[i].value_type =REFTABLE_LOG_UPDATE ; + logs[i].value.update.email =xstrdup ("johndoe@invalid" ); + logs[i].value.update.message =xstrdup ("commit\n" ); +set_test_hash (logs[i].value.update.new_hash, i); + } + +for (i =0 ; i <N ; i++) { + err =reftable_stack_add (st, &write_test_ref, &refs[i]); +check (!err); + }
Nit: you re-add the ampersands before the function pointers here, which basically reintroduces the pattern you got rid of in the second patch.
Patrick
v3 cover t: port reftable/stack_test.c to the unit testing framework
In reply to Chandra PratapThe reftable library comes with self tests, which are exercised as part of the usual end-to-end tests and are designed to observe the end-user visible effects of Git commands. What it exercises, however, is a better match for the unit-testing framework, merged at 8bf6fbd0 (Merge branch 'js/doc-unit-tests', 2023-12-09), which is designed to observe how low level implementation details, at the level of sequences of individual function calls, behave.
Hence, port reftable/stack_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.
---
Changes in v2:
- Use 'size_t' as array index instead of 'int' in a test that is modified in patch 2.
- Fix a coding style violation in the newly introduced test in patch 6.
CI/PR: https://github.com/gitgitgadget/git/pull/1762
Chandra Pratap(6):
t: move reftable/stack_test.c to the unit testing framework
t: harmonize t-reftable-stack.c with coding guidelines
t-reftable-stack: use Git's tempfile API instead of mkstemp()
t-reftable-stack: use reftable_ref_record_equal() to compare ref records
t-reftable-stack: add test for non-default compaction factor
t-reftable-stack: add test for stack iterators
Makefile | 2 +- reftable/reftable-tests.h | 1 - t/helper/test-reftable.c | 1 - reftable/stack_test.c => t/unit-tests/t-reftable-stack.c | 611 +++++++++++++++++++-------------- 4 files changed, 360 insertions(+), 255 deletions(-)
Range-diff against v2:
1: e172ceefd7 ! 1: 838ccc63a7 t: harmonize t-reftable-stack.c with coding guidelines
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_compaction_concurr
/* break abstraction boundary to simulate unclean shutdown. */
- int i = 0;
- for (; i < st->readers_len; i++) {
-+ for (int i = 0; i < st->readers_len; i++)
++ for (size_t i = 0; i < st->readers_len; i++)
reftable_reader_free(st->readers[i]);
- }
st->readers_len = 0;
2: 73b94aaa6a = 2: 2a151c299c t-reftable-stack: use Git's tempfile API instead of mkstemp()
3: b6f0363656 = 3: fb5073da2c t-reftable-stack: use reftable_ref_record_equal() to compare ref records
4: 35c09e6054 = 4: 028fa6f70b t-reftable-stack: add test for non-default compaction factor
5: 3163c2af7d ! 5: fa0d358e65 t-reftable-stack: add test for stack iterators
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add(void)
+ }
+
+ for (i = 0; i < N; i++) {
-+ err = reftable_stack_add(st, &write_test_ref, &refs[i]);
++ err = reftable_stack_add(st, write_test_ref, &refs[i]);
+ check(!err);
+ }
+
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add(void)
+ .log = &logs[i],
+ .update_index = reftable_stack_next_update_index(st),
+ };
-+ err = reftable_stack_add(st, &write_test_log, &arg);
++ err = reftable_stack_add(st, write_test_log, &arg);
+ check(!err);
+ }
+PATCH v3 1/6 t: move reftable/stack_test.c to the unit testing framework
reftable/stack_test.c exercises the functions defined in reftable/stack.{c, h}. Migrate reftable/stack_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework and renaming the tests to be in-line with unit-tests' standards.
Since some of the tests use set_test_hash() defined by reftable/test_framework.{c, h} but these files are not '#included' in the test file, copy this function in the ported test file.
--- Makefile | 2 +- reftable/reftable-tests.h | 1 - t/helper/test-reftable.c | 1 - .../unit-tests/t-reftable-stack.c | 374 +++++++++--------- 4 files changed, 187 insertions(+), 191 deletions(-) rename reftable/stack_test.c => t/unit-tests/t-reftable-stack.c (77%)
Makefile +1 -1
@@ -1344,6 +1344,7 @@ UNIT_TEST_PROGRAMS += t-reftable-basics UNIT_TEST_PROGRAMS += t-reftable-merged UNIT_TEST_PROGRAMS += t-reftable-pq UNIT_TEST_PROGRAMS += t-reftable-record +UNIT_TEST_PROGRAMS += t-reftable-stack UNIT_TEST_PROGRAMS += t-reftable-tree UNIT_TEST_PROGRAMS += t-strbuf UNIT_TEST_PROGRAMS += t-strcmp-offset
@@ -2685,7 +2686,6 @@ REFTABLE_OBJS += reftable/writer.o REFTABLE_TEST_OBJS += reftable/block_test.o REFTABLE_TEST_OBJS += reftable/dump.o REFTABLE_TEST_OBJS += reftable/readwrite_test.o -REFTABLE_TEST_OBJS += reftable/stack_test.o REFTABLE_TEST_OBJS += reftable/test_framework.o TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS)) $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS))
reftable/reftable-tests.h +0 -1
@@ -13,7 +13,6 @@ int basics_test_main(int argc, const char **argv);int block_test_main (int argc,const char **argv);int record_test_main (int argc,const char **argv);int readwrite_test_main (int argc,const char **argv); -int stack_test_main (int argc,const char **argv);int reftable_dump_main (int argc,char *const *argv);#endif
t/helper/test-reftable.c +0 -1
@@ -7,7 +7,6 @@ int cmd__reftable(int argc, const char **argv)/* test from simple to complex. */ block_test_main (argc, argv);readwrite_test_main (argc, argv); -stack_test_main (argc, argv);return 0 ; }
t/unit-tests/t-reftable-stack.c renamed from reftable/stack_test.c +186 -188
897 lines not on this page · raw message
Signature · 1 line
2.45.GIT
PATCH v3 2/6 t: harmonize t-reftable-stack.c with coding guidelines
In reply to Chandra PratapHarmonize 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.
--- t/unit-tests/t-reftable-stack.c | 114 +++++++++++++++----------------- 1 file changed, 54 insertions(+), 60 deletions(-)
t/unit-tests/t-reftable-stack.c +54 -60
@@ -81,7 +81,6 @@ static void t_read_file(void)int n, err;char **names =NULL ;const char *want[] = {"line1" ,"line2" ,"line3" }; -int i =0 ; check_int(fd, >, 0); n =write_in_full (fd, out,strlen (out));
@@ -92,9 +91,8 @@ static void t_read_file(void) err =read_lines (fn, &names);check (!err); -for (i =0 ; names[i]; i++) { +for (size_t i =0 ; names[i]; i++)check_str (want[i], names[i]); - }free_names (names); (void )remove (fn); }
@@ -122,7 +120,7 @@ static void write_n_ref_tables(struct reftable_stack *st, .value_type = REFTABLE_REF_VAL1, }; -strbuf_addf (&buf,"refs/heads/branch-%04u" , (unsigned ) i); +strbuf_addf (&buf,"refs/heads/branch-%04" PRIuMAX, (uintmax_t )i); ref.refname = buf.buf;set_test_hash (ref.value.val1, i);
@@ -163,12 +161,12 @@ static void t_reftable_stack_add_one(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 };struct stat stat_result = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err); err =reftable_stack_read_ref (st, ref.refname, &dest);
@@ -240,16 +238,16 @@ static void t_reftable_stack_uptodate(void) err =reftable_new_stack (&st2, dir, &opts);check (!err); - err =reftable_stack_add (st1, &write_test_ref, &ref1); + err =reftable_stack_add (st1, write_test_ref, &ref1);check (!err); - err =reftable_stack_add (st2, &write_test_ref, &ref2); + err =reftable_stack_add (st2, write_test_ref, &ref2); check_int(err, ==, REFTABLE_OUTDATED_ERROR); err =reftable_stack_reload (st2);check (!err); - err =reftable_stack_add (st2, &write_test_ref, &ref2); + err =reftable_stack_add (st2, write_test_ref, &ref2);check (!err);reftable_stack_destroy (st1);reftable_stack_destroy (st2);
@@ -270,7 +268,7 @@ static void t_reftable_stack_transaction_api(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -280,7 +278,7 @@ static void t_reftable_stack_transaction_api(void) err =reftable_stack_new_addition (&add, st);check (!err); - err =reftable_addition_add (add, &write_test_ref, &ref); + err =reftable_addition_add (add, write_test_ref, &ref);check (!err); err =reftable_addition_commit (add);
@@ -304,12 +302,13 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void)struct reftable_write_options opts = {0 };struct reftable_addition *add =NULL ;struct reftable_stack *st =NULL ; -int i, n =20 , err; +size_t n =20 ; +int err; err =reftable_new_stack (&st, dir, &opts);check (!err); -for (i =0 ; i <= n; i++) { +for (size_t i =0 ; i <= n; i++) {struct reftable_ref_record ref = { .update_index =reftable_stack_next_update_index (st), .value_type =REFTABLE_REF_SYMREF ,
@@ -317,7 +316,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void) };char name[100 ]; -snprintf (name,sizeof (name),"branch%04d" , i); +snprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); ref.refname = name;/*
@@ -330,7 +329,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void) err =reftable_stack_new_addition (&add, st);check (!err); - err =reftable_addition_add (add, &write_test_ref, &ref); + err =reftable_addition_add (add, write_test_ref, &ref);check (!err); err =reftable_addition_commit (add);
@@ -361,7 +360,7 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void) .value_type = REFTABLE_REF_VAL1, .value.val1 = {0x01}, }; -struct reftable_write_options opts = {0 }; +struct reftable_write_options opts = {0 };struct reftable_stack *st;struct strbuf table_path =STRBUF_INIT ;char *dir =get_tmp_dir (__LINE__);
@@ -423,10 +422,10 @@ static void t_reftable_stack_update_index_check(void) err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref1); + err =reftable_stack_add (st, write_test_ref, &ref1);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref2); + err =reftable_stack_add (st, write_test_ref, &ref2); check_int(err, ==, REFTABLE_API_ERROR);reftable_stack_destroy (st);clear_dir (dir);
@@ -442,7 +441,7 @@ static void t_reftable_stack_lock_failure(void) err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =-1 ; i !=REFTABLE_EMPTY_TABLE_ERROR ; i--) { - err =reftable_stack_add (st, &write_error, &i); + err =reftable_stack_add (st, write_error, &i); check_int(err, ==, i); }
@@ -452,7 +451,6 @@ static void t_reftable_stack_lock_failure(void)static void t_reftable_stack_add (void ) { -int i =0 ;int err =0 ;struct reftable_write_options opts = { .exact_log_message =1 ,
@@ -461,18 +459,18 @@ static void t_reftable_stack_add(void) };struct reftable_stack *st =NULL ;char *dir =get_tmp_dir (__LINE__); -struct reftable_ref_record refs[2 ] = { {NULL } }; -struct reftable_log_record logs[2 ] = { {NULL } }; +struct reftable_ref_record refs[2 ] = {0 }; +struct reftable_log_record logs[2 ] = {0 };struct strbuf path =STRBUF_INIT ;struct stat stat_result; -int N =ARRAY_SIZE (refs); +size_t i,N =ARRAY_SIZE (refs); err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =0 ; i <N ; i++) {char buf[256 ]; -snprintf (buf,sizeof (buf),"branch%02d" , i); +snprintf (buf,sizeof (buf),"branch%02" PRIuMAX, (uintmax_t )i); refs[i].refname =xstrdup (buf); refs[i].update_index = i +1 ; refs[i].value_type =REFTABLE_REF_VAL1 ;
@@ -486,7 +484,7 @@ static void t_reftable_stack_add(void) }for (i =0 ; i <N ; i++) { -int err =reftable_stack_add (st, &write_test_ref, &refs[i]); +int err =reftable_stack_add (st, write_test_ref, &refs[i]);check (!err); }
@@ -495,7 +493,7 @@ static void t_reftable_stack_add(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -503,7 +501,7 @@ static void t_reftable_stack_add(void)check (!err);for (i =0 ; i <N ; i++) { -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 };int err =reftable_stack_read_ref (st, refs[i].refname, &dest);check (!err);
@@ -513,7 +511,7 @@ static void t_reftable_stack_add(void) }for (i =0 ; i <N ; i++) { -struct reftable_log_record dest = {NULL }; +struct reftable_log_record dest = {0 };int err =reftable_stack_read_log (st, refs[i].refname, &dest);check (!err); check(reftable_log_record_equal(&dest, logs + i,
@@ -581,11 +579,11 @@ static void t_reftable_stack_log_normalize(void)check (!err); input.value.update.message = (char *)"one\ntwo" ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg); check_int(err, ==, REFTABLE_API_ERROR); input.value.update.message = (char *)"one" ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg);check (!err); err =reftable_stack_read_log (st, input.refname, &dest);
@@ -594,7 +592,7 @@ static void t_reftable_stack_log_normalize(void) input.value.update.message = (char *)"two\n" ; arg.update_index =2 ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg);check (!err); err =reftable_stack_read_log (st, input.refname, &dest);check (!err);
@@ -608,16 +606,15 @@ static void t_reftable_stack_log_normalize(void)static void t_reftable_stack_tombstone (void ) { -int i =0 ;char *dir =get_tmp_dir (__LINE__);struct reftable_write_options opts = {0 };struct reftable_stack *st =NULL ;int err; -struct reftable_ref_record refs[2 ] = { {NULL } }; -struct reftable_log_record logs[2 ] = { {NULL } }; -int N =ARRAY_SIZE (refs); -struct reftable_ref_record dest = {NULL }; -struct reftable_log_record log_dest = {NULL }; +struct reftable_ref_record refs[2 ] = {0 }; +struct reftable_log_record logs[2 ] = {0 }; +size_t i,N =ARRAY_SIZE (refs); +struct reftable_ref_record dest = {0 }; +struct reftable_log_record log_dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -643,7 +640,7 @@ static void t_reftable_stack_tombstone(void) } }for (i =0 ; i <N ; i++) { -int err =reftable_stack_add (st, &write_test_ref, &refs[i]); +int err =reftable_stack_add (st, write_test_ref, &refs[i]);check (!err); }
@@ -652,7 +649,7 @@ static void t_reftable_stack_tombstone(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -701,12 +698,12 @@ static void t_reftable_stack_hash_id(void)struct reftable_stack *st32 =NULL ;struct reftable_write_options opts_default = {0 };struct reftable_stack *st_default =NULL ; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err);/* can't read it with the wrong hash ID. */
@@ -749,21 +746,20 @@ static void t_reflog_expire(void)char *dir =get_tmp_dir (__LINE__);struct reftable_write_options opts = {0 };struct reftable_stack *st =NULL ; -struct reftable_log_record logs[20 ] = { {NULL } }; -int N =ARRAY_SIZE (logs) -1 ; -int i =0 ; +struct reftable_log_record logs[20 ] = {0 }; +size_t i,N =ARRAY_SIZE (logs) -1 ;int err;struct reftable_log_expiry_config expiry = { .time =10 , }; -struct reftable_log_record log = {NULL }; +struct reftable_log_record log = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =1 ; i <=N ; i++) {char buf[256 ]; -snprintf (buf,sizeof (buf),"branch%02d" , i); +snprintf (buf,sizeof (buf),"branch%02" PRIuMAX, (uintmax_t )i); logs[i].refname =xstrdup (buf); logs[i].update_index = i;
@@ -778,7 +774,7 @@ static void t_reflog_expire(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -806,9 +802,8 @@ static void t_reflog_expire(void)/* cleanup */ reftable_stack_destroy (st); -for (i =0 ; i <=N ; i++) { +for (i =0 ; i <=N ; i++)reftable_log_record_release (&logs[i]); - }clear_dir (dir);reftable_log_record_release (&log); }
@@ -830,7 +825,7 @@ static void t_empty_add(void) err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_nothing,NULL ); + err =reftable_stack_add (st, write_nothing,NULL );check (!err); err =reftable_new_stack (&st2, dir, &opts);
@@ -857,8 +852,8 @@ static void t_reftable_stack_auto_compaction(void) };struct reftable_stack *st =NULL ;char *dir =get_tmp_dir (__LINE__); -int err, i; -int N =100 ; +int err; +size_t i,N =100 ; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -871,9 +866,9 @@ static void t_reftable_stack_auto_compaction(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -snprintf (name,sizeof (name),"branch%04d" , i); +snprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err); err =reftable_stack_auto_compact (st);
@@ -935,7 +930,8 @@ static void t_reftable_stack_add_performs_auto_compaction(void)struct reftable_stack *st =NULL ;struct strbuf refname =STRBUF_INIT ;char *dir =get_tmp_dir (__LINE__); -int err, i, n =20 ; +int err; +size_t i, n =20 ; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -955,10 +951,10 @@ static void t_reftable_stack_add_performs_auto_compaction(void) st->opts.disable_auto_compact = i != n;strbuf_reset (&refname); -strbuf_addf (&refname,"branch-%04d" , i); +strbuf_addf (&refname,"branch-%04" PRIuMAX, (uintmax_t )i); ref.refname = refname.buf; - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err);/*
@@ -1039,10 +1035,8 @@ static void t_reftable_stack_compaction_concurrent(void)static void unclean_stack_close (struct reftable_stack *st) {/* break abstraction boundary to simulate unclean shutdown. */ -int i =0 ; -for (; i < st->readers_len; i++) { +for (size_t i =0 ; i < st->readers_len; i++)reftable_reader_free (st->readers[i]); - } st->readers_len =0 ;FREE_AND_NULL (st->readers); }
Signature · 1 line
2.45.GIT
PATCH v3 3/6 t-reftable-stack: use Git's tempfile API instead of mkstemp()
In reply to Chandra PratapGit's tempfile API defined by $GIT_DIR/tempfile.{c,h} provides a unified interface for tempfile operations. Since reftable/stack.c uses this API for all its tempfile needs instead of raw functions like mkstemp(), make the ported stack test strictly use Git's tempfile API as well.
--- t/unit-tests/t-reftable-stack.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
t/unit-tests/t-reftable-stack.c +3 -1
@@ -76,7 +76,8 @@ static char *get_tmp_dir(int linenumber)static void t_read_file (void ) {char *fn =get_tmp_template (__LINE__); -int fd =mkstemp (fn); +struct tempfile *tmp =mks_tempfile (fn); +int fd =get_tempfile_fd (tmp);char out[1024 ] ="line1\n\nline2\nline3" ;int n, err;char **names =NULL ;
@@ -95,6 +96,7 @@ static void t_read_file(void)check_str (want[i], names[i]);free_names (names); (void )remove (fn); +delete_tempfile (&tmp); }static int write_test_ref (struct reftable_writer *wr,void *arg)
Signature · 1 line
2.45.GIT
PATCH v3 4/6 t-reftable-stack: use reftable_ref_record_equal() to compare ref records
In reply to Chandra PratapIn the current stack tests, ref records are compared for equality by sometimes using the dedicated function for ref-record comparison, reftable_ref_record_equal(), and sometimes by explicity comparing contents of the ref records.
Replace the latter instances of ref-record comparison with the former to maintain uniformity throughout the test file.
--- t/unit-tests/t-reftable-stack.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
t/unit-tests/t-reftable-stack.c +2 -2
@@ -173,7 +173,7 @@ static void t_reftable_stack_add_one(void) err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ )); check_int(st->readers_len, >, 0);printf ("testing print functionality:\n" );
@@ -291,7 +291,7 @@ static void t_reftable_stack_transaction_api(void) err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); check_int(REFTABLE_REF_SYMREF, ==, dest.value_type); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ ));reftable_ref_record_release (&dest);reftable_stack_destroy (st);
Signature · 1 line
2.45.GIT
PATCH v3 5/6 t-reftable-stack: add test for non-default compaction factor
In reply to Chandra PratapIn a recent codebase update (commit ae8e378430, merge branch 'ps/reftable-write-options', 2024/05/13) the geometric factor used in auto-compaction of reftable tables was made configurable. Add a test to verify the functionality introduced by this update.
--- t/unit-tests/t-reftable-stack.c | 41 +++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-)
t/unit-tests/t-reftable-stack.c +37 -4
@@ -837,12 +837,12 @@ static void t_empty_add(void)reftable_stack_destroy (st2); } -static int fastlog2 (uint64_t sz) +static int fastlogN (uint64_t sz,uint64_t N ) {int l =0 ;if (sz ==0 )return 0 ; -for (; sz; sz /=2 ) +for (; sz; sz /=N ) l++;return l -1 ; }
@@ -875,11 +875,43 @@ static void t_reftable_stack_auto_compaction(void) err =reftable_stack_auto_compact (st);check (!err); -check (i <3 || st->merged->stack_len <2 *fastlog2 (i)); +check (i <2 || st->merged->stack_len <2 *fastlogN (i,2 )); } check_int(reftable_stack_compaction_stats(st)->entries_written, <, - (uint64_t )(N *fastlog2 (N ))); + (uint64_t )(N *fastlogN (N ,2 ))); + +reftable_stack_destroy (st); +clear_dir (dir); +} + +static void t_reftable_stack_auto_compaction_factor (void ) +{ +struct reftable_write_options opts = { + .auto_compaction_factor =5 , + }; +struct reftable_stack *st =NULL ; +char *dir =get_tmp_dir (__LINE__); +int err; +size_t N =100 ; + + err =reftable_new_stack (&st, dir, &opts); +check (!err); + +for (size_t i =0 ; i <N ; i++) { +char name[20 ]; +struct reftable_ref_record ref = { + .refname = name, + .update_index =reftable_stack_next_update_index (st), + .value_type =REFTABLE_REF_VAL1 , + }; +xsnprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); + + err =reftable_stack_add (st, &write_test_ref, &ref); +check (!err); + +check (i <5 || st->merged->stack_len <5 *fastlogN (i,5 )); + }reftable_stack_destroy (st);clear_dir (dir);
@@ -1086,6 +1118,7 @@ int cmd_main(int argc, const char *argv[])TEST (t_reftable_stack_add_one (),"add a single ref record to stack" );TEST (t_reftable_stack_add_performs_auto_compaction (),"addition to stack triggers auto-compaction" );TEST (t_reftable_stack_auto_compaction (),"stack must form geometric sequence after compaction" ); +TEST (t_reftable_stack_auto_compaction_factor (),"auto-compaction with non-default geometric factor" );TEST (t_reftable_stack_auto_compaction_fails_gracefully (),"failure on auto-compaction" );TEST (t_reftable_stack_auto_compaction_with_locked_tables (),"auto compaction with locked tables" );TEST (t_reftable_stack_compaction_concurrent (),"compaction with concurrent stack" );
Signature · 1 line
2.45.GIT
PATCH v3 6/6 t-reftable-stack: add test for stack iterators
In reply to Chandra Pratapreftable_stack_init_ref_iterator and reftable_stack_init_log_iterator as defined by reftable/stack.{c,h} initialize a stack iterator to iterate over the ref and log records in a reftable stack respectively. Since these functions are not exercised by any of the existing tests, add a test for them.
--- t/unit-tests/t-reftable-stack.c | 80 +++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+)
t/unit-tests/t-reftable-stack.c +80 -0
@@ -550,6 +550,85 @@ static void t_reftable_stack_add(void)clear_dir (dir); } +static void t_reftable_stack_iterator (void ) +{ +struct reftable_write_options opts = {0 }; +struct reftable_stack *st =NULL ; +char *dir =get_tmp_dir (__LINE__); +struct reftable_ref_record refs[10 ] = {0 }; +struct reftable_log_record logs[10 ] = {0 }; +struct reftable_iterator it = {0 }; +size_t N =ARRAY_SIZE (refs), i; +int err; + + err =reftable_new_stack (&st, dir, &opts); +check (!err); + +for (i =0 ; i <N ; i++) { + refs[i].refname =xstrfmt ("branch%02" PRIuMAX, (uintmax_t )i); + refs[i].update_index = i +1 ; + refs[i].value_type =REFTABLE_REF_VAL1 ; +set_test_hash (refs[i].value.val1, i); + + logs[i].refname =xstrfmt ("branch%02" PRIuMAX, (uintmax_t )i); + logs[i].update_index = i +1 ; + logs[i].value_type =REFTABLE_LOG_UPDATE ; + logs[i].value.update.email =xstrdup ("johndoe@invalid" ); + logs[i].value.update.message =xstrdup ("commit\n" ); +set_test_hash (logs[i].value.update.new_hash, i); + } + +for (i =0 ; i <N ; i++) { + err =reftable_stack_add (st, write_test_ref, &refs[i]); +check (!err); + } + +for (i =0 ; i <N ; i++) { +struct write_log_arg arg = { + .log = &logs[i], + .update_index =reftable_stack_next_update_index (st), + }; + err =reftable_stack_add (st, write_test_log, &arg); +check (!err); + } + +reftable_stack_init_ref_iterator (st, &it); +reftable_iterator_seek_ref (&it, refs[0 ].refname); +for (i =0 ; ; i++) { +struct reftable_ref_record ref = {0 }; + err =reftable_iterator_next_ref (&it, &ref); +if (err >0 ) +break ; +check (!err); +check (reftable_ref_record_equal (&ref, &refs[i],GIT_SHA1_RAWSZ )); +reftable_ref_record_release (&ref); + } + check_int(i, ==, N); + +reftable_iterator_destroy (&it); + +reftable_stack_init_log_iterator (st, &it); +reftable_iterator_seek_log (&it, logs[0 ].refname); +for (i =0 ; ; i++) { +struct reftable_log_record log = {0 }; + err =reftable_iterator_next_log (&it, &log); +if (err >0 ) +break ; +check (!err); +check (reftable_log_record_equal (&log, &logs[i],GIT_SHA1_RAWSZ )); +reftable_log_record_release (&log); + } + check_int(i, ==, N); + +reftable_stack_destroy (st); +reftable_iterator_destroy (&it); +for (i =0 ; i <N ; i++) { +reftable_ref_record_release (&refs[i]); +reftable_log_record_release (&logs[i]); + } +clear_dir (dir); +} +static void t_reftable_stack_log_normalize (void ) {int err =0 ;
@@ -1125,6 +1204,7 @@ int cmd_main(int argc, const char *argv[])TEST (t_reftable_stack_compaction_concurrent_clean (),"compaction with unclean stack shutdown" );TEST (t_reftable_stack_compaction_with_locked_tables (),"compaction with locked tables" );TEST (t_reftable_stack_hash_id (),"read stack with wrong hash ID" ); +TEST (t_reftable_stack_iterator (),"log and ref iterator for reftable stack" );TEST (t_reftable_stack_lock_failure (),"stack addition with lockfile failure" );TEST (t_reftable_stack_log_normalize (),"log messages should be normalized" );TEST (t_reftable_stack_tombstone (),"'tombstone' refs in stack" );
Signature · 1 line
2.45.GIT
Quoted 5 lines · Chandra Pratap
Changes in v2:
- Use 'size_t' as array index instead of 'int' in a test that is modified in patch 2.
- Fix a coding style violation in the newly introduced test in patch 6.
If I recall correctly, the first iteration conflicted too badly with other topics in flight, and kept out of 'seen' so far. Have you tested this series, not just standalone, but see how well it works together with other topics by creating trial merges of it into 'next' and 'seen'?
It some other topics that conflict with what you do in this topic have advanced (or better yet, graduated to 'master'), it may not be a bad idea to consider rebasing the topic to more recent 'master', possibly merging other topics into it first.
Thanks.
Quoted 11 lines · Junio C Hamano
Quoted 5 lines · Chandra Pratap
Changes in v2:
- Use 'size_t' as array index instead of 'int' in a test that is modified in patch 2.
- Fix a coding style violation in the newly introduced test in patch 6.If I recall correctly, the first iteration conflicted too badly with other topics in flight, and kept out of 'seen' so far.
Yeah, I remember Patrick was working on some other patch series involving 'reftable/stack_test.c' which caused conflicts with this series. I _did_ make sure to rebase this series on top of that one.
Quoted 3 lines
Have you tested this series, not just standalone, but see how well it works together with other topics by creating trial merges of it into 'next' and 'seen'?
I did notice some small Makefile conflicts with the latest 'master' on the GitHub CI, but other than that, no, I haven't checked the patch against 'next' or 'seen'.
Quoted 4 lines
It some other topics that conflict with what you do in this topic have advanced (or better yet, graduated to 'master'), it may not be a bad idea to consider rebasing the topic to more recent 'master', possibly merging other topics into it first.
I generally make sure to rebase my patches on top of the latest master before sending them to the mailing list. I'll add 'next' and 'seen' to my checklist as well.
Quoted 2 lines · Chandra Pratap
I generally make sure to rebase my patches on top of the latest master before sending them to the mailing list.
This is generally considered a bad practice. You shouldn't rebase unless there is a compelling reason (e.g., API you rely on disappeared and you need to reimplement your change differently, you rewrote some code based on an old version, but the code you touched have been enhanced so you'd need to rewrite the new part).
On the other hand, you should realize that every topic first is queued to 'seen' and only after it proves that it plays well with other topics in flight, it is considered to advance to 'next'. So if you conflict with other topics that makes you conflict when merged to either 'next' or 'seen', you'd be better off creating a more suitable base than 'master' and then build on top of it.
An effective way to do so is to mimick how topics like
https://lore.kernel.org/git/[email protected]/
are built. This one says:
This patch series continues to build on top of 25673b1c47 (The third batch, 2024-08-07) with Junio's ps/reftable-stack-compaction at f234df07f6 (reftable/stack: handle locked tables during auto-compaction, 2024-08-08) merged into it.
in its cover letter, allowing anybody to reconstruct the base with
$ git checkout -b ps/reftable-drop-generic 25673b1c47
$ git merge f234df07f6before running "git am" to apply the series on top. Because it _depends_ on ps/reftable-stack-compaction, the resulting topic cannot be merged to anywhere without dragging the base topic with it, so such a dependency needs to be already fairly stable (e.g., have been thoroughly reviewed and merged to 'next'), but then the topic will have less chance to conflict when merged to 'next' and 'seen'.
Thanks.
Quoted 6 lines · Junio C Hamano
On the other hand, you should realize that every topic first is queued to 'seen' and only after it proves that it plays well with other topics in flight, it is considered to advance to 'next'. So if you conflict with other topics that makes you conflict when merged to either 'next' or 'seen', you'd be better off creating a more suitable base than 'master' and then build on top of it.
$ git log --first-parent --oneline \ cp/unit-test-reftable-stack..next -- reftable/stack_test.c
tells me that Patrick's "concurrent compaction" and "drop generic" are the big two topics that touch reftable/stack_test.c file that you are removing. As the former already depends on the latter, it may make sense to
$ git checkout -b cp/unit-test-reftable-stack master
$ git merge ps/reftable-concurrent-compactionto prepare the base, and then rebuild these patches on top of it. There is another topic jk/mark-unused-parameters that touches the same file, but the conflict it causes is trivial.
In any case, the cover letter is a good place to describe how you prepared such a custom base (as opposed to "the patches in this topic apply cleanly to any recent tip of 'master', as the topic touches a relatively dormant calm area of the code base", in which case you do not say anything).
Thanks.
v4 cover t: port reftable/stack_test.c to the unit testing framework
In reply to Chandra PratapThe reftable library comes with self tests, which are exercised as part of the usual end-to-end tests and are designed to observe the end-user visible effects of Git commands. What it exercises, however, is a better match for the unit-testing framework, merged at 8bf6fbd0 (Merge branch 'js/doc-unit-tests', 2023-12-09), which is designed to observe how low level implementation details, at the level of sequences of individual function calls, behave.
Hence, port reftable/stack_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.
---
Changes in v4:
- v3 of this patch series had conflicts with 85da2a2ab6 (Merge branch 'ps/reftable-concurrent-compaction') and e49d2472d2 (Merge branch 'ps/reftable-drop-generic').
These branches have since graduated to 'master', so this version rebases the commits on top of the latest 'master'. The resulting branch has been tested to have no conflicts with 'next' and trivial ones with 'seen'.
CI/PR: https://github.com/gitgitgadget/git/pull/1762
Chandra Pratap(6):
t: move reftable/stack_test.c to the unit testing framework
t: harmonize t-reftable-stack.c with coding guidelines
t-reftable-stack: use Git's tempfile API instead of mkstemp()
t-reftable-stack: use reftable_ref_record_equal() to compare ref records
t-reftable-stack: add test for non-default compaction factor
t-reftable-stack: add test for stack iterators
Makefile | 2 +- reftable/reftable-tests.h | 1 - t/helper/test-reftable.c | 1 - reftable/stack_test.c => t/unit-tests/t-reftable-stack.c | 611 +++++++++++++++++++-------------- 4 files changed, 360 insertions(+), 255 deletions(-)
Range-diff against v3:
<rebase commits>
1: d094b8f94d ! 184: cdfb49f5d5 t: move reftable/stack_test.c to the unit testing framework
@@ Commit message
Signed-off-by: Chandra Pratap <[email protected]>
## Makefile ##
-@@ Makefile: UNIT_TEST_PROGRAMS += t-reftable-basics
- UNIT_TEST_PROGRAMS += t-reftable-merged
+@@ Makefile: UNIT_TEST_PROGRAMS += t-reftable-merged
UNIT_TEST_PROGRAMS += t-reftable-pq
+ UNIT_TEST_PROGRAMS += t-reftable-readwrite
UNIT_TEST_PROGRAMS += t-reftable-record
+UNIT_TEST_PROGRAMS += t-reftable-stack
UNIT_TEST_PROGRAMS += t-reftable-tree
UNIT_TEST_PROGRAMS += t-strbuf
UNIT_TEST_PROGRAMS += t-strcmp-offset
-@@ Makefile: REFTABLE_OBJS += reftable/writer.o
- REFTABLE_TEST_OBJS += reftable/block_test.o
- REFTABLE_TEST_OBJS += reftable/dump.o
- REFTABLE_TEST_OBJS += reftable/readwrite_test.o
+@@ Makefile: REFTABLE_OBJS += reftable/stack.o
+ REFTABLE_OBJS += reftable/tree.o
+ REFTABLE_OBJS += reftable/writer.o
+
-REFTABLE_TEST_OBJS += reftable/stack_test.o
REFTABLE_TEST_OBJS += reftable/test_framework.o
TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS)) $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS))
## reftable/reftable-tests.h ##
-@@ reftable/reftable-tests.h: int basics_test_main(int argc, const char **argv);
- int block_test_main(int argc, const char **argv);
- int record_test_main(int argc, const char **argv);
- int readwrite_test_main(int argc, const char **argv);
+@@ reftable/reftable-tests.h: license that can be found in the LICENSE file or at
+ #ifndef REFTABLE_TESTS_H
+ #define REFTABLE_TESTS_H
+
-int stack_test_main(int argc, const char **argv);
- int reftable_dump_main(int argc, char *const *argv);
#endif
## t/helper/test-reftable.c ##
-@@ t/helper/test-reftable.c: int cmd__reftable(int argc, const char **argv)
+@@
+ int cmd__reftable(int argc, const char **argv)
+ {
/* test from simple to complex. */
- block_test_main(argc, argv);
- readwrite_test_main(argc, argv);
- stack_test_main(argc, argv);
return 0;
}
@@ t/unit-tests/t-reftable-stack.c: license that can be found in the LICENSE file o
-
-#include "system.h"
-
+-#include "copy.h"
-#include "reftable-reader.h"
-#include "merged.h"
-#include "basics.h"
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_add_one(void)
+ check_str("master", dest.value.symref);
+ check_int(st->readers_len, >, 0);
- printf("testing print functionality:\n");
- err = reftable_stack_print_directory(dir, GIT_SHA1_FORMAT_ID);
-- EXPECT_ERR(err);
-+ check(!err);
-
- err = reftable_stack_print_directory(dir, GIT_SHA256_FORMAT_ID);
-- EXPECT(err == REFTABLE_FORMAT_ERROR);
-+ check_int(err, ==, REFTABLE_FORMAT_ERROR);
-
#ifndef GIT_WINDOWS_NATIVE
strbuf_addstr(&scratch, dir);
strbuf_addstr(&scratch, "/tables.list");
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_transaction_api
* all tables in the stack.
*/
if (i != n)
-- EXPECT(st->merged->stack_len == i + 1);
-+ check_int(st->merged->stack_len, ==, i + 1);
+- EXPECT(st->merged->readers_len == i + 1);
++ check_int(st->merged->readers_len, ==, i + 1);
else
-- EXPECT(st->merged->stack_len == 1);
-+ check_int(st->merged->stack_len, ==, 1);
+- EXPECT(st->merged->readers_len == 1);
++ check_int(st->merged->readers_len, ==, 1);
}
reftable_stack_destroy(st);
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_auto_compaction
err = reftable_stack_add(st, write_test_ref, &ref);
- EXPECT_ERR(err);
-- EXPECT(st->merged->stack_len == 1);
+- EXPECT(st->merged->readers_len == 1);
- EXPECT(st->stats.attempts == 0);
- EXPECT(st->stats.failures == 0);
+ check(!err);
-+ check_int(st->merged->stack_len, ==, 1);
++ check_int(st->merged->readers_len, ==, 1);
+ check_int(st->stats.attempts, ==, 0);
+ check_int(st->stats.failures, ==, 0);
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_auto_compaction
ref.update_index = 2;
err = reftable_stack_add(st, write_test_ref, &ref);
- EXPECT_ERR(err);
-- EXPECT(st->merged->stack_len == 2);
+- EXPECT(st->merged->readers_len == 2);
- EXPECT(st->stats.attempts == 1);
- EXPECT(st->stats.failures == 1);
+ check(!err);
-+ check_int(st->merged->stack_len, ==, 2);
++ check_int(st->merged->readers_len, ==, 2);
+ check_int(st->stats.attempts, ==, 1);
+ check_int(st->stats.failures, ==, 1);
reftable_stack_destroy(st);
strbuf_release(&table_path);
-@@ t/unit-tests/t-reftable-stack.c: static int write_error(struct reftable_writer *wr, void *arg)
+@@ t/unit-tests/t-reftable-stack.c: static int write_error(struct reftable_writer *wr UNUSED, void *arg)
return *((int *)arg);
}
@@ t/unit-tests/t-reftable-stack.c: static void test_reflog_expire(void)
/* cleanup */
reftable_stack_destroy(st);
-@@ t/unit-tests/t-reftable-stack.c: static int write_nothing(struct reftable_writer *wr, void *arg)
+@@ t/unit-tests/t-reftable-stack.c: static int write_nothing(struct reftable_writer *wr, void *arg UNUSED)
return 0;
}
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_auto_compaction
err = reftable_stack_auto_compact(st);
- EXPECT_ERR(err);
-- EXPECT(i < 3 || st->merged->stack_len < 2 * fastlog2(i));
+- EXPECT(i < 3 || st->merged->readers_len < 2 * fastlog2(i));
+ check(!err);
-+ check(i < 3 || st->merged->stack_len < 2 * fastlog2(i));
++ check(i < 3 || st->merged->readers_len < 2 * fastlog2(i));
}
- EXPECT(reftable_stack_compaction_stats(st)->entries_written <
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_auto_compaction
+ check(!err);
write_n_ref_tables(st, 5);
-- EXPECT(st->merged->stack_len == 5);
-+ check_int(st->merged->stack_len, ==, 5);
+- EXPECT(st->merged->readers_len == 5);
++ check_int(st->merged->readers_len, ==, 5);
/*
* Given that all tables we have written should be roughly the same
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_auto_compaction
err = reftable_stack_auto_compact(st);
- EXPECT_ERR(err);
- EXPECT(st->stats.failures == 0);
-- EXPECT(st->merged->stack_len == 4);
+- EXPECT(st->merged->readers_len == 4);
+ check(!err);
+ check_int(st->stats.failures, ==, 0);
-+ check_int(st->merged->stack_len, ==, 4);
++ check_int(st->merged->readers_len, ==, 4);
reftable_stack_destroy(st);
strbuf_release(&buf);
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_add_performs_au
* all tables in the stack.
*/
if (i != n)
-- EXPECT(st->merged->stack_len == i + 1);
-+ check_int(st->merged->stack_len, ==, i + 1);
+- EXPECT(st->merged->readers_len == i + 1);
++ check_int(st->merged->readers_len, ==, i + 1);
else
-- EXPECT(st->merged->stack_len == 1);
-+ check_int(st->merged->stack_len, ==, 1);
+- EXPECT(st->merged->readers_len == 1);
++ check_int(st->merged->readers_len, ==, 1);
}
reftable_stack_destroy(st);
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_compaction_with
+ check(!err);
write_n_ref_tables(st, 3);
-- EXPECT(st->merged->stack_len == 3);
-+ check_int(st->merged->stack_len, ==, 3);
+- EXPECT(st->merged->readers_len == 3);
++ check_int(st->merged->readers_len, ==, 3);
/* Lock one of the tables that we're about to compact. */
strbuf_reset(&buf);
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_compaction_with
err = reftable_stack_compact_all(st, NULL);
- EXPECT(err == REFTABLE_LOCK_ERROR);
- EXPECT(st->stats.failures == 1);
-- EXPECT(st->merged->stack_len == 3);
+- EXPECT(st->merged->readers_len == 3);
+ check_int(err, ==, REFTABLE_LOCK_ERROR);
+ check_int(st->stats.failures, ==, 1);
-+ check_int(st->merged->stack_len, ==, 3);
++ check_int(st->merged->readers_len, ==, 3);
reftable_stack_destroy(st);
strbuf_release(&buf);
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_compaction_conc
clear_dir(dir);
}
--int stack_test_main(int argc, const char *argv[])
-+int cmd_main(int argc, const char *argv[])
+-static void test_reftable_stack_read_across_reload(void)
++static void t_reftable_stack_read_across_reload(void)
+ {
+ struct reftable_write_options opts = { 0 };
+ struct reftable_stack *st1 = NULL, *st2 = NULL;
+@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_read_across_reload(void)
+
+ /* Create a first stack and set up an iterator for it. */
+ err = reftable_new_stack(&st1, dir, &opts);
+- EXPECT_ERR(err);
++ check(!err);
+ write_n_ref_tables(st1, 2);
+- EXPECT(st1->merged->readers_len == 2);
++ check_int(st1->merged->readers_len, ==, 2);
+ reftable_stack_init_ref_iterator(st1, &it);
+ err = reftable_iterator_seek_ref(&it, "");
+- EXPECT_ERR(err);
++ check(!err);
+
+ /* Set up a second stack for the same directory and compact it. */
+ err = reftable_new_stack(&st2, dir, &opts);
+- EXPECT_ERR(err);
+- EXPECT(st2->merged->readers_len == 2);
++ check(!err);
++ check_int(st2->merged->readers_len, ==, 2);
+ err = reftable_stack_compact_all(st2, NULL);
+- EXPECT_ERR(err);
+- EXPECT(st2->merged->readers_len == 1);
++ check(!err);
++ check_int(st2->merged->readers_len, ==, 1);
+
+ /*
+ * Verify that we can continue to use the old iterator even after we
+ * have reloaded its stack.
+ */
+ err = reftable_stack_reload(st1);
+- EXPECT_ERR(err);
+- EXPECT(st1->merged->readers_len == 1);
++ check(!err);
++ check_int(st1->merged->readers_len, ==, 1);
+ err = reftable_iterator_next_ref(&it, &rec);
+- EXPECT_ERR(err);
+- EXPECT(!strcmp(rec.refname, "refs/heads/branch-0000"));
++ check(!err);
++ check_str(rec.refname, "refs/heads/branch-0000");
+ err = reftable_iterator_next_ref(&it, &rec);
+- EXPECT_ERR(err);
+- EXPECT(!strcmp(rec.refname, "refs/heads/branch-0001"));
++ check(!err);
++ check_str(rec.refname, "refs/heads/branch-0001");
+ err = reftable_iterator_next_ref(&it, &rec);
+- EXPECT(err > 0);
++ check_int(err, >, 0);
+
+ reftable_ref_record_release(&rec);
+ reftable_iterator_destroy(&it);
+@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_read_across_reload(void)
+ clear_dir(dir);
+ }
+
+-static void test_reftable_stack_reload_with_missing_table(void)
++static void t_reftable_stack_reload_with_missing_table(void)
+ {
+ struct reftable_write_options opts = { 0 };
+ struct reftable_stack *st = NULL;
+@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_reload_with_missing_table(void)
+
+ /* Create a first stack and set up an iterator for it. */
+ err = reftable_new_stack(&st, dir, &opts);
+- EXPECT_ERR(err);
++ check(!err);
+ write_n_ref_tables(st, 2);
+- EXPECT(st->merged->readers_len == 2);
++ check_int(st->merged->readers_len, ==, 2);
+ reftable_stack_init_ref_iterator(st, &it);
+ err = reftable_iterator_seek_ref(&it, "");
+- EXPECT_ERR(err);
++ check(!err);
+
+ /*
+ * Update the tables.list file with some garbage data, while reusing
+@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_reload_with_missing_table(void)
+ strbuf_addf(&table_path, "%s.lock", st->list_file);
+ write_file_buf(table_path.buf, content.buf, content.len);
+ err = rename(table_path.buf, st->list_file);
+- EXPECT_ERR(err);
++ check(!err);
+
+ err = reftable_stack_reload(st);
+- EXPECT(err == -4);
+- EXPECT(st->merged->readers_len == 2);
++ check_int(err, ==, -4);
++ check_int(st->merged->readers_len, ==, 2);
+
+ /*
+ * Even though the reload has failed, we should be able to continue
+ * using the iterator.
+ */
+ err = reftable_iterator_next_ref(&it, &rec);
+- EXPECT_ERR(err);
+- EXPECT(!strcmp(rec.refname, "refs/heads/branch-0000"));
++ check(!err);
++ check_str(rec.refname, "refs/heads/branch-0000");
+ err = reftable_iterator_next_ref(&it, &rec);
+- EXPECT_ERR(err);
+- EXPECT(!strcmp(rec.refname, "refs/heads/branch-0001"));
++ check(!err);
++ check_str(rec.refname, "refs/heads/branch-0001");
+ err = reftable_iterator_next_ref(&it, &rec);
+- EXPECT(err > 0);
++ check_int(err, >, 0);
+
+ reftable_ref_record_release(&rec);
+ reftable_iterator_destroy(&it);
+@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_reload_with_missing_table(void)
+ clear_dir(dir);
+ }
+
+-int stack_test_main(int argc UNUSED, const char *argv[] UNUSED)
++int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
{
- RUN_TEST(test_empty_add);
- RUN_TEST(test_read_file);
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_compaction_conc
- RUN_TEST(test_reftable_stack_auto_compaction_fails_gracefully);
- RUN_TEST(test_reftable_stack_update_index_check);
- RUN_TEST(test_reftable_stack_uptodate);
+- RUN_TEST(test_reftable_stack_read_across_reload);
+- RUN_TEST(test_reftable_stack_reload_with_missing_table);
- RUN_TEST(test_suggest_compaction_segment);
- RUN_TEST(test_suggest_compaction_segment_nothing);
- return 0;
@@ t/unit-tests/t-reftable-stack.c: static void test_reftable_stack_compaction_conc
+ TEST(t_reftable_stack_hash_id(), "read stack with wrong hash ID");
+ TEST(t_reftable_stack_lock_failure(), "stack addition with lockfile failure");
+ TEST(t_reftable_stack_log_normalize(), "log messages should be normalized");
++ TEST(t_reftable_stack_read_across_reload(), "reading stack works across reloads");
++ TEST(t_reftable_stack_reload_with_missing_table(), "reading stack with garbage tables");
+ TEST(t_reftable_stack_tombstone(), "'tombstone' refs in stack");
+ TEST(t_reftable_stack_transaction_api(), "update transaction to stack");
+ TEST(t_reftable_stack_transaction_api_performs_auto_compaction(), "update transaction triggers auto-compaction"); 2: 838ccc63a7 ! 185: e37be61cbe t: harmonize t-reftable-stack.c with coding guidelines
@@ t/unit-tests/t-reftable-stack.c: static void t_read_file(void)
(void) remove(fn);
}
@@ t/unit-tests/t-reftable-stack.c: static void write_n_ref_tables(struct reftable_stack *st,
- .value_type = REFTABLE_REF_VAL1,
};
+ strbuf_reset(&buf);
- strbuf_addf(&buf, "refs/heads/branch-%04u", (unsigned) i);
+ strbuf_addf(&buf, "refs/heads/branch-%04"PRIuMAX, (uintmax_t)i);
ref.refname = buf.buf;
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add_performs_auto_
check(!err);
/*
-@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_compaction_concurrent(void)
- static void unclean_stack_close(struct reftable_stack *st)
- {
- /* break abstraction boundary to simulate unclean shutdown. */
-- int i = 0;
-- for (; i < st->readers_len; i++) {
-+ for (size_t i = 0; i < st->readers_len; i++)
- reftable_reader_free(st->readers[i]);
-- }
- st->readers_len = 0;
- FREE_AND_NULL(st->readers);
- }
3: 2a151c299c = 186: 449b9e5e26 t-reftable-stack: use Git's tempfile API instead of mkstemp() 4: fb5073da2c ! 187: a4f1a8e93a t-reftable-stack: use reftable_ref_record_equal() to compare ref records
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add_one(void)
+ check(reftable_ref_record_equal(&ref, &dest, GIT_SHA1_RAWSZ));
check_int(st->readers_len, >, 0);
- printf("testing print functionality:\n");
+ #ifndef GIT_WINDOWS_NATIVE
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_transaction_api(void)
err = reftable_stack_read_ref(st, ref.refname, &dest);
check(!err);
5: 028fa6f70b ! 188: b5dce8c505 t-reftable-stack: add test for non-default compaction factor
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_auto_compaction(vo
err = reftable_stack_auto_compact(st);
check(!err);
-- check(i < 3 || st->merged->stack_len < 2 * fastlog2(i));
-+ check(i < 2 || st->merged->stack_len < 2 * fastlogN(i, 2));
+- check(i < 3 || st->merged->readers_len < 2 * fastlog2(i));
++ check(i < 2 || st->merged->readers_len < 2 * fastlogN(i, 2));
}
check_int(reftable_stack_compaction_stats(st)->entries_written, <,
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_auto_compaction(vo
+ err = reftable_stack_add(st, &write_test_ref, &ref);
+ check(!err);
+
-+ check(i < 5 || st->merged->stack_len < 5 * fastlogN(i, 5));
++ check(i < 5 || st->merged->readers_len < 5 * fastlogN(i, 5));
+ }
reftable_stack_destroy(st);
clear_dir(dir);
-@@ t/unit-tests/t-reftable-stack.c: int cmd_main(int argc, const char *argv[])
+@@ t/unit-tests/t-reftable-stack.c: int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
TEST(t_reftable_stack_add_one(), "add a single ref record to stack");
TEST(t_reftable_stack_add_performs_auto_compaction(), "addition to stack triggers auto-compaction");
TEST(t_reftable_stack_auto_compaction(), "stack must form geometric sequence after compaction");
6: fa0d358e65 ! 189: 8081831ce6 t-reftable-stack: add test for stack iterators
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add(void)
static void t_reftable_stack_log_normalize(void)
{
int err = 0;
-@@ t/unit-tests/t-reftable-stack.c: int cmd_main(int argc, const char *argv[])
+@@ t/unit-tests/t-reftable-stack.c: int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
TEST(t_reftable_stack_compaction_concurrent_clean(), "compaction with unclean stack shutdown");
TEST(t_reftable_stack_compaction_with_locked_tables(), "compaction with locked tables");
TEST(t_reftable_stack_hash_id(), "read stack with wrong hash ID");
+ TEST(t_reftable_stack_iterator(), "log and ref iterator for reftable stack");
TEST(t_reftable_stack_lock_failure(), "stack addition with lockfile failure");
TEST(t_reftable_stack_log_normalize(), "log messages should be normalized");
- TEST(t_reftable_stack_tombstone(), "'tombstone' refs in stack");
+ TEST(t_reftable_stack_read_across_reload(), "reading stack works across reloads");PATCH v4 1/6 t: move reftable/stack_test.c to the unit testing framework
reftable/stack_test.c exercises the functions defined in reftable/stack.{c, h}. Migrate reftable/stack_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework and renaming the tests to be in-line with unit-tests' standards.
Since some of the tests use set_test_hash() defined by reftable/test_framework.{c, h} but these files are not '#included' in the test file, copy this function in the ported test file.
--- Makefile | 2 +- reftable/reftable-tests.h | 1 - t/helper/test-reftable.c | 1 - .../unit-tests/t-reftable-stack.c | 429 +++++++++--------- 4 files changed, 214 insertions(+), 219 deletions(-) rename reftable/stack_test.c => t/unit-tests/t-reftable-stack.c (76%)
Makefile +1 -1
@@ -1349,6 +1349,7 @@ UNIT_TEST_PROGRAMS += t-reftable-merged UNIT_TEST_PROGRAMS += t-reftable-pq UNIT_TEST_PROGRAMS += t-reftable-readwrite UNIT_TEST_PROGRAMS += t-reftable-record +UNIT_TEST_PROGRAMS += t-reftable-stack UNIT_TEST_PROGRAMS += t-reftable-tree UNIT_TEST_PROGRAMS += t-strbuf UNIT_TEST_PROGRAMS += t-strcmp-offset
@@ -2691,7 +2692,6 @@ REFTABLE_OBJS += reftable/stack.o REFTABLE_OBJS += reftable/tree.o REFTABLE_OBJS += reftable/writer.o -REFTABLE_TEST_OBJS += reftable/stack_test.o REFTABLE_TEST_OBJS += reftable/test_framework.o TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS)) $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS))
reftable/reftable-tests.h +0 -1
@@ -9,6 +9,5 @@ license that can be found in the LICENSE file or at#ifndef REFTABLE_TESTS_H #define REFTABLE_TESTS_H -int stack_test_main (int argc,const char **argv);#endif
t/helper/test-reftable.c +0 -1
@@ -12,7 +12,6 @@int cmd__reftable (int argc,const char **argv) {/* test from simple to complex. */ -stack_test_main (argc, argv);return 0 ; }
t/unit-tests/t-reftable-stack.c renamed from reftable/stack_test.c +213 -216
1,006 lines not on this page · raw message
Signature · 1 line
2.45.GIT
Re: [PATCH v4 1/6] t: move reftable/stack_test.c to the unit testing framework
Review
Quoted 6 lines · Chandra Pratap
int
cmd__reftable(int argc, const char **argv)
{(quoted diff) +0
-1/* test from simple to complex. */ - stack_test_main(argc, argv); return 0; }
This makes cmd__reftable() a no-op.
Even though you cannot remove t/helpter/test-reftable.c, as it contains the implementation for "test-tool dump-reftable", we should at least be able to do something like this.
--- >8 ---
Subject: [PATCH 7/6] t: clean up leftover reftable test cruft
With migration of the tests of the reftable to the unit-tests framework, "test-tool reftable" has become an expensive no-op.
Retire everything that uses "test-tool reftable", and everything that is used to implement it.
While at it, make the cmds[] list alphabetically sorted again by moving the entry for "dump-reftable".
--- t/helper/test-reftable.c | 6 ------ t/helper/test-tool.c | 3 +-- t/helper/test-tool.h | 1 - t/t0032-reftable-unittest.sh | 16 ---------------- 4 files changed, 1 insertion(+), 25 deletions(-)
t/helper/test-reftable.c +0 -6
@@ -9,12 +9,6 @@#include "reftable/reftable-tests.h" #include "test-tool.h" -int cmd__reftable (int argc,const char **argv) -{ -/* test from simple to complex. */ -return 0 ; -} -static void print_help (void ) { printf("usage: dump [-st] arg\n\n"
t/helper/test-tool.c +1 -2
@@ -26,6 +26,7 @@ static struct test_cmd cmds[] = { { "drop-caches", cmd__drop_caches }, { "dump-cache-tree", cmd__dump_cache_tree }, { "dump-fsmonitor", cmd__dump_fsmonitor }, + { "dump-reftable", cmd__dump_reftable }, { "dump-split-index", cmd__dump_split_index }, { "dump-untracked-cache", cmd__dump_untracked_cache }, { "env-helper", cmd__env_helper },
@@ -61,9 +62,7 @@ static struct test_cmd cmds[] = { { "read-graph", cmd__read_graph }, { "read-midx", cmd__read_midx }, { "ref-store", cmd__ref_store }, - { "reftable", cmd__reftable }, { "rot13-filter", cmd__rot13_filter }, - { "dump-reftable", cmd__dump_reftable }, { "regex", cmd__regex }, { "repository", cmd__repository }, { "revision-walking", cmd__revision_walking },
t/helper/test-tool.h +0 -1
@@ -55,7 +55,6 @@ int cmd__read_graph(int argc, const char **argv);int cmd__read_midx (int argc,const char **argv);int cmd__ref_store (int argc,const char **argv);int cmd__rot13_filter (int argc,const char **argv); -int cmd__reftable (int argc,const char **argv);int cmd__regex (int argc,const char **argv);int cmd__repository (int argc,const char **argv);int cmd__revision_walking (int argc,const char **argv);
t/t0032-reftable-unittest.sh deleted +0 -16
@@ -1,16 +0,0 @@ -#!/bin/sh -# -# Copyright (c) 2020 Google LLC -# - -test_description='reftable unittests' - -TEST_PASSES_SANITIZE_LEAK=true -. ./test-lib.sh - -test_expect_success 'unittests' ' - TMPDIR=$(pwd) && export TMPDIR && - test-tool reftable -' - -test_done
Re: [PATCH v4 1/6] t: move reftable/stack_test.c to the unit testing framework
Quoted 11 lines · Junio C Hamano
Review
Quoted 6 lines · Chandra Pratap
int
cmd__reftable(int argc, const char **argv)
{(quoted diff) +0
-1/* test from simple to complex. */ - stack_test_main(argc, argv); return 0; }This makes
cmd__reftable()a no-op.
Quoted 3 lines
Even though you cannot remove
t/helpter/test-reftable.c, as it contains the implementation for "test-tool dump-reftable", we should at least be able to do something like this.
In addition to Junio's patch you can also remove
"reftable/reftable-tests.h".
Patrick
Re: [PATCH v4 1/6] t: move reftable/stack_test.c to the unit testing framework
Quoted 11 lines · Patrick Steinhardt
Quoted 9 lines · Junio C Hamano
Quoted 6 lines · Chandra Pratap
int
cmd__reftable(int argc, const char **argv){ /* test from simple to complex. */ - stack_test_main(argc, argv); return 0;}
Quoted 8 lines
Quoted 5 lines
This makes
cmd__reftable()a no-op.Even though you cannot remove
t/helpter/test-reftable.c, as it contains the implementation for "test-tool dump-reftable", we should at least be able to do something like this.In addition to Junio's patch you can also remove
"reftable/reftable-tests.h".
Should we get rid of reftable/test_framework.{c, h} as well?
Re: [PATCH v4 1/6] t: move reftable/stack_test.c to the unit testing framework
Quoted 14 lines · Chandra Pratap
Quoted 13 lines · Patrick Steinhardt
Quoted 11 lines · Junio C Hamano
Quoted 6 lines · Chandra Pratap
int
cmd__reftable(int argc, const char **argv){ /* test from simple to complex. */ - stack_test_main(argc, argv); return 0;}
This makes
cmd__reftable()a no-op.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Even though you cannot remove
t/helpter/test-reftable.c, as it contains the implementation for "test-tool dump-reftable", we should at least be able to do something like this.In addition to Junio's patch you can also remove
"reftable/reftable-tests.h".Should we get rid of reftable/test_framework.{c, h} as well?
Ah, yeah. If it's not needed anymore we should remove it. I have a patch series pending anyway where I deduplicate some of the functionality that we have in multiple reftable tests now.
Patrick
Re: [PATCH v4 1/6] t: move reftable/stack_test.c to the unit testing framework
Quoted 7 lines · Patrick Steinhardt
Quoted 1 line · Chandra Pratap
Should we get rid of reftable/test_framework.{c, h} as well?
Ah, yeah. If it's not needed anymore we should remove it. I have a patch series pending anyway where I deduplicate some of the functionality that we have in multiple reftable tests now.
It is unclear if you are saying that Chandra should do the removal as part of an updated version of this series, or you'll do the removal with refactoring in a separate series and Chandra shouldn't worry about it for now.
After reading your message three times, I am leaning to take it as the latter.
Thanks.
Re: [PATCH v4 1/6] t: move reftable/stack_test.c to the unit testing framework
Quoted 10 lines · Junio C Hamano
Quoted 7 lines · Patrick Steinhardt
Quoted 1 line · Chandra Pratap
Should we get rid of reftable/test_framework.{c, h} as well?
Ah, yeah. If it's not needed anymore we should remove it. I have a patch series pending anyway where I deduplicate some of the functionality that we have in multiple reftable tests now.
Quoted 7 lines
It is unclear if you are saying that Chandra should do the removal as part of an updated version of this series, or you'll do the removal with refactoring in a separate series and Chandra shouldn't worry about it for now.
After reading your message three times, I am leaning to take it as the latter.
Ah, sorry for being unclear.
The test framework used to contain some functions that were reused across different reftable tests. During the migration we have instead copied the necessary helpers into each of the migrated tests and thus have a bit of duplication there, which was the right choice during the migration to not have all migrations depend on each other. But now that we are done we should deduplicate them again, and I have a patch series pending that introduces t/unit-tests/lib-reftable.c that deduplicates some of the functions again.
So my intention was to say that this is safe to remove as part of the series we have here, and we do not have to worry about the current duplication we have.
Patrick
PATCH v4 2/6 t: harmonize t-reftable-stack.c with coding guidelines
In reply to Chandra PratapHarmonize 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.
--- t/unit-tests/t-reftable-stack.c | 110 +++++++++++++++----------------- 1 file changed, 53 insertions(+), 57 deletions(-)
t/unit-tests/t-reftable-stack.c +53 -57
@@ -81,7 +81,6 @@ static void t_read_file(void)int n, err;char **names =NULL ;const char *want[] = {"line1" ,"line2" ,"line3" }; -int i =0 ; check_int(fd, >, 0); n =write_in_full (fd, out,strlen (out));
@@ -92,9 +91,8 @@ static void t_read_file(void) err =read_lines (fn, &names);check (!err); -for (i =0 ; names[i]; i++) { +for (size_t i =0 ; names[i]; i++)check_str (want[i], names[i]); - }free_names (names); (void )remove (fn); }
@@ -123,7 +121,7 @@ static void write_n_ref_tables(struct reftable_stack *st, };strbuf_reset (&buf); -strbuf_addf (&buf,"refs/heads/branch-%04u" , (unsigned ) i); +strbuf_addf (&buf,"refs/heads/branch-%04" PRIuMAX, (uintmax_t )i); ref.refname = buf.buf;set_test_hash (ref.value.val1, i);
@@ -164,12 +162,12 @@ static void t_reftable_stack_add_one(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 };struct stat stat_result = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err); err =reftable_stack_read_ref (st, ref.refname, &dest);
@@ -234,16 +232,16 @@ static void t_reftable_stack_uptodate(void) err =reftable_new_stack (&st2, dir, &opts);check (!err); - err =reftable_stack_add (st1, &write_test_ref, &ref1); + err =reftable_stack_add (st1, write_test_ref, &ref1);check (!err); - err =reftable_stack_add (st2, &write_test_ref, &ref2); + err =reftable_stack_add (st2, write_test_ref, &ref2); check_int(err, ==, REFTABLE_OUTDATED_ERROR); err =reftable_stack_reload (st2);check (!err); - err =reftable_stack_add (st2, &write_test_ref, &ref2); + err =reftable_stack_add (st2, write_test_ref, &ref2);check (!err);reftable_stack_destroy (st1);reftable_stack_destroy (st2);
@@ -264,7 +262,7 @@ static void t_reftable_stack_transaction_api(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -274,7 +272,7 @@ static void t_reftable_stack_transaction_api(void) err =reftable_stack_new_addition (&add, st);check (!err); - err =reftable_addition_add (add, &write_test_ref, &ref); + err =reftable_addition_add (add, write_test_ref, &ref);check (!err); err =reftable_addition_commit (add);
@@ -298,12 +296,13 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void)struct reftable_write_options opts = {0 };struct reftable_addition *add =NULL ;struct reftable_stack *st =NULL ; -int i, n =20 , err; +size_t n =20 ; +int err; err =reftable_new_stack (&st, dir, &opts);check (!err); -for (i =0 ; i <= n; i++) { +for (size_t i =0 ; i <= n; i++) {struct reftable_ref_record ref = { .update_index =reftable_stack_next_update_index (st), .value_type =REFTABLE_REF_SYMREF ,
@@ -311,7 +310,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void) };char name[100 ]; -snprintf (name,sizeof (name),"branch%04d" , i); +snprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); ref.refname = name;/*
@@ -324,7 +323,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void) err =reftable_stack_new_addition (&add, st);check (!err); - err =reftable_addition_add (add, &write_test_ref, &ref); + err =reftable_addition_add (add, write_test_ref, &ref);check (!err); err =reftable_addition_commit (add);
@@ -355,7 +354,7 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void) .value_type = REFTABLE_REF_VAL1, .value.val1 = {0x01}, }; -struct reftable_write_options opts = {0 }; +struct reftable_write_options opts = {0 };struct reftable_stack *st;struct strbuf table_path =STRBUF_INIT ;char *dir =get_tmp_dir (__LINE__);
@@ -417,10 +416,10 @@ static void t_reftable_stack_update_index_check(void) err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref1); + err =reftable_stack_add (st, write_test_ref, &ref1);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref2); + err =reftable_stack_add (st, write_test_ref, &ref2); check_int(err, ==, REFTABLE_API_ERROR);reftable_stack_destroy (st);clear_dir (dir);
@@ -436,7 +435,7 @@ static void t_reftable_stack_lock_failure(void) err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =-1 ; i !=REFTABLE_EMPTY_TABLE_ERROR ; i--) { - err =reftable_stack_add (st, &write_error, &i); + err =reftable_stack_add (st, write_error, &i); check_int(err, ==, i); }
@@ -446,7 +445,6 @@ static void t_reftable_stack_lock_failure(void)static void t_reftable_stack_add (void ) { -int i =0 ;int err =0 ;struct reftable_write_options opts = { .exact_log_message =1 ,
@@ -455,18 +453,18 @@ static void t_reftable_stack_add(void) };struct reftable_stack *st =NULL ;char *dir =get_tmp_dir (__LINE__); -struct reftable_ref_record refs[2 ] = { {NULL } }; -struct reftable_log_record logs[2 ] = { {NULL } }; +struct reftable_ref_record refs[2 ] = {0 }; +struct reftable_log_record logs[2 ] = {0 };struct strbuf path =STRBUF_INIT ;struct stat stat_result; -int N =ARRAY_SIZE (refs); +size_t i,N =ARRAY_SIZE (refs); err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =0 ; i <N ; i++) {char buf[256 ]; -snprintf (buf,sizeof (buf),"branch%02d" , i); +snprintf (buf,sizeof (buf),"branch%02" PRIuMAX, (uintmax_t )i); refs[i].refname =xstrdup (buf); refs[i].update_index = i +1 ; refs[i].value_type =REFTABLE_REF_VAL1 ;
@@ -480,7 +478,7 @@ static void t_reftable_stack_add(void) }for (i =0 ; i <N ; i++) { -int err =reftable_stack_add (st, &write_test_ref, &refs[i]); +int err =reftable_stack_add (st, write_test_ref, &refs[i]);check (!err); }
@@ -489,7 +487,7 @@ static void t_reftable_stack_add(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -497,7 +495,7 @@ static void t_reftable_stack_add(void)check (!err);for (i =0 ; i <N ; i++) { -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 };int err =reftable_stack_read_ref (st, refs[i].refname, &dest);check (!err);
@@ -507,7 +505,7 @@ static void t_reftable_stack_add(void) }for (i =0 ; i <N ; i++) { -struct reftable_log_record dest = {NULL }; +struct reftable_log_record dest = {0 };int err =reftable_stack_read_log (st, refs[i].refname, &dest);check (!err); check(reftable_log_record_equal(&dest, logs + i,
@@ -575,11 +573,11 @@ static void t_reftable_stack_log_normalize(void)check (!err); input.value.update.message = (char *)"one\ntwo" ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg); check_int(err, ==, REFTABLE_API_ERROR); input.value.update.message = (char *)"one" ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg);check (!err); err =reftable_stack_read_log (st, input.refname, &dest);
@@ -588,7 +586,7 @@ static void t_reftable_stack_log_normalize(void) input.value.update.message = (char *)"two\n" ; arg.update_index =2 ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg);check (!err); err =reftable_stack_read_log (st, input.refname, &dest);check (!err);
@@ -602,16 +600,15 @@ static void t_reftable_stack_log_normalize(void)static void t_reftable_stack_tombstone (void ) { -int i =0 ;char *dir =get_tmp_dir (__LINE__);struct reftable_write_options opts = {0 };struct reftable_stack *st =NULL ;int err; -struct reftable_ref_record refs[2 ] = { {NULL } }; -struct reftable_log_record logs[2 ] = { {NULL } }; -int N =ARRAY_SIZE (refs); -struct reftable_ref_record dest = {NULL }; -struct reftable_log_record log_dest = {NULL }; +struct reftable_ref_record refs[2 ] = {0 }; +struct reftable_log_record logs[2 ] = {0 }; +size_t i,N =ARRAY_SIZE (refs); +struct reftable_ref_record dest = {0 }; +struct reftable_log_record log_dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -637,7 +634,7 @@ static void t_reftable_stack_tombstone(void) } }for (i =0 ; i <N ; i++) { -int err =reftable_stack_add (st, &write_test_ref, &refs[i]); +int err =reftable_stack_add (st, write_test_ref, &refs[i]);check (!err); }
@@ -646,7 +643,7 @@ static void t_reftable_stack_tombstone(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -695,12 +692,12 @@ static void t_reftable_stack_hash_id(void)struct reftable_stack *st32 =NULL ;struct reftable_write_options opts_default = {0 };struct reftable_stack *st_default =NULL ; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err);/* can't read it with the wrong hash ID. */
@@ -743,21 +740,20 @@ static void t_reflog_expire(void)char *dir =get_tmp_dir (__LINE__);struct reftable_write_options opts = {0 };struct reftable_stack *st =NULL ; -struct reftable_log_record logs[20 ] = { {NULL } }; -int N =ARRAY_SIZE (logs) -1 ; -int i =0 ; +struct reftable_log_record logs[20 ] = {0 }; +size_t i,N =ARRAY_SIZE (logs) -1 ;int err;struct reftable_log_expiry_config expiry = { .time =10 , }; -struct reftable_log_record log = {NULL }; +struct reftable_log_record log = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =1 ; i <=N ; i++) {char buf[256 ]; -snprintf (buf,sizeof (buf),"branch%02d" , i); +snprintf (buf,sizeof (buf),"branch%02" PRIuMAX, (uintmax_t )i); logs[i].refname =xstrdup (buf); logs[i].update_index = i;
@@ -772,7 +768,7 @@ static void t_reflog_expire(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -800,9 +796,8 @@ static void t_reflog_expire(void)/* cleanup */ reftable_stack_destroy (st); -for (i =0 ; i <=N ; i++) { +for (i =0 ; i <=N ; i++)reftable_log_record_release (&logs[i]); - }clear_dir (dir);reftable_log_record_release (&log); }
@@ -824,7 +819,7 @@ static void t_empty_add(void) err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_nothing,NULL ); + err =reftable_stack_add (st, write_nothing,NULL );check (!err); err =reftable_new_stack (&st2, dir, &opts);
@@ -851,8 +846,8 @@ static void t_reftable_stack_auto_compaction(void) };struct reftable_stack *st =NULL ;char *dir =get_tmp_dir (__LINE__); -int err, i; -int N =100 ; +int err; +size_t i,N =100 ; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -865,9 +860,9 @@ static void t_reftable_stack_auto_compaction(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -snprintf (name,sizeof (name),"branch%04d" , i); +snprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err); err =reftable_stack_auto_compact (st);
@@ -929,7 +924,8 @@ static void t_reftable_stack_add_performs_auto_compaction(void)struct reftable_stack *st =NULL ;struct strbuf refname =STRBUF_INIT ;char *dir =get_tmp_dir (__LINE__); -int err, i, n =20 ; +int err; +size_t i, n =20 ; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -949,10 +945,10 @@ static void t_reftable_stack_add_performs_auto_compaction(void) st->opts.disable_auto_compact = i != n;strbuf_reset (&refname); -strbuf_addf (&refname,"branch-%04d" , i); +strbuf_addf (&refname,"branch-%04" PRIuMAX, (uintmax_t )i); ref.refname = refname.buf; - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err);/*
Signature · 1 line
2.45.GIT
PATCH v4 3/6 t-reftable-stack: use Git's tempfile API instead of mkstemp()
In reply to Chandra PratapGit's tempfile API defined by $GIT_DIR/tempfile.{c,h} provides a unified interface for tempfile operations. Since reftable/stack.c uses this API for all its tempfile needs instead of raw functions like mkstemp(), make the ported stack test strictly use Git's tempfile API as well.
--- t/unit-tests/t-reftable-stack.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
t/unit-tests/t-reftable-stack.c +3 -1
@@ -76,7 +76,8 @@ static char *get_tmp_dir(int linenumber)static void t_read_file (void ) {char *fn =get_tmp_template (__LINE__); -int fd =mkstemp (fn); +struct tempfile *tmp =mks_tempfile (fn); +int fd =get_tempfile_fd (tmp);char out[1024 ] ="line1\n\nline2\nline3" ;int n, err;char **names =NULL ;
@@ -95,6 +96,7 @@ static void t_read_file(void)check_str (want[i], names[i]);free_names (names); (void )remove (fn); +delete_tempfile (&tmp); }static int write_test_ref (struct reftable_writer *wr,void *arg)
Signature · 1 line
2.45.GIT
Re: [PATCH v4 3/6] t-reftable-stack: use Git's tempfile API instead of mkstemp()
Quoted 5 lines · Chandra Pratap
Git's tempfile API defined by $GIT_DIR/tempfile.{c,h} provides a unified interface for tempfile operations. Since
reftable/stack.cuses this API for all its tempfile needs instead of raw functions likemkstemp(), make the ported stack test strictly use Git's tempfile API as well.
I don't quite think that this is sufficient motivation. The real benefit is that we know to clean up the tempfile in case the test fails because it gets registered and pruned via a signal handler.
Patrick
Quoted 32 lines
Mentored-by Patrick Steinhardt <[email protected]>Mentored-by Christian Couder <[email protected]>Signed-off-by Chandra Pratap <[email protected]>--- t/unit-tests/t-reftable-stack.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)t/unit-tests/t-reftable-stack.c +3
-1@@ -76,7 +76,8 @@ static char *get_tmp_dir(int linenumber)static void t_read_file (void ) {char *fn =get_tmp_template (__LINE__); -int fd =mkstemp (fn); +struct tempfile *tmp =mks_tempfile (fn); +int fd =get_tempfile_fd (tmp);char out[1024 ] ="line1\n\nline2\nline3" ;int n, err;char **names =NULL ;@@ -95,6 +96,7 @@ static void t_read_file(void)check_str (want[i], names[i]);free_names (names); (void )remove (fn); +delete_tempfile (&tmp); }static int write_test_ref (struct reftable_writer *wr,void *arg)Signature · 1 line
2.45.GIT
PATCH v4 4/6 t-reftable-stack: use reftable_ref_record_equal() to compare ref records
In reply to Chandra PratapIn the current stack tests, ref records are compared for equality by sometimes using the dedicated function for ref-record comparison, reftable_ref_record_equal(), and sometimes by explicity comparing contents of the ref records.
Replace the latter instances of ref-record comparison with the former to maintain uniformity throughout the test file.
--- t/unit-tests/t-reftable-stack.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
t/unit-tests/t-reftable-stack.c +2 -2
@@ -174,7 +174,7 @@ static void t_reftable_stack_add_one(void) err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ )); check_int(st->readers_len, >, 0);#ifndef GIT_WINDOWS_NATIVE
@@ -285,7 +285,7 @@ static void t_reftable_stack_transaction_api(void) err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); check_int(REFTABLE_REF_SYMREF, ==, dest.value_type); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ ));reftable_ref_record_release (&dest);reftable_stack_destroy (st);
Signature · 1 line
2.45.GIT
Re: [PATCH v4 4/6] t-reftable-stack: use reftable_ref_record_equal() to compare ref records
Quoted 7 lines · Chandra Pratap
In the current stack tests, ref records are compared for equality by sometimes using the dedicated function for ref-record comparison,
reftable_ref_record_equal(), and sometimes by explicity comparing contents of the ref records.Replace the latter instances of ref-record comparison with the former to maintain uniformity throughout the test file.
It's not really about uniformity in my opinion. It's rather that the function is a lot more thorough than only comparing the refname, which I think is the real win here.
Patrick
Quoted 32 lines
Mentored-by Patrick Steinhardt <[email protected]>Mentored-by Christian Couder <[email protected]>Signed-off-by Chandra Pratap <[email protected]>--- t/unit-tests/t-reftable-stack.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)t/unit-tests/t-reftable-stack.c +2
-2@@ -174,7 +174,7 @@ static void t_reftable_stack_add_one(void) err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ )); check_int(st->readers_len, >, 0);#ifndef GIT_WINDOWS_NATIVE @@ -285,7 +285,7 @@ static void t_reftable_stack_transaction_api(void) err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); check_int(REFTABLE_REF_SYMREF, ==, dest.value_type); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ ));reftable_ref_record_release (&dest);reftable_stack_destroy (st);Signature · 1 line
2.45.GIT
PATCH v4 5/6 t-reftable-stack: add test for non-default compaction factor
In reply to Chandra PratapIn a recent codebase update (commit ae8e378430, merge branch 'ps/reftable-write-options', 2024/05/13) the geometric factor used in auto-compaction of reftable tables was made configurable. Add a test to verify the functionality introduced by this update.
--- t/unit-tests/t-reftable-stack.c | 41 +++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-)
t/unit-tests/t-reftable-stack.c +37 -4
@@ -831,12 +831,12 @@ static void t_empty_add(void)reftable_stack_destroy (st2); } -static int fastlog2 (uint64_t sz) +static int fastlogN (uint64_t sz,uint64_t N ) {int l =0 ;if (sz ==0 )return 0 ; -for (; sz; sz /=2 ) +for (; sz; sz /=N ) l++;return l -1 ; }
@@ -869,11 +869,43 @@ static void t_reftable_stack_auto_compaction(void) err =reftable_stack_auto_compact (st);check (!err); -check (i <3 || st->merged->readers_len <2 *fastlog2 (i)); +check (i <2 || st->merged->readers_len <2 *fastlogN (i,2 )); } check_int(reftable_stack_compaction_stats(st)->entries_written, <, - (uint64_t )(N *fastlog2 (N ))); + (uint64_t )(N *fastlogN (N ,2 ))); + +reftable_stack_destroy (st); +clear_dir (dir); +} + +static void t_reftable_stack_auto_compaction_factor (void ) +{ +struct reftable_write_options opts = { + .auto_compaction_factor =5 , + }; +struct reftable_stack *st =NULL ; +char *dir =get_tmp_dir (__LINE__); +int err; +size_t N =100 ; + + err =reftable_new_stack (&st, dir, &opts); +check (!err); + +for (size_t i =0 ; i <N ; i++) { +char name[20 ]; +struct reftable_ref_record ref = { + .refname = name, + .update_index =reftable_stack_next_update_index (st), + .value_type =REFTABLE_REF_VAL1 , + }; +xsnprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); + + err =reftable_stack_add (st, &write_test_ref, &ref); +check (!err); + +check (i <5 || st->merged->readers_len <5 *fastlogN (i,5 )); + }reftable_stack_destroy (st);clear_dir (dir);
@@ -1186,6 +1218,7 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)TEST (t_reftable_stack_add_one (),"add a single ref record to stack" );TEST (t_reftable_stack_add_performs_auto_compaction (),"addition to stack triggers auto-compaction" );TEST (t_reftable_stack_auto_compaction (),"stack must form geometric sequence after compaction" ); +TEST (t_reftable_stack_auto_compaction_factor (),"auto-compaction with non-default geometric factor" );TEST (t_reftable_stack_auto_compaction_fails_gracefully (),"failure on auto-compaction" );TEST (t_reftable_stack_auto_compaction_with_locked_tables (),"auto compaction with locked tables" );TEST (t_reftable_stack_compaction_concurrent (),"compaction with concurrent stack" );
Signature · 1 line
2.45.GIT
PATCH v4 6/6 t-reftable-stack: add test for stack iterators
In reply to Chandra Pratapreftable_stack_init_ref_iterator and reftable_stack_init_log_iterator as defined by reftable/stack.{c,h} initialize a stack iterator to iterate over the ref and log records in a reftable stack respectively. Since these functions are not exercised by any of the existing tests, add a test for them.
--- t/unit-tests/t-reftable-stack.c | 80 +++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+)
t/unit-tests/t-reftable-stack.c +80 -0
@@ -544,6 +544,85 @@ static void t_reftable_stack_add(void)clear_dir (dir); } +static void t_reftable_stack_iterator (void ) +{ +struct reftable_write_options opts = {0 }; +struct reftable_stack *st =NULL ; +char *dir =get_tmp_dir (__LINE__); +struct reftable_ref_record refs[10 ] = {0 }; +struct reftable_log_record logs[10 ] = {0 }; +struct reftable_iterator it = {0 }; +size_t N =ARRAY_SIZE (refs), i; +int err; + + err =reftable_new_stack (&st, dir, &opts); +check (!err); + +for (i =0 ; i <N ; i++) { + refs[i].refname =xstrfmt ("branch%02" PRIuMAX, (uintmax_t )i); + refs[i].update_index = i +1 ; + refs[i].value_type =REFTABLE_REF_VAL1 ; +set_test_hash (refs[i].value.val1, i); + + logs[i].refname =xstrfmt ("branch%02" PRIuMAX, (uintmax_t )i); + logs[i].update_index = i +1 ; + logs[i].value_type =REFTABLE_LOG_UPDATE ; + logs[i].value.update.email =xstrdup ("johndoe@invalid" ); + logs[i].value.update.message =xstrdup ("commit\n" ); +set_test_hash (logs[i].value.update.new_hash, i); + } + +for (i =0 ; i <N ; i++) { + err =reftable_stack_add (st, write_test_ref, &refs[i]); +check (!err); + } + +for (i =0 ; i <N ; i++) { +struct write_log_arg arg = { + .log = &logs[i], + .update_index =reftable_stack_next_update_index (st), + }; + err =reftable_stack_add (st, write_test_log, &arg); +check (!err); + } + +reftable_stack_init_ref_iterator (st, &it); +reftable_iterator_seek_ref (&it, refs[0 ].refname); +for (i =0 ; ; i++) { +struct reftable_ref_record ref = {0 }; + err =reftable_iterator_next_ref (&it, &ref); +if (err >0 ) +break ; +check (!err); +check (reftable_ref_record_equal (&ref, &refs[i],GIT_SHA1_RAWSZ )); +reftable_ref_record_release (&ref); + } + check_int(i, ==, N); + +reftable_iterator_destroy (&it); + +reftable_stack_init_log_iterator (st, &it); +reftable_iterator_seek_log (&it, logs[0 ].refname); +for (i =0 ; ; i++) { +struct reftable_log_record log = {0 }; + err =reftable_iterator_next_log (&it, &log); +if (err >0 ) +break ; +check (!err); +check (reftable_log_record_equal (&log, &logs[i],GIT_SHA1_RAWSZ )); +reftable_log_record_release (&log); + } + check_int(i, ==, N); + +reftable_stack_destroy (st); +reftable_iterator_destroy (&it); +for (i =0 ; i <N ; i++) { +reftable_ref_record_release (&refs[i]); +reftable_log_record_release (&logs[i]); + } +clear_dir (dir); +} +static void t_reftable_stack_log_normalize (void ) {int err =0 ;
@@ -1225,6 +1304,7 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)TEST (t_reftable_stack_compaction_concurrent_clean (),"compaction with unclean stack shutdown" );TEST (t_reftable_stack_compaction_with_locked_tables (),"compaction with locked tables" );TEST (t_reftable_stack_hash_id (),"read stack with wrong hash ID" ); +TEST (t_reftable_stack_iterator (),"log and ref iterator for reftable stack" );TEST (t_reftable_stack_lock_failure (),"stack addition with lockfile failure" );TEST (t_reftable_stack_log_normalize (),"log messages should be normalized" );TEST (t_reftable_stack_read_across_reload (),"stack iterators work across reloads" );
Signature · 1 line
2.45.GIT
Re: [PATCH v4 6/6] t-reftable-stack: add test for stack iterators
Quoted 49 lines · Chandra Pratap
reftable_stack_init_ref_iteratorandreftable_stack_init_log_iteratoras defined by reftable/stack.{c,h} initialize a stack iterator to iterate over the ref and log records in a reftable stack respectively. Since these functions are not exercised by any of the existing tests, add a test for them.Mentored-by Patrick Steinhardt <[email protected]>Mentored-by Christian Couder <[email protected]>Signed-off-by Chandra Pratap <[email protected]>--- t/unit-tests/t-reftable-stack.c | 80 +++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+)t/unit-tests/t-reftable-stack.c +28
-0@@ -544,6 +544,85 @@ static void t_reftable_stack_add(void)clear_dir (dir); } +static void t_reftable_stack_iterator (void ) +{ +struct reftable_write_options opts = {0 }; +struct reftable_stack *st =NULL ; +char *dir =get_tmp_dir (__LINE__); +struct reftable_ref_record refs[10 ] = {0 }; +struct reftable_log_record logs[10 ] = {0 }; +struct reftable_iterator it = {0 }; +size_t N =ARRAY_SIZE (refs), i; +int err; + + err =reftable_new_stack (&st, dir, &opts); +check (!err); + +for (i =0 ; i <N ; i++) { + refs[i].refname =xstrfmt ("branch%02" PRIuMAX, (uintmax_t )i); + refs[i].update_index = i +1 ; + refs[i].value_type =REFTABLE_REF_VAL1 ; +set_test_hash (refs[i].value.val1, i); + + logs[i].refname =xstrfmt ("branch%02" PRIuMAX, (uintmax_t )i); + logs[i].update_index = i +1 ; + logs[i].value_type =REFTABLE_LOG_UPDATE ; + logs[i].value.update.email =xstrdup ("johndoe@invalid" ); + logs[i].value.update.message =xstrdup ("commit\n" ); +set_test_hash (logs[i].value.update.new_hash, i); + } +
t/unit-tests/t-reftable-stack.c static void t_reftable_stack_add(void) · line 584
Quoted 10 lines
t/unit-tests/t-reftable-stack.c +10
-0+for (i =0 ; i <N ; i++) { + err =reftable_stack_add (st, write_test_ref, &refs[i]); +check (!err); + } + +for (i =0 ; i <N ; i++) { +struct write_log_arg arg = { + .log = &logs[i], + .update_index =reftable_stack_next_update_index (st), + };
Nit: Let's add a newline between the variable declarations and the code, both here and in the other loops further down.
Patrick
v5 cover t: port reftable/stack_test.c to the unit testing framework
In reply to Chandra PratapThe reftable library comes with self tests, which are exercised as part of the usual end-to-end tests and are designed to observe the end-user visible effects of Git commands. What it exercises, however, is a better match for the unit-testing framework, merged at 8bf6fbd0 (Merge branch 'js/doc-unit-tests', 2023-12-09), which is designed to observe how low level implementation details, at the level of sequences of individual function calls, behave.
Hence, port reftable/stack_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.
---
Changes in v5:
- Edit the commit messages in patches 3 and 4 to reflect the changes and the motivation behind those changes better.
- Add newlines after variable declarations in patch 6.
- Introduce patch 7 which removes leftover cruft from the previous reftable testing scheme.
CI/PR: https://github.com/gitgitgadget/git/pull/1762
Chandra Pratap(7):
t: move reftable/stack_test.c to the unit testing framework
t: harmonize t-reftable-stack.c with coding guidelines
t-reftable-stack: use Git's tempfile API instead of mkstemp()
t-reftable-stack: use reftable_ref_record_equal() to compare ref records
t-reftable-stack: add test for non-default compaction factor
t-reftable-stack: add test for stack iterators
t: clean up leftover reftable test cruft
Makefile | 4 +- reftable/reftable-tests.h | 14 - reftable/test_framework.c | 27 - reftable/test_framework.h | 61 -- t/helper/test-reftable.c | 8 - t/helper/test-tool.c | 3 +- t/helper/test-tool.h | 1 - t/t0032-reftable-unittest.sh | 16 - .../unit-tests/t-reftable-stack.c | 665 ++++++++++++--------- 9 files changed, 390 insertions(+), 409 deletions(-)
Range-diff against v4:
1: eae681f53d ! 1: ca4b00feef t-reftable-stack: use Git's tempfile API instead of mkstemp()
@@ Commit message
like mkstemp(), make the ported stack test strictly use Git's tempfile API as well.
+ A bigger benefit is the fact that we know to clean up the tempfile
+ in case the test fails because it gets registered and pruned via a
+ signal handler.
+
2: 19b2f41003 ! 2: 3e723667dd t-reftable-stack: use reftable_ref_record_equal() to compare ref records
@@ Commit message
reftable_ref_record_equal(), and sometimes by explicity comparing
contents of the ref records.
- Replace the latter instances of ref-record comparison with the
- former to maintain uniformity throughout the test file.
+ The latter method is undesired because there can exist unequal ref
+ records with the some of the contents being equal. Replace the latter
+ instances of ref-record comparison with the former. This has the
+ added benefit of preserving uniformity throughout the test file.
3: ffdfba6b98 = 3: 7526550c92 t-reftable-stack: add test for non-default compaction factor
4: ca447664e7 ! 4: 05e4b7e715 t-reftable-stack: add test for stack iterators
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add(void)
+ .log = &logs[i],
+ .update_index = reftable_stack_next_update_index(st),
+ };
++
+ err = reftable_stack_add(st, write_test_log, &arg);
+ check(!err);
+ }
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add(void)
+ reftable_iterator_seek_ref(&it, refs[0].refname);
+ for (i = 0; ; i++) {
+ struct reftable_ref_record ref = { 0 };
++
+ err = reftable_iterator_next_ref(&it, &ref);
+ if (err > 0)
+ break;
@@ t/unit-tests/t-reftable-stack.c: static void t_reftable_stack_add(void)
+ reftable_iterator_seek_log(&it, logs[0].refname);
+ for (i = 0; ; i++) {
+ struct reftable_log_record log = { 0 };
++
+ err = reftable_iterator_next_log(&it, &log);
+ if (err > 0)
+ break;-: ---------- > 5: 43c1a522fb t: clean up leftover reftable test cruft
PATCH v5 1/7 t: move reftable/stack_test.c to the unit testing framework
reftable/stack_test.c exercises the functions defined in reftable/stack.{c, h}. Migrate reftable/stack_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework and renaming the tests to be in-line with unit-tests' standards.
Since some of the tests use set_test_hash() defined by reftable/test_framework.{c, h} but these files are not '#included' in the test file, copy this function in the ported test file.
--- Makefile | 2 +- reftable/reftable-tests.h | 1 - t/helper/test-reftable.c | 1 - .../unit-tests/t-reftable-stack.c | 429 +++++++++--------- 4 files changed, 214 insertions(+), 219 deletions(-) rename reftable/stack_test.c => t/unit-tests/t-reftable-stack.c (76%)
Makefile +1 -1
@@ -1349,6 +1349,7 @@ UNIT_TEST_PROGRAMS += t-reftable-merged UNIT_TEST_PROGRAMS += t-reftable-pq UNIT_TEST_PROGRAMS += t-reftable-readwrite UNIT_TEST_PROGRAMS += t-reftable-record +UNIT_TEST_PROGRAMS += t-reftable-stack UNIT_TEST_PROGRAMS += t-reftable-tree UNIT_TEST_PROGRAMS += t-strbuf UNIT_TEST_PROGRAMS += t-strcmp-offset
@@ -2691,7 +2692,6 @@ REFTABLE_OBJS += reftable/stack.o REFTABLE_OBJS += reftable/tree.o REFTABLE_OBJS += reftable/writer.o -REFTABLE_TEST_OBJS += reftable/stack_test.o REFTABLE_TEST_OBJS += reftable/test_framework.o TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS)) $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS))
reftable/reftable-tests.h +0 -1
@@ -9,6 +9,5 @@ license that can be found in the LICENSE file or at#ifndef REFTABLE_TESTS_H #define REFTABLE_TESTS_H -int stack_test_main (int argc,const char **argv);#endif
t/helper/test-reftable.c +0 -1
@@ -12,7 +12,6 @@int cmd__reftable (int argc,const char **argv) {/* test from simple to complex. */ -stack_test_main (argc, argv);return 0 ; }
t/unit-tests/t-reftable-stack.c renamed from reftable/stack_test.c +213 -216
1,006 lines not on this page · raw message
Signature · 1 line
2.45.GIT
PATCH v5 2/7 t: harmonize t-reftable-stack.c with coding guidelines
In reply to Chandra PratapHarmonize 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.
--- t/unit-tests/t-reftable-stack.c | 110 +++++++++++++++----------------- 1 file changed, 53 insertions(+), 57 deletions(-)
t/unit-tests/t-reftable-stack.c +53 -57
@@ -81,7 +81,6 @@ static void t_read_file(void)int n, err;char **names =NULL ;const char *want[] = {"line1" ,"line2" ,"line3" }; -int i =0 ; check_int(fd, >, 0); n =write_in_full (fd, out,strlen (out));
@@ -92,9 +91,8 @@ static void t_read_file(void) err =read_lines (fn, &names);check (!err); -for (i =0 ; names[i]; i++) { +for (size_t i =0 ; names[i]; i++)check_str (want[i], names[i]); - }free_names (names); (void )remove (fn); }
@@ -123,7 +121,7 @@ static void write_n_ref_tables(struct reftable_stack *st, };strbuf_reset (&buf); -strbuf_addf (&buf,"refs/heads/branch-%04u" , (unsigned ) i); +strbuf_addf (&buf,"refs/heads/branch-%04" PRIuMAX, (uintmax_t )i); ref.refname = buf.buf;set_test_hash (ref.value.val1, i);
@@ -164,12 +162,12 @@ static void t_reftable_stack_add_one(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 };struct stat stat_result = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err); err =reftable_stack_read_ref (st, ref.refname, &dest);
@@ -234,16 +232,16 @@ static void t_reftable_stack_uptodate(void) err =reftable_new_stack (&st2, dir, &opts);check (!err); - err =reftable_stack_add (st1, &write_test_ref, &ref1); + err =reftable_stack_add (st1, write_test_ref, &ref1);check (!err); - err =reftable_stack_add (st2, &write_test_ref, &ref2); + err =reftable_stack_add (st2, write_test_ref, &ref2); check_int(err, ==, REFTABLE_OUTDATED_ERROR); err =reftable_stack_reload (st2);check (!err); - err =reftable_stack_add (st2, &write_test_ref, &ref2); + err =reftable_stack_add (st2, write_test_ref, &ref2);check (!err);reftable_stack_destroy (st1);reftable_stack_destroy (st2);
@@ -264,7 +262,7 @@ static void t_reftable_stack_transaction_api(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -274,7 +272,7 @@ static void t_reftable_stack_transaction_api(void) err =reftable_stack_new_addition (&add, st);check (!err); - err =reftable_addition_add (add, &write_test_ref, &ref); + err =reftable_addition_add (add, write_test_ref, &ref);check (!err); err =reftable_addition_commit (add);
@@ -298,12 +296,13 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void)struct reftable_write_options opts = {0 };struct reftable_addition *add =NULL ;struct reftable_stack *st =NULL ; -int i, n =20 , err; +size_t n =20 ; +int err; err =reftable_new_stack (&st, dir, &opts);check (!err); -for (i =0 ; i <= n; i++) { +for (size_t i =0 ; i <= n; i++) {struct reftable_ref_record ref = { .update_index =reftable_stack_next_update_index (st), .value_type =REFTABLE_REF_SYMREF ,
@@ -311,7 +310,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void) };char name[100 ]; -snprintf (name,sizeof (name),"branch%04d" , i); +snprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); ref.refname = name;/*
@@ -324,7 +323,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void) err =reftable_stack_new_addition (&add, st);check (!err); - err =reftable_addition_add (add, &write_test_ref, &ref); + err =reftable_addition_add (add, write_test_ref, &ref);check (!err); err =reftable_addition_commit (add);
@@ -355,7 +354,7 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void) .value_type = REFTABLE_REF_VAL1, .value.val1 = {0x01}, }; -struct reftable_write_options opts = {0 }; +struct reftable_write_options opts = {0 };struct reftable_stack *st;struct strbuf table_path =STRBUF_INIT ;char *dir =get_tmp_dir (__LINE__);
@@ -417,10 +416,10 @@ static void t_reftable_stack_update_index_check(void) err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref1); + err =reftable_stack_add (st, write_test_ref, &ref1);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref2); + err =reftable_stack_add (st, write_test_ref, &ref2); check_int(err, ==, REFTABLE_API_ERROR);reftable_stack_destroy (st);clear_dir (dir);
@@ -436,7 +435,7 @@ static void t_reftable_stack_lock_failure(void) err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =-1 ; i !=REFTABLE_EMPTY_TABLE_ERROR ; i--) { - err =reftable_stack_add (st, &write_error, &i); + err =reftable_stack_add (st, write_error, &i); check_int(err, ==, i); }
@@ -446,7 +445,6 @@ static void t_reftable_stack_lock_failure(void)static void t_reftable_stack_add (void ) { -int i =0 ;int err =0 ;struct reftable_write_options opts = { .exact_log_message =1 ,
@@ -455,18 +453,18 @@ static void t_reftable_stack_add(void) };struct reftable_stack *st =NULL ;char *dir =get_tmp_dir (__LINE__); -struct reftable_ref_record refs[2 ] = { {NULL } }; -struct reftable_log_record logs[2 ] = { {NULL } }; +struct reftable_ref_record refs[2 ] = {0 }; +struct reftable_log_record logs[2 ] = {0 };struct strbuf path =STRBUF_INIT ;struct stat stat_result; -int N =ARRAY_SIZE (refs); +size_t i,N =ARRAY_SIZE (refs); err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =0 ; i <N ; i++) {char buf[256 ]; -snprintf (buf,sizeof (buf),"branch%02d" , i); +snprintf (buf,sizeof (buf),"branch%02" PRIuMAX, (uintmax_t )i); refs[i].refname =xstrdup (buf); refs[i].update_index = i +1 ; refs[i].value_type =REFTABLE_REF_VAL1 ;
@@ -480,7 +478,7 @@ static void t_reftable_stack_add(void) }for (i =0 ; i <N ; i++) { -int err =reftable_stack_add (st, &write_test_ref, &refs[i]); +int err =reftable_stack_add (st, write_test_ref, &refs[i]);check (!err); }
@@ -489,7 +487,7 @@ static void t_reftable_stack_add(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -497,7 +495,7 @@ static void t_reftable_stack_add(void)check (!err);for (i =0 ; i <N ; i++) { -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 };int err =reftable_stack_read_ref (st, refs[i].refname, &dest);check (!err);
@@ -507,7 +505,7 @@ static void t_reftable_stack_add(void) }for (i =0 ; i <N ; i++) { -struct reftable_log_record dest = {NULL }; +struct reftable_log_record dest = {0 };int err =reftable_stack_read_log (st, refs[i].refname, &dest);check (!err); check(reftable_log_record_equal(&dest, logs + i,
@@ -575,11 +573,11 @@ static void t_reftable_stack_log_normalize(void)check (!err); input.value.update.message = (char *)"one\ntwo" ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg); check_int(err, ==, REFTABLE_API_ERROR); input.value.update.message = (char *)"one" ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg);check (!err); err =reftable_stack_read_log (st, input.refname, &dest);
@@ -588,7 +586,7 @@ static void t_reftable_stack_log_normalize(void) input.value.update.message = (char *)"two\n" ; arg.update_index =2 ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg);check (!err); err =reftable_stack_read_log (st, input.refname, &dest);check (!err);
@@ -602,16 +600,15 @@ static void t_reftable_stack_log_normalize(void)static void t_reftable_stack_tombstone (void ) { -int i =0 ;char *dir =get_tmp_dir (__LINE__);struct reftable_write_options opts = {0 };struct reftable_stack *st =NULL ;int err; -struct reftable_ref_record refs[2 ] = { {NULL } }; -struct reftable_log_record logs[2 ] = { {NULL } }; -int N =ARRAY_SIZE (refs); -struct reftable_ref_record dest = {NULL }; -struct reftable_log_record log_dest = {NULL }; +struct reftable_ref_record refs[2 ] = {0 }; +struct reftable_log_record logs[2 ] = {0 }; +size_t i,N =ARRAY_SIZE (refs); +struct reftable_ref_record dest = {0 }; +struct reftable_log_record log_dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -637,7 +634,7 @@ static void t_reftable_stack_tombstone(void) } }for (i =0 ; i <N ; i++) { -int err =reftable_stack_add (st, &write_test_ref, &refs[i]); +int err =reftable_stack_add (st, write_test_ref, &refs[i]);check (!err); }
@@ -646,7 +643,7 @@ static void t_reftable_stack_tombstone(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -695,12 +692,12 @@ static void t_reftable_stack_hash_id(void)struct reftable_stack *st32 =NULL ;struct reftable_write_options opts_default = {0 };struct reftable_stack *st_default =NULL ; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err);/* can't read it with the wrong hash ID. */
@@ -743,21 +740,20 @@ static void t_reflog_expire(void)char *dir =get_tmp_dir (__LINE__);struct reftable_write_options opts = {0 };struct reftable_stack *st =NULL ; -struct reftable_log_record logs[20 ] = { {NULL } }; -int N =ARRAY_SIZE (logs) -1 ; -int i =0 ; +struct reftable_log_record logs[20 ] = {0 }; +size_t i,N =ARRAY_SIZE (logs) -1 ;int err;struct reftable_log_expiry_config expiry = { .time =10 , }; -struct reftable_log_record log = {NULL }; +struct reftable_log_record log = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =1 ; i <=N ; i++) {char buf[256 ]; -snprintf (buf,sizeof (buf),"branch%02d" , i); +snprintf (buf,sizeof (buf),"branch%02" PRIuMAX, (uintmax_t )i); logs[i].refname =xstrdup (buf); logs[i].update_index = i;
@@ -772,7 +768,7 @@ static void t_reflog_expire(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -800,9 +796,8 @@ static void t_reflog_expire(void)/* cleanup */ reftable_stack_destroy (st); -for (i =0 ; i <=N ; i++) { +for (i =0 ; i <=N ; i++)reftable_log_record_release (&logs[i]); - }clear_dir (dir);reftable_log_record_release (&log); }
@@ -824,7 +819,7 @@ static void t_empty_add(void) err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_nothing,NULL ); + err =reftable_stack_add (st, write_nothing,NULL );check (!err); err =reftable_new_stack (&st2, dir, &opts);
@@ -851,8 +846,8 @@ static void t_reftable_stack_auto_compaction(void) };struct reftable_stack *st =NULL ;char *dir =get_tmp_dir (__LINE__); -int err, i; -int N =100 ; +int err; +size_t i,N =100 ; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -865,9 +860,9 @@ static void t_reftable_stack_auto_compaction(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -snprintf (name,sizeof (name),"branch%04d" , i); +snprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err); err =reftable_stack_auto_compact (st);
@@ -929,7 +924,8 @@ static void t_reftable_stack_add_performs_auto_compaction(void)struct reftable_stack *st =NULL ;struct strbuf refname =STRBUF_INIT ;char *dir =get_tmp_dir (__LINE__); -int err, i, n =20 ; +int err; +size_t i, n =20 ; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -949,10 +945,10 @@ static void t_reftable_stack_add_performs_auto_compaction(void) st->opts.disable_auto_compact = i != n;strbuf_reset (&refname); -strbuf_addf (&refname,"branch-%04d" , i); +strbuf_addf (&refname,"branch-%04" PRIuMAX, (uintmax_t )i); ref.refname = refname.buf; - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err);/*
Signature · 1 line
2.45.GIT
PATCH v5 3/7 t-reftable-stack: use Git's tempfile API instead of mkstemp()
In reply to Chandra PratapGit's tempfile API defined by $GIT_DIR/tempfile.{c,h} provides a unified interface for tempfile operations. Since reftable/stack.c uses this API for all its tempfile needs instead of raw functions like mkstemp(), make the ported stack test strictly use Git's tempfile API as well.
A bigger benefit is the fact that we know to clean up the tempfile in case the test fails because it gets registered and pruned via a signal handler.
--- t/unit-tests/t-reftable-stack.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
t/unit-tests/t-reftable-stack.c +3 -1
@@ -76,7 +76,8 @@ static char *get_tmp_dir(int linenumber)static void t_read_file (void ) {char *fn =get_tmp_template (__LINE__); -int fd =mkstemp (fn); +struct tempfile *tmp =mks_tempfile (fn); +int fd =get_tempfile_fd (tmp);char out[1024 ] ="line1\n\nline2\nline3" ;int n, err;char **names =NULL ;
@@ -95,6 +96,7 @@ static void t_read_file(void)check_str (want[i], names[i]);free_names (names); (void )remove (fn); +delete_tempfile (&tmp); }static int write_test_ref (struct reftable_writer *wr,void *arg)
Signature · 1 line
2.45.GIT
PATCH v5 4/7 t-reftable-stack: use reftable_ref_record_equal() to compare ref records
In reply to Chandra PratapIn the current stack tests, ref records are compared for equality by sometimes using the dedicated function for ref-record comparison, reftable_ref_record_equal(), and sometimes by explicity comparing contents of the ref records.
The latter method is undesired because there can exist unequal ref records with the some of the contents being equal. Replace the latter instances of ref-record comparison with the former. This has the added benefit of preserving uniformity throughout the test file.
--- t/unit-tests/t-reftable-stack.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
t/unit-tests/t-reftable-stack.c +2 -2
@@ -174,7 +174,7 @@ static void t_reftable_stack_add_one(void) err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ )); check_int(st->readers_len, >, 0);#ifndef GIT_WINDOWS_NATIVE
@@ -285,7 +285,7 @@ static void t_reftable_stack_transaction_api(void) err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); check_int(REFTABLE_REF_SYMREF, ==, dest.value_type); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ ));reftable_ref_record_release (&dest);reftable_stack_destroy (st);
Signature · 1 line
2.45.GIT
PATCH v5 5/7 t-reftable-stack: add test for non-default compaction factor
In reply to Chandra PratapIn a recent codebase update (commit ae8e378430, merge branch 'ps/reftable-write-options', 2024/05/13) the geometric factor used in auto-compaction of reftable tables was made configurable. Add a test to verify the functionality introduced by this update.
--- t/unit-tests/t-reftable-stack.c | 41 +++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-)
t/unit-tests/t-reftable-stack.c +37 -4
@@ -831,12 +831,12 @@ static void t_empty_add(void)reftable_stack_destroy (st2); } -static int fastlog2 (uint64_t sz) +static int fastlogN (uint64_t sz,uint64_t N ) {int l =0 ;if (sz ==0 )return 0 ; -for (; sz; sz /=2 ) +for (; sz; sz /=N ) l++;return l -1 ; }
@@ -869,11 +869,43 @@ static void t_reftable_stack_auto_compaction(void) err =reftable_stack_auto_compact (st);check (!err); -check (i <3 || st->merged->readers_len <2 *fastlog2 (i)); +check (i <2 || st->merged->readers_len <2 *fastlogN (i,2 )); } check_int(reftable_stack_compaction_stats(st)->entries_written, <, - (uint64_t )(N *fastlog2 (N ))); + (uint64_t )(N *fastlogN (N ,2 ))); + +reftable_stack_destroy (st); +clear_dir (dir); +} + +static void t_reftable_stack_auto_compaction_factor (void ) +{ +struct reftable_write_options opts = { + .auto_compaction_factor =5 , + }; +struct reftable_stack *st =NULL ; +char *dir =get_tmp_dir (__LINE__); +int err; +size_t N =100 ; + + err =reftable_new_stack (&st, dir, &opts); +check (!err); + +for (size_t i =0 ; i <N ; i++) { +char name[20 ]; +struct reftable_ref_record ref = { + .refname = name, + .update_index =reftable_stack_next_update_index (st), + .value_type =REFTABLE_REF_VAL1 , + }; +xsnprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); + + err =reftable_stack_add (st, &write_test_ref, &ref); +check (!err); + +check (i <5 || st->merged->readers_len <5 *fastlogN (i,5 )); + }reftable_stack_destroy (st);clear_dir (dir);
@@ -1186,6 +1218,7 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)TEST (t_reftable_stack_add_one (),"add a single ref record to stack" );TEST (t_reftable_stack_add_performs_auto_compaction (),"addition to stack triggers auto-compaction" );TEST (t_reftable_stack_auto_compaction (),"stack must form geometric sequence after compaction" ); +TEST (t_reftable_stack_auto_compaction_factor (),"auto-compaction with non-default geometric factor" );TEST (t_reftable_stack_auto_compaction_fails_gracefully (),"failure on auto-compaction" );TEST (t_reftable_stack_auto_compaction_with_locked_tables (),"auto compaction with locked tables" );TEST (t_reftable_stack_compaction_concurrent (),"compaction with concurrent stack" );
Signature · 1 line
2.45.GIT
PATCH v5 6/7 t-reftable-stack: add test for stack iterators
In reply to Chandra Pratapreftable_stack_init_ref_iterator and reftable_stack_init_log_iterator as defined by reftable/stack.{c,h} initialize a stack iterator to iterate over the ref and log records in a reftable stack respectively. Since these functions are not exercised by any of the existing tests, add a test for them.
--- t/unit-tests/t-reftable-stack.c | 83 +++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+)
t/unit-tests/t-reftable-stack.c +83 -0
@@ -544,6 +544,88 @@ static void t_reftable_stack_add(void)clear_dir (dir); } +static void t_reftable_stack_iterator (void ) +{ +struct reftable_write_options opts = {0 }; +struct reftable_stack *st =NULL ; +char *dir =get_tmp_dir (__LINE__); +struct reftable_ref_record refs[10 ] = {0 }; +struct reftable_log_record logs[10 ] = {0 }; +struct reftable_iterator it = {0 }; +size_t N =ARRAY_SIZE (refs), i; +int err; + + err =reftable_new_stack (&st, dir, &opts); +check (!err); + +for (i =0 ; i <N ; i++) { + refs[i].refname =xstrfmt ("branch%02" PRIuMAX, (uintmax_t )i); + refs[i].update_index = i +1 ; + refs[i].value_type =REFTABLE_REF_VAL1 ; +set_test_hash (refs[i].value.val1, i); + + logs[i].refname =xstrfmt ("branch%02" PRIuMAX, (uintmax_t )i); + logs[i].update_index = i +1 ; + logs[i].value_type =REFTABLE_LOG_UPDATE ; + logs[i].value.update.email =xstrdup ("johndoe@invalid" ); + logs[i].value.update.message =xstrdup ("commit\n" ); +set_test_hash (logs[i].value.update.new_hash, i); + } + +for (i =0 ; i <N ; i++) { + err =reftable_stack_add (st, write_test_ref, &refs[i]); +check (!err); + } + +for (i =0 ; i <N ; i++) { +struct write_log_arg arg = { + .log = &logs[i], + .update_index =reftable_stack_next_update_index (st), + }; + + err =reftable_stack_add (st, write_test_log, &arg); +check (!err); + } + +reftable_stack_init_ref_iterator (st, &it); +reftable_iterator_seek_ref (&it, refs[0 ].refname); +for (i =0 ; ; i++) { +struct reftable_ref_record ref = {0 }; + + err =reftable_iterator_next_ref (&it, &ref); +if (err >0 ) +break ; +check (!err); +check (reftable_ref_record_equal (&ref, &refs[i],GIT_SHA1_RAWSZ )); +reftable_ref_record_release (&ref); + } + check_int(i, ==, N); + +reftable_iterator_destroy (&it); + +reftable_stack_init_log_iterator (st, &it); +reftable_iterator_seek_log (&it, logs[0 ].refname); +for (i =0 ; ; i++) { +struct reftable_log_record log = {0 }; + + err =reftable_iterator_next_log (&it, &log); +if (err >0 ) +break ; +check (!err); +check (reftable_log_record_equal (&log, &logs[i],GIT_SHA1_RAWSZ )); +reftable_log_record_release (&log); + } + check_int(i, ==, N); + +reftable_stack_destroy (st); +reftable_iterator_destroy (&it); +for (i =0 ; i <N ; i++) { +reftable_ref_record_release (&refs[i]); +reftable_log_record_release (&logs[i]); + } +clear_dir (dir); +} +static void t_reftable_stack_log_normalize (void ) {int err =0 ;
@@ -1225,6 +1307,7 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)TEST (t_reftable_stack_compaction_concurrent_clean (),"compaction with unclean stack shutdown" );TEST (t_reftable_stack_compaction_with_locked_tables (),"compaction with locked tables" );TEST (t_reftable_stack_hash_id (),"read stack with wrong hash ID" ); +TEST (t_reftable_stack_iterator (),"log and ref iterator for reftable stack" );TEST (t_reftable_stack_lock_failure (),"stack addition with lockfile failure" );TEST (t_reftable_stack_log_normalize (),"log messages should be normalized" );TEST (t_reftable_stack_read_across_reload (),"stack iterators work across reloads" );
Signature · 1 line
2.45.GIT
PATCH v5 7/7 t: clean up leftover reftable test cruft
In reply to Chandra PratapFrom: Junio C Hamano <[email protected]>
With the migration of reftable tests to the unit-tests framework, "test-tool reftable" becomes a no-op.
Get rid of everything that uses "test-tool reftable" alongside everything that is used to implement it.
While at it, alphabetically sort the cmds[] list in helper/test-tool.c by moving the entry for "dump-reftable".
--- Makefile | 2 -- reftable/reftable-tests.h | 13 -------- reftable/test_framework.c | 27 ---------------- reftable/test_framework.h | 61 ------------------------------------ t/helper/test-reftable.c | 7 ----- t/helper/test-tool.c | 3 +- t/helper/test-tool.h | 1 - t/t0032-reftable-unittest.sh | 16 ---------- 8 files changed, 1 insertion(+), 129 deletions(-) delete mode 100644 reftable/reftable-tests.h delete mode 100644 reftable/test_framework.c delete mode 100644 reftable/test_framework.h delete mode 100755 t/t0032-reftable-unittest.sh
Makefile +0 -2
@@ -2692,8 +2692,6 @@ REFTABLE_OBJS += reftable/stack.o REFTABLE_OBJS += reftable/tree.o REFTABLE_OBJS += reftable/writer.o -REFTABLE_TEST_OBJS += reftable/test_framework.o - TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS)) $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS)) .PHONY: test-objs
reftable/reftable-tests.h deleted +0 -13
@@ -1,13 +0,0 @@ -/* -Copyright 2020 Google LLC - -Use of this source code is governed by a BSD-style -license that can be found in the LICENSE file or at -https://developers.google.com/open-source/licenses/bsd -*/ - -#ifndef REFTABLE_TESTS_H -#define REFTABLE_TESTS_H - - -#endif
reftable/test_framework.c deleted +0 -27
@@ -1,27 +0,0 @@ -/* -Copyright 2020 Google LLC - -Use of this source code is governed by a BSD-style -license that can be found in the LICENSE file or at -https://developers.google.com/open-source/licenses/bsd -*/ - -#include "system.h" -#include "test_framework.h" - - -void set_test_hash (uint8_t *p,int i) -{ -memset (p, (uint8_t )i,hash_size (GIT_SHA1_FORMAT_ID )); -} - -ssize_t strbuf_add_void (void *b,const void *data,size_t sz) -{ -strbuf_add (b, data, sz); -return sz; -} - -int noop_flush(void *arg UNUSED) -{ -return 0 ; -}
reftable/test_framework.h deleted +0 -61
61 lines not on this page · raw message
t/helper/test-reftable.c +0 -7
@@ -6,15 +6,8 @@#include "reftable/reftable-merged.h" #include "reftable/reftable-reader.h" #include "reftable/reftable-stack.h" -#include "reftable/reftable-tests.h" #include "test-tool.h" -int cmd__reftable (int argc,const char **argv) -{ -/* test from simple to complex. */ -return 0 ; -} -static void print_help (void ) { printf("usage: dump [-st] arg\n\n"
t/helper/test-tool.c +1 -2
@@ -26,6 +26,7 @@ static struct test_cmd cmds[] = { { "drop-caches", cmd__drop_caches }, { "dump-cache-tree", cmd__dump_cache_tree }, { "dump-fsmonitor", cmd__dump_fsmonitor }, + { "dump-reftable", cmd__dump_reftable }, { "dump-split-index", cmd__dump_split_index }, { "dump-untracked-cache", cmd__dump_untracked_cache }, { "env-helper", cmd__env_helper },
@@ -61,9 +62,7 @@ static struct test_cmd cmds[] = { { "read-graph", cmd__read_graph }, { "read-midx", cmd__read_midx }, { "ref-store", cmd__ref_store }, - { "reftable", cmd__reftable }, { "rot13-filter", cmd__rot13_filter }, - { "dump-reftable", cmd__dump_reftable }, { "regex", cmd__regex }, { "repository", cmd__repository }, { "revision-walking", cmd__revision_walking },
t/helper/test-tool.h +0 -1
@@ -55,7 +55,6 @@ int cmd__read_graph(int argc, const char **argv);int cmd__read_midx (int argc,const char **argv);int cmd__ref_store (int argc,const char **argv);int cmd__rot13_filter (int argc,const char **argv); -int cmd__reftable (int argc,const char **argv);int cmd__regex (int argc,const char **argv);int cmd__repository (int argc,const char **argv);int cmd__revision_walking (int argc,const char **argv);
t/t0032-reftable-unittest.sh deleted +0 -16
@@ -1,16 +0,0 @@ -#!/bin/sh -# -# Copyright (c) 2020 Google LLC -# - -test_description='reftable unittests' - -TEST_PASSES_SANITIZE_LEAK=true -. ./test-lib.sh - -test_expect_success 'unittests' ' - TMPDIR=$(pwd) && export TMPDIR && - test-tool reftable -' - -test_done
Signature · 1 line
2.45.GIT
Quoted 6 lines · Chandra Pratap
Changes in v5:
- Edit the commit messages in patches 3 and 4 to reflect the changes and the motivation behind those changes better.
- Add newlines after variable declarations in patch 6.
- Introduce patch 7 which removes leftover cruft from the previous reftable testing scheme.
Hmph, the end-result looks good to me, but the structure of the series is a bit curious. I didn't expect there will be a separate step for removal. Shouldn't these "leftover cruft" be removed *in* the same step that they become cruft (which I am assuming is when reftable/stack_test.c and all references to it gets removed in an early part of the series)?
Other than that, looking good.
Thanks.
Quoted 16 lines · Junio C Hamano
Quoted 6 lines · Chandra Pratap
Changes in v5:
- Edit the commit messages in patches 3 and 4 to reflect the changes and the motivation behind those changes better.
- Add newlines after variable declarations in patch 6.
- Introduce patch 7 which removes leftover cruft from the previous reftable testing scheme.Hmph, the end-result looks good to me, but the structure of the series is a bit curious. I didn't expect there will be a separate step for removal. Shouldn't these "leftover cruft" be removed *in* the same step that they become cruft (which I am assuming is when
reftable/stack_test.cand all references to it gets removed in an early part of the series)?
Quoted 3 lines
Other than that, looking good.
Thanks.
There is another issue.
It is unique to this among the reftable/*_test topics, simply because this one happens to be the last and needs to clean up a bit more.
Apparently, GNU "ar" can be invoked without any .o object files and happily creates an empty archive, but on BSD may not be that may be the case. macOS CI jobs seem to be hard failing due to this.
You'd need to squeeze in the following, in addition to [7/7], to earlier patch(es).
Thanks.
Makefile | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
Makefile +2 -6
@@ -912,7 +912,6 @@ TEST_SHELL_PATH = $(SHELL_PATH) LIB_FILE = libgit.a XDIFF_LIB = xdiff/lib.a REFTABLE_LIB = reftable/libreftable.a -REFTABLE_TEST_LIB = reftable/libreftable_test.a GENERATED_H += command-list.h GENERATED_H += config-list.h
@@ -2866,9 +2865,6 @@ $(XDIFF_LIB): $(XDIFF_OBJS) $(REFTABLE_LIB): $(REFTABLE_OBJS) $(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^ -$(REFTABLE_TEST_LIB): $(REFTABLE_TEST_OBJS) - $(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^ - export DEFAULT_EDITOR DEFAULT_PAGER Documentation/GIT-EXCLUDED-PROGRAMS: FORCE
@@ -3248,7 +3244,7 @@ perf: all t/helper/test-tool$X: $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS)) $(UNIT_TEST_DIR)/test-lib.o -t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS) $(REFTABLE_TEST_LIB) +t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS) $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(filter %.a,$^) $(LIBS) check-sha1:: t/helper/test-tool$X
@@ -3709,7 +3705,7 @@ clean: profile-clean coverage-clean cocciclean $(RM) git.res $(RM) $(OBJECTS) $(RM) headless-git.o - $(RM) $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(REFTABLE_TEST_LIB) + $(RM) $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) $(OTHER_PROGRAMS) $(RM) $(TEST_PROGRAMS) $(RM) $(FUZZ_PROGRAMS)
v6 cover t: port reftable/stack_test.c to the unit testing framework
In reply to Chandra PratapThe reftable library comes with self tests, which are exercised as part of the usual end-to-end tests and are designed to observe the end-user visible effects of Git commands. What it exercises, however, is a better match for the unit-testing framework, merged at 8bf6fbd0 (Merge branch 'js/doc-unit-tests', 2023-12-09), which is designed to observe how low level implementation details, at the level of sequences of individual function calls, behave.
Hence, port reftable/stack_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.
---
Changes in v6:
- Fix the Makefile changes in patch 7 of the previous series that broke the OSX CI.
- Squash patch 7 of the previous series into patch 1.
CI/PR: https://github.com/gitgitgadget/git/pull/1762
Chandra Pratap(6):
t: move reftable/stack_test.c to the unit testing framework
t: harmonize t-reftable-stack.c with coding guidelines
t-reftable-stack: use Git's tempfile API instead of mkstemp()
t-reftable-stack: use reftable_ref_record_equal() to compare ref records
t-reftable-stack: add test for non-default compaction factor
t-reftable-stack: add test for stack iterators
Makefile | 12 +- reftable/reftable-tests.h | 14 - reftable/test_framework.c | 27 - reftable/test_framework.h | 61 -- t/helper/test-reftable.c | 8 - t/helper/test-tool.c | 3 +- t/helper/test-tool.h | 1 - t/t0032-reftable-unittest.sh | 16 - .../unit-tests/t-reftable-stack.c | 665 ++++++++++++--------- 9 files changed, 392 insertions(+), 415 deletions(-)
Range-diff against v5:
1: e9af30d14a ! 1: fa549a875a t: move reftable/stack_test.c to the unit testing framework
@@ Commit message
'#included' in the test file, copy this function in the
ported test file.
+ With the migration of stack test to the unit-tests framework,
+ "test-tool reftable" becomes a no-op. Hence, get rid of everything
+ that uses "test-tool reftable" alongside everything that is used
+ to implement it.
+
+ While at it, alphabetically sort the cmds[] list in
+ helper/test-tool.c by moving the entry for "dump-reftable".
+
Mentored-by: Patrick Steinhardt <[email protected]>
Mentored-by: Christian Couder <[email protected]>
Signed-off-by: Chandra Pratap <[email protected]>
## Makefile ##
+@@ Makefile: TEST_SHELL_PATH = $(SHELL_PATH)
+ LIB_FILE = libgit.a
+ XDIFF_LIB = xdiff/lib.a
+ REFTABLE_LIB = reftable/libreftable.a
+-REFTABLE_TEST_LIB = reftable/libreftable_test.a
+
+ GENERATED_H += command-list.h
+ GENERATED_H += config-list.h
@@ Makefile: UNIT_TEST_PROGRAMS += t-reftable-merged
UNIT_TEST_PROGRAMS += t-reftable-pq
UNIT_TEST_PROGRAMS += t-reftable-readwrite
@@ Makefile: REFTABLE_OBJS += reftable/stack.o
REFTABLE_OBJS += reftable/writer.o
-REFTABLE_TEST_OBJS += reftable/stack_test.o
- REFTABLE_TEST_OBJS += reftable/test_framework.o
-
+-REFTABLE_TEST_OBJS += reftable/test_framework.o
+-
TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS)) $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS))
-
- ## reftable/reftable-tests.h ##
-@@ reftable/reftable-tests.h: license that can be found in the LICENSE file or at
- #ifndef REFTABLE_TESTS_H
- #define REFTABLE_TESTS_H
--int stack_test_main(int argc, const char **argv);
+ .PHONY: test-objs
+@@ Makefile: $(XDIFF_LIB): $(XDIFF_OBJS)
+ $(REFTABLE_LIB): $(REFTABLE_OBJS)
+ $(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^
- #endif
+-$(REFTABLE_TEST_LIB): $(REFTABLE_TEST_OBJS)
+- $(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^
+-
+ export DEFAULT_EDITOR DEFAULT_PAGER
+
+ Documentation/GIT-EXCLUDED-PROGRAMS: FORCE
+@@ Makefile: perf: all
+
+ t/helper/test-tool$X: $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS)) $(UNIT_TEST_DIR)/test-lib.o
+
+-t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS) $(REFTABLE_TEST_LIB)
++t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS)
+ $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(filter %.a,$^) $(LIBS)
+
+ check-sha1:: t/helper/test-tool$X
+@@ Makefile: clean: profile-clean coverage-clean cocciclean
+ $(RM) git.res
+ $(RM) $(OBJECTS)
+ $(RM) headless-git.o
+- $(RM) $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(REFTABLE_TEST_LIB)
++ $(RM) $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB)
+ $(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) $(OTHER_PROGRAMS)
+ $(RM) $(TEST_PROGRAMS)
+ $(RM) $(FUZZ_PROGRAMS)
+
+ ## reftable/reftable-tests.h (deleted) ##
+@@
+-/*
+-Copyright 2020 Google LLC
+-
+-Use of this source code is governed by a BSD-style
+-license that can be found in the LICENSE file or at
+-https://developers.google.com/open-source/licenses/bsd
+-*/
+-
+-#ifndef REFTABLE_TESTS_H
+-#define REFTABLE_TESTS_H
+-
+-int stack_test_main(int argc, const char **argv);
+-
+-#endif
+
+ ## reftable/test_framework.c (deleted) ##
+@@
+-/*
+-Copyright 2020 Google LLC
+-
+-Use of this source code is governed by a BSD-style
+-license that can be found in the LICENSE file or at
+-https://developers.google.com/open-source/licenses/bsd
+-*/
+-
+-#include "system.h"
+-#include "test_framework.h"
+-
+-
+-void set_test_hash(uint8_t *p, int i)
+-{
+- memset(p, (uint8_t)i, hash_size(GIT_SHA1_FORMAT_ID));
+-}
+-
+-ssize_t strbuf_add_void(void *b, const void *data, size_t sz)
+-{
+- strbuf_add(b, data, sz);
+- return sz;
+-}
+-
+-int noop_flush(void *arg UNUSED)
+-{
+- return 0;
+-}
+
+ ## reftable/test_framework.h (deleted) ##
+@@
+-/*
+-Copyright 2020 Google LLC
+-
+-Use of this source code is governed by a BSD-style
+-license that can be found in the LICENSE file or at
+-https://developers.google.com/open-source/licenses/bsd
+-*/
+-
+-#ifndef TEST_FRAMEWORK_H
+-#define TEST_FRAMEWORK_H
+-
+-#include "system.h"
+-#include "reftable-error.h"
+-
+-#define EXPECT_ERR(c) \
+- do { \
+- if (c != 0) { \
+- fflush(stderr); \
+- fflush(stdout); \
+- fprintf(stderr, "%s: %d: error == %d (%s), want 0\n", \
+- __FILE__, __LINE__, c, reftable_error_str(c)); \
+- abort(); \
+- } \
+- } while (0)
+-
+-#define EXPECT_STREQ(a, b) \
+- do { \
+- if (strcmp(a, b)) { \
+- fflush(stderr); \
+- fflush(stdout); \
+- fprintf(stderr, "%s:%d: %s (%s) != %s (%s)\n", __FILE__, \
+- __LINE__, #a, a, #b, b); \
+- abort(); \
+- } \
+- } while (0)
+-
+-#define EXPECT(c) \
+- do { \
+- if (!(c)) { \
+- fflush(stderr); \
+- fflush(stdout); \
+- fprintf(stderr, "%s: %d: failed assertion %s\n", __FILE__, \
+- __LINE__, #c); \
+- abort(); \
+- } \
+- } while (0)
+-
+-#define RUN_TEST(f) \
+- fprintf(stderr, "running %s\n", #f); \
+- fflush(stderr); \
+- f();
+-
+-void set_test_hash(uint8_t *p, int i);
+-
+-/* Like strbuf_add, but suitable for passing to reftable_new_writer
+- */
+-ssize_t strbuf_add_void(void *b, const void *data, size_t sz);
+-
+-int noop_flush(void *);
+-
+-#endif
## t/helper/test-reftable.c ##
@@
- int cmd__reftable(int argc, const char **argv)
- {
- /* test from simple to complex. */
+ #include "reftable/reftable-merged.h"
+ #include "reftable/reftable-reader.h"
+ #include "reftable/reftable-stack.h"
+-#include "reftable/reftable-tests.h"
+ #include "test-tool.h"
+
+-int cmd__reftable(int argc, const char **argv)
+-{
+- /* test from simple to complex. */
- stack_test_main(argc, argv);
- return 0;
- }
-
+- return 0;
+-}
+-
+ static void print_help(void)
+ {
+ printf("usage: dump [-st] arg\n\n"
+
+ ## t/helper/test-tool.c ##
+@@ t/helper/test-tool.c: static struct test_cmd cmds[] = {
+ { "drop-caches", cmd__drop_caches },
+ { "dump-cache-tree", cmd__dump_cache_tree },
+ { "dump-fsmonitor", cmd__dump_fsmonitor },
++ { "dump-reftable", cmd__dump_reftable },
+ { "dump-split-index", cmd__dump_split_index },
+ { "dump-untracked-cache", cmd__dump_untracked_cache },
+ { "env-helper", cmd__env_helper },
+@@ t/helper/test-tool.c: static struct test_cmd cmds[] = {
+ { "read-graph", cmd__read_graph },
+ { "read-midx", cmd__read_midx },
+ { "ref-store", cmd__ref_store },
+- { "reftable", cmd__reftable },
+ { "rot13-filter", cmd__rot13_filter },
+- { "dump-reftable", cmd__dump_reftable },
+ { "regex", cmd__regex },
+ { "repository", cmd__repository },
+ { "revision-walking", cmd__revision_walking },
+
+ ## t/helper/test-tool.h ##
+@@ t/helper/test-tool.h: int cmd__read_graph(int argc, const char **argv);
+ int cmd__read_midx(int argc, const char **argv);
+ int cmd__ref_store(int argc, const char **argv);
+ int cmd__rot13_filter(int argc, const char **argv);
+-int cmd__reftable(int argc, const char **argv);
+ int cmd__regex(int argc, const char **argv);
+ int cmd__repository(int argc, const char **argv);
+ int cmd__revision_walking(int argc, const char **argv);
+
+ ## t/t0032-reftable-unittest.sh (deleted) ##
+@@
+-#!/bin/sh
+-#
+-# Copyright (c) 2020 Google LLC
+-#
+-
+-test_description='reftable unittests'
+-
+-TEST_PASSES_SANITIZE_LEAK=true
+-. ./test-lib.sh
+-
+-test_expect_success 'unittests' '
+- TMPDIR=$(pwd) && export TMPDIR &&
+- test-tool reftable
+-'
+-
+-test_done
## reftable/stack_test.c => t/unit-tests/t-reftable-stack.c ##
@@ t/unit-tests/t-reftable-stack.c: license that can be found in the LICENSE file or at
2: 8e8e09bd5b = 2: 1fd91e7a12 t: harmonize t-reftable-stack.c with coding guidelines
3: ca4b00feef = 3: 8a7132500a t-reftable-stack: use Git's tempfile API instead of mkstemp()
4: 3e723667dd = 4: 786f7c2874 t-reftable-stack: use reftable_ref_record_equal() to compare ref records
5: 7526550c92 = 5: 388f1129d8 t-reftable-stack: add test for non-default compaction factor
6: 05e4b7e715 = 6: 5468c9853f t-reftable-stack: add test for stack iterators
7: 560d3c76e0 < -: ---------- t: clean up leftover reftable test cruftPATCH v6 1/6 t: move reftable/stack_test.c to the unit testing framework
reftable/stack_test.c exercises the functions defined in reftable/stack.{c, h}. Migrate reftable/stack_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework and renaming the tests to be in-line with unit-tests' standards.
Since some of the tests use set_test_hash() defined by reftable/test_framework.{c, h} but these files are not '#included' in the test file, copy this function in the ported test file.
With the migration of stack test to the unit-tests framework, "test-tool reftable" becomes a no-op. Hence, get rid of everything that uses "test-tool reftable" alongside everything that is used to implement it.
While at it, alphabetically sort the cmds[] list in helper/test-tool.c by moving the entry for "dump-reftable".
--- Makefile | 12 +- reftable/reftable-tests.h | 14 - reftable/test_framework.c | 27 -- reftable/test_framework.h | 61 --- t/helper/test-reftable.c | 8 - t/helper/test-tool.c | 3 +- t/helper/test-tool.h | 1 - t/t0032-reftable-unittest.sh | 16 - .../unit-tests/t-reftable-stack.c | 429 +++++++++--------- 9 files changed, 217 insertions(+), 354 deletions(-) delete mode 100644 reftable/reftable-tests.h delete mode 100644 reftable/test_framework.c delete mode 100644 reftable/test_framework.h delete mode 100755 t/t0032-reftable-unittest.sh rename reftable/stack_test.c => t/unit-tests/t-reftable-stack.c (76%)
Makefile +3 -9
@@ -912,7 +912,6 @@ TEST_SHELL_PATH = $(SHELL_PATH) LIB_FILE = libgit.a XDIFF_LIB = xdiff/lib.a REFTABLE_LIB = reftable/libreftable.a -REFTABLE_TEST_LIB = reftable/libreftable_test.a GENERATED_H += command-list.h GENERATED_H += config-list.h
@@ -1349,6 +1348,7 @@ UNIT_TEST_PROGRAMS += t-reftable-merged UNIT_TEST_PROGRAMS += t-reftable-pq UNIT_TEST_PROGRAMS += t-reftable-readwrite UNIT_TEST_PROGRAMS += t-reftable-record +UNIT_TEST_PROGRAMS += t-reftable-stack UNIT_TEST_PROGRAMS += t-reftable-tree UNIT_TEST_PROGRAMS += t-strbuf UNIT_TEST_PROGRAMS += t-strcmp-offset
@@ -2691,9 +2691,6 @@ REFTABLE_OBJS += reftable/stack.o REFTABLE_OBJS += reftable/tree.o REFTABLE_OBJS += reftable/writer.o -REFTABLE_TEST_OBJS += reftable/stack_test.o -REFTABLE_TEST_OBJS += reftable/test_framework.o - TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS)) $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS)) .PHONY: test-objs
@@ -2868,9 +2865,6 @@ $(XDIFF_LIB): $(XDIFF_OBJS) $(REFTABLE_LIB): $(REFTABLE_OBJS) $(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^ -$(REFTABLE_TEST_LIB): $(REFTABLE_TEST_OBJS) - $(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^ - export DEFAULT_EDITOR DEFAULT_PAGER Documentation/GIT-EXCLUDED-PROGRAMS: FORCE
@@ -3250,7 +3244,7 @@ perf: all t/helper/test-tool$X: $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS)) $(UNIT_TEST_DIR)/test-lib.o -t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS) $(REFTABLE_TEST_LIB) +t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS) $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(filter %.a,$^) $(LIBS) check-sha1:: t/helper/test-tool$X
@@ -3711,7 +3705,7 @@ clean: profile-clean coverage-clean cocciclean $(RM) git.res $(RM) $(OBJECTS) $(RM) headless-git.o - $(RM) $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(REFTABLE_TEST_LIB) + $(RM) $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) $(OTHER_PROGRAMS) $(RM) $(TEST_PROGRAMS) $(RM) $(FUZZ_PROGRAMS)
reftable/reftable-tests.h deleted +0 -14
@@ -1,14 +0,0 @@ -/* -Copyright 2020 Google LLC - -Use of this source code is governed by a BSD-style -license that can be found in the LICENSE file or at -https://developers.google.com/open-source/licenses/bsd -*/ - -#ifndef REFTABLE_TESTS_H -#define REFTABLE_TESTS_H - -int stack_test_main (int argc,const char **argv); - -#endif
reftable/test_framework.c deleted +0 -27
@@ -1,27 +0,0 @@ -/* -Copyright 2020 Google LLC - -Use of this source code is governed by a BSD-style -license that can be found in the LICENSE file or at -https://developers.google.com/open-source/licenses/bsd -*/ - -#include "system.h" -#include "test_framework.h" - - -void set_test_hash (uint8_t *p,int i) -{ -memset (p, (uint8_t )i,hash_size (GIT_SHA1_FORMAT_ID )); -} - -ssize_t strbuf_add_void (void *b,const void *data,size_t sz) -{ -strbuf_add (b, data, sz); -return sz; -} - -int noop_flush(void *arg UNUSED) -{ -return 0 ; -}
reftable/test_framework.h deleted +0 -61
61 lines not on this page · raw message
t/helper/test-reftable.c +0 -8
@@ -6,16 +6,8 @@#include "reftable/reftable-merged.h" #include "reftable/reftable-reader.h" #include "reftable/reftable-stack.h" -#include "reftable/reftable-tests.h" #include "test-tool.h" -int cmd__reftable (int argc,const char **argv) -{ -/* test from simple to complex. */ -stack_test_main (argc, argv); -return 0 ; -} -static void print_help (void ) { printf("usage: dump [-st] arg\n\n"
t/helper/test-tool.c +1 -2
@@ -26,6 +26,7 @@ static struct test_cmd cmds[] = { { "drop-caches", cmd__drop_caches }, { "dump-cache-tree", cmd__dump_cache_tree }, { "dump-fsmonitor", cmd__dump_fsmonitor }, + { "dump-reftable", cmd__dump_reftable }, { "dump-split-index", cmd__dump_split_index }, { "dump-untracked-cache", cmd__dump_untracked_cache }, { "env-helper", cmd__env_helper },
@@ -61,9 +62,7 @@ static struct test_cmd cmds[] = { { "read-graph", cmd__read_graph }, { "read-midx", cmd__read_midx }, { "ref-store", cmd__ref_store }, - { "reftable", cmd__reftable }, { "rot13-filter", cmd__rot13_filter }, - { "dump-reftable", cmd__dump_reftable }, { "regex", cmd__regex }, { "repository", cmd__repository }, { "revision-walking", cmd__revision_walking },
t/helper/test-tool.h +0 -1
@@ -55,7 +55,6 @@ int cmd__read_graph(int argc, const char **argv);int cmd__read_midx (int argc,const char **argv);int cmd__ref_store (int argc,const char **argv);int cmd__rot13_filter (int argc,const char **argv); -int cmd__reftable (int argc,const char **argv);int cmd__regex (int argc,const char **argv);int cmd__repository (int argc,const char **argv);int cmd__revision_walking (int argc,const char **argv);
t/t0032-reftable-unittest.sh deleted +0 -16
@@ -1,16 +0,0 @@ -#!/bin/sh -# -# Copyright (c) 2020 Google LLC -# - -test_description='reftable unittests' - -TEST_PASSES_SANITIZE_LEAK=true -. ./test-lib.sh - -test_expect_success 'unittests' ' - TMPDIR=$(pwd) && export TMPDIR && - test-tool reftable -' - -test_done
t/unit-tests/t-reftable-stack.c renamed from reftable/stack_test.c +213 -216
1,006 lines not on this page · raw message
Signature · 1 line
2.45.GIT
PATCH v6 2/6 t: harmonize t-reftable-stack.c with coding guidelines
In reply to Chandra PratapHarmonize 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.
--- t/unit-tests/t-reftable-stack.c | 110 +++++++++++++++----------------- 1 file changed, 53 insertions(+), 57 deletions(-)
t/unit-tests/t-reftable-stack.c +53 -57
@@ -81,7 +81,6 @@ static void t_read_file(void)int n, err;char **names =NULL ;const char *want[] = {"line1" ,"line2" ,"line3" }; -int i =0 ; check_int(fd, >, 0); n =write_in_full (fd, out,strlen (out));
@@ -92,9 +91,8 @@ static void t_read_file(void) err =read_lines (fn, &names);check (!err); -for (i =0 ; names[i]; i++) { +for (size_t i =0 ; names[i]; i++)check_str (want[i], names[i]); - }free_names (names); (void )remove (fn); }
@@ -123,7 +121,7 @@ static void write_n_ref_tables(struct reftable_stack *st, };strbuf_reset (&buf); -strbuf_addf (&buf,"refs/heads/branch-%04u" , (unsigned ) i); +strbuf_addf (&buf,"refs/heads/branch-%04" PRIuMAX, (uintmax_t )i); ref.refname = buf.buf;set_test_hash (ref.value.val1, i);
@@ -164,12 +162,12 @@ static void t_reftable_stack_add_one(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 };struct stat stat_result = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err); err =reftable_stack_read_ref (st, ref.refname, &dest);
@@ -234,16 +232,16 @@ static void t_reftable_stack_uptodate(void) err =reftable_new_stack (&st2, dir, &opts);check (!err); - err =reftable_stack_add (st1, &write_test_ref, &ref1); + err =reftable_stack_add (st1, write_test_ref, &ref1);check (!err); - err =reftable_stack_add (st2, &write_test_ref, &ref2); + err =reftable_stack_add (st2, write_test_ref, &ref2); check_int(err, ==, REFTABLE_OUTDATED_ERROR); err =reftable_stack_reload (st2);check (!err); - err =reftable_stack_add (st2, &write_test_ref, &ref2); + err =reftable_stack_add (st2, write_test_ref, &ref2);check (!err);reftable_stack_destroy (st1);reftable_stack_destroy (st2);
@@ -264,7 +262,7 @@ static void t_reftable_stack_transaction_api(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -274,7 +272,7 @@ static void t_reftable_stack_transaction_api(void) err =reftable_stack_new_addition (&add, st);check (!err); - err =reftable_addition_add (add, &write_test_ref, &ref); + err =reftable_addition_add (add, write_test_ref, &ref);check (!err); err =reftable_addition_commit (add);
@@ -298,12 +296,13 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void)struct reftable_write_options opts = {0 };struct reftable_addition *add =NULL ;struct reftable_stack *st =NULL ; -int i, n =20 , err; +size_t n =20 ; +int err; err =reftable_new_stack (&st, dir, &opts);check (!err); -for (i =0 ; i <= n; i++) { +for (size_t i =0 ; i <= n; i++) {struct reftable_ref_record ref = { .update_index =reftable_stack_next_update_index (st), .value_type =REFTABLE_REF_SYMREF ,
@@ -311,7 +310,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void) };char name[100 ]; -snprintf (name,sizeof (name),"branch%04d" , i); +snprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); ref.refname = name;/*
@@ -324,7 +323,7 @@ static void t_reftable_stack_transaction_api_performs_auto_compaction(void) err =reftable_stack_new_addition (&add, st);check (!err); - err =reftable_addition_add (add, &write_test_ref, &ref); + err =reftable_addition_add (add, write_test_ref, &ref);check (!err); err =reftable_addition_commit (add);
@@ -355,7 +354,7 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void) .value_type = REFTABLE_REF_VAL1, .value.val1 = {0x01}, }; -struct reftable_write_options opts = {0 }; +struct reftable_write_options opts = {0 };struct reftable_stack *st;struct strbuf table_path =STRBUF_INIT ;char *dir =get_tmp_dir (__LINE__);
@@ -417,10 +416,10 @@ static void t_reftable_stack_update_index_check(void) err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref1); + err =reftable_stack_add (st, write_test_ref, &ref1);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref2); + err =reftable_stack_add (st, write_test_ref, &ref2); check_int(err, ==, REFTABLE_API_ERROR);reftable_stack_destroy (st);clear_dir (dir);
@@ -436,7 +435,7 @@ static void t_reftable_stack_lock_failure(void) err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =-1 ; i !=REFTABLE_EMPTY_TABLE_ERROR ; i--) { - err =reftable_stack_add (st, &write_error, &i); + err =reftable_stack_add (st, write_error, &i); check_int(err, ==, i); }
@@ -446,7 +445,6 @@ static void t_reftable_stack_lock_failure(void)static void t_reftable_stack_add (void ) { -int i =0 ;int err =0 ;struct reftable_write_options opts = { .exact_log_message =1 ,
@@ -455,18 +453,18 @@ static void t_reftable_stack_add(void) };struct reftable_stack *st =NULL ;char *dir =get_tmp_dir (__LINE__); -struct reftable_ref_record refs[2 ] = { {NULL } }; -struct reftable_log_record logs[2 ] = { {NULL } }; +struct reftable_ref_record refs[2 ] = {0 }; +struct reftable_log_record logs[2 ] = {0 };struct strbuf path =STRBUF_INIT ;struct stat stat_result; -int N =ARRAY_SIZE (refs); +size_t i,N =ARRAY_SIZE (refs); err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =0 ; i <N ; i++) {char buf[256 ]; -snprintf (buf,sizeof (buf),"branch%02d" , i); +snprintf (buf,sizeof (buf),"branch%02" PRIuMAX, (uintmax_t )i); refs[i].refname =xstrdup (buf); refs[i].update_index = i +1 ; refs[i].value_type =REFTABLE_REF_VAL1 ;
@@ -480,7 +478,7 @@ static void t_reftable_stack_add(void) }for (i =0 ; i <N ; i++) { -int err =reftable_stack_add (st, &write_test_ref, &refs[i]); +int err =reftable_stack_add (st, write_test_ref, &refs[i]);check (!err); }
@@ -489,7 +487,7 @@ static void t_reftable_stack_add(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -497,7 +495,7 @@ static void t_reftable_stack_add(void)check (!err);for (i =0 ; i <N ; i++) { -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 };int err =reftable_stack_read_ref (st, refs[i].refname, &dest);check (!err);
@@ -507,7 +505,7 @@ static void t_reftable_stack_add(void) }for (i =0 ; i <N ; i++) { -struct reftable_log_record dest = {NULL }; +struct reftable_log_record dest = {0 };int err =reftable_stack_read_log (st, refs[i].refname, &dest);check (!err); check(reftable_log_record_equal(&dest, logs + i,
@@ -575,11 +573,11 @@ static void t_reftable_stack_log_normalize(void)check (!err); input.value.update.message = (char *)"one\ntwo" ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg); check_int(err, ==, REFTABLE_API_ERROR); input.value.update.message = (char *)"one" ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg);check (!err); err =reftable_stack_read_log (st, input.refname, &dest);
@@ -588,7 +586,7 @@ static void t_reftable_stack_log_normalize(void) input.value.update.message = (char *)"two\n" ; arg.update_index =2 ; - err =reftable_stack_add (st, &write_test_log, &arg); + err =reftable_stack_add (st, write_test_log, &arg);check (!err); err =reftable_stack_read_log (st, input.refname, &dest);check (!err);
@@ -602,16 +600,15 @@ static void t_reftable_stack_log_normalize(void)static void t_reftable_stack_tombstone (void ) { -int i =0 ;char *dir =get_tmp_dir (__LINE__);struct reftable_write_options opts = {0 };struct reftable_stack *st =NULL ;int err; -struct reftable_ref_record refs[2 ] = { {NULL } }; -struct reftable_log_record logs[2 ] = { {NULL } }; -int N =ARRAY_SIZE (refs); -struct reftable_ref_record dest = {NULL }; -struct reftable_log_record log_dest = {NULL }; +struct reftable_ref_record refs[2 ] = {0 }; +struct reftable_log_record logs[2 ] = {0 }; +size_t i,N =ARRAY_SIZE (refs); +struct reftable_ref_record dest = {0 }; +struct reftable_log_record log_dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -637,7 +634,7 @@ static void t_reftable_stack_tombstone(void) } }for (i =0 ; i <N ; i++) { -int err =reftable_stack_add (st, &write_test_ref, &refs[i]); +int err =reftable_stack_add (st, write_test_ref, &refs[i]);check (!err); }
@@ -646,7 +643,7 @@ static void t_reftable_stack_tombstone(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -695,12 +692,12 @@ static void t_reftable_stack_hash_id(void)struct reftable_stack *st32 =NULL ;struct reftable_write_options opts_default = {0 };struct reftable_stack *st_default =NULL ; -struct reftable_ref_record dest = {NULL }; +struct reftable_ref_record dest = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err);/* can't read it with the wrong hash ID. */
@@ -743,21 +740,20 @@ static void t_reflog_expire(void)char *dir =get_tmp_dir (__LINE__);struct reftable_write_options opts = {0 };struct reftable_stack *st =NULL ; -struct reftable_log_record logs[20 ] = { {NULL } }; -int N =ARRAY_SIZE (logs) -1 ; -int i =0 ; +struct reftable_log_record logs[20 ] = {0 }; +size_t i,N =ARRAY_SIZE (logs) -1 ;int err;struct reftable_log_expiry_config expiry = { .time =10 , }; -struct reftable_log_record log = {NULL }; +struct reftable_log_record log = {0 }; err =reftable_new_stack (&st, dir, &opts);check (!err);for (i =1 ; i <=N ; i++) {char buf[256 ]; -snprintf (buf,sizeof (buf),"branch%02d" , i); +snprintf (buf,sizeof (buf),"branch%02" PRIuMAX, (uintmax_t )i); logs[i].refname =xstrdup (buf); logs[i].update_index = i;
@@ -772,7 +768,7 @@ static void t_reflog_expire(void) .log = &logs[i], .update_index = reftable_stack_next_update_index(st), }; -int err =reftable_stack_add (st, &write_test_log, &arg); +int err =reftable_stack_add (st, write_test_log, &arg);check (!err); }
@@ -800,9 +796,8 @@ static void t_reflog_expire(void)/* cleanup */ reftable_stack_destroy (st); -for (i =0 ; i <=N ; i++) { +for (i =0 ; i <=N ; i++)reftable_log_record_release (&logs[i]); - }clear_dir (dir);reftable_log_record_release (&log); }
@@ -824,7 +819,7 @@ static void t_empty_add(void) err =reftable_new_stack (&st, dir, &opts);check (!err); - err =reftable_stack_add (st, &write_nothing,NULL ); + err =reftable_stack_add (st, write_nothing,NULL );check (!err); err =reftable_new_stack (&st2, dir, &opts);
@@ -851,8 +846,8 @@ static void t_reftable_stack_auto_compaction(void) };struct reftable_stack *st =NULL ;char *dir =get_tmp_dir (__LINE__); -int err, i; -int N =100 ; +int err; +size_t i,N =100 ; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -865,9 +860,9 @@ static void t_reftable_stack_auto_compaction(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; -snprintf (name,sizeof (name),"branch%04d" , i); +snprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err); err =reftable_stack_auto_compact (st);
@@ -929,7 +924,8 @@ static void t_reftable_stack_add_performs_auto_compaction(void)struct reftable_stack *st =NULL ;struct strbuf refname =STRBUF_INIT ;char *dir =get_tmp_dir (__LINE__); -int err, i, n =20 ; +int err; +size_t i, n =20 ; err =reftable_new_stack (&st, dir, &opts);check (!err);
@@ -949,10 +945,10 @@ static void t_reftable_stack_add_performs_auto_compaction(void) st->opts.disable_auto_compact = i != n;strbuf_reset (&refname); -strbuf_addf (&refname,"branch-%04d" , i); +strbuf_addf (&refname,"branch-%04" PRIuMAX, (uintmax_t )i); ref.refname = refname.buf; - err =reftable_stack_add (st, &write_test_ref, &ref); + err =reftable_stack_add (st, write_test_ref, &ref);check (!err);/*
Signature · 1 line
2.45.GIT
PATCH v6 3/6 t-reftable-stack: use Git's tempfile API instead of mkstemp()
In reply to Chandra PratapGit's tempfile API defined by $GIT_DIR/tempfile.{c,h} provides a unified interface for tempfile operations. Since reftable/stack.c uses this API for all its tempfile needs instead of raw functions like mkstemp(), make the ported stack test strictly use Git's tempfile API as well.
A bigger benefit is the fact that we know to clean up the tempfile in case the test fails because it gets registered and pruned via a signal handler.
--- t/unit-tests/t-reftable-stack.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
t/unit-tests/t-reftable-stack.c +3 -1
@@ -76,7 +76,8 @@ static char *get_tmp_dir(int linenumber)static void t_read_file (void ) {char *fn =get_tmp_template (__LINE__); -int fd =mkstemp (fn); +struct tempfile *tmp =mks_tempfile (fn); +int fd =get_tempfile_fd (tmp);char out[1024 ] ="line1\n\nline2\nline3" ;int n, err;char **names =NULL ;
@@ -95,6 +96,7 @@ static void t_read_file(void)check_str (want[i], names[i]);free_names (names); (void )remove (fn); +delete_tempfile (&tmp); }static int write_test_ref (struct reftable_writer *wr,void *arg)
Signature · 1 line
2.45.GIT
PATCH v6 4/6 t-reftable-stack: use reftable_ref_record_equal() to compare ref records
In reply to Chandra PratapIn the current stack tests, ref records are compared for equality by sometimes using the dedicated function for ref-record comparison, reftable_ref_record_equal(), and sometimes by explicity comparing contents of the ref records.
The latter method is undesired because there can exist unequal ref records with the some of the contents being equal. Replace the latter instances of ref-record comparison with the former. This has the added benefit of preserving uniformity throughout the test file.
--- t/unit-tests/t-reftable-stack.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
t/unit-tests/t-reftable-stack.c +2 -2
@@ -174,7 +174,7 @@ static void t_reftable_stack_add_one(void) err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ )); check_int(st->readers_len, >, 0);#ifndef GIT_WINDOWS_NATIVE
@@ -285,7 +285,7 @@ static void t_reftable_stack_transaction_api(void) err =reftable_stack_read_ref (st, ref.refname, &dest);check (!err); check_int(REFTABLE_REF_SYMREF, ==, dest.value_type); -check_str ("master" , dest.value.symref); +check (reftable_ref_record_equal (&ref, &dest,GIT_SHA1_RAWSZ ));reftable_ref_record_release (&dest);reftable_stack_destroy (st);
Signature · 1 line
2.45.GIT
Re: [PATCH v6 4/6] t-reftable-stack: use reftable_ref_record_equal() to compare ref records
Quoted 7 lines · Chandra Pratap
In the current stack tests, ref records are compared for equality by sometimes using the dedicated function for ref-record comparison,
reftable_ref_record_equal(), and sometimes by explicity comparing contents of the ref records.The latter method is undesired because there can exist unequal ref records with the some of the contents being equal. Replace the latter
s/the some/some
Not worth a reroll, just pointing it out in case you end up rerolling due to other issues.
Patrick
PATCH v6 5/6 t-reftable-stack: add test for non-default compaction factor
In reply to Chandra PratapIn a recent codebase update (commit ae8e378430, merge branch 'ps/reftable-write-options', 2024/05/13) the geometric factor used in auto-compaction of reftable tables was made configurable. Add a test to verify the functionality introduced by this update.
--- t/unit-tests/t-reftable-stack.c | 41 +++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-)
t/unit-tests/t-reftable-stack.c +37 -4
@@ -831,12 +831,12 @@ static void t_empty_add(void)reftable_stack_destroy (st2); } -static int fastlog2 (uint64_t sz) +static int fastlogN (uint64_t sz,uint64_t N ) {int l =0 ;if (sz ==0 )return 0 ; -for (; sz; sz /=2 ) +for (; sz; sz /=N ) l++;return l -1 ; }
@@ -869,11 +869,43 @@ static void t_reftable_stack_auto_compaction(void) err =reftable_stack_auto_compact (st);check (!err); -check (i <3 || st->merged->readers_len <2 *fastlog2 (i)); +check (i <2 || st->merged->readers_len <2 *fastlogN (i,2 )); } check_int(reftable_stack_compaction_stats(st)->entries_written, <, - (uint64_t )(N *fastlog2 (N ))); + (uint64_t )(N *fastlogN (N ,2 ))); + +reftable_stack_destroy (st); +clear_dir (dir); +} + +static void t_reftable_stack_auto_compaction_factor (void ) +{ +struct reftable_write_options opts = { + .auto_compaction_factor =5 , + }; +struct reftable_stack *st =NULL ; +char *dir =get_tmp_dir (__LINE__); +int err; +size_t N =100 ; + + err =reftable_new_stack (&st, dir, &opts); +check (!err); + +for (size_t i =0 ; i <N ; i++) { +char name[20 ]; +struct reftable_ref_record ref = { + .refname = name, + .update_index =reftable_stack_next_update_index (st), + .value_type =REFTABLE_REF_VAL1 , + }; +xsnprintf (name,sizeof (name),"branch%04" PRIuMAX, (uintmax_t )i); + + err =reftable_stack_add (st, &write_test_ref, &ref); +check (!err); + +check (i <5 || st->merged->readers_len <5 *fastlogN (i,5 )); + }reftable_stack_destroy (st);clear_dir (dir);
@@ -1186,6 +1218,7 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)TEST (t_reftable_stack_add_one (),"add a single ref record to stack" );TEST (t_reftable_stack_add_performs_auto_compaction (),"addition to stack triggers auto-compaction" );TEST (t_reftable_stack_auto_compaction (),"stack must form geometric sequence after compaction" ); +TEST (t_reftable_stack_auto_compaction_factor (),"auto-compaction with non-default geometric factor" );TEST (t_reftable_stack_auto_compaction_fails_gracefully (),"failure on auto-compaction" );TEST (t_reftable_stack_auto_compaction_with_locked_tables (),"auto compaction with locked tables" );TEST (t_reftable_stack_compaction_concurrent (),"compaction with concurrent stack" );
Signature · 1 line
2.45.GIT
PATCH v6 6/6 t-reftable-stack: add test for stack iterators
In reply to Chandra Pratapreftable_stack_init_ref_iterator and reftable_stack_init_log_iterator as defined by reftable/stack.{c,h} initialize a stack iterator to iterate over the ref and log records in a reftable stack respectively. Since these functions are not exercised by any of the existing tests, add a test for them.
--- t/unit-tests/t-reftable-stack.c | 83 +++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+)
t/unit-tests/t-reftable-stack.c +83 -0
@@ -544,6 +544,88 @@ static void t_reftable_stack_add(void)clear_dir (dir); } +static void t_reftable_stack_iterator (void ) +{ +struct reftable_write_options opts = {0 }; +struct reftable_stack *st =NULL ; +char *dir =get_tmp_dir (__LINE__); +struct reftable_ref_record refs[10 ] = {0 }; +struct reftable_log_record logs[10 ] = {0 }; +struct reftable_iterator it = {0 }; +size_t N =ARRAY_SIZE (refs), i; +int err; + + err =reftable_new_stack (&st, dir, &opts); +check (!err); + +for (i =0 ; i <N ; i++) { + refs[i].refname =xstrfmt ("branch%02" PRIuMAX, (uintmax_t )i); + refs[i].update_index = i +1 ; + refs[i].value_type =REFTABLE_REF_VAL1 ; +set_test_hash (refs[i].value.val1, i); + + logs[i].refname =xstrfmt ("branch%02" PRIuMAX, (uintmax_t )i); + logs[i].update_index = i +1 ; + logs[i].value_type =REFTABLE_LOG_UPDATE ; + logs[i].value.update.email =xstrdup ("johndoe@invalid" ); + logs[i].value.update.message =xstrdup ("commit\n" ); +set_test_hash (logs[i].value.update.new_hash, i); + } + +for (i =0 ; i <N ; i++) { + err =reftable_stack_add (st, write_test_ref, &refs[i]); +check (!err); + } + +for (i =0 ; i <N ; i++) { +struct write_log_arg arg = { + .log = &logs[i], + .update_index =reftable_stack_next_update_index (st), + }; + + err =reftable_stack_add (st, write_test_log, &arg); +check (!err); + } + +reftable_stack_init_ref_iterator (st, &it); +reftable_iterator_seek_ref (&it, refs[0 ].refname); +for (i =0 ; ; i++) { +struct reftable_ref_record ref = {0 }; + + err =reftable_iterator_next_ref (&it, &ref); +if (err >0 ) +break ; +check (!err); +check (reftable_ref_record_equal (&ref, &refs[i],GIT_SHA1_RAWSZ )); +reftable_ref_record_release (&ref); + } + check_int(i, ==, N); + +reftable_iterator_destroy (&it); + +reftable_stack_init_log_iterator (st, &it); +reftable_iterator_seek_log (&it, logs[0 ].refname); +for (i =0 ; ; i++) { +struct reftable_log_record log = {0 }; + + err =reftable_iterator_next_log (&it, &log); +if (err >0 ) +break ; +check (!err); +check (reftable_log_record_equal (&log, &logs[i],GIT_SHA1_RAWSZ )); +reftable_log_record_release (&log); + } + check_int(i, ==, N); + +reftable_stack_destroy (st); +reftable_iterator_destroy (&it); +for (i =0 ; i <N ; i++) { +reftable_ref_record_release (&refs[i]); +reftable_log_record_release (&logs[i]); + } +clear_dir (dir); +} +static void t_reftable_stack_log_normalize (void ) {int err =0 ;
@@ -1225,6 +1307,7 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)TEST (t_reftable_stack_compaction_concurrent_clean (),"compaction with unclean stack shutdown" );TEST (t_reftable_stack_compaction_with_locked_tables (),"compaction with locked tables" );TEST (t_reftable_stack_hash_id (),"read stack with wrong hash ID" ); +TEST (t_reftable_stack_iterator (),"log and ref iterator for reftable stack" );TEST (t_reftable_stack_lock_failure (),"stack addition with lockfile failure" );TEST (t_reftable_stack_log_normalize (),"log messages should be normalized" );TEST (t_reftable_stack_read_across_reload (),"stack iterators work across reloads" );
Signature · 1 line
2.45.GIT
Quoted 9 lines · Chandra Pratap
The reftable library comes with self tests, which are exercised as part of the usual end-to-end tests and are designed to observe the end-user visible effects of Git commands. What it exercises, however, is a better match for the unit-testing framework, merged at
8bf6fbd0(Merge branch 'js/doc-unit-tests', 2023-12-09), which is designed to observe how low level implementation details, at the level of sequences of individual function calls, behave.
Quoted 8 lines
Hence, port
reftable/stack_test.cto 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 <[email protected]>Mentored-by Christian Couder <[email protected]>Signed-off-by Chandra Pratap <[email protected]>
Thanks, this version looks good to me.
Patrick