nstrftime, c-nstrftime tests: Avoid test failures on native Windows.
[gnulib.git] / tests / test-c32toupper.c
blobd0c2ba634aeea88e799350b5815a67f1d9ecce45
1 /* Test of c32toupper() function.
2 Copyright (C) 2020-2024 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 #include <config.h>
19 #include <uchar.h>
21 #include "signature.h"
22 SIGNATURE_CHECK (c32toupper, wint_t, (wint_t));
24 #include <locale.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <wchar.h>
29 #include "macros.h"
31 /* Representation of a multibyte character. */
32 #define MBCHAR_BUF_SIZE 6
33 struct multibyte
35 size_t nbytes; /* number of bytes of current character, > 0 */
36 char buf[MBCHAR_BUF_SIZE]; /* room for the bytes */
39 /* Returns the value of c32toupper for the multibyte character s[0..n-1],
40 as a multibyte character. */
41 static struct multibyte
42 for_character (const char *s, size_t n)
44 mbstate_t state;
45 char32_t wc;
46 size_t ret;
47 struct multibyte result;
49 memset (&state, '\0', sizeof (mbstate_t));
50 wc = (char32_t) 0xBADFACE;
51 ret = mbrtoc32 (&wc, s, n, &state);
52 ASSERT (ret == n);
54 wc = c32toupper (wc);
55 ASSERT (wc != WEOF);
57 memset (&state, '\0', sizeof (mbstate_t));
58 ret = c32rtomb (result.buf, wc, &state);
59 ASSERT (ret != 0);
60 if (ret == (size_t)(-1))
61 /* wc cannot be converted back to multibyte. */
62 result.nbytes = 0;
63 else
65 ASSERT (ret <= MBCHAR_BUF_SIZE);
66 result.nbytes = ret;
68 return result;
71 int
72 main (int argc, char *argv[])
74 wint_t wc;
75 struct multibyte mb;
76 char buf[4];
78 /* configure should already have checked that the locale is supported. */
79 if (setlocale (LC_ALL, "") == NULL)
80 return 1;
82 /* Test WEOF. */
83 wc = c32toupper (WEOF);
84 ASSERT (wc == WEOF);
86 /* Test single-byte characters.
87 POSIX specifies in
88 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html>
89 that
90 - in all locales, the lowercase characters include the a ... z
91 characters, and the corresponding characters A ... Z (if not in a
92 Turkish locale) are uppercase,
93 - in the "POSIX" locale (which is usually the same as the "C" locale),
94 the lowercase characters include only the ASCII a ... z characters,
95 and the corresponding characters A ... Z are uppercase.
97 #if defined __NetBSD__
98 /* towupper is broken in the zh_CN.GB18030 locale on NetBSD 9.0.
99 See <https://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=57339>. */
100 if (!(argc > 1 && argv[1][0] == '4'))
101 #endif
103 int c;
105 for (c = 0; c < 0x100; c++)
106 switch (c)
108 case '\t': case '\v': case '\f':
109 case ' ': case '!': case '"': case '#': case '%':
110 case '&': case '\'': case '(': case ')': case '*':
111 case '+': case ',': case '-': case '.': case '/':
112 case '0': case '1': case '2': case '3': case '4':
113 case '5': case '6': case '7': case '8': case '9':
114 case ':': case ';': case '<': case '=': case '>':
115 case '?':
116 case 'A': case 'B': case 'C': case 'D': case 'E':
117 case 'F': case 'G': case 'H': case 'I': case 'J':
118 case 'K': case 'L': case 'M': case 'N': case 'O':
119 case 'P': case 'Q': case 'R': case 'S': case 'T':
120 case 'U': case 'V': case 'W': case 'X': case 'Y':
121 case 'Z':
122 case '[': case '\\': case ']': case '^': case '_':
123 case 'a': case 'b': case 'c': case 'd': case 'e':
124 case 'f': case 'g': case 'h': case 'i': case 'j':
125 case 'k': case 'l': case 'm': case 'n': case 'o':
126 case 'p': case 'q': case 'r': case 's': case 't':
127 case 'u': case 'v': case 'w': case 'x': case 'y':
128 case 'z': case '{': case '|': case '}': case '~':
129 /* c is in the ISO C "basic character set". */
130 buf[0] = (unsigned char) c;
131 mb = for_character (buf, 1);
132 switch (c)
134 case 'a': case 'b': case 'c': case 'd': case 'e':
135 case 'f': case 'g': case 'h': case 'i': case 'j':
136 case 'k': case 'l': case 'm': case 'n': case 'o':
137 case 'p': case 'q': case 'r': case 's': case 't':
138 case 'u': case 'v': case 'w': case 'x': case 'y':
139 case 'z':
140 ASSERT (mb.nbytes == 1);
141 ASSERT ((unsigned char) mb.buf[0] == (unsigned char) c - 'a' + 'A');
142 break;
143 default:
144 ASSERT (mb.nbytes == 1);
145 ASSERT ((unsigned char) mb.buf[0] == c);
146 break;
148 break;
152 if (argc > 1)
153 switch (argv[1][0])
155 case '0':
156 /* C locale; tested above. */
157 return test_exit_status;
159 case '1':
160 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
162 /* U+00B2 SUPERSCRIPT TWO */
163 mb = for_character ("\262", 1);
164 ASSERT (mb.nbytes == 1);
165 ASSERT (memcmp (mb.buf, "\262", 1) == 0);
166 #if !(defined __GLIBC__ || (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __NetBSD__ || defined __sun || defined __CYGWIN__)
167 /* U+00B5 MICRO SIGN */
168 mb = for_character ("\265", 1);
169 ASSERT (mb.nbytes == 1);
170 ASSERT (memcmp (mb.buf, "\265", 1) == 0);
171 #endif
172 /* U+00C9 LATIN CAPITAL LETTER E WITH ACUTE */
173 mb = for_character ("\311", 1);
174 ASSERT (mb.nbytes == 1);
175 ASSERT (memcmp (mb.buf, "\311", 1) == 0);
176 /* U+00DF LATIN SMALL LETTER SHARP S */
177 mb = for_character ("\337", 1);
178 ASSERT (mb.nbytes == 1);
179 ASSERT (memcmp (mb.buf, "\337", 1) == 0);
180 /* U+00E9 LATIN SMALL LETTER E WITH ACUTE */
181 mb = for_character ("\351", 1);
182 ASSERT (mb.nbytes == 1);
183 ASSERT (memcmp (mb.buf, "\311", 1) == 0);
184 #if !(defined __GLIBC__ || (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __DragonFly__ || defined __NetBSD__ || defined __sun || defined __CYGWIN__ || (defined _WIN32 && !defined __CYGWIN__))
185 /* U+00FF LATIN SMALL LETTER Y WITH DIAERESIS */
186 mb = for_character ("\377", 1);
187 ASSERT (mb.nbytes == 1);
188 ASSERT (memcmp (mb.buf, "\377", 1) == 0);
189 #endif
191 return test_exit_status;
193 case '2':
194 /* Locale encoding is EUC-JP. */
196 /* U+00C9 LATIN CAPITAL LETTER E WITH ACUTE */
197 mb = for_character ("\217\252\261", 3);
198 ASSERT (mb.nbytes == 3);
199 ASSERT (memcmp (mb.buf, "\217\252\261", 3) == 0);
200 #if !defined __NetBSD__
201 /* U+00DF LATIN SMALL LETTER SHARP S */
202 mb = for_character ("\217\251\316", 3);
203 ASSERT (mb.nbytes == 3);
204 ASSERT (memcmp (mb.buf, "\217\251\316", 3) == 0);
205 #endif
206 #if !((defined __APPLE__ && defined __MACH__) || defined __DragonFly__)
207 /* U+00E9 LATIN SMALL LETTER E WITH ACUTE */
208 mb = for_character ("\217\253\261", 3);
209 ASSERT (mb.nbytes == 3);
210 ASSERT (memcmp (mb.buf, "\217\252\261", 3) == 0);
211 #endif
212 #if !((defined __APPLE__ && defined __MACH__) || defined __DragonFly__ || defined __NetBSD__)
213 /* U+00FF LATIN SMALL LETTER Y WITH DIAERESIS */
214 mb = for_character ("\217\253\363", 3);
215 ASSERT (mb.nbytes == 3);
216 ASSERT (memcmp (mb.buf, "\217\252\363", 3) == 0);
217 #endif
218 /* U+0141 LATIN CAPITAL LETTER L WITH STROKE */
219 mb = for_character ("\217\251\250", 3);
220 ASSERT (mb.nbytes == 3);
221 ASSERT (memcmp (mb.buf, "\217\251\250", 3) == 0);
222 #if !((defined __APPLE__ && defined __MACH__) || defined __DragonFly__)
223 /* U+0142 LATIN SMALL LETTER L WITH STROKE */
224 mb = for_character ("\217\251\310", 3);
225 ASSERT (mb.nbytes == 3);
226 ASSERT (memcmp (mb.buf, "\217\251\250", 3) == 0);
227 #endif
228 /* U+0429 CYRILLIC CAPITAL LETTER SHCHA */
229 mb = for_character ("\247\273", 2);
230 ASSERT (mb.nbytes == 2);
231 ASSERT (memcmp (mb.buf, "\247\273", 2) == 0);
232 #if !defined __DragonFly__
233 /* U+0449 CYRILLIC SMALL LETTER SHCHA */
234 mb = for_character ("\247\353", 2);
235 ASSERT (mb.nbytes == 2);
236 ASSERT (memcmp (mb.buf, "\247\273", 2) == 0);
237 #endif
238 /* U+3073 HIRAGANA LETTER BI */
239 mb = for_character ("\244\323", 2);
240 ASSERT (mb.nbytes == 2);
241 ASSERT (memcmp (mb.buf, "\244\323", 2) == 0);
242 /* U+FF27 FULLWIDTH LATIN CAPITAL LETTER G */
243 mb = for_character ("\243\307", 2);
244 ASSERT (mb.nbytes == 2);
245 ASSERT (memcmp (mb.buf, "\243\307", 2) == 0);
246 #if !defined __DragonFly__
247 /* U+FF47 FULLWIDTH LATIN SMALL LETTER G */
248 mb = for_character ("\243\347", 2);
249 ASSERT (mb.nbytes == 2);
250 ASSERT (memcmp (mb.buf, "\243\307", 2) == 0);
251 #endif
253 return test_exit_status;
255 case '3':
256 /* Locale encoding is UTF-8. */
258 /* U+00B2 SUPERSCRIPT TWO */
259 mb = for_character ("\302\262", 2);
260 ASSERT (mb.nbytes == 2);
261 ASSERT (memcmp (mb.buf, "\302\262", 2) == 0);
262 #if !(defined __GLIBC__ || defined MUSL_LIBC || (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __DragonFly__ || defined __NetBSD__ || defined _AIX || defined __sun || defined __CYGWIN__)
263 /* U+00B5 MICRO SIGN */
264 mb = for_character ("\302\265", 2);
265 ASSERT (mb.nbytes == 2);
266 ASSERT (memcmp (mb.buf, "\302\265", 2) == 0);
267 #endif
268 /* U+00C9 LATIN CAPITAL LETTER E WITH ACUTE */
269 mb = for_character ("\303\211", 2);
270 ASSERT (mb.nbytes == 2);
271 ASSERT (memcmp (mb.buf, "\303\211", 2) == 0);
272 #if !defined MUSL_LIBC
273 /* U+00DF LATIN SMALL LETTER SHARP S */
274 mb = for_character ("\303\237", 2);
275 ASSERT (mb.nbytes == 2);
276 ASSERT (memcmp (mb.buf, "\303\237", 2) == 0);
277 #endif
278 #if !(defined _WIN32 && !defined __CYGWIN__)
279 /* U+00E9 LATIN SMALL LETTER E WITH ACUTE */
280 mb = for_character ("\303\251", 2);
281 ASSERT (mb.nbytes == 2);
282 ASSERT (memcmp (mb.buf, "\303\211", 2) == 0);
283 /* U+00FF LATIN SMALL LETTER Y WITH DIAERESIS */
284 mb = for_character ("\303\277", 2);
285 ASSERT (mb.nbytes == 2);
286 ASSERT (memcmp (mb.buf, "\305\270", 2) == 0);
287 #endif
288 /* U+0141 LATIN CAPITAL LETTER L WITH STROKE */
289 mb = for_character ("\305\201", 2);
290 ASSERT (mb.nbytes == 2);
291 ASSERT (memcmp (mb.buf, "\305\201", 2) == 0);
292 /* U+0142 LATIN SMALL LETTER L WITH STROKE */
293 mb = for_character ("\305\202", 2);
294 ASSERT (mb.nbytes == 2);
295 ASSERT (memcmp (mb.buf, "\305\201", 2) == 0);
296 /* U+0429 CYRILLIC CAPITAL LETTER SHCHA */
297 mb = for_character ("\320\251", 2);
298 ASSERT (mb.nbytes == 2);
299 ASSERT (memcmp (mb.buf, "\320\251", 2) == 0);
300 /* U+0449 CYRILLIC SMALL LETTER SHCHA */
301 mb = for_character ("\321\211", 2);
302 ASSERT (mb.nbytes == 2);
303 ASSERT (memcmp (mb.buf, "\320\251", 2) == 0);
304 /* U+05D5 HEBREW LETTER VAV */
305 mb = for_character ("\327\225", 2);
306 ASSERT (mb.nbytes == 2);
307 ASSERT (memcmp (mb.buf, "\327\225", 2) == 0);
308 /* U+3073 HIRAGANA LETTER BI */
309 mb = for_character ("\343\201\263", 3);
310 ASSERT (mb.nbytes == 3);
311 ASSERT (memcmp (mb.buf, "\343\201\263", 3) == 0);
312 /* U+3162 HANGUL LETTER YI */
313 mb = for_character ("\343\205\242", 3);
314 ASSERT (mb.nbytes == 3);
315 ASSERT (memcmp (mb.buf, "\343\205\242", 3) == 0);
316 /* U+FF27 FULLWIDTH LATIN CAPITAL LETTER G */
317 mb = for_character ("\357\274\247", 3);
318 ASSERT (mb.nbytes == 3);
319 ASSERT (memcmp (mb.buf, "\357\274\247", 3) == 0);
320 /* U+FF47 FULLWIDTH LATIN SMALL LETTER G */
321 mb = for_character ("\357\275\207", 3);
322 ASSERT (mb.nbytes == 3);
323 ASSERT (memcmp (mb.buf, "\357\274\247", 3) == 0);
324 /* U+FFDB HALFWIDTH HANGUL LETTER YI */
325 mb = for_character ("\357\277\233", 3);
326 ASSERT (mb.nbytes == 3);
327 ASSERT (memcmp (mb.buf, "\357\277\233", 3) == 0);
328 /* U+10419 DESERET CAPITAL LETTER EF */
329 mb = for_character ("\360\220\220\231", 4);
330 ASSERT (mb.nbytes == 4);
331 ASSERT (memcmp (mb.buf, "\360\220\220\231", 4) == 0);
332 #if !(defined __DragonFly__ || defined __sun)
333 /* U+10441 DESERET SMALL LETTER EF */
334 mb = for_character ("\360\220\221\201", 4);
335 ASSERT (mb.nbytes == 4);
336 ASSERT (memcmp (mb.buf, "\360\220\220\231", 4) == 0);
337 #endif
338 /* U+E0041 TAG LATIN CAPITAL LETTER A */
339 mb = for_character ("\363\240\201\201", 4);
340 ASSERT (mb.nbytes == 4);
341 ASSERT (memcmp (mb.buf, "\363\240\201\201", 4) == 0);
342 /* U+E0061 TAG LATIN SMALL LETTER A */
343 mb = for_character ("\363\240\201\241", 4);
344 ASSERT (mb.nbytes == 4);
345 ASSERT (memcmp (mb.buf, "\363\240\201\241", 4) == 0);
347 return test_exit_status;
349 case '4':
350 /* Locale encoding is GB18030. */
351 #if (defined __GLIBC__ && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 13 && __GLIBC_MINOR__ <= 15) || (GL_CHAR32_T_IS_UNICODE && (defined __FreeBSD__ || defined __NetBSD__ || defined __sun))
352 if (test_exit_status != EXIT_SUCCESS)
353 return test_exit_status;
354 fputs ("Skipping test: The GB18030 converter in this system's iconv is broken.\n", stderr);
355 return 77;
356 #endif
358 /* U+00B2 SUPERSCRIPT TWO */
359 mb = for_character ("\201\060\205\065", 4);
360 ASSERT (mb.nbytes == 4);
361 ASSERT (memcmp (mb.buf, "\201\060\205\065", 4) == 0);
362 #if !(defined __GLIBC__ || defined __FreeBSD__ || (defined __APPLE__ && defined __MACH__) || defined __NetBSD__ || defined __CYGWIN__)
363 /* U+00B5 MICRO SIGN */
364 mb = for_character ("\201\060\205\070", 4);
365 ASSERT (mb.nbytes == 4);
366 ASSERT (memcmp (mb.buf, "\201\060\205\070", 4) == 0);
367 #endif
368 /* U+00C9 LATIN CAPITAL LETTER E WITH ACUTE */
369 mb = for_character ("\201\060\207\067", 4);
370 ASSERT (mb.nbytes == 4);
371 ASSERT (memcmp (mb.buf, "\201\060\207\067", 4) == 0);
372 /* U+00DF LATIN SMALL LETTER SHARP S */
373 mb = for_character ("\201\060\211\070", 4);
374 ASSERT (mb.nbytes == 4);
375 ASSERT (memcmp (mb.buf, "\201\060\211\070", 4) == 0);
376 #if !(defined __FreeBSD__ || defined __DragonFly__ || defined __sun)
377 /* U+00E9 LATIN SMALL LETTER E WITH ACUTE */
378 mb = for_character ("\250\246", 2);
379 ASSERT (mb.nbytes == 4);
380 ASSERT (memcmp (mb.buf, "\201\060\207\067", 4) == 0);
381 /* U+00FF LATIN SMALL LETTER Y WITH DIAERESIS */
382 mb = for_character ("\201\060\213\067", 4);
383 ASSERT (mb.nbytes == 4);
384 ASSERT (memcmp (mb.buf, "\201\060\227\060", 4) == 0);
385 #endif
386 /* U+0141 LATIN CAPITAL LETTER L WITH STROKE */
387 mb = for_character ("\201\060\221\071", 4);
388 ASSERT (mb.nbytes == 4);
389 ASSERT (memcmp (mb.buf, "\201\060\221\071", 4) == 0);
390 #if !(defined __FreeBSD__ || defined __DragonFly__ || defined __sun)
391 /* U+0142 LATIN SMALL LETTER L WITH STROKE */
392 mb = for_character ("\201\060\222\060", 4);
393 ASSERT (mb.nbytes == 4);
394 ASSERT (memcmp (mb.buf, "\201\060\221\071", 4) == 0);
395 #endif
396 /* U+0429 CYRILLIC CAPITAL LETTER SHCHA */
397 mb = for_character ("\247\273", 2);
398 ASSERT (mb.nbytes == 2);
399 ASSERT (memcmp (mb.buf, "\247\273", 2) == 0);
400 #if !(defined __FreeBSD__ || defined __DragonFly__)
401 /* U+0449 CYRILLIC SMALL LETTER SHCHA */
402 mb = for_character ("\247\353", 2);
403 ASSERT (mb.nbytes == 2);
404 ASSERT (memcmp (mb.buf, "\247\273", 2) == 0);
405 #endif
406 /* U+05D5 HEBREW LETTER VAV */
407 mb = for_character ("\201\060\371\067", 4);
408 ASSERT (mb.nbytes == 4);
409 ASSERT (memcmp (mb.buf, "\201\060\371\067", 4) == 0);
410 /* U+3073 HIRAGANA LETTER BI */
411 mb = for_character ("\244\323", 2);
412 ASSERT (mb.nbytes == 2);
413 ASSERT (memcmp (mb.buf, "\244\323", 2) == 0);
414 /* U+3162 HANGUL LETTER YI */
415 mb = for_character ("\201\071\256\062", 4);
416 ASSERT (mb.nbytes == 4);
417 ASSERT (memcmp (mb.buf, "\201\071\256\062", 4) == 0);
418 /* U+FF27 FULLWIDTH LATIN CAPITAL LETTER G */
419 mb = for_character ("\243\307", 2);
420 ASSERT (mb.nbytes == 2);
421 ASSERT (memcmp (mb.buf, "\243\307", 2) == 0);
422 #if !defined __DragonFly__
423 /* U+FF47 FULLWIDTH LATIN SMALL LETTER G */
424 mb = for_character ("\243\347", 2);
425 ASSERT (mb.nbytes == 2);
426 ASSERT (memcmp (mb.buf, "\243\307", 2) == 0);
427 #endif
428 /* U+FFDB HALFWIDTH HANGUL LETTER YI */
429 mb = for_character ("\204\061\241\071", 4);
430 ASSERT (mb.nbytes == 4);
431 ASSERT (memcmp (mb.buf, "\204\061\241\071", 4) == 0);
432 /* U+10419 DESERET CAPITAL LETTER EF */
433 mb = for_character ("\220\060\351\071", 4);
434 ASSERT (mb.nbytes == 4);
435 ASSERT (memcmp (mb.buf, "\220\060\351\071", 4) == 0);
436 #if !((defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __DragonFly__ || defined __NetBSD__ || defined __sun)
437 /* U+10441 DESERET SMALL LETTER EF */
438 mb = for_character ("\220\060\355\071", 4);
439 ASSERT (mb.nbytes == 4);
440 ASSERT (memcmp (mb.buf, "\220\060\351\071", 4) == 0);
441 #endif
442 /* U+E0041 TAG LATIN CAPITAL LETTER A */
443 mb = for_character ("\323\066\234\063", 4);
444 ASSERT (mb.nbytes == 4);
445 ASSERT (memcmp (mb.buf, "\323\066\234\063", 4) == 0);
446 /* U+E0061 TAG LATIN SMALL LETTER A */
447 mb = for_character ("\323\066\237\065", 4);
448 ASSERT (mb.nbytes == 4);
449 ASSERT (memcmp (mb.buf, "\323\066\237\065", 4) == 0);
451 return test_exit_status;
455 return 1;