1 /* Test of conversion of wide character to multibyte character.
2 Copyright (C) 2008-2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
18
19 #include <assert.h>
20 #include <locale.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <wchar.h>
24
25 /* Check the multibyte character s[0..n-1]. */
26 static void
check_character(const char * s,size_t n)27 check_character (const char *s, size_t n)
28 {
29 wchar_t wc;
30 char buf[64];
31 int iret;
32 size_t ret;
33
34 wc = (wchar_t) 0xBADFACE;
35 iret = mbtowc (&wc, s, n);
36 assert (iret == (int)n);
37
38 ret = wcrtomb (buf, wc, NULL);
39 assert (ret == n);
40 assert (memcmp (buf, s, n) == 0);
41
42 /* Test special calling convention, passing a NULL pointer. */
43 ret = wcrtomb (NULL, wc, NULL);
44
45 assert (ret == 1);
46 }
47
48 int
main(int argc,char * argv[])49 main (int argc, char *argv[])
50 {
51 char buf[64];
52 size_t ret;
53 int i;
54
55 /* configure should already have checked that the locale is supported. */
56 if (setlocale (LC_ALL, "") == NULL) {
57 fprintf(stderr, "unable to set standard locale\n");
58 return 1;
59 }
60
61 /* Test NUL character. */
62 printf("NUL character ...\n");
63 {
64 buf[0] = 'x';
65 ret = wcrtomb (buf, 0, NULL);
66 assert (ret == 1);
67 assert (buf[0] == '\0');
68 }
69
70 /* Test single bytes. */
71 printf("single bytes ...\n");
72 {
73 int c;
74
75 for (c = 0; c < 0x100; c++)
76 switch (c)
77 {
78 case '\t': case '\v': case '\f':
79 case ' ': case '!': case '"': case '#': case '%':
80 case '&': case '\'': case '(': case ')': case '*':
81 case '+': case ',': case '-': case '.': case '/':
82 case '0': case '1': case '2': case '3': case '4':
83 case '5': case '6': case '7': case '8': case '9':
84 case ':': case ';': case '<': case '=': case '>':
85 case '?':
86 case 'A': case 'B': case 'C': case 'D': case 'E':
87 case 'F': case 'G': case 'H': case 'I': case 'J':
88 case 'K': case 'L': case 'M': case 'N': case 'O':
89 case 'P': case 'Q': case 'R': case 'S': case 'T':
90 case 'U': case 'V': case 'W': case 'X': case 'Y':
91 case 'Z':
92 case '[': case '\\': case ']': case '^': case '_':
93 case 'a': case 'b': case 'c': case 'd': case 'e':
94 case 'f': case 'g': case 'h': case 'i': case 'j':
95 case 'k': case 'l': case 'm': case 'n': case 'o':
96 case 'p': case 'q': case 'r': case 's': case 't':
97 case 'u': case 'v': case 'w': case 'x': case 'y':
98 case 'z': case '{': case '|': case '}': case '~':
99 /* c is in the ISO C "basic character set". */
100 ret = wcrtomb (buf, btowc (c), NULL);
101 assert (ret == 1);
102 assert (buf[0] == (char) c);
103 break;
104 }
105 }
106
107 /* Test special calling convention, passing a NULL pointer. */
108 printf("special calling convention with NULL pointer ...\n");
109 {
110 ret = wcrtomb (NULL, '\0', NULL);
111 assert (ret == 1);
112 ret = wcrtomb (NULL, btowc ('x'), NULL);
113 assert (ret == 1);
114 }
115
116 for (i = '1'; i <= '4'; ++i) {
117 switch (i)
118 {
119 case '1':
120 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
121 printf("ISO8859-1 ...\n");
122 {
123 const char input[] = "B\374\337er"; /* "Büßer" */
124
125 if (setlocale (LC_ALL, "en_US.ISO8859-1") == NULL) {
126 fprintf(stderr, "unable to set ISO8859-1 locale, skipping\n");
127 break;
128 }
129
130 check_character (input + 1, 1);
131 check_character (input + 2, 1);
132 }
133 break;
134
135 case '2':
136 /* Locale encoding is UTF-8. */
137 printf("UTF-8 ...\n");
138 {
139 const char input[] = "B\303\274\303\237er"; /* "Büßer" */
140
141 if (setlocale (LC_ALL, "en_US.UTF-8") == NULL) {
142 fprintf(stderr, "unable to set UTF-8 locale, skipping\n");
143 break;
144 }
145
146 check_character (input + 1, 2);
147 check_character (input + 3, 2);
148 }
149 break;
150
151 case '3':
152 /* Locale encoding is EUC-JP. */
153 printf("EUC-JP ...\n");
154 {
155 const char input[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
156
157 if (setlocale (LC_ALL, "en_US.EUC-JP") == NULL) {
158 fprintf(stderr, "unable to set EUC-JP locale, skipping\n");
159 break;
160 }
161
162 check_character (input + 1, 2);
163 check_character (input + 3, 2);
164 check_character (input + 5, 2);
165 }
166 break;
167
168 case '4':
169 /* Locale encoding is GB18030. */
170 printf("GB18030 ...\n");
171 {
172 const char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
173
174 if (setlocale (LC_ALL, "en_US.GB18030") == NULL) {
175 fprintf(stderr, "unable to set GB18030 locale, skipping\n");
176 break;
177 }
178
179 check_character (input + 1, 2);
180 check_character (input + 3, 4);
181 }
182 break;
183 }
184 }
185
186 return 0;
187 }
188