autoupdate
[gnulib.git] / tests / test-vasnprintf-posix.c
index 3a9879f..dea0d3c 100644 (file)
@@ -1,5 +1,5 @@
 /* Test of POSIX compatible vasnprintf() and asnprintf() functions.
-   Copyright (C) 2007-2010 Free Software Foundation, Inc.
+   Copyright (C) 2007-2011 Free Software Foundation, Inc.
 
    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
@@ -29,6 +29,7 @@
 #include <string.h>
 
 #include "macros.h"
+#include "minus-zero.h"
 #include "nan.h"
 
 /* The SGI MIPS floating-point format does not distinguish 0.0 and -0.0.  */
@@ -36,30 +37,10 @@ static int
 have_minus_zero ()
 {
   static double plus_zero = 0.0;
-  double minus_zero = - plus_zero;
+  double minus_zero = minus_zerod;
   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.
-   IRIX cc can't put -0.0L into .data, but can compute at runtime.
-   Note that the expression -LDBL_MIN * LDBL_MIN does not work on other
-   platforms, such as when cross-compiling to PowerPC on MacOS X 10.5.  */
-#if defined __hpux || defined __sgi
-static long double
-compute_minus_zerol (void)
-{
-  return -LDBL_MIN * LDBL_MIN;
-}
-# define minus_zerol compute_minus_zerol ()
-#else
-long double minus_zerol = -0.0L;
-#endif
-
 /* Representation of an 80-bit 'long double' as an initializer for a sequence
    of 'unsigned int' words.  */
 #ifdef WORDS_BIGENDIAN
@@ -225,7 +206,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* Negative zero.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%a %d", -zerod, 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);
@@ -1052,7 +1033,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* Negative zero.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%f %d", -zerod, 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);
@@ -1662,7 +1643,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* Negative zero.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%F %d", -zerod, 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);
@@ -2039,7 +2020,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* Negative zero.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%e %d", -zerod, 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
@@ -2806,7 +2787,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* Negative zero.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%g %d", -zerod, 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);
@@ -3572,6 +3553,36 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   }
 
   {
+    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;
@@ -3677,6 +3688,19 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
       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 *