usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-vasprintf-gnu.c
blob5b47d84a262c720551fe333a5e0e99d27e697958
1 /* Test of POSIX and GNU compatible vasprintf() and asprintf() 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 <stdio.h>
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "macros.h"
29 static void
30 test_function (int (*my_asprintf) (char **, const char *, ...))
32 /* Test the support of the 'b' conversion specifier for binary output of
33 integers. */
35 { /* Zero. */
36 char *result;
37 int retval =
38 my_asprintf (&result, "%b %d", 0, 33, 44, 55);
39 ASSERT (result != NULL);
40 ASSERT (strcmp (result, "0 33") == 0);
41 ASSERT (retval == strlen (result));
42 free (result);
45 { /* A positive number. */
46 char *result;
47 int retval =
48 my_asprintf (&result, "%b %d", 12345, 33, 44, 55);
49 ASSERT (result != NULL);
50 ASSERT (strcmp (result, "11000000111001 33") == 0);
51 ASSERT (retval == strlen (result));
52 free (result);
55 { /* A large positive number. */
56 char *result;
57 int retval =
58 my_asprintf (&result, "%b %d", 0xFFFFFFFEU, 33, 44, 55);
59 ASSERT (result != NULL);
60 ASSERT (strcmp (result, "11111111111111111111111111111110 33") == 0);
61 ASSERT (retval == strlen (result));
62 free (result);
65 { /* Width. */
66 char *result;
67 int retval =
68 my_asprintf (&result, "%20b %d", 12345, 33, 44, 55);
69 ASSERT (result != NULL);
70 ASSERT (strcmp (result, " 11000000111001 33") == 0);
71 ASSERT (retval == strlen (result));
72 free (result);
75 { /* Width given as argument. */
76 char *result;
77 int retval =
78 my_asprintf (&result, "%*b %d", 20, 12345, 33, 44, 55);
79 ASSERT (result != NULL);
80 ASSERT (strcmp (result, " 11000000111001 33") == 0);
81 ASSERT (retval == strlen (result));
82 free (result);
85 { /* Negative width given as argument (cf. FLAG_LEFT below). */
86 char *result;
87 int retval =
88 my_asprintf (&result, "%*b %d", -20, 12345, 33, 44, 55);
89 ASSERT (result != NULL);
90 ASSERT (strcmp (result, "11000000111001 33") == 0);
91 ASSERT (retval == strlen (result));
92 free (result);
95 { /* Precision. */
96 char *result;
97 int retval =
98 my_asprintf (&result, "%.20b %d", 12345, 33, 44, 55);
99 ASSERT (result != NULL);
100 ASSERT (strcmp (result, "00000011000000111001 33") == 0);
101 ASSERT (retval == strlen (result));
102 free (result);
105 { /* Zero precision and a positive number. */
106 char *result;
107 int retval =
108 my_asprintf (&result, "%.0b %d", 12345, 33, 44, 55);
109 ASSERT (result != NULL);
110 ASSERT (strcmp (result, "11000000111001 33") == 0);
111 ASSERT (retval == strlen (result));
112 free (result);
115 { /* Zero precision and a zero number. */
116 char *result;
117 int retval =
118 my_asprintf (&result, "%.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 (retval == strlen (result));
124 free (result);
127 { /* Width and precision. */
128 char *result;
129 int retval =
130 my_asprintf (&result, "%25.20b %d", 12345, 33, 44, 55);
131 ASSERT (result != NULL);
132 ASSERT (strcmp (result, " 00000011000000111001 33") == 0);
133 ASSERT (retval == strlen (result));
134 free (result);
137 { /* Padding and precision. */
138 char *result;
139 int retval =
140 my_asprintf (&result, "%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 (retval == strlen (result));
146 free (result);
149 { /* FLAG_LEFT. */
150 char *result;
151 int retval =
152 my_asprintf (&result, "%-20b %d", 12345, 33, 44, 55);
153 ASSERT (result != NULL);
154 ASSERT (strcmp (result, "11000000111001 33") == 0);
155 ASSERT (retval == strlen (result));
156 free (result);
159 { /* FLAG_ALT with zero. */
160 char *result;
161 int retval =
162 my_asprintf (&result, "%#b %d", 0, 33, 44, 55);
163 ASSERT (result != NULL);
164 ASSERT (strcmp (result, "0 33") == 0);
165 ASSERT (retval == strlen (result));
166 free (result);
169 { /* FLAG_ALT with a positive number. */
170 char *result;
171 int retval =
172 my_asprintf (&result, "%#b %d", 12345, 33, 44, 55);
173 ASSERT (result != NULL);
174 ASSERT (strcmp (result, "0b11000000111001 33") == 0);
175 ASSERT (retval == strlen (result));
176 free (result);
179 { /* FLAG_ALT with a positive number and width. */
180 char *result;
181 int retval =
182 my_asprintf (&result, "%#20b %d", 12345, 33, 44, 55);
183 ASSERT (result != NULL);
184 ASSERT (strcmp (result, " 0b11000000111001 33") == 0);
185 ASSERT (retval == strlen (result));
186 free (result);
189 { /* FLAG_ALT with a positive number and padding. */
190 char *result;
191 int retval =
192 my_asprintf (&result, "%0#20b %d", 12345, 33, 44, 55);
193 ASSERT (result != NULL);
194 ASSERT (strcmp (result, "0b000011000000111001 33") == 0);
195 ASSERT (retval == strlen (result));
196 free (result);
199 { /* FLAG_ALT with a positive number and precision. */
200 char *result;
201 int retval =
202 my_asprintf (&result, "%0#.20b %d", 12345, 33, 44, 55);
203 ASSERT (result != NULL);
204 ASSERT (strcmp (result, "0b00000011000000111001 33") == 0);
205 ASSERT (retval == strlen (result));
206 free (result);
209 { /* FLAG_ALT with a positive number and width and precision. */
210 char *result;
211 int retval =
212 my_asprintf (&result, "%#25.20b %d", 12345, 33, 44, 55);
213 ASSERT (result != NULL);
214 ASSERT (strcmp (result, " 0b00000011000000111001 33") == 0);
215 ASSERT (retval == strlen (result));
216 free (result);
219 { /* FLAG_ALT with a positive number and padding and precision. */
220 char *result;
221 int retval =
222 my_asprintf (&result, "%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 (retval == strlen (result));
228 free (result);
231 { /* FLAG_ALT with a zero precision and a zero number. */
232 char *result;
233 int retval =
234 my_asprintf (&result, "%#.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 (retval == strlen (result));
241 free (result);
245 static int
246 my_asprintf (char **result, const char *format, ...)
248 va_list args;
249 int ret;
251 va_start (args, format);
252 ret = vasprintf (result, format, args);
253 va_end (args);
254 return ret;
257 static void
258 test_vasprintf ()
260 test_function (my_asprintf);
263 static void
264 test_asprintf ()
266 test_function (asprintf);
270 main (int argc, char *argv[])
272 test_vasprintf ();
273 test_asprintf ();
274 return test_exit_status;