(root)/
gettext-0.22.4/
gettext-tools/
gnulib-tests/
test-c32rtomb.c
       1  /* Test of conversion of wide character to multibyte character.
       2     Copyright (C) 2008-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  /* Written by Bruno Haible <bruno@clisp.org>, 2008.  */
      18  
      19  #include <config.h>
      20  
      21  #include <uchar.h>
      22  
      23  #include "signature.h"
      24  SIGNATURE_CHECK (c32rtomb, size_t, (char *, char32_t, mbstate_t *));
      25  
      26  #include <locale.h>
      27  #include <stdlib.h>
      28  #include <string.h>
      29  
      30  #include "macros.h"
      31  
      32  /* Check the multibyte character s[0..n-1].  */
      33  static void
      34  check_character (const char *s, size_t n)
      35  {
      36    mbstate_t state;
      37    char32_t wc;
      38    char buf[64];
      39    int iret;
      40    size_t ret;
      41  
      42    memset (&state, '\0', sizeof (mbstate_t));
      43    wc = (char32_t) 0xBADFACE;
      44    iret = mbrtoc32 (&wc, s, n, &state);
      45    ASSERT (iret == n);
      46  
      47    ret = c32rtomb (buf, wc, NULL);
      48    ASSERT (ret == n);
      49    ASSERT (memcmp (buf, s, n) == 0);
      50  
      51    /* Test special calling convention, passing a NULL pointer.  */
      52    ret = c32rtomb (NULL, wc, NULL);
      53    ASSERT (ret == 1);
      54  }
      55  
      56  int
      57  main (int argc, char *argv[])
      58  {
      59    char buf[64];
      60    size_t ret;
      61  
      62    /* configure should already have checked that the locale is supported.  */
      63    if (setlocale (LC_ALL, "") == NULL)
      64      return 1;
      65  
      66    /* Test NUL character.  */
      67    {
      68      buf[0] = 'x';
      69      ret = c32rtomb (buf, 0, NULL);
      70      ASSERT (ret == 1);
      71      ASSERT (buf[0] == '\0');
      72    }
      73  
      74    /* Test single bytes.  */
      75    {
      76      int c;
      77  
      78      for (c = 0; c < 0x100; c++)
      79        switch (c)
      80          {
      81          case '\t': case '\v': case '\f':
      82          case ' ': case '!': case '"': case '#': case '%':
      83          case '&': case '\'': case '(': case ')': case '*':
      84          case '+': case ',': case '-': case '.': case '/':
      85          case '0': case '1': case '2': case '3': case '4':
      86          case '5': case '6': case '7': case '8': case '9':
      87          case ':': case ';': case '<': case '=': case '>':
      88          case '?':
      89          case 'A': case 'B': case 'C': case 'D': case 'E':
      90          case 'F': case 'G': case 'H': case 'I': case 'J':
      91          case 'K': case 'L': case 'M': case 'N': case 'O':
      92          case 'P': case 'Q': case 'R': case 'S': case 'T':
      93          case 'U': case 'V': case 'W': case 'X': case 'Y':
      94          case 'Z':
      95          case '[': case '\\': case ']': case '^': case '_':
      96          case 'a': case 'b': case 'c': case 'd': case 'e':
      97          case 'f': case 'g': case 'h': case 'i': case 'j':
      98          case 'k': case 'l': case 'm': case 'n': case 'o':
      99          case 'p': case 'q': case 'r': case 's': case 't':
     100          case 'u': case 'v': case 'w': case 'x': case 'y':
     101          case 'z': case '{': case '|': case '}': case '~':
     102            /* c is in the ISO C "basic character set".  */
     103            ret = c32rtomb (buf, btoc32 (c), NULL);
     104            ASSERT (ret == 1);
     105            ASSERT (buf[0] == (char) c);
     106            break;
     107          }
     108    }
     109  
     110    /* Test special calling convention, passing a NULL pointer.  */
     111    {
     112      ret = c32rtomb (NULL, '\0', NULL);
     113      ASSERT (ret == 1);
     114      ret = c32rtomb (NULL, btoc32 ('x'), NULL);
     115      ASSERT (ret == 1);
     116    }
     117  
     118    if (argc > 1)
     119      switch (argv[1][0])
     120        {
     121        case '1':
     122          /* C locale; tested above.  */
     123          return 0;
     124  
     125        case '2':
     126          /* Locale encoding is ISO-8859-1 or ISO-8859-15.  */
     127          {
     128            const char input[] = "B\374\337er"; /* "Büßer" */
     129  
     130            check_character (input + 1, 1);
     131            check_character (input + 2, 1);
     132          }
     133          return 0;
     134  
     135        case '3':
     136          /* Locale encoding is UTF-8.  */
     137          {
     138            const char input[] = "s\303\274\303\237\360\237\230\213!"; /* "süß😋!" */
     139  
     140            check_character (input + 1, 2);
     141            check_character (input + 3, 2);
     142            check_character (input + 5, 4);
     143          }
     144          return 0;
     145  
     146        case '4':
     147          /* Locale encoding is EUC-JP.  */
     148          {
     149            const char input[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
     150  
     151            check_character (input + 1, 2);
     152            check_character (input + 3, 2);
     153            check_character (input + 5, 2);
     154          }
     155          return 0;
     156  
     157        case '5':
     158          /* Locale encoding is GB18030.  */
     159          #if (defined __GLIBC__ && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 13 && __GLIBC_MINOR__ <= 15) || (GL_CHAR32_T_IS_UNICODE && (defined __NetBSD__ || defined __sun))
     160          fputs ("Skipping test: The GB18030 converter in this system's iconv is broken.\n", stderr);
     161          return 77;
     162          #endif
     163          {
     164            const char input[] = "s\250\271\201\060\211\070\224\071\375\067!"; /* "süß😋!" */
     165  
     166            check_character (input + 1, 2);
     167            check_character (input + 3, 4);
     168            check_character (input + 7, 4);
     169          }
     170          return 0;
     171        }
     172  
     173    return 1;
     174  }