bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

totalorder* tests: Refactor


From: Bruno Haible
Subject: totalorder* tests: Refactor
Date: Wed, 04 Oct 2023 13:39:21 +0200

In the test-totalorder.c file, the destructive array manipulations are a bit
tedious to understand. With a functional style, the code becomes clearer
(and possibly reusable in other tests).


2023-10-04  Bruno Haible  <bruno@clisp.org>

        totalorder* tests: Refactor.
        * tests/test-totalorder.c (positive_nan, negative_nan): New functions,
        extracted from main.
        (main): Use them when initializing the array.

diff --git a/tests/test-totalorder.c b/tests/test-totalorder.c
index 1fc1b84568..b4a121c2a2 100644
--- a/tests/test-totalorder.c
+++ b/tests/test-totalorder.c
@@ -31,25 +31,37 @@
 # define TOTALORDER_TYPE double
 #endif
 
+/* Return a quiet NaN with sign bit == 0.  */
+static TOTALORDER_TYPE
+positive_nan ()
+{
+  /* 'volatile' works around a GCC bug:
+     <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111655>  */
+  TOTALORDER_TYPE volatile nan = TOTALORDER_NAN ();
+  return (signbit (nan) ? - nan : nan);
+}
+
+/* Return a quiet NaN with sign bit == 1.  */
+static TOTALORDER_TYPE
+negative_nan ()
+{
+  /* 'volatile' works around a GCC bug:
+     <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111655>  */
+  TOTALORDER_TYPE volatile nan = TOTALORDER_NAN ();
+  return (signbit (nan) ? nan : - nan);
+}
+
 int
 main ()
 {
   TOTALORDER_TYPE x[] =
     {
-      -TOTALORDER_NAN (), -TOTALORDER_INF (), -1e37, -1, -1e-5,
+      negative_nan (), -TOTALORDER_INF (), -1e37, -1, -1e-5,
       TOTALORDER_MINUS_ZERO, 0,
-      1e-5, 1, 1e37, TOTALORDER_INF (), TOTALORDER_NAN ()
+      1e-5, 1, 1e37, TOTALORDER_INF (), positive_nan ()
     };
   int n = sizeof x / sizeof *x;
 
-  /* TOTALORDER_NAN () yields a NaN of unknown sign, so fix the
-     first NaN to be negative and the last NaN to be nonnegative.
-     Do this via volatile accesses, to work around GCC bug 111655.  */
-  TOTALORDER_TYPE volatile a = x[0], z = x[n - 1];
-  if (! signbit (a)) a = -a;
-  if (  signbit (z)) z = -z;
-  x[0] = a, x[n - 1] = z;
-
   for (int i = 0; i < n; i++)
     for (int j = 0; j < n; j++)
       ASSERT (!!TOTALORDER (&x[i], &x[j]) == (i <= j));






reply via email to

[Prev in Thread] Current Thread [Next in Thread]