1 /* Test of conversion of wide character to multibyte character.
2 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
23 #include "signature.h"
24 SIGNATURE_CHECK (wcrtomb, size_t, (char *, wchar_t, mbstate_t *));
31 #define ASSERT(expr) \
36 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
43 /* Check the multibyte character s[0..n-1]. */
45 check_character (const char *s, size_t n)
52 wc = (wchar_t) 0xBADFACE;
53 iret = mbtowc (&wc, s, n);
56 ret = wcrtomb (buf, wc, NULL);
58 ASSERT (memcmp (buf, s, n) == 0);
60 /* Test special calling convention, passing a NULL pointer. */
61 ret = wcrtomb (NULL, wc, NULL);
66 main (int argc, char *argv[])
71 /* configure should already have checked that the locale is supported. */
72 if (setlocale (LC_ALL, "") == NULL)
75 /* Test NUL character. */
78 ret = wcrtomb (buf, 0, NULL);
80 ASSERT (buf[0] == '\0');
83 /* Test single bytes. */
87 for (c = 0; c < 0x100; c++)
90 case '\t': case '\v': case '\f':
91 case ' ': case '!': case '"': case '#': case '%':
92 case '&': case '\'': case '(': case ')': case '*':
93 case '+': case ',': case '-': case '.': case '/':
94 case '0': case '1': case '2': case '3': case '4':
95 case '5': case '6': case '7': case '8': case '9':
96 case ':': case ';': case '<': case '=': case '>':
98 case 'A': case 'B': case 'C': case 'D': case 'E':
99 case 'F': case 'G': case 'H': case 'I': case 'J':
100 case 'K': case 'L': case 'M': case 'N': case 'O':
101 case 'P': case 'Q': case 'R': case 'S': case 'T':
102 case 'U': case 'V': case 'W': case 'X': case 'Y':
104 case '[': case '\\': case ']': case '^': case '_':
105 case 'a': case 'b': case 'c': case 'd': case 'e':
106 case 'f': case 'g': case 'h': case 'i': case 'j':
107 case 'k': case 'l': case 'm': case 'n': case 'o':
108 case 'p': case 'q': case 'r': case 's': case 't':
109 case 'u': case 'v': case 'w': case 'x': case 'y':
110 case 'z': case '{': case '|': case '}': case '~':
111 /* c is in the ISO C "basic character set". */
112 ret = wcrtomb (buf, btowc (c), NULL);
114 ASSERT (buf[0] == (char) c);
123 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
125 const char input[] = "B\374\337er"; /* "Büßer" */
127 check_character (input + 1, 1);
128 check_character (input + 2, 1);
133 /* Locale encoding is UTF-8. */
135 const char input[] = "B\303\274\303\237er"; /* "Büßer" */
137 check_character (input + 1, 2);
138 check_character (input + 3, 2);
143 /* Locale encoding is EUC-JP. */
145 const char input[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
147 check_character (input + 1, 2);
148 check_character (input + 3, 2);
149 check_character (input + 5, 2);
154 /* Locale encoding is GB18030. */
156 const char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
158 check_character (input + 1, 2);
159 check_character (input + 3, 4);