Skip to content

Commit 572bf49

Browse files
committed
Merge branch 'rs/t-ctype-simplify'
Code simplification to one unit-test program. * rs/t-ctype-simplify: t-ctype: avoid duplicating class names t-ctype: align output of i t-ctype: simplify EOF check t-ctype: allow NUL anywhere in the specification string
2 parents ef7e896 + 6cf06e9 commit 572bf49

File tree

1 file changed

+27
-54
lines changed

1 file changed

+27
-54
lines changed

t/unit-tests/t-ctype.c

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

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); \
3+
#define TEST_CHAR_CLASS(class, string) do { \
4+
size_t len = ARRAY_SIZE(string) - 1 + \
5+
BUILD_ASSERT_OR_ZERO(ARRAY_SIZE(string) > 0) + \
6+
BUILD_ASSERT_OR_ZERO(sizeof(string[0]) == sizeof(char)); \
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)); \
2214
} \
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")
15+
test__run_end(!skip, TEST_LOCATION(), #class " works"); \
16+
} while (0)
2817

2918
#define DIGIT "0123456789"
3019
#define LOWER "abcdefghijklmnopqrstuvwxyz"
@@ -44,37 +33,21 @@ static void test_ctype_##func(void) { \
4433
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
4534
"\x7f"
4635

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-
6236
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);
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 " ");
7851

7952
return test_done();
8053
}

0 commit comments

Comments
 (0)