* [PATCH 0/4] t/unit-tests: convert unit-tests to use clar @ 2025-01-16 10:49 Seyi Kuforiji 2025-01-16 10:49 ` [PATCH 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji ` (4 more replies) 0 siblings, 5 replies; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-16 10:49 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, Seyi Kuforiji Hello, This small patch series transitions the existing unit test files to the Clar testing framework. This change is part of our ongoing effort to standardize our testing approach and enhance maintainability. Thanks Seyi Mentored-by: Patrick Steinhardt ps@pks.im Signed-off-by: Seyi Kuforiji kuforiji98@gmail.com Seyi Kuforiji (4): t/unit-tests: handle dashes in test suite filenames t/unit-tests: convert mem-pool test to use clar test framework t/unit-tests: adapt priority queue test to use clar test framework t/unit-tests: convert reftable tree test to use clar test framework Makefile | 6 +- t/meson.build | 6 +- t/unit-tests/generate-clar-decls.sh | 1 + t/unit-tests/t-mem-pool.c | 31 ---------- t/unit-tests/t-prio-queue.c | 91 ---------------------------- t/unit-tests/t-reftable-tree.c | 86 -------------------------- t/unit-tests/u-mem-pool.c | 26 ++++++++ t/unit-tests/u-prio-queue.c | 94 +++++++++++++++++++++++++++++ t/unit-tests/u-reftable-tree.c | 78 ++++++++++++++++++++++++ 9 files changed, 205 insertions(+), 214 deletions(-) delete mode 100644 t/unit-tests/t-mem-pool.c delete mode 100644 t/unit-tests/t-prio-queue.c delete mode 100644 t/unit-tests/t-reftable-tree.c create mode 100644 t/unit-tests/u-mem-pool.c create mode 100644 t/unit-tests/u-prio-queue.c create mode 100644 t/unit-tests/u-reftable-tree.c -- 2.34.1 ^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH 1/4] t/unit-tests: handle dashes in test suite filenames 2025-01-16 10:49 [PATCH 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji @ 2025-01-16 10:49 ` Seyi Kuforiji 2025-01-16 13:12 ` Patrick Steinhardt 2025-01-16 10:49 ` [PATCH 2/4] t/unit-tests: convert mem-pool test to use clar test framework Seyi Kuforiji ` (3 subsequent siblings) 4 siblings, 1 reply; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-16 10:49 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, Seyi Kuforiji Adapt script to translate dashes (`-`) in test suite filenames to underscores (`_`) to ensure proper extraction of test function names. `generate-clar-decls.sh` does not pick up dashes in filenames such as `u-mem-pool.c`, which prevents the scripts from being run. This will be used by subsequent commits which follows the same construct. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> --- t/unit-tests/generate-clar-decls.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/t/unit-tests/generate-clar-decls.sh b/t/unit-tests/generate-clar-decls.sh index 3b315c64b3..abf6a2ea2a 100755 --- a/t/unit-tests/generate-clar-decls.sh +++ b/t/unit-tests/generate-clar-decls.sh @@ -14,6 +14,7 @@ do suite_name=$(basename "$suite") suite_name=${suite_name%.c} suite_name=${suite_name#u-} + suite_name=$(echo "$suite_name" | tr '-' '_') sed -ne "s/^\(void test_${suite_name}__[a-zA-Z_0-9][a-zA-Z_0-9]*(void)\)$/extern \1;/p" "$suite" || exit 1 done >"$OUTPUT" -- 2.34.1 ^ permalink raw reply related [flat|nested] 27+ messages in thread
* Re: [PATCH 1/4] t/unit-tests: handle dashes in test suite filenames 2025-01-16 10:49 ` [PATCH 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji @ 2025-01-16 13:12 ` Patrick Steinhardt 0 siblings, 0 replies; 27+ messages in thread From: Patrick Steinhardt @ 2025-01-16 13:12 UTC (permalink / raw) To: Seyi Kuforiji; +Cc: git, phillip.wood On Thu, Jan 16, 2025 at 11:49:08AM +0100, Seyi Kuforiji wrote: > Adapt script to translate dashes (`-`) in test suite filenames to > underscores (`_`) to ensure proper extraction of test function names. > `generate-clar-decls.sh` does not pick up dashes in filenames such as > `u-mem-pool.c`, which prevents the scripts from being run. This will be > used by subsequent commits which follows the same construct. Nit: it would be nice to provide a bit more context. What might not be immediately be obvious is that the script extracts function signatures that match a specific pattern derived from the unit test file's name, so pointing that out would likely help. Also note that "u-mem-pool.c" is not a script, so saying that these are getting run as scripts is misleading. I'd rather say something like "which prevents the unit tests declared in those suites from being executed". The change itself looks good to me! Patrick ^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH 2/4] t/unit-tests: convert mem-pool test to use clar test framework 2025-01-16 10:49 [PATCH 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji 2025-01-16 10:49 ` [PATCH 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji @ 2025-01-16 10:49 ` Seyi Kuforiji 2025-01-16 13:12 ` Patrick Steinhardt 2025-01-16 10:49 ` [PATCH 3/4] t/unit-tests: adapt priority queue " Seyi Kuforiji ` (2 subsequent siblings) 4 siblings, 1 reply; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-16 10:49 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, Seyi Kuforiji Adapt the mem-pool test script to use clar framework by using clar assertions where necessary. Following the consensus to convert the unit-tests scripts found in the t/unit-tests folder to clar driven by Patrick Steinhardt. Test functions are created as a standalone to test different test cases. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/t-mem-pool.c | 31 ------------------------------- t/unit-tests/u-mem-pool.c | 26 ++++++++++++++++++++++++++ 4 files changed, 28 insertions(+), 33 deletions(-) delete mode 100644 t/unit-tests/t-mem-pool.c create mode 100644 t/unit-tests/u-mem-pool.c diff --git a/Makefile b/Makefile index 97e8385b66..49ada4169d 100644 --- a/Makefile +++ b/Makefile @@ -1338,6 +1338,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/% THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% CLAR_TEST_SUITES += u-ctype +CLAR_TEST_SUITES += u-mem-pool CLAR_TEST_SUITES += u-strvec CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) @@ -1347,7 +1348,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o UNIT_TEST_PROGRAMS += t-example-decorate UNIT_TEST_PROGRAMS += t-hash UNIT_TEST_PROGRAMS += t-hashmap -UNIT_TEST_PROGRAMS += t-mem-pool UNIT_TEST_PROGRAMS += t-oid-array UNIT_TEST_PROGRAMS += t-oidmap UNIT_TEST_PROGRAMS += t-oidtree diff --git a/t/meson.build b/t/meson.build index 602ebfe6a2..ffe951f9be 100644 --- a/t/meson.build +++ b/t/meson.build @@ -1,5 +1,6 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', + 'unit-tests/u-mem-pool.c', 'unit-tests/u-strvec.c', ] @@ -43,7 +44,6 @@ unit_test_programs = [ 'unit-tests/t-example-decorate.c', 'unit-tests/t-hash.c', 'unit-tests/t-hashmap.c', - 'unit-tests/t-mem-pool.c', 'unit-tests/t-oid-array.c', 'unit-tests/t-oidmap.c', 'unit-tests/t-oidtree.c', diff --git a/t/unit-tests/t-mem-pool.c b/t/unit-tests/t-mem-pool.c deleted file mode 100644 index fe500c704b..0000000000 --- a/t/unit-tests/t-mem-pool.c +++ /dev/null @@ -1,31 +0,0 @@ -#include "test-lib.h" -#include "mem-pool.h" - -static void setup_static(void (*f)(struct mem_pool *), size_t block_alloc) -{ - struct mem_pool pool = { .block_alloc = block_alloc }; - f(&pool); - mem_pool_discard(&pool, 0); -} - -static void t_calloc_100(struct mem_pool *pool) -{ - size_t size = 100; - char *buffer = mem_pool_calloc(pool, 1, size); - for (size_t i = 0; i < size; i++) - check_int(buffer[i], ==, 0); - if (!check(pool->mp_block != NULL)) - return; - check(pool->mp_block->next_free != NULL); - check(pool->mp_block->end != NULL); -} - -int cmd_main(int argc UNUSED, const char **argv UNUSED) -{ - TEST(setup_static(t_calloc_100, 1024 * 1024), - "mem_pool_calloc returns 100 zeroed bytes with big block"); - TEST(setup_static(t_calloc_100, 1), - "mem_pool_calloc returns 100 zeroed bytes with tiny block"); - - return test_done(); -} diff --git a/t/unit-tests/u-mem-pool.c b/t/unit-tests/u-mem-pool.c new file mode 100644 index 0000000000..36e31a3201 --- /dev/null +++ b/t/unit-tests/u-mem-pool.c @@ -0,0 +1,26 @@ +#include "unit-test.h" +#include "mem-pool.h" + +static void t_calloc_100(size_t block_alloc) +{ + struct mem_pool pool = { .block_alloc = block_alloc }; + size_t size = 100; + char *buffer = mem_pool_calloc(&pool, 1, size); + for (size_t i = 0; i < size; i++) + cl_assert_equal_i(0, buffer[i]); + cl_assert(pool.mp_block != NULL); + cl_assert(pool.mp_block->next_free != NULL); + cl_assert(pool.mp_block->end != NULL); + mem_pool_discard(&pool, 0); +} + +void test_mem_pool__big_block(void) +{ + t_calloc_100(1024 * 1024); + +} + +void test_mem_pool__tiny_block(void) +{ + t_calloc_100(1); +} -- 2.34.1 ^ permalink raw reply related [flat|nested] 27+ messages in thread
* Re: [PATCH 2/4] t/unit-tests: convert mem-pool test to use clar test framework 2025-01-16 10:49 ` [PATCH 2/4] t/unit-tests: convert mem-pool test to use clar test framework Seyi Kuforiji @ 2025-01-16 13:12 ` Patrick Steinhardt 2025-01-16 17:45 ` Junio C Hamano 0 siblings, 1 reply; 27+ messages in thread From: Patrick Steinhardt @ 2025-01-16 13:12 UTC (permalink / raw) To: Seyi Kuforiji; +Cc: git, phillip.wood On Thu, Jan 16, 2025 at 11:49:09AM +0100, Seyi Kuforiji wrote: > Adapt the mem-pool test script to use clar framework by using clar > assertions where necessary. Following the consensus to convert the > unit-tests scripts found in the t/unit-tests folder to clar driven by > Patrick Steinhardt. I think it's a minor detail that isn't really worth mentioning that I was the one introducing the clar, so I'd leave my name out of it. This also applies to subsequent commit messages. > diff --git a/t/unit-tests/t-mem-pool.c b/t/unit-tests/t-mem-pool.c > deleted file mode 100644 > index fe500c704b..0000000000 > --- a/t/unit-tests/t-mem-pool.c > +++ /dev/null > @@ -1,31 +0,0 @@ > -#include "test-lib.h" > -#include "mem-pool.h" > - > -static void setup_static(void (*f)(struct mem_pool *), size_t block_alloc) > -{ > - struct mem_pool pool = { .block_alloc = block_alloc }; > - f(&pool); > - mem_pool_discard(&pool, 0); > -} > - > -static void t_calloc_100(struct mem_pool *pool) > -{ > - size_t size = 100; > - char *buffer = mem_pool_calloc(pool, 1, size); > - for (size_t i = 0; i < size; i++) > - check_int(buffer[i], ==, 0); > - if (!check(pool->mp_block != NULL)) > - return; > - check(pool->mp_block->next_free != NULL); > - check(pool->mp_block->end != NULL); > -} > - > -int cmd_main(int argc UNUSED, const char **argv UNUSED) > -{ > - TEST(setup_static(t_calloc_100, 1024 * 1024), > - "mem_pool_calloc returns 100 zeroed bytes with big block"); > - TEST(setup_static(t_calloc_100, 1), > - "mem_pool_calloc returns 100 zeroed bytes with tiny block"); > - > - return test_done(); > -} Mh, too bad that Git doesn't render it as a > diff --git a/t/unit-tests/u-mem-pool.c b/t/unit-tests/u-mem-pool.c > new file mode 100644 > index 0000000000..36e31a3201 > --- /dev/null > +++ b/t/unit-tests/u-mem-pool.c > @@ -0,0 +1,26 @@ > +#include "unit-test.h" > +#include "mem-pool.h" > + > +static void t_calloc_100(size_t block_alloc) Can we maybe give this a more descriptive name? Something like `test_many_pool_allocations()` maybe? > +{ > + struct mem_pool pool = { .block_alloc = block_alloc }; > + size_t size = 100; > + char *buffer = mem_pool_calloc(&pool, 1, size); > + for (size_t i = 0; i < size; i++) > + cl_assert_equal_i(0, buffer[i]); > + cl_assert(pool.mp_block != NULL); > + cl_assert(pool.mp_block->next_free != NULL); > + cl_assert(pool.mp_block->end != NULL); > + mem_pool_discard(&pool, 0); > +} > + > +void test_mem_pool__big_block(void) > +{ > + t_calloc_100(1024 * 1024); > + There is a needless empty line here. Other than that the changes look good to me. Patrick ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH 2/4] t/unit-tests: convert mem-pool test to use clar test framework 2025-01-16 13:12 ` Patrick Steinhardt @ 2025-01-16 17:45 ` Junio C Hamano 0 siblings, 0 replies; 27+ messages in thread From: Junio C Hamano @ 2025-01-16 17:45 UTC (permalink / raw) To: Patrick Steinhardt; +Cc: Seyi Kuforiji, git, phillip.wood Patrick Steinhardt <ps@pks.im> writes: > On Thu, Jan 16, 2025 at 11:49:09AM +0100, Seyi Kuforiji wrote: >> Adapt the mem-pool test script to use clar framework by using clar >> assertions where necessary. Following the consensus to convert the >> unit-tests scripts found in the t/unit-tests folder to clar driven by >> Patrick Steinhardt. > > I think it's a minor detail that isn't really worth mentioning that I > was the one introducing the clar, so I'd leave my name out of it. This > also applies to subsequent commit messages. Besides, that part of the description does not read quite grammatical and the rest of the sentence is missing. I.e. "Following the lead by somebody, WE DO SOMETHING". ^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH 3/4] t/unit-tests: adapt priority queue test to use clar test framework 2025-01-16 10:49 [PATCH 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji 2025-01-16 10:49 ` [PATCH 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji 2025-01-16 10:49 ` [PATCH 2/4] t/unit-tests: convert mem-pool test to use clar test framework Seyi Kuforiji @ 2025-01-16 10:49 ` Seyi Kuforiji 2025-01-16 13:13 ` Patrick Steinhardt 2025-01-16 10:49 ` [PATCH 4/4] t/unit-tests: convert reftable tree " Seyi Kuforiji 2025-01-16 16:15 ` [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji 4 siblings, 1 reply; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-16 10:49 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, Seyi Kuforiji Convert the prio-queue test script to clar framework by using clar assertions where necessary. Following the consensus to convert the unit-tests scripts found in the t/unit-tests folder to clar driven by Patrick Steinhardt. Test functions are created as a standalone to test different cases. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/t-prio-queue.c | 91 ----------------------------------- t/unit-tests/u-prio-queue.c | 94 +++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 93 deletions(-) delete mode 100644 t/unit-tests/t-prio-queue.c create mode 100644 t/unit-tests/u-prio-queue.c diff --git a/Makefile b/Makefile index 49ada4169d..049f857512 100644 --- a/Makefile +++ b/Makefile @@ -1339,6 +1339,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% CLAR_TEST_SUITES += u-ctype CLAR_TEST_SUITES += u-mem-pool +CLAR_TEST_SUITES += u-prio-queue CLAR_TEST_SUITES += u-strvec CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) @@ -1351,7 +1352,6 @@ UNIT_TEST_PROGRAMS += t-hashmap UNIT_TEST_PROGRAMS += t-oid-array UNIT_TEST_PROGRAMS += t-oidmap UNIT_TEST_PROGRAMS += t-oidtree -UNIT_TEST_PROGRAMS += t-prio-queue UNIT_TEST_PROGRAMS += t-reftable-basics UNIT_TEST_PROGRAMS += t-reftable-block UNIT_TEST_PROGRAMS += t-reftable-merged diff --git a/t/meson.build b/t/meson.build index ffe951f9be..09232967cd 100644 --- a/t/meson.build +++ b/t/meson.build @@ -1,6 +1,7 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', 'unit-tests/u-mem-pool.c', + 'unit-tests/u-prio-queue.c', 'unit-tests/u-strvec.c', ] @@ -47,7 +48,6 @@ unit_test_programs = [ 'unit-tests/t-oid-array.c', 'unit-tests/t-oidmap.c', 'unit-tests/t-oidtree.c', - 'unit-tests/t-prio-queue.c', 'unit-tests/t-reftable-basics.c', 'unit-tests/t-reftable-block.c', 'unit-tests/t-reftable-merged.c', diff --git a/t/unit-tests/t-prio-queue.c b/t/unit-tests/t-prio-queue.c deleted file mode 100644 index a053635000..0000000000 --- a/t/unit-tests/t-prio-queue.c +++ /dev/null @@ -1,91 +0,0 @@ -#include "test-lib.h" -#include "prio-queue.h" - -static int intcmp(const void *va, const void *vb, void *data UNUSED) -{ - const int *a = va, *b = vb; - return *a - *b; -} - - -#define MISSING -1 -#define DUMP -2 -#define STACK -3 -#define GET -4 -#define REVERSE -5 - -static int show(int *v) -{ - return v ? *v : MISSING; -} - -static void test_prio_queue(int *input, size_t input_size, - int *result, size_t result_size) -{ - struct prio_queue pq = { intcmp }; - int j = 0; - - for (size_t i = 0; i < input_size; i++) { - void *peek, *get; - switch(input[i]) { - case GET: - peek = prio_queue_peek(&pq); - get = prio_queue_get(&pq); - if (!check(peek == get)) - return; - if (!check_uint(j, <, result_size)) - break; - if (!check_int(result[j], ==, show(get))) - test_msg(" j: %d", j); - j++; - break; - case DUMP: - while ((peek = prio_queue_peek(&pq))) { - get = prio_queue_get(&pq); - if (!check(peek == get)) - return; - if (!check_uint(j, <, result_size)) - break; - if (!check_int(result[j], ==, show(get))) - test_msg(" j: %d", j); - j++; - } - break; - case STACK: - pq.compare = NULL; - break; - case REVERSE: - prio_queue_reverse(&pq); - break; - default: - prio_queue_put(&pq, &input[i]); - break; - } - } - check_uint(j, ==, result_size); - clear_prio_queue(&pq); -} - -#define TEST_INPUT(input, result) \ - test_prio_queue(input, ARRAY_SIZE(input), result, ARRAY_SIZE(result)) - -int cmd_main(int argc UNUSED, const char **argv UNUSED) -{ - TEST(TEST_INPUT(((int []){ 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP }), - ((int []){ 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 })), - "prio-queue works for basic input"); - TEST(TEST_INPUT(((int []){ 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP }), - ((int []){ 2, 3, 4, 1, 5, 6 })), - "prio-queue works for mixed put & get commands"); - TEST(TEST_INPUT(((int []){ 1, 2, GET, GET, GET, 1, 2, GET, GET, GET }), - ((int []){ 1, 2, MISSING, 1, 2, MISSING })), - "prio-queue works when queue is empty"); - TEST(TEST_INPUT(((int []){ STACK, 8, 1, 5, 4, 6, 2, 3, DUMP }), - ((int []){ 3, 2, 6, 4, 5, 1, 8 })), - "prio-queue works when used as a LIFO stack"); - TEST(TEST_INPUT(((int []){ STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP }), - ((int []){ 1, 2, 3, 4, 5, 6 })), - "prio-queue works when LIFO stack is reversed"); - - return test_done(); -} diff --git a/t/unit-tests/u-prio-queue.c b/t/unit-tests/u-prio-queue.c new file mode 100644 index 0000000000..d36a565e6f --- /dev/null +++ b/t/unit-tests/u-prio-queue.c @@ -0,0 +1,94 @@ +#include "unit-test.h" +#include "prio-queue.h" + +static int intcmp(const void *va, const void *vb, void *data UNUSED) +{ + const int *a = va, *b = vb; + return *a - *b; +} + + +#define MISSING -1 +#define DUMP -2 +#define STACK -3 +#define GET -4 +#define REVERSE -5 + +static int show(int *v) +{ + return v ? *v : MISSING; +} + +static void test_prio_queue(int *input, size_t input_size, + int *result, size_t result_size) +{ + struct prio_queue pq = { intcmp }; + size_t j = 0; + + for (size_t i = 0; i < input_size; i++) { + void *peek, *get; + switch(input[i]) { + case GET: + peek = prio_queue_peek(&pq); + get = prio_queue_get(&pq); + cl_assert(peek == get); + cl_assert(j < result_size); + cl_assert_equal_i(result[j], show(get)); + j++; + break; + case DUMP: + while ((peek = prio_queue_peek(&pq))) { + get = prio_queue_get(&pq); + cl_assert(peek == get); + cl_assert((size_t)j < result_size); + cl_assert_equal_i(result[j], show(get)); + j++; + } + break; + case STACK: + pq.compare = NULL; + break; + case REVERSE: + prio_queue_reverse(&pq); + break; + default: + prio_queue_put(&pq, &input[i]); + break; + } + } + cl_assert_equal_i(j, result_size); + clear_prio_queue(&pq); +} + +#define TEST_INPUT(input, result) \ + test_prio_queue(input, ARRAY_SIZE(input), result, ARRAY_SIZE(result)) + +void test_prio_queue__basic(void) +{ + TEST_INPUT(((int []){ 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP }), + ((int []){ 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 })); +} + +void test_prio_queue__mixed(void) +{ + TEST_INPUT(((int []){ 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP }), + ((int []){ 2, 3, 4, 1, 5, 6 })); +} + +void test_prio_queue__empty(void) +{ + TEST_INPUT(((int []){ 1, 2, GET, GET, GET, 1, 2, GET, GET, GET }), + ((int []){ 1, 2, MISSING, 1, 2, MISSING })); +} + +void test_prio_queue__stack(void) +{ + TEST_INPUT(((int []){ STACK, 8, 1, 5, 4, 6, 2, 3, DUMP }), + ((int []){ 3, 2, 6, 4, 5, 1, 8 })); +} + +void test_prio_queue__reverse_stack(void) +{ + TEST_INPUT(((int []){ STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP }), + ((int []){ 1, 2, 3, 4, 5, 6 })); +} -- 2.34.1 ^ permalink raw reply related [flat|nested] 27+ messages in thread
* Re: [PATCH 3/4] t/unit-tests: adapt priority queue test to use clar test framework 2025-01-16 10:49 ` [PATCH 3/4] t/unit-tests: adapt priority queue " Seyi Kuforiji @ 2025-01-16 13:13 ` Patrick Steinhardt 0 siblings, 0 replies; 27+ messages in thread From: Patrick Steinhardt @ 2025-01-16 13:13 UTC (permalink / raw) To: Seyi Kuforiji; +Cc: git, phillip.wood On Thu, Jan 16, 2025 at 11:49:10AM +0100, Seyi Kuforiji wrote: > diff --git a/t/unit-tests/t-prio-queue.c b/t/unit-tests/t-prio-queue.c > deleted file mode 100644 > index a053635000..0000000000 > --- a/t/unit-tests/t-prio-queue.c > +++ /dev/null Hm. A bit surprising that Git decides to not render this as a rename, as most of `test_prio_queue()` is unchanged. > diff --git a/t/unit-tests/u-prio-queue.c b/t/unit-tests/u-prio-queue.c > new file mode 100644 > index 0000000000..d36a565e6f > --- /dev/null > +++ b/t/unit-tests/u-prio-queue.c > @@ -0,0 +1,94 @@ > +#include "unit-test.h" > +#include "prio-queue.h" > + > +static int intcmp(const void *va, const void *vb, void *data UNUSED) > +{ > + const int *a = va, *b = vb; > + return *a - *b; > +} > + > + > +#define MISSING -1 > +#define DUMP -2 > +#define STACK -3 > +#define GET -4 > +#define REVERSE -5 > + > +static int show(int *v) > +{ > + return v ? *v : MISSING; > +} > + > +static void test_prio_queue(int *input, size_t input_size, > + int *result, size_t result_size) > +{ > + struct prio_queue pq = { intcmp }; > + size_t j = 0; This is a `size_t` now, which is different compared to before. Might be worthwhile to point out why you did this in the commit message. > + for (size_t i = 0; i < input_size; i++) { > + void *peek, *get; > + switch(input[i]) { > + case GET: > + peek = prio_queue_peek(&pq); > + get = prio_queue_get(&pq); > + cl_assert(peek == get); > + cl_assert(j < result_size); > + cl_assert_equal_i(result[j], show(get)); > + j++; > + break; > + case DUMP: > + while ((peek = prio_queue_peek(&pq))) { > + get = prio_queue_get(&pq); > + cl_assert(peek == get); > + cl_assert((size_t)j < result_size); This here is the reason, to avoid -Wsign-compare. But the cast here isn't necessary now that you've adapted `j` to be a `size_t` anyway. > + cl_assert_equal_i(result[j], show(get)); > + j++; > + } > + break; > + case STACK: > + pq.compare = NULL; > + break; > + case REVERSE: > + prio_queue_reverse(&pq); > + break; > + default: > + prio_queue_put(&pq, &input[i]); > + break; > + } > + } > + cl_assert_equal_i(j, result_size); > + clear_prio_queue(&pq); > +} > + > +#define TEST_INPUT(input, result) \ > + test_prio_queue(input, ARRAY_SIZE(input), result, ARRAY_SIZE(result)) > + > +void test_prio_queue__basic(void) > +{ > + TEST_INPUT(((int []){ 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP }), > + ((int []){ 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 })); > +} > + > +void test_prio_queue__mixed(void) > +{ > + TEST_INPUT(((int []){ 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP }), > + ((int []){ 2, 3, 4, 1, 5, 6 })); > +} > + > +void test_prio_queue__empty(void) > +{ > + TEST_INPUT(((int []){ 1, 2, GET, GET, GET, 1, 2, GET, GET, GET }), > + ((int []){ 1, 2, MISSING, 1, 2, MISSING })); > +} > + > +void test_prio_queue__stack(void) > +{ > + TEST_INPUT(((int []){ STACK, 8, 1, 5, 4, 6, 2, 3, DUMP }), > + ((int []){ 3, 2, 6, 4, 5, 1, 8 })); > +} > + > +void test_prio_queue__reverse_stack(void) > +{ > + TEST_INPUT(((int []){ STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP }), > + ((int []){ 1, 2, 3, 4, 5, 6 })); > +} All of these look like failthful conversions to me. Patrick ^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH 4/4] t/unit-tests: convert reftable tree test to use clar test framework 2025-01-16 10:49 [PATCH 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji ` (2 preceding siblings ...) 2025-01-16 10:49 ` [PATCH 3/4] t/unit-tests: adapt priority queue " Seyi Kuforiji @ 2025-01-16 10:49 ` Seyi Kuforiji 2025-01-16 16:15 ` [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji 4 siblings, 0 replies; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-16 10:49 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, Seyi Kuforiji Adapts reftable tree test script to clar framework by using clar assertions where necessary. Following the consensus to convert the unit-tests scripts found in the t/unit-tests folder to clar driven by Patrick Steinhardt. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/t-reftable-tree.c | 86 ---------------------------------- t/unit-tests/u-reftable-tree.c | 78 ++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 88 deletions(-) delete mode 100644 t/unit-tests/t-reftable-tree.c create mode 100644 t/unit-tests/u-reftable-tree.c diff --git a/Makefile b/Makefile index 049f857512..75dbb8e25f 100644 --- a/Makefile +++ b/Makefile @@ -1340,6 +1340,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% CLAR_TEST_SUITES += u-ctype CLAR_TEST_SUITES += u-mem-pool CLAR_TEST_SUITES += u-prio-queue +CLAR_TEST_SUITES += u-reftable-tree CLAR_TEST_SUITES += u-strvec CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) @@ -1360,7 +1361,6 @@ UNIT_TEST_PROGRAMS += t-reftable-reader 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 UNIT_TEST_PROGRAMS += t-trailer diff --git a/t/meson.build b/t/meson.build index 09232967cd..6dd41216ef 100644 --- a/t/meson.build +++ b/t/meson.build @@ -2,6 +2,7 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', 'unit-tests/u-mem-pool.c', 'unit-tests/u-prio-queue.c', + 'unit-tests/u-reftable-tree.c', 'unit-tests/u-strvec.c', ] @@ -56,7 +57,6 @@ unit_test_programs = [ 'unit-tests/t-reftable-readwrite.c', 'unit-tests/t-reftable-record.c', 'unit-tests/t-reftable-stack.c', - 'unit-tests/t-reftable-tree.c', 'unit-tests/t-strbuf.c', 'unit-tests/t-strcmp-offset.c', 'unit-tests/t-trailer.c', diff --git a/t/unit-tests/t-reftable-tree.c b/t/unit-tests/t-reftable-tree.c deleted file mode 100644 index 79b175a45a..0000000000 --- a/t/unit-tests/t-reftable-tree.c +++ /dev/null @@ -1,86 +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 "test-lib.h" -#include "reftable/tree.h" - -static int t_compare(const void *a, const void *b) -{ - return (char *)a - (char *)b; -} - -struct curry { - void **arr; - size_t len; -}; - -static void store(void *arg, void *key) -{ - struct curry *c = arg; - c->arr[c->len++] = key; -} - -static void t_tree_search(void) -{ - struct tree_node *root = NULL; - void *values[11] = { 0 }; - struct tree_node *nodes[11] = { 0 }; - size_t i = 1; - - /* - * Pseudo-randomly insert the pointers for elements between - * values[1] and values[10] (inclusive) in the tree. - */ - do { - nodes[i] = tree_insert(&root, &values[i], &t_compare); - check(nodes[i] != NULL); - i = (i * 7) % 11; - } while (i != 1); - - for (i = 1; i < ARRAY_SIZE(nodes); i++) { - check_pointer_eq(&values[i], nodes[i]->key); - check_pointer_eq(nodes[i], tree_search(root, &values[i], &t_compare)); - } - - check(!tree_search(root, values, t_compare)); - tree_free(root); -} - -static void t_infix_walk(void) -{ - struct tree_node *root = NULL; - void *values[11] = { 0 }; - void *out[11] = { 0 }; - struct curry c = { - .arr = (void **) &out, - }; - size_t i = 1; - size_t count = 0; - - do { - struct tree_node *node = tree_insert(&root, &values[i], t_compare); - check(node != NULL); - i = (i * 7) % 11; - count++; - } while (i != 1); - - infix_walk(root, &store, &c); - for (i = 1; i < ARRAY_SIZE(values); i++) - check_pointer_eq(&values[i], out[i - 1]); - check(!out[i - 1]); - check_int(c.len, ==, count); - tree_free(root); -} - -int cmd_main(int argc UNUSED, const char *argv[] UNUSED) -{ - TEST(t_tree_search(), "tree_search works"); - TEST(t_infix_walk(), "infix_walk works"); - - return test_done(); -} diff --git a/t/unit-tests/u-reftable-tree.c b/t/unit-tests/u-reftable-tree.c new file mode 100644 index 0000000000..bcf9061071 --- /dev/null +++ b/t/unit-tests/u-reftable-tree.c @@ -0,0 +1,78 @@ +/* +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 "unit-test.h" +#include "reftable/tree.h" + +static int t_compare(const void *a, const void *b) +{ + return (char *)a - (char *)b; +} + +struct curry { + void **arr; + size_t len; +}; + +static void store(void *arg, void *key) +{ + struct curry *c = arg; + c->arr[c->len++] = key; +} + +void test_reftable_tree__tree_search(void) +{ + struct tree_node *root = NULL; + void *values[11] = { 0 }; + struct tree_node *nodes[11] = { 0 }; + size_t i = 1; + + /* + * Pseudo-randomly insert the pointers for elements between + * values[1] and values[10] (inclusive) in the tree. + */ + do { + nodes[i] = tree_insert(&root, &values[i], &t_compare); + cl_assert(nodes[i] != NULL); + i = (i * 7) % 11; + } while (i != 1); + + for (i = 1; i < ARRAY_SIZE(nodes); i++) { + cl_assert_equal_p(&values[i], nodes[i]->key); + cl_assert_equal_p(nodes[i], tree_search(root, &values[i], &t_compare)); + } + + cl_assert(tree_search(root, values, t_compare) == NULL); + tree_free(root); +} + +void test_reftable_tree__infix_walk(void) +{ + struct tree_node *root = NULL; + void *values[11] = { 0 }; + void *out[11] = { 0 }; + struct curry c = { + .arr = (void **) &out, + }; + size_t i = 1; + size_t count = 0; + + do { + struct tree_node *node = tree_insert(&root, &values[i], t_compare); + cl_assert(node != NULL); + i = (i * 7) % 11; + count++; + } while (i != 1); + + infix_walk(root, &store, &c); + for (i = 1; i < ARRAY_SIZE(values); i++) + cl_assert_equal_p(&values[i], out[i - 1]); + cl_assert(out[i - 1] == NULL); + cl_assert_equal_i(c.len, count); + tree_free(root); +} -- 2.34.1 ^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar 2025-01-16 10:49 [PATCH 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji ` (3 preceding siblings ...) 2025-01-16 10:49 ` [PATCH 4/4] t/unit-tests: convert reftable tree " Seyi Kuforiji @ 2025-01-16 16:15 ` Seyi Kuforiji 2025-01-16 16:15 ` [PATCH v2 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji ` (5 more replies) 4 siblings, 6 replies; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-16 16:15 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, Seyi Kuforiji Hello, This small patch series transitions the existing unit test files to the Clar testing framework. This change is part of our ongoing effort to standardize our testing approach and enhance maintainability. changes in v2: - Some small fixes were made to the commit messages - changes was made to the code format Thanks Seyi Mentored-by: Patrick Steinhardt ps@pks.im Signed-off-by: Seyi Kuforiji kuforiji98@gmail.com Seyi Kuforiji (4): t/unit-tests: handle dashes in test suite filenames t/unit-tests: convert mem-pool test to use clar test framework t/unit-tests: adapt priority queue test to use clar test framework t/unit-tests: convert reftable tree test to use clar test framework Makefile | 6 +- t/meson.build | 6 +- t/unit-tests/generate-clar-decls.sh | 1 + t/unit-tests/t-mem-pool.c | 31 ---------- t/unit-tests/t-prio-queue.c | 91 ---------------------------- t/unit-tests/t-reftable-tree.c | 86 -------------------------- t/unit-tests/u-mem-pool.c | 25 ++++++++ t/unit-tests/u-prio-queue.c | 94 +++++++++++++++++++++++++++++ t/unit-tests/u-reftable-tree.c | 78 ++++++++++++++++++++++++ 9 files changed, 204 insertions(+), 214 deletions(-) delete mode 100644 t/unit-tests/t-mem-pool.c delete mode 100644 t/unit-tests/t-prio-queue.c delete mode 100644 t/unit-tests/t-reftable-tree.c create mode 100644 t/unit-tests/u-mem-pool.c create mode 100644 t/unit-tests/u-prio-queue.c create mode 100644 t/unit-tests/u-reftable-tree.c Range-diff against v1: 1: 00bb4d2880 = 1: 00bb4d2880 t/unit-tests: handle dashes in test suite filenames 2: 59abf45f08 = 2: 59abf45f08 t/unit-tests: convert mem-pool test to use clar test framework 3: 6e01da141d = 3: 6e01da141d t/unit-tests: adapt priority queue test to use clar test framework 4: b397a5f131 = 4: b397a5f131 t/unit-tests: convert reftable tree test to use clar test framework -- 2.34.1 ^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v2 1/4] t/unit-tests: handle dashes in test suite filenames 2025-01-16 16:15 ` [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji @ 2025-01-16 16:15 ` Seyi Kuforiji 2025-01-17 6:27 ` Patrick Steinhardt 2025-01-16 16:15 ` [PATCH v2 2/4] t/unit-tests: convert mem-pool test to use clar test framework Seyi Kuforiji ` (4 subsequent siblings) 5 siblings, 1 reply; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-16 16:15 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, Seyi Kuforiji The script is designed to extract function signatures that match a specific pattern derived from the unit test file's name. `generate-clar-decls.sh` does not pick up dashes in filenames, which prevents the scripts from being run. Adapt script to translate dashes (`-`) in test suite filenames to underscores (`_`) to correctly extract the function signatures and run the corresponding tests. This will be used by subsequent commits which follows the same construct. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> --- t/unit-tests/generate-clar-decls.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/t/unit-tests/generate-clar-decls.sh b/t/unit-tests/generate-clar-decls.sh index 3b315c64b3..abf6a2ea2a 100755 --- a/t/unit-tests/generate-clar-decls.sh +++ b/t/unit-tests/generate-clar-decls.sh @@ -14,6 +14,7 @@ do suite_name=$(basename "$suite") suite_name=${suite_name%.c} suite_name=${suite_name#u-} + suite_name=$(echo "$suite_name" | tr '-' '_') sed -ne "s/^\(void test_${suite_name}__[a-zA-Z_0-9][a-zA-Z_0-9]*(void)\)$/extern \1;/p" "$suite" || exit 1 done >"$OUTPUT" -- 2.34.1 ^ permalink raw reply related [flat|nested] 27+ messages in thread
* Re: [PATCH v2 1/4] t/unit-tests: handle dashes in test suite filenames 2025-01-16 16:15 ` [PATCH v2 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji @ 2025-01-17 6:27 ` Patrick Steinhardt 0 siblings, 0 replies; 27+ messages in thread From: Patrick Steinhardt @ 2025-01-17 6:27 UTC (permalink / raw) To: Seyi Kuforiji; +Cc: git, phillip.wood On Thu, Jan 16, 2025 at 05:15:56PM +0100, Seyi Kuforiji wrote: > The script is designed to extract function signatures that match a > specific pattern derived from the unit test file's name. > `generate-clar-decls.sh` does not pick up dashes in filenames, which > prevents the scripts from being run. As said in my first round, saying "scripts" here is misleading as the unit tests aren't scripts in the first place. How about: The "generate-clar-decls.sh" script is designed to extract function signatures that match a specific pattern derived from the unit test file's name. The script does not know to massage file names with dashes, which will make it search for functions that look like, for example, `test_mem-pool_*`. Having dashes in function names is not allowed though, so these patterns won't ever match a legal function name. Patrick ^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v2 2/4] t/unit-tests: convert mem-pool test to use clar test framework 2025-01-16 16:15 ` [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji 2025-01-16 16:15 ` [PATCH v2 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji @ 2025-01-16 16:15 ` Seyi Kuforiji 2025-01-16 16:15 ` [PATCH v2 3/4] t/unit-tests: adapt priority queue " Seyi Kuforiji ` (3 subsequent siblings) 5 siblings, 0 replies; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-16 16:15 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, Seyi Kuforiji Adapt the mem-pool test script to use clar framework by using clar assertions where necessary.Test functions are created as a standalone to test different test cases. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/t-mem-pool.c | 31 ------------------------------- t/unit-tests/u-mem-pool.c | 25 +++++++++++++++++++++++++ 4 files changed, 27 insertions(+), 33 deletions(-) delete mode 100644 t/unit-tests/t-mem-pool.c create mode 100644 t/unit-tests/u-mem-pool.c diff --git a/Makefile b/Makefile index 97e8385b66..49ada4169d 100644 --- a/Makefile +++ b/Makefile @@ -1338,6 +1338,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/% THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% CLAR_TEST_SUITES += u-ctype +CLAR_TEST_SUITES += u-mem-pool CLAR_TEST_SUITES += u-strvec CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) @@ -1347,7 +1348,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o UNIT_TEST_PROGRAMS += t-example-decorate UNIT_TEST_PROGRAMS += t-hash UNIT_TEST_PROGRAMS += t-hashmap -UNIT_TEST_PROGRAMS += t-mem-pool UNIT_TEST_PROGRAMS += t-oid-array UNIT_TEST_PROGRAMS += t-oidmap UNIT_TEST_PROGRAMS += t-oidtree diff --git a/t/meson.build b/t/meson.build index 602ebfe6a2..ffe951f9be 100644 --- a/t/meson.build +++ b/t/meson.build @@ -1,5 +1,6 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', + 'unit-tests/u-mem-pool.c', 'unit-tests/u-strvec.c', ] @@ -43,7 +44,6 @@ unit_test_programs = [ 'unit-tests/t-example-decorate.c', 'unit-tests/t-hash.c', 'unit-tests/t-hashmap.c', - 'unit-tests/t-mem-pool.c', 'unit-tests/t-oid-array.c', 'unit-tests/t-oidmap.c', 'unit-tests/t-oidtree.c', diff --git a/t/unit-tests/t-mem-pool.c b/t/unit-tests/t-mem-pool.c deleted file mode 100644 index fe500c704b..0000000000 --- a/t/unit-tests/t-mem-pool.c +++ /dev/null @@ -1,31 +0,0 @@ -#include "test-lib.h" -#include "mem-pool.h" - -static void setup_static(void (*f)(struct mem_pool *), size_t block_alloc) -{ - struct mem_pool pool = { .block_alloc = block_alloc }; - f(&pool); - mem_pool_discard(&pool, 0); -} - -static void t_calloc_100(struct mem_pool *pool) -{ - size_t size = 100; - char *buffer = mem_pool_calloc(pool, 1, size); - for (size_t i = 0; i < size; i++) - check_int(buffer[i], ==, 0); - if (!check(pool->mp_block != NULL)) - return; - check(pool->mp_block->next_free != NULL); - check(pool->mp_block->end != NULL); -} - -int cmd_main(int argc UNUSED, const char **argv UNUSED) -{ - TEST(setup_static(t_calloc_100, 1024 * 1024), - "mem_pool_calloc returns 100 zeroed bytes with big block"); - TEST(setup_static(t_calloc_100, 1), - "mem_pool_calloc returns 100 zeroed bytes with tiny block"); - - return test_done(); -} diff --git a/t/unit-tests/u-mem-pool.c b/t/unit-tests/u-mem-pool.c new file mode 100644 index 0000000000..2bc2493b7e --- /dev/null +++ b/t/unit-tests/u-mem-pool.c @@ -0,0 +1,25 @@ +#include "unit-test.h" +#include "mem-pool.h" + +static void test_many_pool_allocations(size_t block_alloc) +{ + struct mem_pool pool = { .block_alloc = block_alloc }; + size_t size = 100; + char *buffer = mem_pool_calloc(&pool, 1, size); + for (size_t i = 0; i < size; i++) + cl_assert_equal_i(0, buffer[i]); + cl_assert(pool.mp_block != NULL); + cl_assert(pool.mp_block->next_free != NULL); + cl_assert(pool.mp_block->end != NULL); + mem_pool_discard(&pool, 0); +} + +void test_mem_pool__big_block(void) +{ + test_many_pool_allocations(1024 * 1024); +} + +void test_mem_pool__tiny_block(void) +{ + test_many_pool_allocations(1); +} -- 2.34.1 ^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH v2 3/4] t/unit-tests: adapt priority queue test to use clar test framework 2025-01-16 16:15 ` [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji 2025-01-16 16:15 ` [PATCH v2 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji 2025-01-16 16:15 ` [PATCH v2 2/4] t/unit-tests: convert mem-pool test to use clar test framework Seyi Kuforiji @ 2025-01-16 16:15 ` Seyi Kuforiji 2025-01-16 16:15 ` [PATCH v2 4/4] t/unit-tests: convert reftable tree " Seyi Kuforiji ` (2 subsequent siblings) 5 siblings, 0 replies; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-16 16:15 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, Seyi Kuforiji Convert the prio-queue test script to clar framework by using clar assertions where necessary. Test functions are created as a standalone to test different cases. update the type of the variable `j` from int to `size_t`, this ensures compatibility with the type used for result_size, which is also size_t, preventing a potential warning or error caused by comparisons between signed and unsigned integers. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/t-prio-queue.c | 91 ----------------------------------- t/unit-tests/u-prio-queue.c | 94 +++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 93 deletions(-) delete mode 100644 t/unit-tests/t-prio-queue.c create mode 100644 t/unit-tests/u-prio-queue.c diff --git a/Makefile b/Makefile index 49ada4169d..049f857512 100644 --- a/Makefile +++ b/Makefile @@ -1339,6 +1339,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% CLAR_TEST_SUITES += u-ctype CLAR_TEST_SUITES += u-mem-pool +CLAR_TEST_SUITES += u-prio-queue CLAR_TEST_SUITES += u-strvec CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) @@ -1351,7 +1352,6 @@ UNIT_TEST_PROGRAMS += t-hashmap UNIT_TEST_PROGRAMS += t-oid-array UNIT_TEST_PROGRAMS += t-oidmap UNIT_TEST_PROGRAMS += t-oidtree -UNIT_TEST_PROGRAMS += t-prio-queue UNIT_TEST_PROGRAMS += t-reftable-basics UNIT_TEST_PROGRAMS += t-reftable-block UNIT_TEST_PROGRAMS += t-reftable-merged diff --git a/t/meson.build b/t/meson.build index ffe951f9be..09232967cd 100644 --- a/t/meson.build +++ b/t/meson.build @@ -1,6 +1,7 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', 'unit-tests/u-mem-pool.c', + 'unit-tests/u-prio-queue.c', 'unit-tests/u-strvec.c', ] @@ -47,7 +48,6 @@ unit_test_programs = [ 'unit-tests/t-oid-array.c', 'unit-tests/t-oidmap.c', 'unit-tests/t-oidtree.c', - 'unit-tests/t-prio-queue.c', 'unit-tests/t-reftable-basics.c', 'unit-tests/t-reftable-block.c', 'unit-tests/t-reftable-merged.c', diff --git a/t/unit-tests/t-prio-queue.c b/t/unit-tests/t-prio-queue.c deleted file mode 100644 index a053635000..0000000000 --- a/t/unit-tests/t-prio-queue.c +++ /dev/null @@ -1,91 +0,0 @@ -#include "test-lib.h" -#include "prio-queue.h" - -static int intcmp(const void *va, const void *vb, void *data UNUSED) -{ - const int *a = va, *b = vb; - return *a - *b; -} - - -#define MISSING -1 -#define DUMP -2 -#define STACK -3 -#define GET -4 -#define REVERSE -5 - -static int show(int *v) -{ - return v ? *v : MISSING; -} - -static void test_prio_queue(int *input, size_t input_size, - int *result, size_t result_size) -{ - struct prio_queue pq = { intcmp }; - int j = 0; - - for (size_t i = 0; i < input_size; i++) { - void *peek, *get; - switch(input[i]) { - case GET: - peek = prio_queue_peek(&pq); - get = prio_queue_get(&pq); - if (!check(peek == get)) - return; - if (!check_uint(j, <, result_size)) - break; - if (!check_int(result[j], ==, show(get))) - test_msg(" j: %d", j); - j++; - break; - case DUMP: - while ((peek = prio_queue_peek(&pq))) { - get = prio_queue_get(&pq); - if (!check(peek == get)) - return; - if (!check_uint(j, <, result_size)) - break; - if (!check_int(result[j], ==, show(get))) - test_msg(" j: %d", j); - j++; - } - break; - case STACK: - pq.compare = NULL; - break; - case REVERSE: - prio_queue_reverse(&pq); - break; - default: - prio_queue_put(&pq, &input[i]); - break; - } - } - check_uint(j, ==, result_size); - clear_prio_queue(&pq); -} - -#define TEST_INPUT(input, result) \ - test_prio_queue(input, ARRAY_SIZE(input), result, ARRAY_SIZE(result)) - -int cmd_main(int argc UNUSED, const char **argv UNUSED) -{ - TEST(TEST_INPUT(((int []){ 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP }), - ((int []){ 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 })), - "prio-queue works for basic input"); - TEST(TEST_INPUT(((int []){ 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP }), - ((int []){ 2, 3, 4, 1, 5, 6 })), - "prio-queue works for mixed put & get commands"); - TEST(TEST_INPUT(((int []){ 1, 2, GET, GET, GET, 1, 2, GET, GET, GET }), - ((int []){ 1, 2, MISSING, 1, 2, MISSING })), - "prio-queue works when queue is empty"); - TEST(TEST_INPUT(((int []){ STACK, 8, 1, 5, 4, 6, 2, 3, DUMP }), - ((int []){ 3, 2, 6, 4, 5, 1, 8 })), - "prio-queue works when used as a LIFO stack"); - TEST(TEST_INPUT(((int []){ STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP }), - ((int []){ 1, 2, 3, 4, 5, 6 })), - "prio-queue works when LIFO stack is reversed"); - - return test_done(); -} diff --git a/t/unit-tests/u-prio-queue.c b/t/unit-tests/u-prio-queue.c new file mode 100644 index 0000000000..145e689c9c --- /dev/null +++ b/t/unit-tests/u-prio-queue.c @@ -0,0 +1,94 @@ +#include "unit-test.h" +#include "prio-queue.h" + +static int intcmp(const void *va, const void *vb, void *data UNUSED) +{ + const int *a = va, *b = vb; + return *a - *b; +} + + +#define MISSING -1 +#define DUMP -2 +#define STACK -3 +#define GET -4 +#define REVERSE -5 + +static int show(int *v) +{ + return v ? *v : MISSING; +} + +static void test_prio_queue(int *input, size_t input_size, + int *result, size_t result_size) +{ + struct prio_queue pq = { intcmp }; + size_t j = 0; + + for (size_t i = 0; i < input_size; i++) { + void *peek, *get; + switch(input[i]) { + case GET: + peek = prio_queue_peek(&pq); + get = prio_queue_get(&pq); + cl_assert(peek == get); + cl_assert(j < result_size); + cl_assert_equal_i(result[j], show(get)); + j++; + break; + case DUMP: + while ((peek = prio_queue_peek(&pq))) { + get = prio_queue_get(&pq); + cl_assert(peek == get); + cl_assert(j < result_size); + cl_assert_equal_i(result[j], show(get)); + j++; + } + break; + case STACK: + pq.compare = NULL; + break; + case REVERSE: + prio_queue_reverse(&pq); + break; + default: + prio_queue_put(&pq, &input[i]); + break; + } + } + cl_assert_equal_i(j, result_size); + clear_prio_queue(&pq); +} + +#define TEST_INPUT(input, result) \ + test_prio_queue(input, ARRAY_SIZE(input), result, ARRAY_SIZE(result)) + +void test_prio_queue__basic(void) +{ + TEST_INPUT(((int []){ 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP }), + ((int []){ 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 })); +} + +void test_prio_queue__mixed(void) +{ + TEST_INPUT(((int []){ 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP }), + ((int []){ 2, 3, 4, 1, 5, 6 })); +} + +void test_prio_queue__empty(void) +{ + TEST_INPUT(((int []){ 1, 2, GET, GET, GET, 1, 2, GET, GET, GET }), + ((int []){ 1, 2, MISSING, 1, 2, MISSING })); +} + +void test_prio_queue__stack(void) +{ + TEST_INPUT(((int []){ STACK, 8, 1, 5, 4, 6, 2, 3, DUMP }), + ((int []){ 3, 2, 6, 4, 5, 1, 8 })); +} + +void test_prio_queue__reverse_stack(void) +{ + TEST_INPUT(((int []){ STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP }), + ((int []){ 1, 2, 3, 4, 5, 6 })); +} -- 2.34.1 ^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH v2 4/4] t/unit-tests: convert reftable tree test to use clar test framework 2025-01-16 16:15 ` [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji ` (2 preceding siblings ...) 2025-01-16 16:15 ` [PATCH v2 3/4] t/unit-tests: adapt priority queue " Seyi Kuforiji @ 2025-01-16 16:15 ` Seyi Kuforiji 2025-01-17 6:27 ` [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar Patrick Steinhardt 2025-01-17 12:29 ` Seyi Kuforiji 5 siblings, 0 replies; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-16 16:15 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, Seyi Kuforiji Adapts reftable tree test script to clar framework by using clar assertions where necessary. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/t-reftable-tree.c | 86 ---------------------------------- t/unit-tests/u-reftable-tree.c | 78 ++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 88 deletions(-) delete mode 100644 t/unit-tests/t-reftable-tree.c create mode 100644 t/unit-tests/u-reftable-tree.c diff --git a/Makefile b/Makefile index 049f857512..75dbb8e25f 100644 --- a/Makefile +++ b/Makefile @@ -1340,6 +1340,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% CLAR_TEST_SUITES += u-ctype CLAR_TEST_SUITES += u-mem-pool CLAR_TEST_SUITES += u-prio-queue +CLAR_TEST_SUITES += u-reftable-tree CLAR_TEST_SUITES += u-strvec CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) @@ -1360,7 +1361,6 @@ UNIT_TEST_PROGRAMS += t-reftable-reader 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 UNIT_TEST_PROGRAMS += t-trailer diff --git a/t/meson.build b/t/meson.build index 09232967cd..6dd41216ef 100644 --- a/t/meson.build +++ b/t/meson.build @@ -2,6 +2,7 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', 'unit-tests/u-mem-pool.c', 'unit-tests/u-prio-queue.c', + 'unit-tests/u-reftable-tree.c', 'unit-tests/u-strvec.c', ] @@ -56,7 +57,6 @@ unit_test_programs = [ 'unit-tests/t-reftable-readwrite.c', 'unit-tests/t-reftable-record.c', 'unit-tests/t-reftable-stack.c', - 'unit-tests/t-reftable-tree.c', 'unit-tests/t-strbuf.c', 'unit-tests/t-strcmp-offset.c', 'unit-tests/t-trailer.c', diff --git a/t/unit-tests/t-reftable-tree.c b/t/unit-tests/t-reftable-tree.c deleted file mode 100644 index 79b175a45a..0000000000 --- a/t/unit-tests/t-reftable-tree.c +++ /dev/null @@ -1,86 +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 "test-lib.h" -#include "reftable/tree.h" - -static int t_compare(const void *a, const void *b) -{ - return (char *)a - (char *)b; -} - -struct curry { - void **arr; - size_t len; -}; - -static void store(void *arg, void *key) -{ - struct curry *c = arg; - c->arr[c->len++] = key; -} - -static void t_tree_search(void) -{ - struct tree_node *root = NULL; - void *values[11] = { 0 }; - struct tree_node *nodes[11] = { 0 }; - size_t i = 1; - - /* - * Pseudo-randomly insert the pointers for elements between - * values[1] and values[10] (inclusive) in the tree. - */ - do { - nodes[i] = tree_insert(&root, &values[i], &t_compare); - check(nodes[i] != NULL); - i = (i * 7) % 11; - } while (i != 1); - - for (i = 1; i < ARRAY_SIZE(nodes); i++) { - check_pointer_eq(&values[i], nodes[i]->key); - check_pointer_eq(nodes[i], tree_search(root, &values[i], &t_compare)); - } - - check(!tree_search(root, values, t_compare)); - tree_free(root); -} - -static void t_infix_walk(void) -{ - struct tree_node *root = NULL; - void *values[11] = { 0 }; - void *out[11] = { 0 }; - struct curry c = { - .arr = (void **) &out, - }; - size_t i = 1; - size_t count = 0; - - do { - struct tree_node *node = tree_insert(&root, &values[i], t_compare); - check(node != NULL); - i = (i * 7) % 11; - count++; - } while (i != 1); - - infix_walk(root, &store, &c); - for (i = 1; i < ARRAY_SIZE(values); i++) - check_pointer_eq(&values[i], out[i - 1]); - check(!out[i - 1]); - check_int(c.len, ==, count); - tree_free(root); -} - -int cmd_main(int argc UNUSED, const char *argv[] UNUSED) -{ - TEST(t_tree_search(), "tree_search works"); - TEST(t_infix_walk(), "infix_walk works"); - - return test_done(); -} diff --git a/t/unit-tests/u-reftable-tree.c b/t/unit-tests/u-reftable-tree.c new file mode 100644 index 0000000000..bcf9061071 --- /dev/null +++ b/t/unit-tests/u-reftable-tree.c @@ -0,0 +1,78 @@ +/* +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 "unit-test.h" +#include "reftable/tree.h" + +static int t_compare(const void *a, const void *b) +{ + return (char *)a - (char *)b; +} + +struct curry { + void **arr; + size_t len; +}; + +static void store(void *arg, void *key) +{ + struct curry *c = arg; + c->arr[c->len++] = key; +} + +void test_reftable_tree__tree_search(void) +{ + struct tree_node *root = NULL; + void *values[11] = { 0 }; + struct tree_node *nodes[11] = { 0 }; + size_t i = 1; + + /* + * Pseudo-randomly insert the pointers for elements between + * values[1] and values[10] (inclusive) in the tree. + */ + do { + nodes[i] = tree_insert(&root, &values[i], &t_compare); + cl_assert(nodes[i] != NULL); + i = (i * 7) % 11; + } while (i != 1); + + for (i = 1; i < ARRAY_SIZE(nodes); i++) { + cl_assert_equal_p(&values[i], nodes[i]->key); + cl_assert_equal_p(nodes[i], tree_search(root, &values[i], &t_compare)); + } + + cl_assert(tree_search(root, values, t_compare) == NULL); + tree_free(root); +} + +void test_reftable_tree__infix_walk(void) +{ + struct tree_node *root = NULL; + void *values[11] = { 0 }; + void *out[11] = { 0 }; + struct curry c = { + .arr = (void **) &out, + }; + size_t i = 1; + size_t count = 0; + + do { + struct tree_node *node = tree_insert(&root, &values[i], t_compare); + cl_assert(node != NULL); + i = (i * 7) % 11; + count++; + } while (i != 1); + + infix_walk(root, &store, &c); + for (i = 1; i < ARRAY_SIZE(values); i++) + cl_assert_equal_p(&values[i], out[i - 1]); + cl_assert(out[i - 1] == NULL); + cl_assert_equal_i(c.len, count); + tree_free(root); +} -- 2.34.1 ^ permalink raw reply related [flat|nested] 27+ messages in thread
* Re: [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar 2025-01-16 16:15 ` [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji ` (3 preceding siblings ...) 2025-01-16 16:15 ` [PATCH v2 4/4] t/unit-tests: convert reftable tree " Seyi Kuforiji @ 2025-01-17 6:27 ` Patrick Steinhardt 2025-01-17 12:29 ` Seyi Kuforiji 5 siblings, 0 replies; 27+ messages in thread From: Patrick Steinhardt @ 2025-01-17 6:27 UTC (permalink / raw) To: Seyi Kuforiji; +Cc: git, phillip.wood On Thu, Jan 16, 2025 at 05:15:55PM +0100, Seyi Kuforiji wrote: > Range-diff against v1: > 1: 00bb4d2880 = 1: 00bb4d2880 t/unit-tests: handle dashes in test suite filenames > 2: 59abf45f08 = 2: 59abf45f08 t/unit-tests: convert mem-pool test to use clar test framework > 3: 6e01da141d = 3: 6e01da141d t/unit-tests: adapt priority queue test to use clar test framework > 4: b397a5f131 = 4: b397a5f131 t/unit-tests: convert reftable tree test to use clar test framework I think you passed wrong commit ranges for the range-diff :) Anyway, I scanned through the changes and saw that all my feedback was addressed. I've got one more proposal for the first commit message, but other than that the series looks good to me. Thanks! Patrick ^ permalink raw reply [flat|nested] 27+ messages in thread
* t/unit-tests: convert unit-tests to use clar 2025-01-16 16:15 ` [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji ` (4 preceding siblings ...) 2025-01-17 6:27 ` [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar Patrick Steinhardt @ 2025-01-17 12:29 ` Seyi Kuforiji 2025-01-17 12:29 ` [PATCH v3 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji ` (4 more replies) 5 siblings, 5 replies; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-17 12:29 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, gitster, Seyi Kuforiji Hello, This small patch series transitions the existing unit test files to the Clar testing framework. This change is part of our ongoing effort to standardize our testing approach and enhance maintainability. changes in v3: - Some small fixes were made to the commit messages Thanks Seyi Mentored-by: Patrick Steinhardt ps@pks.im Signed-off-by: Seyi Kuforiji kuforiji98@gmail.com Seyi Kuforiji (4): t/unit-tests: handle dashes in test suite filenames t/unit-tests: convert mem-pool test to use clar test framework t/unit-tests: adapt priority queue test to use clar test framework t/unit-tests: convert reftable tree test to use clar test framework Makefile | 6 +- t/meson.build | 6 +- t/unit-tests/generate-clar-decls.sh | 1 + t/unit-tests/t-mem-pool.c | 31 ---------- t/unit-tests/t-prio-queue.c | 91 ---------------------------- t/unit-tests/t-reftable-tree.c | 86 -------------------------- t/unit-tests/u-mem-pool.c | 25 ++++++++ t/unit-tests/u-prio-queue.c | 94 +++++++++++++++++++++++++++++ t/unit-tests/u-reftable-tree.c | 78 ++++++++++++++++++++++++ 9 files changed, 204 insertions(+), 214 deletions(-) delete mode 100644 t/unit-tests/t-mem-pool.c delete mode 100644 t/unit-tests/t-prio-queue.c delete mode 100644 t/unit-tests/t-reftable-tree.c create mode 100644 t/unit-tests/u-mem-pool.c create mode 100644 t/unit-tests/u-prio-queue.c create mode 100644 t/unit-tests/u-reftable-tree.c Range-diff against v2: 1: 00bb4d2880 ! 1: f9e163a181 t/unit-tests: handle dashes in test suite filenames @@ Metadata ## Commit message ## t/unit-tests: handle dashes in test suite filenames - The script is designed to extract function signatures that match a - specific pattern derived from the unit test file's name. - `generate-clar-decls.sh` does not pick up dashes in filenames, which - prevents the scripts from being run. + "generate-clar-decls.sh" script is designed to extract function + signatures that match a specific pattern derived from the unit test + file's name. The script does not know to massage file names with dashes, + which will make it search for functions that look like, for example, + `test_mem-pool_*`. Having dashes in function names is not allowed + though, so these patterns won't ever match a legal function name. Adapt script to translate dashes (`-`) in test suite filenames to underscores (`_`) to correctly extract the function signatures and run -: ---------- > 2: f46438f53e t/unit-tests: convert mem-pool test to use clar test framework -: ---------- > 3: f5cabebd3a t/unit-tests: adapt priority queue test to use clar test framework -: ---------- > 4: 5323873612 t/unit-tests: convert reftable tree test to use clar test framework -- 2.34.1 ^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v3 1/4] t/unit-tests: handle dashes in test suite filenames 2025-01-17 12:29 ` Seyi Kuforiji @ 2025-01-17 12:29 ` Seyi Kuforiji 2025-01-17 12:29 ` [PATCH v3 2/4] t/unit-tests: convert mem-pool test to use clar test framework Seyi Kuforiji ` (3 subsequent siblings) 4 siblings, 0 replies; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-17 12:29 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, gitster, Seyi Kuforiji "generate-clar-decls.sh" script is designed to extract function signatures that match a specific pattern derived from the unit test file's name. The script does not know to massage file names with dashes, which will make it search for functions that look like, for example, `test_mem-pool_*`. Having dashes in function names is not allowed though, so these patterns won't ever match a legal function name. Adapt script to translate dashes (`-`) in test suite filenames to underscores (`_`) to correctly extract the function signatures and run the corresponding tests. This will be used by subsequent commits which follows the same construct. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> --- t/unit-tests/generate-clar-decls.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/t/unit-tests/generate-clar-decls.sh b/t/unit-tests/generate-clar-decls.sh index 3b315c64b3..abf6a2ea2a 100755 --- a/t/unit-tests/generate-clar-decls.sh +++ b/t/unit-tests/generate-clar-decls.sh @@ -14,6 +14,7 @@ do suite_name=$(basename "$suite") suite_name=${suite_name%.c} suite_name=${suite_name#u-} + suite_name=$(echo "$suite_name" | tr '-' '_') sed -ne "s/^\(void test_${suite_name}__[a-zA-Z_0-9][a-zA-Z_0-9]*(void)\)$/extern \1;/p" "$suite" || exit 1 done >"$OUTPUT" -- 2.34.1 ^ permalink raw reply related [flat|nested] 27+ messages in thread
* [PATCH v3 2/4] t/unit-tests: convert mem-pool test to use clar test framework 2025-01-17 12:29 ` Seyi Kuforiji 2025-01-17 12:29 ` [PATCH v3 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji @ 2025-01-17 12:29 ` Seyi Kuforiji 2025-01-20 10:18 ` Karthik Nayak 2025-01-17 12:29 ` [PATCH v3 3/4] t/unit-tests: adapt priority queue " Seyi Kuforiji ` (2 subsequent siblings) 4 siblings, 1 reply; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-17 12:29 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, gitster, Seyi Kuforiji Adapt the mem-pool test script to use clar framework by using clar assertions where necessary.Test functions are created as a standalone to test different test cases. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/t-mem-pool.c | 31 ------------------------------- t/unit-tests/u-mem-pool.c | 25 +++++++++++++++++++++++++ 4 files changed, 27 insertions(+), 33 deletions(-) delete mode 100644 t/unit-tests/t-mem-pool.c create mode 100644 t/unit-tests/u-mem-pool.c diff --git a/Makefile b/Makefile index 97e8385b66..49ada4169d 100644 --- a/Makefile +++ b/Makefile @@ -1338,6 +1338,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/% THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% CLAR_TEST_SUITES += u-ctype +CLAR_TEST_SUITES += u-mem-pool CLAR_TEST_SUITES += u-strvec CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) @@ -1347,7 +1348,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o UNIT_TEST_PROGRAMS += t-example-decorate UNIT_TEST_PROGRAMS += t-hash UNIT_TEST_PROGRAMS += t-hashmap -UNIT_TEST_PROGRAMS += t-mem-pool UNIT_TEST_PROGRAMS += t-oid-array UNIT_TEST_PROGRAMS += t-oidmap UNIT_TEST_PROGRAMS += t-oidtree diff --git a/t/meson.build b/t/meson.build index 602ebfe6a2..ffe951f9be 100644 --- a/t/meson.build +++ b/t/meson.build @@ -1,5 +1,6 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', + 'unit-tests/u-mem-pool.c', 'unit-tests/u-strvec.c', ] @@ -43,7 +44,6 @@ unit_test_programs = [ 'unit-tests/t-example-decorate.c', 'unit-tests/t-hash.c', 'unit-tests/t-hashmap.c', - 'unit-tests/t-mem-pool.c', 'unit-tests/t-oid-array.c', 'unit-tests/t-oidmap.c', 'unit-tests/t-oidtree.c', diff --git a/t/unit-tests/t-mem-pool.c b/t/unit-tests/t-mem-pool.c deleted file mode 100644 index fe500c704b..0000000000 --- a/t/unit-tests/t-mem-pool.c +++ /dev/null @@ -1,31 +0,0 @@ -#include "test-lib.h" -#include "mem-pool.h" - -static void setup_static(void (*f)(struct mem_pool *), size_t block_alloc) -{ - struct mem_pool pool = { .block_alloc = block_alloc }; - f(&pool); - mem_pool_discard(&pool, 0); -} - -static void t_calloc_100(struct mem_pool *pool) -{ - size_t size = 100; - char *buffer = mem_pool_calloc(pool, 1, size); - for (size_t i = 0; i < size; i++) - check_int(buffer[i], ==, 0); - if (!check(pool->mp_block != NULL)) - return; - check(pool->mp_block->next_free != NULL); - check(pool->mp_block->end != NULL); -} - -int cmd_main(int argc UNUSED, const char **argv UNUSED) -{ - TEST(setup_static(t_calloc_100, 1024 * 1024), - "mem_pool_calloc returns 100 zeroed bytes with big block"); - TEST(setup_static(t_calloc_100, 1), - "mem_pool_calloc returns 100 zeroed bytes with tiny block"); - - return test_done(); -} diff --git a/t/unit-tests/u-mem-pool.c b/t/unit-tests/u-mem-pool.c new file mode 100644 index 0000000000..2bc2493b7e --- /dev/null +++ b/t/unit-tests/u-mem-pool.c @@ -0,0 +1,25 @@ +#include "unit-test.h" +#include "mem-pool.h" + +static void test_many_pool_allocations(size_t block_alloc) +{ + struct mem_pool pool = { .block_alloc = block_alloc }; + size_t size = 100; + char *buffer = mem_pool_calloc(&pool, 1, size); + for (size_t i = 0; i < size; i++) + cl_assert_equal_i(0, buffer[i]); + cl_assert(pool.mp_block != NULL); + cl_assert(pool.mp_block->next_free != NULL); + cl_assert(pool.mp_block->end != NULL); + mem_pool_discard(&pool, 0); +} + +void test_mem_pool__big_block(void) +{ + test_many_pool_allocations(1024 * 1024); +} + +void test_mem_pool__tiny_block(void) +{ + test_many_pool_allocations(1); +} -- 2.34.1 ^ permalink raw reply related [flat|nested] 27+ messages in thread
* Re: [PATCH v3 2/4] t/unit-tests: convert mem-pool test to use clar test framework 2025-01-17 12:29 ` [PATCH v3 2/4] t/unit-tests: convert mem-pool test to use clar test framework Seyi Kuforiji @ 2025-01-20 10:18 ` Karthik Nayak 0 siblings, 0 replies; 27+ messages in thread From: Karthik Nayak @ 2025-01-20 10:18 UTC (permalink / raw) To: Seyi Kuforiji, git; +Cc: ps, phillip.wood, gitster [-- Attachment #1: Type: text/plain, Size: 291 bytes --] Seyi Kuforiji <kuforiji98@gmail.com> writes: > Adapt the mem-pool test script to use clar framework by using clar > assertions where necessary.Test functions are created as a standalone to Nit: missing space post period. Rest of tha patch looks good! > test different test cases. [snip] [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 690 bytes --] ^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v3 3/4] t/unit-tests: adapt priority queue test to use clar test framework 2025-01-17 12:29 ` Seyi Kuforiji 2025-01-17 12:29 ` [PATCH v3 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji 2025-01-17 12:29 ` [PATCH v3 2/4] t/unit-tests: convert mem-pool test to use clar test framework Seyi Kuforiji @ 2025-01-17 12:29 ` Seyi Kuforiji 2025-01-20 10:22 ` Karthik Nayak 2025-01-17 12:29 ` [PATCH v3 4/4] t/unit-tests: convert reftable tree " Seyi Kuforiji 2025-01-17 13:36 ` t/unit-tests: convert unit-tests to use clar Patrick Steinhardt 4 siblings, 1 reply; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-17 12:29 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, gitster, Seyi Kuforiji Convert the prio-queue test script to clar framework by using clar assertions where necessary. Test functions are created as a standalone to test different cases. update the type of the variable `j` from int to `size_t`, this ensures compatibility with the type used for result_size, which is also size_t, preventing a potential warning or error caused by comparisons between signed and unsigned integers. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/t-prio-queue.c | 91 ----------------------------------- t/unit-tests/u-prio-queue.c | 94 +++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 93 deletions(-) delete mode 100644 t/unit-tests/t-prio-queue.c create mode 100644 t/unit-tests/u-prio-queue.c diff --git a/Makefile b/Makefile index 49ada4169d..049f857512 100644 --- a/Makefile +++ b/Makefile @@ -1339,6 +1339,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% CLAR_TEST_SUITES += u-ctype CLAR_TEST_SUITES += u-mem-pool +CLAR_TEST_SUITES += u-prio-queue CLAR_TEST_SUITES += u-strvec CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) @@ -1351,7 +1352,6 @@ UNIT_TEST_PROGRAMS += t-hashmap UNIT_TEST_PROGRAMS += t-oid-array UNIT_TEST_PROGRAMS += t-oidmap UNIT_TEST_PROGRAMS += t-oidtree -UNIT_TEST_PROGRAMS += t-prio-queue UNIT_TEST_PROGRAMS += t-reftable-basics UNIT_TEST_PROGRAMS += t-reftable-block UNIT_TEST_PROGRAMS += t-reftable-merged diff --git a/t/meson.build b/t/meson.build index ffe951f9be..09232967cd 100644 --- a/t/meson.build +++ b/t/meson.build @@ -1,6 +1,7 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', 'unit-tests/u-mem-pool.c', + 'unit-tests/u-prio-queue.c', 'unit-tests/u-strvec.c', ] @@ -47,7 +48,6 @@ unit_test_programs = [ 'unit-tests/t-oid-array.c', 'unit-tests/t-oidmap.c', 'unit-tests/t-oidtree.c', - 'unit-tests/t-prio-queue.c', 'unit-tests/t-reftable-basics.c', 'unit-tests/t-reftable-block.c', 'unit-tests/t-reftable-merged.c', diff --git a/t/unit-tests/t-prio-queue.c b/t/unit-tests/t-prio-queue.c deleted file mode 100644 index a053635000..0000000000 --- a/t/unit-tests/t-prio-queue.c +++ /dev/null @@ -1,91 +0,0 @@ -#include "test-lib.h" -#include "prio-queue.h" - -static int intcmp(const void *va, const void *vb, void *data UNUSED) -{ - const int *a = va, *b = vb; - return *a - *b; -} - - -#define MISSING -1 -#define DUMP -2 -#define STACK -3 -#define GET -4 -#define REVERSE -5 - -static int show(int *v) -{ - return v ? *v : MISSING; -} - -static void test_prio_queue(int *input, size_t input_size, - int *result, size_t result_size) -{ - struct prio_queue pq = { intcmp }; - int j = 0; - - for (size_t i = 0; i < input_size; i++) { - void *peek, *get; - switch(input[i]) { - case GET: - peek = prio_queue_peek(&pq); - get = prio_queue_get(&pq); - if (!check(peek == get)) - return; - if (!check_uint(j, <, result_size)) - break; - if (!check_int(result[j], ==, show(get))) - test_msg(" j: %d", j); - j++; - break; - case DUMP: - while ((peek = prio_queue_peek(&pq))) { - get = prio_queue_get(&pq); - if (!check(peek == get)) - return; - if (!check_uint(j, <, result_size)) - break; - if (!check_int(result[j], ==, show(get))) - test_msg(" j: %d", j); - j++; - } - break; - case STACK: - pq.compare = NULL; - break; - case REVERSE: - prio_queue_reverse(&pq); - break; - default: - prio_queue_put(&pq, &input[i]); - break; - } - } - check_uint(j, ==, result_size); - clear_prio_queue(&pq); -} - -#define TEST_INPUT(input, result) \ - test_prio_queue(input, ARRAY_SIZE(input), result, ARRAY_SIZE(result)) - -int cmd_main(int argc UNUSED, const char **argv UNUSED) -{ - TEST(TEST_INPUT(((int []){ 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP }), - ((int []){ 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 })), - "prio-queue works for basic input"); - TEST(TEST_INPUT(((int []){ 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP }), - ((int []){ 2, 3, 4, 1, 5, 6 })), - "prio-queue works for mixed put & get commands"); - TEST(TEST_INPUT(((int []){ 1, 2, GET, GET, GET, 1, 2, GET, GET, GET }), - ((int []){ 1, 2, MISSING, 1, 2, MISSING })), - "prio-queue works when queue is empty"); - TEST(TEST_INPUT(((int []){ STACK, 8, 1, 5, 4, 6, 2, 3, DUMP }), - ((int []){ 3, 2, 6, 4, 5, 1, 8 })), - "prio-queue works when used as a LIFO stack"); - TEST(TEST_INPUT(((int []){ STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP }), - ((int []){ 1, 2, 3, 4, 5, 6 })), - "prio-queue works when LIFO stack is reversed"); - - return test_done(); -} diff --git a/t/unit-tests/u-prio-queue.c b/t/unit-tests/u-prio-queue.c new file mode 100644 index 0000000000..145e689c9c --- /dev/null +++ b/t/unit-tests/u-prio-queue.c @@ -0,0 +1,94 @@ +#include "unit-test.h" +#include "prio-queue.h" + +static int intcmp(const void *va, const void *vb, void *data UNUSED) +{ + const int *a = va, *b = vb; + return *a - *b; +} + + +#define MISSING -1 +#define DUMP -2 +#define STACK -3 +#define GET -4 +#define REVERSE -5 + +static int show(int *v) +{ + return v ? *v : MISSING; +} + +static void test_prio_queue(int *input, size_t input_size, + int *result, size_t result_size) +{ + struct prio_queue pq = { intcmp }; + size_t j = 0; + + for (size_t i = 0; i < input_size; i++) { + void *peek, *get; + switch(input[i]) { + case GET: + peek = prio_queue_peek(&pq); + get = prio_queue_get(&pq); + cl_assert(peek == get); + cl_assert(j < result_size); + cl_assert_equal_i(result[j], show(get)); + j++; + break; + case DUMP: + while ((peek = prio_queue_peek(&pq))) { + get = prio_queue_get(&pq); + cl_assert(peek == get); + cl_assert(j < result_size); + cl_assert_equal_i(result[j], show(get)); + j++; + } + break; + case STACK: + pq.compare = NULL; + break; + case REVERSE: + prio_queue_reverse(&pq); + break; + default: + prio_queue_put(&pq, &input[i]); + break; + } + } + cl_assert_equal_i(j, result_size); + clear_prio_queue(&pq); +} + +#define TEST_INPUT(input, result) \ + test_prio_queue(input, ARRAY_SIZE(input), result, ARRAY_SIZE(result)) + +void test_prio_queue__basic(void) +{ + TEST_INPUT(((int []){ 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP }), + ((int []){ 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 })); +} + +void test_prio_queue__mixed(void) +{ + TEST_INPUT(((int []){ 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP }), + ((int []){ 2, 3, 4, 1, 5, 6 })); +} + +void test_prio_queue__empty(void) +{ + TEST_INPUT(((int []){ 1, 2, GET, GET, GET, 1, 2, GET, GET, GET }), + ((int []){ 1, 2, MISSING, 1, 2, MISSING })); +} + +void test_prio_queue__stack(void) +{ + TEST_INPUT(((int []){ STACK, 8, 1, 5, 4, 6, 2, 3, DUMP }), + ((int []){ 3, 2, 6, 4, 5, 1, 8 })); +} + +void test_prio_queue__reverse_stack(void) +{ + TEST_INPUT(((int []){ STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP }), + ((int []){ 1, 2, 3, 4, 5, 6 })); +} -- 2.34.1 ^ permalink raw reply related [flat|nested] 27+ messages in thread
* Re: [PATCH v3 3/4] t/unit-tests: adapt priority queue test to use clar test framework 2025-01-17 12:29 ` [PATCH v3 3/4] t/unit-tests: adapt priority queue " Seyi Kuforiji @ 2025-01-20 10:22 ` Karthik Nayak 0 siblings, 0 replies; 27+ messages in thread From: Karthik Nayak @ 2025-01-20 10:22 UTC (permalink / raw) To: Seyi Kuforiji, git; +Cc: ps, phillip.wood, gitster [-- Attachment #1: Type: text/plain, Size: 501 bytes --] Seyi Kuforiji <kuforiji98@gmail.com> writes: > Convert the prio-queue test script to clar framework by using clar > assertions where necessary. Test functions are created as a standalone > to test different cases. > > update the type of the variable `j` from int to `size_t`, this ensures Nit: s/update/Update > compatibility with the type used for result_size, which is also size_t, > preventing a potential warning or error caused by comparisons between > signed and unsigned integers. > [snip] [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 690 bytes --] ^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v3 4/4] t/unit-tests: convert reftable tree test to use clar test framework 2025-01-17 12:29 ` Seyi Kuforiji ` (2 preceding siblings ...) 2025-01-17 12:29 ` [PATCH v3 3/4] t/unit-tests: adapt priority queue " Seyi Kuforiji @ 2025-01-17 12:29 ` Seyi Kuforiji 2025-01-17 13:36 ` t/unit-tests: convert unit-tests to use clar Patrick Steinhardt 4 siblings, 0 replies; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-17 12:29 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, gitster, Seyi Kuforiji Adapts reftable tree test script to clar framework by using clar assertions where necessary. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/t-reftable-tree.c | 86 ---------------------------------- t/unit-tests/u-reftable-tree.c | 78 ++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 88 deletions(-) delete mode 100644 t/unit-tests/t-reftable-tree.c create mode 100644 t/unit-tests/u-reftable-tree.c diff --git a/Makefile b/Makefile index 049f857512..75dbb8e25f 100644 --- a/Makefile +++ b/Makefile @@ -1340,6 +1340,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% CLAR_TEST_SUITES += u-ctype CLAR_TEST_SUITES += u-mem-pool CLAR_TEST_SUITES += u-prio-queue +CLAR_TEST_SUITES += u-reftable-tree CLAR_TEST_SUITES += u-strvec CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) @@ -1360,7 +1361,6 @@ UNIT_TEST_PROGRAMS += t-reftable-reader 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 UNIT_TEST_PROGRAMS += t-trailer diff --git a/t/meson.build b/t/meson.build index 09232967cd..6dd41216ef 100644 --- a/t/meson.build +++ b/t/meson.build @@ -2,6 +2,7 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', 'unit-tests/u-mem-pool.c', 'unit-tests/u-prio-queue.c', + 'unit-tests/u-reftable-tree.c', 'unit-tests/u-strvec.c', ] @@ -56,7 +57,6 @@ unit_test_programs = [ 'unit-tests/t-reftable-readwrite.c', 'unit-tests/t-reftable-record.c', 'unit-tests/t-reftable-stack.c', - 'unit-tests/t-reftable-tree.c', 'unit-tests/t-strbuf.c', 'unit-tests/t-strcmp-offset.c', 'unit-tests/t-trailer.c', diff --git a/t/unit-tests/t-reftable-tree.c b/t/unit-tests/t-reftable-tree.c deleted file mode 100644 index 79b175a45a..0000000000 --- a/t/unit-tests/t-reftable-tree.c +++ /dev/null @@ -1,86 +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 "test-lib.h" -#include "reftable/tree.h" - -static int t_compare(const void *a, const void *b) -{ - return (char *)a - (char *)b; -} - -struct curry { - void **arr; - size_t len; -}; - -static void store(void *arg, void *key) -{ - struct curry *c = arg; - c->arr[c->len++] = key; -} - -static void t_tree_search(void) -{ - struct tree_node *root = NULL; - void *values[11] = { 0 }; - struct tree_node *nodes[11] = { 0 }; - size_t i = 1; - - /* - * Pseudo-randomly insert the pointers for elements between - * values[1] and values[10] (inclusive) in the tree. - */ - do { - nodes[i] = tree_insert(&root, &values[i], &t_compare); - check(nodes[i] != NULL); - i = (i * 7) % 11; - } while (i != 1); - - for (i = 1; i < ARRAY_SIZE(nodes); i++) { - check_pointer_eq(&values[i], nodes[i]->key); - check_pointer_eq(nodes[i], tree_search(root, &values[i], &t_compare)); - } - - check(!tree_search(root, values, t_compare)); - tree_free(root); -} - -static void t_infix_walk(void) -{ - struct tree_node *root = NULL; - void *values[11] = { 0 }; - void *out[11] = { 0 }; - struct curry c = { - .arr = (void **) &out, - }; - size_t i = 1; - size_t count = 0; - - do { - struct tree_node *node = tree_insert(&root, &values[i], t_compare); - check(node != NULL); - i = (i * 7) % 11; - count++; - } while (i != 1); - - infix_walk(root, &store, &c); - for (i = 1; i < ARRAY_SIZE(values); i++) - check_pointer_eq(&values[i], out[i - 1]); - check(!out[i - 1]); - check_int(c.len, ==, count); - tree_free(root); -} - -int cmd_main(int argc UNUSED, const char *argv[] UNUSED) -{ - TEST(t_tree_search(), "tree_search works"); - TEST(t_infix_walk(), "infix_walk works"); - - return test_done(); -} diff --git a/t/unit-tests/u-reftable-tree.c b/t/unit-tests/u-reftable-tree.c new file mode 100644 index 0000000000..bcf9061071 --- /dev/null +++ b/t/unit-tests/u-reftable-tree.c @@ -0,0 +1,78 @@ +/* +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 "unit-test.h" +#include "reftable/tree.h" + +static int t_compare(const void *a, const void *b) +{ + return (char *)a - (char *)b; +} + +struct curry { + void **arr; + size_t len; +}; + +static void store(void *arg, void *key) +{ + struct curry *c = arg; + c->arr[c->len++] = key; +} + +void test_reftable_tree__tree_search(void) +{ + struct tree_node *root = NULL; + void *values[11] = { 0 }; + struct tree_node *nodes[11] = { 0 }; + size_t i = 1; + + /* + * Pseudo-randomly insert the pointers for elements between + * values[1] and values[10] (inclusive) in the tree. + */ + do { + nodes[i] = tree_insert(&root, &values[i], &t_compare); + cl_assert(nodes[i] != NULL); + i = (i * 7) % 11; + } while (i != 1); + + for (i = 1; i < ARRAY_SIZE(nodes); i++) { + cl_assert_equal_p(&values[i], nodes[i]->key); + cl_assert_equal_p(nodes[i], tree_search(root, &values[i], &t_compare)); + } + + cl_assert(tree_search(root, values, t_compare) == NULL); + tree_free(root); +} + +void test_reftable_tree__infix_walk(void) +{ + struct tree_node *root = NULL; + void *values[11] = { 0 }; + void *out[11] = { 0 }; + struct curry c = { + .arr = (void **) &out, + }; + size_t i = 1; + size_t count = 0; + + do { + struct tree_node *node = tree_insert(&root, &values[i], t_compare); + cl_assert(node != NULL); + i = (i * 7) % 11; + count++; + } while (i != 1); + + infix_walk(root, &store, &c); + for (i = 1; i < ARRAY_SIZE(values); i++) + cl_assert_equal_p(&values[i], out[i - 1]); + cl_assert(out[i - 1] == NULL); + cl_assert_equal_i(c.len, count); + tree_free(root); +} -- 2.34.1 ^ permalink raw reply related [flat|nested] 27+ messages in thread
* Re: t/unit-tests: convert unit-tests to use clar 2025-01-17 12:29 ` Seyi Kuforiji ` (3 preceding siblings ...) 2025-01-17 12:29 ` [PATCH v3 4/4] t/unit-tests: convert reftable tree " Seyi Kuforiji @ 2025-01-17 13:36 ` Patrick Steinhardt 4 siblings, 0 replies; 27+ messages in thread From: Patrick Steinhardt @ 2025-01-17 13:36 UTC (permalink / raw) To: Seyi Kuforiji; +Cc: git, phillip.wood, gitster On Fri, Jan 17, 2025 at 01:29:22PM +0100, Seyi Kuforiji wrote: > Hello, > > This small patch series transitions the existing unit test files to the > Clar testing framework. This change is part of our ongoing effort to > standardize our testing approach and enhance maintainability. > > changes in v3: > - Some small fixes were made to the commit messages Thanks, the series looks good to me now. > Range-diff against v2: > 1: 00bb4d2880 ! 1: f9e163a181 t/unit-tests: handle dashes in test suite filenames > @@ Metadata > ## Commit message ## > t/unit-tests: handle dashes in test suite filenames > > - The script is designed to extract function signatures that match a > - specific pattern derived from the unit test file's name. > - `generate-clar-decls.sh` does not pick up dashes in filenames, which > - prevents the scripts from being run. > + "generate-clar-decls.sh" script is designed to extract function > + signatures that match a specific pattern derived from the unit test > + file's name. The script does not know to massage file names with dashes, > + which will make it search for functions that look like, for example, > + `test_mem-pool_*`. Having dashes in function names is not allowed > + though, so these patterns won't ever match a legal function name. > > Adapt script to translate dashes (`-`) in test suite filenames to > underscores (`_`) to correctly extract the function signatures and run > -: ---------- > 2: f46438f53e t/unit-tests: convert mem-pool test to use clar test framework > -: ---------- > 3: f5cabebd3a t/unit-tests: adapt priority queue test to use clar test framework > -: ---------- > 4: 5323873612 t/unit-tests: convert reftable tree test to use clar test framework The range-diff is still not correct :P Now it seems like you only passed the first commit of your v2 to it, instead of the tip of the v2 branch. Patrick ^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH 0/4] t/unit-tests: convert unit-tests to use clar @ 2025-01-30 9:13 Seyi Kuforiji 2025-01-31 22:14 ` [PATCH v2 " Seyi Kuforiji 0 siblings, 1 reply; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-30 9:13 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, Seyi Kuforiji Hello, This small patch series transitions the existing unit test files to the Clar testing framework. This change is part of our ongoing effort to standardize our testing framework to enhance maintainability. Thanks Seyi Mentored-by: Patrick Steinhardt ps@pks.im Signed-off-by: Seyi Kuforiji kuforiji98@gmail.com Seyi Kuforiji (4): t/unit-tests: convert hashmap test to use clar test framework t/unit-tests: adapt example decorate test to use clar test framework t/unit-tests: convert strbuf test to use clar test framework t/unit-tests: convert strcmp-offset test to use clar test framework Makefile | 8 +- t/meson.build | 8 +- t/unit-tests/t-example-decorate.c | 74 ------- t/unit-tests/t-strbuf.c | 122 ------------ t/unit-tests/t-strcmp-offset.c | 35 ---- t/unit-tests/u-example-decorate.c | 76 ++++++++ t/unit-tests/{t-hashmap.c => u-hashmap.c} | 226 +++++++++++----------- t/unit-tests/u-strbuf.c | 121 ++++++++++++ t/unit-tests/u-strcmp-offset.c | 45 +++++ 9 files changed, 362 insertions(+), 353 deletions(-) delete mode 100644 t/unit-tests/t-example-decorate.c delete mode 100644 t/unit-tests/t-strbuf.c delete mode 100644 t/unit-tests/t-strcmp-offset.c create mode 100644 t/unit-tests/u-example-decorate.c rename t/unit-tests/{t-hashmap.c => u-hashmap.c} (60%) create mode 100644 t/unit-tests/u-strbuf.c create mode 100644 t/unit-tests/u-strcmp-offset.c -- 2.47.0.86.g15030f9556 ^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar 2025-01-30 9:13 [PATCH 0/4] " Seyi Kuforiji @ 2025-01-31 22:14 ` Seyi Kuforiji 2025-01-31 23:06 ` Junio C Hamano 0 siblings, 1 reply; 27+ messages in thread From: Seyi Kuforiji @ 2025-01-31 22:14 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, Seyi Kuforiji Hello, This small patch series transitions the existing unit test files to the Clar testing framework. This change is part of our ongoing effort to standardize our testing approach and enhance maintainability. Changes in v2: - small fixes to the commit messages and how they read - some small code fix up and refactoring Thanks Seyi Mentored-by: Patrick Steinhardt ps@pks.im Signed-off-by: Seyi Kuforiji kuforiji98@gmail.com Seyi Kuforiji (4): t/unit-tests: convert hashmap test to use clar test framework t/unit-tests: adapt example decorate test to use clar test framework t/unit-tests: convert strbuf test to use clar test framework t/unit-tests: convert strcmp-offset test to use clar test framework Makefile | 8 +- t/meson.build | 8 +- ...xample-decorate.c => u-example-decorate.c} | 76 +++--- t/unit-tests/{t-hashmap.c => u-hashmap.c} | 226 +++++++++--------- t/unit-tests/{t-strbuf.c => u-strbuf.c} | 115 +++++---- .../{t-strcmp-offset.c => u-strcmp-offset.c} | 36 ++- 6 files changed, 232 insertions(+), 237 deletions(-) rename t/unit-tests/{t-example-decorate.c => u-example-decorate.c} (30%) rename t/unit-tests/{t-hashmap.c => u-hashmap.c} (60%) rename t/unit-tests/{t-strbuf.c => u-strbuf.c} (35%) rename t/unit-tests/{t-strcmp-offset.c => u-strcmp-offset.c} (39%) Range-diff against v1: 1: 90accb2f75 ! 1: 19697be26b t/unit-tests: convert hashmap test to use clar test framework @@ Commit message t/unit-tests: convert hashmap test to use clar test framework Adapts hashmap test script to clar framework by using clar assertions - where necessary. Test functions are created as both standalone and - inline to test different test cases. + where necessary. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> @@ t/unit-tests/u-hashmap.c: static void t_add(struct hashmap *map, unsigned int ig - } + int ret = key_val_contains(key_val, seen, + ARRAY_SIZE(key_val), entry); -+ cl_assert(ret == 0); ++ cl_assert_equal_i(ret, 0); + count++; } - check_int(count, ==, 2); @@ t/unit-tests/u-hashmap.c: static void t_iterate(struct hashmap *map, unsigned in - } - } + int ret = key_val_contains(key_val, seen, -+ ARRAY_SIZE(key_val), -+ entry); ++ ARRAY_SIZE(key_val), ++ entry); + cl_assert(ret == 0); } 2: 13a407d504 ! 2: 1d8f8974a5 t/unit-tests: adapt example decorate test to use clar test framework @@ Metadata ## Commit message ## t/unit-tests: adapt example decorate test to use clar test framework - Adapts example decorate test script to clar framework by using clar - assertions where necessary. Test functions are created as standalone to - test different test cases. + Introduce `test_example_decorate__initialize()` to explicitly set up + object IDs and retrieve corresponding objects before tests run. This + ensures a consistent and predictable test state without relying on data + from previous tests. + + Add `test_example_decorate__cleanup()` to clear decorations after each + test, preventing interference between tests and ensuring each runs in + isolation. + + Adapt example decorate test script to clar framework by using clar + assertions where necessary. Previously, tests relied on data written by + earlier tests, leading to unintended dependencies between them. This + explicitly initializes the necessary state within + `test_example_decorate__readd`, ensuring it does not depend on prior + test executions. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> @@ t/unit-tests/u-example-decorate.c (new) + +static struct test_vars vars; + -+void test_example_decorate__add(void) ++void test_example_decorate__initialize(void) +{ -+ void *ret = add_decoration(&vars.n, vars.one, &vars.decoration_a); -+ cl_assert(ret == NULL); -+ ret = add_decoration(&vars.n, vars.two, NULL); -+ cl_assert(ret == NULL); ++ struct object_id one_oid = { { 1 } }, two_oid = { { 2 } }, three_oid = { { 3 } }; ++ ++ vars.one = lookup_unknown_object(the_repository, &one_oid); ++ vars.two = lookup_unknown_object(the_repository, &two_oid); ++ vars.three = lookup_unknown_object(the_repository, &three_oid); +} + -+void test_example_decorate__readd(void) ++void test_example_decorate__cleanup(void) +{ -+ void *ret; ++ clear_decoration(&vars.n, NULL); ++} + -+ cl_assert(add_decoration(&vars.n, vars.one, &vars.decoration_a) == NULL); -+ cl_assert(add_decoration(&vars.n, vars.two, NULL) == NULL); ++void test_example_decorate__add(void) ++{ ++ cl_assert_equal_p(add_decoration(&vars.n, vars.one, &vars.decoration_a), NULL); ++ cl_assert_equal_p(add_decoration(&vars.n, vars.two, NULL), NULL); ++} + -+ ret = add_decoration(&vars.n, vars.one, NULL); -+ cl_assert(ret == &vars.decoration_a); -+ ret = add_decoration(&vars.n, vars.two, &vars.decoration_b); -+ cl_assert(ret == NULL); ++void test_example_decorate__readd(void) ++{ ++ cl_assert_equal_p(add_decoration(&vars.n, vars.one, &vars.decoration_a), NULL); ++ cl_assert_equal_p(add_decoration(&vars.n, vars.two, NULL), NULL); ++ cl_assert_equal_p(add_decoration(&vars.n, vars.one, NULL), &vars.decoration_a); ++ cl_assert_equal_p(add_decoration(&vars.n, vars.two, &vars.decoration_b), NULL); +} + +void test_example_decorate__lookup(void) +{ -+ void *ret; -+ -+ add_decoration(&vars.n, vars.two, &vars.decoration_b); -+ add_decoration(&vars.n, vars.one, NULL); -+ -+ ret = lookup_decoration(&vars.n, vars.two); -+ cl_assert(ret == &vars.decoration_b); -+ ret = lookup_decoration(&vars.n, vars.one); -+ cl_assert(ret == NULL); ++ cl_assert_equal_p(add_decoration(&vars.n, vars.two, &vars.decoration_b), NULL); ++ cl_assert_equal_p(add_decoration(&vars.n, vars.one, NULL), NULL); ++ cl_assert_equal_p(lookup_decoration(&vars.n, vars.two), &vars.decoration_b); ++ cl_assert_equal_p(lookup_decoration(&vars.n, vars.one), NULL); +} + +void test_example_decorate__loop(void) +{ + int objects_noticed = 0; + -+ add_decoration(&vars.n, vars.one, &vars.decoration_a); -+ add_decoration(&vars.n, vars.two, &vars.decoration_b); ++ cl_assert_equal_p(add_decoration(&vars.n, vars.one, &vars.decoration_a), NULL); ++ cl_assert_equal_p(add_decoration(&vars.n, vars.two, &vars.decoration_b), NULL); + -+ for (size_t i = 0; i < vars.n.size; i++) { ++ for (size_t i = 0; i < vars.n.size; i++) + if (vars.n.entries[i].base) + objects_noticed++; -+ } -+ cl_assert_equal_i(objects_noticed, 2); -+} + -+void test_example_decorate__initialize(void) -+{ -+ struct object_id one_oid = { { 1 } }, two_oid = { { 2 } }, three_oid = { { 3 } }; -+ -+ vars.one = lookup_unknown_object(the_repository, &one_oid); -+ vars.two = lookup_unknown_object(the_repository, &two_oid); -+ vars.three = lookup_unknown_object(the_repository, &three_oid); -+} -+ -+void test_example_decorate__cleanup(void) -+{ -+ clear_decoration(&vars.n, NULL); ++ cl_assert_equal_i(objects_noticed, 2); +} 3: 08ade6b5cf ! 3: e88ab7ab5f t/unit-tests: convert strbuf test to use clar test framework @@ Commit message t/unit-tests: convert strbuf test to use clar test framework Adapt strbuf test script to clar framework by using clar assertions - where necessary. Test functions are created as standalone to test - different test cases. + where necessary. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> @@ t/unit-tests/u-strbuf.c (new) + /* Buffers should always be NUL-terminated */ + cl_assert(buf->buf[buf->len] == '\0'); + /* -+ * Freshly-initialized strbufs may not have a dynamically allocated -+ * buffer -+ */ -+ if (buf->len == 0 && buf->alloc == 0) -+ return; -+ /* alloc must be at least one byte larger than len */ -+ cl_assert(buf->len < buf->alloc); ++ * In case the buffer contains anything, `alloc` must alloc must ++ * be at least one byte larger than `len`. ++ */ ++ if (buf->len) ++ cl_assert(buf->len < buf->alloc); +} + +void test_strbuf__static_init(void) @@ t/unit-tests/u-strbuf.c (new) + setup(t_addch, ""); +} + -+void test_strbuf__add_multi_char(void) ++void test_strbuf__add_append_char(void) +{ + setup_populated(t_addch, "initial value", "a"); +} @@ t/unit-tests/u-strbuf.c (new) + setup(t_addstr, "hello there"); +} + -+void test_strbuf__add_multi_str(void) ++void test_strbuf__add_append_str(void) +{ + setup_populated(t_addstr, "initial value", "hello there"); +} 4: f648cf4a4d ! 4: 2dde9110c2 t/unit-tests: convert strcmp-offset test to use clar test framework @@ Commit message t/unit-tests: convert strcmp-offset test to use clar test framework Adapt strcmp-offset test script to clar framework by using clar - assertions where necessary. Test functions are created as standalone to - test different test cases. + assertions where necessary. Introduce `test_strcmp_offset__empty()` to + verify `check_strcmp_offset()` behavior when both input strings are + empty. This ensures the function correctly handles edge cases and + returns expected values. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> -- 2.47.0.86.g15030f9556 ^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar 2025-01-31 22:14 ` [PATCH v2 " Seyi Kuforiji @ 2025-01-31 23:06 ` Junio C Hamano 0 siblings, 0 replies; 27+ messages in thread From: Junio C Hamano @ 2025-01-31 23:06 UTC (permalink / raw) To: Seyi Kuforiji; +Cc: git, ps, phillip.wood Seyi Kuforiji <kuforiji98@gmail.com> writes: > Hello, > > This small patch series transitions the existing unit test files to the > Clar testing framework. This change is part of our ongoing effort to > standardize our testing approach and enhance maintainability. > > Changes in v2: > - small fixes to the commit messages and how they read > - some small code fix up and refactoring > > Thanks > Seyi > > Mentored-by: Patrick Steinhardt ps@pks.im > Signed-off-by: Seyi Kuforiji kuforiji98@gmail.com > > Seyi Kuforiji (4): > t/unit-tests: convert hashmap test to use clar test framework > t/unit-tests: adapt example decorate test to use clar test framework > t/unit-tests: convert strbuf test to use clar test framework > t/unit-tests: convert strcmp-offset test to use clar test framework Overall they looked quite straight-forward rewrite. Nicely done. Queued with automated fix-ups, so there is no need to resend only to fix below. Thanks. Applying: t/unit-tests: convert hashmap test to use clar test framework Applying: t/unit-tests: adapt example decorate test to use clar test framework .git/rebase-apply/patch:105: indent with spaces. * In case the buffer contains anything, `alloc` must alloc must .git/rebase-apply/patch:106: indent with spaces. * be at least one byte larger than `len`. .git/rebase-apply/patch:107: indent with spaces. */ .git/rebase-apply/patch:109: indent with spaces. cl_assert(buf->len < buf->alloc); warning: 4 lines add whitespace errors. Applying: t/unit-tests: convert strbuf test to use clar test framework Applying: t/unit-tests: convert strcmp-offset test to use clar test framework ^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH 0/5] t/unit-tests: convert unit-tests to use clar @ 2025-02-20 8:29 Seyi Kuforiji 2025-02-24 15:27 ` [PATCH v2 0/4] " Seyi Kuforiji 0 siblings, 1 reply; 27+ messages in thread From: Seyi Kuforiji @ 2025-02-20 8:29 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, Seyi Kuforiji Hello, This small patch series transitions a couple more of our existing unit test files to the Clar testing framework. This change is part of our ongoing effort to standardize our testing framework to enhance maintainability. Thanks Seyi Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> Seyi Kuforiji (5): t/unit-tests: implement oid helper functions in unit-tests.{c,h} t/unit-tests: convert oid-array test to use clar test framework t/unit-tests: convert oidmap test to use clar test framework t/unit-tests: convert oidtree test to use clar test framework t/unit-tests: remove lib-oid.{c,h,o} Makefile | 7 +- t/meson.build | 7 +- t/unit-tests/lib-oid.c | 52 ------ t/unit-tests/lib-oid.h | 25 --- t/unit-tests/{t-oid-array.c => u-oid-array.c} | 123 +++++++------- t/unit-tests/{t-oidmap.c => u-oidmap.c} | 153 ++++++------------ t/unit-tests/{t-oidtree.c => u-oidtree.c} | 78 ++++----- t/unit-tests/unit-test.c | 42 +++++ t/unit-tests/unit-test.h | 19 +++ 9 files changed, 212 insertions(+), 294 deletions(-) delete mode 100644 t/unit-tests/lib-oid.c delete mode 100644 t/unit-tests/lib-oid.h rename t/unit-tests/{t-oid-array.c => u-oid-array.c} (35%) rename t/unit-tests/{t-oidmap.c => u-oidmap.c} (32%) rename t/unit-tests/{t-oidtree.c => u-oidtree.c} (44%) -- 2.47.0.86.g15030f9556 ^ permalink raw reply [flat|nested] 27+ messages in thread
* [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar 2025-02-20 8:29 [PATCH 0/5] " Seyi Kuforiji @ 2025-02-24 15:27 ` Seyi Kuforiji 0 siblings, 0 replies; 27+ messages in thread From: Seyi Kuforiji @ 2025-02-24 15:27 UTC (permalink / raw) To: git; +Cc: ps, phillip.wood, Seyi Kuforiji Hello, This small patch series transitions a couple more of our existing unit test files to the Clar testing framework. This change is part of our ongoing effort to standardize our testing framework to enhance maintainability. Changes in v2: - fixes to the commit messages and how they read - some code refactoring based on review Thanks Seyi Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: Philip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> Seyi Kuforiji (4): t/unit-tests: implement clar specific oid helper functions t/unit-tests: convert oid-array test to use clar test framework t/unit-tests: convert oidmap test to use clar test framework t/unit-tests: convert oidtree test to use clar test framework Makefile | 8 +- t/meson.build | 8 +- t/unit-tests/lib-oid.c | 31 ++-- t/unit-tests/lib-oid.h | 9 +- t/unit-tests/{t-oid-array.c => u-oid-array.c} | 125 +++++++------- t/unit-tests/{t-oidmap.c => u-oidmap.c} | 153 +++++++----------- t/unit-tests/{t-oidtree.c => u-oidtree.c} | 79 ++++----- t/unit-tests/unit-test.c | 2 + 8 files changed, 177 insertions(+), 238 deletions(-) rename t/unit-tests/{t-oid-array.c => u-oid-array.c} (34%) rename t/unit-tests/{t-oidmap.c => u-oidmap.c} (32%) rename t/unit-tests/{t-oidtree.c => u-oidtree.c} (45%) Range-diff against v1: 1: 19192c6c89 < -: ---------- t/unit-tests: implement oid helper functions in unit-tests.{c,h} 5: e81ec73f27 ! 1: 7f14d0d574 t/unit-tests: remove lib-oid.{c,h,o} @@ Metadata Author: Seyi Kuforiji <kuforiji98@gmail.com> ## Commit message ## - t/unit-tests: remove lib-oid.{c,h,o} + t/unit-tests: implement clar specific oid helper functions - The `lib-oid.c`, `lib-oid.h`, and `lib-oid.o files` are no longer needed - since their equivalent functions have been implemented in unit-test.c - and unit-test.h. This removes redundant code and ensures all unit - test-related functionality is consolidated in a single location. + `get_oid_arbitrary_hex()` and `init_hash_algo()` are both required for + oid-related tests to run without errors. In the current implementation, + both functions are defined and declared in the + `t/unit-tests/lib-oid.{c,h}` which is utilized by oid-related tests in + the homegrown unit tests structure. - Drop references to lib-oid from our `Makefile`, and `meson.build` files - to prevent build errors due to missing files. + Adapt functions in lib-oid.{c,h} to use clar. Both these functions + become available for oid-related test files implemented using the clar + testing framework, which requires them. This will be used by subsequent + commits. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> ## Makefile ## +@@ Makefile: CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) + CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) + CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o + CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o ++CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o + + UNIT_TEST_PROGRAMS += t-oid-array + UNIT_TEST_PROGRAMS += t-oidmap @@ Makefile: UNIT_TEST_PROGRAMS += t-trailer UNIT_TEST_PROGRAMS += t-urlmatch-normalization UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS)) @@ Makefile: UNIT_TEST_PROGRAMS += t-trailer # xdiff and reftable libs may in turn depend on what is in libgit.a ## t/meson.build ## +@@ t/meson.build: clar_test_suites = [ + clar_sources = [ + 'unit-tests/clar/clar.c', + 'unit-tests/unit-test.c', ++ 'unit-tests/lib-oid.c' + ] + + clar_decls_h = custom_target( @@ t/meson.build: foreach unit_test_program : unit_test_programs unit_test = executable(unit_test_name, sources: [ @@ t/meson.build: foreach unit_test_program : unit_test_programs unit_test_program, ], - ## t/unit-tests/lib-oid.c (deleted) ## + ## t/unit-tests/lib-oid.c ## @@ -#include "test-lib.h" --#include "lib-oid.h" --#include "strbuf.h" --#include "hex.h" -- ++#include "unit-test.h" + #include "lib-oid.h" + #include "strbuf.h" + #include "hex.h" + -int init_hash_algo(void) --{ -- static int algo = -1; -- -- if (algo < 0) { -- const char *algo_name = getenv("GIT_TEST_DEFAULT_HASH"); -- algo = algo_name ? hash_algo_by_name(algo_name) : GIT_HASH_SHA1; -- ++int cl_setup_hash_algo(void) + { + static int algo = -1; + +@@ t/unit-tests/lib-oid.c: int init_hash_algo(void) + const char *algo_name = getenv("GIT_TEST_DEFAULT_HASH"); + algo = algo_name ? hash_algo_by_name(algo_name) : GIT_HASH_SHA1; + - if (!check(algo != GIT_HASH_UNKNOWN)) - test_msg("BUG: invalid GIT_TEST_DEFAULT_HASH value ('%s')", - algo_name); -- } -- return algo; --} -- ++ cl_assert(algo != GIT_HASH_UNKNOWN); + } + return algo; + } + -static int get_oid_arbitrary_hex_algop(const char *hex, struct object_id *oid, -- const struct git_hash_algo *algop) --{ -- int ret; -- size_t sz = strlen(hex); -- struct strbuf buf = STRBUF_INIT; -- ++static void cl_parse_oid(const char *hex, struct object_id *oid, + const struct git_hash_algo *algop) + { + int ret; + size_t sz = strlen(hex); + struct strbuf buf = STRBUF_INIT; + - if (!check(sz <= algop->hexsz)) { - test_msg("BUG: hex string (%s) bigger than maximum allowed (%lu)", - hex, (unsigned long)algop->hexsz); - return -1; - } -- -- strbuf_add(&buf, hex, sz); -- strbuf_addchars(&buf, '0', algop->hexsz - sz); -- ++ cl_assert(sz <= algop->hexsz); + + strbuf_add(&buf, hex, sz); + strbuf_addchars(&buf, '0', algop->hexsz - sz); + - ret = get_oid_hex_algop(buf.buf, oid, algop); - if (!check_int(ret, ==, 0)) - test_msg("BUG: invalid hex input (%s) provided", hex); -- -- strbuf_release(&buf); ++ cl_assert_equal_i(get_oid_hex_algop(buf.buf, oid, algop), 0); + + strbuf_release(&buf); - return ret; --} -- + } + -int get_oid_arbitrary_hex(const char *hex, struct object_id *oid) --{ ++ ++void cl_parse_any_oid(const char *hex, struct object_id *oid) + { - int hash_algo = init_hash_algo(); -- ++ int hash_algo = cl_setup_hash_algo(); + - if (!check_int(hash_algo, !=, GIT_HASH_UNKNOWN)) - return -1; - return get_oid_arbitrary_hex_algop(hex, oid, &hash_algos[hash_algo]); --} ++ cl_assert(hash_algo != GIT_HASH_UNKNOWN); ++ cl_parse_oid(hex, oid, &hash_algos[hash_algo]); + } - ## t/unit-tests/lib-oid.h (deleted) ## + ## t/unit-tests/lib-oid.h ## +@@ + + /* + * Convert arbitrary hex string to object_id. ++ * + * For example, passing "abc12" will generate + * "abc1200000000000000000000000000000000000" hex of length 40 for SHA-1 and + * create object_id with that. @@ --#ifndef LIB_OID_H --#define LIB_OID_H -- --#include "hash.h" -- --/* -- * Convert arbitrary hex string to object_id. -- * For example, passing "abc12" will generate -- * "abc1200000000000000000000000000000000000" hex of length 40 for SHA-1 and -- * create object_id with that. -- * WARNING: passing a string of length more than the hexsz of respective hash -- * algo is not allowed. The hash algo is decided based on GIT_TEST_DEFAULT_HASH -- * environment variable. -- */ + * algo is not allowed. The hash algo is decided based on GIT_TEST_DEFAULT_HASH + * environment variable. + */ -int get_oid_arbitrary_hex(const char *s, struct object_id *oid); --/* -- * Returns one of GIT_HASH_{SHA1, SHA256, UNKNOWN} based on the value of -- * GIT_TEST_DEFAULT_HASH environment variable. The fallback value in the -- * absence of GIT_TEST_DEFAULT_HASH is GIT_HASH_SHA1. It also uses ++ ++void cl_parse_any_oid (const char *s, struct object_id *oid); + /* + * Returns one of GIT_HASH_{SHA1, SHA256, UNKNOWN} based on the value of + * GIT_TEST_DEFAULT_HASH environment variable. The fallback value in the + * absence of GIT_TEST_DEFAULT_HASH is GIT_HASH_SHA1. It also uses - * check(algo != GIT_HASH_UNKNOWN) before returning to verify if the -- * GIT_TEST_DEFAULT_HASH's value is valid or not. -- */ ++ * cl_assert(algo != GIT_HASH_UNKNOWN) before returning to verify if the + * GIT_TEST_DEFAULT_HASH's value is valid or not. + */ -int init_hash_algo(void); -- --#endif /* LIB_OID_H */ ++ ++int cl_setup_hash_algo(void); + + #endif /* LIB_OID_H */ + + ## t/unit-tests/unit-test.c ## +@@ + #include "unit-test.h" ++#include "hex.h" + #include "parse-options.h" ++#include "strbuf.h" + #include "string-list.h" + #include "strvec.h" + 2: 8a99bbdc31 ! 2: 430f5c5007 t/unit-tests: convert oid-array test to use clar test framework @@ Makefile: CLAR_TEST_SUITES += u-example-decorate CLAR_TEST_SUITES += u-prio-queue CLAR_TEST_SUITES += u-reftable-tree CLAR_TEST_SUITES += u-strbuf -@@ Makefile: CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) - CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o +@@ Makefile: CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o + CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o -UNIT_TEST_PROGRAMS += t-oid-array UNIT_TEST_PROGRAMS += t-oidmap @@ t/unit-tests/u-oid-array.c (new) +#define USE_THE_REPOSITORY_VARIABLE + +#include "unit-test.h" ++#include "lib-oid.h" +#include "oid-array.h" +#include "hex.h" + @@ t/unit-tests/u-oid-array.c (new) +void test_oid_array__initialize(void) +{ + /* The hash algo is used by oid_array_lookup() internally */ -+ int algo = init_hash_algo(); -+ cl_assert(algo != GIT_HASH_UNKNOWN); ++ int algo = cl_setup_hash_algo(); + repo_set_hash_algo(the_repository, algo); +} + @@ t/unit-tests/u-oid-array.c (new) +{ + const char *nearly_55; + -+ nearly_55 = init_hash_algo() == GIT_HASH_SHA1 ? ++ nearly_55 = cl_setup_hash_algo() == GIT_HASH_SHA1 ? + "5500000000000000000000000000000000000001" : + "5500000000000000000000000000000000000000000000000000000000000001"; + 3: c19545e2bc ! 3: 319cea1265 t/unit-tests: convert oidmap test to use clar test framework @@ Commit message t/unit-tests: convert oidmap test to use clar test framework Adapt oidmap test script to clar framework by using clar assertions - where necessary. `cl_parse_any_oid` handles the necessary checks needed - for the test to run smoothly. + where necessary. `cl_parse_any_oid()` ensures the hash algorithm is set + before parsing. This prevents issues from an uninitialized or invalid + hash algorithm. Introduce 'test_oidmap__initialize` handles the to set up of the global oidmap map with predefined key-value pairs, and `test_oidmap__cleanup` frees the oidmap and its entries when all tests are completed. - This streamlines the test suite, making individual tests self-contained - and reducing redundant code. + The test loops through all entries to detect multiple errors. With this + change, it stops at the first error encountered, making it easier to + address it. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> @@ Makefile: CLAR_TEST_SUITES += u-hash CLAR_TEST_SUITES += u-prio-queue CLAR_TEST_SUITES += u-reftable-tree CLAR_TEST_SUITES += u-strbuf -@@ Makefile: CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) - CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o +@@ Makefile: CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o + CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o -UNIT_TEST_PROGRAMS += t-oidmap UNIT_TEST_PROGRAMS += t-oidtree @@ t/unit-tests/t-oidmap.c (deleted) ## t/unit-tests/u-oidmap.c (new) ## @@ +#include "unit-test.h" ++#include "lib-oid.h" +#include "oidmap.h" +#include "hash.h" +#include "hex.h" @@ t/unit-tests/u-oidmap.c (new) + + oidmap_iter_init(&map, &iter); + while ((entry = oidmap_iter_next(&iter))) { -+ cl_assert_equal_i(key_val_contains(entry, seen), 0); ++ if (key_val_contains(entry, seen) != 0) { ++ cl_failf("Unexpected entry: name = %s, oid = %s", ++ entry->name, oid_to_hex(&entry->entry.oid)); ++ } + count++; + } + cl_assert_equal_i(count, ARRAY_SIZE(key_val)); 4: 733b53cd05 ! 4: ea63a5c9f1 t/unit-tests: convert oidtree test to use clar test framework @@ Commit message t/unit-tests: convert oidtree test to use clar test framework Adapt oidtree test script to clar framework by using clar assertions - where necessary. `cl_parse_any_oid` handles the necessary checks needed - for the test to run smoothly. + where necessary. `cl_parse_any_oid()` ensures the hash algorithm is set + before parsing. This prevents issues from an uninitialized or invalid + hash algorithm. Introduce 'test_oidtree__initialize` handles the to set up of the global oidtree variable and `test_oidtree__cleanup` frees the oidtree when all tests are completed. - This streamlines the test suite, making individual tests self-contained - and reducing redundant code. + With this change, `check_each` stops at the first error encountered, + making it easier to address it. Mentored-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> @@ Makefile: CLAR_TEST_SUITES += u-hashmap CLAR_TEST_SUITES += u-prio-queue CLAR_TEST_SUITES += u-reftable-tree CLAR_TEST_SUITES += u-strbuf -@@ Makefile: CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) - CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o +@@ Makefile: CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o + CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o -UNIT_TEST_PROGRAMS += t-oidtree UNIT_TEST_PROGRAMS += t-reftable-basics @@ t/unit-tests/t-oidtree.c (deleted) ## t/unit-tests/u-oidtree.c (new) ## @@ +#include "unit-test.h" ++#include "lib-oid.h" +#include "oidtree.h" +#include "hash.h" +#include "hex.h" @@ t/unit-tests/u-oidtree.c (new) + cl_parse_any_oid(query, &oid); + oidtree_each(ot, &oid, strlen(query), check_each_cb, &hex_iter); + -+ cl_assert_equal_i(hex_iter.i, hex_iter.expected_hexes.nr); ++ if (hex_iter.i != hex_iter.expected_hexes.nr) ++ cl_failf("error: could not find some 'object_id's for query ('%s')", query); ++ + strvec_clear(&hex_iter.expected_hexes); +} + -- 2.47.0.86.g15030f9556 ^ permalink raw reply [flat|nested] 27+ messages in thread
end of thread, other threads:[~2025-02-24 15:27 UTC | newest] Thread overview: 27+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2025-01-16 10:49 [PATCH 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji 2025-01-16 10:49 ` [PATCH 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji 2025-01-16 13:12 ` Patrick Steinhardt 2025-01-16 10:49 ` [PATCH 2/4] t/unit-tests: convert mem-pool test to use clar test framework Seyi Kuforiji 2025-01-16 13:12 ` Patrick Steinhardt 2025-01-16 17:45 ` Junio C Hamano 2025-01-16 10:49 ` [PATCH 3/4] t/unit-tests: adapt priority queue " Seyi Kuforiji 2025-01-16 13:13 ` Patrick Steinhardt 2025-01-16 10:49 ` [PATCH 4/4] t/unit-tests: convert reftable tree " Seyi Kuforiji 2025-01-16 16:15 ` [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji 2025-01-16 16:15 ` [PATCH v2 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji 2025-01-17 6:27 ` Patrick Steinhardt 2025-01-16 16:15 ` [PATCH v2 2/4] t/unit-tests: convert mem-pool test to use clar test framework Seyi Kuforiji 2025-01-16 16:15 ` [PATCH v2 3/4] t/unit-tests: adapt priority queue " Seyi Kuforiji 2025-01-16 16:15 ` [PATCH v2 4/4] t/unit-tests: convert reftable tree " Seyi Kuforiji 2025-01-17 6:27 ` [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar Patrick Steinhardt 2025-01-17 12:29 ` Seyi Kuforiji 2025-01-17 12:29 ` [PATCH v3 1/4] t/unit-tests: handle dashes in test suite filenames Seyi Kuforiji 2025-01-17 12:29 ` [PATCH v3 2/4] t/unit-tests: convert mem-pool test to use clar test framework Seyi Kuforiji 2025-01-20 10:18 ` Karthik Nayak 2025-01-17 12:29 ` [PATCH v3 3/4] t/unit-tests: adapt priority queue " Seyi Kuforiji 2025-01-20 10:22 ` Karthik Nayak 2025-01-17 12:29 ` [PATCH v3 4/4] t/unit-tests: convert reftable tree " Seyi Kuforiji 2025-01-17 13:36 ` t/unit-tests: convert unit-tests to use clar Patrick Steinhardt -- strict thread matches above, loose matches on Subject: below -- 2025-01-30 9:13 [PATCH 0/4] " Seyi Kuforiji 2025-01-31 22:14 ` [PATCH v2 " Seyi Kuforiji 2025-01-31 23:06 ` Junio C Hamano 2025-02-20 8:29 [PATCH 0/5] " Seyi Kuforiji 2025-02-24 15:27 ` [PATCH v2 0/4] " Seyi Kuforiji
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).