Back to index

glibc  2.9
tst-btowc.c
Go to the documentation of this file.
00001 /* Copyright (C) 2000 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <locale.h>
00021 #include <stdio.h>
00022 #include <wchar.h>
00023 
00024 
00025 /* Currently selected locale.  */
00026 static const char *current_locale;
00027 
00028 
00029 /* Test which should succeed.  */
00030 static int
00031 ok_test (int c, wint_t expwc)
00032 {
00033   wint_t wc = btowc (c);
00034 
00035   if (wc != expwc)
00036     {
00037       printf ("%s: btowc('%c') failed, returned L'\\x%x' instead of L'\\x%x'\n",
00038              current_locale, c, wc, expwc);
00039       return 1;
00040     }
00041 
00042   return 0;
00043 }
00044 
00045 /* Test which should fail.  */
00046 static int
00047 fail_test (int c)
00048 {
00049   wint_t wc = btowc (c);
00050 
00051   if (wc != WEOF)
00052     {
00053       printf ("%s: btowc('%c') succeded, returned L'\\x%x' instead of WEOF\n",
00054              current_locale, c, wc);
00055       return 1;
00056     }
00057 
00058   return 0;
00059 }
00060 
00061 
00062 /* Test EOF handling.  */
00063 static int
00064 eof_test (void)
00065 {
00066   wint_t wc = btowc (EOF);
00067   if (wc != WEOF)
00068     {
00069       printf ("%s: btowc(EOF) returned L'\\x%x', not WEOF\n",
00070              current_locale, wc);
00071     }
00072 
00073   return 0;
00074 }
00075 
00076 
00077 /* Test the btowc() function for a few locales with known character sets.  */
00078 int
00079 main (void)
00080 {
00081   int result = 0;
00082 
00083   current_locale = setlocale (LC_ALL, "en_US.ANSI_X3.4-1968");
00084   if (current_locale == NULL)
00085     {
00086       puts ("cannot set locale \"en_US.ANSI_X3.4-1968\"");
00087       result = 1;
00088     }
00089   else
00090     {
00091       int c;
00092 
00093       for (c = 0; c < 128; ++c)
00094        result |= ok_test (c, c);
00095 
00096       for (c = 128; c < 256; ++c)
00097        result |= fail_test (c);
00098 
00099       result |= eof_test ();
00100     }
00101 
00102   current_locale = setlocale (LC_ALL, "de_DE.ISO-8859-1");
00103   if (current_locale == NULL)
00104     {
00105       puts ("cannot set locale \"de_DE.ISO-8859-1\"");
00106       result = 1;
00107     }
00108   else
00109     {
00110       int c;
00111 
00112       for (c = 0; c < 256; ++c)
00113        result |= ok_test (c, c);
00114 
00115       result |= eof_test ();
00116     }
00117 
00118   current_locale = setlocale (LC_ALL, "de_DE.UTF-8");
00119   if (current_locale == NULL)
00120     {
00121       puts ("cannot set locale \"de_DE.UTF-8\"");
00122       result = 1;
00123     }
00124   else
00125     {
00126       int c;
00127 
00128       for (c = 0; c < 128; ++c)
00129        result |= ok_test (c, c);
00130 
00131       for (c = 128; c < 256; ++c)
00132        result |= fail_test (c);
00133 
00134       result |= eof_test ();
00135     }
00136 
00137   current_locale = setlocale (LC_ALL, "hr_HR.ISO-8859-2");
00138   if (current_locale == NULL)
00139     {
00140       puts ("cannot set locale \"hr_HR.ISO-8859-2\"");
00141       result = 1;
00142     }
00143   else
00144     {
00145       static const wint_t upper_half[] =
00146       {
00147        0x0104, 0x02D8, 0x0141, 0x00A4, 0x013D, 0x015A, 0x00A7, 0x00A8,
00148        0x0160, 0x015E, 0x0164, 0x0179, 0x00AD, 0x017D, 0x017B, 0x00B0,
00149        0x0105, 0x02DB, 0x0142, 0x00B4, 0x013E, 0x015B, 0x02C7, 0x00B8,
00150        0x0161, 0x015F, 0x0165, 0x017A, 0x02DD, 0x017E, 0x017C, 0x0154,
00151        0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7, 0x010C,
00152        0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E, 0x0110,
00153        0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7, 0x0158,
00154        0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF, 0x0155,
00155        0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7, 0x010D,
00156        0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F, 0x0111,
00157        0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7, 0x0159,
00158        0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9
00159       };
00160       int c;
00161 
00162       for (c = 0; c < 161; ++c)
00163        result |= ok_test (c, c);
00164 
00165       for (c = 161; c < 256; ++c)
00166        result |= ok_test (c, upper_half[c - 161]);
00167 
00168       result |= eof_test ();
00169     }
00170 
00171   if (result == 0)
00172     puts ("all OK");
00173 
00174   return result;
00175 }