test-ctype.con commit Add ctype test (b4285c7)
   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
  24#define DIGIT "0123456789"
  25#define LOWER "abcdefghijklmnopqrstuvwxyz"
  26#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  27
  28static 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
  40static 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
  57int 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}