Skip to content

Commit 6cf06e9

Browse files
rscharfegitster
authored andcommitted
t-ctype: avoid duplicating class names
TEST_CTYPE_FUNC defines a function for testing a character classifier, TEST_CHAR_CLASS calls it, causing the class name to be mentioned twice. Avoid the need to define a class-specific function by letting TEST_CHAR_CLASS do all the work. This is done by using the internal functions test__run_begin() and test__run_end(), but they do exist to be used in test macros after all. Alternatively we could unroll the loop to provide a very long expression that tests all 256 characters and EOF and hand that to TEST, but that seems awkward and hard to read. No change of behavior or output intended. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7a8d6c0 commit 6cf06e9

File tree

1 file changed

+24
-40
lines changed

1 file changed

+24
-40
lines changed

t/unit-tests/t-ctype.c

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#include "test-lib.h"
22

3-
/* Macro to test a character type */
4-
#define TEST_CTYPE_FUNC(func, string) \
5-
static void test_ctype_##func(void) { \
3+
#define TEST_CHAR_CLASS(class, string) do { \
64
size_t len = ARRAY_SIZE(string) - 1 + \
75
BUILD_ASSERT_OR_ZERO(ARRAY_SIZE(string) > 0) + \
86
BUILD_ASSERT_OR_ZERO(sizeof(string[0]) == sizeof(char)); \
9-
for (int i = 0; i < 256; i++) { \
10-
if (!check_int(func(i), ==, !!memchr(string, i, len))) \
11-
test_msg(" i: 0x%02x", i); \
7+
int skip = test__run_begin(); \
8+
if (!skip) { \
9+
for (int i = 0; i < 256; i++) { \
10+
if (!check_int(class(i), ==, !!memchr(string, i, len)))\
11+
test_msg(" i: 0x%02x", i); \
12+
} \
13+
check(!class(EOF)); \
1214
} \
13-
check(!func(EOF)); \
14-
}
15-
16-
#define TEST_CHAR_CLASS(class) TEST(test_ctype_##class(), #class " works")
15+
test__run_end(!skip, TEST_LOCATION(), #class " works"); \
16+
} while (0)
1717

1818
#define DIGIT "0123456789"
1919
#define LOWER "abcdefghijklmnopqrstuvwxyz"
@@ -33,37 +33,21 @@ static void test_ctype_##func(void) { \
3333
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
3434
"\x7f"
3535

36-
TEST_CTYPE_FUNC(isdigit, DIGIT)
37-
TEST_CTYPE_FUNC(isspace, " \n\r\t")
38-
TEST_CTYPE_FUNC(isalpha, LOWER UPPER)
39-
TEST_CTYPE_FUNC(isalnum, LOWER UPPER DIGIT)
40-
TEST_CTYPE_FUNC(is_glob_special, "*?[\\")
41-
TEST_CTYPE_FUNC(is_regex_special, "$()*+.?[\\^{|")
42-
TEST_CTYPE_FUNC(is_pathspec_magic, "!\"#%&',-/:;<=>@_`~")
43-
TEST_CTYPE_FUNC(isascii, ASCII)
44-
TEST_CTYPE_FUNC(islower, LOWER)
45-
TEST_CTYPE_FUNC(isupper, UPPER)
46-
TEST_CTYPE_FUNC(iscntrl, CNTRL)
47-
TEST_CTYPE_FUNC(ispunct, PUNCT)
48-
TEST_CTYPE_FUNC(isxdigit, DIGIT "abcdefABCDEF")
49-
TEST_CTYPE_FUNC(isprint, LOWER UPPER DIGIT PUNCT " ")
50-
5136
int cmd_main(int argc, const char **argv) {
52-
/* Run all character type tests */
53-
TEST_CHAR_CLASS(isspace);
54-
TEST_CHAR_CLASS(isdigit);
55-
TEST_CHAR_CLASS(isalpha);
56-
TEST_CHAR_CLASS(isalnum);
57-
TEST_CHAR_CLASS(is_glob_special);
58-
TEST_CHAR_CLASS(is_regex_special);
59-
TEST_CHAR_CLASS(is_pathspec_magic);
60-
TEST_CHAR_CLASS(isascii);
61-
TEST_CHAR_CLASS(islower);
62-
TEST_CHAR_CLASS(isupper);
63-
TEST_CHAR_CLASS(iscntrl);
64-
TEST_CHAR_CLASS(ispunct);
65-
TEST_CHAR_CLASS(isxdigit);
66-
TEST_CHAR_CLASS(isprint);
37+
TEST_CHAR_CLASS(isspace, " \n\r\t");
38+
TEST_CHAR_CLASS(isdigit, DIGIT);
39+
TEST_CHAR_CLASS(isalpha, LOWER UPPER);
40+
TEST_CHAR_CLASS(isalnum, LOWER UPPER DIGIT);
41+
TEST_CHAR_CLASS(is_glob_special, "*?[\\");
42+
TEST_CHAR_CLASS(is_regex_special, "$()*+.?[\\^{|");
43+
TEST_CHAR_CLASS(is_pathspec_magic, "!\"#%&',-/:;<=>@_`~");
44+
TEST_CHAR_CLASS(isascii, ASCII);
45+
TEST_CHAR_CLASS(islower, LOWER);
46+
TEST_CHAR_CLASS(isupper, UPPER);
47+
TEST_CHAR_CLASS(iscntrl, CNTRL);
48+
TEST_CHAR_CLASS(ispunct, PUNCT);
49+
TEST_CHAR_CLASS(isxdigit, DIGIT "abcdefABCDEF");
50+
TEST_CHAR_CLASS(isprint, LOWER UPPER DIGIT PUNCT " ");
6751

6852
return test_done();
6953
}

0 commit comments

Comments
 (0)