(root)/
glibc-2.38/
wcsmbs/
tst-wcrtomb.c
       1  /* Copyright (C) 2000-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library 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 GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <locale.h>
      19  #include <stdio.h>
      20  #include <stdlib.h>
      21  #include <string.h>
      22  #include <wchar.h>
      23  
      24  
      25  static int check_ascii (const char *locname);
      26  
      27  
      28  static int
      29  do_test (void)
      30  {
      31    int result = 0;
      32  
      33    /* Check mapping of ASCII range for some character sets which have
      34       ASCII as a subset.  For those the wide char generated must have
      35       the same value.  */
      36    setlocale (LC_ALL, "C");
      37    result |= check_ascii (setlocale (LC_ALL, NULL));
      38  
      39    setlocale (LC_ALL, "de_DE.UTF-8");
      40    result |= check_ascii (setlocale (LC_ALL, NULL));
      41  
      42    setlocale (LC_ALL, "ja_JP.EUC-JP");
      43    result |= check_ascii (setlocale (LC_ALL, NULL));
      44  
      45    return result;
      46  }
      47  
      48  
      49  static int
      50  check_ascii (const char *locname)
      51  {
      52    wchar_t wc;
      53    int res = 0;
      54  
      55    printf ("Testing locale \"%s\":\n", locname);
      56  
      57    for (wc = 0; wc <= 127; ++wc)
      58      {
      59        char buf[2 * MB_CUR_MAX];
      60        mbstate_t s;
      61        size_t n;
      62  
      63        memset (buf, '\xff', sizeof (buf));
      64        memset (&s, '\0', sizeof (s));
      65  
      66        n = wcrtomb (buf, wc, &s);
      67        if (n == (size_t) -1)
      68  	{
      69  	  printf ("%s: '\\x%x': encoding error\n", locname, (int) wc);
      70  	  ++res;
      71  	}
      72        else if (n == 0)
      73  	{
      74  	  printf ("%s: '\\x%x': 0 returned\n", locname, (int) wc);
      75  	  ++res;
      76  	}
      77        else if (n != 1)
      78  	{
      79  	  printf ("%s: '\\x%x': not 1 returned\n", locname, (int) wc);
      80  	  ++res;
      81  	}
      82        else if (wc != (wchar_t) buf[0])
      83  	{
      84  	  printf ("%s: L'\\x%x': buf[0] != '\\x%x'\n", locname, (int) wc,
      85  		  (int) wc);
      86  	  ++res;
      87  	}
      88      }
      89  
      90    printf (res == 1 ? "%d error\n" : "%d errors\n", res);
      91  
      92    return res != 0;
      93  }
      94  
      95  #include <support/test-driver.c>