Comment.
[gnulib.git] / tests / test-vasnprintf-posix.c
index 1348034..aa537fb 100644 (file)
@@ -1,10 +1,10 @@
 /* Test of POSIX compatible vasnprintf() and asnprintf() functions.
-   Copyright (C) 2007 Free Software Foundation, Inc.
+   Copyright (C) 2007-2008 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
    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 <http://www.gnu.org/licenses/>.  */
 
 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
+#include <config.h>
 
 #include "vasnprintf.h"
 
+#include <float.h>
 #include <stdarg.h>
 #include <stddef.h>
 #include <stdio.h>
@@ -30,6 +28,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "nan.h"
+
 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
 #define ASSERT(expr) \
   do                                                                        \
       if (!(expr))                                                          \
         {                                                                   \
           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
+          fflush (stderr);                                                  \
           abort ();                                                         \
         }                                                                   \
     }                                                                       \
   while (0)
 
-/* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0.  */
-#ifdef __DECC
-static double
-NaN ()
+/* The SGI MIPS floating-point format does not distinguish 0.0 and -0.0.  */
+static int
+have_minus_zero ()
 {
-  static double zero = 0.0;
-  return zero / zero;
+  static double plus_zero = 0.0;
+  double minus_zero = - plus_zero;
+  return memcmp (&plus_zero, &minus_zero, sizeof (double)) != 0;
 }
+
+/* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
+   So we use -zerod instead.  */
+double zerod = 0.0;
+
+/* On HP-UX 10.20, negating 0.0L does not yield -0.0L.
+   So we use minus_zerol instead.  */
+long double minus_zerol = -LDBL_MIN * LDBL_MIN;
+
+/* 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 NaN() (0.0 / 0.0)
+# define LDBL80_WORDS(exponent,manthi,mantlo) \
+    { mantlo, manthi, exponent }
 #endif
 
 static int
@@ -65,6 +84,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 *, ...))
 {
@@ -185,9 +225,10 @@ 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", -zerod, 33, 44, 55);
     ASSERT (result != NULL);
-    ASSERT (strcmp (result, "-0x0p+0 33") == 0);
+    if (have_minus_zero ())
+      ASSERT (strcmp (result, "-0x0p+0 33") == 0);
     ASSERT (length == strlen (result));
     free (result);
   }
@@ -215,9 +256,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 (strcmp (result, "nan 33") == 0);
+    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);
   }
@@ -446,11 +489,13 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* FLAG_ZERO with NaN.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%010a %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
        <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */
-    ASSERT (strcmp (result, "       nan 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);
   }
@@ -494,9 +539,10 @@ 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);
-    ASSERT (strcmp (result, "-0x0p+0 33") == 0);
+    if (have_minus_zero ())
+      ASSERT (strcmp (result, "-0x0p+0 33") == 0);
     ASSERT (length == strlen (result));
     free (result);
   }
@@ -524,12 +570,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_))
+  { /* 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 (strcmp (result, "nan 33") == 0);
+    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
+           && 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;
@@ -756,11 +905,13 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* FLAG_ZERO with NaN.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%010La %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
        <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */
-    ASSERT (strcmp (result, "       nan 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);
   }
@@ -901,9 +1052,10 @@ 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", -zerod, 33, 44, 55);
     ASSERT (result != NULL);
-    ASSERT (strcmp (result, "-0.000000 33") == 0);
+    if (have_minus_zero ())
+      ASSERT (strcmp (result, "-0.000000 33") == 0);
     ASSERT (length == strlen (result));
     free (result);
   }
@@ -933,9 +1085,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 (strcmp (result, "nan 33") == 0);
+    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);
   }
@@ -1024,9 +1178,11 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* FLAG_ZERO with NaN.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%015f %d", NaN (), 33, 44, 55);
+      my_asnprintf (NULL, &length, "%050f %d", NaNd (), 33, 44, 55);
     ASSERT (result != NULL);
-    ASSERT (strcmp (result, "            nan 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);
   }
@@ -1041,6 +1197,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 =
@@ -1175,9 +1351,10 @@ 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);
-    ASSERT (strcmp (result, "-0.000000 33") == 0);
+    if (have_minus_zero ())
+      ASSERT (strcmp (result, "-0.000000 33") == 0);
     ASSERT (length == strlen (result));
     free (result);
   }
