Imported Upstream version 2.72.3
[platform/upstream/glib.git] / glib / tests / test-printf.c
index c6dd9d9..6eadf1e 100644 (file)
 #include <string.h>
 #include "glib.h"
 #include "gstdio.h"
+#ifdef G_OS_WIN32
+#include <io.h>
+#include <fcntl.h>
+#endif
 
 static void
 test_retval_and_trunc (void)
@@ -409,7 +413,7 @@ test_f (void)
   gchar buf[128];
   gint res;
 
-  /* %f, basic formattting */
+  /* %f, basic formatting */
 
   res = g_snprintf (buf, 128, "%f", G_PI);
   g_assert_cmpint (res, ==, 8);
@@ -724,7 +728,7 @@ test_64bit (void)
   /* However, gcc doesn't know about this, so we need to disable printf
    * format warnings...
    */
-#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
+#if G_GNUC_CHECK_VERSION(4, 6)
 _Pragma ("GCC diagnostic push")
 _Pragma ("GCC diagnostic ignored \"-Wformat\"")
 _Pragma ("GCC diagnostic ignored \"-Wformat-extra-args\"")
@@ -762,7 +766,7 @@ _Pragma ("GCC diagnostic ignored \"-Wformat-extra-args\"")
   g_assert_cmpint (res, ==, 5);
   g_assert_cmpstr (buf, ==, "1E240");
 
-#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
+#if G_GNUC_CHECK_VERSION(4, 6)
 _Pragma ("GCC diagnostic pop")
 #endif
 
@@ -815,7 +819,7 @@ test_64bit2_win32 (void)
   /* However, gcc doesn't know about this, so we need to disable printf
    * format warnings...
    */
-#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
+#if G_GNUC_CHECK_VERSION(4, 6)
 _Pragma ("GCC diagnostic push")
 _Pragma ("GCC diagnostic ignored \"-Wformat\"")
 _Pragma ("GCC diagnostic ignored \"-Wformat-extra-args\"")
@@ -845,7 +849,7 @@ _Pragma ("GCC diagnostic ignored \"-Wformat-extra-args\"")
   res = g_printf ("%" "ll" "X\n", (gint64)123456);
   g_assert_cmpint (res, ==, 6);
 
-#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
+#if G_GNUC_CHECK_VERSION(4, 6)
 _Pragma ("GCC diagnostic pop")
 #endif
 }
@@ -859,7 +863,6 @@ test_64bit2 (void)
   g_test_trap_assert_stdout ("123456\n-123456\n123456\n"
                              "361100\n0361100\n1e240\n"
                              "0x1e240\n1E240\n");
-
 #ifdef G_OS_WIN32
   g_test_trap_subprocess ("/printf/test-64bit/subprocess/win32", 0, 0);
   g_test_trap_assert_passed ();
@@ -892,10 +895,63 @@ test_upper_bound (void)
   g_assert_cmpint (res, ==, 20);
 }
 
+#if !defined(__APPLE__) && !defined(__FreeBSD__)
+static gint test_vasprintf_va (gchar       **string,
+                               const gchar  *format,
+                               ...) G_GNUC_PRINTF (2, 3);
+
+/* Wrapper around g_vasprintf() which takes varargs */
+static gint
+test_vasprintf_va (gchar       **string,
+                   const gchar  *format,
+                   ...)
+{
+  va_list args;
+  gint len;
+
+  va_start (args, format);
+  len = g_vasprintf (string, format, args);
+  va_end (args);
+
+  return len;
+}
+#endif  /* !defined(__APPLE__) && !defined(__FreeBSD__) */
+
+static void
+test_vasprintf_invalid_format_placeholder (void)
+{
+#if !defined(__APPLE__) && !defined(__FreeBSD__)
+  gint len = 0;
+  gchar *buf = "some non-null string";
+#endif
+
+  g_test_summary ("Test error handling for invalid format placeholder in g_vasprintf()");
+
+#if !defined(__APPLE__) && !defined(__FreeBSD__)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat"
+#pragma GCC diagnostic ignored "-Wformat-extra-args"
+  len = test_vasprintf_va (&buf, "%l", "nope");
+#pragma GCC diagnostic pop
+
+  g_assert_cmpint (len, ==, -1);
+  g_assert_null (buf);
+#else
+  g_test_skip ("vasprintf() placeholder checks on BSDs are less strict");
+#endif
+}
+
 int
 main (int   argc,
       char *argv[])
 {
+#ifdef G_OS_WIN32
+  /* Ensure binary mode for stdout, this way
+   * tests produce \n line endings on Windows instead of the
+   * default \r\n.
+   */
+  _setmode (fileno (stdout), _O_BINARY);
+#endif
   g_test_init (&argc, &argv, NULL);
 
   g_test_add_func ("/snprintf/retval-and-trunc", test_retval_and_trunc);
@@ -925,5 +981,7 @@ main (int   argc,
   g_test_add_func ("/sprintf/test-positional-params", test_positional_params3);
   g_test_add_func ("/sprintf/upper-bound", test_upper_bound);
 
+  g_test_add_func ("/vasprintf/invalid-format-placeholder", test_vasprintf_invalid_format_placeholder);
+
   return g_test_run();
 }