* [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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ 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; 24+ 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] 24+ messages in thread
end of thread, other threads:[~2025-01-20 10:22 UTC | newest] Thread overview: 24+ 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
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).