@@ -1205,15 +1382,117 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   }
 
   { /* 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 (strcmp (result, "nan 33") == 0);
+    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_))
+  { /* 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
+           && 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;
@@ -1297,12 +1576,13 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   }
 
   { /* FLAG_ZERO with NaN.  */
-    static long double zero = 0.0L;
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%015Lf %d", zero / zero, 33, 44, 55);
+      my_asnprintf (NULL, &length, "%050Lf %d", NaNl (), 33, 44, 55);
     ASSERT (result != NULL);
-    ASSERT (strcmp (result, "            nan 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);
   }
@@ -1317,6 +1597,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.  */
@@ -1362,9 +1662,10 @@ 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", -zerod, 33, 44, 55);
     ASSERT (result != NULL);
-    ASSERT (strcmp (result, "-0.000000 33") == 0);
+    if (have_minus_zero ())
+      ASSERT (strcmp (result, "-0.000000 33") == 0);
     ASSERT (length == strlen (result));
     free (result);
   }
@@ -1394,9 +1695,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 (strcmp (result, "NAN 33") == 0);
+    ASSERT (strlen (result) >= 3 + 3
+           && strisnan (result, 0, strlen (result) - 3, 1)
+           && strcmp (result + strlen (result) - 3, " 33") == 0);
     ASSERT (length == strlen (result));
     free (result);
   }
@@ -1432,6 +1735,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 =
@@ -1475,9 +1798,10 @@ 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);
-    ASSERT (strcmp (result, "-0.000000 33") == 0);
+    if (have_minus_zero ())
+      ASSERT (strcmp (result, "-0.000000 33") == 0);
     ASSERT (length == strlen (result));
     free (result);
   }
@@ -1505,12 +1829,13 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   }
 
   { /* 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 (strcmp (result, "NAN 33") == 0);
+    ASSERT (strlen (result) >= 3 + 3
+           && strisnan (result, 0, strlen (result) - 3, 1)
+           && strcmp (result + strlen (result) - 3, " 33") == 0);
     ASSERT (length == strlen (result));
     free (result);
   }
@@ -1546,6 +1871,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.  */
@@ -1553,7 +1898,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%e %d", 12.75, 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);
   }
@@ -1563,7 +1909,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%e %d", 1234567.0, 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);
   }
@@ -1652,8 +1999,16 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
        size_t length;
        char *result =
          my_asnprintf (NULL, &length, "%e", data[k].value);
+       const char *expected = data[k].string;
        ASSERT (result != NULL);
-       ASSERT (strmatch (data[k].string, result));
+       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);
       }
@@ -1664,7 +2019,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%e %d", -0.03125, 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);
   }
@@ -1674,7 +2030,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%e %d", 0.0, 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);
   }
@@ -1682,9 +2039,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", -zerod, 33, 44, 55);
     ASSERT (result != NULL);
-    ASSERT (strcmp (result, "-0.000000e+00 33") == 0);
+    if (have_minus_zero ())
+      ASSERT (strcmp (result, "-0.000000e+00 33") == 0
+             || strcmp (result, "-0.000000e+000 33") == 0);
     ASSERT (length == strlen (result));
     free (result);
   }
@@ -1714,9 +2073,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 (strcmp (result, "nan 33") == 0);
+    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);
   }
@@ -1726,7 +2087,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%15e %d", 1.75, 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);
   }
@@ -1736,7 +2098,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%-15e %d", 1.75, 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);
   }
@@ -1746,7 +2109,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%+e %d", 1.75, 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);
   }
@@ -1756,7 +2120,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "% e %d", 1.75, 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);
   }
@@ -1766,7 +2131,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%#e %d", 1.75, 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);
   }
@@ -1776,7 +2142,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%#.e %d", 1.75, 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);
   }
@@ -1786,7 +2153,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%#.e %d", 9.75, 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);
   }
@@ -1796,7 +2164,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%015e %d", 1234.0, 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);
   }
@@ -1815,9 +2184,11 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* FLAG_ZERO with NaN.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%015e %d", NaN (), 33, 44, 55);
+      my_asnprintf (NULL, &length, "%050e %d", NaNd (), 33, 44, 55);
     ASSERT (result != NULL);
-    ASSERT (strcmp (result, "            nan 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);
   }
@@ -1827,7 +2198,30 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%.e %d", 1234.0, 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, "%.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);
   }
@@ -1837,7 +2231,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);
   }
@@ -1847,7 +2242,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);
   }
@@ -1936,8 +2332,16 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
        size_t length;
        char *result =
          my_asnprintf (NULL, &length, "%Le", data[k].value);
