[PATCH v3 0/3] Tests for argp/err/error functions with long double

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH v3 0/3] Tests for argp/err/error functions with long double

Gabriel F. T. Gomes-2
Florian, thanks for the review.  It definitely looks better with the use
of support_capture_subprocess.

Changes since v2:

  - Removed shell scripts to deal with program output, replacing them
    with in-memory buffers.
  - Now using support/capture_subprocess.h to avoid the need for
    multiple test files on patches 1/3 and 3/3.
  - No changes to patch 2/3.

Changes since v1:

  - Removed tst-ldbl-warn.sh from patch 2/3 and replaced it with an
    in-memory buffer, as suggested by Florian.
  - No changes to patches 1/3, and 3/3.

Gabriel F. T. Gomes (3):
  Add tests for argp_error and argp_failure with floating-point
    parameters
  Add test for warn, warnx, vwarn, and vwarnx with floating-point
    parameters
  Add tests with floating-point arguments for err* and verr* functions

 argp/Makefile         |   2 +
 argp/tst-ldbl-argp.c  | 109 ++++++++++++++++++++++++++++++++++++++++++++
 misc/Makefile         |   3 ++
 misc/tst-ldbl-error.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++
 misc/tst-ldbl-warn.c  | 115 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 352 insertions(+)
 create mode 100644 argp/tst-ldbl-argp.c
 create mode 100644 misc/tst-ldbl-error.c
 create mode 100644 misc/tst-ldbl-warn.c

--
2.14.4

Reply | Threaded
Open this post in threaded view
|

[PATCH v3 1/3] Add tests for argp_error and argp_failure with floating-point parameters

