update from texinfo
[gnulib.git] / tests / test-vasnprintf-posix.c
index 1a6054a..0162196 100644 (file)
@@ -1,5 +1,5 @@
 /* Test of POSIX compatible vasnprintf() and asnprintf() functions.
-   Copyright (C) 2007-2008 Free Software Foundation, Inc.
+   Copyright (C) 2007-2009 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
@@ -53,12 +53,24 @@ have_minus_zero ()
 }
 
 /* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0.
-   So we use -zero instead.  */
+   So we use -zerod instead.  */
 double zerod = 0.0;
 
-/* HP cc on HP-UX 10.20 has a bug with the constant expression -0.0L.
-   So we use -zero instead.  */
-long double zerol = 0.0L;
+/* 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.  */
@@ -539,7 +551,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* Negative zero.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%La %d", -zerol, 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);
@@ -1351,7 +1363,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* Negative zero.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%Lf %d", -zerol, 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);
@@ -1798,7 +1810,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* Negative zero.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%LF %d", -zerol, 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);
@@ -2372,7 +2384,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* Negative zero.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%Le %d", -zerol, 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
@@ -3127,7 +3139,7 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
   { /* Negative zero.  */
     size_t length;
     char *result =
-      my_asnprintf (NULL, &length, "%Lg %d", -zerol, 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);
@@ -3587,6 +3599,56 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...))
     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
 }
 
 static char *