usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-c32stombs.c
blob62f77342e4366267a1c26c95a2c588512f471a6e
1 /* Test of conversion of 32-bit wide string to string.
2 Copyright (C) 2008-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 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
19 #include <config.h>
21 #include <uchar.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (c32stombs, size_t,
25 (char *, const char32_t *, size_t));
27 #include <locale.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "macros.h"
33 int
34 main (int argc, char *argv[])
36 /* configure should already have checked that the locale is supported. */
37 if (setlocale (LC_ALL, "") == NULL)
38 return 1;
40 if (argc > 1)
42 char32_t input[10];
43 size_t n;
44 #define BUFSIZE 20
45 char buf[BUFSIZE];
46 size_t ret;
49 size_t i;
50 for (i = 0; i < BUFSIZE; i++)
51 buf[i] = '_';
54 switch (argv[1][0])
56 case '2':
57 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
59 const char original[] = "B\374\337er"; /* "Büßer" */
61 ret = mbstoc32s (input, original, 10);
62 ASSERT (ret == 5);
64 for (n = 0; n < 10; n++)
66 ret = c32stombs (NULL, input, n);
67 ASSERT (ret == 5);
69 ret = c32stombs (buf, input, n);
70 ASSERT (ret == (n <= 5 ? n : 5));
71 ASSERT (memcmp (buf, original, ret) == 0);
72 if (n > 5)
73 ASSERT (buf[ret] == '\0');
74 ASSERT (buf[ret + (n > 5) + 0] == '_');
75 ASSERT (buf[ret + (n > 5) + 1] == '_');
76 ASSERT (buf[ret + (n > 5) + 2] == '_');
79 break;
81 case '3':
82 /* Locale encoding is UTF-8. */
84 const char original[] = "s\303\274\303\237\360\237\230\213!"; /* "süß😋!" */
86 ret = mbstoc32s (input, original, 10);
87 ASSERT (ret == 5);
89 for (n = 0; n < 15; n++)
91 ret = c32stombs (NULL, input, n);
92 ASSERT (ret == 10);
94 ret = c32stombs (buf, input, n);
95 ASSERT (ret == (n < 1 ? n :
96 n < 3 ? 1 :
97 n < 5 ? 3 :
98 n < 9 ? 5 :
99 n <= 10 ? n : 10));
100 ASSERT (memcmp (buf, original, ret) == 0);
101 if (n > 10)
102 ASSERT (buf[ret] == '\0');
103 ASSERT (buf[ret + (n > 10) + 0] == '_');
104 ASSERT (buf[ret + (n > 10) + 1] == '_');
105 ASSERT (buf[ret + (n > 10) + 2] == '_');
108 break;
110 case '4':
111 /* Locale encoding is EUC-JP. */
113 const char original[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
115 ret = mbstoc32s (input, original, 10);
116 ASSERT (ret == 5);
118 for (n = 0; n < 10; n++)
120 ret = c32stombs (NULL, input, n);
121 ASSERT (ret == 8);
123 ret = c32stombs (buf, input, n);
124 ASSERT (ret == (n < 1 ? n :
125 n < 3 ? 1 :
126 n < 5 ? 3 :
127 n < 7 ? 5 :
128 n <= 8 ? n : 8));
129 ASSERT (memcmp (buf, original, ret) == 0);
130 if (n > 8)
131 ASSERT (buf[ret] == '\0');
132 ASSERT (buf[ret + (n > 8) + 0] == '_');
133 ASSERT (buf[ret + (n > 8) + 1] == '_');
134 ASSERT (buf[ret + (n > 8) + 2] == '_');
137 break;
140 case '5':
141 /* Locale encoding is GB18030. */
142 #if (defined __GLIBC__ && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 13 && __GLIBC_MINOR__ <= 15) || (GL_CHAR32_T_IS_UNICODE && (defined __FreeBSD__ || defined __NetBSD__ || defined __sun))
143 if (test_exit_status != EXIT_SUCCESS)
144 return test_exit_status;
145 fputs ("Skipping test: The GB18030 converter in this system's iconv is broken.\n", stderr);
146 return 77;
147 #endif
149 const char original[] = "s\250\271\201\060\211\070\224\071\375\067!"; /* "süß😋!" */
151 ret = mbstoc32s (input, original, 10);
152 ASSERT (ret == 5);
154 for (n = 0; n < 15; n++)
156 ret = c32stombs (NULL, input, n);
157 ASSERT (ret == 12);
159 ret = c32stombs (buf, input, n);
160 ASSERT (ret == (n < 1 ? n :
161 n < 3 ? 1 :
162 n < 7 ? 3 :
163 n < 11 ? 7 :
164 n <= 12 ? n : 12));
165 ASSERT (memcmp (buf, original, ret) == 0);
166 if (n > 12)
167 ASSERT (buf[ret] == '\0');
168 ASSERT (buf[ret + (n > 12) + 0] == '_');
169 ASSERT (buf[ret + (n > 12) + 1] == '_');
170 ASSERT (buf[ret + (n > 12) + 2] == '_');
173 break;
175 default:
176 return 1;
179 return test_exit_status;
182 return 1;