Add ctype test
authorRené Scharfe <rene.scharfe@lsrfire.ath.cx>
Sat, 17 Jan 2009 15:50:13 +0000 (16:50 +0100)
committerJunio C Hamano <gitster@pobox.com>
Sun, 18 Jan 2009 02:30:23 +0000 (18:30 -0800)
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>
Makefile
t/t0070-fundamental.sh [new file with mode: 0755]
test-ctype.c [new file with mode: 0644]
index dee97c1b010503acb170be08b721e69e45f5d569..20ba65d1e60f1e6fc0373839d3a082ad1888d377 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1356,7 +1356,15 @@ endif
 
 ### Testing rules
 
-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
+TEST_PROGRAMS += test-chmtime$X
+TEST_PROGRAMS += test-ctype$X
+TEST_PROGRAMS += test-date$X
+TEST_PROGRAMS += test-delta$X
+TEST_PROGRAMS += test-genrandom$X
+TEST_PROGRAMS += test-match-trees$X
+TEST_PROGRAMS += test-parse-options$X
+TEST_PROGRAMS += test-path-utils$X
+TEST_PROGRAMS += test-sha1$X
 
 all:: $(TEST_PROGRAMS)
 
@@ -1369,6 +1377,8 @@ export NO_SVN_TESTS
 test: all
        $(MAKE) -C t/ all
 
+test-ctype$X: ctype.o
+
 test-date$X: date.o ctype.o
 
 test-delta$X: diff-delta.o patch-delta.o
diff --git a/t/t0070-fundamental.sh b/t/t0070-fundamental.sh
new file mode 100755 (executable)
index 0000000..680d7d6
--- /dev/null
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+test_description='check that the most basic functions work
+
+
+Verify wrappers and compatibility functions.
+'
+
+. ./test-lib.sh
+
+test_expect_success 'character classes (isspace, isalpha etc.)' '
+       test-ctype
+'
+
+test_done
diff --git a/test-ctype.c b/test-ctype.c
new file mode 100644 (file)
index 0000000..723eff4
--- /dev/null
@@ -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;
+}