usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-vasnwprintf-gnu.c
blob13622476033c2b6299b08b0fdf2eabc88fd2b23e
1 /* Test of POSIX and GNU compatible vasnwprintf() and asnwprintf() functions.
2 Copyright (C) 2007-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>, 2023. */
19 #include <config.h>
21 #include "vasnwprintf.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <wchar.h>
28 #include "macros.h"
30 static void
31 test_function (wchar_t * (*my_asnwprintf) (wchar_t *, size_t *, const wchar_t *, ...))
33 /* Test the support of the 'b' conversion specifier for binary output of
34 integers. */
36 { /* Zero. */
37 size_t length;
38 wchar_t *result =
39 my_asnwprintf (NULL, &length, L"%b %d", 0, 33, 44, 55);
40 ASSERT (result != NULL);
41 ASSERT (wcscmp (result, L"0 33") == 0);
42 ASSERT (length == wcslen (result));
43 free (result);
46 { /* A positive number. */
47 size_t length;
48 wchar_t *result =
49 my_asnwprintf (NULL, &length, L"%b %d", 12345, 33, 44, 55);
50 ASSERT (result != NULL);
51 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
52 ASSERT (length == wcslen (result));
53 free (result);
56 { /* A large positive number. */
57 size_t length;
58 wchar_t *result =
59 my_asnwprintf (NULL, &length, L"%b %d", 0xFFFFFFFEU, 33, 44, 55);
60 ASSERT (result != NULL);
61 ASSERT (wcscmp (result, L"11111111111111111111111111111110 33") == 0);
62 ASSERT (length == wcslen (result));
63 free (result);
66 { /* Width. */
67 size_t length;
68 wchar_t *result =
69 my_asnwprintf (NULL, &length, L"%20b %d", 12345, 33, 44, 55);
70 ASSERT (result != NULL);
71 ASSERT (wcscmp (result, L" 11000000111001 33") == 0);
72 ASSERT (length == wcslen (result));
73 free (result);
76 { /* Width given as argument. */
77 size_t length;
78 wchar_t *result =
79 my_asnwprintf (NULL, &length, L"%*b %d", 20, 12345, 33, 44, 55);
80 ASSERT (result != NULL);
81 ASSERT (wcscmp (result, L" 11000000111001 33") == 0);
82 ASSERT (length == wcslen (result));
83 free (result);
86 { /* Negative width given as argument (cf. FLAG_LEFT below). */
87 size_t length;
88 wchar_t *result =
89 my_asnwprintf (NULL, &length, L"%*b %d", -20, 12345, 33, 44, 55);
90 ASSERT (result != NULL);
91 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
92 ASSERT (length == wcslen (result));
93 free (result);
96 { /* Precision. */
97 size_t length;
98 wchar_t *result =
99 my_asnwprintf (NULL, &length, L"%.20b %d", 12345, 33, 44, 55);
100 ASSERT (result != NULL);
101 ASSERT (wcscmp (result, L"00000011000000111001 33") == 0);
102 ASSERT (length == wcslen (result));
103 free (result);
106 { /* Zero precision and a positive number. */
107 size_t length;
108 wchar_t *result =
109 my_asnwprintf (NULL, &length, L"%.0b %d", 12345, 33, 44, 55);
110 ASSERT (result != NULL);
111 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
112 ASSERT (length == wcslen (result));
113 free (result);
116 { /* Zero precision and a zero number. */
117 size_t length;
118 wchar_t *result =
119 my_asnwprintf (NULL, &length, L"%.0b %d", 0, 33, 44, 55);
120 ASSERT (result != NULL);
121 /* ISO C and POSIX specify that "The result of converting a zero value
122 with a precision of zero is no characters." */
123 ASSERT (wcscmp (result, L" 33") == 0);
124 ASSERT (length == wcslen (result));
125 free (result);
128 { /* Width and precision. */
129 size_t length;
130 wchar_t *result =
131 my_asnwprintf (NULL, &length, L"%25.20b %d", 12345, 33, 44, 55);
132 ASSERT (result != NULL);
133 ASSERT (wcscmp (result, L" 00000011000000111001 33") == 0);
134 ASSERT (length == wcslen (result));
135 free (result);
138 { /* Padding and precision. */
139 size_t length;
140 wchar_t *result =
141 my_asnwprintf (NULL, &length, L"%025.20b %d", 12345, 33, 44, 55);
142 ASSERT (result != NULL);
143 /* Neither ISO C nor POSIX specify that the '0' flag is ignored when
144 a width and a precision are both present. But implementations do so. */
145 ASSERT (wcscmp (result, L" 00000011000000111001 33") == 0);
146 ASSERT (length == wcslen (result));
147 free (result);
150 { /* FLAG_LEFT. */
151 size_t length;
152 wchar_t *result =
153 my_asnwprintf (NULL, &length, L"%-20b %d", 12345, 33, 44, 55);
154 ASSERT (result != NULL);
155 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
156 ASSERT (length == wcslen (result));
157 free (result);
160 { /* FLAG_ALT with zero. */
161 size_t length;
162 wchar_t *result =
163 my_asnwprintf (NULL, &length, L"%#b %d", 0, 33, 44, 55);
164 ASSERT (result != NULL);
165 ASSERT (wcscmp (result, L"0 33") == 0);
166 ASSERT (length == wcslen (result));
167 free (result);
170 { /* FLAG_ALT with a positive number. */
171 size_t length;
172 wchar_t *result =
173 my_asnwprintf (NULL, &length, L"%#b %d", 12345, 33, 44, 55);
174 ASSERT (result != NULL);
175 ASSERT (wcscmp (result, L"0b11000000111001 33") == 0);
176 ASSERT (length == wcslen (result));
177 free (result);
180 { /* FLAG_ALT with a positive number and width. */
181 size_t length;
182 wchar_t *result =
183 my_asnwprintf (NULL, &length, L"%#20b %d", 12345, 33, 44, 55);
184 ASSERT (result != NULL);
185 ASSERT (wcscmp (result, L" 0b11000000111001 33") == 0);
186 ASSERT (length == wcslen (result));
187 free (result);
190 { /* FLAG_ALT with a positive number and padding. */
191 size_t length;
192 wchar_t *result =
193 my_asnwprintf (NULL, &length, L"%0#20b %d", 12345, 33, 44, 55);
194 ASSERT (result != NULL);
195 ASSERT (wcscmp (result, L"0b000011000000111001 33") == 0);
196 ASSERT (length == wcslen (result));
197 free (result);
200 { /* FLAG_ALT with a positive number and precision. */
201 size_t length;
202 wchar_t *result =
203 my_asnwprintf (NULL, &length, L"%0#.20b %d", 12345, 33, 44, 55);
204 ASSERT (result != NULL);
205 ASSERT (wcscmp (result, L"0b00000011000000111001 33") == 0);
206 ASSERT (length == wcslen (result));
207 free (result);
210 { /* FLAG_ALT with a positive number and width and precision. */
211 size_t length;
212 wchar_t *result =
213 my_asnwprintf (NULL, &length, L"%#25.20b %d", 12345, 33, 44, 55);
214 ASSERT (result != NULL);
215 ASSERT (wcscmp (result, L" 0b00000011000000111001 33") == 0);
216 ASSERT (length == wcslen (result));
217 free (result);
220 { /* FLAG_ALT with a positive number and padding and precision. */
221 size_t length;
222 wchar_t *result =
223 my_asnwprintf (NULL, &length, L"%0#25.20b %d", 12345, 33, 44, 55);
224 ASSERT (result != NULL);
225 /* Neither ISO C nor POSIX specify that the '0' flag is ignored when
226 a width and a precision are both present. But implementations do so. */
227 ASSERT (wcscmp (result, L" 0b00000011000000111001 33") == 0);
228 ASSERT (length == wcslen (result));
229 free (result);
232 { /* FLAG_ALT with a zero precision and a zero number. */
233 size_t length;
234 wchar_t *result =
235 my_asnwprintf (NULL, &length, L"%#.0b %d", 0, 33, 44, 55);
236 ASSERT (result != NULL);
237 /* ISO C and POSIX specify that "The result of converting a zero value
238 with a precision of zero is no characters.", and the prefix is added
239 only for non-zero values. */
240 ASSERT (wcscmp (result, L" 33") == 0);
241 ASSERT (length == wcslen (result));
242 free (result);
246 static wchar_t *
247 my_asnwprintf (wchar_t *resultbuf, size_t *lengthp, const wchar_t *format, ...)
249 va_list args;
250 wchar_t *ret;
252 va_start (args, format);
253 ret = vasnwprintf (resultbuf, lengthp, format, args);
254 va_end (args);
255 return ret;
258 static void
259 test_vasnwprintf ()
261 test_function (my_asnwprintf);
264 static void
265 test_asnwprintf ()
267 test_function (asnwprintf);
271 main (int argc, char *argv[])
273 test_vasnwprintf ();
274 test_asnwprintf ();
275 return test_exit_status;