Merge branch 'rs/test-ctype'

* rs/test-ctype:
  test-ctype: add test for is_pathspec_magic
  test-ctype: macrofy
This commit is contained in:
Junio C Hamano 2011-10-12 12:34:11 -07:00
commit fbca6911de

View File

@ -1,78 +1,42 @@
#include "cache.h" #include "cache.h"
static int rc;
static int test_isdigit(int c) static void report_error(const char *class, int ch)
{ {
return isdigit(c); printf("%s classifies char %d (0x%02x) wrongly\n", class, ch, ch);
rc = 1;
} }
static int test_isspace(int c) static int is_in(const char *s, int ch)
{ {
return isspace(c); /* We can't find NUL using strchr. It's classless anyway. */
if (ch == '\0')
return 0;
return !!strchr(s, ch);
} }
static int test_isalpha(int c) #define TEST_CLASS(t,s) { \
{ int i; \
return isalpha(c); for (i = 0; i < 256; i++) { \
} if (is_in(s, i) != t(i)) \
report_error(#t, i); \
static int test_isalnum(int c) } \
{
return isalnum(c);
}
static int test_is_glob_special(int c)
{
return is_glob_special(c);
}
static int test_is_regex_special(int c)
{
return is_regex_special(c);
} }
#define DIGIT "0123456789" #define DIGIT "0123456789"
#define LOWER "abcdefghijklmnopqrstuvwxyz" #define LOWER "abcdefghijklmnopqrstuvwxyz"
#define UPPER "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 },
{ "is_glob_special", test_is_glob_special, "*?[\\" },
{ "is_regex_special", test_is_regex_special, "$()*+.?[\\^{|" },
{ 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) int main(int argc, char **argv)
{ {
const struct ctype_class *test; TEST_CLASS(isdigit, DIGIT);
int rc = 0; TEST_CLASS(isspace, " \n\r\t");
TEST_CLASS(isalpha, LOWER UPPER);
for (test = classes; test->name; test++) TEST_CLASS(isalnum, LOWER UPPER DIGIT);
rc |= test_class(test); TEST_CLASS(is_glob_special, "*?[\\");
TEST_CLASS(is_regex_special, "$()*+.?[\\^{|");
TEST_CLASS(is_pathspec_magic, "!\"#%&',-/:;<=>@_`~");
return rc; return rc;
} }