Back to index

glibc  2.9
tst-mbswcs1.c
Go to the documentation of this file.
00001 /* Test restarting behaviour of mbrtowc.
00002    Copyright (C) 2000, 2005 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, wcexp) \
00027   n = expr;                                                      \
00028   printf (#expr " -> %Zd", n);                                          \
00029   printf (", wc = %lu", (unsigned long int) wc);                 \
00030   if (n != (size_t) nexp || wc != wcexp)                         \
00031     {                                                            \
00032       printf (", expected %Zd and %lu", nexp, (unsigned long int) wcexp); \
00033       result = 1;                                                \
00034     }                                                            \
00035   putc ('\n', stdout)
00036 
00037 int
00038 main (void)
00039 {
00040   const unsigned char buf[6] = { 0x25,  0xe2, 0x82, 0xac,  0xce, 0xbb };
00041   mbstate_t state;
00042   wchar_t wc = 42;
00043   size_t n;
00044   int result = 0;
00045   const char *used_locale;
00046 
00047   setlocale (LC_CTYPE, "de_DE.UTF-8");
00048   /* Double check.  */
00049   used_locale = setlocale (LC_CTYPE, NULL);
00050   printf ("used locale: \"%s\"\n", used_locale);
00051   result = strcmp (used_locale, "de_DE.UTF-8");
00052 
00053   memset (&state, '\0', sizeof (state));
00054 
00055   show (mbrtowc (&wc, (const char *) buf + 0, 1, &state), 1, 37);
00056   show (mbrtowc (&wc, (const char *) buf + 1, 1, &state), -2, 37);
00057   show (mbrtowc (&wc, (const char *) buf + 2, 3, &state), 2, 8364);
00058   show (mbrtowc (&wc, (const char *) buf + 4, 1, &state), -2, 8364);
00059   show (mbrtowc (&wc, (const char *) buf + 5, 1, &state), 1, 955);
00060   show (mbrtowc (&wc, (const char *) buf + 5, 1, &state), -1, 955);
00061 
00062   return result;
00063 }