Gabriel F. T. Gomes-2
The functions argp_error and argp_failure, from argp.h, have a format
string as parameter, which can possibly request the printing of
floating-point values.  These values could be of long double type, which
can have different formats, depending on the architecture and on
compilation parameters (for instance, on powerpc, long double values can
have double format (-mlong-double-64) or IBM Extended Precision format
(-mlong-double-128).

This patch adds tests for argp_error and argp_failure that contain a
format string with double and long double conversion specifiers ('%f'
and '%Lf').  These tests automatically check that the default format of
the long double type works.  A future patch will extend the test for
platforms that can have an optional format for long double.

Tested for powerpc64le.

        * argp/Makefile: (tests): Add tst-ldbl-argp.
        * argp/tst-ldbl-argp.c: New file.
---
 argp/Makefile        |   3 +-
 argp/tst-ldbl-argp.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 111 insertions(+), 1 deletion(-)
 create mode 100644 argp/tst-ldbl-argp.c

diff --git a/argp/Makefile b/argp/Makefile
index 6e4799c220..2ea71185a3 100644
--- a/argp/Makefile
+++ b/argp/Makefile
@@ -26,7 +26,8 @@ headers = argp.h
 routines = $(addprefix argp-, ba fmtstream fs-xinl help parse pv \
      pvh xinl eexst)
 
-tests = argp-test tst-argp1 bug-argp1 tst-argp2 bug-argp2
+tests = argp-test tst-argp1 bug-argp1 tst-argp2 bug-argp2 \
+  tst-ldbl-argp
 
 CFLAGS-argp-help.c += $(uses-callbacks) -fexceptions
 CFLAGS-argp-parse.c += $(uses-callbacks)
diff --git a/argp/tst-ldbl-argp.c b/argp/tst-ldbl-argp.c
new file mode 100644
index 0000000000..4465f812af
--- /dev/null
+++ b/argp/tst-ldbl-argp.c
@@ -0,0 +1,109 @@
+/* Testing of long double conversions in argp.h functions.
+   Copyright (C) 2018 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <argp.h>
+#include <string.h>
+
+#include <support/capture_subprocess.h>
+#include <support/check.h>
+
+static const struct argp_option
+options[] =
+{
+  { "error", 'e', "format", OPTION_ARG_OPTIONAL,
+    "Run argp_error function with a format string", 0 },
+  { "failure", 'f', "format", OPTION_ARG_OPTIONAL,
+    "Run argp_failure function with a format string", 0 },
+  { NULL, 0, NULL, 0, NULL }
+};
+
+static error_t
+parser (int key, char *arg, struct argp_state *state)
+{
+  switch (key)
+    {
+      case 'e':
+ argp_error (state, "%Lf%f%Lf%f", (long double) -1, (double) -2,
+    (long double) -3, (double) -4);
+ break;
+      case 'f':
+ argp_failure (state, 0, 0, "%Lf%f%Lf%f", (long double) -1,
+      (double) -2, (long double) -3, (double) -4);
+ break;
+      default:
+ return ARGP_ERR_UNKNOWN;
+    }
+  return 0;
+}
+
+static struct argp
+argp =
+{
+  options, parser
+};
+
+int argc = 2;
+char *argv[3] = { (char *) "test-argp", NULL, NULL };
+
+static void
+do_test_call (void)
+{
+  int remaining;
+  argp_parse (&argp, argc, argv, 0, &remaining, NULL);
+}
+
+static int
+do_one_test (const char *expected)
+{
+  struct support_capture_subprocess result;
+  result = support_capture_subprocess ((void *) &do_test_call, NULL);
+
+  if (strcmp (result.err.buffer, expected) != 0)
+    {
+      support_record_failure ();
+      printf ("error:\n"
+      "    expected: '%s'\n"
+      "      actual: '%s'\n",
+      expected, result.err.buffer);
+    }
+
+  return 0;
+}
+
+static int
+do_test (void)
+{
+  const char *param_error = "--error";
+  const char *expected_error =
+    "test-argp: -1.000000-2.000000-3.000000-4.000000\n"
+    "Try `test-argp --help' or `test-argp --usage' for more information.\n";
+
+  const char *param_failure = "--failure";
+  const char *expected_failure =
+    "test-argp: -1.000000-2.000000-3.000000-4.000000\n";
+
+  argv[1] = (char *) param_error;
+  do_one_test (expected_error);
+
+  argv[1] = (char *) param_failure;
+  do_one_test (expected_failure);
+
+  return 0;
+}
+
+#include <support/test-driver.c>
--
2.14.4

Reply | Threaded
Open this post in threaded view
|

[PATCH v3 2/3] Add test for warn, warnx, vwarn, and vwarnx with floating-point parameters

Gabriel F. T. Gomes-2
In reply to this post by Gabriel F. T. Gomes-2
Similarly to what has been done for argp_error and argp_failure, this
patch patch adds new tests for the warn, warnx, vwarn, and vwarnx
functions.  The new tests use the format string to request the
conversion of long double parameters into string.  Currently, these
tests only check that the default format of the long double type works.
Future patches will extend the test for platforms that can have an
optional format for long double.

Tested for powerpc64le.

        * misc/Makefile (tests): Add tst-ldbl-warn.
        * misc/tst-ldbl-warn.c: New file.
---
 misc/Makefile        |   3 +-
 misc/tst-ldbl-warn.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 117 insertions(+), 1 deletion(-)
 create mode 100644 misc/tst-ldbl-warn.c

diff --git a/misc/Makefile b/misc/Makefile
index 9a87e81ae5..e3758e1fe4 100644
--- a/misc/Makefile
+++ b/misc/Makefile
@@ -84,7 +84,8 @@ tests := tst-dirname tst-tsearch tst-fdset tst-efgcvt tst-mntent tst-hsearch \
  tst-error1 tst-pselect tst-insremque tst-mntent2 bug-hsearch1 \
  tst-mntent-blank-corrupt tst-mntent-blank-passno bug18240 \
  tst-preadvwritev tst-preadvwritev64 tst-makedev tst-empty \
- tst-preadvwritev2 tst-preadvwritev64v2 tst-warn-wide
+ tst-preadvwritev2 tst-preadvwritev64v2 tst-warn-wide \
+ tst-ldbl-warn
 
 tests-internal := tst-atomic tst-atomic-long tst-allocate_once
 tests-static := tst-empty
diff --git a/misc/tst-ldbl-warn.c b/misc/tst-ldbl-warn.c
new file mode 100644
index 0000000000..a9406404bf
--- /dev/null
+++ b/misc/tst-ldbl-warn.c
@@ -0,0 +1,115 @@
+/* Test for the long double conversions in *warn* functions.
+   Copyright (C) 2018 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <err.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <support/check.h>
+
+enum {WARN, WARNX, VWARN, VWARNX};
+
+static void
+do_one_test (int select, const char *format, va_list args,
+     long double arg1, double arg2, long double arg3,
+     double arg4, const char *expected)
+{
+  /* Prepare in-memory buffer to hold the output.  */
+  char *buffer = NULL;
+  size_t length = 0;
+  FILE *fp = open_memstream (&buffer, &length);
+  TEST_VERIFY_EXIT (fp != NULL);
+  FILE *old_stderr = stderr;
+  stderr = fp;
+
+  /* Write to the buffer using one of the *warn* functions.  */
+  switch (select)
+    {
+      case WARN:
+ warn (format, arg1, arg2, arg3, arg4);
+ break;
+      case WARNX:
+ warnx (format, arg1, arg2, arg3, arg4);
+ break;
+      case VWARN:
+ vwarn (format, args);
+ break;
+      case VWARNX:
+ vwarnx (format, args);
+ break;
+    }
+
+  stderr = old_stderr;
+  TEST_VERIFY_EXIT (!ferror (fp));
+  TEST_COMPARE (fclose (fp), 0);
+
+  /* Check that the rest of the output is as expected.  */
+  if (strcmp (buffer, expected) != 0)
+    FAIL_EXIT1 ("unexpected output: %s", buffer);
+  free (buffer);
+}
+
+static void
+do_test_call_varg (const char *format, ...)
+{
+  va_list args;
+
+  va_start (args, format);
+  do_one_test (VWARN, format, args, 0, 0, 0, 0,
+       "tst-ldbl-warn: "
+       "-1.000000 - -2.000000 - -3.000000 - -4.000000: Success\n");
+  va_end (args);
+
+  va_start (args, format);
+  do_one_test (VWARNX, format, args, 0, 0, 0, 0,
+       "tst-ldbl-warn: "
+       "-1.000000 - -2.000000 - -3.000000 - -4.000000\n");
+  va_end (args);
+}
+
+static void
+do_test_call_rarg (const char *format, long double arg1, double arg2,
+   long double arg3, double arg4)
+{
+  va_list args;
+  memset (&args, 0, sizeof (args));
+  do_one_test (WARN, format, args, arg1, arg2, arg3, arg4,
+       "tst-ldbl-warn: "
+       "-1.000000 - -2.000000 - -3.000000 - -4.000000: Success\n");
+  do_one_test (WARNX, format, args, arg1, arg2, arg3, arg4,
+       "tst-ldbl-warn: "
+       "-1.000000 - -2.000000 - -3.000000 - -4.000000\n");
+}
+
+static int
+do_test (void)
+{
+  long double arg1 = -1;
+  long double arg3 = -3;
+  double arg2 = -2;
+  double arg4 = -4;
+
+  do_test_call_rarg ("%Lf - %f - %Lf - %f", arg1, arg2, arg3, arg4);
+  do_test_call_varg ("%Lf - %f - %Lf - %f", arg1, arg2, arg3, arg4);
+
+  return 0;
+}
+
+#include <support/test-driver.c>
--
2.14.4

