1 /* Derived from the test case in
2 https://sourceware.org/bugzilla/show_bug.cgi?id=714 */
3 #include <locale.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <wchar.h>
7
8
9 static struct
10 {
11 const char *chp;
12 size_t nchp;
13 const char *loc;
14 } tests[] =
15 {
16 { (const char[]) { 0x8F, 0xA2, 0xAF }, 3, "ja_JP.EUC-JP" },
17 { (const char[]) { 0xD1, 0xA5 }, 2, "ja_JP.EUC-JP" },
18 { (const char[]) { 0x8E, 0xA5 }, 2, "ja_JP.EUC-JP" },
19 { (const char[]) { 0x8E, 0xA2, 0xA1, 0xA1 }, 4, "zh_TW.EUC-TW" },
20 { (const char[]) { 0xA1, 0xA1 }, 2, "zh_TW.EUC-TW" },
21 { (const char[]) { 0xE3, 0x80, 0x80 }, 3, "de_DE.UTF-8" },
22 { (const char[]) { 0xC3, 0xA4 }, 2, "de_DE.UTF-8" }
23 };
24 #define ntests (sizeof (tests) / sizeof (tests[0]))
25
26
27 static int t (const char *ch, size_t nch, const char *loc);
28
29 static int
do_test(void)30 do_test (void)
31 {
32 int r = 0;
33 for (int i = 0; i < ntests; ++i)
34 r |= t (tests[i].chp, tests[i].nchp, tests[i].loc);
35 return r;
36 }
37
38 static int
t(const char * ch,size_t nch,const char * loc)39 t (const char *ch, size_t nch, const char *loc)
40 {
41 int i;
42 wchar_t wch;
43 wchar_t wch2;
44 mbstate_t mbs;
45 int n = 0;
46
47 setlocale (LC_ALL, loc);
48
49 memset (&mbs, '\0', sizeof (mbstate_t));
50 for (i = 0; i < nch; i++)
51 {
52 n = mbrtowc (&wch, ch + i, 1, &mbs);
53 if (n >= 0)
54 break;
55 }
56 printf ("n = %d, count = %d, wch = %08lX\n", n, i, (unsigned long int) wch);
57
58 memset (&mbs, '\0', sizeof (mbstate_t));
59 n = mbrtowc (&wch2, ch, nch, &mbs);
60 printf ("n = %d, wch = %08lX\n", n, (unsigned long int) wch2);
61
62 int ret = n != nch || i + 1 != nch || n != nch || wch != wch2;
63 puts (ret ? "FAIL\n" : "OK\n");
64 return ret;
65 }
66
67 #include <support/test-driver.c>
68