update from texinfo
[gnulib.git] / tests / test-vasnprintf-posix.c
index 07c173e..0162196 100644 (file)
@@ -3599,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 *