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 v3 4/4] t/unit-tests: convert oidtree test to use clar test framework
Date: Tue, 25 Feb 2025 11:10:44 +0100 [thread overview]
Message-ID: <20250225101044.84210-5-kuforiji98@gmail.com> (raw)
In-Reply-To: <20250225101044.84210-1-kuforiji98@gmail.com>
Adapt oidtree test script to clar framework by using clar assertions
where necessary. `cl_parse_any_oid()` ensures the hash algorithm is set
before parsing. This prevents issues from an uninitialized or invalid
hash algorithm.
Introduce 'test_oidtree__initialize` handles the to set up of the global
oidtree variable and `test_oidtree__cleanup` frees the oidtree when all
tests are completed.
With this change, `check_each` stops at the first error encountered,
making it easier to address it.
Mentored-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com>
---
Makefile | 2 +-
t/meson.build | 2 +-
t/unit-tests/{t-oidtree.c => u-oidtree.c} | 79 +++++++++--------------
3 files changed, 34 insertions(+), 49 deletions(-)
rename t/unit-tests/{t-oidtree.c => u-oidtree.c} (45%)
diff --git a/Makefile b/Makefile
index e4e85e6007..2b134efc70 100644
--- a/Makefile
+++ b/Makefile
@@ -1358,6 +1358,7 @@ CLAR_TEST_SUITES += u-hashmap
CLAR_TEST_SUITES += u-mem-pool
CLAR_TEST_SUITES += u-oid-array
CLAR_TEST_SUITES += u-oidmap
+CLAR_TEST_SUITES += u-oidtree
CLAR_TEST_SUITES += u-prio-queue
CLAR_TEST_SUITES += u-reftable-tree
CLAR_TEST_SUITES += u-strbuf
@@ -1369,7 +1370,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
-UNIT_TEST_PROGRAMS += t-oidtree
UNIT_TEST_PROGRAMS += t-reftable-basics
UNIT_TEST_PROGRAMS += t-reftable-block
UNIT_TEST_PROGRAMS += t-reftable-merged
diff --git a/t/meson.build b/t/meson.build
index d5b83cdb72..91699917ff 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -6,6 +6,7 @@ clar_test_suites = [
'unit-tests/u-mem-pool.c',
'unit-tests/u-oid-array.c',
'unit-tests/u-oidmap.c',
+ 'unit-tests/u-oidtree.c',
'unit-tests/u-prio-queue.c',
'unit-tests/u-reftable-tree.c',
'unit-tests/u-strbuf.c',
@@ -51,7 +52,6 @@ clar_unit_tests = executable('unit-tests',
test('unit-tests', clar_unit_tests)
unit_test_programs = [
- 'unit-tests/t-oidtree.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-oidtree.c b/t/unit-tests/u-oidtree.c
similarity index 45%
rename from t/unit-tests/t-oidtree.c
rename to t/unit-tests/u-oidtree.c
index a38754b066..e6eede2740 100644
--- a/t/unit-tests/t-oidtree.c
+++ b/t/unit-tests/u-oidtree.c
@@ -1,10 +1,12 @@
-#include "test-lib.h"
+#include "unit-test.h"
#include "lib-oid.h"
#include "oidtree.h"
#include "hash.h"
#include "hex.h"
#include "strvec.h"
+static struct oidtree ot;
+
#define FILL_TREE(tree, ...) \
do { \
const char *hexes[] = { __VA_ARGS__ }; \
@@ -16,8 +18,7 @@ static int fill_tree_loc(struct oidtree *ot, const char *hexes[], size_t n)
{
for (size_t i = 0; i < n; i++) {
struct object_id oid;
- if (!check_int(get_oid_arbitrary_hex(hexes[i], &oid), ==, 0))
- return -1;
+ cl_parse_any_oid(hexes[i], &oid);
oidtree_insert(ot, &oid);
}
return 0;
@@ -27,10 +28,8 @@ static void check_contains(struct oidtree *ot, const char *hex, int expected)
{
struct object_id oid;
- if (!check_int(get_oid_arbitrary_hex(hex, &oid), ==, 0))
- return;
- if (!check_int(oidtree_contains(ot, &oid), ==, expected))
- test_msg("oid: %s", oid_to_hex(&oid));
+ cl_parse_any_oid(hex, &oid);
+ cl_assert_equal_i(oidtree_contains(ot, &oid), expected);
}
struct expected_hex_iter {
@@ -44,19 +43,11 @@ static enum cb_next check_each_cb(const struct object_id *oid, void *data)
struct expected_hex_iter *hex_iter = data;
struct object_id expected;
- if (!check_int(hex_iter->i, <, hex_iter->expected_hexes.nr)) {
- test_msg("error: extraneous callback for query: ('%s'), object_id: ('%s')",
- hex_iter->query, oid_to_hex(oid));
- return CB_BREAK;
- }
-
- if (!check_int(get_oid_arbitrary_hex(hex_iter->expected_hexes.v[hex_iter->i],
- &expected), ==, 0))
- ; /* the data is bogus and cannot be used */
- else if (!check(oideq(oid, &expected)))
- test_msg("expected: %s\n got: %s\n query: %s",
- oid_to_hex(&expected), oid_to_hex(oid), hex_iter->query);
+ cl_assert(hex_iter->i < hex_iter->expected_hexes.nr);
+ cl_parse_any_oid(hex_iter->expected_hexes.v[hex_iter->i],
+ &expected);
+ cl_assert_equal_s(oid_to_hex(oid), oid_to_hex(&expected));
hex_iter->i += 1;
return CB_CONTINUE;
}
@@ -75,48 +66,42 @@ static void check_each(struct oidtree *ot, const char *query, ...)
strvec_push(&hex_iter.expected_hexes, arg);
va_end(hex_args);
- if (!check_int(get_oid_arbitrary_hex(query, &oid), ==, 0))
- return;
+ cl_parse_any_oid(query, &oid);
oidtree_each(ot, &oid, strlen(query), check_each_cb, &hex_iter);
- if (!check_int(hex_iter.i, ==, hex_iter.expected_hexes.nr))
- test_msg("error: could not find some 'object_id's for query ('%s')", query);
+ if (hex_iter.i != hex_iter.expected_hexes.nr)
+ cl_failf("error: could not find some 'object_id's for query ('%s')", query);
+
strvec_clear(&hex_iter.expected_hexes);
}
-static void setup(void (*f)(struct oidtree *ot))
+void test_oidtree__initialize(void)
{
- struct oidtree ot;
-
oidtree_init(&ot);
- f(&ot);
- oidtree_clear(&ot);
}
-static void t_contains(struct oidtree *ot)
+void test_oidtree__cleanup(void)
{
- FILL_TREE(ot, "444", "1", "2", "3", "4", "5", "a", "b", "c", "d", "e");
- check_contains(ot, "44", 0);
- check_contains(ot, "441", 0);
- check_contains(ot, "440", 0);
- check_contains(ot, "444", 1);
- check_contains(ot, "4440", 1);
- check_contains(ot, "4444", 0);
+ oidtree_clear(&ot);
}
-static void t_each(struct oidtree *ot)
+void test_oidtree__contains(void)
{
- FILL_TREE(ot, "f", "9", "8", "123", "321", "320", "a", "b", "c", "d", "e");
- check_each(ot, "12300", "123", NULL);
- check_each(ot, "3211", NULL); /* should not reach callback */
- check_each(ot, "3210", "321", NULL);
- check_each(ot, "32100", "321", NULL);
- check_each(ot, "32", "320", "321", NULL);
+ FILL_TREE(&ot, "444", "1", "2", "3", "4", "5", "a", "b", "c", "d", "e");
+ check_contains(&ot, "44", 0);
+ check_contains(&ot, "441", 0);
+ check_contains(&ot, "440", 0);
+ check_contains(&ot, "444", 1);
+ check_contains(&ot, "4440", 1);
+ check_contains(&ot, "4444", 0);
}
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
+void test_oidtree__each(void)
{
- TEST(setup(t_contains), "oidtree insert and contains works");
- TEST(setup(t_each), "oidtree each works");
- return test_done();
+ FILL_TREE(&ot, "f", "9", "8", "123", "321", "320", "a", "b", "c", "d", "e");
+ check_each(&ot, "12300", "123", NULL);
+ check_each(&ot, "3211", NULL); /* should not reach callback */
+ check_each(&ot, "3210", "321", NULL);
+ check_each(&ot, "32100", "321", NULL);
+ check_each(&ot, "32", "320", "321", NULL);
}
--
2.47.0.86.g15030f9556
prev parent reply other threads:[~2025-02-25 10:13 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-20 8:29 [PATCH 0/5] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji
2025-02-20 8:29 ` [PATCH 1/5] t/unit-tests: implement oid helper functions in unit-tests.{c,h} Seyi Kuforiji
2025-02-20 14:38 ` Phillip Wood
2025-02-21 7:59 ` Patrick Steinhardt
2025-02-21 7:59 ` Patrick Steinhardt
2025-02-21 14:50 ` phillip.wood123
2025-02-20 8:29 ` [PATCH 2/5] t/unit-tests: convert oid-array test to use clar Seyi Kuforiji
2025-02-20 14:38 ` Phillip Wood
2025-02-24 9:11 ` Seyi Chamber
2025-02-24 10:12 ` phillip.wood123
2025-02-20 8:29 ` [PATCH 3/5] t/unit-tests: convert oidmap " Seyi Kuforiji
2025-02-21 10:04 ` phillip.wood123
2025-02-24 10:56 ` Seyi Chamber
2025-02-20 8:29 ` [PATCH 4/5] t/unit-tests: convert oidtree " Seyi Kuforiji
2025-02-21 14:48 ` phillip.wood123
2025-02-20 8:29 ` [PATCH 5/5] t/unit-tests: remove lib-oid.{c,h,o} Seyi Kuforiji
2025-02-21 14:52 ` [PATCH 0/5] t/unit-tests: convert unit-tests to use clar phillip.wood123
2025-02-24 15:27 ` [PATCH v2 0/4] " Seyi Kuforiji
2025-02-24 15:27 ` [PATCH v2 1/4] t/unit-tests: implement clar specific oid helper functions Seyi Kuforiji
2025-02-24 17:55 ` Junio C Hamano
2025-02-25 7:14 ` Seyi Chamber
2025-02-25 7:56 ` Patrick Steinhardt
2025-02-24 15:27 ` [PATCH v2 2/4] t/unit-tests: convert oid-array test to use clar test framework Seyi Kuforiji
2025-02-24 15:27 ` [PATCH v2 3/4] t/unit-tests: convert oidmap " Seyi Kuforiji
2025-02-24 15:27 ` [PATCH v2 4/4] t/unit-tests: convert oidtree " Seyi Kuforiji
2025-02-25 10:10 ` [PATCH v3 0/4] t/unit-tests: convert unit-tests to use clar Seyi Kuforiji
2025-02-25 10:10 ` [PATCH v3 1/4] t/unit-tests: implement clar specific oid helper functions Seyi Kuforiji
2025-02-25 10:10 ` [PATCH v3 2/4] t/unit-tests: convert oid-array test to use clar test framework Seyi Kuforiji
2025-02-25 10:10 ` [PATCH v3 3/4] t/unit-tests: convert oidmap " Seyi Kuforiji
2025-02-25 10:10 ` Seyi Kuforiji [this message]
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=20250225101044.84210-5-kuforiji98@gmail.com \
--to=kuforiji98@gmail.com \
--cc=git@vger.kernel.org \
--cc=phillip.wood@dunelm.org.uk \
--cc=ps@pks.im \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
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;
as well as URLs for NNTP newsgroup(s).