unit-tests: rewrite t/helper/test-ctype.c as a unit test

In the recent codebase update (8bf6fbd00d (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. This commit migrates the unit tests for C character classification functions (isdigit(), isspace(), etc) from the legacy approach using the test-tool command `test-tool ctype` in t/helper/test-ctype.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_*()). Mentored-by: Christian Couder <chriscool@tuxfamily.org> Helped-by: René Scharfe <l.s.r@web.de> Helped-by: Phillip Wood <phillip.wood123@gmail.com> Helped-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Achu Luma <ach.lumap@gmail.com> Acked-by: Phillip Wood <phillip.wood123@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Achu Luma committed Jan 12, 2024 at 11:27 UTC e875d4511c34eed952f1123401a1727b4bd583fa
6 files changed +81 -77
Makefile
+1 -1
@@ -792,7 +792,6 @@ TEST_BUILTINS_OBJS += test-chmtime.o
792 TEST_BUILTINS_OBJS += test-config.o
793 TEST_BUILTINS_OBJS += test-crontab.o
794 TEST_BUILTINS_OBJS += test-csprng.o
795 -TEST_BUILTINS_OBJS += test-ctype.o
795 TEST_BUILTINS_OBJS += test-date.o
796 TEST_BUILTINS_OBJS += test-delta.o
797 TEST_BUILTINS_OBJS += test-dir-iterator.o
@@ -1341,6 +1340,7 @@ THIRD_PARTY_SOURCES += sha1dc/%
1340
1341 UNIT_TEST_PROGRAMS += t-basic
1342 UNIT_TEST_PROGRAMS += t-strbuf
1343 +UNIT_TEST_PROGRAMS += t-ctype
1344 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
1345 UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS))
1346 UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
t/helper/test-ctype.c deleted
-70
@@ -1,70 +0,0 @@
1 -#include "test-tool.h"
2 -
3 -static int rc;
4 -
5 -static void report_error(const char *class, int ch)
6 -{
7 - printf("%s classifies char %d (0x%02x) wrongly\n", class, ch, ch);
8 - rc = 1;
9 -}
10 -
11 -static int is_in(const char *s, int ch)
12 -{
13 - /*
14 - * We can't find NUL using strchr. Accept it as the first
15 - * character in the spec -- there are no empty classes.
16 - */
17 - if (ch == '\0')
18 - return ch == *s;
19 - if (*s == '\0')
20 - s++;
21 - return !!strchr(s, ch);
22 -}
23 -
24 -#define TEST_CLASS(t,s) { \
25 - int i; \
26 - for (i = 0; i < 256; i++) { \
27 - if (is_in(s, i) != t(i)) \
28 - report_error(#t, i); \
29 - } \
30 - if (t(EOF)) \
31 - report_error(#t, EOF); \
32 -}
33 -
34 -#define DIGIT "0123456789"
35 -#define LOWER "abcdefghijklmnopqrstuvwxyz"
36 -#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
37 -#define PUNCT "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
38 -#define ASCII \
39 - "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
40 - "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
41 - "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" \
42 - "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" \
43 - "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" \
44 - "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f" \
45 - "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" \
46 - "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
47 -#define CNTRL \
48 - "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
49 - "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
50 - "\x7f"
51 -
52 -int cmd__ctype(int argc UNUSED, const char **argv UNUSED)
53 -{
54 - TEST_CLASS(isdigit, DIGIT);
55 - TEST_CLASS(isspace, " \n\r\t");
56 - TEST_CLASS(isalpha, LOWER UPPER);
57 - TEST_CLASS(isalnum, LOWER UPPER DIGIT);
58 - TEST_CLASS(is_glob_special, "*?[\\");
59 - TEST_CLASS(is_regex_special, "$()*+.?[\\^{|");
60 - TEST_CLASS(is_pathspec_magic, "!\"#%&',-/:;<=>@_`~");
61 - TEST_CLASS(isascii, ASCII);
62 - TEST_CLASS(islower, LOWER);
63 - TEST_CLASS(isupper, UPPER);
64 - TEST_CLASS(iscntrl, CNTRL);
65 - TEST_CLASS(ispunct, PUNCT);
66 - TEST_CLASS(isxdigit, DIGIT "abcdefABCDEF");
67 - TEST_CLASS(isprint, LOWER UPPER DIGIT PUNCT " ");
68 -
69 - return rc;
70 -}
t/helper/test-tool.c
-1
@@ -19,7 +19,6 @@ static struct test_cmd cmds[] = {
19 { "config", cmd__config },
20 { "crontab", cmd__crontab },
21 { "csprng", cmd__csprng },
22 - { "ctype", cmd__ctype },
22 { "date", cmd__date },
23 { "delta", cmd__delta },
24 { "dir-iterator", cmd__dir_iterator },
t/helper/test-tool.h
-1
@@ -12,7 +12,6 @@ int cmd__chmtime(int argc, const char **argv);
12 int cmd__config(int argc, const char **argv);
13 int cmd__crontab(int argc, const char **argv);
14 int cmd__csprng(int argc, const char **argv);
15 -int cmd__ctype(int argc, const char **argv);
15 int cmd__date(int argc, const char **argv);
16 int cmd__delta(int argc, const char **argv);
17 int cmd__dir_iterator(int argc, const char **argv);
t/t0070-fundamental.sh
-4
@@ -9,10 +9,6 @@ Verify wrappers and compatibility functions.
9 TEST_PASSES_SANITIZE_LEAK=true
10 . ./test-lib.sh
11
12 -test_expect_success 'character classes (isspace, isalpha etc.)' '
13 - test-tool ctype
14 -'
15 -
12 test_expect_success 'mktemp to nonexistent directory prints filename' '
13 test_must_fail test-tool mktemp doesnotexist/testXXXXXX 2>err &&
14 grep "doesnotexist/test" err
t/unit-tests/t-ctype.c new
+80
@@ -0,0 +1,80 @@
1 +#include "test-lib.h"
2 +
3 +static int is_in(const char *s, int ch)
4 +{
5 + /*
6 + * We can't find NUL using strchr. Accept it as the first
7 + * character in the spec -- there are no empty classes.
8 + */
9 + if (ch == '\0')
10 + return ch == *s;
11 + if (*s == '\0')
12 + s++;
13 + return !!strchr(s, ch);
14 +}
15 +
16 +/* Macro to test a character type */
17 +#define TEST_CTYPE_FUNC(func, string) \
18 +static void test_ctype_##func(void) { \
19 + for (int i = 0; i < 256; i++) { \
20 + if (!check_int(func(i), ==, is_in(string, i))) \
21 + test_msg(" i: 0x%02x", i); \
22 + } \
23 + if (!check(!func(EOF))) \
24 + test_msg(" i: 0x%02x (EOF)", EOF); \
25 +}
26 +
27 +#define TEST_CHAR_CLASS(class) TEST(test_ctype_##class(), #class " works")
28 +
29 +#define DIGIT "0123456789"
30 +#define LOWER "abcdefghijklmnopqrstuvwxyz"
31 +#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
32 +#define PUNCT "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
33 +#define ASCII \
34 + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
35 + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
36 + "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" \
37 + "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" \
38 + "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" \
39 + "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f" \
40 + "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" \
41 + "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
42 +#define CNTRL \
43 + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
44 + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
45 + "\x7f"
46 +
47 +TEST_CTYPE_FUNC(isdigit, DIGIT)
48 +TEST_CTYPE_FUNC(isspace, " \n\r\t")
49 +TEST_CTYPE_FUNC(isalpha, LOWER UPPER)
50 +TEST_CTYPE_FUNC(isalnum, LOWER UPPER DIGIT)
51 +TEST_CTYPE_FUNC(is_glob_special, "*?[\\")
52 +TEST_CTYPE_FUNC(is_regex_special, "$()*+.?[\\^{|")
53 +TEST_CTYPE_FUNC(is_pathspec_magic, "!\"#%&',-/:;<=>@_`~")
54 +TEST_CTYPE_FUNC(isascii, ASCII)
55 +TEST_CTYPE_FUNC(islower, LOWER)
56 +TEST_CTYPE_FUNC(isupper, UPPER)
57 +TEST_CTYPE_FUNC(iscntrl, CNTRL)
58 +TEST_CTYPE_FUNC(ispunct, PUNCT)
59 +TEST_CTYPE_FUNC(isxdigit, DIGIT "abcdefABCDEF")
60 +TEST_CTYPE_FUNC(isprint, LOWER UPPER DIGIT PUNCT " ")
61 +
62 +int cmd_main(int argc, const char **argv) {
63 + /* Run all character type tests */
64 + TEST_CHAR_CLASS(isspace);
65 + TEST_CHAR_CLASS(isdigit);
66 + TEST_CHAR_CLASS(isalpha);
67 + TEST_CHAR_CLASS(isalnum);
68 + TEST_CHAR_CLASS(is_glob_special);
69 + TEST_CHAR_CLASS(is_regex_special);
70 + TEST_CHAR_CLASS(is_pathspec_magic);
71 + TEST_CHAR_CLASS(isascii);
72 + TEST_CHAR_CLASS(islower);
73 + TEST_CHAR_CLASS(isupper);
74 + TEST_CHAR_CLASS(iscntrl);
75 + TEST_CHAR_CLASS(ispunct);
76 + TEST_CHAR_CLASS(isxdigit);
77 + TEST_CHAR_CLASS(isprint);
78 +
79 + return test_done();
80 +}