test-ctype.con commit Merge branch 'jn/maint-format-patch-doc' into maint (4bbfd8e)
   1#include "cache.h"
   2
   3
   4static int test_isdigit(int c)
   5{
   6        return isdigit(c);
   7}
   8
   9static int test_isspace(int c)
  10{
  11        return isspace(c);
  12}
  13
  14static int test_isalpha(int c)
  15{
  16        return isalpha(c);
  17}
  18
  19static int test_isalnum(int c)
  20{
  21        return isalnum(c);
  22}
  23
  24static int test_is_glob_special(int c)
  25{
  26        return is_glob_special(c);
  27}
  28
  29static int test_is_regex_special(int c)
  30{
  31        return is_regex_special(c);
  32}
  33
  34#define DIGIT "0123456789"
  35#define LOWER "abcdefghijklmnopqrstuvwxyz"
  36#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  37
  38static 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
  52static 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
  69int main(int argc, char **argv)
  70{
  71        const struct ctype_class *test;
  72        int rc = 0;
  73
  74        for (test = classes; test->name; test++)
  75                rc |= test_class(test);
  76
  77        return rc;
  78}