Skip to content

Commit fbca691

Browse files
committed
Merge branch 'rs/test-ctype'
* rs/test-ctype: test-ctype: add test for is_pathspec_magic test-ctype: macrofy
2 parents 9f24152 + 32ec231 commit fbca691

File tree

1 file changed

+22
-58
lines changed

1 file changed

+22
-58
lines changed

test-ctype.c

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,42 @@
11
#include "cache.h"
22

3+
static int rc;
34

4-
static int test_isdigit(int c)
5+
static void report_error(const char *class, int ch)
56
{
6-
return isdigit(c);
7+
printf("%s classifies char %d (0x%02x) wrongly\n", class, ch, ch);
8+
rc = 1;
79
}
810

9-
static int test_isspace(int c)
11+
static int is_in(const char *s, int ch)
1012
{
11-
return isspace(c);
13+
/* We can't find NUL using strchr. It's classless anyway. */
14+
if (ch == '\0')
15+
return 0;
16+
return !!strchr(s, ch);
1217
}
1318

14-
static int test_isalpha(int c)
15-
{
16-
return isalpha(c);
17-
}
18-
19-
static int test_isalnum(int c)
20-
{
21-
return isalnum(c);
22-
}
23-
24-
static int test_is_glob_special(int c)
25-
{
26-
return is_glob_special(c);
27-
}
28-
29-
static int test_is_regex_special(int c)
30-
{
31-
return is_regex_special(c);
19+
#define TEST_CLASS(t,s) { \
20+
int i; \
21+
for (i = 0; i < 256; i++) { \
22+
if (is_in(s, i) != t(i)) \
23+
report_error(#t, i); \
24+
} \
3225
}
3326

3427
#define DIGIT "0123456789"
3528
#define LOWER "abcdefghijklmnopqrstuvwxyz"
3629
#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
3730

38-
static const struct ctype_class {
39-
const char *name;
40-
int (*test_fn)(int);
41-
const char *members;
42-
} classes[] = {
43-
{ "isdigit", test_isdigit, DIGIT },
44-
{ "isspace", test_isspace, " \n\r\t" },
45-
{ "isalpha", test_isalpha, LOWER UPPER },
46-
{ "isalnum", test_isalnum, LOWER UPPER DIGIT },
47-
{ "is_glob_special", test_is_glob_special, "*?[\\" },
48-
{ "is_regex_special", test_is_regex_special, "$()*+.?[\\^{|" },
49-
{ NULL }
50-
};
51-
52-
static int test_class(const struct ctype_class *test)
53-
{
54-
int i, rc = 0;
55-
56-
for (i = 0; i < 256; i++) {
57-
int expected = i ? !!strchr(test->members, i) : 0;
58-
int actual = test->test_fn(i);
59-
60-
if (actual != expected) {
61-
rc = 1;
62-
printf("%s classifies char %d (0x%02x) wrongly\n",
63-
test->name, i, i);
64-
}
65-
}
66-
return rc;
67-
}
68-
6931
int main(int argc, char **argv)
7032
{
71-
const struct ctype_class *test;
72-
int rc = 0;
73-
74-
for (test = classes; test->name; test++)
75-
rc |= test_class(test);
33+
TEST_CLASS(isdigit, DIGIT);
34+
TEST_CLASS(isspace, " \n\r\t");
35+
TEST_CLASS(isalpha, LOWER UPPER);
36+
TEST_CLASS(isalnum, LOWER UPPER DIGIT);
37+
TEST_CLASS(is_glob_special, "*?[\\");
38+
TEST_CLASS(is_regex_special, "$()*+.?[\\^{|");
39+
TEST_CLASS(is_pathspec_magic, "!\"#%&',-/:;<=>@_`~");
7640

7741
return rc;
7842
}

0 commit comments

Comments
 (0)