X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-vasnprintf-posix.c;h=4fd21c8836bbcf91efd530dc97a0edbc5d374432;hb=cd56634a4a8179fd5a4419fbb3e27211b042ab1c;hp=730c1053688e910fa02aabe3109f74a84d5c24d3;hpb=e10764b1e130842f06b574ee7fb96c8f018bc60b;p=gnulib.git diff --git a/tests/test-vasnprintf-posix.c b/tests/test-vasnprintf-posix.c index 730c10536..4fd21c883 100644 --- a/tests/test-vasnprintf-posix.c +++ b/tests/test-vasnprintf-posix.c @@ -1,10 +1,10 @@ /* Test of POSIX compatible vasnprintf() and asnprintf() functions. - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007-2014 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or modify + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -12,57 +12,49 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + along with this program. If not, see . */ /* Written by Bruno Haible , 2007. */ -#ifdef HAVE_CONFIG_H -# include -#endif +#include #include "vasnprintf.h" +#include +#include #include #include -#include #include #include #include -#define SIZEOF(array) (sizeof (array) / sizeof (array[0])) -#define ASSERT(expr) \ - do \ - { \ - if (!(expr)) \ - { \ - fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ - abort (); \ - } \ - } \ - while (0) - -/* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0. */ -#ifdef __DECC -static double -NaN () -{ - static double zero = 0.0; - return zero / zero; -} -#else -# define NaN() (0.0 / 0.0) -#endif +#include "macros.h" +#include "minus-zero.h" +#include "infinity.h" +#include "nan.h" /* The SGI MIPS floating-point format does not distinguish 0.0 and -0.0. */ static int have_minus_zero () { static double plus_zero = 0.0; - static double minus_zero = -0.0; + double minus_zero = minus_zerod; return memcmp (&plus_zero, &minus_zero, sizeof (double)) != 0; } +/* Representation of an 80-bit 'long double' as an initializer for a sequence + of 'unsigned int' words. */ +#ifdef WORDS_BIGENDIAN +# define LDBL80_WORDS(exponent,manthi,mantlo) \ + { ((unsigned int) (exponent) << 16) | ((unsigned int) (manthi) >> 16), \ + ((unsigned int) (manthi) << 16) | (unsigned int) (mantlo) >> 16), \ + (unsigned int) (mantlo) << 16 \ + } +#else +# define LDBL80_WORDS(exponent,manthi,mantlo) \ + { mantlo, manthi, exponent } +#endif + static int strmatch (const char *pattern, const char *string) { @@ -74,6 +66,27 @@ strmatch (const char *pattern, const char *string) return 1; } +/* Test whether string[start_index..end_index-1] is a valid textual + representation of NaN. */ +static int +strisnan (const char *string, size_t start_index, size_t end_index, int uppercase) +{ + if (start_index < end_index) + { + if (string[start_index] == '-') + start_index++; + if (start_index + 3 <= end_index + && memcmp (string + start_index, uppercase ? "NAN" : "nan", 3) == 0) + { + start_index += 3; + if (start_index == end_index + || (string[start_index] == '(' && string[end_index - 1] == ')')) + return 1; + } + } + return 0; +} + static void test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { @@ -104,10 +117,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) ASSERT (strcmp (result, "12345") == 0); ASSERT (length == 5); if (size < 6) - ASSERT (result != buf); - ASSERT (memcmp (buf + size, "DEADBEEF" + size, 8 - size) == 0); + ASSERT (result != buf); + ASSERT (memcmp (buf + size, &"DEADBEEF"[size], 8 - size) == 0); if (result != buf) - free (result); + free (result); } /* Test support of size specifiers as in C99. */ @@ -161,9 +174,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%a %d", 3.1416015625, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.922p+1 33") == 0 - || strcmp (result, "0x3.244p+0 33") == 0 - || strcmp (result, "0x6.488p-1 33") == 0 - || strcmp (result, "0xc.91p-2 33") == 0); + || strcmp (result, "0x3.244p+0 33") == 0 + || strcmp (result, "0x6.488p-1 33") == 0 + || strcmp (result, "0xc.91p-2 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -174,9 +187,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%A %d", -3.1416015625, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "-0X1.922P+1 33") == 0 - || strcmp (result, "-0X3.244P+0 33") == 0 - || strcmp (result, "-0X6.488P-1 33") == 0 - || strcmp (result, "-0XC.91P-2 33") == 0); + || strcmp (result, "-0X3.244P+0 33") == 0 + || strcmp (result, "-0X6.488P-1 33") == 0 + || strcmp (result, "-0XC.91P-2 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -194,7 +207,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%a %d", -0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%a %d", minus_zerod, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0x0p+0 33") == 0); @@ -205,7 +218,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Positive infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%a %d", 1.0 / 0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%a %d", Infinityd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "inf 33") == 0); ASSERT (length == strlen (result)); @@ -215,7 +228,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%a %d", -1.0 / 0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%a %d", - Infinityd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "-inf 33") == 0); ASSERT (length == strlen (result)); @@ -225,11 +238,11 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* NaN. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%a %d", NaN (), 33, 44, 55); + my_asnprintf (NULL, &length, "%a %d", NaNd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && memcmp (result, "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -239,10 +252,11 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%.0a %d", 1.5, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "0x2p+0 33") == 0 - || strcmp (result, "0x3p-1 33") == 0 - || strcmp (result, "0x6p-2 33") == 0 - || strcmp (result, "0xcp-3 33") == 0); + ASSERT (strcmp (result, "0x1p+0 33") == 0 + || strcmp (result, "0x2p+0 33") == 0 + || strcmp (result, "0x3p-1 33") == 0 + || strcmp (result, "0x6p-2 33") == 0 + || strcmp (result, "0xcp-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -253,9 +267,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.0a %d", 1.51, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x2p+0 33") == 0 - || strcmp (result, "0x3p-1 33") == 0 - || strcmp (result, "0x6p-2 33") == 0 - || strcmp (result, "0xcp-3 33") == 0); + || strcmp (result, "0x3p-1 33") == 0 + || strcmp (result, "0x6p-2 33") == 0 + || strcmp (result, "0xcp-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -266,9 +280,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.1a %d", 1.51, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.8p+0 33") == 0 - || strcmp (result, "0x3.0p-1 33") == 0 - || strcmp (result, "0x6.1p-2 33") == 0 - || strcmp (result, "0xc.1p-3 33") == 0); + || strcmp (result, "0x3.0p-1 33") == 0 + || strcmp (result, "0x6.1p-2 33") == 0 + || strcmp (result, "0xc.1p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -279,9 +293,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.2a %d", 1.51, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.83p+0 33") == 0 - || strcmp (result, "0x3.05p-1 33") == 0 - || strcmp (result, "0x6.0ap-2 33") == 0 - || strcmp (result, "0xc.14p-3 33") == 0); + || strcmp (result, "0x3.05p-1 33") == 0 + || strcmp (result, "0x6.0ap-2 33") == 0 + || strcmp (result, "0xc.14p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -292,9 +306,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.3a %d", 1.51, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.829p+0 33") == 0 - || strcmp (result, "0x3.052p-1 33") == 0 - || strcmp (result, "0x6.0a4p-2 33") == 0 - || strcmp (result, "0xc.148p-3 33") == 0); + || strcmp (result, "0x3.052p-1 33") == 0 + || strcmp (result, "0x6.0a4p-2 33") == 0 + || strcmp (result, "0xc.148p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -305,23 +319,23 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.3a %d", 1.49999, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.800p+0 33") == 0 - || strcmp (result, "0x3.000p-1 33") == 0 - || strcmp (result, "0x6.000p-2 33") == 0 - || strcmp (result, "0xc.000p-3 33") == 0); + || strcmp (result, "0x3.000p-1 33") == 0 + || strcmp (result, "0x6.000p-2 33") == 0 + || strcmp (result, "0xc.000p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } { /* Rounding can turn a ...FFF into a ...000. - This shows a MacOS X 10.3.9 (Darwin 7.9) bug. */ + This shows a Mac OS X 10.3.9 (Darwin 7.9) bug. */ size_t length; char *result = my_asnprintf (NULL, &length, "%.1a %d", 1.999, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.0p+1 33") == 0 - || strcmp (result, "0x2.0p+0 33") == 0 - || strcmp (result, "0x4.0p-1 33") == 0 - || strcmp (result, "0x8.0p-2 33") == 0); + || strcmp (result, "0x2.0p+0 33") == 0 + || strcmp (result, "0x4.0p-1 33") == 0 + || strcmp (result, "0x8.0p-2 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -332,9 +346,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%10a %d", 1.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, " 0x1.cp+0 33") == 0 - || strcmp (result, " 0x3.8p-1 33") == 0 - || strcmp (result, " 0x7p-2 33") == 0 - || strcmp (result, " 0xep-3 33") == 0); + || strcmp (result, " 0x3.8p-1 33") == 0 + || strcmp (result, " 0x7p-2 33") == 0 + || strcmp (result, " 0xep-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -345,9 +359,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.10a %d", 1.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.c000000000p+0 33") == 0 - || strcmp (result, "0x3.8000000000p-1 33") == 0 - || strcmp (result, "0x7.0000000000p-2 33") == 0 - || strcmp (result, "0xe.0000000000p-3 33") == 0); + || strcmp (result, "0x3.8000000000p-1 33") == 0 + || strcmp (result, "0x7.0000000000p-2 33") == 0 + || strcmp (result, "0xe.0000000000p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -358,9 +372,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.50a %d", 1.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.c0000000000000000000000000000000000000000000000000p+0 33") == 0 - || strcmp (result, "0x3.80000000000000000000000000000000000000000000000000p-1 33") == 0 - || strcmp (result, "0x7.00000000000000000000000000000000000000000000000000p-2 33") == 0 - || strcmp (result, "0xe.00000000000000000000000000000000000000000000000000p-3 33") == 0); + || strcmp (result, "0x3.80000000000000000000000000000000000000000000000000p-1 33") == 0 + || strcmp (result, "0x7.00000000000000000000000000000000000000000000000000p-2 33") == 0 + || strcmp (result, "0xe.00000000000000000000000000000000000000000000000000p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -371,9 +385,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%-10a %d", 1.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.cp+0 33") == 0 - || strcmp (result, "0x3.8p-1 33") == 0 - || strcmp (result, "0x7p-2 33") == 0 - || strcmp (result, "0xep-3 33") == 0); + || strcmp (result, "0x3.8p-1 33") == 0 + || strcmp (result, "0x7p-2 33") == 0 + || strcmp (result, "0xep-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -384,9 +398,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%+a %d", 1.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "+0x1.cp+0 33") == 0 - || strcmp (result, "+0x3.8p-1 33") == 0 - || strcmp (result, "+0x7p-2 33") == 0 - || strcmp (result, "+0xep-3 33") == 0); + || strcmp (result, "+0x3.8p-1 33") == 0 + || strcmp (result, "+0x7p-2 33") == 0 + || strcmp (result, "+0xep-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -397,9 +411,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "% a %d", 1.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, " 0x1.cp+0 33") == 0 - || strcmp (result, " 0x3.8p-1 33") == 0 - || strcmp (result, " 0x7p-2 33") == 0 - || strcmp (result, " 0xep-3 33") == 0); + || strcmp (result, " 0x3.8p-1 33") == 0 + || strcmp (result, " 0x7p-2 33") == 0 + || strcmp (result, " 0xep-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -410,9 +424,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%#a %d", 1.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.cp+0 33") == 0 - || strcmp (result, "0x3.8p-1 33") == 0 - || strcmp (result, "0x7.p-2 33") == 0 - || strcmp (result, "0xe.p-3 33") == 0); + || strcmp (result, "0x3.8p-1 33") == 0 + || strcmp (result, "0x7.p-2 33") == 0 + || strcmp (result, "0xe.p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -423,9 +437,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%#a %d", 1.0, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.p+0 33") == 0 - || strcmp (result, "0x2.p-1 33") == 0 - || strcmp (result, "0x4.p-2 33") == 0 - || strcmp (result, "0x8.p-3 33") == 0); + || strcmp (result, "0x2.p-1 33") == 0 + || strcmp (result, "0x4.p-2 33") == 0 + || strcmp (result, "0x8.p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -436,9 +450,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%010a %d", 1.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x001.cp+0 33") == 0 - || strcmp (result, "0x003.8p-1 33") == 0 - || strcmp (result, "0x00007p-2 33") == 0 - || strcmp (result, "0x0000ep-3 33") == 0); + || strcmp (result, "0x003.8p-1 33") == 0 + || strcmp (result, "0x00007p-2 33") == 0 + || strcmp (result, "0x0000ep-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -446,7 +460,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* FLAG_ZERO with infinite number. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%010a %d", 1.0 / 0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%010a %d", Infinityd (), 33, 44, 55); ASSERT (result != NULL); /* "0000000inf 33" is not a valid result; see */ @@ -458,13 +472,13 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* FLAG_ZERO with NaN. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%020a %d", NaN (), 33, 44, 55); + my_asnprintf (NULL, &length, "%050a %d", NaNd (), 33, 44, 55); ASSERT (result != NULL); /* "0000000nan 33" is not a valid result; see */ - ASSERT (strlen (result) == 20 + 3 - && memcmp (result + strspn (result, " "), "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (strlen (result) == 50 + 3 + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -475,9 +489,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%La %d", 3.1416015625L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.922p+1 33") == 0 - || strcmp (result, "0x3.244p+0 33") == 0 - || strcmp (result, "0x6.488p-1 33") == 0 - || strcmp (result, "0xc.91p-2 33") == 0); + || strcmp (result, "0x3.244p+0 33") == 0 + || strcmp (result, "0x6.488p-1 33") == 0 + || strcmp (result, "0xc.91p-2 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -488,9 +502,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%LA %d", -3.1416015625L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "-0X1.922P+1 33") == 0 - || strcmp (result, "-0X3.244P+0 33") == 0 - || strcmp (result, "-0X6.488P-1 33") == 0 - || strcmp (result, "-0XC.91P-2 33") == 0); + || strcmp (result, "-0X3.244P+0 33") == 0 + || strcmp (result, "-0X6.488P-1 33") == 0 + || strcmp (result, "-0XC.91P-2 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -508,7 +522,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%La %d", -0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%La %d", minus_zerol, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0x0p+0 33") == 0); @@ -519,7 +533,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Positive infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%La %d", 1.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%La %d", Infinityl (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "inf 33") == 0); ASSERT (length == strlen (result)); @@ -529,7 +543,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%La %d", -1.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%La %d", - Infinityl (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "-inf 33") == 0); ASSERT (length == strlen (result)); @@ -539,14 +553,115 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* NaN. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%La %d", 0.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%La %d", NaNl (), 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } +#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 + { /* Quiet NaN. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%La %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { + /* Signalling NaN. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%La %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + /* The isnanl function should recognize Pseudo-NaNs, Pseudo-Infinities, + Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in + Intel IA-64 Architecture Software Developer's Manual, Volume 1: + Application Architecture. + Table 5-2 "Floating-Point Register Encodings" + Figure 5-6 "Memory to Floating-Point Register Data Translation" + */ + { /* Pseudo-NaN. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%La %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Pseudo-Infinity. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%La %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Pseudo-Zero. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%La %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Unnormalized number. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%La %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Pseudo-Denormal. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%La %d", x.value, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && memcmp (result, "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } +#endif { /* Rounding near the decimal point. */ size_t length; @@ -554,9 +669,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.0La %d", 1.5L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x2p+0 33") == 0 - || strcmp (result, "0x3p-1 33") == 0 - || strcmp (result, "0x6p-2 33") == 0 - || strcmp (result, "0xcp-3 33") == 0); + || strcmp (result, "0x3p-1 33") == 0 + || strcmp (result, "0x6p-2 33") == 0 + || strcmp (result, "0xcp-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -567,9 +682,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.0La %d", 1.51L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x2p+0 33") == 0 - || strcmp (result, "0x3p-1 33") == 0 - || strcmp (result, "0x6p-2 33") == 0 - || strcmp (result, "0xcp-3 33") == 0); + || strcmp (result, "0x3p-1 33") == 0 + || strcmp (result, "0x6p-2 33") == 0 + || strcmp (result, "0xcp-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -580,9 +695,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.1La %d", 1.51L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.8p+0 33") == 0 - || strcmp (result, "0x3.0p-1 33") == 0 - || strcmp (result, "0x6.1p-2 33") == 0 - || strcmp (result, "0xc.1p-3 33") == 0); + || strcmp (result, "0x3.0p-1 33") == 0 + || strcmp (result, "0x6.1p-2 33") == 0 + || strcmp (result, "0xc.1p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -593,9 +708,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.2La %d", 1.51L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.83p+0 33") == 0 - || strcmp (result, "0x3.05p-1 33") == 0 - || strcmp (result, "0x6.0ap-2 33") == 0 - || strcmp (result, "0xc.14p-3 33") == 0); + || strcmp (result, "0x3.05p-1 33") == 0 + || strcmp (result, "0x6.0ap-2 33") == 0 + || strcmp (result, "0xc.14p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -606,9 +721,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.3La %d", 1.51L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.829p+0 33") == 0 - || strcmp (result, "0x3.052p-1 33") == 0 - || strcmp (result, "0x6.0a4p-2 33") == 0 - || strcmp (result, "0xc.148p-3 33") == 0); + || strcmp (result, "0x3.052p-1 33") == 0 + || strcmp (result, "0x6.0a4p-2 33") == 0 + || strcmp (result, "0xc.148p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -619,24 +734,24 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.3La %d", 1.49999L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.800p+0 33") == 0 - || strcmp (result, "0x3.000p-1 33") == 0 - || strcmp (result, "0x6.000p-2 33") == 0 - || strcmp (result, "0xc.000p-3 33") == 0); + || strcmp (result, "0x3.000p-1 33") == 0 + || strcmp (result, "0x6.000p-2 33") == 0 + || strcmp (result, "0xc.000p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } { /* Rounding can turn a ...FFF into a ...000. - This shows a MacOS X 10.3.9 (Darwin 7.9) bug and a + This shows a Mac OS X 10.3.9 (Darwin 7.9) bug and a glibc 2.4 bug . */ size_t length; char *result = my_asnprintf (NULL, &length, "%.1La %d", 1.999L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.0p+1 33") == 0 - || strcmp (result, "0x2.0p+0 33") == 0 - || strcmp (result, "0x4.0p-1 33") == 0 - || strcmp (result, "0x8.0p-2 33") == 0); + || strcmp (result, "0x2.0p+0 33") == 0 + || strcmp (result, "0x4.0p-1 33") == 0 + || strcmp (result, "0x8.0p-2 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -647,9 +762,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%10La %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, " 0x1.cp+0 33") == 0 - || strcmp (result, " 0x3.8p-1 33") == 0 - || strcmp (result, " 0x7p-2 33") == 0 - || strcmp (result, " 0xep-3 33") == 0); + || strcmp (result, " 0x3.8p-1 33") == 0 + || strcmp (result, " 0x7p-2 33") == 0 + || strcmp (result, " 0xep-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -660,9 +775,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.10La %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.c000000000p+0 33") == 0 - || strcmp (result, "0x3.8000000000p-1 33") == 0 - || strcmp (result, "0x7.0000000000p-2 33") == 0 - || strcmp (result, "0xe.0000000000p-3 33") == 0); + || strcmp (result, "0x3.8000000000p-1 33") == 0 + || strcmp (result, "0x7.0000000000p-2 33") == 0 + || strcmp (result, "0xe.0000000000p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -673,9 +788,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.50La %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.c0000000000000000000000000000000000000000000000000p+0 33") == 0 - || strcmp (result, "0x3.80000000000000000000000000000000000000000000000000p-1 33") == 0 - || strcmp (result, "0x7.00000000000000000000000000000000000000000000000000p-2 33") == 0 - || strcmp (result, "0xe.00000000000000000000000000000000000000000000000000p-3 33") == 0); + || strcmp (result, "0x3.80000000000000000000000000000000000000000000000000p-1 33") == 0 + || strcmp (result, "0x7.00000000000000000000000000000000000000000000000000p-2 33") == 0 + || strcmp (result, "0xe.00000000000000000000000000000000000000000000000000p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -686,9 +801,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%-10La %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.cp+0 33") == 0 - || strcmp (result, "0x3.8p-1 33") == 0 - || strcmp (result, "0x7p-2 33") == 0 - || strcmp (result, "0xep-3 33") == 0); + || strcmp (result, "0x3.8p-1 33") == 0 + || strcmp (result, "0x7p-2 33") == 0 + || strcmp (result, "0xep-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -699,9 +814,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%+La %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "+0x1.cp+0 33") == 0 - || strcmp (result, "+0x3.8p-1 33") == 0 - || strcmp (result, "+0x7p-2 33") == 0 - || strcmp (result, "+0xep-3 33") == 0); + || strcmp (result, "+0x3.8p-1 33") == 0 + || strcmp (result, "+0x7p-2 33") == 0 + || strcmp (result, "+0xep-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -712,9 +827,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "% La %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, " 0x1.cp+0 33") == 0 - || strcmp (result, " 0x3.8p-1 33") == 0 - || strcmp (result, " 0x7p-2 33") == 0 - || strcmp (result, " 0xep-3 33") == 0); + || strcmp (result, " 0x3.8p-1 33") == 0 + || strcmp (result, " 0x7p-2 33") == 0 + || strcmp (result, " 0xep-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -725,9 +840,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%#La %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.cp+0 33") == 0 - || strcmp (result, "0x3.8p-1 33") == 0 - || strcmp (result, "0x7.p-2 33") == 0 - || strcmp (result, "0xe.p-3 33") == 0); + || strcmp (result, "0x3.8p-1 33") == 0 + || strcmp (result, "0x7.p-2 33") == 0 + || strcmp (result, "0xe.p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -738,9 +853,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%#La %d", 1.0L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x1.p+0 33") == 0 - || strcmp (result, "0x2.p-1 33") == 0 - || strcmp (result, "0x4.p-2 33") == 0 - || strcmp (result, "0x8.p-3 33") == 0); + || strcmp (result, "0x2.p-1 33") == 0 + || strcmp (result, "0x4.p-2 33") == 0 + || strcmp (result, "0x8.p-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -751,9 +866,9 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%010La %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0x001.cp+0 33") == 0 - || strcmp (result, "0x003.8p-1 33") == 0 - || strcmp (result, "0x00007p-2 33") == 0 - || strcmp (result, "0x0000ep-3 33") == 0); + || strcmp (result, "0x003.8p-1 33") == 0 + || strcmp (result, "0x00007p-2 33") == 0 + || strcmp (result, "0x0000ep-3 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -761,7 +876,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* FLAG_ZERO with infinite number. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%010La %d", 1.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%010La %d", Infinityl (), 33, 44, 55); ASSERT (result != NULL); /* "0000000inf 33" is not a valid result; see */ @@ -773,13 +888,13 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* FLAG_ZERO with NaN. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%020La %d", 0.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%050La %d", NaNl (), 33, 44, 55); ASSERT (result != NULL); /* "0000000nan 33" is not a valid result; see */ - ASSERT (strlen (result) == 20 + 3 - && memcmp (result + strspn (result, " "), "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (strlen (result) == 50 + 3 + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -809,91 +924,91 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Small and large positive numbers. */ static struct { double value; const char *string; } data[] = { - { 1.234321234321234e-37, "0.000000" }, - { 1.234321234321234e-36, "0.000000" }, - { 1.234321234321234e-35, "0.000000" }, - { 1.234321234321234e-34, "0.000000" }, - { 1.234321234321234e-33, "0.000000" }, - { 1.234321234321234e-32, "0.000000" }, - { 1.234321234321234e-31, "0.000000" }, - { 1.234321234321234e-30, "0.000000" }, - { 1.234321234321234e-29, "0.000000" }, - { 1.234321234321234e-28, "0.000000" }, - { 1.234321234321234e-27, "0.000000" }, - { 1.234321234321234e-26, "0.000000" }, - { 1.234321234321234e-25, "0.000000" }, - { 1.234321234321234e-24, "0.000000" }, - { 1.234321234321234e-23, "0.000000" }, - { 1.234321234321234e-22, "0.000000" }, - { 1.234321234321234e-21, "0.000000" }, - { 1.234321234321234e-20, "0.000000" }, - { 1.234321234321234e-19, "0.000000" }, - { 1.234321234321234e-18, "0.000000" }, - { 1.234321234321234e-17, "0.000000" }, - { 1.234321234321234e-16, "0.000000" }, - { 1.234321234321234e-15, "0.000000" }, - { 1.234321234321234e-14, "0.000000" }, - { 1.234321234321234e-13, "0.000000" }, - { 1.234321234321234e-12, "0.000000" }, - { 1.234321234321234e-11, "0.000000" }, - { 1.234321234321234e-10, "0.000000" }, - { 1.234321234321234e-9, "0.000000" }, - { 1.234321234321234e-8, "0.000000" }, - { 1.234321234321234e-7, "0.000000" }, - { 1.234321234321234e-6, "0.000001" }, - { 1.234321234321234e-5, "0.000012" }, - { 1.234321234321234e-4, "0.000123" }, - { 1.234321234321234e-3, "0.001234" }, - { 1.234321234321234e-2, "0.012343" }, - { 1.234321234321234e-1, "0.123432" }, - { 1.234321234321234, "1.234321" }, - { 1.234321234321234e1, "12.343212" }, - { 1.234321234321234e2, "123.432123" }, - { 1.234321234321234e3, "1234.321234" }, - { 1.234321234321234e4, "12343.212343" }, - { 1.234321234321234e5, "123432.123432" }, - { 1.234321234321234e6, "1234321.234321" }, - { 1.234321234321234e7, "12343212.343212" }, - { 1.234321234321234e8, "123432123.432123" }, - { 1.234321234321234e9, "1234321234.321234" }, - { 1.234321234321234e10, "12343212343.2123**" }, - { 1.234321234321234e11, "123432123432.123***" }, - { 1.234321234321234e12, "1234321234321.23****" }, - { 1.234321234321234e13, "12343212343212.3*****" }, - { 1.234321234321234e14, "123432123432123.******" }, - { 1.234321234321234e15, "1234321234321234.000000" }, - { 1.234321234321234e16, "123432123432123**.000000" }, - { 1.234321234321234e17, "123432123432123***.000000" }, - { 1.234321234321234e18, "123432123432123****.000000" }, - { 1.234321234321234e19, "123432123432123*****.000000" }, - { 1.234321234321234e20, "123432123432123******.000000" }, - { 1.234321234321234e21, "123432123432123*******.000000" }, - { 1.234321234321234e22, "123432123432123********.000000" }, - { 1.234321234321234e23, "123432123432123*********.000000" }, - { 1.234321234321234e24, "123432123432123**********.000000" }, - { 1.234321234321234e25, "123432123432123***********.000000" }, - { 1.234321234321234e26, "123432123432123************.000000" }, - { 1.234321234321234e27, "123432123432123*************.000000" }, - { 1.234321234321234e28, "123432123432123**************.000000" }, - { 1.234321234321234e29, "123432123432123***************.000000" }, - { 1.234321234321234e30, "123432123432123****************.000000" }, - { 1.234321234321234e31, "123432123432123*****************.000000" }, - { 1.234321234321234e32, "123432123432123******************.000000" }, - { 1.234321234321234e33, "123432123432123*******************.000000" }, - { 1.234321234321234e34, "123432123432123********************.000000" }, - { 1.234321234321234e35, "123432123432123*********************.000000" }, - { 1.234321234321234e36, "123432123432123**********************.000000" } + { 1.234321234321234e-37, "0.000000" }, + { 1.234321234321234e-36, "0.000000" }, + { 1.234321234321234e-35, "0.000000" }, + { 1.234321234321234e-34, "0.000000" }, + { 1.234321234321234e-33, "0.000000" }, + { 1.234321234321234e-32, "0.000000" }, + { 1.234321234321234e-31, "0.000000" }, + { 1.234321234321234e-30, "0.000000" }, + { 1.234321234321234e-29, "0.000000" }, + { 1.234321234321234e-28, "0.000000" }, + { 1.234321234321234e-27, "0.000000" }, + { 1.234321234321234e-26, "0.000000" }, + { 1.234321234321234e-25, "0.000000" }, + { 1.234321234321234e-24, "0.000000" }, + { 1.234321234321234e-23, "0.000000" }, + { 1.234321234321234e-22, "0.000000" }, + { 1.234321234321234e-21, "0.000000" }, + { 1.234321234321234e-20, "0.000000" }, + { 1.234321234321234e-19, "0.000000" }, + { 1.234321234321234e-18, "0.000000" }, + { 1.234321234321234e-17, "0.000000" }, + { 1.234321234321234e-16, "0.000000" }, + { 1.234321234321234e-15, "0.000000" }, + { 1.234321234321234e-14, "0.000000" }, + { 1.234321234321234e-13, "0.000000" }, + { 1.234321234321234e-12, "0.000000" }, + { 1.234321234321234e-11, "0.000000" }, + { 1.234321234321234e-10, "0.000000" }, + { 1.234321234321234e-9, "0.000000" }, + { 1.234321234321234e-8, "0.000000" }, + { 1.234321234321234e-7, "0.000000" }, + { 1.234321234321234e-6, "0.000001" }, + { 1.234321234321234e-5, "0.000012" }, + { 1.234321234321234e-4, "0.000123" }, + { 1.234321234321234e-3, "0.001234" }, + { 1.234321234321234e-2, "0.012343" }, + { 1.234321234321234e-1, "0.123432" }, + { 1.234321234321234, "1.234321" }, + { 1.234321234321234e1, "12.343212" }, + { 1.234321234321234e2, "123.432123" }, + { 1.234321234321234e3, "1234.321234" }, + { 1.234321234321234e4, "12343.212343" }, + { 1.234321234321234e5, "123432.123432" }, + { 1.234321234321234e6, "1234321.234321" }, + { 1.234321234321234e7, "12343212.343212" }, + { 1.234321234321234e8, "123432123.432123" }, + { 1.234321234321234e9, "1234321234.321234" }, + { 1.234321234321234e10, "12343212343.2123**" }, + { 1.234321234321234e11, "123432123432.123***" }, + { 1.234321234321234e12, "1234321234321.23****" }, + { 1.234321234321234e13, "12343212343212.3*****" }, + { 1.234321234321234e14, "123432123432123.******" }, + { 1.234321234321234e15, "1234321234321234.000000" }, + { 1.234321234321234e16, "123432123432123**.000000" }, + { 1.234321234321234e17, "123432123432123***.000000" }, + { 1.234321234321234e18, "123432123432123****.000000" }, + { 1.234321234321234e19, "123432123432123*****.000000" }, + { 1.234321234321234e20, "123432123432123******.000000" }, + { 1.234321234321234e21, "123432123432123*******.000000" }, + { 1.234321234321234e22, "123432123432123********.000000" }, + { 1.234321234321234e23, "123432123432123*********.000000" }, + { 1.234321234321234e24, "123432123432123**********.000000" }, + { 1.234321234321234e25, "123432123432123***********.000000" }, + { 1.234321234321234e26, "123432123432123************.000000" }, + { 1.234321234321234e27, "123432123432123*************.000000" }, + { 1.234321234321234e28, "123432123432123**************.000000" }, + { 1.234321234321234e29, "123432123432123***************.000000" }, + { 1.234321234321234e30, "123432123432123****************.000000" }, + { 1.234321234321234e31, "123432123432123*****************.000000" }, + { 1.234321234321234e32, "123432123432123******************.000000" }, + { 1.234321234321234e33, "123432123432123*******************.000000" }, + { 1.234321234321234e34, "123432123432123********************.000000" }, + { 1.234321234321234e35, "123432123432123*********************.000000" }, + { 1.234321234321234e36, "123432123432123**********************.000000" } }; size_t k; for (k = 0; k < SIZEOF (data); k++) { - size_t length; - char *result = - my_asnprintf (NULL, &length, "%f", data[k].value); - ASSERT (result != NULL); - ASSERT (strmatch (data[k].string, result)); - ASSERT (length == strlen (result)); - free (result); + size_t length; + char *result = + my_asnprintf (NULL, &length, "%f", data[k].value); + ASSERT (result != NULL); + ASSERT (strmatch (data[k].string, result)); + ASSERT (length == strlen (result)); + free (result); } } @@ -920,7 +1035,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%f %d", -0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%f %d", minus_zerod, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0.000000 33") == 0); @@ -931,10 +1046,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Positive infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%f %d", 1.0 / 0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%f %d", Infinityd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "inf 33") == 0 - || strcmp (result, "infinity 33") == 0); + || strcmp (result, "infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -942,10 +1057,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%f %d", -1.0 / 0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%f %d", - Infinityd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "-inf 33") == 0 - || strcmp (result, "-infinity 33") == 0); + || strcmp (result, "-infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -953,11 +1068,11 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* NaN. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%f %d", NaN (), 33, 44, 55); + my_asnprintf (NULL, &length, "%f %d", NaNd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && memcmp (result, "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1035,10 +1150,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* FLAG_ZERO with infinite number. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%015f %d", -1.0 / 0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%015f %d", - Infinityd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, " -inf 33") == 0 - || strcmp (result, " -infinity 33") == 0); + || strcmp (result, " -infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1046,11 +1161,11 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* FLAG_ZERO with NaN. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%020f %d", NaN (), 33, 44, 55); + my_asnprintf (NULL, &length, "%050f %d", NaNd (), 33, 44, 55); ASSERT (result != NULL); - ASSERT (strlen (result) == 20 + 3 - && memcmp (result + strspn (result, " "), "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (strlen (result) == 50 + 3 + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1065,6 +1180,26 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) free (result); } + { /* Precision with no rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.2f %d", 999.951, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.2f %d", 999.996, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* A positive number. */ size_t length; char *result = @@ -1088,91 +1223,91 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Small and large positive numbers. */ static struct { long double value; const char *string; } data[] = { - { 1.234321234321234e-37L, "0.000000" }, - { 1.234321234321234e-36L, "0.000000" }, - { 1.234321234321234e-35L, "0.000000" }, - { 1.234321234321234e-34L, "0.000000" }, - { 1.234321234321234e-33L, "0.000000" }, - { 1.234321234321234e-32L, "0.000000" }, - { 1.234321234321234e-31L, "0.000000" }, - { 1.234321234321234e-30L, "0.000000" }, - { 1.234321234321234e-29L, "0.000000" }, - { 1.234321234321234e-28L, "0.000000" }, - { 1.234321234321234e-27L, "0.000000" }, - { 1.234321234321234e-26L, "0.000000" }, - { 1.234321234321234e-25L, "0.000000" }, - { 1.234321234321234e-24L, "0.000000" }, - { 1.234321234321234e-23L, "0.000000" }, - { 1.234321234321234e-22L, "0.000000" }, - { 1.234321234321234e-21L, "0.000000" }, - { 1.234321234321234e-20L, "0.000000" }, - { 1.234321234321234e-19L, "0.000000" }, - { 1.234321234321234e-18L, "0.000000" }, - { 1.234321234321234e-17L, "0.000000" }, - { 1.234321234321234e-16L, "0.000000" }, - { 1.234321234321234e-15L, "0.000000" }, - { 1.234321234321234e-14L, "0.000000" }, - { 1.234321234321234e-13L, "0.000000" }, - { 1.234321234321234e-12L, "0.000000" }, - { 1.234321234321234e-11L, "0.000000" }, - { 1.234321234321234e-10L, "0.000000" }, - { 1.234321234321234e-9L, "0.000000" }, - { 1.234321234321234e-8L, "0.000000" }, - { 1.234321234321234e-7L, "0.000000" }, - { 1.234321234321234e-6L, "0.000001" }, - { 1.234321234321234e-5L, "0.000012" }, - { 1.234321234321234e-4L, "0.000123" }, - { 1.234321234321234e-3L, "0.001234" }, - { 1.234321234321234e-2L, "0.012343" }, - { 1.234321234321234e-1L, "0.123432" }, - { 1.234321234321234L, "1.234321" }, - { 1.234321234321234e1L, "12.343212" }, - { 1.234321234321234e2L, "123.432123" }, - { 1.234321234321234e3L, "1234.321234" }, - { 1.234321234321234e4L, "12343.212343" }, - { 1.234321234321234e5L, "123432.123432" }, - { 1.234321234321234e6L, "1234321.234321" }, - { 1.234321234321234e7L, "12343212.343212" }, - { 1.234321234321234e8L, "123432123.432123" }, - { 1.234321234321234e9L, "1234321234.321234" }, - { 1.234321234321234e10L, "12343212343.2123**" }, - { 1.234321234321234e11L, "123432123432.123***" }, - { 1.234321234321234e12L, "1234321234321.23****" }, - { 1.234321234321234e13L, "12343212343212.3*****" }, - { 1.234321234321234e14L, "123432123432123.******" }, - { 1.234321234321234e15L, "1234321234321234.000000" }, - { 1.234321234321234e16L, "123432123432123**.000000" }, - { 1.234321234321234e17L, "123432123432123***.000000" }, - { 1.234321234321234e18L, "123432123432123****.000000" }, - { 1.234321234321234e19L, "123432123432123*****.000000" }, - { 1.234321234321234e20L, "123432123432123******.000000" }, - { 1.234321234321234e21L, "123432123432123*******.000000" }, - { 1.234321234321234e22L, "123432123432123********.000000" }, - { 1.234321234321234e23L, "123432123432123*********.000000" }, - { 1.234321234321234e24L, "123432123432123**********.000000" }, - { 1.234321234321234e25L, "123432123432123***********.000000" }, - { 1.234321234321234e26L, "123432123432123************.000000" }, - { 1.234321234321234e27L, "123432123432123*************.000000" }, - { 1.234321234321234e28L, "123432123432123**************.000000" }, - { 1.234321234321234e29L, "123432123432123***************.000000" }, - { 1.234321234321234e30L, "123432123432123****************.000000" }, - { 1.234321234321234e31L, "123432123432123*****************.000000" }, - { 1.234321234321234e32L, "123432123432123******************.000000" }, - { 1.234321234321234e33L, "123432123432123*******************.000000" }, - { 1.234321234321234e34L, "123432123432123********************.000000" }, - { 1.234321234321234e35L, "123432123432123*********************.000000" }, - { 1.234321234321234e36L, "123432123432123**********************.000000" } + { 1.234321234321234e-37L, "0.000000" }, + { 1.234321234321234e-36L, "0.000000" }, + { 1.234321234321234e-35L, "0.000000" }, + { 1.234321234321234e-34L, "0.000000" }, + { 1.234321234321234e-33L, "0.000000" }, + { 1.234321234321234e-32L, "0.000000" }, + { 1.234321234321234e-31L, "0.000000" }, + { 1.234321234321234e-30L, "0.000000" }, + { 1.234321234321234e-29L, "0.000000" }, + { 1.234321234321234e-28L, "0.000000" }, + { 1.234321234321234e-27L, "0.000000" }, + { 1.234321234321234e-26L, "0.000000" }, + { 1.234321234321234e-25L, "0.000000" }, + { 1.234321234321234e-24L, "0.000000" }, + { 1.234321234321234e-23L, "0.000000" }, + { 1.234321234321234e-22L, "0.000000" }, + { 1.234321234321234e-21L, "0.000000" }, + { 1.234321234321234e-20L, "0.000000" }, + { 1.234321234321234e-19L, "0.000000" }, + { 1.234321234321234e-18L, "0.000000" }, + { 1.234321234321234e-17L, "0.000000" }, + { 1.234321234321234e-16L, "0.000000" }, + { 1.234321234321234e-15L, "0.000000" }, + { 1.234321234321234e-14L, "0.000000" }, + { 1.234321234321234e-13L, "0.000000" }, + { 1.234321234321234e-12L, "0.000000" }, + { 1.234321234321234e-11L, "0.000000" }, + { 1.234321234321234e-10L, "0.000000" }, + { 1.234321234321234e-9L, "0.000000" }, + { 1.234321234321234e-8L, "0.000000" }, + { 1.234321234321234e-7L, "0.000000" }, + { 1.234321234321234e-6L, "0.000001" }, + { 1.234321234321234e-5L, "0.000012" }, + { 1.234321234321234e-4L, "0.000123" }, + { 1.234321234321234e-3L, "0.001234" }, + { 1.234321234321234e-2L, "0.012343" }, + { 1.234321234321234e-1L, "0.123432" }, + { 1.234321234321234L, "1.234321" }, + { 1.234321234321234e1L, "12.343212" }, + { 1.234321234321234e2L, "123.432123" }, + { 1.234321234321234e3L, "1234.321234" }, + { 1.234321234321234e4L, "12343.212343" }, + { 1.234321234321234e5L, "123432.123432" }, + { 1.234321234321234e6L, "1234321.234321" }, + { 1.234321234321234e7L, "12343212.343212" }, + { 1.234321234321234e8L, "123432123.432123" }, + { 1.234321234321234e9L, "1234321234.321234" }, + { 1.234321234321234e10L, "12343212343.2123**" }, + { 1.234321234321234e11L, "123432123432.123***" }, + { 1.234321234321234e12L, "1234321234321.23****" }, + { 1.234321234321234e13L, "12343212343212.3*****" }, + { 1.234321234321234e14L, "123432123432123.******" }, + { 1.234321234321234e15L, "1234321234321234.000000" }, + { 1.234321234321234e16L, "123432123432123**.000000" }, + { 1.234321234321234e17L, "123432123432123***.000000" }, + { 1.234321234321234e18L, "123432123432123****.000000" }, + { 1.234321234321234e19L, "123432123432123*****.000000" }, + { 1.234321234321234e20L, "123432123432123******.000000" }, + { 1.234321234321234e21L, "123432123432123*******.000000" }, + { 1.234321234321234e22L, "123432123432123********.000000" }, + { 1.234321234321234e23L, "123432123432123*********.000000" }, + { 1.234321234321234e24L, "123432123432123**********.000000" }, + { 1.234321234321234e25L, "123432123432123***********.000000" }, + { 1.234321234321234e26L, "123432123432123************.000000" }, + { 1.234321234321234e27L, "123432123432123*************.000000" }, + { 1.234321234321234e28L, "123432123432123**************.000000" }, + { 1.234321234321234e29L, "123432123432123***************.000000" }, + { 1.234321234321234e30L, "123432123432123****************.000000" }, + { 1.234321234321234e31L, "123432123432123*****************.000000" }, + { 1.234321234321234e32L, "123432123432123******************.000000" }, + { 1.234321234321234e33L, "123432123432123*******************.000000" }, + { 1.234321234321234e34L, "123432123432123********************.000000" }, + { 1.234321234321234e35L, "123432123432123*********************.000000" }, + { 1.234321234321234e36L, "123432123432123**********************.000000" } }; size_t k; for (k = 0; k < SIZEOF (data); k++) { - size_t length; - char *result = - my_asnprintf (NULL, &length, "%Lf", data[k].value); - ASSERT (result != NULL); - ASSERT (strmatch (data[k].string, result)); - ASSERT (length == strlen (result)); - free (result); + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lf", data[k].value); + ASSERT (result != NULL); + ASSERT (strmatch (data[k].string, result)); + ASSERT (length == strlen (result)); + free (result); } } @@ -1199,7 +1334,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%Lf %d", -0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%Lf %d", minus_zerol, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0.000000 33") == 0); @@ -1210,10 +1345,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Positive infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%Lf %d", 1.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%Lf %d", Infinityl (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "inf 33") == 0 - || strcmp (result, "infinity 33") == 0); + || strcmp (result, "infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1221,26 +1356,126 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%Lf %d", -1.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%Lf %d", - Infinityl (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "-inf 33") == 0 - || strcmp (result, "-infinity 33") == 0); + || strcmp (result, "-infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } { /* NaN. */ - static long double zero = 0.0L; size_t length; char *result = - my_asnprintf (NULL, &length, "%Lf %d", zero / zero, 33, 44, 55); + my_asnprintf (NULL, &length, "%Lf %d", NaNl (), 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } +#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 + { /* Quiet NaN. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lf %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { + /* Signalling NaN. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lf %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + /* The isnanl function should recognize Pseudo-NaNs, Pseudo-Infinities, + Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in + Intel IA-64 Architecture Software Developer's Manual, Volume 1: + Application Architecture. + Table 5-2 "Floating-Point Register Encodings" + Figure 5-6 "Memory to Floating-Point Register Data Translation" + */ + { /* Pseudo-NaN. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lf %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Pseudo-Infinity. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lf %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Pseudo-Zero. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lf %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Unnormalized number. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lf %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Pseudo-Denormal. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lf %d", x.value, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && memcmp (result, "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } +#endif { /* Width. */ size_t length; @@ -1315,23 +1550,22 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* FLAG_ZERO with infinite number. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%015Lf %d", -1.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%015Lf %d", - Infinityl (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, " -inf 33") == 0 - || strcmp (result, " -infinity 33") == 0); + || strcmp (result, " -infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } { /* FLAG_ZERO with NaN. */ - static long double zero = 0.0L; size_t length; char *result = - my_asnprintf (NULL, &length, "%020Lf %d", zero / zero, 33, 44, 55); + my_asnprintf (NULL, &length, "%050Lf %d", NaNl (), 33, 44, 55); ASSERT (result != NULL); - ASSERT (strlen (result) == 20 + 3 - && memcmp (result + strspn (result, " "), "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (strlen (result) == 50 + 3 + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1346,6 +1580,26 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) free (result); } + { /* Precision with no rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.2Lf %d", 999.951L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.2Lf %d", 999.996L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + /* Test the support of the %F format directive. */ { /* A positive number. */ @@ -1391,7 +1645,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%F %d", -0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%F %d", minus_zerod, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0.000000 33") == 0); @@ -1402,10 +1656,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Positive infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%F %d", 1.0 / 0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%F %d", Infinityd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "INF 33") == 0 - || strcmp (result, "INFINITY 33") == 0); + || strcmp (result, "INFINITY 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1413,10 +1667,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%F %d", -1.0 / 0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%F %d", - Infinityd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "-INF 33") == 0 - || strcmp (result, "-INFINITY 33") == 0); + || strcmp (result, "-INFINITY 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1424,11 +1678,11 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* NaN. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%F %d", NaN (), 33, 44, 55); + my_asnprintf (NULL, &length, "%F %d", NaNd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && memcmp (result, "NAN", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + && strisnan (result, 0, strlen (result) - 3, 1) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1446,10 +1700,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* FLAG_ZERO with infinite number. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%015F %d", -1.0 / 0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%015F %d", - Infinityd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, " -INF 33") == 0 - || strcmp (result, " -INFINITY 33") == 0); + || strcmp (result, " -INFINITY 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1464,6 +1718,26 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) free (result); } + { /* Precision with no rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.2F %d", 999.951, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.2F %d", 999.996, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* A positive number. */ size_t length; char *result = @@ -1507,7 +1781,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%LF %d", -0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%LF %d", minus_zerol, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0.000000 33") == 0); @@ -1518,10 +1792,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Positive infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%LF %d", 1.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%LF %d", Infinityl (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "INF 33") == 0 - || strcmp (result, "INFINITY 33") == 0); + || strcmp (result, "INFINITY 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1529,23 +1803,22 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%LF %d", -1.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%LF %d", - Infinityl (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "-INF 33") == 0 - || strcmp (result, "-INFINITY 33") == 0); + || strcmp (result, "-INFINITY 33") == 0); ASSERT (length == strlen (result)); free (result); } { /* NaN. */ - static long double zero = 0.0L; size_t length; char *result = - my_asnprintf (NULL, &length, "%LF %d", zero / zero, 33, 44, 55); + my_asnprintf (NULL, &length, "%LF %d", NaNl (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && memcmp (result, "NAN", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + && strisnan (result, 0, strlen (result) - 3, 1) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1563,10 +1836,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* FLAG_ZERO with infinite number. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%015LF %d", -1.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%015LF %d", - Infinityl (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, " -INF 33") == 0 - || strcmp (result, " -INFINITY 33") == 0); + || strcmp (result, " -INFINITY 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1581,6 +1854,26 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) free (result); } + { /* Precision with no rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.2LF %d", 999.951L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.2LF %d", 999.996L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1000.00 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + /* Test the support of the %e format directive. */ { /* A positive number. */ @@ -1589,7 +1882,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%e %d", 12.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "1.275000e+01 33") == 0 - || strcmp (result, "1.275000e+001 33") == 0); + || strcmp (result, "1.275000e+001 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1600,7 +1893,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%e %d", 1234567.0, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "1.234567e+06 33") == 0 - || strcmp (result, "1.234567e+006 33") == 0); + || strcmp (result, "1.234567e+006 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1608,99 +1901,99 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Small and large positive numbers. */ static struct { double value; const char *string; } data[] = { - { 1.234321234321234e-37, "1.234321e-37" }, - { 1.234321234321234e-36, "1.234321e-36" }, - { 1.234321234321234e-35, "1.234321e-35" }, - { 1.234321234321234e-34, "1.234321e-34" }, - { 1.234321234321234e-33, "1.234321e-33" }, - { 1.234321234321234e-32, "1.234321e-32" }, - { 1.234321234321234e-31, "1.234321e-31" }, - { 1.234321234321234e-30, "1.234321e-30" }, - { 1.234321234321234e-29, "1.234321e-29" }, - { 1.234321234321234e-28, "1.234321e-28" }, - { 1.234321234321234e-27, "1.234321e-27" }, - { 1.234321234321234e-26, "1.234321e-26" }, - { 1.234321234321234e-25, "1.234321e-25" }, - { 1.234321234321234e-24, "1.234321e-24" }, - { 1.234321234321234e-23, "1.234321e-23" }, - { 1.234321234321234e-22, "1.234321e-22" }, - { 1.234321234321234e-21, "1.234321e-21" }, - { 1.234321234321234e-20, "1.234321e-20" }, - { 1.234321234321234e-19, "1.234321e-19" }, - { 1.234321234321234e-18, "1.234321e-18" }, - { 1.234321234321234e-17, "1.234321e-17" }, - { 1.234321234321234e-16, "1.234321e-16" }, - { 1.234321234321234e-15, "1.234321e-15" }, - { 1.234321234321234e-14, "1.234321e-14" }, - { 1.234321234321234e-13, "1.234321e-13" }, - { 1.234321234321234e-12, "1.234321e-12" }, - { 1.234321234321234e-11, "1.234321e-11" }, - { 1.234321234321234e-10, "1.234321e-10" }, - { 1.234321234321234e-9, "1.234321e-09" }, - { 1.234321234321234e-8, "1.234321e-08" }, - { 1.234321234321234e-7, "1.234321e-07" }, - { 1.234321234321234e-6, "1.234321e-06" }, - { 1.234321234321234e-5, "1.234321e-05" }, - { 1.234321234321234e-4, "1.234321e-04" }, - { 1.234321234321234e-3, "1.234321e-03" }, - { 1.234321234321234e-2, "1.234321e-02" }, - { 1.234321234321234e-1, "1.234321e-01" }, - { 1.234321234321234, "1.234321e+00" }, - { 1.234321234321234e1, "1.234321e+01" }, - { 1.234321234321234e2, "1.234321e+02" }, - { 1.234321234321234e3, "1.234321e+03" }, - { 1.234321234321234e4, "1.234321e+04" }, - { 1.234321234321234e5, "1.234321e+05" }, - { 1.234321234321234e6, "1.234321e+06" }, - { 1.234321234321234e7, "1.234321e+07" }, - { 1.234321234321234e8, "1.234321e+08" }, - { 1.234321234321234e9, "1.234321e+09" }, - { 1.234321234321234e10, "1.234321e+10" }, - { 1.234321234321234e11, "1.234321e+11" }, - { 1.234321234321234e12, "1.234321e+12" }, - { 1.234321234321234e13, "1.234321e+13" }, - { 1.234321234321234e14, "1.234321e+14" }, - { 1.234321234321234e15, "1.234321e+15" }, - { 1.234321234321234e16, "1.234321e+16" }, - { 1.234321234321234e17, "1.234321e+17" }, - { 1.234321234321234e18, "1.234321e+18" }, - { 1.234321234321234e19, "1.234321e+19" }, - { 1.234321234321234e20, "1.234321e+20" }, - { 1.234321234321234e21, "1.234321e+21" }, - { 1.234321234321234e22, "1.234321e+22" }, - { 1.234321234321234e23, "1.234321e+23" }, - { 1.234321234321234e24, "1.234321e+24" }, - { 1.234321234321234e25, "1.234321e+25" }, - { 1.234321234321234e26, "1.234321e+26" }, - { 1.234321234321234e27, "1.234321e+27" }, - { 1.234321234321234e28, "1.234321e+28" }, - { 1.234321234321234e29, "1.234321e+29" }, - { 1.234321234321234e30, "1.234321e+30" }, - { 1.234321234321234e31, "1.234321e+31" }, - { 1.234321234321234e32, "1.234321e+32" }, - { 1.234321234321234e33, "1.234321e+33" }, - { 1.234321234321234e34, "1.234321e+34" }, - { 1.234321234321234e35, "1.234321e+35" }, - { 1.234321234321234e36, "1.234321e+36" } + { 1.234321234321234e-37, "1.234321e-37" }, + { 1.234321234321234e-36, "1.234321e-36" }, + { 1.234321234321234e-35, "1.234321e-35" }, + { 1.234321234321234e-34, "1.234321e-34" }, + { 1.234321234321234e-33, "1.234321e-33" }, + { 1.234321234321234e-32, "1.234321e-32" }, + { 1.234321234321234e-31, "1.234321e-31" }, + { 1.234321234321234e-30, "1.234321e-30" }, + { 1.234321234321234e-29, "1.234321e-29" }, + { 1.234321234321234e-28, "1.234321e-28" }, + { 1.234321234321234e-27, "1.234321e-27" }, + { 1.234321234321234e-26, "1.234321e-26" }, + { 1.234321234321234e-25, "1.234321e-25" }, + { 1.234321234321234e-24, "1.234321e-24" }, + { 1.234321234321234e-23, "1.234321e-23" }, + { 1.234321234321234e-22, "1.234321e-22" }, + { 1.234321234321234e-21, "1.234321e-21" }, + { 1.234321234321234e-20, "1.234321e-20" }, + { 1.234321234321234e-19, "1.234321e-19" }, + { 1.234321234321234e-18, "1.234321e-18" }, + { 1.234321234321234e-17, "1.234321e-17" }, + { 1.234321234321234e-16, "1.234321e-16" }, + { 1.234321234321234e-15, "1.234321e-15" }, + { 1.234321234321234e-14, "1.234321e-14" }, + { 1.234321234321234e-13, "1.234321e-13" }, + { 1.234321234321234e-12, "1.234321e-12" }, + { 1.234321234321234e-11, "1.234321e-11" }, + { 1.234321234321234e-10, "1.234321e-10" }, + { 1.234321234321234e-9, "1.234321e-09" }, + { 1.234321234321234e-8, "1.234321e-08" }, + { 1.234321234321234e-7, "1.234321e-07" }, + { 1.234321234321234e-6, "1.234321e-06" }, + { 1.234321234321234e-5, "1.234321e-05" }, + { 1.234321234321234e-4, "1.234321e-04" }, + { 1.234321234321234e-3, "1.234321e-03" }, + { 1.234321234321234e-2, "1.234321e-02" }, + { 1.234321234321234e-1, "1.234321e-01" }, + { 1.234321234321234, "1.234321e+00" }, + { 1.234321234321234e1, "1.234321e+01" }, + { 1.234321234321234e2, "1.234321e+02" }, + { 1.234321234321234e3, "1.234321e+03" }, + { 1.234321234321234e4, "1.234321e+04" }, + { 1.234321234321234e5, "1.234321e+05" }, + { 1.234321234321234e6, "1.234321e+06" }, + { 1.234321234321234e7, "1.234321e+07" }, + { 1.234321234321234e8, "1.234321e+08" }, + { 1.234321234321234e9, "1.234321e+09" }, + { 1.234321234321234e10, "1.234321e+10" }, + { 1.234321234321234e11, "1.234321e+11" }, + { 1.234321234321234e12, "1.234321e+12" }, + { 1.234321234321234e13, "1.234321e+13" }, + { 1.234321234321234e14, "1.234321e+14" }, + { 1.234321234321234e15, "1.234321e+15" }, + { 1.234321234321234e16, "1.234321e+16" }, + { 1.234321234321234e17, "1.234321e+17" }, + { 1.234321234321234e18, "1.234321e+18" }, + { 1.234321234321234e19, "1.234321e+19" }, + { 1.234321234321234e20, "1.234321e+20" }, + { 1.234321234321234e21, "1.234321e+21" }, + { 1.234321234321234e22, "1.234321e+22" }, + { 1.234321234321234e23, "1.234321e+23" }, + { 1.234321234321234e24, "1.234321e+24" }, + { 1.234321234321234e25, "1.234321e+25" }, + { 1.234321234321234e26, "1.234321e+26" }, + { 1.234321234321234e27, "1.234321e+27" }, + { 1.234321234321234e28, "1.234321e+28" }, + { 1.234321234321234e29, "1.234321e+29" }, + { 1.234321234321234e30, "1.234321e+30" }, + { 1.234321234321234e31, "1.234321e+31" }, + { 1.234321234321234e32, "1.234321e+32" }, + { 1.234321234321234e33, "1.234321e+33" }, + { 1.234321234321234e34, "1.234321e+34" }, + { 1.234321234321234e35, "1.234321e+35" }, + { 1.234321234321234e36, "1.234321e+36" } }; size_t k; for (k = 0; k < SIZEOF (data); k++) { - size_t length; - char *result = - my_asnprintf (NULL, &length, "%e", data[k].value); - const char *expected = data[k].string; - ASSERT (result != NULL); - ASSERT (strcmp (result, expected) == 0 - /* Some implementations produce exponents with 3 digits. */ - || (strlen (result) == strlen (expected) + 1 - && memcmp (result, expected, strlen (expected) - 2) == 0 - && result[strlen (expected) - 2] == '0' - && strcmp (result + strlen (expected) - 1, - expected + strlen (expected) - 2) - == 0)); - ASSERT (length == strlen (result)); - free (result); + size_t length; + char *result = + my_asnprintf (NULL, &length, "%e", data[k].value); + const char *expected = data[k].string; + ASSERT (result != NULL); + ASSERT (strcmp (result, expected) == 0 + /* Some implementations produce exponents with 3 digits. */ + || (strlen (result) == strlen (expected) + 1 + && memcmp (result, expected, strlen (expected) - 2) == 0 + && result[strlen (expected) - 2] == '0' + && strcmp (result + strlen (expected) - 1, + expected + strlen (expected) - 2) + == 0)); + ASSERT (length == strlen (result)); + free (result); } } @@ -1710,7 +2003,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%e %d", -0.03125, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "-3.125000e-02 33") == 0 - || strcmp (result, "-3.125000e-002 33") == 0); + || strcmp (result, "-3.125000e-002 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1721,7 +2014,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%e %d", 0.0, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0.000000e+00 33") == 0 - || strcmp (result, "0.000000e+000 33") == 0); + || strcmp (result, "0.000000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1729,11 +2022,11 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%e %d", -0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%e %d", minus_zerod, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0.000000e+00 33") == 0 - || strcmp (result, "-0.000000e+000 33") == 0); + || strcmp (result, "-0.000000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1741,10 +2034,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Positive infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%e %d", 1.0 / 0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%e %d", Infinityd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "inf 33") == 0 - || strcmp (result, "infinity 33") == 0); + || strcmp (result, "infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1752,10 +2045,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%e %d", -1.0 / 0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%e %d", - Infinityd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "-inf 33") == 0 - || strcmp (result, "-infinity 33") == 0); + || strcmp (result, "-infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1763,11 +2056,11 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* NaN. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%e %d", NaN (), 33, 44, 55); + my_asnprintf (NULL, &length, "%e %d", NaNd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && memcmp (result, "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1778,7 +2071,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%15e %d", 1.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, " 1.750000e+00 33") == 0 - || strcmp (result, " 1.750000e+000 33") == 0); + || strcmp (result, " 1.750000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1789,7 +2082,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%-15e %d", 1.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "1.750000e+00 33") == 0 - || strcmp (result, "1.750000e+000 33") == 0); + || strcmp (result, "1.750000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1800,7 +2093,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%+e %d", 1.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "+1.750000e+00 33") == 0 - || strcmp (result, "+1.750000e+000 33") == 0); + || strcmp (result, "+1.750000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1811,7 +2104,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "% e %d", 1.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, " 1.750000e+00 33") == 0 - || strcmp (result, " 1.750000e+000 33") == 0); + || strcmp (result, " 1.750000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1822,7 +2115,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%#e %d", 1.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "1.750000e+00 33") == 0 - || strcmp (result, "1.750000e+000 33") == 0); + || strcmp (result, "1.750000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1833,7 +2126,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%#.e %d", 1.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "2.e+00 33") == 0 - || strcmp (result, "2.e+000 33") == 0); + || strcmp (result, "2.e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1844,7 +2137,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%#.e %d", 9.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "1.e+01 33") == 0 - || strcmp (result, "1.e+001 33") == 0); + || strcmp (result, "1.e+001 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1855,7 +2148,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%015e %d", 1234.0, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "0001.234000e+03 33") == 0 - || strcmp (result, "001.234000e+003 33") == 0); + || strcmp (result, "001.234000e+003 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1863,10 +2156,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* FLAG_ZERO with infinite number. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%015e %d", -1.0 / 0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%015e %d", - Infinityd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, " -inf 33") == 0 - || strcmp (result, " -infinity 33") == 0); + || strcmp (result, " -infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1874,11 +2167,11 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* FLAG_ZERO with NaN. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%020e %d", NaN (), 33, 44, 55); + my_asnprintf (NULL, &length, "%050e %d", NaNd (), 33, 44, 55); ASSERT (result != NULL); - ASSERT (strlen (result) == 20 + 3 - && memcmp (result + strspn (result, " "), "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (strlen (result) == 50 + 3 + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1889,7 +2182,29 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.e %d", 1234.0, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "1e+03 33") == 0 - || strcmp (result, "1e+003 33") == 0); + || strcmp (result, "1e+003 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* Precision with no rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.4e %d", 999.951, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "9.9995e+02 33") == 0 + || strcmp (result, "9.9995e+002 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.4e %d", 999.996, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1.0000e+03 33") == 0 + || strcmp (result, "1.0000e+003 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1899,7 +2214,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%Le %d", 12.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1.275000e+01 33") == 0); + ASSERT (strcmp (result, "1.275000e+01 33") == 0 + || strcmp (result, "1.275000e+001 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1909,7 +2225,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%Le %d", 1234567.0L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1.234567e+06 33") == 0); + ASSERT (strcmp (result, "1.234567e+06 33") == 0 + || strcmp (result, "1.234567e+006 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -1917,91 +2234,99 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Small and large positive numbers. */ static struct { long double value; const char *string; } data[] = { - { 1.234321234321234e-37L, "1.234321e-37" }, - { 1.234321234321234e-36L, "1.234321e-36" }, - { 1.234321234321234e-35L, "1.234321e-35" }, - { 1.234321234321234e-34L, "1.234321e-34" }, - { 1.234321234321234e-33L, "1.234321e-33" }, - { 1.234321234321234e-32L, "1.234321e-32" }, - { 1.234321234321234e-31L, "1.234321e-31" }, - { 1.234321234321234e-30L, "1.234321e-30" }, - { 1.234321234321234e-29L, "1.234321e-29" }, - { 1.234321234321234e-28L, "1.234321e-28" }, - { 1.234321234321234e-27L, "1.234321e-27" }, - { 1.234321234321234e-26L, "1.234321e-26" }, - { 1.234321234321234e-25L, "1.234321e-25" }, - { 1.234321234321234e-24L, "1.234321e-24" }, - { 1.234321234321234e-23L, "1.234321e-23" }, - { 1.234321234321234e-22L, "1.234321e-22" }, - { 1.234321234321234e-21L, "1.234321e-21" }, - { 1.234321234321234e-20L, "1.234321e-20" }, - { 1.234321234321234e-19L, "1.234321e-19" }, - { 1.234321234321234e-18L, "1.234321e-18" }, - { 1.234321234321234e-17L, "1.234321e-17" }, - { 1.234321234321234e-16L, "1.234321e-16" }, - { 1.234321234321234e-15L, "1.234321e-15" }, - { 1.234321234321234e-14L, "1.234321e-14" }, - { 1.234321234321234e-13L, "1.234321e-13" }, - { 1.234321234321234e-12L, "1.234321e-12" }, - { 1.234321234321234e-11L, "1.234321e-11" }, - { 1.234321234321234e-10L, "1.234321e-10" }, - { 1.234321234321234e-9L, "1.234321e-09" }, - { 1.234321234321234e-8L, "1.234321e-08" }, - { 1.234321234321234e-7L, "1.234321e-07" }, - { 1.234321234321234e-6L, "1.234321e-06" }, - { 1.234321234321234e-5L, "1.234321e-05" }, - { 1.234321234321234e-4L, "1.234321e-04" }, - { 1.234321234321234e-3L, "1.234321e-03" }, - { 1.234321234321234e-2L, "1.234321e-02" }, - { 1.234321234321234e-1L, "1.234321e-01" }, - { 1.234321234321234L, "1.234321e+00" }, - { 1.234321234321234e1L, "1.234321e+01" }, - { 1.234321234321234e2L, "1.234321e+02" }, - { 1.234321234321234e3L, "1.234321e+03" }, - { 1.234321234321234e4L, "1.234321e+04" }, - { 1.234321234321234e5L, "1.234321e+05" }, - { 1.234321234321234e6L, "1.234321e+06" }, - { 1.234321234321234e7L, "1.234321e+07" }, - { 1.234321234321234e8L, "1.234321e+08" }, - { 1.234321234321234e9L, "1.234321e+09" }, - { 1.234321234321234e10L, "1.234321e+10" }, - { 1.234321234321234e11L, "1.234321e+11" }, - { 1.234321234321234e12L, "1.234321e+12" }, - { 1.234321234321234e13L, "1.234321e+13" }, - { 1.234321234321234e14L, "1.234321e+14" }, - { 1.234321234321234e15L, "1.234321e+15" }, - { 1.234321234321234e16L, "1.234321e+16" }, - { 1.234321234321234e17L, "1.234321e+17" }, - { 1.234321234321234e18L, "1.234321e+18" }, - { 1.234321234321234e19L, "1.234321e+19" }, - { 1.234321234321234e20L, "1.234321e+20" }, - { 1.234321234321234e21L, "1.234321e+21" }, - { 1.234321234321234e22L, "1.234321e+22" }, - { 1.234321234321234e23L, "1.234321e+23" }, - { 1.234321234321234e24L, "1.234321e+24" }, - { 1.234321234321234e25L, "1.234321e+25" }, - { 1.234321234321234e26L, "1.234321e+26" }, - { 1.234321234321234e27L, "1.234321e+27" }, - { 1.234321234321234e28L, "1.234321e+28" }, - { 1.234321234321234e29L, "1.234321e+29" }, - { 1.234321234321234e30L, "1.234321e+30" }, - { 1.234321234321234e31L, "1.234321e+31" }, - { 1.234321234321234e32L, "1.234321e+32" }, - { 1.234321234321234e33L, "1.234321e+33" }, - { 1.234321234321234e34L, "1.234321e+34" }, - { 1.234321234321234e35L, "1.234321e+35" }, - { 1.234321234321234e36L, "1.234321e+36" } + { 1.234321234321234e-37L, "1.234321e-37" }, + { 1.234321234321234e-36L, "1.234321e-36" }, + { 1.234321234321234e-35L, "1.234321e-35" }, + { 1.234321234321234e-34L, "1.234321e-34" }, + { 1.234321234321234e-33L, "1.234321e-33" }, + { 1.234321234321234e-32L, "1.234321e-32" }, + { 1.234321234321234e-31L, "1.234321e-31" }, + { 1.234321234321234e-30L, "1.234321e-30" }, + { 1.234321234321234e-29L, "1.234321e-29" }, + { 1.234321234321234e-28L, "1.234321e-28" }, + { 1.234321234321234e-27L, "1.234321e-27" }, + { 1.234321234321234e-26L, "1.234321e-26" }, + { 1.234321234321234e-25L, "1.234321e-25" }, + { 1.234321234321234e-24L, "1.234321e-24" }, + { 1.234321234321234e-23L, "1.234321e-23" }, + { 1.234321234321234e-22L, "1.234321e-22" }, + { 1.234321234321234e-21L, "1.234321e-21" }, + { 1.234321234321234e-20L, "1.234321e-20" }, + { 1.234321234321234e-19L, "1.234321e-19" }, + { 1.234321234321234e-18L, "1.234321e-18" }, + { 1.234321234321234e-17L, "1.234321e-17" }, + { 1.234321234321234e-16L, "1.234321e-16" }, + { 1.234321234321234e-15L, "1.234321e-15" }, + { 1.234321234321234e-14L, "1.234321e-14" }, + { 1.234321234321234e-13L, "1.234321e-13" }, + { 1.234321234321234e-12L, "1.234321e-12" }, + { 1.234321234321234e-11L, "1.234321e-11" }, + { 1.234321234321234e-10L, "1.234321e-10" }, + { 1.234321234321234e-9L, "1.234321e-09" }, + { 1.234321234321234e-8L, "1.234321e-08" }, + { 1.234321234321234e-7L, "1.234321e-07" }, + { 1.234321234321234e-6L, "1.234321e-06" }, + { 1.234321234321234e-5L, "1.234321e-05" }, + { 1.234321234321234e-4L, "1.234321e-04" }, + { 1.234321234321234e-3L, "1.234321e-03" }, + { 1.234321234321234e-2L, "1.234321e-02" }, + { 1.234321234321234e-1L, "1.234321e-01" }, + { 1.234321234321234L, "1.234321e+00" }, + { 1.234321234321234e1L, "1.234321e+01" }, + { 1.234321234321234e2L, "1.234321e+02" }, + { 1.234321234321234e3L, "1.234321e+03" }, + { 1.234321234321234e4L, "1.234321e+04" }, + { 1.234321234321234e5L, "1.234321e+05" }, + { 1.234321234321234e6L, "1.234321e+06" }, + { 1.234321234321234e7L, "1.234321e+07" }, + { 1.234321234321234e8L, "1.234321e+08" }, + { 1.234321234321234e9L, "1.234321e+09" }, + { 1.234321234321234e10L, "1.234321e+10" }, + { 1.234321234321234e11L, "1.234321e+11" }, + { 1.234321234321234e12L, "1.234321e+12" }, + { 1.234321234321234e13L, "1.234321e+13" }, + { 1.234321234321234e14L, "1.234321e+14" }, + { 1.234321234321234e15L, "1.234321e+15" }, + { 1.234321234321234e16L, "1.234321e+16" }, + { 1.234321234321234e17L, "1.234321e+17" }, + { 1.234321234321234e18L, "1.234321e+18" }, + { 1.234321234321234e19L, "1.234321e+19" }, + { 1.234321234321234e20L, "1.234321e+20" }, + { 1.234321234321234e21L, "1.234321e+21" }, + { 1.234321234321234e22L, "1.234321e+22" }, + { 1.234321234321234e23L, "1.234321e+23" }, + { 1.234321234321234e24L, "1.234321e+24" }, + { 1.234321234321234e25L, "1.234321e+25" }, + { 1.234321234321234e26L, "1.234321e+26" }, + { 1.234321234321234e27L, "1.234321e+27" }, + { 1.234321234321234e28L, "1.234321e+28" }, + { 1.234321234321234e29L, "1.234321e+29" }, + { 1.234321234321234e30L, "1.234321e+30" }, + { 1.234321234321234e31L, "1.234321e+31" }, + { 1.234321234321234e32L, "1.234321e+32" }, + { 1.234321234321234e33L, "1.234321e+33" }, + { 1.234321234321234e34L, "1.234321e+34" }, + { 1.234321234321234e35L, "1.234321e+35" }, + { 1.234321234321234e36L, "1.234321e+36" } }; size_t k; for (k = 0; k < SIZEOF (data); k++) { - size_t length; - char *result = - my_asnprintf (NULL, &length, "%Le", data[k].value); - ASSERT (result != NULL); - ASSERT (strcmp (result, data[k].string) == 0); - ASSERT (length == strlen (result)); - free (result); + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Le", data[k].value); + const char *expected = data[k].string; + ASSERT (result != NULL); + ASSERT (strcmp (result, expected) == 0 + /* Some implementations produce exponents with 3 digits. */ + || (strlen (result) == strlen (expected) + 1 + && memcmp (result, expected, strlen (expected) - 2) == 0 + && result[strlen (expected) - 2] == '0' + && strcmp (result + strlen (expected) - 1, + expected + strlen (expected) - 2) + == 0)); + ASSERT (length == strlen (result)); + free (result); } } @@ -2010,7 +2335,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%Le %d", -0.03125L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "-3.125000e-02 33") == 0); + ASSERT (strcmp (result, "-3.125000e-02 33") == 0 + || strcmp (result, "-3.125000e-002 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2020,7 +2346,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%Le %d", 0.0L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "0.000000e+00 33") == 0); + ASSERT (strcmp (result, "0.000000e+00 33") == 0 + || strcmp (result, "0.000000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2028,10 +2355,11 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%Le %d", -0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%Le %d", minus_zerol, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) - ASSERT (strcmp (result, "-0.000000e+00 33") == 0); + ASSERT (strcmp (result, "-0.000000e+00 33") == 0 + || strcmp (result, "-0.000000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2039,10 +2367,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Positive infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%Le %d", 1.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%Le %d", Infinityl (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "inf 33") == 0 - || strcmp (result, "infinity 33") == 0); + || strcmp (result, "infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2050,33 +2378,134 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%Le %d", -1.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%Le %d", - Infinityl (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "-inf 33") == 0 - || strcmp (result, "-infinity 33") == 0); + || strcmp (result, "-infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } { /* NaN. */ - static long double zero = 0.0L; size_t length; char *result = - my_asnprintf (NULL, &length, "%Le %d", zero / zero, 33, 44, 55); + my_asnprintf (NULL, &length, "%Le %d", NaNl (), 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } +#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 + { /* Quiet NaN. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Le %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { + /* Signalling NaN. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Le %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + /* The isnanl function should recognize Pseudo-NaNs, Pseudo-Infinities, + Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in + Intel IA-64 Architecture Software Developer's Manual, Volume 1: + Application Architecture. + Table 5-2 "Floating-Point Register Encodings" + Figure 5-6 "Memory to Floating-Point Register Data Translation" + */ + { /* Pseudo-NaN. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Le %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Pseudo-Infinity. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Le %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Pseudo-Zero. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Le %d", x.value, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && memcmp (result, "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } + { /* Unnormalized number. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Le %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Pseudo-Denormal. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Le %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } +#endif { /* Width. */ size_t length; char *result = my_asnprintf (NULL, &length, "%15Le %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, " 1.750000e+00 33") == 0); + ASSERT (strcmp (result, " 1.750000e+00 33") == 0 + || strcmp (result, " 1.750000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2086,7 +2515,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%-15Le %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1.750000e+00 33") == 0); + ASSERT (strcmp (result, "1.750000e+00 33") == 0 + || strcmp (result, "1.750000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2096,7 +2526,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%+Le %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "+1.750000e+00 33") == 0); + ASSERT (strcmp (result, "+1.750000e+00 33") == 0 + || strcmp (result, "+1.750000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2106,7 +2537,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "% Le %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, " 1.750000e+00 33") == 0); + ASSERT (strcmp (result, " 1.750000e+00 33") == 0 + || strcmp (result, " 1.750000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2116,7 +2548,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%#Le %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1.750000e+00 33") == 0); + ASSERT (strcmp (result, "1.750000e+00 33") == 0 + || strcmp (result, "1.750000e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2126,7 +2559,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%#.Le %d", 1.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "2.e+00 33") == 0); + ASSERT (strcmp (result, "2.e+00 33") == 0 + || strcmp (result, "2.e+000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2136,7 +2570,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%#.Le %d", 9.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1.e+01 33") == 0); + ASSERT (strcmp (result, "1.e+01 33") == 0 + || strcmp (result, "1.e+001 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2146,7 +2581,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%015Le %d", 1234.0L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "0001.234000e+03 33") == 0); + ASSERT (strcmp (result, "0001.234000e+03 33") == 0 + || strcmp (result, "001.234000e+003 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2154,23 +2590,22 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* FLAG_ZERO with infinite number. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%015Le %d", -1.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%015Le %d", - Infinityl (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, " -inf 33") == 0 - || strcmp (result, " -infinity 33") == 0); + || strcmp (result, " -infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } { /* FLAG_ZERO with NaN. */ - static long double zero = 0.0L; size_t length; char *result = - my_asnprintf (NULL, &length, "%020Le %d", zero / zero, 33, 44, 55); + my_asnprintf (NULL, &length, "%050Le %d", NaNl (), 33, 44, 55); ASSERT (result != NULL); - ASSERT (strlen (result) == 20 + 3 - && memcmp (result + strspn (result, " "), "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (strlen (result) == 50 + 3 + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2180,7 +2615,30 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%.Le %d", 1234.0L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1e+03 33") == 0); + ASSERT (strcmp (result, "1e+03 33") == 0 + || strcmp (result, "1e+003 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* Precision with no rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.4Le %d", 999.951L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "9.9995e+02 33") == 0 + || strcmp (result, "9.9995e+002 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.4Le %d", 999.996L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1.0000e+03 33") == 0 + || strcmp (result, "1.0000e+003 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2203,7 +2661,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%g %d", 1234567.0, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "1.23457e+06 33") == 0 - || strcmp (result, "1.23457e+006 33") == 0); + || strcmp (result, "1.23457e+006 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2211,100 +2669,100 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Small and large positive numbers. */ static struct { double value; const char *string; } data[] = { - { 1.234321234321234e-37, "1.23432e-37" }, - { 1.234321234321234e-36, "1.23432e-36" }, - { 1.234321234321234e-35, "1.23432e-35" }, - { 1.234321234321234e-34, "1.23432e-34" }, - { 1.234321234321234e-33, "1.23432e-33" }, - { 1.234321234321234e-32, "1.23432e-32" }, - { 1.234321234321234e-31, "1.23432e-31" }, - { 1.234321234321234e-30, "1.23432e-30" }, - { 1.234321234321234e-29, "1.23432e-29" }, - { 1.234321234321234e-28, "1.23432e-28" }, - { 1.234321234321234e-27, "1.23432e-27" }, - { 1.234321234321234e-26, "1.23432e-26" }, - { 1.234321234321234e-25, "1.23432e-25" }, - { 1.234321234321234e-24, "1.23432e-24" }, - { 1.234321234321234e-23, "1.23432e-23" }, - { 1.234321234321234e-22, "1.23432e-22" }, - { 1.234321234321234e-21, "1.23432e-21" }, - { 1.234321234321234e-20, "1.23432e-20" }, - { 1.234321234321234e-19, "1.23432e-19" }, - { 1.234321234321234e-18, "1.23432e-18" }, - { 1.234321234321234e-17, "1.23432e-17" }, - { 1.234321234321234e-16, "1.23432e-16" }, - { 1.234321234321234e-15, "1.23432e-15" }, - { 1.234321234321234e-14, "1.23432e-14" }, - { 1.234321234321234e-13, "1.23432e-13" }, - { 1.234321234321234e-12, "1.23432e-12" }, - { 1.234321234321234e-11, "1.23432e-11" }, - { 1.234321234321234e-10, "1.23432e-10" }, - { 1.234321234321234e-9, "1.23432e-09" }, - { 1.234321234321234e-8, "1.23432e-08" }, - { 1.234321234321234e-7, "1.23432e-07" }, - { 1.234321234321234e-6, "1.23432e-06" }, - { 1.234321234321234e-5, "1.23432e-05" }, - { 1.234321234321234e-4, "0.000123432" }, - { 1.234321234321234e-3, "0.00123432" }, - { 1.234321234321234e-2, "0.0123432" }, - { 1.234321234321234e-1, "0.123432" }, - { 1.234321234321234, "1.23432" }, - { 1.234321234321234e1, "12.3432" }, - { 1.234321234321234e2, "123.432" }, - { 1.234321234321234e3, "1234.32" }, - { 1.234321234321234e4, "12343.2" }, - { 1.234321234321234e5, "123432" }, - { 1.234321234321234e6, "1.23432e+06" }, - { 1.234321234321234e7, "1.23432e+07" }, - { 1.234321234321234e8, "1.23432e+08" }, - { 1.234321234321234e9, "1.23432e+09" }, - { 1.234321234321234e10, "1.23432e+10" }, - { 1.234321234321234e11, "1.23432e+11" }, - { 1.234321234321234e12, "1.23432e+12" }, - { 1.234321234321234e13, "1.23432e+13" }, - { 1.234321234321234e14, "1.23432e+14" }, - { 1.234321234321234e15, "1.23432e+15" }, - { 1.234321234321234e16, "1.23432e+16" }, - { 1.234321234321234e17, "1.23432e+17" }, - { 1.234321234321234e18, "1.23432e+18" }, - { 1.234321234321234e19, "1.23432e+19" }, - { 1.234321234321234e20, "1.23432e+20" }, - { 1.234321234321234e21, "1.23432e+21" }, - { 1.234321234321234e22, "1.23432e+22" }, - { 1.234321234321234e23, "1.23432e+23" }, - { 1.234321234321234e24, "1.23432e+24" }, - { 1.234321234321234e25, "1.23432e+25" }, - { 1.234321234321234e26, "1.23432e+26" }, - { 1.234321234321234e27, "1.23432e+27" }, - { 1.234321234321234e28, "1.23432e+28" }, - { 1.234321234321234e29, "1.23432e+29" }, - { 1.234321234321234e30, "1.23432e+30" }, - { 1.234321234321234e31, "1.23432e+31" }, - { 1.234321234321234e32, "1.23432e+32" }, - { 1.234321234321234e33, "1.23432e+33" }, - { 1.234321234321234e34, "1.23432e+34" }, - { 1.234321234321234e35, "1.23432e+35" }, - { 1.234321234321234e36, "1.23432e+36" } + { 1.234321234321234e-37, "1.23432e-37" }, + { 1.234321234321234e-36, "1.23432e-36" }, + { 1.234321234321234e-35, "1.23432e-35" }, + { 1.234321234321234e-34, "1.23432e-34" }, + { 1.234321234321234e-33, "1.23432e-33" }, + { 1.234321234321234e-32, "1.23432e-32" }, + { 1.234321234321234e-31, "1.23432e-31" }, + { 1.234321234321234e-30, "1.23432e-30" }, + { 1.234321234321234e-29, "1.23432e-29" }, + { 1.234321234321234e-28, "1.23432e-28" }, + { 1.234321234321234e-27, "1.23432e-27" }, + { 1.234321234321234e-26, "1.23432e-26" }, + { 1.234321234321234e-25, "1.23432e-25" }, + { 1.234321234321234e-24, "1.23432e-24" }, + { 1.234321234321234e-23, "1.23432e-23" }, + { 1.234321234321234e-22, "1.23432e-22" }, + { 1.234321234321234e-21, "1.23432e-21" }, + { 1.234321234321234e-20, "1.23432e-20" }, + { 1.234321234321234e-19, "1.23432e-19" }, + { 1.234321234321234e-18, "1.23432e-18" }, + { 1.234321234321234e-17, "1.23432e-17" }, + { 1.234321234321234e-16, "1.23432e-16" }, + { 1.234321234321234e-15, "1.23432e-15" }, + { 1.234321234321234e-14, "1.23432e-14" }, + { 1.234321234321234e-13, "1.23432e-13" }, + { 1.234321234321234e-12, "1.23432e-12" }, + { 1.234321234321234e-11, "1.23432e-11" }, + { 1.234321234321234e-10, "1.23432e-10" }, + { 1.234321234321234e-9, "1.23432e-09" }, + { 1.234321234321234e-8, "1.23432e-08" }, + { 1.234321234321234e-7, "1.23432e-07" }, + { 1.234321234321234e-6, "1.23432e-06" }, + { 1.234321234321234e-5, "1.23432e-05" }, + { 1.234321234321234e-4, "0.000123432" }, + { 1.234321234321234e-3, "0.00123432" }, + { 1.234321234321234e-2, "0.0123432" }, + { 1.234321234321234e-1, "0.123432" }, + { 1.234321234321234, "1.23432" }, + { 1.234321234321234e1, "12.3432" }, + { 1.234321234321234e2, "123.432" }, + { 1.234321234321234e3, "1234.32" }, + { 1.234321234321234e4, "12343.2" }, + { 1.234321234321234e5, "123432" }, + { 1.234321234321234e6, "1.23432e+06" }, + { 1.234321234321234e7, "1.23432e+07" }, + { 1.234321234321234e8, "1.23432e+08" }, + { 1.234321234321234e9, "1.23432e+09" }, + { 1.234321234321234e10, "1.23432e+10" }, + { 1.234321234321234e11, "1.23432e+11" }, + { 1.234321234321234e12, "1.23432e+12" }, + { 1.234321234321234e13, "1.23432e+13" }, + { 1.234321234321234e14, "1.23432e+14" }, + { 1.234321234321234e15, "1.23432e+15" }, + { 1.234321234321234e16, "1.23432e+16" }, + { 1.234321234321234e17, "1.23432e+17" }, + { 1.234321234321234e18, "1.23432e+18" }, + { 1.234321234321234e19, "1.23432e+19" }, + { 1.234321234321234e20, "1.23432e+20" }, + { 1.234321234321234e21, "1.23432e+21" }, + { 1.234321234321234e22, "1.23432e+22" }, + { 1.234321234321234e23, "1.23432e+23" }, + { 1.234321234321234e24, "1.23432e+24" }, + { 1.234321234321234e25, "1.23432e+25" }, + { 1.234321234321234e26, "1.23432e+26" }, + { 1.234321234321234e27, "1.23432e+27" }, + { 1.234321234321234e28, "1.23432e+28" }, + { 1.234321234321234e29, "1.23432e+29" }, + { 1.234321234321234e30, "1.23432e+30" }, + { 1.234321234321234e31, "1.23432e+31" }, + { 1.234321234321234e32, "1.23432e+32" }, + { 1.234321234321234e33, "1.23432e+33" }, + { 1.234321234321234e34, "1.23432e+34" }, + { 1.234321234321234e35, "1.23432e+35" }, + { 1.234321234321234e36, "1.23432e+36" } }; size_t k; for (k = 0; k < SIZEOF (data); k++) { - size_t length; - char *result = - my_asnprintf (NULL, &length, "%g", data[k].value); - const char *expected = data[k].string; - ASSERT (result != NULL); - ASSERT (strcmp (result, expected) == 0 - /* Some implementations produce exponents with 3 digits. */ - || (expected[strlen (expected) - 4] == 'e' - && strlen (result) == strlen (expected) + 1 - && memcmp (result, expected, strlen (expected) - 2) == 0 - && result[strlen (expected) - 2] == '0' - && strcmp (result + strlen (expected) - 1, - expected + strlen (expected) - 2) - == 0)); - ASSERT (length == strlen (result)); - free (result); + size_t length; + char *result = + my_asnprintf (NULL, &length, "%g", data[k].value); + const char *expected = data[k].string; + ASSERT (result != NULL); + ASSERT (strcmp (result, expected) == 0 + /* Some implementations produce exponents with 3 digits. */ + || (expected[strlen (expected) - 4] == 'e' + && strlen (result) == strlen (expected) + 1 + && memcmp (result, expected, strlen (expected) - 2) == 0 + && result[strlen (expected) - 2] == '0' + && strcmp (result + strlen (expected) - 1, + expected + strlen (expected) - 2) + == 0)); + ASSERT (length == strlen (result)); + free (result); } } @@ -2331,7 +2789,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%g %d", -0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%g %d", minus_zerod, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0 33") == 0); @@ -2342,10 +2800,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Positive infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%g %d", 1.0 / 0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%g %d", Infinityd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "inf 33") == 0 - || strcmp (result, "infinity 33") == 0); + || strcmp (result, "infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2353,10 +2811,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%g %d", -1.0 / 0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%g %d", - Infinityd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "-inf 33") == 0 - || strcmp (result, "-infinity 33") == 0); + || strcmp (result, "-infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2364,11 +2822,11 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* NaN. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%g %d", NaN (), 33, 44, 55); + my_asnprintf (NULL, &length, "%g %d", NaNd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && memcmp (result, "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2439,7 +2897,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%#.g %d", 9.75, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "1.e+01 33") == 0 - || strcmp (result, "1.e+001 33") == 0); + || strcmp (result, "1.e+001 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2457,10 +2915,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* FLAG_ZERO with infinite number. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%015g %d", -1.0 / 0.0, 33, 44, 55); + my_asnprintf (NULL, &length, "%015g %d", - Infinityd (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, " -inf 33") == 0 - || strcmp (result, " -infinity 33") == 0); + || strcmp (result, " -infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2468,11 +2926,11 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* FLAG_ZERO with NaN. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%020g %d", NaN (), 33, 44, 55); + my_asnprintf (NULL, &length, "%050g %d", NaNd (), 33, 44, 55); ASSERT (result != NULL); - ASSERT (strlen (result) == 20 + 3 - && memcmp (result + strspn (result, " "), "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (strlen (result) == 50 + 3 + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2483,7 +2941,27 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) my_asnprintf (NULL, &length, "%.g %d", 1234.0, 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "1e+03 33") == 0 - || strcmp (result, "1e+003 33") == 0); + || strcmp (result, "1e+003 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* Precision with no rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.5g %d", 999.951, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.5g %d", 999.996, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2503,7 +2981,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%Lg %d", 1234567.0L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1.23457e+06 33") == 0); + ASSERT (strcmp (result, "1.23457e+06 33") == 0 + || strcmp (result, "1.23457e+006 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2511,91 +2990,100 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Small and large positive numbers. */ static struct { long double value; const char *string; } data[] = { - { 1.234321234321234e-37L, "1.23432e-37" }, - { 1.234321234321234e-36L, "1.23432e-36" }, - { 1.234321234321234e-35L, "1.23432e-35" }, - { 1.234321234321234e-34L, "1.23432e-34" }, - { 1.234321234321234e-33L, "1.23432e-33" }, - { 1.234321234321234e-32L, "1.23432e-32" }, - { 1.234321234321234e-31L, "1.23432e-31" }, - { 1.234321234321234e-30L, "1.23432e-30" }, - { 1.234321234321234e-29L, "1.23432e-29" }, - { 1.234321234321234e-28L, "1.23432e-28" }, - { 1.234321234321234e-27L, "1.23432e-27" }, - { 1.234321234321234e-26L, "1.23432e-26" }, - { 1.234321234321234e-25L, "1.23432e-25" }, - { 1.234321234321234e-24L, "1.23432e-24" }, - { 1.234321234321234e-23L, "1.23432e-23" }, - { 1.234321234321234e-22L, "1.23432e-22" }, - { 1.234321234321234e-21L, "1.23432e-21" }, - { 1.234321234321234e-20L, "1.23432e-20" }, - { 1.234321234321234e-19L, "1.23432e-19" }, - { 1.234321234321234e-18L, "1.23432e-18" }, - { 1.234321234321234e-17L, "1.23432e-17" }, - { 1.234321234321234e-16L, "1.23432e-16" }, - { 1.234321234321234e-15L, "1.23432e-15" }, - { 1.234321234321234e-14L, "1.23432e-14" }, - { 1.234321234321234e-13L, "1.23432e-13" }, - { 1.234321234321234e-12L, "1.23432e-12" }, - { 1.234321234321234e-11L, "1.23432e-11" }, - { 1.234321234321234e-10L, "1.23432e-10" }, - { 1.234321234321234e-9L, "1.23432e-09" }, - { 1.234321234321234e-8L, "1.23432e-08" }, - { 1.234321234321234e-7L, "1.23432e-07" }, - { 1.234321234321234e-6L, "1.23432e-06" }, - { 1.234321234321234e-5L, "1.23432e-05" }, - { 1.234321234321234e-4L, "0.000123432" }, - { 1.234321234321234e-3L, "0.00123432" }, - { 1.234321234321234e-2L, "0.0123432" }, - { 1.234321234321234e-1L, "0.123432" }, - { 1.234321234321234L, "1.23432" }, - { 1.234321234321234e1L, "12.3432" }, - { 1.234321234321234e2L, "123.432" }, - { 1.234321234321234e3L, "1234.32" }, - { 1.234321234321234e4L, "12343.2" }, - { 1.234321234321234e5L, "123432" }, - { 1.234321234321234e6L, "1.23432e+06" }, - { 1.234321234321234e7L, "1.23432e+07" }, - { 1.234321234321234e8L, "1.23432e+08" }, - { 1.234321234321234e9L, "1.23432e+09" }, - { 1.234321234321234e10L, "1.23432e+10" }, - { 1.234321234321234e11L, "1.23432e+11" }, - { 1.234321234321234e12L, "1.23432e+12" }, - { 1.234321234321234e13L, "1.23432e+13" }, - { 1.234321234321234e14L, "1.23432e+14" }, - { 1.234321234321234e15L, "1.23432e+15" }, - { 1.234321234321234e16L, "1.23432e+16" }, - { 1.234321234321234e17L, "1.23432e+17" }, - { 1.234321234321234e18L, "1.23432e+18" }, - { 1.234321234321234e19L, "1.23432e+19" }, - { 1.234321234321234e20L, "1.23432e+20" }, - { 1.234321234321234e21L, "1.23432e+21" }, - { 1.234321234321234e22L, "1.23432e+22" }, - { 1.234321234321234e23L, "1.23432e+23" }, - { 1.234321234321234e24L, "1.23432e+24" }, - { 1.234321234321234e25L, "1.23432e+25" }, - { 1.234321234321234e26L, "1.23432e+26" }, - { 1.234321234321234e27L, "1.23432e+27" }, - { 1.234321234321234e28L, "1.23432e+28" }, - { 1.234321234321234e29L, "1.23432e+29" }, - { 1.234321234321234e30L, "1.23432e+30" }, - { 1.234321234321234e31L, "1.23432e+31" }, - { 1.234321234321234e32L, "1.23432e+32" }, - { 1.234321234321234e33L, "1.23432e+33" }, - { 1.234321234321234e34L, "1.23432e+34" }, - { 1.234321234321234e35L, "1.23432e+35" }, - { 1.234321234321234e36L, "1.23432e+36" } + { 1.234321234321234e-37L, "1.23432e-37" }, + { 1.234321234321234e-36L, "1.23432e-36" }, + { 1.234321234321234e-35L, "1.23432e-35" }, + { 1.234321234321234e-34L, "1.23432e-34" }, + { 1.234321234321234e-33L, "1.23432e-33" }, + { 1.234321234321234e-32L, "1.23432e-32" }, + { 1.234321234321234e-31L, "1.23432e-31" }, + { 1.234321234321234e-30L, "1.23432e-30" }, + { 1.234321234321234e-29L, "1.23432e-29" }, + { 1.234321234321234e-28L, "1.23432e-28" }, + { 1.234321234321234e-27L, "1.23432e-27" }, + { 1.234321234321234e-26L, "1.23432e-26" }, + { 1.234321234321234e-25L, "1.23432e-25" }, + { 1.234321234321234e-24L, "1.23432e-24" }, + { 1.234321234321234e-23L, "1.23432e-23" }, + { 1.234321234321234e-22L, "1.23432e-22" }, + { 1.234321234321234e-21L, "1.23432e-21" }, + { 1.234321234321234e-20L, "1.23432e-20" }, + { 1.234321234321234e-19L, "1.23432e-19" }, + { 1.234321234321234e-18L, "1.23432e-18" }, + { 1.234321234321234e-17L, "1.23432e-17" }, + { 1.234321234321234e-16L, "1.23432e-16" }, + { 1.234321234321234e-15L, "1.23432e-15" }, + { 1.234321234321234e-14L, "1.23432e-14" }, + { 1.234321234321234e-13L, "1.23432e-13" }, + { 1.234321234321234e-12L, "1.23432e-12" }, + { 1.234321234321234e-11L, "1.23432e-11" }, + { 1.234321234321234e-10L, "1.23432e-10" }, + { 1.234321234321234e-9L, "1.23432e-09" }, + { 1.234321234321234e-8L, "1.23432e-08" }, + { 1.234321234321234e-7L, "1.23432e-07" }, + { 1.234321234321234e-6L, "1.23432e-06" }, + { 1.234321234321234e-5L, "1.23432e-05" }, + { 1.234321234321234e-4L, "0.000123432" }, + { 1.234321234321234e-3L, "0.00123432" }, + { 1.234321234321234e-2L, "0.0123432" }, + { 1.234321234321234e-1L, "0.123432" }, + { 1.234321234321234L, "1.23432" }, + { 1.234321234321234e1L, "12.3432" }, + { 1.234321234321234e2L, "123.432" }, + { 1.234321234321234e3L, "1234.32" }, + { 1.234321234321234e4L, "12343.2" }, + { 1.234321234321234e5L, "123432" }, + { 1.234321234321234e6L, "1.23432e+06" }, + { 1.234321234321234e7L, "1.23432e+07" }, + { 1.234321234321234e8L, "1.23432e+08" }, + { 1.234321234321234e9L, "1.23432e+09" }, + { 1.234321234321234e10L, "1.23432e+10" }, + { 1.234321234321234e11L, "1.23432e+11" }, + { 1.234321234321234e12L, "1.23432e+12" }, + { 1.234321234321234e13L, "1.23432e+13" }, + { 1.234321234321234e14L, "1.23432e+14" }, + { 1.234321234321234e15L, "1.23432e+15" }, + { 1.234321234321234e16L, "1.23432e+16" }, + { 1.234321234321234e17L, "1.23432e+17" }, + { 1.234321234321234e18L, "1.23432e+18" }, + { 1.234321234321234e19L, "1.23432e+19" }, + { 1.234321234321234e20L, "1.23432e+20" }, + { 1.234321234321234e21L, "1.23432e+21" }, + { 1.234321234321234e22L, "1.23432e+22" }, + { 1.234321234321234e23L, "1.23432e+23" }, + { 1.234321234321234e24L, "1.23432e+24" }, + { 1.234321234321234e25L, "1.23432e+25" }, + { 1.234321234321234e26L, "1.23432e+26" }, + { 1.234321234321234e27L, "1.23432e+27" }, + { 1.234321234321234e28L, "1.23432e+28" }, + { 1.234321234321234e29L, "1.23432e+29" }, + { 1.234321234321234e30L, "1.23432e+30" }, + { 1.234321234321234e31L, "1.23432e+31" }, + { 1.234321234321234e32L, "1.23432e+32" }, + { 1.234321234321234e33L, "1.23432e+33" }, + { 1.234321234321234e34L, "1.23432e+34" }, + { 1.234321234321234e35L, "1.23432e+35" }, + { 1.234321234321234e36L, "1.23432e+36" } }; size_t k; for (k = 0; k < SIZEOF (data); k++) { - size_t length; - char *result = - my_asnprintf (NULL, &length, "%Lg", data[k].value); - ASSERT (result != NULL); - ASSERT (strcmp (result, data[k].string) == 0); - ASSERT (length == strlen (result)); - free (result); + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lg", data[k].value); + const char *expected = data[k].string; + ASSERT (result != NULL); + ASSERT (strcmp (result, expected) == 0 + /* Some implementations produce exponents with 3 digits. */ + || (expected[strlen (expected) - 4] == 'e' + && strlen (result) == strlen (expected) + 1 + && memcmp (result, expected, strlen (expected) - 2) == 0 + && result[strlen (expected) - 2] == '0' + && strcmp (result + strlen (expected) - 1, + expected + strlen (expected) - 2) + == 0)); + ASSERT (length == strlen (result)); + free (result); } } @@ -2622,7 +3110,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative zero. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%Lg %d", -0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%Lg %d", minus_zerol, 33, 44, 55); ASSERT (result != NULL); if (have_minus_zero ()) ASSERT (strcmp (result, "-0 33") == 0); @@ -2633,10 +3121,10 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Positive infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%Lg %d", 1.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%Lg %d", Infinityl (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "inf 33") == 0 - || strcmp (result, "infinity 33") == 0); + || strcmp (result, "infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2644,26 +3132,126 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* Negative infinity. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%Lg %d", -1.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%Lg %d", - Infinityl (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, "-inf 33") == 0 - || strcmp (result, "-infinity 33") == 0); + || strcmp (result, "-infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } { /* NaN. */ - static long double zero = 0.0L; size_t length; char *result = - my_asnprintf (NULL, &length, "%Lg %d", zero / zero, 33, 44, 55); + my_asnprintf (NULL, &length, "%Lg %d", NaNl (), 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } +#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 + { /* Quiet NaN. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0xC3333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lg %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { + /* Signalling NaN. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0x83333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lg %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + /* The isnanl function should recognize Pseudo-NaNs, Pseudo-Infinities, + Pseudo-Zeroes, Unnormalized Numbers, and Pseudo-Denormals, as defined in + Intel IA-64 Architecture Software Developer's Manual, Volume 1: + Application Architecture. + Table 5-2 "Floating-Point Register Encodings" + Figure 5-6 "Memory to Floating-Point Register Data Translation" + */ + { /* Pseudo-NaN. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0x40000001, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lg %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Pseudo-Infinity. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0xFFFF, 0x00000000, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lg %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Pseudo-Zero. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0x4004, 0x00000000, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lg %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + { /* Unnormalized number. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0x4000, 0x63333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lg %d", x.value, 33, 44, 55); ASSERT (result != NULL); ASSERT (strlen (result) >= 3 + 3 - && memcmp (result, "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } + { /* Pseudo-Denormal. */ + static union { unsigned int word[4]; long double value; } x = + { LDBL80_WORDS (0x0000, 0x83333333, 0x00000000) }; + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Lg %d", x.value, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strlen (result) >= 3 + 3 + && strisnan (result, 0, strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } +#endif { /* Width. */ size_t length; @@ -2730,7 +3318,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%#.Lg %d", 9.75L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1.e+01 33") == 0); + ASSERT (strcmp (result, "1.e+01 33") == 0 + || strcmp (result, "1.e+001 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2748,23 +3337,22 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { /* FLAG_ZERO with infinite number. */ size_t length; char *result = - my_asnprintf (NULL, &length, "%015Lg %d", -1.0L / 0.0L, 33, 44, 55); + my_asnprintf (NULL, &length, "%015Lg %d", - Infinityl (), 33, 44, 55); ASSERT (result != NULL); ASSERT (strcmp (result, " -inf 33") == 0 - || strcmp (result, " -infinity 33") == 0); + || strcmp (result, " -infinity 33") == 0); ASSERT (length == strlen (result)); free (result); } { /* FLAG_ZERO with NaN. */ - static long double zero = 0.0L; size_t length; char *result = - my_asnprintf (NULL, &length, "%020Lg %d", zero / zero, 33, 44, 55); + my_asnprintf (NULL, &length, "%050Lg %d", NaNl (), 33, 44, 55); ASSERT (result != NULL); - ASSERT (strlen (result) == 20 + 3 - && memcmp (result + strspn (result, " "), "nan", 3) == 0 - && strcmp (result + strlen (result) - 3, " 33") == 0); + ASSERT (strlen (result) == 50 + 3 + && strisnan (result, strspn (result, " "), strlen (result) - 3, 0) + && strcmp (result + strlen (result) - 3, " 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2774,7 +3362,28 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) char *result = my_asnprintf (NULL, &length, "%.Lg %d", 1234.0L, 33, 44, 55); ASSERT (result != NULL); - ASSERT (strcmp (result, "1e+03 33") == 0); + ASSERT (strcmp (result, "1e+03 33") == 0 + || strcmp (result, "1e+003 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* Precision with no rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.5Lg %d", 999.951L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "999.95 33") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { /* Precision with rounding. */ + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.5Lg %d", 999.996L, 33, 44, 55); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1000 33") == 0); ASSERT (length == strlen (result)); free (result); } @@ -2816,6 +3425,284 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) ASSERT (length == strlen (result)); free (result); } + + /* Test the support of the left-adjust flag. */ + + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "a%*sc", -3, "b"); + ASSERT (result != NULL); + ASSERT (strcmp (result, "ab c") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "a%-*sc", 3, "b"); + ASSERT (result != NULL); + ASSERT (strcmp (result, "ab c") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "a%-*sc", -3, "b"); + ASSERT (result != NULL); + ASSERT (strcmp (result, "ab c") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + /* Test the support of large precision. */ + + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.4000d %d", 1234567, 99); + size_t i; + ASSERT (result != NULL); + for (i = 0; i < 4000 - 7; i++) + ASSERT (result[i] == '0'); + ASSERT (strcmp (result + 4000 - 7, "1234567 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.*d %d", 4000, 1234567, 99); + size_t i; + ASSERT (result != NULL); + for (i = 0; i < 4000 - 7; i++) + ASSERT (result[i] == '0'); + ASSERT (strcmp (result + 4000 - 7, "1234567 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.4000d %d", -1234567, 99); + size_t i; + ASSERT (result != NULL); + ASSERT (result[0] == '-'); + for (i = 0; i < 4000 - 7; i++) + ASSERT (result[1 + i] == '0'); + ASSERT (strcmp (result + 1 + 4000 - 7, "1234567 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.4000u %d", 1234567, 99); + size_t i; + ASSERT (result != NULL); + for (i = 0; i < 4000 - 7; i++) + ASSERT (result[i] == '0'); + ASSERT (strcmp (result + 4000 - 7, "1234567 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.4000o %d", 1234567, 99); + size_t i; + ASSERT (result != NULL); + for (i = 0; i < 4000 - 7; i++) + ASSERT (result[i] == '0'); + ASSERT (strcmp (result + 4000 - 7, "4553207 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.4000x %d", 1234567, 99); + size_t i; + ASSERT (result != NULL); + for (i = 0; i < 4000 - 6; i++) + ASSERT (result[i] == '0'); + ASSERT (strcmp (result + 4000 - 6, "12d687 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%#.4000x %d", 1234567, 99); + size_t i; + ASSERT (result != NULL); + ASSERT (result[0] == '0'); + ASSERT (result[1] == 'x'); + for (i = 0; i < 4000 - 6; i++) + ASSERT (result[2 + i] == '0'); + ASSERT (strcmp (result + 2 + 4000 - 6, "12d687 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.4000f %d", 1.0, 99); + size_t i; + ASSERT (result != NULL); + ASSERT (result[0] == '1'); + ASSERT (result[1] == '.'); + for (i = 0; i < 4000; i++) + ASSERT (result[2 + i] == '0'); + ASSERT (strcmp (result + 2 + 4000, " 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%.511f %d", 1.0, 99); + size_t i; + ASSERT (result != NULL); + ASSERT (result[0] == '1'); + ASSERT (result[1] == '.'); + for (i = 0; i < 511; i++) + ASSERT (result[2 + i] == '0'); + ASSERT (strcmp (result + 2 + 511, " 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { + char input[5000]; + size_t length; + char *result; + size_t i; + + for (i = 0; i < sizeof (input) - 1; i++) + input[i] = 'a' + ((1000000 / (i + 1)) % 26); + input[i] = '\0'; + result = my_asnprintf (NULL, &length, "%.4000s %d", input, 99); + ASSERT (result != NULL); + ASSERT (memcmp (result, input, 4000) == 0); + ASSERT (strcmp (result + 4000, " 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + /* Test the support of the %s format directive. */ + + /* To verify that these tests succeed, it is necessary to run them under + a tool that checks against invalid memory accesses, such as ElectricFence + or "valgrind --tool=memcheck". */ + { + size_t i; + + for (i = 1; i <= 8; i++) + { + char *block; + size_t length; + char *result; + + block = (char *) malloc (i); + memcpy (block, "abcdefgh", i); + result = my_asnprintf (NULL, &length, "%.*s", (int) i, block); + ASSERT (result != NULL); + ASSERT (memcmp (result, block, i) == 0); + ASSERT (result[i] == '\0'); + ASSERT (length == strlen (result)); + free (result); + free (block); + } + } +#if HAVE_WCHAR_T + { + size_t i; + + for (i = 1; i <= 8; i++) + { + wchar_t *block; + size_t j; + size_t length; + char *result; + + block = (wchar_t *) malloc (i * sizeof (wchar_t)); + for (j = 0; j < i; j++) + block[j] = "abcdefgh"[j]; + result = my_asnprintf (NULL, &length, "%.*ls", (int) i, block); + ASSERT (result != NULL); + ASSERT (memcmp (result, "abcdefgh", i) == 0); + ASSERT (result[i] == '\0'); + ASSERT (length == strlen (result)); + free (result); + free (block); + } + } +#endif + +#if HAVE_WCHAR_T + /* Test that converting an invalid wchar_t[] to char[] fails with EILSEQ. */ + { + static const wchar_t input[] = { (wchar_t) 1702057263, 114, 0 }; + size_t length; + char *result = my_asnprintf (NULL, &length, "%ls %d", input, 99); + if (result == NULL) + ASSERT (errno == EILSEQ); + else + free (result); + } + { + static const wchar_t input[] = { (wchar_t) 1702057263, 114, 0 }; + size_t length; + char *result = my_asnprintf (NULL, &length, "%3ls %d", input, 99); + if (result == NULL) + ASSERT (errno == EILSEQ); + else + free (result); + } + { + static const wchar_t input[] = { (wchar_t) 1702057263, 114, 0 }; + size_t length; + char *result = my_asnprintf (NULL, &length, "%.1ls %d", input, 99); + if (result == NULL) + ASSERT (errno == EILSEQ); + else + free (result); + } + { + static const wchar_t input[] = { (wchar_t) 1702057263, 114, 0 }; + size_t length; + char *result = my_asnprintf (NULL, &length, "%3.1ls %d", input, 99); + if (result == NULL) + ASSERT (errno == EILSEQ); + else + free (result); + } +#endif + +#if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)) && !defined __UCLIBC__ + /* Test that the 'I' flag is supported. */ + { + size_t length; + char *result = + my_asnprintf (NULL, &length, "%Id %d", 1234567, 99); + ASSERT (result != NULL); + ASSERT (strcmp (result, "1234567 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } +#endif } static char *