+       const char *expected = data[k].string;
        ASSERT (result != NULL);
-       ASSERT (strmatch (data[k].string, result));
+       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);
       }
@@ -1948,7 +2352,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);
   }
@@ -1958,7 +2363,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);
   }
@@ -1966,9 +2372,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);
-    ASSERT (strcmp (result, "-0.000000e+00 33") == 0);
+    if (have_minus_zero ())
+      ASSERT (strcmp (result, "-0.000000e+00 33") == 0
+             || strcmp (result, "-0.000000e+000 33") == 0);
     ASSERT (length == strlen (result));
     free (result);
   }
@@ -1996,22 +2404,125 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   }
 
   { /* 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_))
+  { /* 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 (strcmp (result, "nan 33") == 0);
+    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
+           && 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);
   }
@@ -2021,7 +2532,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);
   }
@@ -2031,7 +2543,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);
   }
@@ -2041,7 +2554,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);
   }
@@ -2051,7 +2565,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);
   }
@@ -2061,7 +2576,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);
   }
@@ -2071,7 +2587,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);
   }
@@ -2081,7 +2598,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);
   }
@@ -2098,12 +2616,13 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   }
 
   { /* FLAG_ZERO with NaN.  */
-    static long double zero = 0.0L;
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%015Le %d", zero / zero, 33, 44, 55);
+      my_asnprintf (NULL, &length, "%050Le %d", NaNl (), 33, 44, 55);
     ASSERT (result != NULL);
-    ASSERT (strcmp (result, "            nan 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);
   }
@@ -2113,7 +2632,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);
   }
@@ -2135,7 +2677,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%g %d", 1234567.0, 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);
   }
@@ -2224,8 +2767,17 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
        size_t length;
        char *result =
          my_asnprintf (NULL, &length, "%g", data[k].value);
+       const char *expected = data[k].string;
        ASSERT (result != NULL);
-       ASSERT (strmatch (data[k].string, result));
+       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);
       }
@@ -2254,9 +2806,10 @@ 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", -zerod, 33, 44, 55);
     ASSERT (result != NULL);
-    ASSERT (strcmp (result, "-0 33") == 0);
+    if (have_minus_zero ())
+      ASSERT (strcmp (result, "-0 33") == 0);
     ASSERT (length == strlen (result));
     free (result);
   }
@@ -2286,9 +2839,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 (strcmp (result, "nan 33") == 0);
+    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);
   }
@@ -2358,7 +2913,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%#.g %d", 9.75, 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);
   }
@@ -2387,9 +2943,11 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* FLAG_ZERO with NaN.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%015g %d", NaN (), 33, 44, 55);
+      my_asnprintf (NULL, &length, "%050g %d", NaNd (), 33, 44, 55);
     ASSERT (result != NULL);
-    ASSERT (strcmp (result, "            nan 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);
   }
@@ -2399,7 +2957,28 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%.g %d", 1234.0, 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, "%.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);
   }
@@ -2419,7 +2998,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);
   }
@@ -2508,8 +3088,17 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
        size_t length;
        char *result =
          my_asnprintf (NULL, &length, "%Lg", data[k].value);
+       const char *expected = data[k].string;
        ASSERT (result != NULL);
-       ASSERT (strmatch (data[k].string, result));
+       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);
       }
@@ -2538,9 +3127,10 @@ 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);
-    ASSERT (strcmp (result, "-0 33") == 0);
+    if (have_minus_zero ())
+      ASSERT (strcmp (result, "-0 33") == 0);
     ASSERT (length == strlen (result));
     free (result);
   }
@@ -2568,15 +3158,117 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   }
 
   { /* 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 (strcmp (result, "nan 33") == 0);
+    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_))
+  { /* 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
+           && 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;
@@ -2643,7 +3335,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);
   }
@@ -2670,12 +3363,13 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   }
 
   { /* FLAG_ZERO with NaN.  */
-    static long double zero = 0.0L;
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%015Lg %d", zero / zero, 33, 44, 55);
+      my_asnprintf (NULL, &length, "%050Lg %d", NaNl (), 33, 44, 55);
     ASSERT (result != NULL);
-    ASSERT (strcmp (result, "            nan 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);
   }
@@ -2685,7 +3379,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);
   }
@@ -2727,6 +3442,151 @@ 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);
+  }
+
+  {
+    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);
+  }
 }
 
 static char *