Reply | Threaded
Open this post in threaded view
|

[PATCH v3 3/3] Add tests with floating-point arguments for err* and verr* functions

Gabriel F. T. Gomes-2
In reply to this post by Gabriel F. T. Gomes-2
Similarly to what has been done for argp_error, and argp_failure, as
well as for warn, warnx, vwarn, and vwarnx, this patch adds new tests
for the following functions: err, errx, verr, verrx, error, and
error_at_line.  The new tests check that the conversion of long double
variables into string works correctly on the default format of the type.
Future patches will reuse these tests for other formats that long double
can take.

Tested for powerpc64le.

        * misc/Makefile (tests): Add tst-ldbl-error.
        * misc/tst-ldbl-error.c: New file.
---
 misc/Makefile         |   2 +-
 misc/tst-ldbl-error.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 misc/tst-ldbl-error.c

diff --git a/misc/Makefile b/misc/Makefile
index e3758e1fe4..1a4aaac1ab 100644
--- a/misc/Makefile
+++ b/misc/Makefile
@@ -85,7 +85,7 @@ tests := tst-dirname tst-tsearch tst-fdset tst-efgcvt tst-mntent tst-hsearch \
  tst-mntent-blank-corrupt tst-mntent-blank-passno bug18240 \
  tst-preadvwritev tst-preadvwritev64 tst-makedev tst-empty \
  tst-preadvwritev2 tst-preadvwritev64v2 tst-warn-wide \
