Back to index

glibc  2.9
tst-wcrtomb.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 <stdlib.h>
00023 #include <string.h>
00024 #include <wchar.h>
00025 
00026 
00027 static int check_ascii (const char *locname);
00028 
00029 
00030 int
00031 main (void)
00032 {
00033   int result = 0;
00034 
00035   /* Check mapping of ASCII range for some character sets which have
00036      ASCII as a subset.  For those the wide char generated must have
00037      the same value.  */
00038   setlocale (LC_ALL, "C");
00039   result |= check_ascii (setlocale (LC_ALL, NULL));
00040 
00041   setlocale (LC_ALL, "de_DE.UTF-8");
00042   result |= check_ascii (setlocale (LC_ALL, NULL));
00043 
00044   setlocale (LC_ALL, "ja_JP.EUC-JP");
00045   result |= check_ascii (setlocale (LC_ALL, NULL));
00046 
00047   return result;
00048 }
00049 
00050 
00051 static int
00052 check_ascii (const char *locname)
00053 {
00054   wchar_t wc;
00055   int res = 0;
00056 
00057   printf ("Testing locale \"%s\":\n", locname);
00058 
00059   for (wc = 0; wc <= 127; ++wc)
00060     {
00061       char buf[2 * MB_CUR_MAX];
00062       mbstate_t s;
00063       size_t n;
00064 
00065       memset (buf, '\xff', sizeof (buf));
00066       memset (&s, '\0', sizeof (s));
00067 
00068       n = wcrtomb (buf, wc, &s);
00069       if (n == (size_t) -1)
00070        {
00071          printf ("%s: '\\x%x': encoding error\n", locname, (int) wc);
00072          ++res;
00073        }
00074       else if (n == 0)
00075        {
00076          printf ("%s: '\\x%x': 0 returned\n", locname, (int) wc);
00077          ++res;
00078        }
00079       else if (n != 1)
00080        {
00081          printf ("%s: '\\x%x': not 1 returned\n", locname, (int) wc);
00082          ++res;
00083        }
00084       else if (wc != (wchar_t) buf[0])
00085        {
00086          printf ("%s: L'\\x%x': buf[0] != '\\x%x'\n", locname, (int) wc,
00087                 (int) wc);
00088          ++res;
00089        }
00090     }
00091 
00092   printf (res == 1 ? "%d error\n" : "%d errors\n", res);
00093 
00094   return res != 0;
00095 }