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