- tst-ldbl-warn
+ tst-ldbl-warn tst-ldbl-error
 
 tests-internal := tst-atomic tst-atomic-long tst-allocate_once
 tests-static := tst-empty
diff --git a/misc/tst-ldbl-error.c b/misc/tst-ldbl-error.c
new file mode 100644
index 0000000000..09bba61b5e
--- /dev/null
+++ b/misc/tst-ldbl-error.c
@@ -0,0 +1,123 @@
+/* Test for the long double conversions in *err* functions.
+   Copyright (C) 2018 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <err.h>
+#include <error.h>
+#include <stdarg.h>
+#include <string.h>
+
+#include <support/capture_subprocess.h>
+#include <support/check.h>
+
+struct tests
+{
+  void *callback;
+  const char *expected;
+};
+
+va_list args;
+
+static void
+callback_err (void *closure)
+{
+  err (0, "%Lf", (long double) -1);
+}
+
+static void
+callback_errx (void *closure)
+{
+  errx (0, "%Lf", (long double) -1);
+}
+
+static void
+callback_verr (void *closure)
+{
+  verr (0, "%Lf", args);
+}
+
+static void
+callback_verrx (void *closure)
+{
+  verrx (0, "%Lf", args);
+}
+
+static void
+callback_error (void *closure)
+{
+  error (0, 0, "%Lf", (long double) -1);
+}
+
+static void
+callback_error_at_line (void *closure)
+{
+  error_at_line (0, 0, "", 0, "%Lf", (long double) -1);
+}
+
+static void
+do_one_test (void *callback, const char *expected, ...)
+{
+  struct support_capture_subprocess result;
+
+  va_start (args, expected);
+
+  /* Call 'callback', which fills in the output and error buffers.  */
+  result = support_capture_subprocess (callback, NULL);
+
+  /* The functions err, errx, verr, and verrx print just the program
+     name followed by a colon, whereas error and error_at_line print the
+     whole path to the program.  Since the whole path depends on the
+     working directory used to build and test glibc, remove it from the
+     comparison against the expected result.  */
+  const char *needle = "tst-ldbl-error:";
+  char *message;
+  message = strstr (result.err.buffer, needle);
+
+  /* Verify that the output message is as expected.  */
+  if (strcmp (message, expected) != 0)
+    {
+      support_record_failure ();
+      printf ("error:\n"
+      "    expected: '%s'\n"
+      "      actual: '%s'\n",
+      expected, message);
+    }
+
+  va_end (args);
+}
+
+static int
+do_test (void)
+{
+  struct tests tests[] = {
+    { &callback_err, "tst-ldbl-error: -1.000000: Success\n" },
+    { &callback_errx, "tst-ldbl-error: -1.000000\n" },
+    { &callback_verr, "tst-ldbl-error: -1.000000: Success\n" },
+    { &callback_verrx, "tst-ldbl-error: -1.000000\n" },
+    { &callback_error, "tst-ldbl-error: -1.000000\n" },
+    { &callback_error_at_line, "tst-ldbl-error::0: -1.000000\n" }
+  };
+
+  for (int i = 0; i < sizeof (tests) / sizeof (tests[0]); i++)
+    {
+      do_one_test (tests[i].callback, tests[i].expected, (long double) -1);
+    }
+
+  return 0;
+}
+
+#include <support/test-driver.c>
--
2.14.4

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v3 0/3] Tests for argp/err/error functions with long double

Gabriel F. T. Gomes-2
In reply to this post by Gabriel F. T. Gomes-2
Ping.

