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