Back to index

glibc  2.9
tst-mbrtowc2.c
Go to the documentation of this file.
00001 /* Derived from the test case in http://sourceware.org/bugzilla/show_bug.cgi?id=714 */
00002 #include <locale.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <wchar.h>
00006 
00007 
00008 static struct
00009 {
00010   const char *chp;
00011   size_t nchp;
00012   const char *loc;
00013 } tests[] =
00014 {
00015   { (const char[]) { 0x8F, 0xA2, 0xAF }, 3, "ja_JP.EUC-JP" },
00016   { (const char[]) { 0xD1, 0xA5 }, 2, "ja_JP.EUC-JP" },
00017   { (const char[]) { 0x8E, 0xA5 }, 2, "ja_JP.EUC-JP" },
00018   { (const char[]) { 0x8E, 0xA2, 0xA1, 0xA1 }, 4, "zh_TW.EUC-TW" },
00019   { (const char[]) { 0xA1, 0xA1 }, 2, "zh_TW.EUC-TW" },
00020   { (const char[]) { 0xE3, 0x80, 0x80 }, 3, "de_DE.UTF-8" },
00021   { (const char[]) { 0xC3, 0xA4 }, 2, "de_DE.UTF-8" }
00022 };
00023 #define ntests (sizeof (tests) / sizeof (tests[0]))
00024 
00025 
00026 static int t (const char *ch, size_t nch, const char *loc);
00027 
00028 static int
00029 do_test (void)
00030 {
00031   int r = 0;
00032   for (int i = 0; i < ntests; ++i)
00033     r |= t (tests[i].chp, tests[i].nchp, tests[i].loc);
00034   return r;
00035 }
00036 
00037 static int
00038 t (const char *ch, size_t nch, const char *loc)
00039 {
00040   int i;
00041   wchar_t wch;
00042   wchar_t wch2;
00043   mbstate_t mbs;
00044   int n = 0;
00045 
00046   setlocale (LC_ALL, loc);
00047 
00048   memset (&mbs, '\0', sizeof (mbstate_t));
00049   for (i = 0; i < nch; i++)
00050     {
00051       n = mbrtowc (&wch, ch + i, 1, &mbs);
00052       if (n >= 0)
00053        break;
00054     }
00055   printf ("n = %d, count = %d, wch = %08lX\n", n, i, (unsigned long int) wch);
00056 
00057   memset (&mbs, '\0', sizeof (mbstate_t));
00058   n = mbrtowc (&wch2, ch, nch, &mbs);
00059   printf ("n = %d, wch = %08lX\n", n, (unsigned long int) wch2);
00060 
00061   int ret = n != nch || i + 1 != nch || n != nch || wch != wch2;
00062   puts (ret ? "FAIL\n" : "OK\n");
00063   return ret;
00064 }
00065 
00066 #define TEST_FUNCTION do_test ()
00067 #include "../test-skeleton.c"