This patch set is pending review.
(https://sourceware.org/ml/libc-alpha/2018-08/msg00583.html)

On Tue, 28 Aug 2018, Gabriel F. T. Gomes wrote:

>Florian, thanks for the review.  It definitely looks better with the use
>of support_capture_subprocess.
>
>Changes since v2:
>
>  - Removed shell scripts to deal with program output, replacing them
>    with in-memory buffers.
>  - Now using support/capture_subprocess.h to avoid the need for
>    multiple test files on patches 1/3 and 3/3.
>  - No changes to patch 2/3.
>
>Changes since v1:
>
>  - Removed tst-ldbl-warn.sh from patch 2/3 and replaced it with an
>    in-memory buffer, as suggested by Florian.
>  - No changes to patches 1/3, and 3/3.
>
>Gabriel F. T. Gomes (3):
>  Add tests for argp_error and argp_failure with floating-point
>    parameters
>  Add test for warn, warnx, vwarn, and vwarnx with floating-point
>    parameters
>  Add tests with floating-point arguments for err* and verr* functions
>
> argp/Makefile         |   2 +
> argp/tst-ldbl-argp.c  | 109 ++++++++++++++++++++++++++++++++++++++++++++
> misc/Makefile         |   3 ++
> misc/tst-ldbl-error.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++
> misc/tst-ldbl-warn.c  | 115 ++++++++++++++++++++++++++++++++++++++++++++++
> 5 files changed, 352 insertions(+)
> create mode 100644 argp/tst-ldbl-argp.c
> create mode 100644 misc/tst-ldbl-error.c
> create mode 100644 misc/tst-ldbl-warn.c
>

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v3 2/3] Add test for warn, warnx, vwarn, and vwarnx with floating-point parameters

Florian Weimer-5
In reply to this post by Gabriel F. T. Gomes-2
* Gabriel F. T. Gomes:

> +  FILE *fp = open_memstream (&buffer, &length);

I think I mentioned xopen_memstream before.

> +  /* Write to the buffer using one of the *warn* functions.  */
> +  switch (select)
> +    {

You need to initialize errno to zero before the switch, I think.  The
test is currently rather brittle.

> +  /* Check that the rest of the output is as expected.  */
> +  if (strcmp (buffer, expected) != 0)
> +    FAIL_EXIT1 ("unexpected output: %s", buffer);

It would also help to include expected output here, so that the context
of the failure is clearer.

Okay with these changes.

Thanks,
Florian
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v3 3/3] Add tests with floating-point arguments for err* and verr* functions

Florian Weimer-5
In reply to this post by Gabriel F. T. Gomes-2
* Gabriel F. T. Gomes:

> +static void
> +callback_err (void *closure)
> +{
> +  err (0, "%Lf", (long double) -1);
> +}

Same problem as in the other test: You need to initialize errno in each
subprocess callback.

Otherwise, looks okay.  You can commit if you add the errno
initialization.

Thanks,
Florian
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v3 1/3] Add tests for argp_error and argp_failure with floating-point parameters

Florian Weimer-5
In reply to this post by Gabriel F. T. Gomes-2
* Gabriel F. T. Gomes:

> * argp/Makefile: (tests): Add tst-ldbl-argp.
> * argp/tst-ldbl-argp.c: New file.

This looks okay to me.  I'll look into adding TEST_COMPARE_STRING, but
this should not block you.

Thanks,
Florian
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v3 1/3] Add tests for argp_error and argp_failure with floating-point parameters

Gabriel F. T. Gomes-2
On Mon, 05 Nov 2018, Florian Weimer wrote:

>* Gabriel F. T. Gomes:
>
>> * argp/Makefile: (tests): Add tst-ldbl-argp.
>> * argp/tst-ldbl-argp.c: New file.  
>
>This looks okay to me.  I'll look into adding TEST_COMPARE_STRING, but
>this should not block you.

Thank you.  I have pushed this patch.
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v3 2/3] Add test for warn, warnx, vwarn, and vwarnx with floating-point parameters

