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. 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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Seyi Kuforiji committed Jan 31, 2025 at 23:14 UTC af8bf677c150144166454f311642825a0b08e506
4 files changed +47 -37
Makefile
+1 -1
@@ -1347,6 +1347,7 @@ CLAR_TEST_SUITES += u-mem-pool
1347 CLAR_TEST_SUITES += u-prio-queue
1348 CLAR_TEST_SUITES += u-reftable-tree
1349 CLAR_TEST_SUITES += u-strbuf
1350 +CLAR_TEST_SUITES += u-strcmp-offset
1351 CLAR_TEST_SUITES += u-strvec
1352 CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
1353 CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
@@ -1364,7 +1365,6 @@ UNIT_TEST_PROGRAMS += t-reftable-reader
1365 UNIT_TEST_PROGRAMS += t-reftable-readwrite
1366 UNIT_TEST_PROGRAMS += t-reftable-record
1367 UNIT_TEST_PROGRAMS += t-reftable-stack
1367 -UNIT_TEST_PROGRAMS += t-strcmp-offset
1368 UNIT_TEST_PROGRAMS += t-trailer
1369 UNIT_TEST_PROGRAMS += t-urlmatch-normalization
1370 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
t/meson.build
+1 -1
@@ -7,6 +7,7 @@ clar_test_suites = [
7 'unit-tests/u-prio-queue.c',
8 'unit-tests/u-reftable-tree.c',
9 'unit-tests/u-strbuf.c',
10 + 'unit-tests/u-strcmp-offset.c',
11 'unit-tests/u-strvec.c',
12 ]
13
@@ -58,7 +59,6 @@ unit_test_programs = [
59 'unit-tests/t-reftable-readwrite.c',
60 'unit-tests/t-reftable-record.c',
61 'unit-tests/t-reftable-stack.c',
61 - 'unit-tests/t-strcmp-offset.c',
62 'unit-tests/t-trailer.c',
63 'unit-tests/t-urlmatch-normalization.c',
64 ]
t/unit-tests/t-strcmp-offset.c deleted
-35
@@ -1,35 +0,0 @@
1 -#include "test-lib.h"
2 -#include "read-cache-ll.h"
3 -
4 -static void check_strcmp_offset(const char *string1, const char *string2,
5 - int expect_result, uintmax_t expect_offset)
6 -{
7 - size_t offset;
8 - int result = strcmp_offset(string1, string2, &offset);
9 -
10 - /*
11 - * Because different CRTs behave differently, only rely on signs of the
12 - * result values.
13 - */
14 - result = (result < 0 ? -1 :
15 - result > 0 ? 1 :
16 - 0);
17 -
18 - check_int(result, ==, expect_result);
19 - check_uint((uintmax_t)offset, ==, expect_offset);
20 -}
21 -
22 -#define TEST_STRCMP_OFFSET(string1, string2, expect_result, expect_offset) \
23 - TEST(check_strcmp_offset(string1, string2, expect_result, \
24 - expect_offset), \
25 - "strcmp_offset(%s, %s) works", #string1, #string2)
26 -
27 -int cmd_main(int argc UNUSED, const char **argv UNUSED)
28 -{
29 - TEST_STRCMP_OFFSET("abc", "abc", 0, 3);
30 - TEST_STRCMP_OFFSET("abc", "def", -1, 0);
31 - TEST_STRCMP_OFFSET("abc", "abz", -1, 2);
32 - TEST_STRCMP_OFFSET("abc", "abcdef", -1, 3);
33 -
34 - return test_done();
35 -}
t/unit-tests/u-strcmp-offset.c new
+45
@@ -0,0 +1,45 @@
1 +#include "unit-test.h"
2 +#include "read-cache-ll.h"
3 +
4 +static void check_strcmp_offset(const char *string1, const char *string2,
5 + int expect_result, uintmax_t expect_offset)
6 +{
7 + size_t offset;
8 + int result = strcmp_offset(string1, string2, &offset);
9 +
10 + /*
11 + * Because different CRTs behave differently, only rely on signs of the
12 + * result values.
13 + */
14 + result = (result < 0 ? -1 :
15 + result > 0 ? 1 :
16 + 0);
17 +
18 + cl_assert_equal_i(result, expect_result);
19 + cl_assert_equal_i((uintmax_t)offset, expect_offset);
20 +}
21 +
22 +void test_strcmp_offset__empty(void)
23 +{
24 + check_strcmp_offset("", "", 0, 0);
25 +}
26 +
27 +void test_strcmp_offset__equal(void)
28 +{
29 + check_strcmp_offset("abc", "abc", 0, 3);
30 +}
31 +
32 +void test_strcmp_offset__different(void)
33 +{
34 + check_strcmp_offset("abc", "def", -1, 0);
35 +}
36 +
37 +void test_strcmp_offset__mismatch(void)
38 +{
39 + check_strcmp_offset("abc", "abz", -1, 2);
40 +}
41 +
42 +void test_strcmp_offset__different_length(void)
43 +{
44 + check_strcmp_offset("abc", "abcdef", -1, 3);
45 +}