(root)/
coreutils-9.4/
gnulib-tests/
test-c32isgraph.c
       1  /* Test of c32isgraph() function.
       2     Copyright (C) 2020-2023 Free Software Foundation, Inc.
       3  
       4     This program is free software: you can redistribute it and/or modify
       5     it under the terms of the GNU General Public License as published by
       6     the Free Software Foundation, either version 3 of the License, or
       7     (at your option) any later version.
       8  
       9     This program is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU General Public License for more details.
      13  
      14     You should have received a copy of the GNU General Public License
      15     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      16  
      17  #include <config.h>
      18  
      19  #include <uchar.h>
      20  
      21  #include "signature.h"
      22  SIGNATURE_CHECK (c32isgraph, int, (wint_t));
      23  
      24  #include <locale.h>
      25  #include <stdlib.h>
      26  #include <string.h>
      27  #include <wchar.h>
      28  
      29  #include "macros.h"
      30  
      31  /* Returns the value of c32isgraph for the multibyte character s[0..n-1].  */
      32  static int
      33  for_character (const char *s, size_t n)
      34  {
      35    mbstate_t state;
      36    char32_t wc;
      37    size_t ret;
      38  
      39    memset (&state, '\0', sizeof (mbstate_t));
      40    wc = (char32_t) 0xBADFACE;
      41    ret = mbrtoc32 (&wc, s, n, &state);
      42    ASSERT (ret == n);
      43  
      44    return c32isgraph (wc);
      45  }
      46  
      47  int
      48  main (int argc, char *argv[])
      49  {
      50    int is;
      51    char buf[4];
      52  
      53    /* configure should already have checked that the locale is supported.  */
      54    if (setlocale (LC_ALL, "") == NULL)
      55      return 1;
      56  
      57    /* Test WEOF.  */
      58    is = c32isgraph (WEOF);
      59    ASSERT (is == 0);
      60  
      61    /* Test single-byte characters.
      62       POSIX specifies in
      63         <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html>
      64       no explicit list of graphic characters.  */
      65    {
      66      int c;
      67  
      68      for (c = 0; c < 0x100; c++)
      69        switch (c)
      70          {
      71          case '\t': case '\v': case '\f':
      72          case ' ': case '!': case '"': case '#': case '%':
      73          case '&': case '\'': case '(': case ')': case '*':
      74          case '+': case ',': case '-': case '.': case '/':
      75          case '0': case '1': case '2': case '3': case '4':
      76          case '5': case '6': case '7': case '8': case '9':
      77          case ':': case ';': case '<': case '=': case '>':
      78          case '?':
      79          case 'A': case 'B': case 'C': case 'D': case 'E':
      80          case 'F': case 'G': case 'H': case 'I': case 'J':
      81          case 'K': case 'L': case 'M': case 'N': case 'O':
      82          case 'P': case 'Q': case 'R': case 'S': case 'T':
      83          case 'U': case 'V': case 'W': case 'X': case 'Y':
      84          case 'Z':
      85          case '[': case '\\': case ']': case '^': case '_':
      86          case 'a': case 'b': case 'c': case 'd': case 'e':
      87          case 'f': case 'g': case 'h': case 'i': case 'j':
      88          case 'k': case 'l': case 'm': case 'n': case 'o':
      89          case 'p': case 'q': case 'r': case 's': case 't':
      90          case 'u': case 'v': case 'w': case 'x': case 'y':
      91          case 'z': case '{': case '|': case '}': case '~':
      92            /* c is in the ISO C "basic character set".  */
      93            buf[0] = (unsigned char) c;
      94            is = for_character (buf, 1);
      95            switch (c)
      96              {
      97              case '\t': case '\v': case '\f':
      98              case ' ':
      99                ASSERT (is == 0);
     100                break;
     101              default:
     102                ASSERT (is != 0);
     103                break;
     104              }
     105            break;
     106          }
     107    }
     108  
     109    if (argc > 1)
     110      switch (argv[1][0])
     111        {
     112        case '0':
     113          /* C locale; tested above.  */
     114          return 0;
     115  
     116        case '1':
     117          /* Locale encoding is ISO-8859-1 or ISO-8859-15.  */
     118          {
     119            /* U+007F <control> */
     120            is = for_character ("\177", 1);
     121            ASSERT (is == 0);
     122          #if !((defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __DragonFly__ || defined __NetBSD__ || defined __sgi || defined __sun || defined __CYGWIN__ || (defined _WIN32 && !defined __CYGWIN__))
     123            /* U+00A0 NO-BREAK SPACE */
     124            is = for_character ("\240", 1);
     125            ASSERT (is != 0);
     126          #endif
     127          #if !(defined __FreeBSD__ || defined __DragonFly__)
     128            /* U+00B8 CEDILLA */
     129            is = for_character ("\270", 1);
     130            ASSERT (is != 0);
     131          #endif
     132          }
     133          return 0;
     134  
     135        case '2':
     136          /* Locale encoding is EUC-JP.  */
     137          {
     138            /* U+007F <control> */
     139            is = for_character ("\177", 1);
     140            ASSERT (is == 0);
     141          #if !((defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __DragonFly__)
     142            /* U+00B8 CEDILLA */
     143            is = for_character ("\217\242\261", 3);
     144            ASSERT (is != 0);
     145          #endif
     146            /* U+3000 IDEOGRAPHIC SPACE */
     147            is = for_character ("\241\241", 2);
     148            ASSERT (is == 0);
     149          }
     150          return 0;
     151  
     152        case '3':
     153          /* Locale encoding is UTF-8.  */
     154          {
     155            /* U+007F <control> */
     156            is = for_character ("\177", 1);
     157            ASSERT (is == 0);
     158          #if !((defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __DragonFly__ || defined __NetBSD__ || defined __sun || defined __CYGWIN__ || (defined _WIN32 && !defined __CYGWIN__))
     159            /* U+00A0 NO-BREAK SPACE */
     160            is = for_character ("\302\240", 2);
     161            ASSERT (is != 0);
     162          #endif
     163            /* U+00B8 CEDILLA */
     164            is = for_character ("\302\270", 2);
     165            ASSERT (is != 0);
     166            /* U+2002 EN SPACE */
     167            is = for_character ("\342\200\202", 3);
     168            ASSERT (is == 0);
     169          #if !(defined __GLIBC__ || defined MUSL_LIBC || (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __NetBSD__ || defined _AIX || defined __sun || defined __CYGWIN__)
     170            /* U+202E RIGHT-TO-LEFT OVERRIDE */
     171            is = for_character ("\342\200\256", 3);
     172            ASSERT (is == 0);
     173          #endif
     174            /* U+3000 IDEOGRAPHIC SPACE */
     175            is = for_character ("\343\200\200", 3);
     176            ASSERT (is == 0);
     177          #if !(defined __GLIBC__ || defined MUSL_LIBC || (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __NetBSD__ || defined _AIX || defined __sun || defined __CYGWIN__)
     178            /* U+FEFF ZERO WIDTH NO-BREAK SPACE */
     179            is = for_character ("\357\273\277", 3);
     180            ASSERT (is == 0);
     181          #endif
     182          #if !defined __sun
     183            /* U+20000 <CJK Ideograph> */
     184            is = for_character ("\360\240\200\200", 4);
     185            ASSERT (is != 0);
     186          #endif
     187          #if !(defined __GLIBC__ || defined MUSL_LIBC || (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __NetBSD__ || defined _AIX || defined __sun || defined __CYGWIN__ || (defined _WIN32 && !defined __CYGWIN__))
     188            /* U+E0001 LANGUAGE TAG */
     189            is = for_character ("\363\240\200\201", 4);
     190            ASSERT (is == 0);
     191          #endif
     192          }
     193          return 0;
     194  
     195        case '4':
     196          /* Locale encoding is GB18030.  */
     197          #if (defined __GLIBC__ && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 13 && __GLIBC_MINOR__ <= 15) || (GL_CHAR32_T_IS_UNICODE && (defined __NetBSD__ || defined __sun))
     198          fputs ("Skipping test: The GB18030 converter in this system's iconv is broken.\n", stderr);
     199          return 77;
     200          #endif
     201          {
     202            /* U+007F <control> */
     203            is = for_character ("\177", 1);
     204            ASSERT (is == 0);
     205          #if !((defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __DragonFly__ || defined __NetBSD__ || defined __sun)
     206            /* U+00A0 NO-BREAK SPACE */
     207            is = for_character ("\201\060\204\062", 4);
     208            ASSERT (is != 0);
     209          #endif
     210          #if !(defined __FreeBSD__ || defined __DragonFly__ || defined __sun)
     211            /* U+00B8 CEDILLA */
     212            is = for_character ("\201\060\206\060", 4);
     213            ASSERT (is != 0);
     214          #endif
     215            /* U+2002 EN SPACE */
     216            is = for_character ("\201\066\243\070", 4);
     217            ASSERT (is == 0);
     218          #if !(defined __GLIBC__ || (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__)
     219            /* U+202E RIGHT-TO-LEFT OVERRIDE */
     220            is = for_character ("\201\066\247\061", 4);
     221            ASSERT (is == 0);
     222          #endif
     223            /* U+3000 IDEOGRAPHIC SPACE */
     224            is = for_character ("\241\241", 2);
     225            ASSERT (is == 0);
     226          #if !(defined __GLIBC__ || (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__)
     227            /* U+FEFF ZERO WIDTH NO-BREAK SPACE */
     228            is = for_character ("\204\061\225\063", 4);
     229            ASSERT (is == 0);
     230          #endif
     231          #if !((defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __DragonFly__ || defined __NetBSD__ || defined __sun)
     232            /* U+20000 <CJK Ideograph> */
     233            is = for_character ("\225\062\202\066", 4);
     234            ASSERT (is != 0);
     235          #endif
     236          #if !(defined __GLIBC__ || (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__)
     237            /* U+E0001 LANGUAGE TAG */
     238            is = for_character ("\323\066\225\071", 4);
     239            ASSERT (is == 0);
     240          #endif
     241          }
     242          return 0;
     243  
     244        }
     245  
     246    return 1;
     247  }