Gabriel F. T. Gomes-2
In reply to this post by Florian Weimer-5
On Mon, 05 Nov 2018, Florian Weimer wrote:

>* Gabriel F. T. Gomes:
>
>> +  FILE *fp = open_memstream (&buffer, &length);  
>
>I think I mentioned xopen_memstream before.
>
>> +  /* Write to the buffer using one of the *warn* functions.  */
>> +  switch (select)
>> +    {  
>
>You need to initialize errno to zero before the switch, I think.  The
>test is currently rather brittle.
>
>> +  /* Check that the rest of the output is as expected.  */
>> +  if (strcmp (buffer, expected) != 0)
>> +    FAIL_EXIT1 ("unexpected output: %s", buffer);  
>
>It would also help to include expected output here, so that the context
>of the failure is clearer.
>
>Okay with these changes.

Thank you.  Here's what I have committed:


Add test for warn, warnx, vwarn, and vwarnx with floating-point parameters

Similarly to what has been done for argp_error and argp_failure, this
patch patch adds new tests for the warn, warnx, vwarn, and vwarnx
functions.  The new tests use the format string to request the
conversion of long double parameters into string.  Currently, these
tests only check that the default format of the long double type works.
Future patches will extend the test for platforms that can have an
optional format for long double.

Tested for powerpc64le.

        * misc/Makefile (tests): Add tst-ldbl-warn.
        * misc/tst-ldbl-warn.c: New file.
---
 ChangeLog            |   5 +++
 misc/Makefile        |   3 +-
 misc/tst-ldbl-warn.c | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 127 insertions(+), 1 deletion(-)
 create mode 100644 misc/tst-ldbl-warn.c

diff --git a/ChangeLog b/ChangeLog
index f61a292d97..7368be4c78 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2018-11-06  Gabriel F. T. Gomes  <[hidden email]>
+
+ * misc/Makefile (tests): Add tst-ldbl-warn.
+ * misc/tst-ldbl-warn.c: New file.
+
 2018-11-06  Gabriel F. T. Gomes  <[hidden email]>
 
  * argp/Makefile (tests): Add tst-ldbl-argp.
diff --git a/misc/Makefile b/misc/Makefile
index 30e0673c8a..201de124f4 100644
--- a/misc/Makefile
+++ b/misc/Makefile
@@ -84,7 +84,8 @@ tests := tst-dirname tst-tsearch tst-fdset tst-efgcvt tst-mntent tst-hsearch \
  tst-error1 tst-pselect tst-insremque tst-mntent2 bug-hsearch1 \
  tst-mntent-blank-corrupt tst-mntent-blank-passno bug18240 \
  tst-preadvwritev tst-preadvwritev64 tst-makedev tst-empty \
- tst-preadvwritev2 tst-preadvwritev64v2 tst-warn-wide
+ tst-preadvwritev2 tst-preadvwritev64v2 tst-warn-wide \
+ tst-ldbl-warn
 
 # Tests which need libdl.
 ifeq (yes,$(build-shared))
diff --git a/misc/tst-ldbl-warn.c b/misc/tst-ldbl-warn.c
new file mode 100644
index 0000000000..3ac0e4c873
--- /dev/null
+++ b/misc/tst-ldbl-warn.c
@@ -0,0 +1,120 @@
+/* Test for the long double conversions in *warn* functions.
+   Copyright (C) 2018 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <err.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <support/check.h>
+#include <support/xmemstream.h>
+
+enum {WARN, WARNX, VWARN, VWARNX};
+
+static void
+do_one_test (int select, const char *format, va_list args,
+     long double arg1, double arg2, long double arg3,
+     double arg4, const char *expected)
+{
+  /* Prepare in-memory buffer to hold the output.  */
+  struct xmemstream stream;
+  xopen_memstream (&stream);
+  FILE *old_stderr = stderr;
+  stderr = stream.out;
+
+  /* Write to the buffer using one of the *warn* functions.  */
+  errno = 0;
+  switch (select)
+    {
+      case WARN:
+ warn (format, arg1, arg2, arg3, arg4);
+ break;
+      case WARNX:
+ warnx (format, arg1, arg2, arg3, arg4);
+ break;
+      case VWARN:
+ vwarn (format, args);
+ break;
+      case VWARNX:
+ vwarnx (format, args);
+ break;
+    }
+
+  stderr = old_stderr;
+
+  /* Close the in-memory stream and check the output buffer.  */
+  xfclose_memstream (&stream);
+  if (strcmp (stream.buffer, expected) != 0)
+    FAIL_EXIT1 ("error:\n"
+ "    expected: '%s'\n"
+ "      actual: '%s'\n",
+ expected, stream.buffer);
+
+  if (stream.buffer != NULL)
+    free (stream.buffer);
+}
+
+static void
+do_test_call_varg (const char *format, ...)
+{
+  va_list args;
+
+  va_start (args, format);
+  do_one_test (VWARN, format, args, 0, 0, 0, 0,
+       "tst-ldbl-warn: "
+       "-1.000000 - -2.000000 - -3.000000 - -4.000000: Success\n");
+  va_end (args);
+
+  va_start (args, format);
+  do_one_test (VWARNX, format, args, 0, 0, 0, 0,
+       "tst-ldbl-warn: "
+       "-1.000000 - -2.000000 - -3.000000 - -4.000000\n");
+  va_end (args);
+}
+
+static void
+do_test_call_rarg (const char *format, long double arg1, double arg2,
+   long double arg3, double arg4)
+{
+  va_list args;
+  memset (&args, 0, sizeof (args));
+  do_one_test (WARN, format, args, arg1, arg2, arg3, arg4,
+       "tst-ldbl-warn: "
+       "-1.000000 - -2.000000 - -3.000000 - -4.000000: Success\n");
+  do_one_test (WARNX, format, args, arg1, arg2, arg3, arg4,
+       "tst-ldbl-warn: "
+       "-1.000000 - -2.000000 - -3.000000 - -4.000000\n");
+}
+
+static int
+do_test (void)
+{
+  long double arg1 = -1;
+  long double arg3 = -3;
+  double arg2 = -2;
+  double arg4 = -4;
+
+  do_test_call_rarg ("%Lf - %f - %Lf - %f", arg1, arg2, arg3, arg4);
+  do_test_call_varg ("%Lf - %f - %Lf - %f", arg1, arg2, arg3, arg4);
+
+  return 0;
+}
+
+#include <support/test-driver.c>



Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v3 3/3] Add tests with floating-point arguments for err* and verr* functions

