Skip to content

Commit b4285c7

Browse files
René Scharfegitster
authored andcommitted
Add ctype test
Manipulating the character class table in ctype.c by hand is error prone. To ensure that typos are found quickly, add a test program and script. test-ctype checks the output of the character class macros isspace() et. al. by applying them on all possible char values and consulting a list of all characters in the particular class. It doesn't check tolower() and toupper(); this could be added later. The test script t0070-fundamental.sh is created because there is no good place for the ctype test, yet -- except for t0000-basic.sh perhaps, but it doesn't run well on Windows, yet. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c822255 commit b4285c7

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

Makefile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,15 @@ endif
13561356

13571357
### Testing rules
13581358

1359-
TEST_PROGRAMS = test-chmtime$X test-genrandom$X test-date$X test-delta$X test-sha1$X test-match-trees$X test-parse-options$X test-path-utils$X
1359+
TEST_PROGRAMS += test-chmtime$X
1360+
TEST_PROGRAMS += test-ctype$X
1361+
TEST_PROGRAMS += test-date$X
1362+
TEST_PROGRAMS += test-delta$X
1363+
TEST_PROGRAMS += test-genrandom$X
1364+
TEST_PROGRAMS += test-match-trees$X
1365+
TEST_PROGRAMS += test-parse-options$X
1366+
TEST_PROGRAMS += test-path-utils$X
1367+
TEST_PROGRAMS += test-sha1$X
13601368

13611369
all:: $(TEST_PROGRAMS)
13621370

@@ -1369,6 +1377,8 @@ export NO_SVN_TESTS
13691377
test: all
13701378
$(MAKE) -C t/ all
13711379

1380+
test-ctype$X: ctype.o
1381+
13721382
test-date$X: date.o ctype.o
13731383

13741384
test-delta$X: diff-delta.o patch-delta.o

t/t0070-fundamental.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
3+
test_description='check that the most basic functions work
4+
5+
6+
Verify wrappers and compatibility functions.
7+
'
8+
9+
. ./test-lib.sh
10+
11+
test_expect_success 'character classes (isspace, isalpha etc.)' '
12+
test-ctype
13+
'
14+
15+
test_done

test-ctype.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "cache.h"
2+
3+
4+
static int test_isdigit(int c)
5+
{
6+
return isdigit(c);
7+
}
8+
9+
static int test_isspace(int c)
10+
{
11+
return isspace(c);
12+
}
13+
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+
#define DIGIT "0123456789"
25+
#define LOWER "abcdefghijklmnopqrstuvwxyz"
26+
#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
27+
28+
static const struct ctype_class {
29+
const char *name;
30+
int (*test_fn)(int);
31+
const char *members;
32+
} classes[] = {
33+
{ "isdigit", test_isdigit, DIGIT },
34+
{ "isspace", test_isspace, " \n\r\t" },
35+
{ "isalpha", test_isalpha, LOWER UPPER },
36+
{ "isalnum", test_isalnum, LOWER UPPER DIGIT },
37+
{ NULL }
38+
};
39+
40+
static int test_class(const struct ctype_class *test)
41+
{
42+
int i, rc = 0;
43+
44+
for (i = 0; i < 256; i++) {
45+
int expected = i ? !!strchr(test->members, i) : 0;
46+
int actual = test->test_fn(i);
47+
48+
if (actual != expected) {
49+
rc = 1;
50+
printf("%s classifies char %d (0x%02x) wrongly\n",
51+
test->name, i, i);
52+
}
53+
}
54+
return rc;
55+
}
56+
57+
int main(int argc, char **argv)
58+
{
59+
const struct ctype_class *test;
60+
int rc = 0;
61+
62+
for (test = classes; test->name; test++)
63+
rc |= test_class(test);
64+
65+
return rc;
66+
}

0 commit comments

Comments
 (0)