public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Florian Weimer <fw@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc] stdio-common: Add wide stream coverage to tst-vfprintf-user-type
Date: Fri, 18 Mar 2022 20:42:27 +0000 (GMT)	[thread overview]
Message-ID: <20220318204227.B693B3888C57@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=59c30d5708e4bfc1510176222f7772fe800ff9d2

commit 59c30d5708e4bfc1510176222f7772fe800ff9d2
Author: Florian Weimer <fweimer@redhat.com>
Date:   Fri Mar 18 21:27:54 2022 +0100

    stdio-common: Add wide stream coverage to tst-vfprintf-user-type
    
    And use TEST_COMPARE_STRING for the narrow tests.
    
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

Diff:
---
 stdio-common/tst-vfprintf-user-type.c | 120 ++++++++++++++++++++++++++--------
 1 file changed, 93 insertions(+), 27 deletions(-)

diff --git a/stdio-common/tst-vfprintf-user-type.c b/stdio-common/tst-vfprintf-user-type.c
index f2650ac018..41b2e22641 100644
--- a/stdio-common/tst-vfprintf-user-type.c
+++ b/stdio-common/tst-vfprintf-user-type.c
@@ -21,6 +21,7 @@
    this indicates the number of such pairs which constitute the
    argument.  */
 
+#include <array_length.h>
 #include <locale.h>
 #include <printf.h>
 #include <stdio.h>
@@ -60,13 +61,23 @@ my_printf_function (FILE *fp, const struct printf_info *info,
             __func__, fp, info, args[0], args, (wint_t) info->spec,
             info->prec);
 
+  TEST_COMPARE (info->wide, fwide (fp, 0) > 0);
+
   TEST_VERIFY (info->spec == 'P');
   size_t nargs;
   int printed;
   if (info->prec >= 0)
     {
-      if (fputc ('{', fp) < 0)
-        return -1;
+      if (info->wide)
+        {
+          if (fputwc (L'{', fp) < 0)
+            return -1;
+          }
+      else
+        {
+          if (fputc ('{', fp) < 0)
+            return -1;
+        }
       nargs = info->prec;
       printed = 1;
     }
@@ -80,8 +91,16 @@ my_printf_function (FILE *fp, const struct printf_info *info,
     {
       if (i != 0)
         {
-          if (fputc (',', fp) < 0)
-            return -1;
+          if (info->wide)
+            {
+              if (fputwc (L',', fp) < 0)
+                return -1;
+            }
+          else
+            {
+              if (fputc (',', fp) < 0)
+                return -1;
+            }
           ++printed;
         }
 
@@ -89,15 +108,27 @@ my_printf_function (FILE *fp, const struct printf_info *info,
          and those pointers point to a pointer to the memory area
          supplied to my_va_arg_function.  */
       struct two_argument *pair = *(void **) args[i];
-      int ret = fprintf (fp, "(%ld, %f)", pair->i, pair->d);
+      int ret;
+      if (info->wide)
+        ret = fwprintf (fp, L"(%ld, %f)", pair->i, pair->d);
+      else
+        ret = fprintf (fp, "(%ld, %f)", pair->i, pair->d);
       if (ret < 0)
         return -1;
       printed += ret;
     }
   if (info->prec >= 0)
     {
-      if (fputc ('}', fp) < 0)
-        return -1;
+      if (info->wide)
+        {
+          if (fputwc (L'}', fp) < 0)
+            return -1;
+        }
+      else
+        {
+          if (fputc ('}', fp) < 0)
+            return -1;
+        }
       ++printed;
     }
   return printed;
@@ -155,30 +186,22 @@ do_test (void)
   TEST_VERIFY (asprintf_alias == asprintf);
   char *str = NULL;
   TEST_VERIFY (asprintf_alias (&str, "[[%P]]", 123L, 456.0) >= 0);
-  if (test_verbose > 0)
-    printf ("info: %s\n", str);
-  TEST_VERIFY (strcmp (str, "[[(123, 456.000000)]]") == 0);
+  TEST_COMPARE_STRING (str, "[[(123, 456.000000)]]");
   free (str);
 
   str = NULL;
   TEST_VERIFY (asprintf_alias (&str, "[[%1$P %1$P]]", 123L, 457.0) >= 0);
-  if (test_verbose > 0)
-    printf ("info: %s\n", str);
-  TEST_VERIFY (strcmp (str, "[[(123, 457.000000) (123, 457.000000)]]") == 0);
+  TEST_COMPARE_STRING (str, "[[(123, 457.000000) (123, 457.000000)]]");
   free (str);
 
   str = NULL;
   TEST_VERIFY (asprintf_alias (&str, "[[%.1P]]", 1L, 2.0) >= 0);
-  if (test_verbose > 0)
-    printf ("info: %s\n", str);
-  TEST_VERIFY (strcmp (str, "[[{(1, 2.000000)}]]") == 0);
+  TEST_COMPARE_STRING (str, "[[{(1, 2.000000)}]]");
   free (str);
 
   str = NULL;
   TEST_VERIFY (asprintf_alias (&str, "[[%.2P]]", 1L, 2.0, 3L, 4.0) >= 0);
-  if (test_verbose > 0)
-    printf ("info: %s\n", str);
-  TEST_VERIFY (strcmp (str, "[[{(1, 2.000000),(3, 4.000000)}]]") == 0);
+  TEST_COMPARE_STRING (str, "[[{(1, 2.000000),(3, 4.000000)}]]");
   free (str);
 
   str = NULL;
@@ -187,14 +210,12 @@ do_test (void)
                 /* argument 1: */ 1L, 2.0, 3L, 4.0,
                 /* argument 2: */ 5L, 6.0, 7L, 8.0, 9L, 10.0)
                >= 0);
