diff options
author | René Scharfe <rene.scharfe@lsrfire.ath.cx> | 2009-01-17 16:50:13 +0100 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2009-01-18 03:30:23 +0100 |
commit | b4285c71bcc43c4cc9939b8d170aaf8ddb25f09f (patch) | |
tree | 6c63596853c57aca828ebbf0a71ed4a5047bb6d5 /test-ctype.c | |
parent | grep: don't call regexec() for fixed strings (diff) | |
download | git-b4285c71bcc43c4cc9939b8d170aaf8ddb25f09f.tar.xz git-b4285c71bcc43c4cc9939b8d170aaf8ddb25f09f.zip |
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 <rene.scharfe@lsrfire.ath.cx>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'test-ctype.c')
-rw-r--r-- | test-ctype.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/test-ctype.c b/test-ctype.c new file mode 100644 index 0000000000..723eff4e96 --- /dev/null +++ b/test-ctype.c @@ -0,0 +1,66 @@ +#include "cache.h" + + +static int test_isdigit(int c) +{ + return isdigit(c); +} + +static int test_isspace(int c) +{ + return isspace(c); +} + +static int test_isalpha(int c) +{ + return isalpha(c); +} + +static int test_isalnum(int c) +{ + return isalnum(c); +} + +#define DIGIT "0123456789" +#define LOWER "abcdefghijklmnopqrstuvwxyz" +#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + +static const struct ctype_class { + const char *name; + int (*test_fn)(int); + const char *members; +} classes[] = { + { "isdigit", test_isdigit, DIGIT }, + { "isspace", test_isspace, " \n\r\t" }, + { "isalpha", test_isalpha, LOWER UPPER }, + { "isalnum", test_isalnum, LOWER UPPER DIGIT }, + { NULL } +}; + +static int test_class(const struct ctype_class *test) +{ + int i, rc = 0; + + for (i = 0; i < 256; i++) { + int expected = i ? !!strchr(test->members, i) : 0; + int actual = test->test_fn(i); + + if (actual != expected) { + rc = 1; + printf("%s classifies char %d (0x%02x) wrongly\n", + test->name, i, i); + } + } + return rc; +} + +int main(int argc, char **argv) +{ + const struct ctype_class *test; + int rc = 0; + + for (test = classes; test->name; test++) + rc |= test_class(test); + + return rc; +} |