(root)/
coreutils-9.4/
gnulib-tests/
test-unicodeio.c
       1  /* Tests for Unicode character output.
       2  
       3     Copyright (C) 2020-2023 Free Software Foundation, Inc.
       4  
       5     This program is free software: you can redistribute it and/or modify
       6     it under the terms of the GNU General Public License as published by
       7     the Free Software Foundation, either version 3 of the License, or
       8     (at your option) any later version.
       9  
      10     This program is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13     GNU General Public License for more details.
      14  
      15     You should have received a copy of the GNU General Public License
      16     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      17  
      18  /* Written by Bruno Haible, 2020.  */
      19  
      20  #include <config.h>
      21  
      22  /* Specification.  */
      23  #include "unicodeio.h"
      24  
      25  #include <locale.h>
      26  #include <string.h>
      27  
      28  #include "localcharset.h"
      29  #include "macros.h"
      30  
      31  #define TEST_CODE 0x2022
      32  #define TEST_CODE_AS_UTF8 "\xe2\x80\xa2"
      33  #define TEST_CODE_AS_GB18030 "\x81\x36\xa6\x31"
      34  
      35  static char result[64];
      36  
      37  static long
      38  success_callback (const char *buf, size_t buflen, void *callback_arg)
      39  {
      40    memcpy (result, buf, buflen);
      41    result[buflen] = '\0';
      42    return 42;
      43  }
      44  
      45  static long
      46  failure_callback (unsigned int code, const char *msg, void *callback_arg)
      47  {
      48    ASSERT (code == TEST_CODE);
      49    strcpy (result, ".");
      50    return 55;
      51  }
      52  
      53  int
      54  main (int argc, char *argv[])
      55  {
      56    /* configure should already have checked that the locale is supported.  */
      57    if (setlocale (LC_ALL, "") == NULL)
      58      return 1;
      59  
      60    switch (unicode_to_mb (TEST_CODE, success_callback, failure_callback, NULL))
      61      {
      62      case 42:
      63        if (argc > 1)
      64          switch (argv[1][0])
      65            {
      66            case '1':
      67              /* On some platforms, the "C" locale has UTF-8 encoding.
      68                 And on native Windows, the "C" locale may have an 8-bit encoding
      69                 such as CP1252, that contains the U+2022 character.  */
      70              {
      71                const char *charset = locale_charset ();
      72                if (strcmp (charset, "CP874") == 0
      73                    || strcmp (charset, "CP1250") == 0
      74                    || strcmp (charset, "CP1251") == 0
      75                    || strcmp (charset, "CP1252") == 0
      76                    || strcmp (charset, "CP1253") == 0
      77                    || strcmp (charset, "CP1254") == 0
      78                    || strcmp (charset, "CP1255") == 0
      79                    || strcmp (charset, "CP1256") == 0
      80                    || strcmp (charset, "CP1257") == 0
      81                    || strcmp (charset, "CP1258") == 0)
      82                  ASSERT (strcmp (result, "\x95") == 0);
      83                else
      84                  ASSERT (strcmp (result, TEST_CODE_AS_UTF8) == 0);
      85              }
      86              break;
      87            case '2':
      88              ASSERT (strcmp (result, TEST_CODE_AS_UTF8) == 0);
      89              break;
      90            case '3':
      91              ASSERT (strcmp (result, TEST_CODE_AS_GB18030) == 0);
      92              break;
      93            }
      94        break;
      95      case 55:
      96        ASSERT (strcmp (result, ".") == 0);
      97        break;
      98      default:
      99        ASSERT (0);
     100      }
     101  
     102    return 0;
     103  }