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. */
21 #include "vasnprintf.h"
30 test_function (char * (*my_asnprintf
) (char *, size_t *, const char *, ...))
32 /* Test the support of the 'B' conversion specifier for binary output of
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
));
45 { /* A positive number. */
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
));
55 { /* A large positive number. */
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
));
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
));
75 { /* Width given as argument. */
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
));
85 { /* Negative width given as argument (cf. FLAG_LEFT below). */
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
));
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
));
105 { /* Zero precision and a positive number. */
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
));
115 { /* Zero precision and a zero number. */
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
));
127 { /* Width and precision. */
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
));
137 { /* Padding and precision. */
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
));
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
));
159 { /* FLAG_ALT with zero. */
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
));
169 { /* FLAG_ALT with a positive number. */
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
));
179 { /* FLAG_ALT with a positive number and width. */
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
));
189 { /* FLAG_ALT with a positive number and padding. */
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
));
199 { /* FLAG_ALT with a positive number and precision. */
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
));
209 { /* FLAG_ALT with a positive number and width and precision. */
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
));
219 { /* FLAG_ALT with a positive number and padding and precision. */
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
));
231 { /* FLAG_ALT with a zero precision and a zero number. */
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
));
246 my_asnprintf (char *resultbuf
, size_t *lengthp
, const char *format
, ...)
251 va_start (args
, format
);
252 ret
= vasnprintf (resultbuf
, lengthp
, format
, args
);
260 test_function (my_asnprintf
);
266 test_function (asnprintf
);
270 main (int argc
, char *argv
[])
274 return test_exit_status
;