Gabriel F. T. Gomes-2
In reply to this post by Florian Weimer-5
On Mon, 05 Nov 2018, Florian Weimer wrote:

>* Gabriel F. T. Gomes:
>
>> +static void
>> +callback_err (void *closure)
>> +{
>> +  err (0, "%Lf", (long double) -1);
>> +}  
>
>Same problem as in the other test: You need to initialize errno in each
>subprocess callback.
>
>Otherwise, looks okay.  You can commit if you add the errno
>initialization.

Thank you.  I have committed the following patch to master:

Add tests with floating-point arguments for err* and verr* functions

Similarly to what has been done for argp_error, and argp_failure, as
well as for warn, warnx, vwarn, and vwarnx, this patch adds new tests
for the following functions: err, errx, verr, verrx, error, and
error_at_line.  The new tests check that the conversion of long double
variables into string works correctly on the default format of the type.
Future patches will reuse these tests for other formats that long double
can take.

Tested for powerpc64le.

        * misc/Makefile (tests): Add tst-ldbl-error.
        * misc/tst-ldbl-error.c: New file.
---
 ChangeLog             |   5 ++
 misc/Makefile         |   2 +-
 misc/tst-ldbl-error.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 136 insertions(+), 1 deletion(-)
 create mode 100644 misc/tst-ldbl-error.c

diff --git a/ChangeLog b/ChangeLog
index 7368be4c78..d64838bc04 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2018-11-06  Gabriel F. T. Gomes  <[hidden email]>
+
+ * misc/Makefile (tests): Add tst-ldbl-error.
+ * misc/tst-ldbl-error.c: New file.
+
 2018-11-06  Gabriel F. T. Gomes  <[hidden email]>
 
  * misc/Makefile (tests): Add tst-ldbl-warn.
