* doc/headers/assert.texi (assert.h): Document assert module use.
[gnulib.git] / tests / test-vasnprintf-posix.c
index 47af6e0..a818655 100644 (file)
 #include "vasnprintf.h"
 
 #include <stdarg.h>
+#include <stddef.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 
 #define ASSERT(expr) if (!(expr)) abort ();
 
+/* 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
+
 static void
 test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
 {
@@ -98,7 +111,6 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     free (result);
   }
 
-#if HAVE_LONG_DOUBLE
   {
     size_t length;
     char *result =
@@ -108,7 +120,6 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     ASSERT (length == strlen (result));
     free (result);
   }
-#endif
 
   /* Test the support of the 'a' and 'A' conversion specifier for hexadecimal
      output of floating-point numbers.  */
@@ -182,7 +193,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* NaN.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%a %d", 0.0 / 0.0, 33, 44, 55);
+      my_asnprintf (NULL, &length, "%a %d", NaN (), 33, 44, 55);
     ASSERT (result != NULL);
     ASSERT (strcmp (result, "nan 33") == 0);
     ASSERT (length == strlen (result));
@@ -267,6 +278,20 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     free (result);
   }
 
+  { /* Rounding can turn a ...FFF into a ...000.
+       This shows a MacOS 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);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
   { /* Width.  */
     size_t length;
     char *result =
@@ -389,6 +414,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%010a %d", 1.0 / 0.0, 33, 44, 55);
     ASSERT (result != NULL);
+    /* "0000000inf 33" is not a valid result; see
+       <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */
     ASSERT (strcmp (result, "       inf 33") == 0);
     ASSERT (length == strlen (result));
     free (result);
@@ -397,15 +424,15 @@ 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", 0.0 / 0.0, 33, 44, 55);
+      my_asnprintf (NULL, &length, "%010a %d", NaN (), 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 (length == strlen (result));
     free (result);
   }
 
-#if HAVE_LONG_DOUBLE
-
   { /* A positive number.  */
     size_t length;
     char *result =
@@ -560,6 +587,21 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     free (result);
   }
 
+  { /* Rounding can turn a ...FFF into a ...000.
+       This shows a MacOS X 10.3.9 (Darwin 7.9) bug and a
+       glibc 2.4 bug <http://sourceware.org/bugzilla/show_bug.cgi?id=2908>.  */
+    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);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
   { /* Width.  */
     size_t length;
     char *result =
@@ -682,6 +724,8 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%010La %d", 1.0L / 0.0L, 33, 44, 55);
     ASSERT (result != NULL);
+    /* "0000000inf 33" is not a valid result; see
+       <http://lists.gnu.org/archive/html/bug-gnulib/2007-04/msg00107.html> */
     ASSERT (strcmp (result, "       inf 33") == 0);
     ASSERT (length == strlen (result));
     free (result);
@@ -692,12 +736,470 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     char *result =
       my_asnprintf (NULL, &length, "%010La %d", 0.0L / 0.0L, 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 (length == strlen (result));
     free (result);
   }
 
-#endif
+  /* Test the support of the %f format directive.  */
+
+  { /* A positive number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%f %d", 12.75, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "12.750000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* A larger positive number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%f %d", 1234567.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "1234567.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* A negative number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%f %d", -0.03125, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "-0.031250 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Positive zero.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%f %d", 0.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "0.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Negative zero.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%f %d", -0.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "-0.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Positive infinity.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%f %d", 1.0 / 0.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "inf 33") == 0
+           || strcmp (result, "infinity 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Negative infinity.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%f %d", -1.0 / 0.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "-inf 33") == 0
+           || strcmp (result, "-infinity 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* NaN.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%f %d", NaN (), 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "nan 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* FLAG_ZERO.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%015f %d", 1234.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "00001234.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* FLAG_ZERO with infinite number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%015f %d", -1.0 / 0.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "           -inf 33") == 0
+           || strcmp (result, "      -infinity 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Precision.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%.f %d", 1234.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "1234 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* A positive number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%Lf %d", 12.75L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "12.750000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* A larger positive number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%Lf %d", 1234567.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "1234567.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* A negative number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%Lf %d", -0.03125L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "-0.031250 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Positive zero.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%Lf %d", 0.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "0.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Negative zero.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%Lf %d", -0.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "-0.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Positive infinity.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%Lf %d", 1.0L / 0.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "inf 33") == 0
+           || strcmp (result, "infinity 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Negative infinity.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%Lf %d", -1.0L / 0.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "-inf 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);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "nan 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* FLAG_ZERO.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%015Lf %d", 1234.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "00001234.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* FLAG_ZERO with infinite number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%015Lf %d", -1.0L / 0.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "           -inf 33") == 0
+           || strcmp (result, "      -infinity 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Precision.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%.Lf %d", 1234.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "1234 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  /* Test the support of the %F format directive.  */
+
+  { /* A positive number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%F %d", 12.75, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "12.750000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* A larger positive number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%F %d", 1234567.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "1234567.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* A negative number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%F %d", -0.03125, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "-0.031250 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Positive zero.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%F %d", 0.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "0.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Negative zero.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%F %d", -0.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "-0.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Positive infinity.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%F %d", 1.0 / 0.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "INF 33") == 0
+           || strcmp (result, "INFINITY 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Negative infinity.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%F %d", -1.0 / 0.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "-INF 33") == 0
+           || strcmp (result, "-INFINITY 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* NaN.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%F %d", NaN (), 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "NAN 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* FLAG_ZERO.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%015F %d", 1234.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "00001234.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* FLAG_ZERO with infinite number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%015F %d", -1.0 / 0.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "           -INF 33") == 0
+           || strcmp (result, "      -INFINITY 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Precision.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%.F %d", 1234.0, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "1234 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* A positive number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%LF %d", 12.75L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "12.750000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* A larger positive number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%LF %d", 1234567.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "1234567.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* A negative number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%LF %d", -0.03125L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "-0.031250 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Positive zero.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%LF %d", 0.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "0.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Negative zero.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%LF %d", -0.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "-0.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Positive infinity.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%LF %d", 1.0L / 0.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "INF 33") == 0
+           || strcmp (result, "INFINITY 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Negative infinity.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%LF %d", -1.0L / 0.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "-INF 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);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "NAN 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* FLAG_ZERO.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%015LF %d", 1234.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "00001234.000000 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* FLAG_ZERO with infinite number.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%015LF %d", -1.0L / 0.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "           -INF 33") == 0
+           || strcmp (result, "      -INFINITY 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
+
+  { /* Precision.  */
+    size_t length;
+    char *result =
+      my_asnprintf (NULL, &length, "%.LF %d", 1234.0L, 33, 44, 55);
+    ASSERT (result != NULL);
+    ASSERT (strcmp (result, "1234 33") == 0);
+    ASSERT (length == strlen (result));
+    free (result);
+  }
 
   /* Test the support of the %n format directive.  */