Back to index

glibc  2.9
tst-mbsrtowcs.c
Go to the documentation of this file.
00001 /* Test NUL handling of mbsrtowcs.
00002    Copyright (C) 2004, 2005 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
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 
00025 int
00026 main (void)
00027 {
00028   const unsigned char buf[] = { 'a', 'b', '\0', 'c', 'd', '\0', 'e' };
00029   wchar_t out[sizeof (buf)];
00030   memset (out, '\xff', sizeof (out));
00031 
00032   mbstate_t state;
00033   memset (&state, '\0', sizeof (state));
00034 
00035   const char *in = (const char *) buf;
00036   size_t n = mbsrtowcs (out, &in, sizeof (out) / sizeof (wchar_t), &state);
00037 
00038   int result = 0;
00039   if (n != 2)
00040     {
00041       printf ("n = %zu, expected 2\n", n);
00042       result = 1;
00043     }
00044   if (in != NULL)
00045     {
00046       printf ("in = %p, expected NULL\n", in);
00047       result = 1;
00048     }
00049   if (out[0] != L'a')
00050     {
00051       printf ("out[0] = L'%lc', expected L'a'\n", (wint_t) out[0]);
00052       result = 1;
00053     }
00054   if (out[1] != L'b')
00055     {
00056       printf ("out[1] = L'%lc', expected L'b'\n", (wint_t) out[1]);
00057       result = 1;
00058     }
00059   if (out[2] != L'\0')
00060     {
00061       printf ("out[2] = L'%lc', expected L'\\0'\n", (wint_t) out[2]);
00062       result = 1;
00063     }
00064   return result;
00065 }