usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-vasnwprintf-posix.c
blob9b1380d1c2b5b288aaad6cea3f8206506c2b5ecb
1 /* Test of POSIX 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>, 2007. */
19 #include <config.h>
21 #include "vasnwprintf.h"
23 #include <errno.h>
24 #include <float.h>
25 #include <stdarg.h>
26 #include <stddef.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <wchar.h>
32 #include "macros.h"
33 #include "minus-zero.h"
34 #include "infinity.h"
35 #include "nan.h"
36 #include "snan.h"
38 /* The SGI MIPS floating-point format does not distinguish 0.0 and -0.0. */
39 static int
40 have_minus_zero ()
42 static double plus_zero = 0.0;
43 double minus_zero = minus_zerod;
44 return memcmp (&plus_zero, &minus_zero, sizeof (double)) != 0;
47 /* Representation of an 80-bit 'long double' as an initializer for a sequence
48 of 'unsigned int' words. */
49 #ifdef WORDS_BIGENDIAN
50 # define LDBL80_WORDS(exponent,manthi,mantlo) \
51 { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \
52 ((unsigned int) (manthi) << 16) | ((unsigned int) (mantlo) >> 16), \
53 (unsigned int) (mantlo) << 16 \
55 #else
56 # define LDBL80_WORDS(exponent,manthi,mantlo) \
57 { mantlo, manthi, exponent }
58 #endif
60 static int
61 wcsmatch (const wchar_t *pattern, const wchar_t *string)
63 if (wcslen (pattern) != wcslen (string))
64 return 0;
65 for (; *pattern != L'\0'; pattern++, string++)
66 if (*pattern != L'*' && *string != *pattern)
67 return 0;
68 return 1;
71 /* Test whether string[start_index..end_index-1] is a valid textual
72 representation of NaN. */
73 static int
74 wcsisnan (const wchar_t *string, size_t start_index, size_t end_index, int uppercase)
76 if (start_index < end_index)
78 if (string[start_index] == L'-')
79 start_index++;
80 if (start_index + 3 <= end_index
81 && wmemcmp (string + start_index, uppercase ? L"NAN" : L"nan", 3) == 0)
83 start_index += 3;
84 if (start_index == end_index
85 || (string[start_index] == L'(' && string[end_index - 1] == L')'))
86 return 1;
89 return 0;
92 static void
93 test_function (wchar_t * (*my_asnwprintf) (wchar_t *, size_t *, const wchar_t *, ...))
95 wchar_t buf[8];
96 int size;
98 /* Test return value convention. */
100 for (size = 0; size <= 8; size++)
102 size_t length = size;
103 wchar_t *result = my_asnwprintf (NULL, &length, L"%d", 12345);
104 ASSERT (result != NULL);
105 ASSERT (wcscmp (result, L"12345") == 0);
106 ASSERT (length == 5);
107 free (result);
110 for (size = 0; size <= 8; size++)
112 size_t length;
113 wchar_t *result;
115 wmemcpy (buf, L"DEADBEEF", 8);
116 length = size;
117 result = my_asnwprintf (buf, &length, L"%d", 12345);
118 ASSERT (result != NULL);
119 ASSERT (wcscmp (result, L"12345") == 0);
120 ASSERT (length == 5);
121 if (size < 6)
122 ASSERT (result != buf);
123 ASSERT (wmemcmp (buf + size, &L"DEADBEEF"[size], 8 - size) == 0);
124 if (result != buf)
125 free (result);
128 /* Test support of size specifiers as in C99. */
131 size_t length;
132 wchar_t *result =
133 my_asnwprintf (NULL, &length, L"%ju %d", (uintmax_t) 12345671, 33, 44, 55);
134 ASSERT (result != NULL);
135 ASSERT (wcscmp (result, L"12345671 33") == 0);
136 ASSERT (length == wcslen (result));
137 free (result);
141 size_t length;
142 wchar_t *result =
143 my_asnwprintf (NULL, &length, L"%zu %d", (size_t) 12345672, 33, 44, 55);
144 ASSERT (result != NULL);
145 ASSERT (wcscmp (result, L"12345672 33") == 0);
146 ASSERT (length == wcslen (result));
147 free (result);
151 size_t length;
152 wchar_t *result =
153 my_asnwprintf (NULL, &length, L"%tu %d", (ptrdiff_t) 12345673, 33, 44, 55);
154 ASSERT (result != NULL);
155 ASSERT (wcscmp (result, L"12345673 33") == 0);
156 ASSERT (length == wcslen (result));
157 free (result);
161 size_t length;
162 wchar_t *result =
163 my_asnwprintf (NULL, &length, L"%Lg %d", (long double) 1.5, 33, 44, 55);
164 ASSERT (result != NULL);
165 ASSERT (wcscmp (result, L"1.5 33") == 0);
166 ASSERT (length == wcslen (result));
167 free (result);
170 /* Test the support of the 'a' and 'A' conversion specifier for hexadecimal
171 output of floating-point numbers. */
173 { /* A positive number. */
174 size_t length;
175 wchar_t *result =
176 my_asnwprintf (NULL, &length, L"%a %d", 3.1416015625, 33, 44, 55);
177 ASSERT (result != NULL);
178 ASSERT (wcscmp (result, L"0x1.922p+1 33") == 0
179 || wcscmp (result, L"0x3.244p+0 33") == 0
180 || wcscmp (result, L"0x6.488p-1 33") == 0
181 || wcscmp (result, L"0xc.91p-2 33") == 0);
182 ASSERT (length == wcslen (result));
183 free (result);
186 { /* A negative number. */
187 size_t length;
188 wchar_t *result =
189 my_asnwprintf (NULL, &length, L"%A %d", -3.1416015625, 33, 44, 55);
190 ASSERT (result != NULL);
191 ASSERT (wcscmp (result, L"-0X1.922P+1 33") == 0
192 || wcscmp (result, L"-0X3.244P+0 33") == 0
193 || wcscmp (result, L"-0X6.488P-1 33") == 0
194 || wcscmp (result, L"-0XC.91P-2 33") == 0);
195 ASSERT (length == wcslen (result));
196 free (result);
199 { /* Positive zero. */
200 size_t length;
201 wchar_t *result =
202 my_asnwprintf (NULL, &length, L"%a %d", 0.0, 33, 44, 55);
203 ASSERT (result != NULL);
204 ASSERT (wcscmp (result, L"0x0p+0 33") == 0);
205 ASSERT (length == wcslen (result));
206 free (result);
209 { /* Negative zero. */
210 size_t length;
211 wchar_t *result =
212 my_asnwprintf (NULL, &length, L"%a %d", minus_zerod, 33, 44, 55);
213 ASSERT (result != NULL);
214 if (have_minus_zero ())
215 ASSERT (wcscmp (result, L"-0x0p+0 33") == 0);
216 ASSERT (length == wcslen (result));
217 free (result);
220 { /* Positive infinity. */
221 size_t length;
222 wchar_t *result =
223 my_asnwprintf (NULL, &length, L"%a %d", Infinityd (), 33, 44, 55);
224 ASSERT (result != NULL);
225 ASSERT (wcscmp (result, L"inf 33") == 0);
226 ASSERT (length == wcslen (result));
227 free (result);
230 { /* Negative infinity. */
231 size_t length;
232 wchar_t *result =
233 my_asnwprintf (NULL, &length, L"%a %d", - Infinityd (), 33, 44, 55);
234 ASSERT (result != NULL);
235 ASSERT (wcscmp (result, L"-inf 33") == 0);
236 ASSERT (length == wcslen (result));
237 free (result);
240 { /* NaN. */
241 size_t length;
242 wchar_t *result =
243 my_asnwprintf (NULL, &length, L"%a %d", NaNd (), 33, 44, 55);
244 ASSERT (result != NULL);
245 ASSERT (wcslen (result) >= 3 + 3
246 && wcsisnan (result, 0, wcslen (result) - 3, 0)
247 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
248 ASSERT (length == wcslen (result));
249 free (result);
251 #if HAVE_SNAND
252 { /* Signalling NaN. */
253 size_t length;
254 wchar_t *result =
255 my_asnwprintf (NULL, &length, L"%a %d", SNaNd (), 33, 44, 55);
256 ASSERT (result != NULL);
257 ASSERT (wcslen (result) >= 3 + 3
258 && wcsisnan (result, 0, wcslen (result) - 3, 0)
259 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
260 ASSERT (length == wcslen (result));
261 free (result);
263 #endif
265 { /* Rounding near the decimal point. */
266 size_t length;
267 wchar_t *result =
268 my_asnwprintf (NULL, &length, L"%.0a %d", 1.5, 33, 44, 55);
269 ASSERT (result != NULL);
270 ASSERT (wcscmp (result, L"0x1p+0 33") == 0
271 || wcscmp (result, L"0x2p+0 33") == 0
272 || wcscmp (result, L"0x3p-1 33") == 0
273 || wcscmp (result, L"0x6p-2 33") == 0
274 || wcscmp (result, L"0xcp-3 33") == 0);
275 ASSERT (length == wcslen (result));
276 free (result);
279 { /* Rounding with precision 0. */
280 size_t length;
281 wchar_t *result =
282 my_asnwprintf (NULL, &length, L"%.0a %d", 1.51, 33, 44, 55);
283 ASSERT (result != NULL);
284 ASSERT (wcscmp (result, L"0x2p+0 33") == 0
285 || wcscmp (result, L"0x3p-1 33") == 0
286 || wcscmp (result, L"0x6p-2 33") == 0
287 || wcscmp (result, L"0xcp-3 33") == 0);
288 ASSERT (length == wcslen (result));
289 free (result);
292 { /* Rounding with precision 1. */
293 size_t length;
294 wchar_t *result =
295 my_asnwprintf (NULL, &length, L"%.1a %d", 1.51, 33, 44, 55);
296 ASSERT (result != NULL);
297 ASSERT (wcscmp (result, L"0x1.8p+0 33") == 0
298 || wcscmp (result, L"0x3.0p-1 33") == 0
299 || wcscmp (result, L"0x6.1p-2 33") == 0
300 || wcscmp (result, L"0xc.1p-3 33") == 0);
301 ASSERT (length == wcslen (result));
302 free (result);
305 { /* Rounding with precision 2. */
306 size_t length;
307 wchar_t *result =
308 my_asnwprintf (NULL, &length, L"%.2a %d", 1.51, 33, 44, 55);
309 ASSERT (result != NULL);
310 ASSERT (wcscmp (result, L"0x1.83p+0 33") == 0
311 || wcscmp (result, L"0x3.05p-1 33") == 0
312 || wcscmp (result, L"0x6.0ap-2 33") == 0
313 || wcscmp (result, L"0xc.14p-3 33") == 0);
314 ASSERT (length == wcslen (result));
315 free (result);
318 { /* Rounding with precision 3. */
319 size_t length;
320 wchar_t *result =
321 my_asnwprintf (NULL, &length, L"%.3a %d", 1.51, 33, 44, 55);
322 ASSERT (result != NULL);
323 ASSERT (wcscmp (result, L"0x1.829p+0 33") == 0
324 || wcscmp (result, L"0x3.052p-1 33") == 0
325 || wcscmp (result, L"0x6.0a4p-2 33") == 0
326 || wcscmp (result, L"0xc.148p-3 33") == 0);
327 ASSERT (length == wcslen (result));
328 free (result);
331 { /* Rounding can turn a ...FFF into a ...000. */
332 size_t length;
333 wchar_t *result =
334 my_asnwprintf (NULL, &length, L"%.3a %d", 1.49999, 33, 44, 55);
335 ASSERT (result != NULL);
336 ASSERT (wcscmp (result, L"0x1.800p+0 33") == 0
337 || wcscmp (result, L"0x3.000p-1 33") == 0
338 || wcscmp (result, L"0x6.000p-2 33") == 0
339 || wcscmp (result, L"0xc.000p-3 33") == 0);
340 ASSERT (length == wcslen (result));
341 free (result);
344 { /* Rounding can turn a ...FFF into a ...000.
345 This shows a Mac OS X 10.3.9 (Darwin 7.9) bug. */
346 size_t length;
347 wchar_t *result =
348 my_asnwprintf (NULL, &length, L"%.1a %d", 1.999, 33, 44, 55);
349 ASSERT (result != NULL);
350 ASSERT (wcscmp (result, L"0x1.0p+1 33") == 0
351 || wcscmp (result, L"0x2.0p+0 33") == 0
352 || wcscmp (result, L"0x4.0p-1 33") == 0
353 || wcscmp (result, L"0x8.0p-2 33") == 0);
354 ASSERT (length == wcslen (result));
355 free (result);
358 { /* Width. */
359 size_t length;
360 wchar_t *result =
361 my_asnwprintf (NULL, &length, L"%10a %d", 1.75, 33, 44, 55);
362 ASSERT (result != NULL);
363 ASSERT (wcscmp (result, L" 0x1.cp+0 33") == 0
364 || wcscmp (result, L" 0x3.8p-1 33") == 0
365 || wcscmp (result, L" 0x7p-2 33") == 0
366 || wcscmp (result, L" 0xep-3 33") == 0);
367 ASSERT (length == wcslen (result));
368 free (result);
371 { /* Width given as argument. */
372 size_t length;
373 wchar_t *result =
374 my_asnwprintf (NULL, &length, L"%*a %d", 10, 1.75, 33, 44, 55);
375 ASSERT (result != NULL);
376 ASSERT (wcscmp (result, L" 0x1.cp+0 33") == 0
377 || wcscmp (result, L" 0x3.8p-1 33") == 0
378 || wcscmp (result, L" 0x7p-2 33") == 0
379 || wcscmp (result, L" 0xep-3 33") == 0);
380 ASSERT (length == wcslen (result));
381 free (result);
384 { /* Negative width given as argument (cf. FLAG_LEFT below). */
385 size_t length;
386 wchar_t *result =
387 my_asnwprintf (NULL, &length, L"%*a %d", -10, 1.75, 33, 44, 55);
388 ASSERT (result != NULL);
389 ASSERT (wcscmp (result, L"0x1.cp+0 33") == 0
390 || wcscmp (result, L"0x3.8p-1 33") == 0
391 || wcscmp (result, L"0x7p-2 33") == 0
392 || wcscmp (result, L"0xep-3 33") == 0);
393 ASSERT (length == wcslen (result));
394 free (result);
397 { /* Small precision. */
398 size_t length;
399 wchar_t *result =
400 my_asnwprintf (NULL, &length, L"%.10a %d", 1.75, 33, 44, 55);
401 ASSERT (result != NULL);
402 ASSERT (wcscmp (result, L"0x1.c000000000p+0 33") == 0
403 || wcscmp (result, L"0x3.8000000000p-1 33") == 0
404 || wcscmp (result, L"0x7.0000000000p-2 33") == 0
405 || wcscmp (result, L"0xe.0000000000p-3 33") == 0);
406 ASSERT (length == wcslen (result));
407 free (result);
410 { /* Large precision. */
411 size_t length;
412 wchar_t *result =
413 my_asnwprintf (NULL, &length, L"%.50a %d", 1.75, 33, 44, 55);
414 ASSERT (result != NULL);
415 ASSERT (wcscmp (result, L"0x1.c0000000000000000000000000000000000000000000000000p+0 33") == 0
416 || wcscmp (result, L"0x3.80000000000000000000000000000000000000000000000000p-1 33") == 0
417 || wcscmp (result, L"0x7.00000000000000000000000000000000000000000000000000p-2 33") == 0
418 || wcscmp (result, L"0xe.00000000000000000000000000000000000000000000000000p-3 33") == 0);
419 ASSERT (length == wcslen (result));
420 free (result);
423 { /* FLAG_LEFT. */
424 size_t length;
425 wchar_t *result =
426 my_asnwprintf (NULL, &length, L"%-10a %d", 1.75, 33, 44, 55);
427 ASSERT (result != NULL);
428 ASSERT (wcscmp (result, L"0x1.cp+0 33") == 0
429 || wcscmp (result, L"0x3.8p-1 33") == 0
430 || wcscmp (result, L"0x7p-2 33") == 0
431 || wcscmp (result, L"0xep-3 33") == 0);
432 ASSERT (length == wcslen (result));
433 free (result);
436 { /* FLAG_SHOWSIGN. */
437 size_t length;
438 wchar_t *result =
439 my_asnwprintf (NULL, &length, L"%+a %d", 1.75, 33, 44, 55);
440 ASSERT (result != NULL);
441 ASSERT (wcscmp (result, L"+0x1.cp+0 33") == 0
442 || wcscmp (result, L"+0x3.8p-1 33") == 0
443 || wcscmp (result, L"+0x7p-2 33") == 0
444 || wcscmp (result, L"+0xep-3 33") == 0);
445 ASSERT (length == wcslen (result));
446 free (result);
449 { /* FLAG_SPACE. */
450 size_t length;
451 wchar_t *result =
452 my_asnwprintf (NULL, &length, L"% a %d", 1.75, 33, 44, 55);
453 ASSERT (result != NULL);
454 ASSERT (wcscmp (result, L" 0x1.cp+0 33") == 0
455 || wcscmp (result, L" 0x3.8p-1 33") == 0
456 || wcscmp (result, L" 0x7p-2 33") == 0
457 || wcscmp (result, L" 0xep-3 33") == 0);
458 ASSERT (length == wcslen (result));
459 free (result);
462 { /* FLAG_ALT. */
463 size_t length;
464 wchar_t *result =
465 my_asnwprintf (NULL, &length, L"%#a %d", 1.75, 33, 44, 55);
466 ASSERT (result != NULL);
467 ASSERT (wcscmp (result, L"0x1.cp+0 33") == 0
468 || wcscmp (result, L"0x3.8p-1 33") == 0
469 || wcscmp (result, L"0x7.p-2 33") == 0
470 || wcscmp (result, L"0xe.p-3 33") == 0);
471 ASSERT (length == wcslen (result));
472 free (result);
475 { /* FLAG_ALT. */
476 size_t length;
477 wchar_t *result =
478 my_asnwprintf (NULL, &length, L"%#a %d", 1.0, 33, 44, 55);
479 ASSERT (result != NULL);
480 ASSERT (wcscmp (result, L"0x1.p+0 33") == 0
481 || wcscmp (result, L"0x2.p-1 33") == 0
482 || wcscmp (result, L"0x4.p-2 33") == 0
483 || wcscmp (result, L"0x8.p-3 33") == 0);
484 ASSERT (length == wcslen (result));
485 free (result);
488 { /* FLAG_ZERO with finite number. */
489 size_t length;
490 wchar_t *result =
491 my_asnwprintf (NULL, &length, L"%010a %d", 1.75, 33, 44, 55);
492 ASSERT (result != NULL);
493 ASSERT (wcscmp (result, L"0x001.cp+0 33") == 0
494 || wcscmp (result, L"0x003.8p-1 33") == 0
495 || wcscmp (result, L"0x00007p-2 33") == 0
496 || wcscmp (result, L"0x0000ep-3 33") == 0);
497 ASSERT (length == wcslen (result));
498 free (result);
501 { /* FLAG_ZERO with infinite number. */
502 size_t length;
503 wchar_t *result =
504 my_asnwprintf (NULL, &length, L"%010a %d", Infinityd (), 33, 44, 55);
505 ASSERT (result != NULL);
506 /* "0000000inf 33" is not a valid result; see
507 <https://lists.gnu.org/r/bug-gnulib/2007-04/msg00107.html> */
508 ASSERT (wcscmp (result, L" inf 33") == 0);
509 ASSERT (length == wcslen (result));
510 free (result);
513 { /* FLAG_ZERO with NaN. */
514 size_t length;
515 wchar_t *result =
516 my_asnwprintf (NULL, &length, L"%050a %d", NaNd (), 33, 44, 55);
517 ASSERT (result != NULL);
518 /* "0000000nan 33" is not a valid result; see
519 <https://lists.gnu.org/r/bug-gnulib/2007-04/msg00107.html> */
520 ASSERT (wcslen (result) == 50 + 3
521 && wcsisnan (result, wcsspn (result, L" "), wcslen (result) - 3, 0)
522 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
523 ASSERT (length == wcslen (result));
524 free (result);
527 { /* A positive number. */
528 size_t length;
529 wchar_t *result =
530 my_asnwprintf (NULL, &length, L"%La %d", 3.1416015625L, 33, 44, 55);
531 ASSERT (result != NULL);
532 ASSERT (wcscmp (result, L"0x1.922p+1 33") == 0
533 || wcscmp (result, L"0x3.244p+0 33") == 0
534 || wcscmp (result, L"0x6.488p-1 33") == 0
535 || wcscmp (result, L"0xc.91p-2 33") == 0);
536 ASSERT (length == wcslen (result));
537 free (result);
540 { /* A negative number. */
541 size_t length;
542 wchar_t *result =
543 my_asnwprintf (NULL, &length, L"%LA %d", -3.1416015625L, 33, 44, 55);
544 ASSERT (result != NULL);
545 ASSERT (wcscmp (result, L"-0X1.922P+1 33") == 0
546 || wcscmp (result, L"-0X3.244P+0 33") == 0
547 || wcscmp (result, L"-0X6.488P-1 33") == 0
548 || wcscmp (result, L"-0XC.91P-2 33") == 0);
549 ASSERT (length == wcslen (result));
550 free (result);
553 { /* Positive zero. */
554 size_t length;
555 wchar_t *result =
556 my_asnwprintf (NULL, &length, L"%La %d", 0.0L, 33, 44, 55);
557 ASSERT (result != NULL);
558 ASSERT (wcscmp (result, L"0x0p+0 33") == 0);
559 ASSERT (length == wcslen (result));
560 free (result);
563 { /* Negative zero. */
564 size_t length;
565 wchar_t *result =
566 my_asnwprintf (NULL, &length, L"%La %d", minus_zerol, 33, 44, 55);
567 ASSERT (result != NULL);
568 if (have_minus_zero ())
569 ASSERT (wcscmp (result, L"-0x0p+0 33") == 0);
570 ASSERT (length == wcslen (result));
571 free (result);
574 { /* Positive infinity. */
575 size_t length;
576 wchar_t *result =
577 my_asnwprintf (NULL, &length, L"%La %d", Infinityl (), 33, 44, 55);
578 ASSERT (result != NULL);
579 /* Note: This assertion fails under valgrind.
580 Reported at <https://bugs.kde.org/show_bug.cgi?id=424044>. */
581 ASSERT (wcscmp (result, L"inf 33") == 0);
582 ASSERT (length == wcslen (result));
583 free (result);
586 { /* Negative infinity. */
587 size_t length;
588 wchar_t *result =
589 my_asnwprintf (NULL, &length, L"%La %d", - Infinityl (), 33, 44, 55);
590 ASSERT (result != NULL);
591 ASSERT (wcscmp (result, L"-inf 33") == 0);
592 ASSERT (length == wcslen (result));
593 free (result);
596 { /* NaN. */
597 size_t length;
598 wchar_t *result =
599 my_asnwprintf (NULL, &length, L"%La %d", NaNl (), 33, 44, 55);
600 ASSERT (result != NULL);
601 ASSERT (wcslen (result) >= 3 + 3
602 && wcsisnan (result, 0, wcslen (result) - 3, 0)
603 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
604 ASSERT (length == wcslen (result));
605 free (result);
607 #if HAVE_SNANL
608 { /* Signalling NaN. */
609 size_t length;
610 wchar_t *result =
611 my_asnwprintf (NULL, &length, L"%La %d", SNaNl (), 33, 44, 55);
612 ASSERT (result != NULL);
613 ASSERT (wcslen (result) >= 3 + 3
614 && wcsisnan (result, 0, wcslen (result) - 3, 0)
615 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
616 ASSERT (length == wcslen (result));
617 free (result);
619 #endif
620 #if CHECK_PRINTF_SAFE && ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_)) && !HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
621 { /* Quiet NaN. */
622 static union { unsigned int word[4]; long double value; } x =
623 { .word = LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
624 size_t length;
625 wchar_t *result =
626 my_asnwprintf (NULL, &length, L"%La %d", x.value, 33, 44, 55);
627 ASSERT (result != NULL);
628 ASSERT (wcslen (result) >= 3 + 3
629 && wcsisnan (result, 0, wcslen (result) - 3, 0)
630 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
631 ASSERT (length == wcslen (result));
632 free (result);
635 /* Signalling NaN. */
636 static union { unsigned int word[4]; long double value; } x =
637 { .word = LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
638 size_t length;
639 wchar_t *result =
640 my_asnwprintf (NULL, &length, L"%La %d", x.value, 33, 44, 55);
641 ASSERT (result != NULL);
642 ASSERT (wcslen (result) >= 3 + 3
643 && wcsisnan (result, 0, wcslen (result) - 3, 0)
644 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
645 ASSERT (length == wcslen (result));
646 free (result);
648 /* asnwprintf should print something for noncanonical values. */
649 { /* Pseudo-NaN. */
650 static union { unsigned int word[4]; long double value; } x =
651 { .word = LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
652 size_t length;
653 wchar_t *result =
654 my_asnwprintf (NULL, &length, L"%La %d", x.value, 33, 44, 55);
655 ASSERT (result != NULL);
656 ASSERT (length == wcslen (result));
657 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
658 free (result);
660 { /* Pseudo-Infinity. */
661 static union { unsigned int word[4]; long double value; } x =
662 { .word = LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
663 size_t length;
664 wchar_t *result =
665 my_asnwprintf (NULL, &length, L"%La %d", x.value, 33, 44, 55);
666 ASSERT (result != NULL);
667 ASSERT (length == wcslen (result));
668 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
669 free (result);
671 { /* Pseudo-Zero. */
672 static union { unsigned int word[4]; long double value; } x =
673 { .word = LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
674 size_t length;
675 wchar_t *result =
676 my_asnwprintf (NULL, &length, L"%La %d", x.value, 33, 44, 55);
677 ASSERT (result != NULL);
678 ASSERT (length == wcslen (result));
679 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
680 free (result);
682 { /* Unnormalized number. */
683 static union { unsigned int word[4]; long double value; } x =
684 { .word = LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
685 size_t length;
686 wchar_t *result =
687 my_asnwprintf (NULL, &length, L"%La %d", x.value, 33, 44, 55);
688 ASSERT (result != NULL);
689 ASSERT (length == wcslen (result));
690 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
691 free (result);
693 { /* Pseudo-Denormal. */
694 static union { unsigned int word[4]; long double value; } x =
695 { .word = LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
696 size_t length;
697 wchar_t *result =
698 my_asnwprintf (NULL, &length, L"%La %d", x.value, 33, 44, 55);
699 ASSERT (result != NULL);
700 ASSERT (length == wcslen (result));
701 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
702 free (result);
704 #endif
706 { /* Rounding near the decimal point. */
707 size_t length;
708 wchar_t *result =
709 my_asnwprintf (NULL, &length, L"%.0La %d", 1.5L, 33, 44, 55);
710 ASSERT (result != NULL);
711 ASSERT (wcscmp (result, L"0x2p+0 33") == 0
712 || wcscmp (result, L"0x3p-1 33") == 0
713 || wcscmp (result, L"0x6p-2 33") == 0
714 || wcscmp (result, L"0xcp-3 33") == 0);
715 ASSERT (length == wcslen (result));
716 free (result);
719 { /* Rounding with precision 0. */
720 size_t length;
721 wchar_t *result =
722 my_asnwprintf (NULL, &length, L"%.0La %d", 1.51L, 33, 44, 55);
723 ASSERT (result != NULL);
724 ASSERT (wcscmp (result, L"0x2p+0 33") == 0
725 || wcscmp (result, L"0x3p-1 33") == 0
726 || wcscmp (result, L"0x6p-2 33") == 0
727 || wcscmp (result, L"0xcp-3 33") == 0);
728 ASSERT (length == wcslen (result));
729 free (result);
732 { /* Rounding with precision 1. */
733 size_t length;
734 wchar_t *result =
735 my_asnwprintf (NULL, &length, L"%.1La %d", 1.51L, 33, 44, 55);
736 ASSERT (result != NULL);
737 ASSERT (wcscmp (result, L"0x1.8p+0 33") == 0
738 || wcscmp (result, L"0x3.0p-1 33") == 0
739 || wcscmp (result, L"0x6.1p-2 33") == 0
740 || wcscmp (result, L"0xc.1p-3 33") == 0);
741 ASSERT (length == wcslen (result));
742 free (result);
745 { /* Rounding with precision 2. */
746 size_t length;
747 wchar_t *result =
748 my_asnwprintf (NULL, &length, L"%.2La %d", 1.51L, 33, 44, 55);
749 ASSERT (result != NULL);
750 ASSERT (wcscmp (result, L"0x1.83p+0 33") == 0
751 || wcscmp (result, L"0x3.05p-1 33") == 0
752 || wcscmp (result, L"0x6.0ap-2 33") == 0
753 || wcscmp (result, L"0xc.14p-3 33") == 0);
754 ASSERT (length == wcslen (result));
755 free (result);
758 { /* Rounding with precision 3. */
759 size_t length;
760 wchar_t *result =
761 my_asnwprintf (NULL, &length, L"%.3La %d", 1.51L, 33, 44, 55);
762 ASSERT (result != NULL);
763 ASSERT (wcscmp (result, L"0x1.829p+0 33") == 0
764 || wcscmp (result, L"0x3.052p-1 33") == 0
765 || wcscmp (result, L"0x6.0a4p-2 33") == 0
766 || wcscmp (result, L"0xc.148p-3 33") == 0);
767 ASSERT (length == wcslen (result));
768 free (result);
771 { /* Rounding can turn a ...FFF into a ...000. */
772 size_t length;
773 wchar_t *result =
774 my_asnwprintf (NULL, &length, L"%.3La %d", 1.49999L, 33, 44, 55);
775 ASSERT (result != NULL);
776 ASSERT (wcscmp (result, L"0x1.800p+0 33") == 0
777 || wcscmp (result, L"0x3.000p-1 33") == 0
778 || wcscmp (result, L"0x6.000p-2 33") == 0
779 || wcscmp (result, L"0xc.000p-3 33") == 0);
780 ASSERT (length == wcslen (result));
781 free (result);
784 { /* Rounding can turn a ...FFF into a ...000.
785 This shows a Mac OS X 10.3.9 (Darwin 7.9) bug and a
786 glibc 2.4 bug <https://sourceware.org/bugzilla/show_bug.cgi?id=2908>. */
787 size_t length;
788 wchar_t *result =
789 my_asnwprintf (NULL, &length, L"%.1La %d", 1.999L, 33, 44, 55);
790 ASSERT (result != NULL);
791 ASSERT (wcscmp (result, L"0x1.0p+1 33") == 0
792 || wcscmp (result, L"0x2.0p+0 33") == 0
793 || wcscmp (result, L"0x4.0p-1 33") == 0
794 || wcscmp (result, L"0x8.0p-2 33") == 0);
795 ASSERT (length == wcslen (result));
796 free (result);
799 { /* Width. */
800 size_t length;
801 wchar_t *result =
802 my_asnwprintf (NULL, &length, L"%10La %d", 1.75L, 33, 44, 55);
803 ASSERT (result != NULL);
804 ASSERT (wcscmp (result, L" 0x1.cp+0 33") == 0
805 || wcscmp (result, L" 0x3.8p-1 33") == 0
806 || wcscmp (result, L" 0x7p-2 33") == 0
807 || wcscmp (result, L" 0xep-3 33") == 0);
808 ASSERT (length == wcslen (result));
809 free (result);
812 { /* Width given as argument. */
813 size_t length;
814 wchar_t *result =
815 my_asnwprintf (NULL, &length, L"%*La %d", 10, 1.75L, 33, 44, 55);
816 ASSERT (result != NULL);
817 ASSERT (wcscmp (result, L" 0x1.cp+0 33") == 0
818 || wcscmp (result, L" 0x3.8p-1 33") == 0
819 || wcscmp (result, L" 0x7p-2 33") == 0
820 || wcscmp (result, L" 0xep-3 33") == 0);
821 ASSERT (length == wcslen (result));
822 free (result);
825 { /* Negative width given as argument (cf. FLAG_LEFT below). */
826 size_t length;
827 wchar_t *result =
828 my_asnwprintf (NULL, &length, L"%*La %d", -10, 1.75L, 33, 44, 55);
829 ASSERT (result != NULL);
830 ASSERT (wcscmp (result, L"0x1.cp+0 33") == 0
831 || wcscmp (result, L"0x3.8p-1 33") == 0
832 || wcscmp (result, L"0x7p-2 33") == 0
833 || wcscmp (result, L"0xep-3 33") == 0);
834 ASSERT (length == wcslen (result));
835 free (result);
838 { /* Small precision. */
839 size_t length;
840 wchar_t *result =
841 my_asnwprintf (NULL, &length, L"%.10La %d", 1.75L, 33, 44, 55);
842 ASSERT (result != NULL);
843 ASSERT (wcscmp (result, L"0x1.c000000000p+0 33") == 0
844 || wcscmp (result, L"0x3.8000000000p-1 33") == 0
845 || wcscmp (result, L"0x7.0000000000p-2 33") == 0
846 || wcscmp (result, L"0xe.0000000000p-3 33") == 0);
847 ASSERT (length == wcslen (result));
848 free (result);
851 { /* Large precision. */
852 size_t length;
853 wchar_t *result =
854 my_asnwprintf (NULL, &length, L"%.50La %d", 1.75L, 33, 44, 55);
855 ASSERT (result != NULL);
856 ASSERT (wcscmp (result, L"0x1.c0000000000000000000000000000000000000000000000000p+0 33") == 0
857 || wcscmp (result, L"0x3.80000000000000000000000000000000000000000000000000p-1 33") == 0
858 || wcscmp (result, L"0x7.00000000000000000000000000000000000000000000000000p-2 33") == 0
859 || wcscmp (result, L"0xe.00000000000000000000000000000000000000000000000000p-3 33") == 0);
860 ASSERT (length == wcslen (result));
861 free (result);
864 { /* FLAG_LEFT. */
865 size_t length;
866 wchar_t *result =
867 my_asnwprintf (NULL, &length, L"%-10La %d", 1.75L, 33, 44, 55);
868 ASSERT (result != NULL);
869 ASSERT (wcscmp (result, L"0x1.cp+0 33") == 0
870 || wcscmp (result, L"0x3.8p-1 33") == 0
871 || wcscmp (result, L"0x7p-2 33") == 0
872 || wcscmp (result, L"0xep-3 33") == 0);
873 ASSERT (length == wcslen (result));
874 free (result);
877 { /* FLAG_SHOWSIGN. */
878 size_t length;
879 wchar_t *result =
880 my_asnwprintf (NULL, &length, L"%+La %d", 1.75L, 33, 44, 55);
881 ASSERT (result != NULL);
882 ASSERT (wcscmp (result, L"+0x1.cp+0 33") == 0
883 || wcscmp (result, L"+0x3.8p-1 33") == 0
884 || wcscmp (result, L"+0x7p-2 33") == 0
885 || wcscmp (result, L"+0xep-3 33") == 0);
886 ASSERT (length == wcslen (result));
887 free (result);
890 { /* FLAG_SPACE. */
891 size_t length;
892 wchar_t *result =
893 my_asnwprintf (NULL, &length, L"% La %d", 1.75L, 33, 44, 55);
894 ASSERT (result != NULL);
895 ASSERT (wcscmp (result, L" 0x1.cp+0 33") == 0
896 || wcscmp (result, L" 0x3.8p-1 33") == 0
897 || wcscmp (result, L" 0x7p-2 33") == 0
898 || wcscmp (result, L" 0xep-3 33") == 0);
899 ASSERT (length == wcslen (result));
900 free (result);
903 { /* FLAG_ALT. */
904 size_t length;
905 wchar_t *result =
906 my_asnwprintf (NULL, &length, L"%#La %d", 1.75L, 33, 44, 55);
907 ASSERT (result != NULL);
908 ASSERT (wcscmp (result, L"0x1.cp+0 33") == 0
909 || wcscmp (result, L"0x3.8p-1 33") == 0
910 || wcscmp (result, L"0x7.p-2 33") == 0
911 || wcscmp (result, L"0xe.p-3 33") == 0);
912 ASSERT (length == wcslen (result));
913 free (result);
916 { /* FLAG_ALT. */
917 size_t length;
918 wchar_t *result =
919 my_asnwprintf (NULL, &length, L"%#La %d", 1.0L, 33, 44, 55);
920 ASSERT (result != NULL);
921 ASSERT (wcscmp (result, L"0x1.p+0 33") == 0
922 || wcscmp (result, L"0x2.p-1 33") == 0
923 || wcscmp (result, L"0x4.p-2 33") == 0
924 || wcscmp (result, L"0x8.p-3 33") == 0);
925 ASSERT (length == wcslen (result));
926 free (result);
929 { /* FLAG_ZERO with finite number. */
930 size_t length;
931 wchar_t *result =
932 my_asnwprintf (NULL, &length, L"%010La %d", 1.75L, 33, 44, 55);
933 ASSERT (result != NULL);
934 ASSERT (wcscmp (result, L"0x001.cp+0 33") == 0
935 || wcscmp (result, L"0x003.8p-1 33") == 0
936 || wcscmp (result, L"0x00007p-2 33") == 0
937 || wcscmp (result, L"0x0000ep-3 33") == 0);
938 ASSERT (length == wcslen (result));
939 free (result);
942 { /* FLAG_ZERO with infinite number. */
943 size_t length;
944 wchar_t *result =
945 my_asnwprintf (NULL, &length, L"%010La %d", Infinityl (), 33, 44, 55);
946 ASSERT (result != NULL);
947 /* "0000000inf 33" is not a valid result; see
948 <https://lists.gnu.org/r/bug-gnulib/2007-04/msg00107.html> */
949 ASSERT (wcscmp (result, L" inf 33") == 0);
950 ASSERT (length == wcslen (result));
951 free (result);
954 { /* FLAG_ZERO with NaN. */
955 size_t length;
956 wchar_t *result =
957 my_asnwprintf (NULL, &length, L"%050La %d", NaNl (), 33, 44, 55);
958 ASSERT (result != NULL);
959 /* "0000000nan 33" is not a valid result; see
960 <https://lists.gnu.org/r/bug-gnulib/2007-04/msg00107.html> */
961 ASSERT (wcslen (result) == 50 + 3
962 && wcsisnan (result, wcsspn (result, L" "), wcslen (result) - 3, 0)
963 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
964 ASSERT (length == wcslen (result));
965 free (result);
968 /* Test the support of the %f format directive. */
970 { /* A positive number. */
971 size_t length;
972 wchar_t *result =
973 my_asnwprintf (NULL, &length, L"%f %d", 12.75, 33, 44, 55);
974 ASSERT (result != NULL);
975 ASSERT (wcscmp (result, L"12.750000 33") == 0);
976 ASSERT (length == wcslen (result));
977 free (result);
980 { /* A larger positive number. */
981 size_t length;
982 wchar_t *result =
983 my_asnwprintf (NULL, &length, L"%f %d", 1234567.0, 33, 44, 55);
984 ASSERT (result != NULL);
985 ASSERT (wcscmp (result, L"1234567.000000 33") == 0);
986 ASSERT (length == wcslen (result));
987 free (result);
990 { /* Small and large positive numbers. */
991 static struct { double value; const wchar_t *string; } data[] =
993 { 1.234321234321234e-37, L"0.000000" },
994 { 1.234321234321234e-36, L"0.000000" },
995 { 1.234321234321234e-35, L"0.000000" },
996 { 1.234321234321234e-34, L"0.000000" },
997 { 1.234321234321234e-33, L"0.000000" },
998 { 1.234321234321234e-32, L"0.000000" },
999 { 1.234321234321234e-31, L"0.000000" },
1000 { 1.234321234321234e-30, L"0.000000" },
1001 { 1.234321234321234e-29, L"0.000000" },
1002 { 1.234321234321234e-28, L"0.000000" },
1003 { 1.234321234321234e-27, L"0.000000" },
1004 { 1.234321234321234e-26, L"0.000000" },
1005 { 1.234321234321234e-25, L"0.000000" },
1006 { 1.234321234321234e-24, L"0.000000" },
1007 { 1.234321234321234e-23, L"0.000000" },
1008 { 1.234321234321234e-22, L"0.000000" },
1009 { 1.234321234321234e-21, L"0.000000" },
1010 { 1.234321234321234e-20, L"0.000000" },
1011 { 1.234321234321234e-19, L"0.000000" },
1012 { 1.234321234321234e-18, L"0.000000" },
1013 { 1.234321234321234e-17, L"0.000000" },
1014 { 1.234321234321234e-16, L"0.000000" },
1015 { 1.234321234321234e-15, L"0.000000" },
1016 { 1.234321234321234e-14, L"0.000000" },
1017 { 1.234321234321234e-13, L"0.000000" },
1018 { 1.234321234321234e-12, L"0.000000" },
1019 { 1.234321234321234e-11, L"0.000000" },
1020 { 1.234321234321234e-10, L"0.000000" },
1021 { 1.234321234321234e-9, L"0.000000" },
1022 { 1.234321234321234e-8, L"0.000000" },
1023 { 1.234321234321234e-7, L"0.000000" },
1024 { 1.234321234321234e-6, L"0.000001" },
1025 { 1.234321234321234e-5, L"0.000012" },
1026 { 1.234321234321234e-4, L"0.000123" },
1027 { 1.234321234321234e-3, L"0.001234" },
1028 { 1.234321234321234e-2, L"0.012343" },
1029 { 1.234321234321234e-1, L"0.123432" },
1030 { 1.234321234321234, L"1.234321" },
1031 { 1.234321234321234e1, L"12.343212" },
1032 { 1.234321234321234e2, L"123.432123" },
1033 { 1.234321234321234e3, L"1234.321234" },
1034 { 1.234321234321234e4, L"12343.212343" },
1035 { 1.234321234321234e5, L"123432.123432" },
1036 { 1.234321234321234e6, L"1234321.234321" },
1037 { 1.234321234321234e7, L"12343212.343212" },
1038 { 1.234321234321234e8, L"123432123.432123" },
1039 { 1.234321234321234e9, L"1234321234.321234" },
1040 { 1.234321234321234e10, L"12343212343.2123**" },
1041 { 1.234321234321234e11, L"123432123432.123***" },
1042 { 1.234321234321234e12, L"1234321234321.23****" },
1043 { 1.234321234321234e13, L"12343212343212.3*****" },
1044 { 1.234321234321234e14, L"123432123432123.******" },
1045 { 1.234321234321234e15, L"1234321234321234.000000" },
1046 { 1.234321234321234e16, L"123432123432123**.000000" },
1047 { 1.234321234321234e17, L"123432123432123***.000000" },
1048 { 1.234321234321234e18, L"123432123432123****.000000" },
1049 { 1.234321234321234e19, L"123432123432123*****.000000" },
1050 { 1.234321234321234e20, L"123432123432123******.000000" },
1051 { 1.234321234321234e21, L"123432123432123*******.000000" },
1052 { 1.234321234321234e22, L"123432123432123********.000000" },
1053 { 1.234321234321234e23, L"123432123432123*********.000000" },
1054 { 1.234321234321234e24, L"123432123432123**********.000000" },
1055 { 1.234321234321234e25, L"123432123432123***********.000000" },
1056 { 1.234321234321234e26, L"123432123432123************.000000" },
1057 { 1.234321234321234e27, L"123432123432123*************.000000" },
1058 { 1.234321234321234e28, L"123432123432123**************.000000" },
1059 { 1.234321234321234e29, L"123432123432123***************.000000" },
1060 { 1.234321234321234e30, L"123432123432123****************.000000" },
1061 { 1.234321234321234e31, L"123432123432123*****************.000000" },
1062 { 1.234321234321234e32, L"123432123432123******************.000000" },
1063 { 1.234321234321234e33, L"123432123432123*******************.000000" },
1064 { 1.234321234321234e34, L"123432123432123********************.000000" },
1065 { 1.234321234321234e35, L"123432123432123*********************.000000" },
1066 { 1.234321234321234e36, L"123432123432123**********************.000000" }
1068 size_t k;
1069 for (k = 0; k < SIZEOF (data); k++)
1071 size_t length;
1072 wchar_t *result =
1073 my_asnwprintf (NULL, &length, L"%f", data[k].value);
1074 ASSERT (result != NULL);
1075 ASSERT (wcsmatch (data[k].string, result));
1076 ASSERT (length == wcslen (result));
1077 free (result);
1081 { /* A negative number. */
1082 size_t length;
1083 wchar_t *result =
1084 my_asnwprintf (NULL, &length, L"%f %d", -0.03125, 33, 44, 55);
1085 ASSERT (result != NULL);
1086 ASSERT (wcscmp (result, L"-0.031250 33") == 0);
1087 ASSERT (length == wcslen (result));
1088 free (result);
1091 { /* Positive zero. */
1092 size_t length;
1093 wchar_t *result =
1094 my_asnwprintf (NULL, &length, L"%f %d", 0.0, 33, 44, 55);
1095 ASSERT (result != NULL);
1096 ASSERT (wcscmp (result, L"0.000000 33") == 0);
1097 ASSERT (length == wcslen (result));
1098 free (result);
1101 { /* Negative zero. */
1102 size_t length;
1103 wchar_t *result =
1104 my_asnwprintf (NULL, &length, L"%f %d", minus_zerod, 33, 44, 55);
1105 ASSERT (result != NULL);
1106 if (have_minus_zero ())
1107 ASSERT (wcscmp (result, L"-0.000000 33") == 0);
1108 ASSERT (length == wcslen (result));
1109 free (result);
1112 { /* Positive infinity. */
1113 size_t length;
1114 wchar_t *result =
1115 my_asnwprintf (NULL, &length, L"%f %d", Infinityd (), 33, 44, 55);
1116 ASSERT (result != NULL);
1117 ASSERT (wcscmp (result, L"inf 33") == 0
1118 || wcscmp (result, L"infinity 33") == 0);
1119 ASSERT (length == wcslen (result));
1120 free (result);
1123 { /* Negative infinity. */
1124 size_t length;
1125 wchar_t *result =
1126 my_asnwprintf (NULL, &length, L"%f %d", - Infinityd (), 33, 44, 55);
1127 ASSERT (result != NULL);
1128 ASSERT (wcscmp (result, L"-inf 33") == 0
1129 || wcscmp (result, L"-infinity 33") == 0);
1130 ASSERT (length == wcslen (result));
1131 free (result);
1134 { /* NaN. */
1135 size_t length;
1136 wchar_t *result =
1137 my_asnwprintf (NULL, &length, L"%f %d", NaNd (), 33, 44, 55);
1138 ASSERT (result != NULL);
1139 ASSERT (wcslen (result) >= 3 + 3
1140 && wcsisnan (result, 0, wcslen (result) - 3, 0)
1141 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
1142 ASSERT (length == wcslen (result));
1143 free (result);
1145 #if HAVE_SNAND
1146 { /* Signalling NaN. */
1147 size_t length;
1148 wchar_t *result =
1149 my_asnwprintf (NULL, &length, L"%f %d", SNaNd (), 33, 44, 55);
1150 ASSERT (result != NULL);
1151 ASSERT (wcslen (result) >= 3 + 3
1152 && wcsisnan (result, 0, wcslen (result) - 3, 0)
1153 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
1154 ASSERT (length == wcslen (result));
1155 free (result);
1157 #endif
1159 { /* Width. */
1160 size_t length;
1161 wchar_t *result =
1162 my_asnwprintf (NULL, &length, L"%10f %d", 1.75, 33, 44, 55);
1163 ASSERT (result != NULL);
1164 ASSERT (wcscmp (result, L" 1.750000 33") == 0);
1165 ASSERT (length == wcslen (result));
1166 free (result);
1169 { /* Width given as argument. */
1170 size_t length;
1171 wchar_t *result =
1172 my_asnwprintf (NULL, &length, L"%*f %d", 10, 1.75, 33, 44, 55);
1173 ASSERT (result != NULL);
1174 ASSERT (wcscmp (result, L" 1.750000 33") == 0);
1175 ASSERT (length == wcslen (result));
1176 free (result);
1179 { /* Negative width given as argument (cf. FLAG_LEFT below). */
1180 size_t length;
1181 wchar_t *result =
1182 my_asnwprintf (NULL, &length, L"%*f %d", -10, 1.75, 33, 44, 55);
1183 ASSERT (result != NULL);
1184 ASSERT (wcscmp (result, L"1.750000 33") == 0);
1185 ASSERT (length == wcslen (result));
1186 free (result);
1189 { /* FLAG_LEFT. */
1190 size_t length;
1191 wchar_t *result =
1192 my_asnwprintf (NULL, &length, L"%-10f %d", 1.75, 33, 44, 55);
1193 ASSERT (result != NULL);
1194 ASSERT (wcscmp (result, L"1.750000 33") == 0);
1195 ASSERT (length == wcslen (result));
1196 free (result);
1199 { /* FLAG_SHOWSIGN. */
1200 size_t length;
1201 wchar_t *result =
1202 my_asnwprintf (NULL, &length, L"%+f %d", 1.75, 33, 44, 55);
1203 ASSERT (result != NULL);
1204 ASSERT (wcscmp (result, L"+1.750000 33") == 0);
1205 ASSERT (length == wcslen (result));
1206 free (result);
1209 { /* FLAG_SPACE. */
1210 size_t length;
1211 wchar_t *result =
1212 my_asnwprintf (NULL, &length, L"% f %d", 1.75, 33, 44, 55);
1213 ASSERT (result != NULL);
1214 ASSERT (wcscmp (result, L" 1.750000 33") == 0);
1215 ASSERT (length == wcslen (result));
1216 free (result);
1219 { /* FLAG_ALT. */
1220 size_t length;
1221 wchar_t *result =
1222 my_asnwprintf (NULL, &length, L"%#f %d", 1.75, 33, 44, 55);
1223 ASSERT (result != NULL);
1224 ASSERT (wcscmp (result, L"1.750000 33") == 0);
1225 ASSERT (length == wcslen (result));
1226 free (result);
1229 { /* FLAG_ALT. */
1230 size_t length;
1231 wchar_t *result =
1232 my_asnwprintf (NULL, &length, L"%#.f %d", 1.75, 33, 44, 55);
1233 ASSERT (result != NULL);
1234 ASSERT (wcscmp (result, L"2. 33") == 0);
1235 ASSERT (length == wcslen (result));
1236 free (result);
1239 { /* FLAG_ZERO with finite number. */
1240 size_t length;
1241 wchar_t *result =
1242 my_asnwprintf (NULL, &length, L"%015f %d", 1234.0, 33, 44, 55);
1243 ASSERT (result != NULL);
1244 ASSERT (wcscmp (result, L"00001234.000000 33") == 0);
1245 ASSERT (length == wcslen (result));
1246 free (result);
1249 { /* FLAG_ZERO with infinite number. */
1250 size_t length;
1251 wchar_t *result =
1252 my_asnwprintf (NULL, &length, L"%015f %d", - Infinityd (), 33, 44, 55);
1253 ASSERT (result != NULL);
1254 ASSERT (wcscmp (result, L" -inf 33") == 0
1255 || wcscmp (result, L" -infinity 33") == 0);
1256 ASSERT (length == wcslen (result));
1257 free (result);
1260 { /* FLAG_ZERO with NaN. */
1261 size_t length;
1262 wchar_t *result =
1263 my_asnwprintf (NULL, &length, L"%050f %d", NaNd (), 33, 44, 55);
1264 ASSERT (result != NULL);
1265 ASSERT (wcslen (result) == 50 + 3
1266 && wcsisnan (result, wcsspn (result, L" "), wcslen (result) - 3, 0)
1267 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
1268 ASSERT (length == wcslen (result));
1269 free (result);
1272 { /* Precision. */
1273 size_t length;
1274 wchar_t *result =
1275 my_asnwprintf (NULL, &length, L"%.f %d", 1234.0, 33, 44, 55);
1276 ASSERT (result != NULL);
1277 ASSERT (wcscmp (result, L"1234 33") == 0);
1278 ASSERT (length == wcslen (result));
1279 free (result);
1282 { /* Precision with no rounding. */
1283 size_t length;
1284 wchar_t *result =
1285 my_asnwprintf (NULL, &length, L"%.2f %d", 999.951, 33, 44, 55);
1286 ASSERT (result != NULL);
1287 ASSERT (wcscmp (result, L"999.95 33") == 0);
1288 ASSERT (length == wcslen (result));
1289 free (result);
1292 { /* Precision with rounding. */
1293 size_t length;
1294 wchar_t *result =
1295 my_asnwprintf (NULL, &length, L"%.2f %d", 999.996, 33, 44, 55);
1296 ASSERT (result != NULL);
1297 ASSERT (wcscmp (result, L"1000.00 33") == 0);
1298 ASSERT (length == wcslen (result));
1299 free (result);
1302 { /* A positive number. */
1303 size_t length;
1304 wchar_t *result =
1305 my_asnwprintf (NULL, &length, L"%Lf %d", 12.75L, 33, 44, 55);
1306 ASSERT (result != NULL);
1307 ASSERT (wcscmp (result, L"12.750000 33") == 0);
1308 ASSERT (length == wcslen (result));
1309 free (result);
1312 { /* A larger positive number. */
1313 size_t length;
1314 wchar_t *result =
1315 my_asnwprintf (NULL, &length, L"%Lf %d", 1234567.0L, 33, 44, 55);
1316 ASSERT (result != NULL);
1317 ASSERT (wcscmp (result, L"1234567.000000 33") == 0);
1318 ASSERT (length == wcslen (result));
1319 free (result);
1322 { /* Small and large positive numbers. */
1323 static struct { long double value; const wchar_t *string; } data[] =
1325 { 1.234321234321234e-37L, L"0.000000" },
1326 { 1.234321234321234e-36L, L"0.000000" },
1327 { 1.234321234321234e-35L, L"0.000000" },
1328 { 1.234321234321234e-34L, L"0.000000" },
1329 { 1.234321234321234e-33L, L"0.000000" },
1330 { 1.234321234321234e-32L, L"0.000000" },
1331 { 1.234321234321234e-31L, L"0.000000" },
1332 { 1.234321234321234e-30L, L"0.000000" },
1333 { 1.234321234321234e-29L, L"0.000000" },
1334 { 1.234321234321234e-28L, L"0.000000" },
1335 { 1.234321234321234e-27L, L"0.000000" },
1336 { 1.234321234321234e-26L, L"0.000000" },
1337 { 1.234321234321234e-25L, L"0.000000" },
1338 { 1.234321234321234e-24L, L"0.000000" },
1339 { 1.234321234321234e-23L, L"0.000000" },
1340 { 1.234321234321234e-22L, L"0.000000" },
1341 { 1.234321234321234e-21L, L"0.000000" },
1342 { 1.234321234321234e-20L, L"0.000000" },
1343 { 1.234321234321234e-19L, L"0.000000" },
1344 { 1.234321234321234e-18L, L"0.000000" },
1345 { 1.234321234321234e-17L, L"0.000000" },
1346 { 1.234321234321234e-16L, L"0.000000" },
1347 { 1.234321234321234e-15L, L"0.000000" },
1348 { 1.234321234321234e-14L, L"0.000000" },
1349 { 1.234321234321234e-13L, L"0.000000" },
1350 { 1.234321234321234e-12L, L"0.000000" },
1351 { 1.234321234321234e-11L, L"0.000000" },
1352 { 1.234321234321234e-10L, L"0.000000" },
1353 { 1.234321234321234e-9L, L"0.000000" },
1354 { 1.234321234321234e-8L, L"0.000000" },
1355 { 1.234321234321234e-7L, L"0.000000" },
1356 { 1.234321234321234e-6L, L"0.000001" },
1357 { 1.234321234321234e-5L, L"0.000012" },
1358 { 1.234321234321234e-4L, L"0.000123" },
1359 { 1.234321234321234e-3L, L"0.001234" },
1360 { 1.234321234321234e-2L, L"0.012343" },
1361 { 1.234321234321234e-1L, L"0.123432" },
1362 { 1.234321234321234L, L"1.234321" },
1363 { 1.234321234321234e1L, L"12.343212" },
1364 { 1.234321234321234e2L, L"123.432123" },
1365 { 1.234321234321234e3L, L"1234.321234" },
1366 { 1.234321234321234e4L, L"12343.212343" },
1367 { 1.234321234321234e5L, L"123432.123432" },
1368 { 1.234321234321234e6L, L"1234321.234321" },
1369 { 1.234321234321234e7L, L"12343212.343212" },
1370 { 1.234321234321234e8L, L"123432123.432123" },
1371 { 1.234321234321234e9L, L"1234321234.321234" },
1372 { 1.234321234321234e10L, L"12343212343.2123**" },
1373 { 1.234321234321234e11L, L"123432123432.123***" },
1374 { 1.234321234321234e12L, L"1234321234321.23****" },
1375 { 1.234321234321234e13L, L"12343212343212.3*****" },
1376 { 1.234321234321234e14L, L"123432123432123.******" },
1377 { 1.234321234321234e15L, L"1234321234321234.000000" },
1378 { 1.234321234321234e16L, L"123432123432123**.000000" },
1379 { 1.234321234321234e17L, L"123432123432123***.000000" },
1380 { 1.234321234321234e18L, L"123432123432123****.000000" },
1381 { 1.234321234321234e19L, L"123432123432123*****.000000" },
1382 { 1.234321234321234e20L, L"123432123432123******.000000" },
1383 { 1.234321234321234e21L, L"123432123432123*******.000000" },
1384 { 1.234321234321234e22L, L"123432123432123********.000000" },
1385 { 1.234321234321234e23L, L"123432123432123*********.000000" },
1386 { 1.234321234321234e24L, L"123432123432123**********.000000" },
1387 { 1.234321234321234e25L, L"123432123432123***********.000000" },
1388 { 1.234321234321234e26L, L"123432123432123************.000000" },
1389 { 1.234321234321234e27L, L"123432123432123*************.000000" },
1390 { 1.234321234321234e28L, L"123432123432123**************.000000" },
1391 { 1.234321234321234e29L, L"123432123432123***************.000000" },
1392 { 1.234321234321234e30L, L"123432123432123****************.000000" },
1393 { 1.234321234321234e31L, L"123432123432123*****************.000000" },
1394 { 1.234321234321234e32L, L"123432123432123******************.000000" },
1395 { 1.234321234321234e33L, L"123432123432123*******************.000000" },
1396 { 1.234321234321234e34L, L"123432123432123********************.000000" },
1397 { 1.234321234321234e35L, L"123432123432123*********************.000000" },
1398 { 1.234321234321234e36L, L"123432123432123**********************.000000" }
1400 size_t k;
1401 for (k = 0; k < SIZEOF (data); k++)
1403 size_t length;
1404 wchar_t *result =
1405 my_asnwprintf (NULL, &length, L"%Lf", data[k].value);
1406 ASSERT (result != NULL);
1407 ASSERT (wcsmatch (data[k].string, result));
1408 ASSERT (length == wcslen (result));
1409 free (result);
1413 { /* A negative number. */
1414 size_t length;
1415 wchar_t *result =
1416 my_asnwprintf (NULL, &length, L"%Lf %d", -0.03125L, 33, 44, 55);
1417 ASSERT (result != NULL);
1418 ASSERT (wcscmp (result, L"-0.031250 33") == 0);
1419 ASSERT (length == wcslen (result));
1420 free (result);
1423 { /* Positive zero. */
1424 size_t length;
1425 wchar_t *result =
1426 my_asnwprintf (NULL, &length, L"%Lf %d", 0.0L, 33, 44, 55);
1427 ASSERT (result != NULL);
1428 ASSERT (wcscmp (result, L"0.000000 33") == 0);
1429 ASSERT (length == wcslen (result));
1430 free (result);
1433 { /* Negative zero. */
1434 size_t length;
1435 wchar_t *result =
1436 my_asnwprintf (NULL, &length, L"%Lf %d", minus_zerol, 33, 44, 55);
1437 ASSERT (result != NULL);
1438 if (have_minus_zero ())
1439 ASSERT (wcscmp (result, L"-0.000000 33") == 0);
1440 ASSERT (length == wcslen (result));
1441 free (result);
1444 { /* Positive infinity. */
1445 size_t length;
1446 wchar_t *result =
1447 my_asnwprintf (NULL, &length, L"%Lf %d", Infinityl (), 33, 44, 55);
1448 ASSERT (result != NULL);
1449 ASSERT (wcscmp (result, L"inf 33") == 0
1450 || wcscmp (result, L"infinity 33") == 0);
1451 ASSERT (length == wcslen (result));
1452 free (result);
1455 { /* Negative infinity. */
1456 size_t length;
1457 wchar_t *result =
1458 my_asnwprintf (NULL, &length, L"%Lf %d", - Infinityl (), 33, 44, 55);
1459 ASSERT (result != NULL);
1460 ASSERT (wcscmp (result, L"-inf 33") == 0
1461 || wcscmp (result, L"-infinity 33") == 0);
1462 ASSERT (length == wcslen (result));
1463 free (result);
1466 { /* NaN. */
1467 size_t length;
1468 wchar_t *result =
1469 my_asnwprintf (NULL, &length, L"%Lf %d", NaNl (), 33, 44, 55);
1470 ASSERT (result != NULL);
1471 ASSERT (wcslen (result) >= 3 + 3
1472 && wcsisnan (result, 0, wcslen (result) - 3, 0)
1473 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
1474 ASSERT (length == wcslen (result));
1475 free (result);
1477 #if HAVE_SNANL
1478 { /* Signalling NaN. */
1479 size_t length;
1480 wchar_t *result =
1481 my_asnwprintf (NULL, &length, L"%Lf %d", SNaNl (), 33, 44, 55);
1482 ASSERT (result != NULL);
1483 ASSERT (wcslen (result) >= 3 + 3
1484 && wcsisnan (result, 0, wcslen (result) - 3, 0)
1485 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
1486 ASSERT (length == wcslen (result));
1487 free (result);
1489 #endif
1490 #if CHECK_PRINTF_SAFE && ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_)) && !HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
1491 { /* Quiet NaN. */
1492 static union { unsigned int word[4]; long double value; } x =
1493 { .word = LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
1494 size_t length;
1495 wchar_t *result =
1496 my_asnwprintf (NULL, &length, L"%Lf %d", x.value, 33, 44, 55);
1497 ASSERT (result != NULL);
1498 ASSERT (wcslen (result) >= 3 + 3
1499 && wcsisnan (result, 0, wcslen (result) - 3, 0)
1500 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
1501 ASSERT (length == wcslen (result));
1502 free (result);
1505 /* Signalling NaN. */
1506 static union { unsigned int word[4]; long double value; } x =
1507 { .word = LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
1508 size_t length;
1509 wchar_t *result =
1510 my_asnwprintf (NULL, &length, L"%Lf %d", x.value, 33, 44, 55);
1511 ASSERT (result != NULL);
1512 ASSERT (wcslen (result) >= 3 + 3
1513 && wcsisnan (result, 0, wcslen (result) - 3, 0)
1514 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
1515 ASSERT (length == wcslen (result));
1516 free (result);
1518 /* asnwprintf should print something for noncanonical values. */
1519 { /* Pseudo-NaN. */
1520 static union { unsigned int word[4]; long double value; } x =
1521 { .word = LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
1522 size_t length;
1523 wchar_t *result =
1524 my_asnwprintf (NULL, &length, L"%Lf %d", x.value, 33, 44, 55);
1525 ASSERT (result != NULL);
1526 ASSERT (length == wcslen (result));
1527 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
1528 free (result);
1530 { /* Pseudo-Infinity. */
1531 static union { unsigned int word[4]; long double value; } x =
1532 { .word = LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
1533 size_t length;
1534 wchar_t *result =
1535 my_asnwprintf (NULL, &length, L"%Lf %d", x.value, 33, 44, 55);
1536 ASSERT (result != NULL);
1537 ASSERT (length == wcslen (result));
1538 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
1539 free (result);
1541 { /* Pseudo-Zero. */
1542 static union { unsigned int word[4]; long double value; } x =
1543 { .word = LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
1544 size_t length;
1545 wchar_t *result =
1546 my_asnwprintf (NULL, &length, L"%Lf %d", x.value, 33, 44, 55);
1547 ASSERT (result != NULL);
1548 ASSERT (length == wcslen (result));
1549 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
1550 free (result);
1552 { /* Unnormalized number. */
1553 static union { unsigned int word[4]; long double value; } x =
1554 { .word = LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
1555 size_t length;
1556 wchar_t *result =
1557 my_asnwprintf (NULL, &length, L"%Lf %d", x.value, 33, 44, 55);
1558 ASSERT (result != NULL);
1559 ASSERT (length == wcslen (result));
1560 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
1561 free (result);
1563 { /* Pseudo-Denormal. */
1564 static union { unsigned int word[4]; long double value; } x =
1565 { .word = LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
1566 size_t length;
1567 wchar_t *result =
1568 my_asnwprintf (NULL, &length, L"%Lf %d", x.value, 33, 44, 55);
1569 ASSERT (result != NULL);
1570 ASSERT (length == wcslen (result));
1571 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
1572 free (result);
1574 #endif
1576 { /* Width. */
1577 size_t length;
1578 wchar_t *result =
1579 my_asnwprintf (NULL, &length, L"%10Lf %d", 1.75L, 33, 44, 55);
1580 ASSERT (result != NULL);
1581 ASSERT (wcscmp (result, L" 1.750000 33") == 0);
1582 ASSERT (length == wcslen (result));
1583 free (result);
1586 { /* Width given as argument. */
1587 size_t length;
1588 wchar_t *result =
1589 my_asnwprintf (NULL, &length, L"%*Lf %d", 10, 1.75L, 33, 44, 55);
1590 ASSERT (result != NULL);
1591 ASSERT (wcscmp (result, L" 1.750000 33") == 0);
1592 ASSERT (length == wcslen (result));
1593 free (result);
1596 { /* Negative width given as argument (cf. FLAG_LEFT below). */
1597 size_t length;
1598 wchar_t *result =
1599 my_asnwprintf (NULL, &length, L"%*Lf %d", -10, 1.75L, 33, 44, 55);
1600 ASSERT (result != NULL);
1601 ASSERT (wcscmp (result, L"1.750000 33") == 0);
1602 ASSERT (length == wcslen (result));
1603 free (result);
1606 { /* FLAG_LEFT. */
1607 size_t length;
1608 wchar_t *result =
1609 my_asnwprintf (NULL, &length, L"%-10Lf %d", 1.75L, 33, 44, 55);
1610 ASSERT (result != NULL);
1611 ASSERT (wcscmp (result, L"1.750000 33") == 0);
1612 ASSERT (length == wcslen (result));
1613 free (result);
1616 { /* FLAG_SHOWSIGN. */
1617 size_t length;
1618 wchar_t *result =
1619 my_asnwprintf (NULL, &length, L"%+Lf %d", 1.75L, 33, 44, 55);
1620 ASSERT (result != NULL);
1621 ASSERT (wcscmp (result, L"+1.750000 33") == 0);
1622 ASSERT (length == wcslen (result));
1623 free (result);
1626 { /* FLAG_SPACE. */
1627 size_t length;
1628 wchar_t *result =
1629 my_asnwprintf (NULL, &length, L"% Lf %d", 1.75L, 33, 44, 55);
1630 ASSERT (result != NULL);
1631 ASSERT (wcscmp (result, L" 1.750000 33") == 0);
1632 ASSERT (length == wcslen (result));
1633 free (result);
1636 { /* FLAG_ALT. */
1637 size_t length;
1638 wchar_t *result =
1639 my_asnwprintf (NULL, &length, L"%#Lf %d", 1.75L, 33, 44, 55);
1640 ASSERT (result != NULL);
1641 ASSERT (wcscmp (result, L"1.750000 33") == 0);
1642 ASSERT (length == wcslen (result));
1643 free (result);
1646 { /* FLAG_ALT. */
1647 size_t length;
1648 wchar_t *result =
1649 my_asnwprintf (NULL, &length, L"%#.Lf %d", 1.75L, 33, 44, 55);
1650 ASSERT (result != NULL);
1651 ASSERT (wcscmp (result, L"2. 33") == 0);
1652 ASSERT (length == wcslen (result));
1653 free (result);
1656 { /* FLAG_ZERO with finite number. */
1657 size_t length;
1658 wchar_t *result =
1659 my_asnwprintf (NULL, &length, L"%015Lf %d", 1234.0L, 33, 44, 55);
1660 ASSERT (result != NULL);
1661 ASSERT (wcscmp (result, L"00001234.000000 33") == 0);
1662 ASSERT (length == wcslen (result));
1663 free (result);
1666 { /* FLAG_ZERO with infinite number. */
1667 size_t length;
1668 wchar_t *result =
1669 my_asnwprintf (NULL, &length, L"%015Lf %d", - Infinityl (), 33, 44, 55);
1670 ASSERT (result != NULL);
1671 ASSERT (wcscmp (result, L" -inf 33") == 0
1672 || wcscmp (result, L" -infinity 33") == 0);
1673 ASSERT (length == wcslen (result));
1674 free (result);
1677 { /* FLAG_ZERO with NaN. */
1678 size_t length;
1679 wchar_t *result =
1680 my_asnwprintf (NULL, &length, L"%050Lf %d", NaNl (), 33, 44, 55);
1681 ASSERT (result != NULL);
1682 ASSERT (wcslen (result) == 50 + 3
1683 && wcsisnan (result, wcsspn (result, L" "), wcslen (result) - 3, 0)
1684 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
1685 ASSERT (length == wcslen (result));
1686 free (result);
1689 { /* Precision. */
1690 size_t length;
1691 wchar_t *result =
1692 my_asnwprintf (NULL, &length, L"%.Lf %d", 1234.0L, 33, 44, 55);
1693 ASSERT (result != NULL);
1694 ASSERT (wcscmp (result, L"1234 33") == 0);
1695 ASSERT (length == wcslen (result));
1696 free (result);
1699 { /* Precision with no rounding. */
1700 size_t length;
1701 wchar_t *result =
1702 my_asnwprintf (NULL, &length, L"%.2Lf %d", 999.951L, 33, 44, 55);
1703 ASSERT (result != NULL);
1704 ASSERT (wcscmp (result, L"999.95 33") == 0);
1705 ASSERT (length == wcslen (result));
1706 free (result);
1709 { /* Precision with rounding. */
1710 size_t length;
1711 wchar_t *result =
1712 my_asnwprintf (NULL, &length, L"%.2Lf %d", 999.996L, 33, 44, 55);
1713 ASSERT (result != NULL);
1714 ASSERT (wcscmp (result, L"1000.00 33") == 0);
1715 ASSERT (length == wcslen (result));
1716 free (result);
1719 /* Test the support of the %F format directive. */
1721 { /* A positive number. */
1722 size_t length;
1723 wchar_t *result =
1724 my_asnwprintf (NULL, &length, L"%F %d", 12.75, 33, 44, 55);
1725 ASSERT (result != NULL);
1726 ASSERT (wcscmp (result, L"12.750000 33") == 0);
1727 ASSERT (length == wcslen (result));
1728 free (result);
1731 { /* A larger positive number. */
1732 size_t length;
1733 wchar_t *result =
1734 my_asnwprintf (NULL, &length, L"%F %d", 1234567.0, 33, 44, 55);
1735 ASSERT (result != NULL);
1736 ASSERT (wcscmp (result, L"1234567.000000 33") == 0);
1737 ASSERT (length == wcslen (result));
1738 free (result);
1741 { /* A negative number. */
1742 size_t length;
1743 wchar_t *result =
1744 my_asnwprintf (NULL, &length, L"%F %d", -0.03125, 33, 44, 55);
1745 ASSERT (result != NULL);
1746 ASSERT (wcscmp (result, L"-0.031250 33") == 0);
1747 ASSERT (length == wcslen (result));
1748 free (result);
1751 { /* Positive zero. */
1752 size_t length;
1753 wchar_t *result =
1754 my_asnwprintf (NULL, &length, L"%F %d", 0.0, 33, 44, 55);
1755 ASSERT (result != NULL);
1756 ASSERT (wcscmp (result, L"0.000000 33") == 0);
1757 ASSERT (length == wcslen (result));
1758 free (result);
1761 { /* Negative zero. */
1762 size_t length;
1763 wchar_t *result =
1764 my_asnwprintf (NULL, &length, L"%F %d", minus_zerod, 33, 44, 55);
1765 ASSERT (result != NULL);
1766 if (have_minus_zero ())
1767 ASSERT (wcscmp (result, L"-0.000000 33") == 0);
1768 ASSERT (length == wcslen (result));
1769 free (result);
1772 { /* Positive infinity. */
1773 size_t length;
1774 wchar_t *result =
1775 my_asnwprintf (NULL, &length, L"%F %d", Infinityd (), 33, 44, 55);
1776 ASSERT (result != NULL);
1777 ASSERT (wcscmp (result, L"INF 33") == 0
1778 || wcscmp (result, L"INFINITY 33") == 0);
1779 ASSERT (length == wcslen (result));
1780 free (result);
1783 { /* Negative infinity. */
1784 size_t length;
1785 wchar_t *result =
1786 my_asnwprintf (NULL, &length, L"%F %d", - Infinityd (), 33, 44, 55);
1787 ASSERT (result != NULL);
1788 ASSERT (wcscmp (result, L"-INF 33") == 0
1789 || wcscmp (result, L"-INFINITY 33") == 0);
1790 ASSERT (length == wcslen (result));
1791 free (result);
1794 { /* NaN. */
1795 size_t length;
1796 wchar_t *result =
1797 my_asnwprintf (NULL, &length, L"%F %d", NaNd (), 33, 44, 55);
1798 ASSERT (result != NULL);
1799 ASSERT (wcslen (result) >= 3 + 3
1800 && wcsisnan (result, 0, wcslen (result) - 3, 1)
1801 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
1802 ASSERT (length == wcslen (result));
1803 free (result);
1805 #if HAVE_SNAND
1806 { /* Signalling NaN. */
1807 size_t length;
1808 wchar_t *result =
1809 my_asnwprintf (NULL, &length, L"%F %d", SNaNd (), 33, 44, 55);
1810 ASSERT (result != NULL);
1811 ASSERT (wcslen (result) >= 3 + 3
1812 && wcsisnan (result, 0, wcslen (result) - 3, 1)
1813 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
1814 ASSERT (length == wcslen (result));
1815 free (result);
1817 #endif
1819 { /* FLAG_ZERO. */
1820 size_t length;
1821 wchar_t *result =
1822 my_asnwprintf (NULL, &length, L"%015F %d", 1234.0, 33, 44, 55);
1823 ASSERT (result != NULL);
1824 ASSERT (wcscmp (result, L"00001234.000000 33") == 0);
1825 ASSERT (length == wcslen (result));
1826 free (result);
1829 { /* FLAG_ZERO with infinite number. */
1830 size_t length;
1831 wchar_t *result =
1832 my_asnwprintf (NULL, &length, L"%015F %d", - Infinityd (), 33, 44, 55);
1833 ASSERT (result != NULL);
1834 ASSERT (wcscmp (result, L" -INF 33") == 0
1835 || wcscmp (result, L" -INFINITY 33") == 0);
1836 ASSERT (length == wcslen (result));
1837 free (result);
1840 { /* Precision. */
1841 size_t length;
1842 wchar_t *result =
1843 my_asnwprintf (NULL, &length, L"%.F %d", 1234.0, 33, 44, 55);
1844 ASSERT (result != NULL);
1845 ASSERT (wcscmp (result, L"1234 33") == 0);
1846 ASSERT (length == wcslen (result));
1847 free (result);
1850 { /* Precision with no rounding. */
1851 size_t length;
1852 wchar_t *result =
1853 my_asnwprintf (NULL, &length, L"%.2F %d", 999.951, 33, 44, 55);
1854 ASSERT (result != NULL);
1855 ASSERT (wcscmp (result, L"999.95 33") == 0);
1856 ASSERT (length == wcslen (result));
1857 free (result);
1860 { /* Precision with rounding. */
1861 size_t length;
1862 wchar_t *result =
1863 my_asnwprintf (NULL, &length, L"%.2F %d", 999.996, 33, 44, 55);
1864 ASSERT (result != NULL);
1865 ASSERT (wcscmp (result, L"1000.00 33") == 0);
1866 ASSERT (length == wcslen (result));
1867 free (result);
1870 { /* A positive number. */
1871 size_t length;
1872 wchar_t *result =
1873 my_asnwprintf (NULL, &length, L"%LF %d", 12.75L, 33, 44, 55);
1874 ASSERT (result != NULL);
1875 ASSERT (wcscmp (result, L"12.750000 33") == 0);
1876 ASSERT (length == wcslen (result));
1877 free (result);
1880 { /* A larger positive number. */
1881 size_t length;
1882 wchar_t *result =
1883 my_asnwprintf (NULL, &length, L"%LF %d", 1234567.0L, 33, 44, 55);
1884 ASSERT (result != NULL);
1885 ASSERT (wcscmp (result, L"1234567.000000 33") == 0);
1886 ASSERT (length == wcslen (result));
1887 free (result);
1890 { /* A negative number. */
1891 size_t length;
1892 wchar_t *result =
1893 my_asnwprintf (NULL, &length, L"%LF %d", -0.03125L, 33, 44, 55);
1894 ASSERT (result != NULL);
1895 ASSERT (wcscmp (result, L"-0.031250 33") == 0);
1896 ASSERT (length == wcslen (result));
1897 free (result);
1900 { /* Positive zero. */
1901 size_t length;
1902 wchar_t *result =
1903 my_asnwprintf (NULL, &length, L"%LF %d", 0.0L, 33, 44, 55);
1904 ASSERT (result != NULL);
1905 ASSERT (wcscmp (result, L"0.000000 33") == 0);
1906 ASSERT (length == wcslen (result));
1907 free (result);
1910 { /* Negative zero. */
1911 size_t length;
1912 wchar_t *result =
1913 my_asnwprintf (NULL, &length, L"%LF %d", minus_zerol, 33, 44, 55);
1914 ASSERT (result != NULL);
1915 if (have_minus_zero ())
1916 ASSERT (wcscmp (result, L"-0.000000 33") == 0);
1917 ASSERT (length == wcslen (result));
1918 free (result);
1921 { /* Positive infinity. */
1922 size_t length;
1923 wchar_t *result =
1924 my_asnwprintf (NULL, &length, L"%LF %d", Infinityl (), 33, 44, 55);
1925 ASSERT (result != NULL);
1926 ASSERT (wcscmp (result, L"INF 33") == 0
1927 || wcscmp (result, L"INFINITY 33") == 0);
1928 ASSERT (length == wcslen (result));
1929 free (result);
1932 { /* Negative infinity. */
1933 size_t length;
1934 wchar_t *result =
1935 my_asnwprintf (NULL, &length, L"%LF %d", - Infinityl (), 33, 44, 55);
1936 ASSERT (result != NULL);
1937 ASSERT (wcscmp (result, L"-INF 33") == 0
1938 || wcscmp (result, L"-INFINITY 33") == 0);
1939 ASSERT (length == wcslen (result));
1940 free (result);
1943 { /* NaN. */
1944 size_t length;
1945 wchar_t *result =
1946 my_asnwprintf (NULL, &length, L"%LF %d", NaNl (), 33, 44, 55);
1947 ASSERT (result != NULL);
1948 ASSERT (wcslen (result) >= 3 + 3
1949 && wcsisnan (result, 0, wcslen (result) - 3, 1)
1950 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
1951 ASSERT (length == wcslen (result));
1952 free (result);
1954 #if HAVE_SNANL
1955 { /* Signalling NaN. */
1956 size_t length;
1957 wchar_t *result =
1958 my_asnwprintf (NULL, &length, L"%LF %d", SNaNl (), 33, 44, 55);
1959 ASSERT (result != NULL);
1960 ASSERT (wcslen (result) >= 3 + 3
1961 && wcsisnan (result, 0, wcslen (result) - 3, 1)
1962 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
1963 ASSERT (length == wcslen (result));
1964 free (result);
1966 #endif
1968 { /* FLAG_ZERO. */
1969 size_t length;
1970 wchar_t *result =
1971 my_asnwprintf (NULL, &length, L"%015LF %d", 1234.0L, 33, 44, 55);
1972 ASSERT (result != NULL);
1973 ASSERT (wcscmp (result, L"00001234.000000 33") == 0);
1974 ASSERT (length == wcslen (result));
1975 free (result);
1978 { /* FLAG_ZERO with infinite number. */
1979 size_t length;
1980 wchar_t *result =
1981 my_asnwprintf (NULL, &length, L"%015LF %d", - Infinityl (), 33, 44, 55);
1982 ASSERT (result != NULL);
1983 ASSERT (wcscmp (result, L" -INF 33") == 0
1984 || wcscmp (result, L" -INFINITY 33") == 0);
1985 ASSERT (length == wcslen (result));
1986 free (result);
1989 { /* Precision. */
1990 size_t length;
1991 wchar_t *result =
1992 my_asnwprintf (NULL, &length, L"%.LF %d", 1234.0L, 33, 44, 55);
1993 ASSERT (result != NULL);
1994 ASSERT (wcscmp (result, L"1234 33") == 0);
1995 ASSERT (length == wcslen (result));
1996 free (result);
1999 { /* Precision with no rounding. */
2000 size_t length;
2001 wchar_t *result =
2002 my_asnwprintf (NULL, &length, L"%.2LF %d", 999.951L, 33, 44, 55);
2003 ASSERT (result != NULL);
2004 ASSERT (wcscmp (result, L"999.95 33") == 0);
2005 ASSERT (length == wcslen (result));
2006 free (result);
2009 { /* Precision with rounding. */
2010 size_t length;
2011 wchar_t *result =
2012 my_asnwprintf (NULL, &length, L"%.2LF %d", 999.996L, 33, 44, 55);
2013 ASSERT (result != NULL);
2014 ASSERT (wcscmp (result, L"1000.00 33") == 0);
2015 ASSERT (length == wcslen (result));
2016 free (result);
2019 /* Test the support of the %e format directive. */
2021 { /* A positive number. */
2022 size_t length;
2023 wchar_t *result =
2024 my_asnwprintf (NULL, &length, L"%e %d", 12.75, 33, 44, 55);
2025 ASSERT (result != NULL);
2026 ASSERT (wcscmp (result, L"1.275000e+01 33") == 0
2027 || wcscmp (result, L"1.275000e+001 33") == 0);
2028 ASSERT (length == wcslen (result));
2029 free (result);
2032 { /* A larger positive number. */
2033 size_t length;
2034 wchar_t *result =
2035 my_asnwprintf (NULL, &length, L"%e %d", 1234567.0, 33, 44, 55);
2036 ASSERT (result != NULL);
2037 ASSERT (wcscmp (result, L"1.234567e+06 33") == 0
2038 || wcscmp (result, L"1.234567e+006 33") == 0);
2039 ASSERT (length == wcslen (result));
2040 free (result);
2043 { /* Small and large positive numbers. */
2044 static struct { double value; const wchar_t *string; } data[] =
2046 { 1.234321234321234e-37, L"1.234321e-37" },
2047 { 1.234321234321234e-36, L"1.234321e-36" },
2048 { 1.234321234321234e-35, L"1.234321e-35" },
2049 { 1.234321234321234e-34, L"1.234321e-34" },
2050 { 1.234321234321234e-33, L"1.234321e-33" },
2051 { 1.234321234321234e-32, L"1.234321e-32" },
2052 { 1.234321234321234e-31, L"1.234321e-31" },
2053 { 1.234321234321234e-30, L"1.234321e-30" },
2054 { 1.234321234321234e-29, L"1.234321e-29" },
2055 { 1.234321234321234e-28, L"1.234321e-28" },
2056 { 1.234321234321234e-27, L"1.234321e-27" },
2057 { 1.234321234321234e-26, L"1.234321e-26" },
2058 { 1.234321234321234e-25, L"1.234321e-25" },
2059 { 1.234321234321234e-24, L"1.234321e-24" },
2060 { 1.234321234321234e-23, L"1.234321e-23" },
2061 { 1.234321234321234e-22, L"1.234321e-22" },
2062 { 1.234321234321234e-21, L"1.234321e-21" },
2063 { 1.234321234321234e-20, L"1.234321e-20" },
2064 { 1.234321234321234e-19, L"1.234321e-19" },
2065 { 1.234321234321234e-18, L"1.234321e-18" },
2066 { 1.234321234321234e-17, L"1.234321e-17" },
2067 { 1.234321234321234e-16, L"1.234321e-16" },
2068 { 1.234321234321234e-15, L"1.234321e-15" },
2069 { 1.234321234321234e-14, L"1.234321e-14" },
2070 { 1.234321234321234e-13, L"1.234321e-13" },
2071 { 1.234321234321234e-12, L"1.234321e-12" },
2072 { 1.234321234321234e-11, L"1.234321e-11" },
2073 { 1.234321234321234e-10, L"1.234321e-10" },
2074 { 1.234321234321234e-9, L"1.234321e-09" },
2075 { 1.234321234321234e-8, L"1.234321e-08" },
2076 { 1.234321234321234e-7, L"1.234321e-07" },
2077 { 1.234321234321234e-6, L"1.234321e-06" },
2078 { 1.234321234321234e-5, L"1.234321e-05" },
2079 { 1.234321234321234e-4, L"1.234321e-04" },
2080 { 1.234321234321234e-3, L"1.234321e-03" },
2081 { 1.234321234321234e-2, L"1.234321e-02" },
2082 { 1.234321234321234e-1, L"1.234321e-01" },
2083 { 1.234321234321234, L"1.234321e+00" },
2084 { 1.234321234321234e1, L"1.234321e+01" },
2085 { 1.234321234321234e2, L"1.234321e+02" },
2086 { 1.234321234321234e3, L"1.234321e+03" },
2087 { 1.234321234321234e4, L"1.234321e+04" },
2088 { 1.234321234321234e5, L"1.234321e+05" },
2089 { 1.234321234321234e6, L"1.234321e+06" },
2090 { 1.234321234321234e7, L"1.234321e+07" },
2091 { 1.234321234321234e8, L"1.234321e+08" },
2092 { 1.234321234321234e9, L"1.234321e+09" },
2093 { 1.234321234321234e10, L"1.234321e+10" },
2094 { 1.234321234321234e11, L"1.234321e+11" },
2095 { 1.234321234321234e12, L"1.234321e+12" },
2096 { 1.234321234321234e13, L"1.234321e+13" },
2097 { 1.234321234321234e14, L"1.234321e+14" },
2098 { 1.234321234321234e15, L"1.234321e+15" },
2099 { 1.234321234321234e16, L"1.234321e+16" },
2100 { 1.234321234321234e17, L"1.234321e+17" },
2101 { 1.234321234321234e18, L"1.234321e+18" },
2102 { 1.234321234321234e19, L"1.234321e+19" },
2103 { 1.234321234321234e20, L"1.234321e+20" },
2104 { 1.234321234321234e21, L"1.234321e+21" },
2105 { 1.234321234321234e22, L"1.234321e+22" },
2106 { 1.234321234321234e23, L"1.234321e+23" },
2107 { 1.234321234321234e24, L"1.234321e+24" },
2108 { 1.234321234321234e25, L"1.234321e+25" },
2109 { 1.234321234321234e26, L"1.234321e+26" },
2110 { 1.234321234321234e27, L"1.234321e+27" },
2111 { 1.234321234321234e28, L"1.234321e+28" },
2112 { 1.234321234321234e29, L"1.234321e+29" },
2113 { 1.234321234321234e30, L"1.234321e+30" },
2114 { 1.234321234321234e31, L"1.234321e+31" },
2115 { 1.234321234321234e32, L"1.234321e+32" },
2116 { 1.234321234321234e33, L"1.234321e+33" },
2117 { 1.234321234321234e34, L"1.234321e+34" },
2118 { 1.234321234321234e35, L"1.234321e+35" },
2119 { 1.234321234321234e36, L"1.234321e+36" }
2121 size_t k;
2122 for (k = 0; k < SIZEOF (data); k++)
2124 size_t length;
2125 wchar_t *result =
2126 my_asnwprintf (NULL, &length, L"%e", data[k].value);
2127 const wchar_t *expected = data[k].string;
2128 ASSERT (result != NULL);
2129 ASSERT (wcscmp (result, expected) == 0
2130 /* Some implementations produce exponents with 3 digits. */
2131 || (wcslen (result) == wcslen (expected) + 1
2132 && wmemcmp (result, expected, wcslen (expected) - 2) == 0
2133 && result[wcslen (expected) - 2] == '0'
2134 && wcscmp (result + wcslen (expected) - 1,
2135 expected + wcslen (expected) - 2)
2136 == 0));
2137 ASSERT (length == wcslen (result));
2138 free (result);
2142 { /* A negative number. */
2143 size_t length;
2144 wchar_t *result =
2145 my_asnwprintf (NULL, &length, L"%e %d", -0.03125, 33, 44, 55);
2146 ASSERT (result != NULL);
2147 ASSERT (wcscmp (result, L"-3.125000e-02 33") == 0
2148 || wcscmp (result, L"-3.125000e-002 33") == 0);
2149 ASSERT (length == wcslen (result));
2150 free (result);
2153 { /* Positive zero. */
2154 size_t length;
2155 wchar_t *result =
2156 my_asnwprintf (NULL, &length, L"%e %d", 0.0, 33, 44, 55);
2157 ASSERT (result != NULL);
2158 ASSERT (wcscmp (result, L"0.000000e+00 33") == 0
2159 || wcscmp (result, L"0.000000e+000 33") == 0);
2160 ASSERT (length == wcslen (result));
2161 free (result);
2164 { /* Negative zero. */
2165 size_t length;
2166 wchar_t *result =
2167 my_asnwprintf (NULL, &length, L"%e %d", minus_zerod, 33, 44, 55);
2168 ASSERT (result != NULL);
2169 if (have_minus_zero ())
2170 ASSERT (wcscmp (result, L"-0.000000e+00 33") == 0
2171 || wcscmp (result, L"-0.000000e+000 33") == 0);
2172 ASSERT (length == wcslen (result));
2173 free (result);
2176 { /* Positive infinity. */
2177 size_t length;
2178 wchar_t *result =
2179 my_asnwprintf (NULL, &length, L"%e %d", Infinityd (), 33, 44, 55);
2180 ASSERT (result != NULL);
2181 ASSERT (wcscmp (result, L"inf 33") == 0
2182 || wcscmp (result, L"infinity 33") == 0);
2183 ASSERT (length == wcslen (result));
2184 free (result);
2187 { /* Negative infinity. */
2188 size_t length;
2189 wchar_t *result =
2190 my_asnwprintf (NULL, &length, L"%e %d", - Infinityd (), 33, 44, 55);
2191 ASSERT (result != NULL);
2192 ASSERT (wcscmp (result, L"-inf 33") == 0
2193 || wcscmp (result, L"-infinity 33") == 0);
2194 ASSERT (length == wcslen (result));
2195 free (result);
2198 { /* NaN. */
2199 size_t length;
2200 wchar_t *result =
2201 my_asnwprintf (NULL, &length, L"%e %d", NaNd (), 33, 44, 55);
2202 ASSERT (result != NULL);
2203 ASSERT (wcslen (result) >= 3 + 3
2204 && wcsisnan (result, 0, wcslen (result) - 3, 0)
2205 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
2206 ASSERT (length == wcslen (result));
2207 free (result);
2209 #if HAVE_SNAND
2210 { /* Signalling NaN. */
2211 size_t length;
2212 wchar_t *result =
2213 my_asnwprintf (NULL, &length, L"%e %d", SNaNd (), 33, 44, 55);
2214 ASSERT (result != NULL);
2215 ASSERT (wcslen (result) >= 3 + 3
2216 && wcsisnan (result, 0, wcslen (result) - 3, 0)
2217 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
2218 ASSERT (length == wcslen (result));
2219 free (result);
2221 #endif
2223 { /* Width. */
2224 size_t length;
2225 wchar_t *result =
2226 my_asnwprintf (NULL, &length, L"%15e %d", 1.75, 33, 44, 55);
2227 ASSERT (result != NULL);
2228 ASSERT (wcscmp (result, L" 1.750000e+00 33") == 0
2229 || wcscmp (result, L" 1.750000e+000 33") == 0);
2230 ASSERT (length == wcslen (result));
2231 free (result);
2234 { /* Width given as argument. */
2235 size_t length;
2236 wchar_t *result =
2237 my_asnwprintf (NULL, &length, L"%*e %d", 15, 1.75, 33, 44, 55);
2238 ASSERT (result != NULL);
2239 ASSERT (wcscmp (result, L" 1.750000e+00 33") == 0
2240 || wcscmp (result, L" 1.750000e+000 33") == 0);
2241 ASSERT (length == wcslen (result));
2242 free (result);
2245 { /* Negative width given as argument (cf. FLAG_LEFT below). */
2246 size_t length;
2247 wchar_t *result =
2248 my_asnwprintf (NULL, &length, L"%*e %d", -15, 1.75, 33, 44, 55);
2249 ASSERT (result != NULL);
2250 ASSERT (wcscmp (result, L"1.750000e+00 33") == 0
2251 || wcscmp (result, L"1.750000e+000 33") == 0);
2252 ASSERT (length == wcslen (result));
2253 free (result);
2256 { /* FLAG_LEFT. */
2257 size_t length;
2258 wchar_t *result =
2259 my_asnwprintf (NULL, &length, L"%-15e %d", 1.75, 33, 44, 55);
2260 ASSERT (result != NULL);
2261 ASSERT (wcscmp (result, L"1.750000e+00 33") == 0
2262 || wcscmp (result, L"1.750000e+000 33") == 0);
2263 ASSERT (length == wcslen (result));
2264 free (result);
2267 { /* FLAG_SHOWSIGN. */
2268 size_t length;
2269 wchar_t *result =
2270 my_asnwprintf (NULL, &length, L"%+e %d", 1.75, 33, 44, 55);
2271 ASSERT (result != NULL);
2272 ASSERT (wcscmp (result, L"+1.750000e+00 33") == 0
2273 || wcscmp (result, L"+1.750000e+000 33") == 0);
2274 ASSERT (length == wcslen (result));
2275 free (result);
2278 { /* FLAG_SPACE. */
2279 size_t length;
2280 wchar_t *result =
2281 my_asnwprintf (NULL, &length, L"% e %d", 1.75, 33, 44, 55);
2282 ASSERT (result != NULL);
2283 ASSERT (wcscmp (result, L" 1.750000e+00 33") == 0
2284 || wcscmp (result, L" 1.750000e+000 33") == 0);
2285 ASSERT (length == wcslen (result));
2286 free (result);
2289 { /* FLAG_ALT. */
2290 size_t length;
2291 wchar_t *result =
2292 my_asnwprintf (NULL, &length, L"%#e %d", 1.75, 33, 44, 55);
2293 ASSERT (result != NULL);
2294 ASSERT (wcscmp (result, L"1.750000e+00 33") == 0
2295 || wcscmp (result, L"1.750000e+000 33") == 0);
2296 ASSERT (length == wcslen (result));
2297 free (result);
2300 { /* FLAG_ALT. */
2301 size_t length;
2302 wchar_t *result =
2303 my_asnwprintf (NULL, &length, L"%#.e %d", 1.75, 33, 44, 55);
2304 ASSERT (result != NULL);
2305 ASSERT (wcscmp (result, L"2.e+00 33") == 0
2306 || wcscmp (result, L"2.e+000 33") == 0);
2307 ASSERT (length == wcslen (result));
2308 free (result);
2311 { /* FLAG_ALT. */
2312 size_t length;
2313 wchar_t *result =
2314 my_asnwprintf (NULL, &length, L"%#.e %d", 9.75, 33, 44, 55);
2315 ASSERT (result != NULL);
2316 ASSERT (wcscmp (result, L"1.e+01 33") == 0
2317 || wcscmp (result, L"1.e+001 33") == 0);
2318 ASSERT (length == wcslen (result));
2319 free (result);
2322 { /* FLAG_ZERO with finite number. */
2323 size_t length;
2324 wchar_t *result =
2325 my_asnwprintf (NULL, &length, L"%015e %d", 1234.0, 33, 44, 55);
2326 ASSERT (result != NULL);
2327 ASSERT (wcscmp (result, L"0001.234000e+03 33") == 0
2328 || wcscmp (result, L"001.234000e+003 33") == 0);
2329 ASSERT (length == wcslen (result));
2330 free (result);
2333 { /* FLAG_ZERO with infinite number. */
2334 size_t length;
2335 wchar_t *result =
2336 my_asnwprintf (NULL, &length, L"%015e %d", - Infinityd (), 33, 44, 55);
2337 ASSERT (result != NULL);
2338 ASSERT (wcscmp (result, L" -inf 33") == 0
2339 || wcscmp (result, L" -infinity 33") == 0);
2340 ASSERT (length == wcslen (result));
2341 free (result);
2344 { /* FLAG_ZERO with NaN. */
2345 size_t length;
2346 wchar_t *result =
2347 my_asnwprintf (NULL, &length, L"%050e %d", NaNd (), 33, 44, 55);
2348 ASSERT (result != NULL);
2349 ASSERT (wcslen (result) == 50 + 3
2350 && wcsisnan (result, wcsspn (result, L" "), wcslen (result) - 3, 0)
2351 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
2352 ASSERT (length == wcslen (result));
2353 free (result);
2356 { /* Precision. */
2357 size_t length;
2358 wchar_t *result =
2359 my_asnwprintf (NULL, &length, L"%.e %d", 1234.0, 33, 44, 55);
2360 ASSERT (result != NULL);
2361 ASSERT (wcscmp (result, L"1e+03 33") == 0
2362 || wcscmp (result, L"1e+003 33") == 0);
2363 ASSERT (length == wcslen (result));
2364 free (result);
2367 { /* Precision with no rounding. */
2368 size_t length;
2369 wchar_t *result =
2370 my_asnwprintf (NULL, &length, L"%.4e %d", 999.951, 33, 44, 55);
2371 ASSERT (result != NULL);
2372 ASSERT (wcscmp (result, L"9.9995e+02 33") == 0
2373 || wcscmp (result, L"9.9995e+002 33") == 0);
2374 ASSERT (length == wcslen (result));
2375 free (result);
2378 { /* Precision with rounding. */
2379 size_t length;
2380 wchar_t *result =
2381 my_asnwprintf (NULL, &length, L"%.4e %d", 999.996, 33, 44, 55);
2382 ASSERT (result != NULL);
2383 ASSERT (wcscmp (result, L"1.0000e+03 33") == 0
2384 || wcscmp (result, L"1.0000e+003 33") == 0);
2385 ASSERT (length == wcslen (result));
2386 free (result);
2389 { /* A positive number. */
2390 size_t length;
2391 wchar_t *result =
2392 my_asnwprintf (NULL, &length, L"%Le %d", 12.75L, 33, 44, 55);
2393 ASSERT (result != NULL);
2394 ASSERT (wcscmp (result, L"1.275000e+01 33") == 0
2395 || wcscmp (result, L"1.275000e+001 33") == 0);
2396 ASSERT (length == wcslen (result));
2397 free (result);
2400 { /* A larger positive number. */
2401 size_t length;
2402 wchar_t *result =
2403 my_asnwprintf (NULL, &length, L"%Le %d", 1234567.0L, 33, 44, 55);
2404 ASSERT (result != NULL);
2405 ASSERT (wcscmp (result, L"1.234567e+06 33") == 0
2406 || wcscmp (result, L"1.234567e+006 33") == 0);
2407 ASSERT (length == wcslen (result));
2408 free (result);
2411 { /* Small and large positive numbers. */
2412 static struct { long double value; const wchar_t *string; } data[] =
2414 { 1.234321234321234e-37L, L"1.234321e-37" },
2415 { 1.234321234321234e-36L, L"1.234321e-36" },
2416 { 1.234321234321234e-35L, L"1.234321e-35" },
2417 { 1.234321234321234e-34L, L"1.234321e-34" },
2418 { 1.234321234321234e-33L, L"1.234321e-33" },
2419 { 1.234321234321234e-32L, L"1.234321e-32" },
2420 { 1.234321234321234e-31L, L"1.234321e-31" },
2421 { 1.234321234321234e-30L, L"1.234321e-30" },
2422 { 1.234321234321234e-29L, L"1.234321e-29" },
2423 { 1.234321234321234e-28L, L"1.234321e-28" },
2424 { 1.234321234321234e-27L, L"1.234321e-27" },
2425 { 1.234321234321234e-26L, L"1.234321e-26" },
2426 { 1.234321234321234e-25L, L"1.234321e-25" },
2427 { 1.234321234321234e-24L, L"1.234321e-24" },
2428 { 1.234321234321234e-23L, L"1.234321e-23" },
2429 { 1.234321234321234e-22L, L"1.234321e-22" },
2430 { 1.234321234321234e-21L, L"1.234321e-21" },
2431 { 1.234321234321234e-20L, L"1.234321e-20" },
2432 { 1.234321234321234e-19L, L"1.234321e-19" },
2433 { 1.234321234321234e-18L, L"1.234321e-18" },
2434 { 1.234321234321234e-17L, L"1.234321e-17" },
2435 { 1.234321234321234e-16L, L"1.234321e-16" },
2436 { 1.234321234321234e-15L, L"1.234321e-15" },
2437 { 1.234321234321234e-14L, L"1.234321e-14" },
2438 { 1.234321234321234e-13L, L"1.234321e-13" },
2439 { 1.234321234321234e-12L, L"1.234321e-12" },
2440 { 1.234321234321234e-11L, L"1.234321e-11" },
2441 { 1.234321234321234e-10L, L"1.234321e-10" },
2442 { 1.234321234321234e-9L, L"1.234321e-09" },
2443 { 1.234321234321234e-8L, L"1.234321e-08" },
2444 { 1.234321234321234e-7L, L"1.234321e-07" },
2445 { 1.234321234321234e-6L, L"1.234321e-06" },
2446 { 1.234321234321234e-5L, L"1.234321e-05" },
2447 { 1.234321234321234e-4L, L"1.234321e-04" },
2448 { 1.234321234321234e-3L, L"1.234321e-03" },
2449 { 1.234321234321234e-2L, L"1.234321e-02" },
2450 { 1.234321234321234e-1L, L"1.234321e-01" },
2451 { 1.234321234321234L, L"1.234321e+00" },
2452 { 1.234321234321234e1L, L"1.234321e+01" },
2453 { 1.234321234321234e2L, L"1.234321e+02" },
2454 { 1.234321234321234e3L, L"1.234321e+03" },
2455 { 1.234321234321234e4L, L"1.234321e+04" },
2456 { 1.234321234321234e5L, L"1.234321e+05" },
2457 { 1.234321234321234e6L, L"1.234321e+06" },
2458 { 1.234321234321234e7L, L"1.234321e+07" },
2459 { 1.234321234321234e8L, L"1.234321e+08" },
2460 { 1.234321234321234e9L, L"1.234321e+09" },
2461 { 1.234321234321234e10L, L"1.234321e+10" },
2462 { 1.234321234321234e11L, L"1.234321e+11" },
2463 { 1.234321234321234e12L, L"1.234321e+12" },
2464 { 1.234321234321234e13L, L"1.234321e+13" },
2465 { 1.234321234321234e14L, L"1.234321e+14" },
2466 { 1.234321234321234e15L, L"1.234321e+15" },
2467 { 1.234321234321234e16L, L"1.234321e+16" },
2468 { 1.234321234321234e17L, L"1.234321e+17" },
2469 { 1.234321234321234e18L, L"1.234321e+18" },
2470 { 1.234321234321234e19L, L"1.234321e+19" },
2471 { 1.234321234321234e20L, L"1.234321e+20" },
2472 { 1.234321234321234e21L, L"1.234321e+21" },
2473 { 1.234321234321234e22L, L"1.234321e+22" },
2474 { 1.234321234321234e23L, L"1.234321e+23" },
2475 { 1.234321234321234e24L, L"1.234321e+24" },
2476 { 1.234321234321234e25L, L"1.234321e+25" },
2477 { 1.234321234321234e26L, L"1.234321e+26" },
2478 { 1.234321234321234e27L, L"1.234321e+27" },
2479 { 1.234321234321234e28L, L"1.234321e+28" },
2480 { 1.234321234321234e29L, L"1.234321e+29" },
2481 { 1.234321234321234e30L, L"1.234321e+30" },
2482 { 1.234321234321234e31L, L"1.234321e+31" },
2483 { 1.234321234321234e32L, L"1.234321e+32" },
2484 { 1.234321234321234e33L, L"1.234321e+33" },
2485 { 1.234321234321234e34L, L"1.234321e+34" },
2486 { 1.234321234321234e35L, L"1.234321e+35" },
2487 { 1.234321234321234e36L, L"1.234321e+36" }
2489 size_t k;
2490 for (k = 0; k < SIZEOF (data); k++)
2492 size_t length;
2493 wchar_t *result =
2494 my_asnwprintf (NULL, &length, L"%Le", data[k].value);
2495 const wchar_t *expected = data[k].string;
2496 ASSERT (result != NULL);
2497 ASSERT (wcscmp (result, expected) == 0
2498 /* Some implementations produce exponents with 3 digits. */
2499 || (wcslen (result) == wcslen (expected) + 1
2500 && wmemcmp (result, expected, wcslen (expected) - 2) == 0
2501 && result[wcslen (expected) - 2] == '0'
2502 && wcscmp (result + wcslen (expected) - 1,
2503 expected + wcslen (expected) - 2)
2504 == 0));
2505 ASSERT (length == wcslen (result));
2506 free (result);
2510 { /* A negative number. */
2511 size_t length;
2512 wchar_t *result =
2513 my_asnwprintf (NULL, &length, L"%Le %d", -0.03125L, 33, 44, 55);
2514 ASSERT (result != NULL);
2515 ASSERT (wcscmp (result, L"-3.125000e-02 33") == 0
2516 || wcscmp (result, L"-3.125000e-002 33") == 0);
2517 ASSERT (length == wcslen (result));
2518 free (result);
2521 { /* Positive zero. */
2522 size_t length;
2523 wchar_t *result =
2524 my_asnwprintf (NULL, &length, L"%Le %d", 0.0L, 33, 44, 55);
2525 ASSERT (result != NULL);
2526 ASSERT (wcscmp (result, L"0.000000e+00 33") == 0
2527 || wcscmp (result, L"0.000000e+000 33") == 0);
2528 ASSERT (length == wcslen (result));
2529 free (result);
2532 { /* Negative zero. */
2533 size_t length;
2534 wchar_t *result =
2535 my_asnwprintf (NULL, &length, L"%Le %d", minus_zerol, 33, 44, 55);
2536 ASSERT (result != NULL);
2537 if (have_minus_zero ())
2538 ASSERT (wcscmp (result, L"-0.000000e+00 33") == 0
2539 || wcscmp (result, L"-0.000000e+000 33") == 0);
2540 ASSERT (length == wcslen (result));
2541 free (result);
2544 { /* Positive infinity. */
2545 size_t length;
2546 wchar_t *result =
2547 my_asnwprintf (NULL, &length, L"%Le %d", Infinityl (), 33, 44, 55);
2548 ASSERT (result != NULL);
2549 ASSERT (wcscmp (result, L"inf 33") == 0
2550 || wcscmp (result, L"infinity 33") == 0);
2551 ASSERT (length == wcslen (result));
2552 free (result);
2555 { /* Negative infinity. */
2556 size_t length;
2557 wchar_t *result =
2558 my_asnwprintf (NULL, &length, L"%Le %d", - Infinityl (), 33, 44, 55);
2559 ASSERT (result != NULL);
2560 ASSERT (wcscmp (result, L"-inf 33") == 0
2561 || wcscmp (result, L"-infinity 33") == 0);
2562 ASSERT (length == wcslen (result));
2563 free (result);
2566 { /* NaN. */
2567 size_t length;
2568 wchar_t *result =
2569 my_asnwprintf (NULL, &length, L"%Le %d", NaNl (), 33, 44, 55);
2570 ASSERT (result != NULL);
2571 ASSERT (wcslen (result) >= 3 + 3
2572 && wcsisnan (result, 0, wcslen (result) - 3, 0)
2573 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
2574 ASSERT (length == wcslen (result));
2575 free (result);
2577 #if HAVE_SNANL
2578 { /* Signalling NaN. */
2579 size_t length;
2580 wchar_t *result =
2581 my_asnwprintf (NULL, &length, L"%Le %d", SNaNl (), 33, 44, 55);
2582 ASSERT (result != NULL);
2583 ASSERT (wcslen (result) >= 3 + 3
2584 && wcsisnan (result, 0, wcslen (result) - 3, 0)
2585 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
2586 ASSERT (length == wcslen (result));
2587 free (result);
2589 #endif
2590 #if CHECK_PRINTF_SAFE && ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_)) && !HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
2591 { /* Quiet NaN. */
2592 static union { unsigned int word[4]; long double value; } x =
2593 { .word = LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
2594 size_t length;
2595 wchar_t *result =
2596 my_asnwprintf (NULL, &length, L"%Le %d", x.value, 33, 44, 55);
2597 ASSERT (result != NULL);
2598 ASSERT (length == wcslen (result));
2599 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
2600 free (result);
2603 /* Signalling NaN. */
2604 static union { unsigned int word[4]; long double value; } x =
2605 { .word = LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
2606 size_t length;
2607 wchar_t *result =
2608 my_asnwprintf (NULL, &length, L"%Le %d", x.value, 33, 44, 55);
2609 ASSERT (result != NULL);
2610 ASSERT (length == wcslen (result));
2611 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
2612 free (result);
2614 /* asnwprintf should print something even for noncanonical values. */
2615 { /* Pseudo-NaN. */
2616 static union { unsigned int word[4]; long double value; } x =
2617 { .word = LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
2618 size_t length;
2619 wchar_t *result =
2620 my_asnwprintf (NULL, &length, L"%Le %d", x.value, 33, 44, 55);
2621 ASSERT (result != NULL);
2622 ASSERT (length == wcslen (result));
2623 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
2624 free (result);
2626 { /* Pseudo-Infinity. */
2627 static union { unsigned int word[4]; long double value; } x =
2628 { .word = LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
2629 size_t length;
2630 wchar_t *result =
2631 my_asnwprintf (NULL, &length, L"%Le %d", x.value, 33, 44, 55);
2632 ASSERT (result != NULL);
2633 ASSERT (length == wcslen (result));
2634 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
2635 free (result);
2637 { /* Pseudo-Zero. */
2638 static union { unsigned int word[4]; long double value; } x =
2639 { .word = LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
2640 size_t length;
2641 wchar_t *result =
2642 my_asnwprintf (NULL, &length, L"%Le %d", x.value, 33, 44, 55);
2643 ASSERT (result != NULL);
2644 ASSERT (length == wcslen (result));
2645 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
2646 free (result);
2648 { /* Unnormalized number. */
2649 static union { unsigned int word[4]; long double value; } x =
2650 { .word = LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
2651 size_t length;
2652 wchar_t *result =
2653 my_asnwprintf (NULL, &length, L"%Le %d", x.value, 33, 44, 55);
2654 ASSERT (result != NULL);
2655 ASSERT (length == wcslen (result));
2656 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
2657 free (result);
2659 { /* Pseudo-Denormal. */
2660 static union { unsigned int word[4]; long double value; } x =
2661 { .word = LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
2662 size_t length;
2663 wchar_t *result =
2664 my_asnwprintf (NULL, &length, L"%Le %d", x.value, 33, 44, 55);
2665 ASSERT (result != NULL);
2666 ASSERT (length == wcslen (result));
2667 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
2668 free (result);
2670 #endif
2672 { /* Width. */
2673 size_t length;
2674 wchar_t *result =
2675 my_asnwprintf (NULL, &length, L"%15Le %d", 1.75L, 33, 44, 55);
2676 ASSERT (result != NULL);
2677 ASSERT (wcscmp (result, L" 1.750000e+00 33") == 0
2678 || wcscmp (result, L" 1.750000e+000 33") == 0);
2679 ASSERT (length == wcslen (result));
2680 free (result);
2683 { /* Width given as argument. */
2684 size_t length;
2685 wchar_t *result =
2686 my_asnwprintf (NULL, &length, L"%*Le %d", 15, 1.75L, 33, 44, 55);
2687 ASSERT (result != NULL);
2688 ASSERT (wcscmp (result, L" 1.750000e+00 33") == 0
2689 || wcscmp (result, L" 1.750000e+000 33") == 0);
2690 ASSERT (length == wcslen (result));
2691 free (result);
2694 { /* Negative width given as argument (cf. FLAG_LEFT below). */
2695 size_t length;
2696 wchar_t *result =
2697 my_asnwprintf (NULL, &length, L"%*Le %d", -15, 1.75L, 33, 44, 55);
2698 ASSERT (result != NULL);
2699 ASSERT (wcscmp (result, L"1.750000e+00 33") == 0
2700 || wcscmp (result, L"1.750000e+000 33") == 0);
2701 ASSERT (length == wcslen (result));
2702 free (result);
2705 { /* FLAG_LEFT. */
2706 size_t length;
2707 wchar_t *result =
2708 my_asnwprintf (NULL, &length, L"%-15Le %d", 1.75L, 33, 44, 55);
2709 ASSERT (result != NULL);
2710 ASSERT (wcscmp (result, L"1.750000e+00 33") == 0
2711 || wcscmp (result, L"1.750000e+000 33") == 0);
2712 ASSERT (length == wcslen (result));
2713 free (result);
2716 { /* FLAG_SHOWSIGN. */
2717 size_t length;
2718 wchar_t *result =
2719 my_asnwprintf (NULL, &length, L"%+Le %d", 1.75L, 33, 44, 55);
2720 ASSERT (result != NULL);
2721 ASSERT (wcscmp (result, L"+1.750000e+00 33") == 0
2722 || wcscmp (result, L"+1.750000e+000 33") == 0);
2723 ASSERT (length == wcslen (result));
2724 free (result);
2727 { /* FLAG_SPACE. */
2728 size_t length;
2729 wchar_t *result =
2730 my_asnwprintf (NULL, &length, L"% Le %d", 1.75L, 33, 44, 55);
2731 ASSERT (result != NULL);
2732 ASSERT (wcscmp (result, L" 1.750000e+00 33") == 0
2733 || wcscmp (result, L" 1.750000e+000 33") == 0);
2734 ASSERT (length == wcslen (result));
2735 free (result);
2738 { /* FLAG_ALT. */
2739 size_t length;
2740 wchar_t *result =
2741 my_asnwprintf (NULL, &length, L"%#Le %d", 1.75L, 33, 44, 55);
2742 ASSERT (result != NULL);
2743 ASSERT (wcscmp (result, L"1.750000e+00 33") == 0
2744 || wcscmp (result, L"1.750000e+000 33") == 0);
2745 ASSERT (length == wcslen (result));
2746 free (result);
2749 { /* FLAG_ALT. */
2750 size_t length;
2751 wchar_t *result =
2752 my_asnwprintf (NULL, &length, L"%#.Le %d", 1.75L, 33, 44, 55);
2753 ASSERT (result != NULL);
2754 ASSERT (wcscmp (result, L"2.e+00 33") == 0
2755 || wcscmp (result, L"2.e+000 33") == 0);
2756 ASSERT (length == wcslen (result));
2757 free (result);
2760 { /* FLAG_ALT. */
2761 size_t length;
2762 wchar_t *result =
2763 my_asnwprintf (NULL, &length, L"%#.Le %d", 9.75L, 33, 44, 55);
2764 ASSERT (result != NULL);
2765 ASSERT (wcscmp (result, L"1.e+01 33") == 0
2766 || wcscmp (result, L"1.e+001 33") == 0);
2767 ASSERT (length == wcslen (result));
2768 free (result);
2771 { /* FLAG_ZERO with finite number. */
2772 size_t length;
2773 wchar_t *result =
2774 my_asnwprintf (NULL, &length, L"%015Le %d", 1234.0L, 33, 44, 55);
2775 ASSERT (result != NULL);
2776 ASSERT (wcscmp (result, L"0001.234000e+03 33") == 0
2777 || wcscmp (result, L"001.234000e+003 33") == 0);
2778 ASSERT (length == wcslen (result));
2779 free (result);
2782 { /* FLAG_ZERO with infinite number. */
2783 size_t length;
2784 wchar_t *result =
2785 my_asnwprintf (NULL, &length, L"%015Le %d", - Infinityl (), 33, 44, 55);
2786 ASSERT (result != NULL);
2787 ASSERT (wcscmp (result, L" -inf 33") == 0
2788 || wcscmp (result, L" -infinity 33") == 0);
2789 ASSERT (length == wcslen (result));
2790 free (result);
2793 { /* FLAG_ZERO with NaN. */
2794 size_t length;
2795 wchar_t *result =
2796 my_asnwprintf (NULL, &length, L"%050Le %d", NaNl (), 33, 44, 55);
2797 ASSERT (result != NULL);
2798 ASSERT (wcslen (result) == 50 + 3
2799 && wcsisnan (result, wcsspn (result, L" "), wcslen (result) - 3, 0)
2800 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
2801 ASSERT (length == wcslen (result));
2802 free (result);
2805 { /* Precision. */
2806 size_t length;
2807 wchar_t *result =
2808 my_asnwprintf (NULL, &length, L"%.Le %d", 1234.0L, 33, 44, 55);
2809 ASSERT (result != NULL);
2810 ASSERT (wcscmp (result, L"1e+03 33") == 0
2811 || wcscmp (result, L"1e+003 33") == 0);
2812 ASSERT (length == wcslen (result));
2813 free (result);
2816 { /* Precision with no rounding. */
2817 size_t length;
2818 wchar_t *result =
2819 my_asnwprintf (NULL, &length, L"%.4Le %d", 999.951L, 33, 44, 55);
2820 ASSERT (result != NULL);
2821 ASSERT (wcscmp (result, L"9.9995e+02 33") == 0
2822 || wcscmp (result, L"9.9995e+002 33") == 0);
2823 ASSERT (length == wcslen (result));
2824 free (result);
2827 { /* Precision with rounding. */
2828 size_t length;
2829 wchar_t *result =
2830 my_asnwprintf (NULL, &length, L"%.4Le %d", 999.996L, 33, 44, 55);
2831 ASSERT (result != NULL);
2832 ASSERT (wcscmp (result, L"1.0000e+03 33") == 0
2833 || wcscmp (result, L"1.0000e+003 33") == 0);
2834 ASSERT (length == wcslen (result));
2835 free (result);
2838 /* Test the support of the %g format directive. */
2840 { /* A positive number. */
2841 size_t length;
2842 wchar_t *result =
2843 my_asnwprintf (NULL, &length, L"%g %d", 12.75, 33, 44, 55);
2844 ASSERT (result != NULL);
2845 ASSERT (wcscmp (result, L"12.75 33") == 0);
2846 ASSERT (length == wcslen (result));
2847 free (result);
2850 { /* A larger positive number. */
2851 size_t length;
2852 wchar_t *result =
2853 my_asnwprintf (NULL, &length, L"%g %d", 1234567.0, 33, 44, 55);
2854 ASSERT (result != NULL);
2855 ASSERT (wcscmp (result, L"1.23457e+06 33") == 0
2856 || wcscmp (result, L"1.23457e+006 33") == 0);
2857 ASSERT (length == wcslen (result));
2858 free (result);
2861 { /* Small and large positive numbers. */
2862 static struct { double value; const wchar_t *string; } data[] =
2864 { 1.234321234321234e-37, L"1.23432e-37" },
2865 { 1.234321234321234e-36, L"1.23432e-36" },
2866 { 1.234321234321234e-35, L"1.23432e-35" },
2867 { 1.234321234321234e-34, L"1.23432e-34" },
2868 { 1.234321234321234e-33, L"1.23432e-33" },
2869 { 1.234321234321234e-32, L"1.23432e-32" },
2870 { 1.234321234321234e-31, L"1.23432e-31" },
2871 { 1.234321234321234e-30, L"1.23432e-30" },
2872 { 1.234321234321234e-29, L"1.23432e-29" },
2873 { 1.234321234321234e-28, L"1.23432e-28" },
2874 { 1.234321234321234e-27, L"1.23432e-27" },
2875 { 1.234321234321234e-26, L"1.23432e-26" },
2876 { 1.234321234321234e-25, L"1.23432e-25" },
2877 { 1.234321234321234e-24, L"1.23432e-24" },
2878 { 1.234321234321234e-23, L"1.23432e-23" },
2879 { 1.234321234321234e-22, L"1.23432e-22" },
2880 { 1.234321234321234e-21, L"1.23432e-21" },
2881 { 1.234321234321234e-20, L"1.23432e-20" },
2882 { 1.234321234321234e-19, L"1.23432e-19" },
2883 { 1.234321234321234e-18, L"1.23432e-18" },
2884 { 1.234321234321234e-17, L"1.23432e-17" },
2885 { 1.234321234321234e-16, L"1.23432e-16" },
2886 { 1.234321234321234e-15, L"1.23432e-15" },
2887 { 1.234321234321234e-14, L"1.23432e-14" },
2888 { 1.234321234321234e-13, L"1.23432e-13" },
2889 { 1.234321234321234e-12, L"1.23432e-12" },
2890 { 1.234321234321234e-11, L"1.23432e-11" },
2891 { 1.234321234321234e-10, L"1.23432e-10" },
2892 { 1.234321234321234e-9, L"1.23432e-09" },
2893 { 1.234321234321234e-8, L"1.23432e-08" },
2894 { 1.234321234321234e-7, L"1.23432e-07" },
2895 { 1.234321234321234e-6, L"1.23432e-06" },
2896 { 1.234321234321234e-5, L"1.23432e-05" },
2897 { 1.234321234321234e-4, L"0.000123432" },
2898 { 1.234321234321234e-3, L"0.00123432" },
2899 { 1.234321234321234e-2, L"0.0123432" },
2900 { 1.234321234321234e-1, L"0.123432" },
2901 { 1.234321234321234, L"1.23432" },
2902 { 1.234321234321234e1, L"12.3432" },
2903 { 1.234321234321234e2, L"123.432" },
2904 { 1.234321234321234e3, L"1234.32" },
2905 { 1.234321234321234e4, L"12343.2" },
2906 { 1.234321234321234e5, L"123432" },
2907 { 1.234321234321234e6, L"1.23432e+06" },
2908 { 1.234321234321234e7, L"1.23432e+07" },
2909 { 1.234321234321234e8, L"1.23432e+08" },
2910 { 1.234321234321234e9, L"1.23432e+09" },
2911 { 1.234321234321234e10, L"1.23432e+10" },
2912 { 1.234321234321234e11, L"1.23432e+11" },
2913 { 1.234321234321234e12, L"1.23432e+12" },
2914 { 1.234321234321234e13, L"1.23432e+13" },
2915 { 1.234321234321234e14, L"1.23432e+14" },
2916 { 1.234321234321234e15, L"1.23432e+15" },
2917 { 1.234321234321234e16, L"1.23432e+16" },
2918 { 1.234321234321234e17, L"1.23432e+17" },
2919 { 1.234321234321234e18, L"1.23432e+18" },
2920 { 1.234321234321234e19, L"1.23432e+19" },
2921 { 1.234321234321234e20, L"1.23432e+20" },
2922 { 1.234321234321234e21, L"1.23432e+21" },
2923 { 1.234321234321234e22, L"1.23432e+22" },
2924 { 1.234321234321234e23, L"1.23432e+23" },
2925 { 1.234321234321234e24, L"1.23432e+24" },
2926 { 1.234321234321234e25, L"1.23432e+25" },
2927 { 1.234321234321234e26, L"1.23432e+26" },
2928 { 1.234321234321234e27, L"1.23432e+27" },
2929 { 1.234321234321234e28, L"1.23432e+28" },
2930 { 1.234321234321234e29, L"1.23432e+29" },
2931 { 1.234321234321234e30, L"1.23432e+30" },
2932 { 1.234321234321234e31, L"1.23432e+31" },
2933 { 1.234321234321234e32, L"1.23432e+32" },
2934 { 1.234321234321234e33, L"1.23432e+33" },
2935 { 1.234321234321234e34, L"1.23432e+34" },
2936 { 1.234321234321234e35, L"1.23432e+35" },
2937 { 1.234321234321234e36, L"1.23432e+36" }
2939 size_t k;
2940 for (k = 0; k < SIZEOF (data); k++)
2942 size_t length;
2943 wchar_t *result =
2944 my_asnwprintf (NULL, &length, L"%g", data[k].value);
2945 const wchar_t *expected = data[k].string;
2946 ASSERT (result != NULL);
2947 ASSERT (wcscmp (result, expected) == 0
2948 /* Some implementations produce exponents with 3 digits. */
2949 || (expected[wcslen (expected) - 4] == 'e'
2950 && wcslen (result) == wcslen (expected) + 1
2951 && wmemcmp (result, expected, wcslen (expected) - 2) == 0
2952 && result[wcslen (expected) - 2] == '0'
2953 && wcscmp (result + wcslen (expected) - 1,
2954 expected + wcslen (expected) - 2)
2955 == 0));
2956 ASSERT (length == wcslen (result));
2957 free (result);
2961 { /* A negative number. */
2962 size_t length;
2963 wchar_t *result =
2964 my_asnwprintf (NULL, &length, L"%g %d", -0.03125, 33, 44, 55);
2965 ASSERT (result != NULL);
2966 ASSERT (wcscmp (result, L"-0.03125 33") == 0);
2967 ASSERT (length == wcslen (result));
2968 free (result);
2971 { /* Positive zero. */
2972 size_t length;
2973 wchar_t *result =
2974 my_asnwprintf (NULL, &length, L"%g %d", 0.0, 33, 44, 55);
2975 ASSERT (result != NULL);
2976 ASSERT (wcscmp (result, L"0 33") == 0);
2977 ASSERT (length == wcslen (result));
2978 free (result);
2981 { /* Negative zero. */
2982 size_t length;
2983 wchar_t *result =
2984 my_asnwprintf (NULL, &length, L"%g %d", minus_zerod, 33, 44, 55);
2985 ASSERT (result != NULL);
2986 if (have_minus_zero ())
2987 ASSERT (wcscmp (result, L"-0 33") == 0);
2988 ASSERT (length == wcslen (result));
2989 free (result);
2992 { /* Positive infinity. */
2993 size_t length;
2994 wchar_t *result =
2995 my_asnwprintf (NULL, &length, L"%g %d", Infinityd (), 33, 44, 55);
2996 ASSERT (result != NULL);
2997 ASSERT (wcscmp (result, L"inf 33") == 0
2998 || wcscmp (result, L"infinity 33") == 0);
2999 ASSERT (length == wcslen (result));
3000 free (result);
3003 { /* Negative infinity. */
3004 size_t length;
3005 wchar_t *result =
3006 my_asnwprintf (NULL, &length, L"%g %d", - Infinityd (), 33, 44, 55);
3007 ASSERT (result != NULL);
3008 ASSERT (wcscmp (result, L"-inf 33") == 0
3009 || wcscmp (result, L"-infinity 33") == 0);
3010 ASSERT (length == wcslen (result));
3011 free (result);
3014 { /* NaN. */
3015 size_t length;
3016 wchar_t *result =
3017 my_asnwprintf (NULL, &length, L"%g %d", NaNd (), 33, 44, 55);
3018 ASSERT (result != NULL);
3019 ASSERT (wcslen (result) >= 3 + 3
3020 && wcsisnan (result, 0, wcslen (result) - 3, 0)
3021 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
3022 ASSERT (length == wcslen (result));
3023 free (result);
3025 #if HAVE_SNAND
3026 { /* Signalling NaN. */
3027 size_t length;
3028 wchar_t *result =
3029 my_asnwprintf (NULL, &length, L"%g %d", SNaNd (), 33, 44, 55);
3030 ASSERT (result != NULL);
3031 ASSERT (wcslen (result) >= 3 + 3
3032 && wcsisnan (result, 0, wcslen (result) - 3, 0)
3033 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
3034 ASSERT (length == wcslen (result));
3035 free (result);
3037 #endif
3039 { /* Width. */
3040 size_t length;
3041 wchar_t *result =
3042 my_asnwprintf (NULL, &length, L"%10g %d", 1.75, 33, 44, 55);
3043 ASSERT (result != NULL);
3044 ASSERT (wcscmp (result, L" 1.75 33") == 0);
3045 ASSERT (length == wcslen (result));
3046 free (result);
3049 { /* Width given as argument. */
3050 size_t length;
3051 wchar_t *result =
3052 my_asnwprintf (NULL, &length, L"%*g %d", 10, 1.75, 33, 44, 55);
3053 ASSERT (result != NULL);
3054 ASSERT (wcscmp (result, L" 1.75 33") == 0);
3055 ASSERT (length == wcslen (result));
3056 free (result);
3059 { /* Negative width given as argument (cf. FLAG_LEFT below). */
3060 size_t length;
3061 wchar_t *result =
3062 my_asnwprintf (NULL, &length, L"%*g %d", -10, 1.75, 33, 44, 55);
3063 ASSERT (result != NULL);
3064 ASSERT (wcscmp (result, L"1.75 33") == 0);
3065 ASSERT (length == wcslen (result));
3066 free (result);
3069 { /* FLAG_LEFT. */
3070 size_t length;
3071 wchar_t *result =
3072 my_asnwprintf (NULL, &length, L"%-10g %d", 1.75, 33, 44, 55);
3073 ASSERT (result != NULL);
3074 ASSERT (wcscmp (result, L"1.75 33") == 0);
3075 ASSERT (length == wcslen (result));
3076 free (result);
3079 { /* FLAG_SHOWSIGN. */
3080 size_t length;
3081 wchar_t *result =
3082 my_asnwprintf (NULL, &length, L"%+g %d", 1.75, 33, 44, 55);
3083 ASSERT (result != NULL);
3084 ASSERT (wcscmp (result, L"+1.75 33") == 0);
3085 ASSERT (length == wcslen (result));
3086 free (result);
3089 { /* FLAG_SPACE. */
3090 size_t length;
3091 wchar_t *result =
3092 my_asnwprintf (NULL, &length, L"% g %d", 1.75, 33, 44, 55);
3093 ASSERT (result != NULL);
3094 ASSERT (wcscmp (result, L" 1.75 33") == 0);
3095 ASSERT (length == wcslen (result));
3096 free (result);
3099 { /* FLAG_ALT. */
3100 size_t length;
3101 wchar_t *result =
3102 my_asnwprintf (NULL, &length, L"%#g %d", 1.75, 33, 44, 55);
3103 ASSERT (result != NULL);
3104 ASSERT (wcscmp (result, L"1.75000 33") == 0);
3105 ASSERT (length == wcslen (result));
3106 free (result);
3109 { /* FLAG_ALT. */
3110 size_t length;
3111 wchar_t *result =
3112 my_asnwprintf (NULL, &length, L"%#.g %d", 1.75, 33, 44, 55);
3113 ASSERT (result != NULL);
3114 ASSERT (wcscmp (result, L"2. 33") == 0);
3115 ASSERT (length == wcslen (result));
3116 free (result);
3119 { /* FLAG_ALT. */
3120 size_t length;
3121 wchar_t *result =
3122 my_asnwprintf (NULL, &length, L"%#.g %d", 9.75, 33, 44, 55);
3123 ASSERT (result != NULL);
3124 ASSERT (wcscmp (result, L"1.e+01 33") == 0
3125 || wcscmp (result, L"1.e+001 33") == 0);
3126 ASSERT (length == wcslen (result));
3127 free (result);
3130 { /* FLAG_ZERO with finite number. */
3131 size_t length;
3132 wchar_t *result =
3133 my_asnwprintf (NULL, &length, L"%010g %d", 1234.0, 33, 44, 55);
3134 ASSERT (result != NULL);
3135 ASSERT (wcscmp (result, L"0000001234 33") == 0);
3136 ASSERT (length == wcslen (result));
3137 free (result);
3140 { /* FLAG_ZERO with infinite number. */
3141 size_t length;
3142 wchar_t *result =
3143 my_asnwprintf (NULL, &length, L"%015g %d", - Infinityd (), 33, 44, 55);
3144 ASSERT (result != NULL);
3145 ASSERT (wcscmp (result, L" -inf 33") == 0
3146 || wcscmp (result, L" -infinity 33") == 0);
3147 ASSERT (length == wcslen (result));
3148 free (result);
3151 { /* FLAG_ZERO with NaN. */
3152 size_t length;
3153 wchar_t *result =
3154 my_asnwprintf (NULL, &length, L"%050g %d", NaNd (), 33, 44, 55);
3155 ASSERT (result != NULL);
3156 ASSERT (wcslen (result) == 50 + 3
3157 && wcsisnan (result, wcsspn (result, L" "), wcslen (result) - 3, 0)
3158 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
3159 ASSERT (length == wcslen (result));
3160 free (result);
3163 { /* Precision. */
3164 size_t length;
3165 wchar_t *result =
3166 my_asnwprintf (NULL, &length, L"%.g %d", 1234.0, 33, 44, 55);
3167 ASSERT (result != NULL);
3168 ASSERT (wcscmp (result, L"1e+03 33") == 0
3169 || wcscmp (result, L"1e+003 33") == 0);
3170 ASSERT (length == wcslen (result));
3171 free (result);
3174 { /* Precision with no rounding. */
3175 size_t length;
3176 wchar_t *result =
3177 my_asnwprintf (NULL, &length, L"%.5g %d", 999.951, 33, 44, 55);
3178 ASSERT (result != NULL);
3179 ASSERT (wcscmp (result, L"999.95 33") == 0);
3180 ASSERT (length == wcslen (result));
3181 free (result);
3184 { /* Precision with rounding. */
3185 size_t length;
3186 wchar_t *result =
3187 my_asnwprintf (NULL, &length, L"%.5g %d", 999.996, 33, 44, 55);
3188 ASSERT (result != NULL);
3189 ASSERT (wcscmp (result, L"1000 33") == 0);
3190 ASSERT (length == wcslen (result));
3191 free (result);
3194 { /* A positive number. */
3195 size_t length;
3196 wchar_t *result =
3197 my_asnwprintf (NULL, &length, L"%Lg %d", 12.75L, 33, 44, 55);
3198 ASSERT (result != NULL);
3199 ASSERT (wcscmp (result, L"12.75 33") == 0);
3200 ASSERT (length == wcslen (result));
3201 free (result);
3204 { /* A larger positive number. */
3205 size_t length;
3206 wchar_t *result =
3207 my_asnwprintf (NULL, &length, L"%Lg %d", 1234567.0L, 33, 44, 55);
3208 ASSERT (result != NULL);
3209 ASSERT (wcscmp (result, L"1.23457e+06 33") == 0
3210 || wcscmp (result, L"1.23457e+006 33") == 0);
3211 ASSERT (length == wcslen (result));
3212 free (result);
3215 { /* Small and large positive numbers. */
3216 static struct { long double value; const wchar_t *string; } data[] =
3218 { 1.234321234321234e-37L, L"1.23432e-37" },
3219 { 1.234321234321234e-36L, L"1.23432e-36" },
3220 { 1.234321234321234e-35L, L"1.23432e-35" },
3221 { 1.234321234321234e-34L, L"1.23432e-34" },
3222 { 1.234321234321234e-33L, L"1.23432e-33" },
3223 { 1.234321234321234e-32L, L"1.23432e-32" },
3224 { 1.234321234321234e-31L, L"1.23432e-31" },
3225 { 1.234321234321234e-30L, L"1.23432e-30" },
3226 { 1.234321234321234e-29L, L"1.23432e-29" },
3227 { 1.234321234321234e-28L, L"1.23432e-28" },
3228 { 1.234321234321234e-27L, L"1.23432e-27" },
3229 { 1.234321234321234e-26L, L"1.23432e-26" },
3230 { 1.234321234321234e-25L, L"1.23432e-25" },
3231 { 1.234321234321234e-24L, L"1.23432e-24" },
3232 { 1.234321234321234e-23L, L"1.23432e-23" },
3233 { 1.234321234321234e-22L, L"1.23432e-22" },
3234 { 1.234321234321234e-21L, L"1.23432e-21" },
3235 { 1.234321234321234e-20L, L"1.23432e-20" },
3236 { 1.234321234321234e-19L, L"1.23432e-19" },
3237 { 1.234321234321234e-18L, L"1.23432e-18" },
3238 { 1.234321234321234e-17L, L"1.23432e-17" },
3239 { 1.234321234321234e-16L, L"1.23432e-16" },
3240 { 1.234321234321234e-15L, L"1.23432e-15" },
3241 { 1.234321234321234e-14L, L"1.23432e-14" },
3242 { 1.234321234321234e-13L, L"1.23432e-13" },
3243 { 1.234321234321234e-12L, L"1.23432e-12" },
3244 { 1.234321234321234e-11L, L"1.23432e-11" },
3245 { 1.234321234321234e-10L, L"1.23432e-10" },
3246 { 1.234321234321234e-9L, L"1.23432e-09" },
3247 { 1.234321234321234e-8L, L"1.23432e-08" },
3248 { 1.234321234321234e-7L, L"1.23432e-07" },
3249 { 1.234321234321234e-6L, L"1.23432e-06" },
3250 { 1.234321234321234e-5L, L"1.23432e-05" },
3251 { 1.234321234321234e-4L, L"0.000123432" },
3252 { 1.234321234321234e-3L, L"0.00123432" },
3253 { 1.234321234321234e-2L, L"0.0123432" },
3254 { 1.234321234321234e-1L, L"0.123432" },
3255 { 1.234321234321234L, L"1.23432" },
3256 { 1.234321234321234e1L, L"12.3432" },
3257 { 1.234321234321234e2L, L"123.432" },
3258 { 1.234321234321234e3L, L"1234.32" },
3259 { 1.234321234321234e4L, L"12343.2" },
3260 { 1.234321234321234e5L, L"123432" },
3261 { 1.234321234321234e6L, L"1.23432e+06" },
3262 { 1.234321234321234e7L, L"1.23432e+07" },
3263 { 1.234321234321234e8L, L"1.23432e+08" },
3264 { 1.234321234321234e9L, L"1.23432e+09" },
3265 { 1.234321234321234e10L, L"1.23432e+10" },
3266 { 1.234321234321234e11L, L"1.23432e+11" },
3267 { 1.234321234321234e12L, L"1.23432e+12" },
3268 { 1.234321234321234e13L, L"1.23432e+13" },
3269 { 1.234321234321234e14L, L"1.23432e+14" },
3270 { 1.234321234321234e15L, L"1.23432e+15" },
3271 { 1.234321234321234e16L, L"1.23432e+16" },
3272 { 1.234321234321234e17L, L"1.23432e+17" },
3273 { 1.234321234321234e18L, L"1.23432e+18" },
3274 { 1.234321234321234e19L, L"1.23432e+19" },
3275 { 1.234321234321234e20L, L"1.23432e+20" },
3276 { 1.234321234321234e21L, L"1.23432e+21" },
3277 { 1.234321234321234e22L, L"1.23432e+22" },
3278 { 1.234321234321234e23L, L"1.23432e+23" },
3279 { 1.234321234321234e24L, L"1.23432e+24" },
3280 { 1.234321234321234e25L, L"1.23432e+25" },
3281 { 1.234321234321234e26L, L"1.23432e+26" },
3282 { 1.234321234321234e27L, L"1.23432e+27" },
3283 { 1.234321234321234e28L, L"1.23432e+28" },
3284 { 1.234321234321234e29L, L"1.23432e+29" },
3285 { 1.234321234321234e30L, L"1.23432e+30" },
3286 { 1.234321234321234e31L, L"1.23432e+31" },
3287 { 1.234321234321234e32L, L"1.23432e+32" },
3288 { 1.234321234321234e33L, L"1.23432e+33" },
3289 { 1.234321234321234e34L, L"1.23432e+34" },
3290 { 1.234321234321234e35L, L"1.23432e+35" },
3291 { 1.234321234321234e36L, L"1.23432e+36" }
3293 size_t k;
3294 for (k = 0; k < SIZEOF (data); k++)
3296 size_t length;
3297 wchar_t *result =
3298 my_asnwprintf (NULL, &length, L"%Lg", data[k].value);
3299 const wchar_t *expected = data[k].string;
3300 ASSERT (result != NULL);
3301 ASSERT (wcscmp (result, expected) == 0
3302 /* Some implementations produce exponents with 3 digits. */
3303 || (expected[wcslen (expected) - 4] == 'e'
3304 && wcslen (result) == wcslen (expected) + 1
3305 && wmemcmp (result, expected, wcslen (expected) - 2) == 0
3306 && result[wcslen (expected) - 2] == '0'
3307 && wcscmp (result + wcslen (expected) - 1,
3308 expected + wcslen (expected) - 2)
3309 == 0));
3310 ASSERT (length == wcslen (result));
3311 free (result);
3315 { /* A negative number. */
3316 size_t length;
3317 wchar_t *result =
3318 my_asnwprintf (NULL, &length, L"%Lg %d", -0.03125L, 33, 44, 55);
3319 ASSERT (result != NULL);
3320 ASSERT (wcscmp (result, L"-0.03125 33") == 0);
3321 ASSERT (length == wcslen (result));
3322 free (result);
3325 { /* Positive zero. */
3326 size_t length;
3327 wchar_t *result =
3328 my_asnwprintf (NULL, &length, L"%Lg %d", 0.0L, 33, 44, 55);
3329 ASSERT (result != NULL);
3330 ASSERT (wcscmp (result, L"0 33") == 0);
3331 ASSERT (length == wcslen (result));
3332 free (result);
3335 { /* Negative zero. */
3336 size_t length;
3337 wchar_t *result =
3338 my_asnwprintf (NULL, &length, L"%Lg %d", minus_zerol, 33, 44, 55);
3339 ASSERT (result != NULL);
3340 if (have_minus_zero ())
3341 ASSERT (wcscmp (result, L"-0 33") == 0);
3342 ASSERT (length == wcslen (result));
3343 free (result);
3346 { /* Positive infinity. */
3347 size_t length;
3348 wchar_t *result =
3349 my_asnwprintf (NULL, &length, L"%Lg %d", Infinityl (), 33, 44, 55);
3350 ASSERT (result != NULL);
3351 ASSERT (wcscmp (result, L"inf 33") == 0
3352 || wcscmp (result, L"infinity 33") == 0);
3353 ASSERT (length == wcslen (result));
3354 free (result);
3357 { /* Negative infinity. */
3358 size_t length;
3359 wchar_t *result =
3360 my_asnwprintf (NULL, &length, L"%Lg %d", - Infinityl (), 33, 44, 55);
3361 ASSERT (result != NULL);
3362 ASSERT (wcscmp (result, L"-inf 33") == 0
3363 || wcscmp (result, L"-infinity 33") == 0);
3364 ASSERT (length == wcslen (result));
3365 free (result);
3368 { /* NaN. */
3369 size_t length;
3370 wchar_t *result =
3371 my_asnwprintf (NULL, &length, L"%Lg %d", NaNl (), 33, 44, 55);
3372 ASSERT (result != NULL);
3373 ASSERT (wcslen (result) >= 3 + 3
3374 && wcsisnan (result, 0, wcslen (result) - 3, 0)
3375 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
3376 ASSERT (length == wcslen (result));
3377 free (result);
3379 #if HAVE_SNANL
3380 { /* Signalling NaN. */
3381 size_t length;
3382 wchar_t *result =
3383 my_asnwprintf (NULL, &length, L"%Lg %d", SNaNl (), 33, 44, 55);
3384 ASSERT (result != NULL);
3385 ASSERT (wcslen (result) >= 3 + 3
3386 && wcsisnan (result, 0, wcslen (result) - 3, 0)
3387 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
3388 ASSERT (length == wcslen (result));
3389 free (result);
3391 #endif
3392 #if CHECK_PRINTF_SAFE && ((defined __ia64 && LDBL_MANT_DIG == 64) || (defined __x86_64__ || defined __amd64__) || (defined __i386 || defined __i386__ || defined _I386 || defined _M_IX86 || defined _X86_)) && !HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
3393 { /* Quiet NaN. */
3394 static union { unsigned int word[4]; long double value; } x =
3395 { .word = LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) };
3396 size_t length;
3397 wchar_t *result =
3398 my_asnwprintf (NULL, &length, L"%Lg %d", x.value, 33, 44, 55);
3399 ASSERT (result != NULL);
3400 ASSERT (wcslen (result) >= 3 + 3
3401 && wcsisnan (result, 0, wcslen (result) - 3, 0)
3402 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
3403 ASSERT (length == wcslen (result));
3404 free (result);
3407 /* Signalling NaN. */
3408 static union { unsigned int word[4]; long double value; } x =
3409 { .word = LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) };
3410 size_t length;
3411 wchar_t *result =
3412 my_asnwprintf (NULL, &length, L"%Lg %d", x.value, 33, 44, 55);
3413 ASSERT (result != NULL);
3414 ASSERT (wcslen (result) >= 3 + 3
3415 && wcsisnan (result, 0, wcslen (result) - 3, 0)
3416 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
3417 ASSERT (length == wcslen (result));
3418 free (result);
3420 /* asnwprintf should print something for noncanonical values. */
3421 { /* Pseudo-NaN. */
3422 static union { unsigned int word[4]; long double value; } x =
3423 { .word = LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) };
3424 size_t length;
3425 wchar_t *result =
3426 my_asnwprintf (NULL, &length, L"%Lg %d", x.value, 33, 44, 55);
3427 ASSERT (result != NULL);
3428 ASSERT (length == wcslen (result));
3429 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
3430 free (result);
3432 { /* Pseudo-Infinity. */
3433 static union { unsigned int word[4]; long double value; } x =
3434 { .word = LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) };
3435 size_t length;
3436 wchar_t *result =
3437 my_asnwprintf (NULL, &length, L"%Lg %d", x.value, 33, 44, 55);
3438 ASSERT (result != NULL);
3439 ASSERT (length == wcslen (result));
3440 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
3441 free (result);
3443 { /* Pseudo-Zero. */
3444 static union { unsigned int word[4]; long double value; } x =
3445 { .word = LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) };
3446 size_t length;
3447 wchar_t *result =
3448 my_asnwprintf (NULL, &length, L"%Lg %d", x.value, 33, 44, 55);
3449 ASSERT (result != NULL);
3450 ASSERT (length == wcslen (result));
3451 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
3452 free (result);
3454 { /* Unnormalized number. */
3455 static union { unsigned int word[4]; long double value; } x =
3456 { .word = LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) };
3457 size_t length;
3458 wchar_t *result =
3459 my_asnwprintf (NULL, &length, L"%Lg %d", x.value, 33, 44, 55);
3460 ASSERT (result != NULL);
3461 ASSERT (length == wcslen (result));
3462 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
3463 free (result);
3465 { /* Pseudo-Denormal. */
3466 static union { unsigned int word[4]; long double value; } x =
3467 { .word = LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) };
3468 size_t length;
3469 wchar_t *result =
3470 my_asnwprintf (NULL, &length, L"%Lg %d", x.value, 33, 44, 55);
3471 ASSERT (result != NULL);
3472 ASSERT (length == wcslen (result));
3473 ASSERT (3 < length && wcscmp (result + length - 3, L" 33") == 0);
3474 free (result);
3476 #endif
3478 { /* Width. */
3479 size_t length;
3480 wchar_t *result =
3481 my_asnwprintf (NULL, &length, L"%10Lg %d", 1.75L, 33, 44, 55);
3482 ASSERT (result != NULL);
3483 ASSERT (wcscmp (result, L" 1.75 33") == 0);
3484 ASSERT (length == wcslen (result));
3485 free (result);
3488 { /* Width given as argument. */
3489 size_t length;
3490 wchar_t *result =
3491 my_asnwprintf (NULL, &length, L"%*Lg %d", 10, 1.75L, 33, 44, 55);
3492 ASSERT (result != NULL);
3493 ASSERT (wcscmp (result, L" 1.75 33") == 0);
3494 ASSERT (length == wcslen (result));
3495 free (result);
3498 { /* Negative width given as argument (cf. FLAG_LEFT below). */
3499 size_t length;
3500 wchar_t *result =
3501 my_asnwprintf (NULL, &length, L"%*Lg %d", -10, 1.75L, 33, 44, 55);
3502 ASSERT (result != NULL);
3503 ASSERT (wcscmp (result, L"1.75 33") == 0);
3504 ASSERT (length == wcslen (result));
3505 free (result);
3508 { /* FLAG_LEFT. */
3509 size_t length;
3510 wchar_t *result =
3511 my_asnwprintf (NULL, &length, L"%-10Lg %d", 1.75L, 33, 44, 55);
3512 ASSERT (result != NULL);
3513 ASSERT (wcscmp (result, L"1.75 33") == 0);
3514 ASSERT (length == wcslen (result));
3515 free (result);
3518 { /* FLAG_SHOWSIGN. */
3519 size_t length;
3520 wchar_t *result =
3521 my_asnwprintf (NULL, &length, L"%+Lg %d", 1.75L, 33, 44, 55);
3522 ASSERT (result != NULL);
3523 ASSERT (wcscmp (result, L"+1.75 33") == 0);
3524 ASSERT (length == wcslen (result));
3525 free (result);
3528 { /* FLAG_SPACE. */
3529 size_t length;
3530 wchar_t *result =
3531 my_asnwprintf (NULL, &length, L"% Lg %d", 1.75L, 33, 44, 55);
3532 ASSERT (result != NULL);
3533 ASSERT (wcscmp (result, L" 1.75 33") == 0);
3534 ASSERT (length == wcslen (result));
3535 free (result);
3538 { /* FLAG_ALT. */
3539 size_t length;
3540 wchar_t *result =
3541 my_asnwprintf (NULL, &length, L"%#Lg %d", 1.75L, 33, 44, 55);
3542 ASSERT (result != NULL);
3543 ASSERT (wcscmp (result, L"1.75000 33") == 0);
3544 ASSERT (length == wcslen (result));
3545 free (result);
3548 { /* FLAG_ALT. */
3549 size_t length;
3550 wchar_t *result =
3551 my_asnwprintf (NULL, &length, L"%#.Lg %d", 1.75L, 33, 44, 55);
3552 ASSERT (result != NULL);
3553 ASSERT (wcscmp (result, L"2. 33") == 0);
3554 ASSERT (length == wcslen (result));
3555 free (result);
3558 { /* FLAG_ALT. */
3559 size_t length;
3560 wchar_t *result =
3561 my_asnwprintf (NULL, &length, L"%#.Lg %d", 9.75L, 33, 44, 55);
3562 ASSERT (result != NULL);
3563 ASSERT (wcscmp (result, L"1.e+01 33") == 0
3564 || wcscmp (result, L"1.e+001 33") == 0);
3565 ASSERT (length == wcslen (result));
3566 free (result);
3569 { /* FLAG_ZERO with finite number. */
3570 size_t length;
3571 wchar_t *result =
3572 my_asnwprintf (NULL, &length, L"%010Lg %d", 1234.0L, 33, 44, 55);
3573 ASSERT (result != NULL);
3574 ASSERT (wcscmp (result, L"0000001234 33") == 0);
3575 ASSERT (length == wcslen (result));
3576 free (result);
3579 { /* FLAG_ZERO with infinite number. */
3580 size_t length;
3581 wchar_t *result =
3582 my_asnwprintf (NULL, &length, L"%015Lg %d", - Infinityl (), 33, 44, 55);
3583 ASSERT (result != NULL);
3584 ASSERT (wcscmp (result, L" -inf 33") == 0
3585 || wcscmp (result, L" -infinity 33") == 0);
3586 ASSERT (length == wcslen (result));
3587 free (result);
3590 { /* FLAG_ZERO with NaN. */
3591 size_t length;
3592 wchar_t *result =
3593 my_asnwprintf (NULL, &length, L"%050Lg %d", NaNl (), 33, 44, 55);
3594 ASSERT (result != NULL);
3595 ASSERT (wcslen (result) == 50 + 3
3596 && wcsisnan (result, wcsspn (result, L" "), wcslen (result) - 3, 0)
3597 && wcscmp (result + wcslen (result) - 3, L" 33") == 0);
3598 ASSERT (length == wcslen (result));
3599 free (result);
3602 { /* Precision. */
3603 size_t length;
3604 wchar_t *result =
3605 my_asnwprintf (NULL, &length, L"%.Lg %d", 1234.0L, 33, 44, 55);
3606 ASSERT (result != NULL);
3607 ASSERT (wcscmp (result, L"1e+03 33") == 0
3608 || wcscmp (result, L"1e+003 33") == 0);
3609 ASSERT (length == wcslen (result));
3610 free (result);
3613 { /* Precision with no rounding. */
3614 size_t length;
3615 wchar_t *result =
3616 my_asnwprintf (NULL, &length, L"%.5Lg %d", 999.951L, 33, 44, 55);
3617 ASSERT (result != NULL);
3618 ASSERT (wcscmp (result, L"999.95 33") == 0);
3619 ASSERT (length == wcslen (result));
3620 free (result);
3623 { /* Precision with rounding. */
3624 size_t length;
3625 wchar_t *result =
3626 my_asnwprintf (NULL, &length, L"%.5Lg %d", 999.996L, 33, 44, 55);
3627 ASSERT (result != NULL);
3628 ASSERT (wcscmp (result, L"1000 33") == 0);
3629 ASSERT (length == wcslen (result));
3630 free (result);
3633 #if NEED_PRINTF_WITH_N_DIRECTIVE
3634 /* Test the support of the %n format directive. */
3637 int count = -1;
3638 size_t length;
3639 wchar_t *result =
3640 my_asnwprintf (NULL, &length, L"%d %n", 123, &count, 33, 44, 55);
3641 ASSERT (result != NULL);
3642 ASSERT (wcscmp (result, L"123 ") == 0);
3643 ASSERT (length == wcslen (result));
3644 ASSERT (count == 4);
3645 free (result);
3647 #endif
3649 /* Test the support of the POSIX/XSI format strings with positions. */
3652 size_t length;
3653 wchar_t *result =
3654 my_asnwprintf (NULL, &length, L"%2$d %1$d", 33, 55);
3655 ASSERT (result != NULL);
3656 ASSERT (wcscmp (result, L"55 33") == 0);
3657 ASSERT (length == wcslen (result));
3658 free (result);
3661 /* Test the support of the grouping flag. */
3664 size_t length;
3665 wchar_t *result =
3666 my_asnwprintf (NULL, &length, L"%'d %d", 1234567, 99);
3667 ASSERT (result != NULL);
3668 ASSERT (result[wcslen (result) - 1] == '9');
3669 ASSERT (length == wcslen (result));
3670 free (result);
3673 /* Test the support of the left-adjust flag. */
3676 size_t length;
3677 wchar_t *result =
3678 my_asnwprintf (NULL, &length, L"a%*sc", -3, "b");
3679 ASSERT (result != NULL);
3680 ASSERT (wcscmp (result, L"ab c") == 0);
3681 ASSERT (length == wcslen (result));
3682 free (result);
3686 size_t length;
3687 wchar_t *result =
3688 my_asnwprintf (NULL, &length, L"a%-*sc", 3, "b");
3689 ASSERT (result != NULL);
3690 ASSERT (wcscmp (result, L"ab c") == 0);
3691 ASSERT (length == wcslen (result));
3692 free (result);
3696 size_t length;
3697 wchar_t *result =
3698 my_asnwprintf (NULL, &length, L"a%-*sc", -3, "b");
3699 ASSERT (result != NULL);
3700 ASSERT (wcscmp (result, L"ab c") == 0);
3701 ASSERT (length == wcslen (result));
3702 free (result);
3705 /* Test the support of large precision. */
3708 size_t length;
3709 wchar_t *result =
3710 my_asnwprintf (NULL, &length, L"%.4000d %d", 1234567, 99);
3711 size_t i;
3712 ASSERT (result != NULL);
3713 for (i = 0; i < 4000 - 7; i++)
3714 ASSERT (result[i] == '0');
3715 ASSERT (wcscmp (result + 4000 - 7, L"1234567 99") == 0);
3716 ASSERT (length == wcslen (result));
3717 free (result);
3721 size_t length;
3722 wchar_t *result =
3723 my_asnwprintf (NULL, &length, L"%.*d %d", 4000, 1234567, 99);
3724 size_t i;
3725 ASSERT (result != NULL);
3726 for (i = 0; i < 4000 - 7; i++)
3727 ASSERT (result[i] == '0');
3728 ASSERT (wcscmp (result + 4000 - 7, L"1234567 99") == 0);
3729 ASSERT (length == wcslen (result));
3730 free (result);
3734 size_t length;
3735 wchar_t *result =
3736 my_asnwprintf (NULL, &length, L"%.4000d %d", -1234567, 99);
3737 size_t i;
3738 ASSERT (result != NULL);
3739 ASSERT (result[0] == '-');
3740 for (i = 0; i < 4000 - 7; i++)
3741 ASSERT (result[1 + i] == '0');
3742 ASSERT (wcscmp (result + 1 + 4000 - 7, L"1234567 99") == 0);
3743 ASSERT (length == wcslen (result));
3744 free (result);
3748 size_t length;
3749 wchar_t *result =
3750 my_asnwprintf (NULL, &length, L"%.4000u %d", 1234567, 99);
3751 size_t i;
3752 ASSERT (result != NULL);
3753 for (i = 0; i < 4000 - 7; i++)
3754 ASSERT (result[i] == '0');
3755 ASSERT (wcscmp (result + 4000 - 7, L"1234567 99") == 0);
3756 ASSERT (length == wcslen (result));
3757 free (result);
3761 size_t length;
3762 wchar_t *result =
3763 my_asnwprintf (NULL, &length, L"%.4000o %d", 1234567, 99);
3764 size_t i;
3765 ASSERT (result != NULL);
3766 for (i = 0; i < 4000 - 7; i++)
3767 ASSERT (result[i] == '0');
3768 ASSERT (wcscmp (result + 4000 - 7, L"4553207 99") == 0);
3769 ASSERT (length == wcslen (result));
3770 free (result);
3774 size_t length;
3775 wchar_t *result =
3776 my_asnwprintf (NULL, &length, L"%.4000x %d", 1234567, 99);
3777 size_t i;
3778 ASSERT (result != NULL);
3779 for (i = 0; i < 4000 - 6; i++)
3780 ASSERT (result[i] == '0');
3781 ASSERT (wcscmp (result + 4000 - 6, L"12d687 99") == 0);
3782 ASSERT (length == wcslen (result));
3783 free (result);
3787 size_t length;
3788 wchar_t *result =
3789 my_asnwprintf (NULL, &length, L"%#.4000x %d", 1234567, 99);
3790 size_t i;
3791 ASSERT (result != NULL);
3792 ASSERT (result[0] == '0');
3793 ASSERT (result[1] == 'x');
3794 for (i = 0; i < 4000 - 6; i++)
3795 ASSERT (result[2 + i] == '0');
3796 ASSERT (wcscmp (result + 2 + 4000 - 6, L"12d687 99") == 0);
3797 ASSERT (length == wcslen (result));
3798 free (result);
3802 size_t length;
3803 wchar_t *result =
3804 my_asnwprintf (NULL, &length, L"%.4000f %d", 1.0, 99);
3805 size_t i;
3806 ASSERT (result != NULL);
3807 ASSERT (result[0] == '1');
3808 ASSERT (result[1] == '.');
3809 for (i = 0; i < 4000; i++)
3810 ASSERT (result[2 + i] == '0');
3811 ASSERT (wcscmp (result + 2 + 4000, L" 99") == 0);
3812 ASSERT (length == wcslen (result));
3813 free (result);
3817 size_t length;
3818 wchar_t *result =
3819 my_asnwprintf (NULL, &length, L"%.511f %d", 1.0, 99);
3820 size_t i;
3821 ASSERT (result != NULL);
3822 ASSERT (result[0] == '1');
3823 ASSERT (result[1] == '.');
3824 for (i = 0; i < 511; i++)
3825 ASSERT (result[2 + i] == '0');
3826 ASSERT (wcscmp (result + 2 + 511, L" 99") == 0);
3827 ASSERT (length == wcslen (result));
3828 free (result);
3832 char input[5000];
3833 size_t length;
3834 wchar_t *result;
3835 wchar_t winput[5000];
3836 size_t i;
3838 for (i = 0; i < sizeof (input) - 1; i++)
3839 input[i] = 'a' + ((1000000 / (i + 1)) % 26);
3840 input[i] = '\0';
3841 result = my_asnwprintf (NULL, &length, L"%.4000s %d", input, 99);
3842 ASSERT (result != NULL);
3843 for (i = 0; i < sizeof (input); i++)
3844 winput[i] = (wchar_t) input[i];
3845 ASSERT (wmemcmp (result, winput, 4000) == 0);
3846 ASSERT (wcscmp (result + 4000, L" 99") == 0);
3847 ASSERT (length == wcslen (result));
3848 free (result);
3851 /* Test the support of the %s format directive. */
3853 { /* Width. */
3854 size_t length;
3855 wchar_t *result =
3856 my_asnwprintf (NULL, &length, L"%10s %d", "xyz", 33, 44, 55);
3857 ASSERT (result != NULL);
3858 ASSERT (wcscmp (result, L" xyz 33") == 0);
3859 ASSERT (length == wcslen (result));
3860 free (result);
3863 { /* Width given as argument. */
3864 size_t length;
3865 wchar_t *result =
3866 my_asnwprintf (NULL, &length, L"%*s %d", 10, "xyz", 33, 44, 55);
3867 ASSERT (result != NULL);
3868 ASSERT (wcscmp (result, L" xyz 33") == 0);
3869 ASSERT (length == wcslen (result));
3870 free (result);
3873 { /* Negative width given as argument (cf. FLAG_LEFT below). */
3874 size_t length;
3875 wchar_t *result =
3876 my_asnwprintf (NULL, &length, L"%*s %d", -10, "xyz", 33, 44, 55);
3877 ASSERT (result != NULL);
3878 ASSERT (wcscmp (result, L"xyz 33") == 0);
3879 ASSERT (length == wcslen (result));
3880 free (result);
3883 { /* FLAG_LEFT. */
3884 size_t length;
3885 wchar_t *result =
3886 my_asnwprintf (NULL, &length, L"%-10s %d", "xyz", 33, 44, 55);
3887 ASSERT (result != NULL);
3888 ASSERT (wcscmp (result, L"xyz 33") == 0);
3889 ASSERT (length == wcslen (result));
3890 free (result);
3893 /* On Android ≥ 5.0, the default locale is the "C.UTF-8" locale, not the
3894 "C" locale. Furthermore, when you attempt to set the "C" or "POSIX"
3895 locale via setlocale(), what you get is a "C" locale with UTF-8 encoding,
3896 that is, effectively the "C.UTF-8" locale. */
3897 #ifndef __ANDROID__
3898 { /* The conversion of %s to wide characters is done as if through repeated
3899 invocations of mbrtowc(), and in the "C" and "POSIX" locales, "an
3900 [EILSEQ] error cannot occur since all byte values are valid characters",
3901 says POSIX:2018. */
3902 int c;
3904 for (c = 0; c < 0x100; c++)
3906 char s[2] = { c, '\0' };
3907 size_t length;
3908 wchar_t *result = my_asnwprintf (NULL, &length, L"%s", s);
3909 ASSERT (result != NULL);
3910 free (result);
3913 #endif
3915 #if HAVE_WCHAR_T
3916 static wchar_t L_xyz[4] = { 'x', 'y', 'z', 0 };
3918 { /* Width. */
3919 size_t length;
3920 wchar_t *result =
3921 my_asnwprintf (NULL, &length, L"%10ls %d", L_xyz, 33, 44, 55);
3922 ASSERT (result != NULL);
3923 ASSERT (wcscmp (result, L" xyz 33") == 0);
3924 ASSERT (length == wcslen (result));
3925 free (result);
3928 { /* Width given as argument. */
3929 size_t length;
3930 wchar_t *result =
3931 my_asnwprintf (NULL, &length, L"%*ls %d", 10, L_xyz, 33, 44, 55);
3932 ASSERT (result != NULL);
3933 ASSERT (wcscmp (result, L" xyz 33") == 0);
3934 ASSERT (length == wcslen (result));
3935 free (result);
3938 { /* Negative width given as argument (cf. FLAG_LEFT below). */
3939 size_t length;
3940 wchar_t *result =
3941 my_asnwprintf (NULL, &length, L"%*ls %d", -10, L_xyz, 33, 44, 55);
3942 ASSERT (result != NULL);
3943 ASSERT (wcscmp (result, L"xyz 33") == 0);
3944 ASSERT (length == wcslen (result));
3945 free (result);
3948 { /* FLAG_LEFT. */
3949 size_t length;
3950 wchar_t *result =
3951 my_asnwprintf (NULL, &length, L"%-10ls %d", L_xyz, 33, 44, 55);
3952 ASSERT (result != NULL);
3953 ASSERT (wcscmp (result, L"xyz 33") == 0);
3954 ASSERT (length == wcslen (result));
3955 free (result);
3957 #endif
3959 /* To verify that these tests succeed, it is necessary to run them under
3960 a tool that checks against invalid memory accesses, such as ElectricFence
3961 or "valgrind --tool=memcheck". */
3963 size_t i;
3965 for (i = 1; i <= 8; i++)
3967 char *block;
3968 size_t length;
3969 wchar_t *result;
3970 wchar_t *wblock;
3971 size_t j;
3973 block = (char *) malloc (i);
3974 memcpy (block, "abcdefgh", i);
3975 result = my_asnwprintf (NULL, &length, L"%.*s", (int) i, block);
3976 ASSERT (result != NULL);
3977 wblock = (wchar_t *) malloc (i * sizeof (wchar_t));
3978 for (j = 0; j < i; j++)
3979 wblock[j] = (wchar_t) block[j];
3980 ASSERT (wmemcmp (result, wblock, i) == 0);
3981 ASSERT (result[i] == '\0');
3982 ASSERT (length == wcslen (result));
3983 free (result);
3984 free (block);
3987 #if HAVE_WCHAR_T
3989 size_t i;
3991 for (i = 1; i <= 8; i++)
3993 wchar_t *block;
3994 size_t j;
3995 size_t length;
3996 wchar_t *result;
3998 block = (wchar_t *) malloc (i * sizeof (wchar_t));
3999 for (j = 0; j < i; j++)
4000 block[j] = "abcdefgh"[j];
4001 result = my_asnwprintf (NULL, &length, L"%.*ls", (int) i, block);
4002 ASSERT (result != NULL);
4003 ASSERT (wmemcmp (result, L"abcdefgh", i) == 0);
4004 ASSERT (result[i] == '\0');
4005 ASSERT (length == wcslen (result));
4006 free (result);
4007 free (block);
4010 #endif
4012 #if HAVE_WCHAR_T
4013 /* Test that converting an invalid wchar_t[] to char[] fails with EILSEQ. */
4015 static const wchar_t input[] = { (wchar_t) 1702057263, 114, 0 };
4016 size_t length;
4017 wchar_t *result = my_asnwprintf (NULL, &length, L"%ls %d", input, 99);
4018 if (result == NULL)
4019 ASSERT (errno == EILSEQ);
4020 else
4021 free (result);
4024 static const wchar_t input[] = { (wchar_t) 1702057263, 114, 0 };
4025 size_t length;
4026 wchar_t *result = my_asnwprintf (NULL, &length, L"%3ls %d", input, 99);
4027 if (result == NULL)
4028 ASSERT (errno == EILSEQ);
4029 else
4030 free (result);
4033 static const wchar_t input[] = { (wchar_t) 1702057263, 114, 0 };
4034 size_t length;
4035 wchar_t *result = my_asnwprintf (NULL, &length, L"%.1ls %d", input, 99);
4036 if (result == NULL)
4037 ASSERT (errno == EILSEQ);
4038 else
4039 free (result);
4042 static const wchar_t input[] = { (wchar_t) 1702057263, 114, 0 };
4043 size_t length;
4044 wchar_t *result = my_asnwprintf (NULL, &length, L"%3.1ls %d", input, 99);
4045 if (result == NULL)
4046 ASSERT (errno == EILSEQ);
4047 else
4048 free (result);
4050 #endif
4052 /* Test the support of the %c format directive. */
4054 { /* Width. */
4055 size_t length;
4056 wchar_t *result =
4057 my_asnwprintf (NULL, &length,
4058 L"%10c %d", (unsigned char) 'x', 33, 44, 55);
4059 ASSERT (result != NULL);
4060 ASSERT (wcscmp (result, L" x 33") == 0);
4061 ASSERT (length == wcslen (result));
4062 free (result);
4065 { /* Width given as argument. */
4066 size_t length;
4067 wchar_t *result =
4068 my_asnwprintf (NULL, &length,
4069 L"%*c %d", 10, (unsigned char) 'x', 33, 44, 55);
4070 ASSERT (result != NULL);
4071 ASSERT (wcscmp (result, L" x 33") == 0);
4072 ASSERT (length == wcslen (result));
4073 free (result);
4076 { /* Negative width given as argument (cf. FLAG_LEFT below). */
4077 size_t length;
4078 wchar_t *result =
4079 my_asnwprintf (NULL, &length,
4080 L"%*c %d", -10, (unsigned char) 'x', 33, 44, 55);
4081 ASSERT (result != NULL);
4082 ASSERT (wcscmp (result, L"x 33") == 0);
4083 ASSERT (length == wcslen (result));
4084 free (result);
4087 { /* FLAG_LEFT. */
4088 size_t length;
4089 wchar_t *result =
4090 my_asnwprintf (NULL, &length,
4091 L"%-10c %d", (unsigned char) 'x', 33, 44, 55);
4092 ASSERT (result != NULL);
4093 ASSERT (wcscmp (result, L"x 33") == 0);
4094 ASSERT (length == wcslen (result));
4095 free (result);
4098 { /* Precision is ignored. */
4099 size_t length;
4100 wchar_t *result =
4101 my_asnwprintf (NULL, &length,
4102 L"%.0c %d", (unsigned char) 'x', 33, 44, 55);
4103 ASSERT (wcscmp (result, L"x 33") == 0);
4104 ASSERT (length == wcslen (result));
4105 free (result);
4108 { /* NUL character. */
4109 size_t length;
4110 wchar_t *result =
4111 my_asnwprintf (NULL, &length,
4112 L"a%cz %d", '\0', 33, 44, 55);
4113 ASSERT (wmemcmp (result, L"a\0z 33\0", 6 + 1) == 0);
4114 ASSERT (length == 6);
4115 free (result);
4118 /* On Android ≥ 5.0, the default locale is the "C.UTF-8" locale, not the
4119 "C" locale. Furthermore, when you attempt to set the "C" or "POSIX"
4120 locale via setlocale(), what you get is a "C" locale with UTF-8 encoding,
4121 that is, effectively the "C.UTF-8" locale. */
4122 #ifndef __ANDROID__
4123 { /* The conversion of %c to wide character is done as if through btowc(),
4124 and in the "C" and "POSIX" locales, "btowc() shall not return WEOF if
4125 c has a value in the range 0 to 255 inclusive", says POSIX:2018. */
4126 int c;
4128 for (c = 0; c < 0x100; c++)
4130 size_t length;
4131 wchar_t *result = my_asnwprintf (NULL, &length, L"%c", c);
4132 ASSERT (result != NULL);
4133 free (result);
4136 #endif
4138 #if HAVE_WCHAR_T
4139 static wint_t L_x = (wchar_t) 'x';
4141 { /* Width. */
4142 size_t length;
4143 wchar_t *result =
4144 my_asnwprintf (NULL, &length, L"%10lc %d", L_x, 33, 44, 55);
4145 ASSERT (result != NULL);
4146 ASSERT (wcscmp (result, L" x 33") == 0);
4147 ASSERT (length == wcslen (result));
4148 free (result);
4151 { /* Width given as argument. */
4152 size_t length;
4153 wchar_t *result =
4154 my_asnwprintf (NULL, &length, L"%*lc %d", 10, L_x, 33, 44, 55);
4155 ASSERT (result != NULL);
4156 ASSERT (wcscmp (result, L" x 33") == 0);
4157 ASSERT (length == wcslen (result));
4158 free (result);
4161 { /* Negative width given as argument (cf. FLAG_LEFT below). */
4162 size_t length;
4163 wchar_t *result =
4164 my_asnwprintf (NULL, &length, L"%*lc %d", -10, L_x, 33, 44, 55);
4165 ASSERT (result != NULL);
4166 ASSERT (wcscmp (result, L"x 33") == 0);
4167 ASSERT (length == wcslen (result));
4168 free (result);
4171 { /* FLAG_LEFT. */
4172 size_t length;
4173 wchar_t *result =
4174 my_asnwprintf (NULL, &length, L"%-10lc %d", L_x, 33, 44, 55);
4175 ASSERT (result != NULL);
4176 ASSERT (wcscmp (result, L"x 33") == 0);
4177 ASSERT (length == wcslen (result));
4178 free (result);
4181 { /* Precision is ignored. */
4182 size_t length;
4183 wchar_t *result =
4184 my_asnwprintf (NULL, &length, L"%.0lc %d", L_x, 33, 44, 55);
4185 ASSERT (wcscmp (result, L"x 33") == 0);
4186 ASSERT (length == wcslen (result));
4187 free (result);
4190 { /* NUL character. */
4191 size_t length;
4192 wchar_t *result =
4193 my_asnwprintf (NULL, &length, L"a%lcz %d", (wint_t) L'\0', 33, 44, 55);
4194 ASSERT (wmemcmp (result, L"a\0z 33\0", 6 + 1) == 0);
4195 ASSERT (length == 6);
4196 free (result);
4199 static wint_t L_invalid = (wchar_t) 0x76543210;
4201 { /* Invalid wide character. */
4202 size_t length;
4203 wchar_t *result =
4204 my_asnwprintf (NULL, &length, L"%lc %d", L_invalid, 33, 44, 55);
4205 /* No failure is allowed: ISO C says "the wint_t argument is converted
4206 to wchar_t and written." */
4207 ASSERT (result != NULL);
4208 ASSERT (result[0] == (wchar_t) 0x76543210);
4209 ASSERT (wmemcmp (result + 1, L" 33\0", 3 + 1) == 0);
4210 ASSERT (length == 4);
4211 free (result);
4214 { /* Invalid wide character and width. */
4215 size_t length;
4216 wchar_t *result =
4217 my_asnwprintf (NULL, &length, L"%10lc %d", L_invalid, 33, 44, 55);
4218 /* No failure is allowed: ISO C says "the wint_t argument is converted
4219 to wchar_t and written." */
4220 ASSERT (result != NULL);
4221 ASSERT (wmemcmp (result, L" ", 9) == 0);
4222 ASSERT (result[9] == (wchar_t) 0x76543210);
4223 ASSERT (wmemcmp (result + 10, L" 33\0", 3 + 1) == 0);
4224 ASSERT (length == 13);
4225 free (result);
4227 #endif
4229 /* Test the support of the 'x' conversion specifier for hexadecimal output of
4230 integers. */
4232 { /* Zero. */
4233 size_t length;
4234 wchar_t *result =
4235 my_asnwprintf (NULL, &length, L"%x %d", 0, 33, 44, 55);
4236 ASSERT (result != NULL);
4237 ASSERT (wcscmp (result, L"0 33") == 0);
4238 ASSERT (length == wcslen (result));
4239 free (result);
4242 { /* A positive number. */
4243 size_t length;
4244 wchar_t *result =
4245 my_asnwprintf (NULL, &length, L"%x %d", 12348, 33, 44, 55);
4246 ASSERT (result != NULL);
4247 ASSERT (wcscmp (result, L"303c 33") == 0);
4248 ASSERT (length == wcslen (result));
4249 free (result);
4252 { /* A large positive number. */
4253 size_t length;
4254 wchar_t *result =
4255 my_asnwprintf (NULL, &length, L"%x %d", 0xFFFFFFFEU, 33, 44, 55);
4256 ASSERT (result != NULL);
4257 ASSERT (wcscmp (result, L"fffffffe 33") == 0);
4258 ASSERT (length == wcslen (result));
4259 free (result);
4262 { /* Width. */
4263 size_t length;
4264 wchar_t *result =
4265 my_asnwprintf (NULL, &length, L"%10x %d", 12348, 33, 44, 55);
4266 ASSERT (result != NULL);
4267 ASSERT (wcscmp (result, L" 303c 33") == 0);
4268 ASSERT (length == wcslen (result));
4269 free (result);
4272 { /* Width given as argument. */
4273 size_t length;
4274 wchar_t *result =
4275 my_asnwprintf (NULL, &length, L"%*x %d", 10, 12348, 33, 44, 55);
4276 ASSERT (result != NULL);
4277 ASSERT (wcscmp (result, L" 303c 33") == 0);
4278 ASSERT (length == wcslen (result));
4279 free (result);
4282 { /* Negative width given as argument (cf. FLAG_LEFT below). */
4283 size_t length;
4284 wchar_t *result =
4285 my_asnwprintf (NULL, &length, L"%*x %d", -10, 12348, 33, 44, 55);
4286 ASSERT (result != NULL);
4287 ASSERT (wcscmp (result, L"303c 33") == 0);
4288 ASSERT (length == wcslen (result));
4289 free (result);
4292 { /* Precision. */
4293 size_t length;
4294 wchar_t *result =
4295 my_asnwprintf (NULL, &length, L"%.10x %d", 12348, 33, 44, 55);
4296 ASSERT (result != NULL);
4297 ASSERT (wcscmp (result, L"000000303c 33") == 0);
4298 ASSERT (length == wcslen (result));
4299 free (result);
4302 { /* Zero precision and a positive number. */
4303 size_t length;
4304 wchar_t *result =
4305 my_asnwprintf (NULL, &length, L"%.0x %d", 12348, 33, 44, 55);
4306 ASSERT (result != NULL);
4307 ASSERT (wcscmp (result, L"303c 33") == 0);
4308 ASSERT (length == wcslen (result));
4309 free (result);
4312 { /* Zero precision and a zero number. */
4313 size_t length;
4314 wchar_t *result =
4315 my_asnwprintf (NULL, &length, L"%.0x %d", 0, 33, 44, 55);
4316 ASSERT (result != NULL);
4317 /* ISO C and POSIX specify that "The result of converting a zero value
4318 with a precision of zero is no characters." */
4319 ASSERT (wcscmp (result, L" 33") == 0);
4320 ASSERT (length == wcslen (result));
4321 free (result);
4324 { /* Width and precision. */
4325 size_t length;
4326 wchar_t *result =
4327 my_asnwprintf (NULL, &length, L"%15.10x %d", 12348, 33, 44, 55);
4328 ASSERT (result != NULL);
4329 ASSERT (wcscmp (result, L" 000000303c 33") == 0);
4330 ASSERT (length == wcslen (result));
4331 free (result);
4334 { /* Padding and precision. */
4335 size_t length;
4336 wchar_t *result =
4337 my_asnwprintf (NULL, &length, L"%015.10x %d", 12348, 33, 44, 55);
4338 ASSERT (result != NULL);
4339 /* ISO C 99 § 7.24.2.1.(6) says: "For d, i, o, u, x, and X conversions, if a
4340 precision is specified, the 0 flag is ignored." */
4341 ASSERT (wcscmp (result, L" 000000303c 33") == 0);
4342 ASSERT (length == wcslen (result));
4343 free (result);
4346 { /* FLAG_LEFT. */
4347 size_t length;
4348 wchar_t *result =
4349 my_asnwprintf (NULL, &length, L"%-10x %d", 12348, 33, 44, 55);
4350 ASSERT (result != NULL);
4351 ASSERT (wcscmp (result, L"303c 33") == 0);
4352 ASSERT (length == wcslen (result));
4353 free (result);
4356 { /* FLAG_ALT with zero. */
4357 size_t length;
4358 wchar_t *result =
4359 my_asnwprintf (NULL, &length, L"%#x %d", 0, 33, 44, 55);
4360 ASSERT (result != NULL);
4361 ASSERT (wcscmp (result, L"0 33") == 0);
4362 ASSERT (length == wcslen (result));
4363 free (result);
4366 { /* FLAG_ALT with a positive number. */
4367 size_t length;
4368 wchar_t *result =
4369 my_asnwprintf (NULL, &length, L"%#x %d", 12348, 33, 44, 55);
4370 ASSERT (result != NULL);
4371 ASSERT (wcscmp (result, L"0x303c 33") == 0);
4372 ASSERT (length == wcslen (result));
4373 free (result);
4376 { /* FLAG_ALT with a positive number and width. */
4377 size_t length;
4378 wchar_t *result =
4379 my_asnwprintf (NULL, &length, L"%#10x %d", 12348, 33, 44, 55);
4380 ASSERT (result != NULL);
4381 ASSERT (wcscmp (result, L" 0x303c 33") == 0);
4382 ASSERT (length == wcslen (result));
4383 free (result);
4386 { /* FLAG_ALT with a positive number and padding. */
4387 size_t length;
4388 wchar_t *result =
4389 my_asnwprintf (NULL, &length, L"%0#10x %d", 12348, 33, 44, 55);
4390 ASSERT (result != NULL);
4391 ASSERT (wcscmp (result, L"0x0000303c 33") == 0);
4392 ASSERT (length == wcslen (result));
4393 free (result);
4396 { /* FLAG_ALT with a positive number and precision. */
4397 size_t length;
4398 wchar_t *result =
4399 my_asnwprintf (NULL, &length, L"%0#.10x %d", 12348, 33, 44, 55);
4400 ASSERT (result != NULL);
4401 ASSERT (wcscmp (result, L"0x000000303c 33") == 0);
4402 ASSERT (length == wcslen (result));
4403 free (result);
4406 { /* FLAG_ALT with a positive number and width and precision. */
4407 size_t length;
4408 wchar_t *result =
4409 my_asnwprintf (NULL, &length, L"%#15.10x %d", 12348, 33, 44, 55);
4410 ASSERT (result != NULL);
4411 ASSERT (wcscmp (result, L" 0x000000303c 33") == 0);
4412 ASSERT (length == wcslen (result));
4413 free (result);
4416 { /* FLAG_ALT with a positive number and padding and precision. */
4417 size_t length;
4418 wchar_t *result =
4419 my_asnwprintf (NULL, &length, L"%0#15.10x %d", 12348, 33, 44, 55);
4420 ASSERT (result != NULL);
4421 /* ISO C 99 § 7.24.2.1.(6) says: "For d, i, o, u, x, and X conversions, if a
4422 precision is specified, the 0 flag is ignored." */
4423 ASSERT (wcscmp (result, L" 0x000000303c 33") == 0);
4424 ASSERT (length == wcslen (result));
4425 free (result);
4428 { /* FLAG_ALT with a zero precision and a zero number. */
4429 size_t length;
4430 wchar_t *result =
4431 my_asnwprintf (NULL, &length, L"%#.0x %d", 0, 33, 44, 55);
4432 ASSERT (result != NULL);
4433 /* ISO C and POSIX specify that "The result of converting a zero value
4434 with a precision of zero is no characters.", and the prefix is added
4435 only for non-zero values. */
4436 ASSERT (wcscmp (result, L" 33") == 0);
4437 ASSERT (length == wcslen (result));
4438 free (result);
4441 { /* Uppercase 'X'. */
4442 size_t length;
4443 wchar_t *result =
4444 my_asnwprintf (NULL, &length, L"%X %d", 12348, 33, 44, 55);
4445 ASSERT (result != NULL);
4446 ASSERT (wcscmp (result, L"303C 33") == 0);
4447 ASSERT (length == wcslen (result));
4448 free (result);
4451 { /* Uppercase 'X' with FLAG_ALT. */
4452 size_t length;
4453 wchar_t *result =
4454 my_asnwprintf (NULL, &length, L"%#X %d", 12348, 33, 44, 55);
4455 ASSERT (result != NULL);
4456 ASSERT (wcscmp (result, L"0X303C 33") == 0);
4457 ASSERT (length == wcslen (result));
4458 free (result);
4461 { /* Uppercase 'X' with FLAG_ALT and zero precision and a zero number. */
4462 size_t length;
4463 wchar_t *result =
4464 my_asnwprintf (NULL, &length, L"%#.0X %d", 0, 33, 44, 55);
4465 ASSERT (result != NULL);
4466 /* ISO C and POSIX specify that "The result of converting a zero value
4467 with a precision of zero is no characters.", and the prefix is added
4468 only for non-zero values. */
4469 ASSERT (wcscmp (result, L" 33") == 0);
4470 ASSERT (length == wcslen (result));
4471 free (result);
4474 /* Test the support of the 'b' conversion specifier for binary output of
4475 integers. */
4477 { /* Zero. */
4478 size_t length;
4479 wchar_t *result =
4480 my_asnwprintf (NULL, &length, L"%b %d", 0, 33, 44, 55);
4481 ASSERT (result != NULL);
4482 ASSERT (wcscmp (result, L"0 33") == 0);
4483 ASSERT (length == wcslen (result));
4484 free (result);
4487 { /* A positive number. */
4488 size_t length;
4489 wchar_t *result =
4490 my_asnwprintf (NULL, &length, L"%b %d", 12345, 33, 44, 55);
4491 ASSERT (result != NULL);
4492 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
4493 ASSERT (length == wcslen (result));
4494 free (result);
4497 { /* A large positive number. */
4498 size_t length;
4499 wchar_t *result =
4500 my_asnwprintf (NULL, &length, L"%b %d", 0xFFFFFFFEU, 33, 44, 55);
4501 ASSERT (result != NULL);
4502 ASSERT (wcscmp (result, L"11111111111111111111111111111110 33") == 0);
4503 ASSERT (length == wcslen (result));
4504 free (result);
4507 { /* Width. */
4508 size_t length;
4509 wchar_t *result =
4510 my_asnwprintf (NULL, &length, L"%20b %d", 12345, 33, 44, 55);
4511 ASSERT (result != NULL);
4512 ASSERT (wcscmp (result, L" 11000000111001 33") == 0);
4513 ASSERT (length == wcslen (result));
4514 free (result);
4517 { /* Width given as argument. */
4518 size_t length;
4519 wchar_t *result =
4520 my_asnwprintf (NULL, &length, L"%*b %d", 20, 12345, 33, 44, 55);
4521 ASSERT (result != NULL);
4522 ASSERT (wcscmp (result, L" 11000000111001 33") == 0);
4523 ASSERT (length == wcslen (result));
4524 free (result);
4527 { /* Negative width given as argument (cf. FLAG_LEFT below). */
4528 size_t length;
4529 wchar_t *result =
4530 my_asnwprintf (NULL, &length, L"%*b %d", -20, 12345, 33, 44, 55);
4531 ASSERT (result != NULL);
4532 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
4533 ASSERT (length == wcslen (result));
4534 free (result);
4537 { /* Precision. */
4538 size_t length;
4539 wchar_t *result =
4540 my_asnwprintf (NULL, &length, L"%.20b %d", 12345, 33, 44, 55);
4541 ASSERT (result != NULL);
4542 ASSERT (wcscmp (result, L"00000011000000111001 33") == 0);
4543 ASSERT (length == wcslen (result));
4544 free (result);
4547 { /* Zero precision and a positive number. */
4548 size_t length;
4549 wchar_t *result =
4550 my_asnwprintf (NULL, &length, L"%.0b %d", 12345, 33, 44, 55);
4551 ASSERT (result != NULL);
4552 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
4553 ASSERT (length == wcslen (result));
4554 free (result);
4557 { /* Zero precision and a zero number. */
4558 size_t length;
4559 wchar_t *result =
4560 my_asnwprintf (NULL, &length, L"%.0b %d", 0, 33, 44, 55);
4561 ASSERT (result != NULL);
4562 /* ISO C and POSIX specify that "The result of converting a zero value
4563 with a precision of zero is no characters." */
4564 ASSERT (wcscmp (result, L" 33") == 0);
4565 ASSERT (length == wcslen (result));
4566 free (result);
4569 { /* Width and precision. */
4570 size_t length;
4571 wchar_t *result =
4572 my_asnwprintf (NULL, &length, L"%25.20b %d", 12345, 33, 44, 55);
4573 ASSERT (result != NULL);
4574 ASSERT (wcscmp (result, L" 00000011000000111001 33") == 0);
4575 ASSERT (length == wcslen (result));
4576 free (result);
4579 { /* Padding and precision. */
4580 size_t length;
4581 wchar_t *result =
4582 my_asnwprintf (NULL, &length, L"%025.20b %d", 12345, 33, 44, 55);
4583 ASSERT (result != NULL);
4584 /* Neither ISO C nor POSIX specify that the '0' flag is ignored when
4585 a width and a precision are both present. But implementations do so. */
4586 ASSERT (wcscmp (result, L" 00000011000000111001 33") == 0);
4587 ASSERT (length == wcslen (result));
4588 free (result);
4591 { /* FLAG_LEFT. */
4592 size_t length;
4593 wchar_t *result =
4594 my_asnwprintf (NULL, &length, L"%-20b %d", 12345, 33, 44, 55);
4595 ASSERT (result != NULL);
4596 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
4597 ASSERT (length == wcslen (result));
4598 free (result);
4601 { /* FLAG_ALT with zero. */
4602 size_t length;
4603 wchar_t *result =
4604 my_asnwprintf (NULL, &length, L"%#b %d", 0, 33, 44, 55);
4605 ASSERT (result != NULL);
4606 ASSERT (wcscmp (result, L"0 33") == 0);
4607 ASSERT (length == wcslen (result));
4608 free (result);
4611 { /* FLAG_ALT with a positive number. */
4612 size_t length;
4613 wchar_t *result =
4614 my_asnwprintf (NULL, &length, L"%#b %d", 12345, 33, 44, 55);
4615 ASSERT (result != NULL);
4616 ASSERT (wcscmp (result, L"0b11000000111001 33") == 0);
4617 ASSERT (length == wcslen (result));
4618 free (result);
4621 { /* FLAG_ALT with a positive number and width. */
4622 size_t length;
4623 wchar_t *result =
4624 my_asnwprintf (NULL, &length, L"%#20b %d", 12345, 33, 44, 55);
4625 ASSERT (result != NULL);
4626 ASSERT (wcscmp (result, L" 0b11000000111001 33") == 0);
4627 ASSERT (length == wcslen (result));
4628 free (result);
4631 { /* FLAG_ALT with a positive number and padding. */
4632 size_t length;
4633 wchar_t *result =
4634 my_asnwprintf (NULL, &length, L"%0#20b %d", 12345, 33, 44, 55);
4635 ASSERT (result != NULL);
4636 ASSERT (wcscmp (result, L"0b000011000000111001 33") == 0);
4637 ASSERT (length == wcslen (result));
4638 free (result);
4641 { /* FLAG_ALT with a positive number and precision. */
4642 size_t length;
4643 wchar_t *result =
4644 my_asnwprintf (NULL, &length, L"%0#.20b %d", 12345, 33, 44, 55);
4645 ASSERT (result != NULL);
4646 ASSERT (wcscmp (result, L"0b00000011000000111001 33") == 0);
4647 ASSERT (length == wcslen (result));
4648 free (result);
4651 { /* FLAG_ALT with a positive number and width and precision. */
4652 size_t length;
4653 wchar_t *result =
4654 my_asnwprintf (NULL, &length, L"%#25.20b %d", 12345, 33, 44, 55);
4655 ASSERT (result != NULL);
4656 ASSERT (wcscmp (result, L" 0b00000011000000111001 33") == 0);
4657 ASSERT (length == wcslen (result));
4658 free (result);
4661 { /* FLAG_ALT with a positive number and padding and precision. */
4662 size_t length;
4663 wchar_t *result =
4664 my_asnwprintf (NULL, &length, L"%0#25.20b %d", 12345, 33, 44, 55);
4665 ASSERT (result != NULL);
4666 /* Neither ISO C nor POSIX specify that the '0' flag is ignored when
4667 a width and a precision are both present. But implementations do so. */
4668 ASSERT (wcscmp (result, L" 0b00000011000000111001 33") == 0);
4669 ASSERT (length == wcslen (result));
4670 free (result);
4673 { /* FLAG_ALT with a zero precision and a zero number. */
4674 size_t length;
4675 wchar_t *result =
4676 my_asnwprintf (NULL, &length, L"%#.0b %d", 0, 33, 44, 55);
4677 ASSERT (result != NULL);
4678 /* ISO C and POSIX specify that "The result of converting a zero value
4679 with a precision of zero is no characters.", and the prefix is added
4680 only for non-zero values. */
4681 ASSERT (wcscmp (result, L" 33") == 0);
4682 ASSERT (length == wcslen (result));
4683 free (result);
4686 /* Test the support of argument type/size specifiers for signed integer
4687 conversions. */
4690 size_t length;
4691 wchar_t *result =
4692 my_asnwprintf (NULL, &length, L"%hhd %d", (signed char) -42, 33, 44, 55);
4693 ASSERT (wcscmp (result, L"-42 33") == 0);
4694 ASSERT (length == wcslen (result));
4695 free (result);
4699 size_t length;
4700 wchar_t *result =
4701 my_asnwprintf (NULL, &length, L"%hd %d", (short) -12345, 33, 44, 55);
4702 ASSERT (wcscmp (result, L"-12345 33") == 0);
4703 ASSERT (length == wcslen (result));
4704 free (result);
4708 size_t length;
4709 wchar_t *result =
4710 my_asnwprintf (NULL, &length, L"%d %d", -12345, 33, 44, 55);
4711 ASSERT (wcscmp (result, L"-12345 33") == 0);
4712 ASSERT (length == wcslen (result));
4713 free (result);
4717 size_t length;
4718 wchar_t *result =
4719 my_asnwprintf (NULL, &length, L"%ld %d", (long int) -12345, 33, 44, 55);
4720 ASSERT (wcscmp (result, L"-12345 33") == 0);
4721 ASSERT (length == wcslen (result));
4722 free (result);
4726 size_t length;
4727 wchar_t *result =
4728 my_asnwprintf (NULL, &length, L"%lld %d", (long long int) -12345, 33, 44, 55);
4729 ASSERT (wcscmp (result, L"-12345 33") == 0);
4730 ASSERT (length == wcslen (result));
4731 free (result);
4735 size_t length;
4736 wchar_t *result =
4737 my_asnwprintf (NULL, &length, L"%w8d %d", (int8_t) -42, 33, 44, 55);
4738 ASSERT (wcscmp (result, L"-42 33") == 0);
4739 ASSERT (length == wcslen (result));
4740 free (result);
4744 size_t length;
4745 wchar_t *result =
4746 my_asnwprintf (NULL, &length, L"%w16d %d", (int16_t) -12345, 33, 44, 55);
4747 ASSERT (wcscmp (result, L"-12345 33") == 0);
4748 ASSERT (length == wcslen (result));
4749 free (result);
4753 size_t length;
4754 wchar_t *result =
4755 my_asnwprintf (NULL, &length, L"%w32d %d", (int32_t) -12345, 33, 44, 55);
4756 ASSERT (wcscmp (result, L"-12345 33") == 0);
4757 ASSERT (length == wcslen (result));
4758 free (result);
4762 size_t length;
4763 wchar_t *result =
4764 my_asnwprintf (NULL, &length, L"%w64d %d", (int64_t) -12345, 33, 44, 55);
4765 ASSERT (wcscmp (result, L"-12345 33") == 0);
4766 ASSERT (length == wcslen (result));
4767 free (result);
4771 size_t length;
4772 wchar_t *result =
4773 my_asnwprintf (NULL, &length, L"%wf8d %d", (int_fast8_t) -42, 33, 44, 55);
4774 ASSERT (wcscmp (result, L"-42 33") == 0);
4775 ASSERT (length == wcslen (result));
4776 free (result);
4780 size_t length;
4781 wchar_t *result =
4782 my_asnwprintf (NULL, &length, L"%wf16d %d", (int_fast16_t) -12345, 33, 44, 55);
4783 ASSERT (wcscmp (result, L"-12345 33") == 0);
4784 ASSERT (length == wcslen (result));
4785 free (result);
4789 size_t length;
4790 wchar_t *result =
4791 my_asnwprintf (NULL, &length, L"%wf32d %d", (int_fast32_t) -12345, 33, 44, 55);
4792 ASSERT (wcscmp (result, L"-12345 33") == 0);
4793 ASSERT (length == wcslen (result));
4794 free (result);
4798 size_t length;
4799 wchar_t *result =
4800 my_asnwprintf (NULL, &length, L"%wf64d %d", (int_fast64_t) -12345, 33, 44, 55);
4801 ASSERT (wcscmp (result, L"-12345 33") == 0);
4802 ASSERT (length == wcslen (result));
4803 free (result);
4806 /* Test the support of argument type/size specifiers for unsigned integer
4807 conversions: %u */
4810 size_t length;
4811 wchar_t *result =
4812 my_asnwprintf (NULL, &length, L"%hhu %d", (unsigned char) 42, 33, 44, 55);
4813 ASSERT (wcscmp (result, L"42 33") == 0);
4814 ASSERT (length == wcslen (result));
4815 free (result);
4819 size_t length;
4820 wchar_t *result =
4821 my_asnwprintf (NULL, &length, L"%hu %d", (unsigned short) 12345, 33, 44, 55);
4822 ASSERT (wcscmp (result, L"12345 33") == 0);
4823 ASSERT (length == wcslen (result));
4824 free (result);
4828 size_t length;
4829 wchar_t *result =
4830 my_asnwprintf (NULL, &length, L"%u %d", (unsigned int) 12345, 33, 44, 55);
4831 ASSERT (wcscmp (result, L"12345 33") == 0);
4832 ASSERT (length == wcslen (result));
4833 free (result);
4837 size_t length;
4838 wchar_t *result =
4839 my_asnwprintf (NULL, &length, L"%lu %d", (unsigned long int) 12345, 33, 44, 55);
4840 ASSERT (wcscmp (result, L"12345 33") == 0);
4841 ASSERT (length == wcslen (result));
4842 free (result);
4846 size_t length;
4847 wchar_t *result =
4848 my_asnwprintf (NULL, &length, L"%llu %d", (unsigned long long int) 12345, 33, 44, 55);
4849 ASSERT (wcscmp (result, L"12345 33") == 0);
4850 ASSERT (length == wcslen (result));
4851 free (result);
4855 size_t length;
4856 wchar_t *result =
4857 my_asnwprintf (NULL, &length, L"%w8u %d", (uint8_t) 42, 33, 44, 55);
4858 ASSERT (wcscmp (result, L"42 33") == 0);
4859 ASSERT (length == wcslen (result));
4860 free (result);
4864 size_t length;
4865 wchar_t *result =
4866 my_asnwprintf (NULL, &length, L"%w16u %d", (uint16_t) 12345, 33, 44, 55);
4867 ASSERT (wcscmp (result, L"12345 33") == 0);
4868 ASSERT (length == wcslen (result));
4869 free (result);
4873 size_t length;
4874 wchar_t *result =
4875 my_asnwprintf (NULL, &length, L"%w32u %d", (uint32_t) 12345, 33, 44, 55);
4876 ASSERT (wcscmp (result, L"12345 33") == 0);
4877 ASSERT (length == wcslen (result));
4878 free (result);
4882 size_t length;
4883 wchar_t *result =
4884 my_asnwprintf (NULL, &length, L"%w64u %d", (uint64_t) 12345, 33, 44, 55);
4885 ASSERT (wcscmp (result, L"12345 33") == 0);
4886 ASSERT (length == wcslen (result));
4887 free (result);
4891 size_t length;
4892 wchar_t *result =
4893 my_asnwprintf (NULL, &length, L"%wf8u %d", (uint_fast8_t) 42, 33, 44, 55);
4894 ASSERT (wcscmp (result, L"42 33") == 0);
4895 ASSERT (length == wcslen (result));
4896 free (result);
4900 size_t length;
4901 wchar_t *result =
4902 my_asnwprintf (NULL, &length, L"%wf16u %d", (uint_fast16_t) 12345, 33, 44, 55);
4903 ASSERT (wcscmp (result, L"12345 33") == 0);
4904 ASSERT (length == wcslen (result));
4905 free (result);
4909 size_t length;
4910 wchar_t *result =
4911 my_asnwprintf (NULL, &length, L"%wf32u %d", (uint_fast32_t) 12345, 33, 44, 55);
4912 ASSERT (wcscmp (result, L"12345 33") == 0);
4913 ASSERT (length == wcslen (result));
4914 free (result);
4918 size_t length;
4919 wchar_t *result =
4920 my_asnwprintf (NULL, &length, L"%wf64u %d", (uint_fast64_t) 12345, 33, 44, 55);
4921 ASSERT (wcscmp (result, L"12345 33") == 0);
4922 ASSERT (length == wcslen (result));
4923 free (result);
4926 /* Test the support of argument type/size specifiers for unsigned integer
4927 conversions: %b */
4930 size_t length;
4931 wchar_t *result =
4932 my_asnwprintf (NULL, &length, L"%hhb %d", (unsigned char) 42, 33, 44, 55);
4933 ASSERT (wcscmp (result, L"101010 33") == 0);
4934 ASSERT (length == wcslen (result));
4935 free (result);
4939 size_t length;
4940 wchar_t *result =
4941 my_asnwprintf (NULL, &length, L"%hb %d", (unsigned short) 12345, 33, 44, 55);
4942 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
4943 ASSERT (length == wcslen (result));
4944 free (result);
4948 size_t length;
4949 wchar_t *result =
4950 my_asnwprintf (NULL, &length, L"%b %d", (unsigned int) 12345, 33, 44, 55);
4951 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
4952 ASSERT (length == wcslen (result));
4953 free (result);
4957 size_t length;
4958 wchar_t *result =
4959 my_asnwprintf (NULL, &length, L"%lb %d", (unsigned long int) 12345, 33, 44, 55);
4960 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
4961 ASSERT (length == wcslen (result));
4962 free (result);
4966 size_t length;
4967 wchar_t *result =
4968 my_asnwprintf (NULL, &length, L"%llb %d", (unsigned long long int) 12345, 33, 44, 55);
4969 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
4970 ASSERT (length == wcslen (result));
4971 free (result);
4975 size_t length;
4976 wchar_t *result =
4977 my_asnwprintf (NULL, &length, L"%w8b %d", (uint8_t) 42, 33, 44, 55);
4978 ASSERT (wcscmp (result, L"101010 33") == 0);
4979 ASSERT (length == wcslen (result));
4980 free (result);
4984 size_t length;
4985 wchar_t *result =
4986 my_asnwprintf (NULL, &length, L"%w16b %d", (uint16_t) 12345, 33, 44, 55);
4987 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
4988 ASSERT (length == wcslen (result));
4989 free (result);
4993 size_t length;
4994 wchar_t *result =
4995 my_asnwprintf (NULL, &length, L"%w32b %d", (uint32_t) 12345, 33, 44, 55);
4996 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
4997 ASSERT (length == wcslen (result));
4998 free (result);
5002 size_t length;
5003 wchar_t *result =
5004 my_asnwprintf (NULL, &length, L"%w64b %d", (uint64_t) 12345, 33, 44, 55);
5005 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
5006 ASSERT (length == wcslen (result));
5007 free (result);
5011 size_t length;
5012 wchar_t *result =
5013 my_asnwprintf (NULL, &length, L"%wf8b %d", (uint_fast8_t) 42, 33, 44, 55);
5014 ASSERT (wcscmp (result, L"101010 33") == 0);
5015 ASSERT (length == wcslen (result));
5016 free (result);
5020 size_t length;
5021 wchar_t *result =
5022 my_asnwprintf (NULL, &length, L"%wf16b %d", (uint_fast16_t) 12345, 33, 44, 55);
5023 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
5024 ASSERT (length == wcslen (result));
5025 free (result);
5029 size_t length;
5030 wchar_t *result =
5031 my_asnwprintf (NULL, &length, L"%wf32b %d", (uint_fast32_t) 12345, 33, 44, 55);
5032 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
5033 ASSERT (length == wcslen (result));
5034 free (result);
5038 size_t length;
5039 wchar_t *result =
5040 my_asnwprintf (NULL, &length, L"%wf64b %d", (uint_fast64_t) 12345, 33, 44, 55);
5041 ASSERT (wcscmp (result, L"11000000111001 33") == 0);
5042 ASSERT (length == wcslen (result));
5043 free (result);
5046 /* Test the support of argument type/size specifiers for unsigned integer
5047 conversions: %o */
5050 size_t length;
5051 wchar_t *result =
5052 my_asnwprintf (NULL, &length, L"%hho %d", (unsigned char) 42, 33, 44, 55);
5053 ASSERT (wcscmp (result, L"52 33") == 0);
5054 ASSERT (length == wcslen (result));
5055 free (result);
5059 size_t length;
5060 wchar_t *result =
5061 my_asnwprintf (NULL, &length, L"%ho %d", (unsigned short) 12345, 33, 44, 55);
5062 ASSERT (wcscmp (result, L"30071 33") == 0);
5063 ASSERT (length == wcslen (result));
5064 free (result);
5068 size_t length;
5069 wchar_t *result =
5070 my_asnwprintf (NULL, &length, L"%o %d", (unsigned int) 12345, 33, 44, 55);
5071 ASSERT (wcscmp (result, L"30071 33") == 0);
5072 ASSERT (length == wcslen (result));
5073 free (result);
5077 size_t length;
5078 wchar_t *result =
5079 my_asnwprintf (NULL, &length, L"%lo %d", (unsigned long int) 12345, 33, 44, 55);
5080 ASSERT (wcscmp (result, L"30071 33") == 0);
5081 ASSERT (length == wcslen (result));
5082 free (result);
5086 size_t length;
5087 wchar_t *result =
5088 my_asnwprintf (NULL, &length, L"%llo %d", (unsigned long long int) 12345, 33, 44, 55);
5089 ASSERT (wcscmp (result, L"30071 33") == 0);
5090 ASSERT (length == wcslen (result));
5091 free (result);
5095 size_t length;
5096 wchar_t *result =
5097 my_asnwprintf (NULL, &length, L"%w8o %d", (uint8_t) 42, 33, 44, 55);
5098 ASSERT (wcscmp (result, L"52 33") == 0);
5099 ASSERT (length == wcslen (result));
5100 free (result);
5104 size_t length;
5105 wchar_t *result =
5106 my_asnwprintf (NULL, &length, L"%w16o %d", (uint16_t) 12345, 33, 44, 55);
5107 ASSERT (wcscmp (result, L"30071 33") == 0);
5108 ASSERT (length == wcslen (result));
5109 free (result);
5113 size_t length;
5114 wchar_t *result =
5115 my_asnwprintf (NULL, &length, L"%w32o %d", (uint32_t) 12345, 33, 44, 55);
5116 ASSERT (wcscmp (result, L"30071 33") == 0);
5117 ASSERT (length == wcslen (result));
5118 free (result);
5122 size_t length;
5123 wchar_t *result =
5124 my_asnwprintf (NULL, &length, L"%w64o %d", (uint64_t) 12345, 33, 44, 55);
5125 ASSERT (wcscmp (result, L"30071 33") == 0);
5126 ASSERT (length == wcslen (result));
5127 free (result);
5131 size_t length;
5132 wchar_t *result =
5133 my_asnwprintf (NULL, &length, L"%wf8o %d", (uint_fast8_t) 42, 33, 44, 55);
5134 ASSERT (wcscmp (result, L"52 33") == 0);
5135 ASSERT (length == wcslen (result));
5136 free (result);
5140 size_t length;
5141 wchar_t *result =
5142 my_asnwprintf (NULL, &length, L"%wf16o %d", (uint_fast16_t) 12345, 33, 44, 55);
5143 ASSERT (wcscmp (result, L"30071 33") == 0);
5144 ASSERT (length == wcslen (result));
5145 free (result);
5149 size_t length;
5150 wchar_t *result =
5151 my_asnwprintf (NULL, &length, L"%wf32o %d", (uint_fast32_t) 12345, 33, 44, 55);
5152 ASSERT (wcscmp (result, L"30071 33") == 0);
5153 ASSERT (length == wcslen (result));
5154 free (result);
5158 size_t length;
5159 wchar_t *result =
5160 my_asnwprintf (NULL, &length, L"%wf64o %d", (uint_fast64_t) 12345, 33, 44, 55);
5161 ASSERT (wcscmp (result, L"30071 33") == 0);
5162 ASSERT (length == wcslen (result));
5163 free (result);
5166 /* Test the support of argument type/size specifiers for unsigned integer
5167 conversions: %x */
5170 size_t length;
5171 wchar_t *result =
5172 my_asnwprintf (NULL, &length, L"%hhX %d", (unsigned char) 42, 33, 44, 55);
5173 ASSERT (wcscmp (result, L"2A 33") == 0);
5174 ASSERT (length == wcslen (result));
5175 free (result);
5179 size_t length;
5180 wchar_t *result =
5181 my_asnwprintf (NULL, &length, L"%hX %d", (unsigned short) 12345, 33, 44, 55);
5182 ASSERT (wcscmp (result, L"3039 33") == 0);
5183 ASSERT (length == wcslen (result));
5184 free (result);
5188 size_t length;
5189 wchar_t *result =
5190 my_asnwprintf (NULL, &length, L"%X %d", (unsigned int) 12345, 33, 44, 55);
5191 ASSERT (wcscmp (result, L"3039 33") == 0);
5192 ASSERT (length == wcslen (result));
5193 free (result);
5197 size_t length;
5198 wchar_t *result =
5199 my_asnwprintf (NULL, &length, L"%lX %d", (unsigned long int) 12345, 33, 44, 55);
5200 ASSERT (wcscmp (result, L"3039 33") == 0);
5201 ASSERT (length == wcslen (result));
5202 free (result);
5206 size_t length;
5207 wchar_t *result =
5208 my_asnwprintf (NULL, &length, L"%llX %d", (unsigned long long int) 12345, 33, 44, 55);
5209 ASSERT (wcscmp (result, L"3039 33") == 0);
5210 ASSERT (length == wcslen (result));
5211 free (result);
5215 size_t length;
5216 wchar_t *result =
5217 my_asnwprintf (NULL, &length, L"%w8X %d", (uint8_t) 42, 33, 44, 55);
5218 ASSERT (wcscmp (result, L"2A 33") == 0);
5219 ASSERT (length == wcslen (result));
5220 free (result);
5224 size_t length;
5225 wchar_t *result =
5226 my_asnwprintf (NULL, &length, L"%w16X %d", (uint16_t) 12345, 33, 44, 55);
5227 ASSERT (wcscmp (result, L"3039 33") == 0);
5228 ASSERT (length == wcslen (result));
5229 free (result);
5233 size_t length;
5234 wchar_t *result =
5235 my_asnwprintf (NULL, &length, L"%w32X %d", (uint32_t) 12345, 33, 44, 55);
5236 ASSERT (wcscmp (result, L"3039 33") == 0);
5237 ASSERT (length == wcslen (result));
5238 free (result);
5242 size_t length;
5243 wchar_t *result =
5244 my_asnwprintf (NULL, &length, L"%w64X %d", (uint64_t) 12345, 33, 44, 55);
5245 ASSERT (wcscmp (result, L"3039 33") == 0);
5246 ASSERT (length == wcslen (result));
5247 free (result);
5251 size_t length;
5252 wchar_t *result =
5253 my_asnwprintf (NULL, &length, L"%wf8X %d", (uint_fast8_t) 42, 33, 44, 55);
5254 ASSERT (wcscmp (result, L"2A 33") == 0);
5255 ASSERT (length == wcslen (result));
5256 free (result);
5260 size_t length;
5261 wchar_t *result =
5262 my_asnwprintf (NULL, &length, L"%wf16X %d", (uint_fast16_t) 12345, 33, 44, 55);
5263 ASSERT (wcscmp (result, L"3039 33") == 0);
5264 ASSERT (length == wcslen (result));
5265 free (result);
5269 size_t length;
5270 wchar_t *result =
5271 my_asnwprintf (NULL, &length, L"%wf32X %d", (uint_fast32_t) 12345, 33, 44, 55);
5272 ASSERT (wcscmp (result, L"3039 33") == 0);
5273 ASSERT (length == wcslen (result));
5274 free (result);
5278 size_t length;
5279 wchar_t *result =
5280 my_asnwprintf (NULL, &length, L"%wf64X %d", (uint_fast64_t) 12345, 33, 44, 55);
5281 ASSERT (wcscmp (result, L"3039 33") == 0);
5282 ASSERT (length == wcslen (result));
5283 free (result);
5286 #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) && !defined __UCLIBC__
5287 /* Test that the 'I' flag is supported. */
5289 size_t length;
5290 wchar_t *result =
5291 my_asnwprintf (NULL, &length, L"%Id %d", 1234567, 99);
5292 ASSERT (result != NULL);
5293 ASSERT (wcscmp (result, L"1234567 99") == 0);
5294 ASSERT (length == wcslen (result));
5295 free (result);
5297 #endif
5300 static wchar_t *
5301 my_asnwprintf (wchar_t *resultbuf, size_t *lengthp, const wchar_t *format, ...)
5303 va_list args;
5304 wchar_t *ret;
5306 va_start (args, format);
5307 ret = vasnwprintf (resultbuf, lengthp, format, args);
5308 va_end (args);
5309 return ret;
5312 static void
5313 test_vasnwprintf ()
5315 test_function (my_asnwprintf);
5318 static void
5319 test_asnwprintf ()
5321 test_function (asnwprintf);
5325 main (int argc, char *argv[])
5327 test_vasnwprintf ();
5328 test_asnwprintf ();
5329 return test_exit_status;