]> git.ipfire.org Git - thirdparty/git.git/commitdiff
t/unit-tests: convert strcmp-offset test to use clar test framework
authorSeyi Kuforiji <kuforiji98@gmail.com>
Fri, 31 Jan 2025 22:14:20 +0000 (23:14 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 31 Jan 2025 22:58:45 +0000 (14:58 -0800)
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>
Makefile
t/meson.build
t/unit-tests/t-strcmp-offset.c [deleted file]
t/unit-tests/u-strcmp-offset.c [new file with mode: 0644]

index cdf999784627737cc30031938720c62aabee9dfd..efa9df2128751b7bfa9e736039dc86648885a52a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1347,6 +1347,7 @@ CLAR_TEST_SUITES += u-mem-pool
 CLAR_TEST_SUITES += u-prio-queue
 CLAR_TEST_SUITES += u-reftable-tree
 CLAR_TEST_SUITES += u-strbuf
+CLAR_TEST_SUITES += u-strcmp-offset
 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))
@@ -1364,7 +1365,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-strcmp-offset
 UNIT_TEST_PROGRAMS += t-trailer
 UNIT_TEST_PROGRAMS += t-urlmatch-normalization
 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
index dffb7f7cdf35bc3afcb22ba1853f52640ac9dac4..45742805909b66cae618ab96aebf2f522862f3c3 100644 (file)
@@ -7,6 +7,7 @@ clar_test_suites = [
   'unit-tests/u-prio-queue.c',
   'unit-tests/u-reftable-tree.c',
   'unit-tests/u-strbuf.c',
+  'unit-tests/u-strcmp-offset.c',
   'unit-tests/u-strvec.c',
 ]
 
@@ -58,7 +59,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-strcmp-offset.c',
   'unit-tests/t-trailer.c',
   'unit-tests/t-urlmatch-normalization.c',
 ]
diff --git a/t/unit-tests/t-strcmp-offset.c b/t/unit-tests/t-strcmp-offset.c
deleted file mode 100644 (file)
index 6880f21..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-#include "test-lib.h"
-#include "read-cache-ll.h"
-
-static void check_strcmp_offset(const char *string1, const char *string2,
-                               int expect_result, uintmax_t expect_offset)
-{
-       size_t offset;
-       int result = strcmp_offset(string1, string2, &offset);
-
-       /*
-        * Because different CRTs behave differently, only rely on signs of the
-        * result values.
-        */
-       result = (result < 0 ? -1 :
-                       result > 0 ? 1 :
-                       0);
-
-       check_int(result, ==, expect_result);
-       check_uint((uintmax_t)offset, ==, expect_offset);
-}
-
-#define TEST_STRCMP_OFFSET(string1, string2, expect_result, expect_offset) \
-       TEST(check_strcmp_offset(string1, string2, expect_result,          \
-                                expect_offset),                           \
-            "strcmp_offset(%s, %s) works", #string1, #string2)
-
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
-{
-       TEST_STRCMP_OFFSET("abc", "abc", 0, 3);
-       TEST_STRCMP_OFFSET("abc", "def", -1, 0);
-       TEST_STRCMP_OFFSET("abc", "abz", -1, 2);
-       TEST_STRCMP_OFFSET("abc", "abcdef", -1, 3);
-
-       return test_done();
-}
diff --git a/t/unit-tests/u-strcmp-offset.c b/t/unit-tests/u-strcmp-offset.c
new file mode 100644 (file)
index 0000000..7e8e9ac
--- /dev/null
@@ -0,0 +1,45 @@
+#include "unit-test.h"
+#include "read-cache-ll.h"
+
+static void check_strcmp_offset(const char *string1, const char *string2,
+                               int expect_result, uintmax_t expect_offset)
+{
+       size_t offset;
+       int result = strcmp_offset(string1, string2, &offset);
+
+       /*
+        * Because different CRTs behave differently, only rely on signs of the
+        * result values.
+        */
+       result = (result < 0 ? -1 :
+                       result > 0 ? 1 :
+                       0);
+
+       cl_assert_equal_i(result, expect_result);
+       cl_assert_equal_i((uintmax_t)offset, expect_offset);
+}
+
+void test_strcmp_offset__empty(void)
+{
+       check_strcmp_offset("", "", 0, 0);
+}
+
+void test_strcmp_offset__equal(void)
+{
+       check_strcmp_offset("abc", "abc", 0, 3);
+}
+
+void test_strcmp_offset__different(void)
+{
+       check_strcmp_offset("abc", "def", -1, 0);
+}
+
+void test_strcmp_offset__mismatch(void)
+{
+       check_strcmp_offset("abc", "abz", -1, 2);
+}
+
+void test_strcmp_offset__different_length(void)
+{
+       check_strcmp_offset("abc", "abcdef", -1, 3);
+}