Back to index

glibc  2.9
wcsmbs-tst1.c
Go to the documentation of this file.
00001 /* Based on a test program by Won Kyu Park <wkpark@chem.skku.ac.kr>.  */
00002 
00003 #include <wchar.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <wctype.h>
00008 #include <locale.h>
00009 
00010 int
00011 main (void)
00012 {
00013   int test = 0;
00014   int idx = 0;
00015   char buf[100], *pchar;
00016   wchar_t tmp[10];
00017   wchar_t tmp1[] = { L'W', L'o', L'r', L'l', L'd', L'\0' };
00018   char str[] = "Hello";
00019   int result = 0;
00020 
00021   pchar = setlocale (LC_ALL, "");
00022   printf ("locale : %s\n",pchar);
00023   printf ("MB_CUR_MAX %Zd\n", MB_CUR_MAX);
00024 
00025   puts ("---- test 1 ------");
00026   test = mbstowcs (tmp, str, (strlen (str) + 1) * sizeof (char));
00027   printf ("size of string by mbstowcs %d\n", test);
00028   if (test != strlen (str))
00029     result = 1;
00030   idx += wctomb (&buf[0], tmp[0]);
00031   idx += wctomb (&buf[idx], tmp[1]);
00032   buf[idx] = 0;
00033   printf ("orig string %s\n", str);
00034   printf ("string by wctomb %s\n", buf);
00035   printf ("string by %%C %C", (wint_t) tmp[0]);
00036   if (tmp[0] != L'H')
00037     result = 1;
00038   printf ("%C\n", (wint_t) tmp[1]);
00039   if (tmp[1] != L'e')
00040     result = 1;
00041   printf ("string by %%S %S\n", tmp);
00042   if (wcscmp (tmp, L"Hello") != 0)
00043     result = 1;
00044   puts ("---- test 2 ------");
00045   printf ("wchar string %S\n", tmp1);
00046   printf ("wchar %C\n", (wint_t) tmp1[0]);
00047   test = wcstombs (buf, tmp1, (wcslen (tmp1) + 1) * sizeof (wchar_t));
00048   printf ("size of string by wcstombs %d\n", test);
00049   if (test != wcslen (tmp1))
00050     result = 1;
00051   test = wcslen (tmp1);
00052   printf ("size of string by wcslen %d\n", test);
00053   printf ("char %s\n", buf);
00054   if (strcmp (buf, "World") != 0)
00055     result = 1;
00056   puts ("------------------");
00057 
00058   return result;
00059 }