github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/tests/ctype.c (about)

     1  // Tests for ctype.h.
     2  
     3  #include "tests.h"
     4  #include <ctype.h>
     5  #include <stdio.h>
     6  
     7  #define T is_true
     8  #define F is_false
     9  
    10  #define CTYPE(functionName, A, B, C, D, E, F, G, H) \
    11      diag(#functionName);                            \
    12      A(functionName('a'));                           \
    13      B(functionName('B'));                           \
    14      C(functionName('0'));                           \
    15      D(functionName('*'));                           \
    16      E(functionName('\0'));                          \
    17      F(functionName(' '));                           \
    18      G(functionName('\n'));                          \
    19      H(functionName('z'));
    20  
    21  // This is just helpful for alignment.
    22  #define _CTYPE CTYPE
    23  
    24  char* strnul = "this string has a \0 NUL";
    25  char arrnul[] = "this string has a \0 NUL";
    26  
    27  static int clrcomp(char* s, int len)
    28  {
    29      static char* digs = "0123456789abcdef";
    30      int i;
    31      for (i = 0; i < len; i++) {
    32          if (strchr(digs, tolower(s[i]))) {
    33              printf("%d\n", (int)(*strchr(digs, tolower(s[i]))));
    34              return (strchr(digs, tolower(s[i])) - digs);
    35          }
    36      }
    37      return -1;
    38  }
    39  
    40  int main()
    41  {
    42      plan(114);
    43  
    44      //              . Lower alpha (a)
    45      //              |  . Upper alpha (B)
    46      //              |  |  . Digit (0)
    47      //              |  |  |  . Punctuation (*)
    48      //              |  |  |  |  . NULL
    49      //              |  |  |  |  |  . Space
    50      //              |  |  |  |  |  |  . New line
    51      //              |  |  |  |  |  |  |  . Non-hex digit (z)
    52      //              v  v  v  v  v  v  v  v
    53      _CTYPE(isalnum, T, T, T, F, F, F, F, T);
    54      _CTYPE(isalpha, T, T, F, F, F, F, F, T);
    55      _CTYPE(iscntrl, F, F, F, F, T, F, T, F);
    56      _CTYPE(isdigit, F, F, T, F, F, F, F, F);
    57      _CTYPE(isgraph, T, T, T, T, F, F, F, T);
    58      _CTYPE(islower, T, F, F, F, F, F, F, T);
    59      _CTYPE(isprint, T, T, T, T, F, T, F, T);
    60      _CTYPE(ispunct, F, F, F, T, F, F, F, F);
    61      _CTYPE(isspace, F, F, F, F, F, T, T, F);
    62      _CTYPE(isupper, F, T, F, F, F, F, F, F);
    63      _CTYPE(isblank, F, F, F, F, F, T, F, F);
    64      CTYPE(isxdigit, T, T, T, F, F, F, F, F);
    65  
    66      diag("tolower");
    67      is_eq(tolower('a'), 'a');
    68      is_eq(tolower('B'), 'b');
    69      is_eq(tolower('0'), '0');
    70      is_eq(tolower('*'), '*');
    71      is_eq(tolower('\0'), '\0');
    72      is_eq(tolower(' '), ' ');
    73      is_eq(tolower('\n'), '\n');
    74      is_eq(tolower('z'), 'z');
    75  
    76      diag("toupper");
    77      is_eq(toupper('a'), 'A');
    78      is_eq(toupper('B'), 'B');
    79      is_eq(toupper('0'), '0');
    80      is_eq(toupper('*'), '*');
    81      is_eq(toupper('\0'), '\0');
    82      is_eq(toupper(' '), ' ');
    83      is_eq(toupper('\n'), '\n');
    84      is_eq(toupper('z'), 'Z');
    85  
    86      diag("clrcomp");
    87      {
    88          char* word1 = "12";
    89          is_eq(clrcomp(word1, 2), 1);
    90          char* word2 = "qwe12";
    91          is_eq(clrcomp(word2, 5), 14);
    92      }
    93  
    94      done_testing();
    95  }