diff --git a/misc/Makefile b/misc/Makefile
index 201de124f4..c2c9994d17 100644
--- a/misc/Makefile
+++ b/misc/Makefile
@@ -85,7 +85,7 @@ tests := tst-dirname tst-tsearch tst-fdset tst-efgcvt tst-mntent tst-hsearch \
  tst-mntent-blank-corrupt tst-mntent-blank-passno bug18240 \
  tst-preadvwritev tst-preadvwritev64 tst-makedev tst-empty \
  tst-preadvwritev2 tst-preadvwritev64v2 tst-warn-wide \
- tst-ldbl-warn
+ tst-ldbl-warn tst-ldbl-error
 
 # Tests which need libdl.
 ifeq (yes,$(build-shared))
diff --git a/misc/tst-ldbl-error.c b/misc/tst-ldbl-error.c
new file mode 100644
index 0000000000..a02cb2772a
--- /dev/null
+++ b/misc/tst-ldbl-error.c
@@ -0,0 +1,130 @@
+/* Test for the long double conversions in *err* functions.
+   Copyright (C) 2018 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <err.h>
+#include <errno.h>
+#include <error.h>
+#include <stdarg.h>
+#include <string.h>
+
+#include <support/capture_subprocess.h>
+#include <support/check.h>
+
+struct tests
+{
+  void *callback;
+  const char *expected;
+};
+
+va_list args;
+
+static void
+callback_err (void *closure)
+{
+  errno = 0;
+  err (0, "%Lf", (long double) -1);
+}
+
+static void
+callback_errx (void *closure)
+{
+  errno = 0;
+  errx (0, "%Lf", (long double) -1);
+}
+
+static void
+callback_verr (void *closure)
+{
+  errno = 0;
+  verr (0, "%Lf", args);
+}
+
+static void
+callback_verrx (void *closure)
+{
+  errno = 0;
+  verrx (0, "%Lf", args);
+}
+
+static void
+callback_error (void *closure)
+{
+  errno = 0;
+  error (0, 0, "%Lf", (long double) -1);
+}
+
+static void
+callback_error_at_line (void *closure)
+{
+  errno = 0;
+  error_at_line (0, 0, "", 0, "%Lf", (long double) -1);
+}
+
+static void
+do_one_test (void *callback, const char *expected, ...)
+{
+  struct support_capture_subprocess result;
+
+  va_start (args, expected);
+
+  /* Call 'callback', which fills in the output and error buffers.  */
+  result = support_capture_subprocess (callback, NULL);
+
+  /* The functions err, errx, verr, and verrx print just the program
+     name followed by a colon, whereas error and error_at_line print the
+     whole path to the program.  Since the whole path depends on the
+     working directory used to build and test glibc, remove it from the
+     comparison against the expected result.  */
+  const char *needle = "tst-ldbl-error:";
+  char *message;
+  message = strstr (result.err.buffer, needle);
+
+  /* Verify that the output message is as expected.  */
+  if (strcmp (message, expected) != 0)
+    {
+      support_record_failure ();
+      printf ("error:\n"
+      "    expected: '%s'\n"
+      "      actual: '%s'\n",
+      expected, message);
+    }
+
+  va_end (args);
+}
+
+static int
+do_test (void)
+{
+  struct tests tests[] = {
+    { &callback_err, "tst-ldbl-error: -1.000000: Success\n" },
+    { &callback_errx, "tst-ldbl-error: -1.000000\n" },
+    { &callback_verr, "tst-ldbl-error: -1.000000: Success\n" },
+    { &callback_verrx, "tst-ldbl-error: -1.000000\n" },
+    { &callback_error, "tst-ldbl-error: -1.000000\n" },
+    { &callback_error_at_line, "tst-ldbl-error::0: -1.000000\n" }
+  };
+
+  for (int i = 0; i < sizeof (tests) / sizeof (tests[0]); i++)
+    {
+      do_one_test (tests[i].callback, tests[i].expected, (long double) -1);
+    }
+
+  return 0;
+}
+
+#include <support/test-driver.c>