From: Seyi Kuforiji <kuforiji98@gmail•com>
To: git@vger•kernel.org
Cc: ps@pks•im, phillip.wood@dunelm•org.uk,
Seyi Kuforiji <kuforiji98@gmail•com>
Subject: [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar
Date: Fri, 31 Jan 2025 23:14:16 +0100 [thread overview]
Message-ID: <20250131221420.38161-1-kuforiji98@gmail.com> (raw)
In-Reply-To: <20250130091334.39922-1-kuforiji98@gmail.com>
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
next prev parent reply other threads:[~2025-01-31 22:14 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-30 9:13 [PATCH 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji
2025-01-30 9:13 ` [PATCH 1/4] t/unit-tests: convert hashmap test to clar framework Seyi Kuforiji
2025-01-31 11:43 ` Patrick Steinhardt
2025-01-30 9:13 ` [PATCH 2/4] t/unit-tests: adapt example decorate " Seyi Kuforiji
2025-01-31 11:43 ` Patrick Steinhardt
2025-01-30 9:13 ` [PATCH 3/4] t/unit-tests: convert strbuf " Seyi Kuforiji
2025-01-31 11:43 ` Patrick Steinhardt
2025-01-30 9:13 ` [PATCH 4/4] t/unit-tests: convert strcmp-offset " Seyi Kuforiji
2025-01-31 11:44 ` Patrick Steinhardt
2025-01-31 11:43 ` [PATCH 0/4] t/unit-tests: convert unit-tests to use clar Patrick Steinhardt
2025-01-31 22:14 ` Seyi Kuforiji [this message]
2025-01-31 22:14 ` [PATCH v2 1/4] t/unit-tests: convert hashmap test to use clar test framework Seyi Kuforiji
2025-01-31 23:03 ` Junio C Hamano
2025-02-02 11:09 ` phillip.wood123
2025-02-03 7:30 ` Patrick Steinhardt
2025-02-03 14:56 ` phillip.wood123
2025-01-31 22:14 ` [PATCH v2 2/4] t/unit-tests: adapt example decorate " Seyi Kuforiji
2025-01-31 22:14 ` [PATCH v2 3/4] t/unit-tests: convert strbuf " Seyi Kuforiji
2025-02-02 14:38 ` Phillip Wood
2025-01-31 22:14 ` [PATCH v2 4/4] t/unit-tests: convert strcmp-offset " Seyi Kuforiji
2025-01-31 23:06 ` [PATCH v2 0/4] t/unit-tests: convert unit-tests to use clar Junio C Hamano
-- strict thread matches above, loose matches on Subject: below --
2025-02-20 8:29 [PATCH 0/5] " Seyi Kuforiji
2025-02-24 15:27 ` [PATCH v2 0/4] " Seyi Kuforiji
2025-01-16 10:49 [PATCH " Seyi Kuforiji
2025-01-16 16:15 ` [PATCH v2 " Seyi Kuforiji
2025-01-17 6:27 ` Patrick Steinhardt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250131221420.38161-1-kuforiji98@gmail.com \
--to=kuforiji98@gmail$(echo .)com \
--cc=git@vger$(echo .)kernel.org \
--cc=phillip.wood@dunelm$(echo .)org.uk \
--cc=ps@pks$(echo .)im \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox