(root)/
glibc-2.38/
wcsmbs/
tst-btowc.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 <wchar.h>
      21  
      22  
      23  /* Currently selected locale.  */
      24  static const char *current_locale;
      25  
      26  
      27  /* Test which should succeed.  */
      28  static int
      29  ok_test (int c, wint_t expwc)
      30  {
      31    wint_t wc = btowc (c);
      32  
      33    if (wc != expwc)
      34      {
      35        printf ("%s: btowc('%c') failed, returned L'\\x%x' instead of L'\\x%x'\n",
      36  	      current_locale, c, wc, expwc);
      37        return 1;
      38      }
      39  
      40    return 0;
      41  }
      42  
      43  /* Test which should fail.  */
      44  static int
      45  fail_test (int c)
      46  {
      47    wint_t wc = btowc (c);
      48  
      49    if (wc != WEOF)
      50      {
      51        printf ("%s: btowc('%c') succeeded, returned L'\\x%x' instead of WEOF\n",
      52  	      current_locale, c, wc);
      53        return 1;
      54      }
      55  
      56    return 0;
      57  }
      58  
      59  
      60  /* Test EOF handling.  */
      61  static int
      62  eof_test (void)
      63  {
      64    wint_t wc = btowc (EOF);
      65    if (wc != WEOF)
      66      {
      67        printf ("%s: btowc(EOF) returned L'\\x%x', not WEOF\n",
      68  	      current_locale, wc);
      69      }
      70  
      71    return 0;
      72  }
      73  
      74  
      75  /* Test the btowc() function for a few locales with known character sets.  */
      76  static int
      77  do_test (void)
      78  {
      79    int result = 0;
      80  
      81    current_locale = setlocale (LC_ALL, "en_US.ANSI_X3.4-1968");
      82    if (current_locale == NULL)
      83      {
      84        puts ("cannot set locale \"en_US.ANSI_X3.4-1968\"");
      85        result = 1;
      86      }
      87    else
      88      {
      89        int c;
      90  
      91        for (c = 0; c < 128; ++c)
      92  	result |= ok_test (c, c);
      93  
      94        for (c = 128; c < 256; ++c)
      95  	result |= fail_test (c);
      96  
      97        result |= eof_test ();
      98      }
      99  
     100    current_locale = setlocale (LC_ALL, "de_DE.ISO-8859-1");
     101    if (current_locale == NULL)
     102      {
     103        puts ("cannot set locale \"de_DE.ISO-8859-1\"");
     104        result = 1;
     105      }
     106    else
     107      {
     108        int c;
     109  
     110        for (c = 0; c < 256; ++c)
     111  	result |= ok_test (c, c);
     112  
     113        result |= eof_test ();
     114      }
     115  
     116    current_locale = setlocale (LC_ALL, "de_DE.UTF-8");
     117    if (current_locale == NULL)
     118      {
     119        puts ("cannot set locale \"de_DE.UTF-8\"");
     120        result = 1;
     121      }
     122    else
     123      {
     124        int c;
     125  
     126        for (c = 0; c < 128; ++c)
     127  	result |= ok_test (c, c);
     128  
     129        for (c = 128; c < 256; ++c)
     130  	result |= fail_test (c);
     131  
     132        result |= eof_test ();
     133      }
     134  
     135    current_locale = setlocale (LC_ALL, "hr_HR.ISO-8859-2");
     136    if (current_locale == NULL)
     137      {
     138        puts ("cannot set locale \"hr_HR.ISO-8859-2\"");
     139        result = 1;
     140      }
     141    else
     142      {
     143        static const wint_t upper_half[] =
     144        {
     145  	0x0104, 0x02D8, 0x0141, 0x00A4, 0x013D, 0x015A, 0x00A7, 0x00A8,
     146  	0x0160, 0x015E, 0x0164, 0x0179, 0x00AD, 0x017D, 0x017B, 0x00B0,
     147  	0x0105, 0x02DB, 0x0142, 0x00B4, 0x013E, 0x015B, 0x02C7, 0x00B8,
     148  	0x0161, 0x015F, 0x0165, 0x017A, 0x02DD, 0x017E, 0x017C, 0x0154,
     149  	0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7, 0x010C,
     150  	0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E, 0x0110,
     151  	0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7, 0x0158,
     152  	0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF, 0x0155,
     153  	0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7, 0x010D,
     154  	0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F, 0x0111,
     155  	0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7, 0x0159,
     156  	0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9
     157        };
     158        int c;
     159  
     160        for (c = 0; c < 161; ++c)
     161  	result |= ok_test (c, c);
     162  
     163        for (c = 161; c < 256; ++c)
     164  	result |= ok_test (c, upper_half[c - 161]);
     165  
     166        result |= eof_test ();
     167      }
     168  
     169    if (result == 0)
     170      puts ("all OK");
     171  
     172    return result;
     173  }
     174  
     175  #include <support/test-driver.c>