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

Message ID 20180810220011.8432-3-gabriel@inconstante.eti.br
State Superseded
Headers

Commit Message

Gabriel F. T. Gomes Aug. 10, 2018, 10 p.m. UTC
  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-internal): Add tst-ldbl-warn.
	[run-built-tests == yes] (test-special): Add
	$(objpfx)tst-ldbl-warn.out.
	($(objpfx)tst-ldbl-warn.out): New build and run rule.
	* misc/tst-ldbl-warn.c: New file.
	* misc/tst-ldbl-warn.sh: Likewise.
---
 misc/Makefile         |  9 ++++++++
 misc/tst-ldbl-warn.c  | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++
 misc/tst-ldbl-warn.sh | 48 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 118 insertions(+)
 create mode 100644 misc/tst-ldbl-warn.c
 create mode 100644 misc/tst-ldbl-warn.sh
  

Comments

Florian Weimer Aug. 14, 2018, 1:21 p.m. UTC | #1
On 08/11/2018 12:00 AM, Gabriel F. T. Gomes wrote:
> +${test_program_prefix} \
> +  ${test_program} \
> +  2> ${test_program_output} || status=1

You could assign stderr to an in-memory buffer, which would avoid the 
separate shell script.

Thanks,
Florian
  
Gabriel F. T. Gomes Aug. 14, 2018, 2:02 p.m. UTC | #2
On Tue, 14 Aug 2018, Florian Weimer wrote:

>On 08/11/2018 12:00 AM, Gabriel F. T. Gomes wrote:
>> +${test_program_prefix} \
>> +  ${test_program} \
>> +  2> ${test_program_output} || status=1  
>
>You could assign stderr to an in-memory buffer, which would avoid the 
>separate shell script.

Oh, like you did for the err, warn, etc. with __fxprintf test case. Thanks
for the suggestion and example. :)
  

Patch

diff --git a/misc/Makefile b/misc/Makefile
index b7be2bc19a..eb77b87b0d 100644
--- a/misc/Makefile
+++ b/misc/Makefile
@@ -89,11 +89,20 @@  tests := tst-dirname tst-tsearch tst-fdset tst-efgcvt tst-mntent tst-hsearch \
 tests-internal := tst-atomic tst-atomic-long tst-allocate_once
 tests-static := tst-empty
 
+tests-internal += tst-ldbl-warn
+
 ifeq ($(run-built-tests),yes)
 tests-special += $(objpfx)tst-error1-mem.out \
   $(objpfx)tst-allocate_once-mem.out
+
+tests-special += $(objpfx)tst-ldbl-warn.out
 endif
 
+$(objpfx)tst-ldbl-warn.out: \
+  tst-ldbl-warn.sh $(objpfx)tst-ldbl-warn
+	$(SHELL) $^ '$(test-program-prefix)' $@; \
+	$(evaluate-test)
+
 CFLAGS-select.c += -fexceptions -fasynchronous-unwind-tables
 CFLAGS-tsearch.c += $(uses-callbacks)
 CFLAGS-lsearch.c += $(uses-callbacks)
diff --git a/misc/tst-ldbl-warn.c b/misc/tst-ldbl-warn.c
new file mode 100644
index 0000000000..db771b5c59
--- /dev/null
+++ b/misc/tst-ldbl-warn.c
@@ -0,0 +1,61 @@ 
+/* 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 <support/check.h>
+
+static void
+do_test_call_varg (const char *format, ...)
+{
+  va_list args;
+
+  va_start (args, format);
+  vwarn (format, args);
+  va_end (args);
+
+  va_start (args, format);
+  vwarnx (format, args);
+  va_end (args);
+}
+
+static void
+do_test_call_rarg (const char *format, long double ld1, double d1,
+		   long double ld2, double d2)
+{
+  warn (format, ld1, d1, ld2, d2);
+  warnx (format, ld1, d1, ld2, d2);
+}
+
+static int
+do_test (void)
+{
+  double d1 = -2;
+  double d2 = -4;
+  long double ld1 = -1;
+  long double ld2 = -3;
+
+  /* Print in decimal notation.  */
+  do_test_call_rarg ("%Lf - %f - %Lf - %f", ld1, d1, ld2, d2);
+  do_test_call_varg ("%Lf - %f - %Lf - %f", ld1, d1, ld2, d2);
+
+  return 0;
+}
+
+#include <support/test-driver.c>
diff --git a/misc/tst-ldbl-warn.sh b/misc/tst-ldbl-warn.sh
new file mode 100644
index 0000000000..5827be9dbe
--- /dev/null
+++ b/misc/tst-ldbl-warn.sh
@@ -0,0 +1,48 @@ 
+#!/bin/sh
+# 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/>.
+
+set -e
+
+test_program=$1; shift
+test_program_prefix=$1; shift
+test_program_output=$1; shift
+
+status=0
+
+${test_program_prefix} \
+  ${test_program} \
+  2> ${test_program_output} || status=1
+
+# Remove the program name from the output, because checking the program
+# name is out of the scope of this test case.
+sed -i "s/.*tst-[^:]*: //" ${test_program_output}
+
+cat <<'EOF' |
+-1.000000 - -2.000000 - -3.000000 - -4.000000: Success
+-1.000000 - -2.000000 - -3.000000 - -4.000000
+-1.000000 - -2.000000 - -3.000000 - -4.000000: Success
+-1.000000 - -2.000000 - -3.000000 - -4.000000
+EOF
+cmp - ${test_program_output} > /dev/null 2>&1 ||
+{
+  status=1
+  echo "*** output comparison failed"
+}
+
+exit $status