-  if (test_verbose > 0)
-    printf ("info: %s\n", str);
-  TEST_VERIFY (strcmp (str,
+  TEST_COMPARE_STRING (str,
                        "[["
                        "{(1, 2.000000),(3, 4.000000)}"
                        " | "
                        "{(5, 6.000000),(7, 8.000000),(9, 10.000000)}"
-                       "]]") == 0);
+                       "]]");
   free (str);
 
   /* The following subtest fails due to bug 21534.  */
@@ -205,19 +226,64 @@ do_test (void)
                 /* argument 1: */ 1L, 2.0, 3L, 4.0,
                 /* argument 2: */ 5L, 6.0, 7L, 8.0, 9L, 10.0)
                >= 0);
-  if (test_verbose > 0)
-    printf ("info: %s\n", str);
-  TEST_VERIFY (strcmp (str,
+  TEST_COMPARE_STRING (str,
                        "[["
                        "{(1, 2.000000),(3, 4.000000)}"
                        " | "
                        "{(5, 6.000000),(7, 8.000000),(9, 10.000000)}"
                        " | "
                        "{(1, 2.000000),(3, 4.000000)}"
-                       "]]") == 0);
+                       "]]");
   free (str);
 #endif
 
+  /* Wide variants of the tests above.  */
+
+  wchar_t buf[200];
+  TEST_VERIFY (swprintf (buf, array_length (buf), L"[[%P]]", 123L, 456.0)
+               >= 0);
+  TEST_COMPARE_STRING_WIDE (buf, L"[[(123, 456.000000)]]");
+
+  TEST_VERIFY (swprintf (buf, array_length (buf), L"[[%1$P %1$P]]",
+                         123L, 457.0) >= 0);
+  TEST_COMPARE_STRING_WIDE (buf, L"[[(123, 457.000000) (123, 457.000000)]]");
+
+  TEST_VERIFY (swprintf (buf, array_length (buf), L"[[%.1P]]", 1L, 2.0) >= 0);
+  TEST_COMPARE_STRING_WIDE (buf, L"[[{(1, 2.000000)}]]");
+
+  TEST_VERIFY (swprintf (buf, array_length (buf), L"[[%.2P]]",
+                         1L, 2.0, 3L, 4.0) >= 0);
+  TEST_COMPARE_STRING_WIDE (buf, L"[[{(1, 2.000000),(3, 4.000000)}]]");
+
+  TEST_VERIFY (swprintf
+               (buf, array_length (buf), L"[[%.2P | %.3P]]",
+                /* argument 1: */ 1L, 2.0, 3L, 4.0,
+                /* argument 2: */ 5L, 6.0, 7L, 8.0, 9L, 10.0)
+               >= 0);
+  TEST_COMPARE_STRING_WIDE (buf,
+                            L"[["
+                            "{(1, 2.000000),(3, 4.000000)}"
+                            " | "
+                            "{(5, 6.000000),(7, 8.000000),(9, 10.000000)}"
+                            "]]");
+
+  /* The following subtest fails due to bug 21534.  */
+#if 0
+  TEST_VERIFY (swprintf
+               (&buf, array_length (buf), L"[[%1$.2P | %2$.3P | %1$.2P]]",
+                /* argument 1: */ 1L, 2.0, 3L, 4.0,
+                /* argument 2: */ 5L, 6.0, 7L, 8.0, 9L, 10.0)
+               >= 0);
+  TEST_COMPARE_STRING_WIDE (buf,
+                            L"[["
+                            "{(1, 2.000000),(3, 4.000000)}"
+                            " | "
+                            "{(5, 6.000000),(7, 8.000000),(9, 10.000000)}"
+                            " | "
+                            "{(1, 2.000000),(3, 4.000000)}"
+                            "]]");
+#endif
+
   return 0;
 }


                 reply	other threads:[~2022-03-18 20:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220318204227.B693B3888C57@sourceware.org \
    --to=fw@sourceware.org \
    --cc=glibc-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).