Support compiling without -loldnames on native Windows.
[gnulib.git] / tests / test-mbsnrtoc32s.c
blob6b63fcc053c410d071e55a6c2390f98cc6339c23
1 /* Test of conversion of string to 32-bit wide string.
2 Copyright (C) 2008-2020 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
19 #include <config.h>
21 #include <uchar.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (mbsnrtoc32s, size_t,
25 (char32_t *, const char **, size_t, size_t, mbstate_t *));
27 #include <locale.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include "macros.h"
33 int
34 main (int argc, char *argv[])
36 mbstate_t state;
37 char32_t wc;
38 size_t ret;
40 /* configure should already have checked that the locale is supported. */
41 if (setlocale (LC_ALL, "") == NULL)
42 return 1;
44 /* Test NUL byte input. */
46 const char *src;
48 memset (&state, '\0', sizeof (mbstate_t));
50 src = "";
51 ret = mbsnrtoc32s (NULL, &src, 1, 0, &state);
52 ASSERT (ret == 0);
53 ASSERT (mbsinit (&state));
55 src = "";
56 ret = mbsnrtoc32s (NULL, &src, 1, 1, &state);
57 ASSERT (ret == 0);
58 ASSERT (mbsinit (&state));
60 wc = (char32_t) 0xBADFACE;
61 src = "";
62 ret = mbsnrtoc32s (&wc, &src, 1, 0, &state);
63 ASSERT (ret == 0);
64 ASSERT (wc == (char32_t) 0xBADFACE);
65 ASSERT (mbsinit (&state));
67 wc = (char32_t) 0xBADFACE;
68 src = "";
69 ret = mbsnrtoc32s (&wc, &src, 1, 1, &state);
70 ASSERT (ret == 0);
71 ASSERT (wc == 0);
72 ASSERT (mbsinit (&state));
75 if (argc > 1)
77 int unlimited;
79 for (unlimited = 0; unlimited < 2; unlimited++)
81 #define BUFSIZE 10
82 char32_t buf[BUFSIZE];
83 const char *src;
84 mbstate_t temp_state;
87 size_t i;
88 for (i = 0; i < BUFSIZE; i++)
89 buf[i] = (char32_t) 0xBADFACE;
92 switch (argv[1][0])
94 case '1':
95 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
97 char input[] = "B\374\337er"; /* "Büßer" */
98 memset (&state, '\0', sizeof (mbstate_t));
100 wc = (char32_t) 0xBADFACE;
101 ret = mbrtoc32 (&wc, input, 1, &state);
102 ASSERT (ret == 1);
103 ASSERT (wc == 'B');
104 ASSERT (mbsinit (&state));
105 input[0] = '\0';
107 wc = (char32_t) 0xBADFACE;
108 ret = mbrtoc32 (&wc, input + 1, 1, &state);
109 ASSERT (ret == 1);
110 ASSERT (c32tob (wc) == (unsigned char) '\374');
111 ASSERT (mbsinit (&state));
112 input[1] = '\0';
114 src = input + 2;
115 temp_state = state;
116 ret = mbsnrtoc32s (NULL, &src, 4, unlimited ? BUFSIZE : 1, &temp_state);
117 ASSERT (ret == 3);
118 ASSERT (src == input + 2);
119 ASSERT (mbsinit (&state));
121 src = input + 2;
122 ret = mbsnrtoc32s (buf, &src, 4, unlimited ? BUFSIZE : 1, &state);
123 ASSERT (ret == (unlimited ? 3 : 1));
124 ASSERT (src == (unlimited ? NULL : input + 3));
125 ASSERT (c32tob (buf[0]) == (unsigned char) '\337');
126 if (unlimited)
128 ASSERT (buf[1] == 'e');
129 ASSERT (buf[2] == 'r');
130 ASSERT (buf[3] == 0);
131 ASSERT (buf[4] == (char32_t) 0xBADFACE);
133 else
134 ASSERT (buf[1] == (char32_t) 0xBADFACE);
135 ASSERT (mbsinit (&state));
137 break;
139 case '2':
140 /* Locale encoding is UTF-8. */
142 char input[] = "s\303\274\303\237\360\237\230\213!"; /* "süß😋!" */
143 memset (&state, '\0', sizeof (mbstate_t));
145 wc = (char32_t) 0xBADFACE;
146 ret = mbrtoc32 (&wc, input, 1, &state);
147 ASSERT (ret == 1);
148 ASSERT (wc == 's');
149 ASSERT (mbsinit (&state));
150 input[0] = '\0';
152 wc = (char32_t) 0xBADFACE;
153 ret = mbrtoc32 (&wc, input + 1, 1, &state);
154 ASSERT (ret == (size_t)(-2));
155 ASSERT (wc == (char32_t) 0xBADFACE);
156 ASSERT (!mbsinit (&state));
157 input[1] = '\0';
159 src = input + 2;
160 temp_state = state;
161 ret = mbsnrtoc32s (NULL, &src, 9, unlimited ? BUFSIZE : 2, &temp_state);
162 ASSERT (ret == 4);
163 ASSERT (src == input + 2);
164 ASSERT (!mbsinit (&state));
166 src = input + 2;
167 ret = mbsnrtoc32s (buf, &src, 9, unlimited ? BUFSIZE : 2, &state);
168 ASSERT (ret == (unlimited ? 4 : 2));
169 ASSERT (src == (unlimited ? NULL : input + 5));
170 ASSERT (c32tob (buf[0]) == EOF);
171 ASSERT (c32tob (buf[1]) == EOF);
172 if (unlimited)
174 ASSERT (buf[2] == 0x1F60B); /* expect Unicode encoding */
175 ASSERT (buf[3] == '!');
176 ASSERT (buf[4] == 0);
177 ASSERT (buf[5] == (char32_t) 0xBADFACE);
179 else
180 ASSERT (buf[2] == (char32_t) 0xBADFACE);
181 ASSERT (mbsinit (&state));
183 break;
185 case '3':
186 /* Locale encoding is EUC-JP. */
188 char input[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
189 memset (&state, '\0', sizeof (mbstate_t));
191 wc = (char32_t) 0xBADFACE;
192 ret = mbrtoc32 (&wc, input, 1, &state);
193 ASSERT (ret == 1);
194 ASSERT (wc == '<');
195 ASSERT (mbsinit (&state));
196 input[0] = '\0';
198 wc = (char32_t) 0xBADFACE;
199 ret = mbrtoc32 (&wc, input + 1, 2, &state);
200 ASSERT (ret == 2);
201 ASSERT (c32tob (wc) == EOF);
202 ASSERT (mbsinit (&state));
203 input[1] = '\0';
204 input[2] = '\0';
206 wc = (char32_t) 0xBADFACE;
207 ret = mbrtoc32 (&wc, input + 3, 1, &state);
208 ASSERT (ret == (size_t)(-2));
209 ASSERT (wc == (char32_t) 0xBADFACE);
210 ASSERT (!mbsinit (&state));
211 input[3] = '\0';
213 src = input + 4;
214 temp_state = state;
215 ret = mbsnrtoc32s (NULL, &src, 5, unlimited ? BUFSIZE : 2, &temp_state);
216 ASSERT (ret == 3);
217 ASSERT (src == input + 4);
218 ASSERT (!mbsinit (&state));
220 src = input + 4;
221 ret = mbsnrtoc32s (buf, &src, 5, unlimited ? BUFSIZE : 2, &state);
222 ASSERT (ret == (unlimited ? 3 : 2));
223 ASSERT (src == (unlimited ? NULL : input + 7));
224 ASSERT (c32tob (buf[0]) == EOF);
225 ASSERT (c32tob (buf[1]) == EOF);
226 if (unlimited)
228 ASSERT (buf[2] == '>');
229 ASSERT (buf[3] == 0);
230 ASSERT (buf[4] == (char32_t) 0xBADFACE);
232 else
233 ASSERT (buf[2] == (char32_t) 0xBADFACE);
234 ASSERT (mbsinit (&state));
236 break;
238 case '4':
239 /* Locale encoding is GB18030. */
241 char input[] = "s\250\271\201\060\211\070\224\071\375\067!"; /* "süß😋!" */
242 memset (&state, '\0', sizeof (mbstate_t));
244 wc = (char32_t) 0xBADFACE;
245 ret = mbrtoc32 (&wc, input, 1, &state);
246 ASSERT (ret == 1);
247 ASSERT (wc == 's');
248 ASSERT (mbsinit (&state));
249 input[0] = '\0';
251 wc = (char32_t) 0xBADFACE;
252 ret = mbrtoc32 (&wc, input + 1, 1, &state);
253 ASSERT (ret == (size_t)(-2));
254 ASSERT (wc == (char32_t) 0xBADFACE);
255 ASSERT (!mbsinit (&state));
256 input[1] = '\0';
258 src = input + 2;
259 temp_state = state;
260 ret = mbsnrtoc32s (NULL, &src, 11, unlimited ? BUFSIZE : 2, &temp_state);
261 ASSERT (ret == 4);
262 ASSERT (src == input + 2);
263 ASSERT (!mbsinit (&state));
265 src = input + 2;
266 ret = mbsnrtoc32s (buf, &src, 11, unlimited ? BUFSIZE : 2, &state);
267 ASSERT (ret == (unlimited ? 4 : 2));
268 ASSERT (src == (unlimited ? NULL : input + 7));
269 ASSERT (c32tob (buf[0]) == EOF);
270 ASSERT (c32tob (buf[1]) == EOF);
271 if (unlimited)
273 ASSERT (c32tob (buf[2]) == EOF);
274 ASSERT (buf[3] == '!');
275 ASSERT (buf[4] == 0);
276 ASSERT (buf[5] == (char32_t) 0xBADFACE);
278 else
279 ASSERT (buf[2] == (char32_t) 0xBADFACE);
280 ASSERT (mbsinit (&state));
282 break;
284 default:
285 return 1;
289 return 0;
292 return 1;