usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-vasnprintf-gnu.c
blobbcb4f143ff07d12e585c4f4d0a3be3ad99a21d40
1 /* Test of POSIX and GNU compatible vasnprintf() and asnprintf() 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 "vasnprintf.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "macros.h"
29 static void
30 test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
32 /* Test the support of the 'B' conversion specifier for binary output of
33 integers. */
35 { /* Zero. */
36 size_t length;
37 char *result =
38 my_asnprintf (NULL, &length, "%B %d", 0, 33, 44, 55);
39 ASSERT (result != NULL);
40 ASSERT (strcmp (result, "0 33") == 0);
41 ASSERT (length == strlen (result));
42 free (result);
45 { /* A positive number. */
46 size_t length;
47 char *result =
48 my_asnprintf (NULL, &length, "%B %d", 12345, 33, 44, 55);
49 ASSERT (result != NULL);
50 ASSERT (strcmp (result, "11000000111001 33") == 0);
51 ASSERT (length == strlen (result));
52 free (result);
55 { /* A large positive number. */
56 size_t length;
57 char *result =
58 my_asnprintf (NULL, &length, "%B %d", 0xFFFFFFFEU, 33, 44, 55);
59 ASSERT (result != NULL);
60 ASSERT (strcmp (result, "11111111111111111111111111111110 33") == 0);
61 ASSERT (length == strlen (result));
62 free (result);
65 { /* Width. */
66 size_t length;
67 char *result =
68 my_asnprintf (NULL, &length, "%20B %d", 12345, 33, 44, 55);
69 ASSERT (result != NULL);
70 ASSERT (strcmp (result, " 11000000111001 33") == 0);
71 ASSERT (length == strlen (result));
72 free (result);
75 { /* Width given as argument. */
76 size_t length;
77 char *result =
78 my_asnprintf (NULL, &length, "%*B %d", 20, 12345, 33, 44, 55);
79 ASSERT (result != NULL);
80 ASSERT (strcmp (result, " 11000000111001 33") == 0);
81 ASSERT (length == strlen (result));
82 free (result);
85 { /* Negative width given as argument (cf. FLAG_LEFT below). */
86 size_t length;
87 char *result =
88 my_asnprintf (NULL, &length, "%*B %d", -20, 12345, 33, 44, 55);
89 ASSERT (result != NULL);
90 ASSERT (strcmp (result, "11000000111001 33") == 0);
91 ASSERT (length == strlen (result));
92 free (result);
95 { /* Precision. */
96 size_t length;
97 char *result =
98 my_asnprintf (NULL, &length, "%.20B %d", 12345, 33, 44, 55);
99 ASSERT (result != NULL);
100 ASSERT (strcmp (result, "00000011000000111001 33") == 0);
101 ASSERT (length == strlen (result));
102 free (result);
105 { /* Zero precision and a positive number. */
106 size_t length;
107 char *result =
108 my_asnprintf (NULL, &length, "%.0B %d", 12345, 33, 44, 55);
109 ASSERT (result != NULL);
110 ASSERT (strcmp (result, "11000000111001 33") == 0);
111 ASSERT (length == strlen (result));
112 free (result);
115 { /* Zero precision and a zero number. */
116 size_t length;
117 char *result =
118 my_asnprintf (NULL, &length, "%.0B %d", 0, 33, 44, 55);
119 ASSERT (result != NULL);
120 /* ISO C and POSIX specify that "The result of converting a zero value
121 with a precision of zero is no characters." */
122 ASSERT (strcmp (result, " 33") == 0);
123 ASSERT (length == strlen (result));
124 free (result);
127 { /* Width and precision. */
128 size_t length;
129 char *result =
130 my_asnprintf (NULL, &length, "%25.20B %d", 12345, 33, 44, 55);
131 ASSERT (result != NULL);
132 ASSERT (strcmp (result, " 00000011000000111001 33") == 0);
133 ASSERT (length == strlen (result));
134 free (result);
137 { /* Padding and precision. */
138 size_t length;
139 char *result =
140 my_asnprintf (NULL, &length, "%025.20B %d", 12345, 33, 44, 55);
141 ASSERT (result != NULL);
142 /* Neither ISO C nor POSIX specify that the '0' flag is ignored when
143 a width and a precision are both present. But implementations do so. */
144 ASSERT (strcmp (result, " 00000011000000111001 33") == 0);
145 ASSERT (length == strlen (result));
146 free (result);
149 { /* FLAG_LEFT. */
150 size_t length;
151 char *result =
152 my_asnprintf (NULL, &length, "%-20B %d", 12345, 33, 44, 55);
153 ASSERT (result != NULL);
154 ASSERT (strcmp (result, "11000000111001 33") == 0);
155 ASSERT (length == strlen (result));
156 free (result);
159 { /* FLAG_ALT with zero. */
160 size_t length;
161 char *result =
162 my_asnprintf (NULL, &length, "%#B %d", 0, 33, 44, 55);
163 ASSERT (result != NULL);
164 ASSERT (strcmp (result, "0 33") == 0);
165 ASSERT (length == strlen (result));
166 free (result);
169 { /* FLAG_ALT with a positive number. */
170 size_t length;
171 char *result =
172 my_asnprintf (NULL, &length, "%#B %d", 12345, 33, 44, 55);
173 ASSERT (result != NULL);
174 ASSERT (strcmp (result, "0B11000000111001 33") == 0);
175 ASSERT (length == strlen (result));
176 free (result);
179 { /* FLAG_ALT with a positive number and width. */
180 size_t length;
181 char *result =
182 my_asnprintf (NULL, &length, "%#20B %d", 12345, 33, 44, 55);
183 ASSERT (result != NULL);
184 ASSERT (strcmp (result, " 0B11000000111001 33") == 0);
185 ASSERT (length == strlen (result));
186 free (result);
189 { /* FLAG_ALT with a positive number and padding. */
190 size_t length;
191 char *result =
192 my_asnprintf (NULL, &length, "%0#20B %d", 12345, 33, 44, 55);
193 ASSERT (result != NULL);
194 ASSERT (strcmp (result, "0B000011000000111001 33") == 0);
195 ASSERT (length == strlen (result));
196 free (result);
199 { /* FLAG_ALT with a positive number and precision. */
200 size_t length;
201 char *result =
202 my_asnprintf (NULL, &length, "%0#.20B %d", 12345, 33, 44, 55);
203 ASSERT (result != NULL);
204 ASSERT (strcmp (result, "0B00000011000000111001 33") == 0);
205 ASSERT (length == strlen (result));
206 free (result);
209 { /* FLAG_ALT with a positive number and width and precision. */
210 size_t length;
211 char *result =
212 my_asnprintf (NULL, &length, "%#25.20B %d", 12345, 33, 44, 55);
213 ASSERT (result != NULL);
214 ASSERT (strcmp (result, " 0B00000011000000111001 33") == 0);
215 ASSERT (length == strlen (result));
216 free (result);
219 { /* FLAG_ALT with a positive number and padding and precision. */
220 size_t length;
221 char *result =
222 my_asnprintf (NULL, &length, "%0#25.20B %d", 12345, 33, 44, 55);
223 ASSERT (result != NULL);
224 /* Neither ISO C nor POSIX specify that the '0' flag is ignored when
225 a width and a precision are both present. But implementations do so. */
226 ASSERT (strcmp (result, " 0B00000011000000111001 33") == 0);
227 ASSERT (length == strlen (result));
228 free (result);
231 { /* FLAG_ALT with a zero precision and a zero number. */
232 size_t length;
233 char *result =
234 my_asnprintf (NULL, &length, "%#.0B %d", 0, 33, 44, 55);
235 ASSERT (result != NULL);
236 /* ISO C and POSIX specify that "The result of converting a zero value
237 with a precision of zero is no characters.", and the prefix is added
238 only for non-zero values. */
239 ASSERT (strcmp (result, " 33") == 0);
240 ASSERT (length == strlen (result));
241 free (result);
245 static char *
246 my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
248 va_list args;
249 char *ret;
251 va_start (args, format);
252 ret = vasnprintf (resultbuf, lengthp, format, args);
253 va_end (args);
254 return ret;
257 static void
258 test_vasnprintf ()
260 test_function (my_asnprintf);
263 static void
264 test_asnprintf ()
266 test_function (asnprintf);
270 main (int argc, char *argv[])
272 test_vasnprintf ();
273 test_asnprintf ();
274 return test_exit_status;