1 /* Test of POSIX and GNU compatible vasnwprintf() and asnwprintf() functions.
2 Copyright (C) 2007-2025 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 "vasnwprintf.h"
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
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
));
46 { /* A positive number. */
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
));
56 { /* A large positive number. */
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
));
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
));
76 { /* Width given as argument. */
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
));
86 { /* Negative width given as argument (cf. FLAG_LEFT below). */
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
));
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
));
106 { /* Zero precision and a positive number. */
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
));
116 { /* Zero precision and a zero number. */
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
));
128 { /* Width and precision. */
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
));
138 { /* Padding and precision. */
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
));
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
));
160 { /* FLAG_ALT with zero. */
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
));
170 { /* FLAG_ALT with a positive number. */
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
));
180 { /* FLAG_ALT with a positive number and width. */
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
));
190 { /* FLAG_ALT with a positive number and padding. */
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
));
200 { /* FLAG_ALT with a positive number and precision. */
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
));
210 { /* FLAG_ALT with a positive number and width and precision. */
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
));
220 { /* FLAG_ALT with a positive number and padding and precision. */
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
));
232 { /* FLAG_ALT with a zero precision and a zero number. */
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
));
247 my_asnwprintf (wchar_t *resultbuf
, size_t *lengthp
, const wchar_t *format
, ...)
252 va_start (args
, format
);
253 ret
= vasnwprintf (resultbuf
, lengthp
, format
, args
);
261 test_function (my_asnwprintf
);
267 test_function (asnwprintf
);
271 main (int argc
, char *argv
[])
275 return test_exit_status
;