t/: port helper/test-strcmp-offset.c to unit-tests/t-strcmp-offset.c

In the recent codebase update (8bf6fbd (Merge branch 'js/doc-unit-tests', 2023-12-09)), a new unit testing framework was merged, providing a standardized approach for testing C code. Prior to this update, some unit tests relied on the test helper mechanism, lacking a dedicated unit testing framework. It's more natural to perform these unit tests using the new unit test framework. Let's migrate the unit tests for strcmp-offset functionality from the legacy approach using the test-tool command `test-tool strcmp-offset` in helper/test-strcmp-offset.c to the new unit testing framework (t/unit-tests/test-lib.h). The migration involves refactoring the tests to utilize the testing macros provided by the framework (TEST() and check_*()). Helped-by: Patrick Steinhardt <ps@pks.im> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Mentored-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com> Co-authored-by: Achu Luma <ach.lumap@gmail.com> Signed-off-by: Achu Luma <ach.lumap@gmail.com> Signed-off-by: Ghanshyam Thakkar <shyamthakkar001@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ghanshyam Thakkar committed May 20, 2024 at 02:14 UTC 4d00d948ff5796ba5a8a253eb73da5ea36a72657
6 files changed +36 -48
Makefile
+1 -1
@@ -839,7 +839,6 @@ TEST_BUILTINS_OBJS += test-sha1.o
839 TEST_BUILTINS_OBJS += test-sha256.o
840 TEST_BUILTINS_OBJS += test-sigchain.o
841 TEST_BUILTINS_OBJS += test-simple-ipc.o
842 -TEST_BUILTINS_OBJS += test-strcmp-offset.o
842 TEST_BUILTINS_OBJS += test-string-list.o
843 TEST_BUILTINS_OBJS += test-submodule-config.o
844 TEST_BUILTINS_OBJS += test-submodule-nested-repo-config.o
@@ -1338,6 +1337,7 @@ UNIT_TEST_PROGRAMS += t-mem-pool
1337 UNIT_TEST_PROGRAMS += t-strbuf
1338 UNIT_TEST_PROGRAMS += t-ctype
1339 UNIT_TEST_PROGRAMS += t-prio-queue
1340 +UNIT_TEST_PROGRAMS += t-strcmp-offset
1341 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
1342 UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS))
1343 UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
t/helper/test-strcmp-offset.c deleted
-23
@@ -1,23 +0,0 @@
1 -#include "test-tool.h"
2 -#include "read-cache-ll.h"
3 -
4 -int cmd__strcmp_offset(int argc UNUSED, const char **argv)
5 -{
6 - int result;
7 - size_t offset;
8 -
9 - if (!argv[1] || !argv[2])
10 - die("usage: %s <string1> <string2>", argv[0]);
11 -
12 - result = strcmp_offset(argv[1], argv[2], &offset);
13 -
14 - /*
15 - * Because different CRTs behave differently, only rely on signs
16 - * of the result values.
17 - */
18 - result = (result < 0 ? -1 :
19 - result > 0 ? 1 :
20 - 0);
21 - printf("%d %"PRIuMAX"\n", result, (uintmax_t)offset);
22 - return 0;
23 -}
t/helper/test-tool.c
-1
@@ -78,7 +78,6 @@ static struct test_cmd cmds[] = {
78 { "sha256", cmd__sha256 },
79 { "sigchain", cmd__sigchain },
80 { "simple-ipc", cmd__simple_ipc },
81 - { "strcmp-offset", cmd__strcmp_offset },
81 { "string-list", cmd__string_list },
82 { "submodule", cmd__submodule },
83 { "submodule-config", cmd__submodule_config },
t/helper/test-tool.h
-1
@@ -71,7 +71,6 @@ int cmd__oid_array(int argc, const char **argv);
71 int cmd__sha256(int argc, const char **argv);
72 int cmd__sigchain(int argc, const char **argv);
73 int cmd__simple_ipc(int argc, const char **argv);
74 -int cmd__strcmp_offset(int argc, const char **argv);
74 int cmd__string_list(int argc, const char **argv);
75 int cmd__submodule(int argc, const char **argv);
76 int cmd__submodule_config(int argc, const char **argv);
t/t0065-strcmp-offset.sh deleted
-22
@@ -1,22 +0,0 @@
1 -#!/bin/sh
2 -
3 -test_description='Test strcmp_offset functionality'
4 -
5 -TEST_PASSES_SANITIZE_LEAK=true
6 -. ./test-lib.sh
7 -
8 -while read s1 s2 expect
9 -do
10 - test_expect_success "strcmp_offset($s1, $s2)" '
11 - echo "$expect" >expect &&
12 - test-tool strcmp-offset "$s1" "$s2" >actual &&
13 - test_cmp expect actual
14 - '
15 -done <<-EOF
16 -abc abc 0 3
17 -abc def -1 0
18 -abc abz -1 2
19 -abc abcdef -1 3
20 -EOF
21 -
22 -test_done
t/unit-tests/t-strcmp-offset.c new
+35
@@ -0,0 +1,35 @@
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, const char **argv)
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 +}