Back to index

glibc  2.9
tst-mbswcs3.c
Go to the documentation of this file.
00001 /* Test restarting behaviour of wcsrtombs.
00002    Copyright (C) 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Bruno Haible <haible@ilog.fr>.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <wchar.h>
00024 #include <locale.h>
00025 
00026 #define show(expr, nexp, srcexp, bufexp) \
00027   {                                                            \
00028     size_t res = expr;                                                \
00029     printf (#expr " -> %Zd", res);                             \
00030     dst += res;                                                       \
00031     printf (", src = srcbuf+%td, dst = buf+%td",               \
00032            src - srcbuf, dst - (char *) buf);                         \
00033     if (res != nexp || src != (srcexp) || dst != (char *) (bufexp))   \
00034       {                                                               \
00035        printf (", expected %Zd and srcbuf+%td and buf+%td", nexp,     \
00036               (srcexp) - srcbuf, (bufexp) - (unsigned char *) buf);   \
00037        result = 1;                                             \
00038       }                                                               \
00039     putc ('\n', stdout);                                       \
00040   }
00041 
00042 int
00043 main (void)
00044 {
00045   unsigned char buf[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
00046   const unsigned char bufcheck[6] = { 0x25, 0xe2, 0x82, 0xac, 0xce, 0xbb };
00047   const wchar_t srcbuf[4] = { 0x25, 0x20ac, 0x03bb, 0 };
00048   mbstate_t state;
00049   const wchar_t *src;
00050   char *dst;
00051   int result = 0;
00052   const char *used_locale;
00053 
00054   setlocale (LC_CTYPE, "de_DE.UTF-8");
00055   /* Double check.  */
00056   used_locale = setlocale (LC_CTYPE, NULL);
00057   printf ("used locale: \"%s\"\n", used_locale);
00058   result = strcmp (used_locale, "de_DE.UTF-8");
00059 
00060   memset (&state, '\0', sizeof (state));
00061 
00062   src = srcbuf;
00063   dst = (char *) buf;
00064   show (wcsrtombs (dst, &src, 1, &state), 1, srcbuf + 1, buf + 1);
00065   show (wcsrtombs (dst, &src, 1, &state), 0, srcbuf + 1, buf + 1);
00066   show (wcsrtombs (dst, &src, 4, &state), 3, srcbuf + 2, buf + 4);
00067   show (wcsrtombs (dst, &src, 2, &state), 2, srcbuf + 3, buf + 6);
00068 
00069   if (memcmp (buf, bufcheck, 6))
00070     {
00071       puts ("wrong results");
00072       result = 1;
00073     }
00074 
00075   return result;
00076 }