tests: Refactor functions for quiet NaNs.
authorBruno Haible <bruno@clisp.org>
Sat, 7 Oct 2023 13:09:23 +0000 (15:09 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 7 Oct 2023 13:09:23 +0000 (15:09 +0200)
* tests/qnan.h: New file, based on tests/totalorder.c.
* tests/nan.h: Add double-inclusion guard.
* tests/test-signbit.c: Include qnan.h.
(test_signbitf, test_signbitd, test_signbitl): Simplify.
* tests/test-totalorder.c: Include qnan.h instead of NaN.h.
(TOTALORDER_NAN): Remove macro.
(TOTALORDER_POSITIVE_NAN, TOTALORDER_NEGATIVE_NAN): New macros.
(positive_nan, negative_nan): Remove functions.
(main): Update.
* tests/test-totalorderf.c (TOTALORDER_NAN): Remove macro.
(TOTALORDER_POSITIVE_NAN, TOTALORDER_NEGATIVE_NAN): New macros.
* tests/test-totalorderl.c (TOTALORDER_NAN): Remove macro.
(TOTALORDER_POSITIVE_NAN, TOTALORDER_NEGATIVE_NAN): New macros.
* modules/signbit-tests (Files): Add tests/nan.h, tests/qnan.h.
* modules/totalorder-tests (Files): Add tests/qnan.h.
(Depends-on): Add signbit.
* modules/totalorderf-tests (Files): Add tests/qnan.h.
(Depends-on): Add signbit.
* modules/totalorderl-tests (Files): Add tests/qnan.h.
(Depends-on): Add signbit.

ChangeLog
modules/signbit-tests
modules/totalorder-tests
modules/totalorderf-tests
modules/totalorderl-tests
tests/nan.h
tests/qnan.h [new file with mode: 0644]
tests/test-signbit.c
tests/test-totalorder.c
tests/test-totalorderf.c
tests/test-totalorderl.c

index 94f8906aef024f28b34fb9554cc44b2f77f64dce..0a0a6a3338d7e4c0544f2bbbed55b49f148bbf67 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,27 @@
+2023-10-07  Bruno Haible  <bruno@clisp.org>
+
+       tests: Refactor functions for quiet NaNs.
+       * tests/qnan.h: New file, based on tests/totalorder.c.
+       * tests/nan.h: Add double-inclusion guard.
+       * tests/test-signbit.c: Include qnan.h.
+       (test_signbitf, test_signbitd, test_signbitl): Simplify.
+       * tests/test-totalorder.c: Include qnan.h instead of NaN.h.
+       (TOTALORDER_NAN): Remove macro.
+       (TOTALORDER_POSITIVE_NAN, TOTALORDER_NEGATIVE_NAN): New macros.
+       (positive_nan, negative_nan): Remove functions.
+       (main): Update.
+       * tests/test-totalorderf.c (TOTALORDER_NAN): Remove macro.
+       (TOTALORDER_POSITIVE_NAN, TOTALORDER_NEGATIVE_NAN): New macros.
+       * tests/test-totalorderl.c (TOTALORDER_NAN): Remove macro.
+       (TOTALORDER_POSITIVE_NAN, TOTALORDER_NEGATIVE_NAN): New macros.
+       * modules/signbit-tests (Files): Add tests/nan.h, tests/qnan.h.
+       * modules/totalorder-tests (Files): Add tests/qnan.h.
+       (Depends-on): Add signbit.
+       * modules/totalorderf-tests (Files): Add tests/qnan.h.
+       (Depends-on): Add signbit.
+       * modules/totalorderl-tests (Files): Add tests/qnan.h.
+       (Depends-on): Add signbit.
+
 2023-10-07  Bruno Haible  <bruno@clisp.org>
 
        totalorder*: Fix compilation error on glibc 2.25..2.30.
index 65d5602b903ebdbbe11f91092f7f704b1d5beb45..9465de790bcf254dd9d2ef504114ee140e9f3036 100644 (file)
@@ -2,6 +2,8 @@ Files:
 tests/test-signbit.c
 tests/minus-zero.h
 tests/infinity.h
+tests/nan.h
+tests/qnan.h
 tests/macros.h
 m4/exponentf.m4
 m4/exponentd.m4
index fbcfb11a99d891b6f86ba84f592b4af99ff6bf68..43eff42e7afdb46e7c6221f24270e60b3bf9f4c4 100644 (file)
@@ -3,9 +3,11 @@ tests/test-totalorder.c
 tests/minus-zero.h
 tests/infinity.h
 tests/nan.h
+tests/qnan.h
 tests/macros.h
 
 Depends-on:
+signbit
 
 configure.ac:
 
index e76c9fda388f9151b7f63cf52822878304f8d744..581e3e8cc27628dd40da7e00359554993845a209 100644 (file)
@@ -4,9 +4,11 @@ tests/test-totalorder.c
 tests/minus-zero.h
 tests/infinity.h
 tests/nan.h
+tests/qnan.h
 tests/macros.h
 
 Depends-on:
+signbit
 
 configure.ac:
 
index cfc50e21e96a9516c67fb053b2f25e51a8fa3792..c443fcc525fc082a93172074c04ba37fa507bec8 100644 (file)
@@ -4,9 +4,11 @@ tests/test-totalorder.c
 tests/minus-zero.h
 tests/infinity.h
 tests/nan.h
+tests/qnan.h
 tests/macros.h
 
 Depends-on:
+signbit
 
 configure.ac:
 
index 05f141d2a900c2f47a6582f357948d35682ba148..3ba41d8de9b368a2bed731109c7c363f316dbb7a 100644 (file)
@@ -1,4 +1,4 @@
-/* Macros for not-a-number.
+/* Macros for quiet not-a-number.
    Copyright (C) 2007-2023 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
@@ -14,6 +14,9 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
 
+#ifndef _TESTS_NAN_H
+#define _TESTS_NAN_H
+
 
 /* IBM z/OS supports both hexadecimal and IEEE floating-point formats. The
    former does not support NaN and its isnan() implementation returns zero
@@ -83,3 +86,6 @@ NaNl ()
 #else
 # define NaNl() (0.0L / 0.0L)
 #endif
+
+
+#endif /* _TESTS_NAN_H */
diff --git a/tests/qnan.h b/tests/qnan.h
new file mode 100644 (file)
index 0000000..7006699
--- /dev/null
@@ -0,0 +1,82 @@
+/* Macros for quiet not-a-number.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+#include <math.h>
+
+#include "nan.h"
+
+
+/* Returns a quiet 'float' NaN with sign bit == 0.  */
+_GL_UNUSED static float
+positive_NaNf ()
+{
+  /* 'volatile' works around a GCC bug:
+     <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111655>  */
+  float volatile nan = NaNf ();
+  return (signbit (nan) ? - nan : nan);
+}
+
+/* Returns a quiet 'float' NaN with sign bit == 1.  */
+_GL_UNUSED static float
+negative_NaNf ()
+{
+  /* 'volatile' works around a GCC bug:
+     <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111655>  */
+  float volatile nan = NaNf ();
+  return (signbit (nan) ? nan : - nan);
+}
+
+
+/* Returns a quiet 'double' NaN with sign bit == 0.  */
+_GL_UNUSED static double
+positive_NaNd ()
+{
+  /* 'volatile' works around a GCC bug:
+     <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111655>  */
+  double volatile nan = NaNd ();
+  return (signbit (nan) ? - nan : nan);
+}
+
+/* Returns a quiet 'double' NaN with sign bit == 1.  */
+_GL_UNUSED static double
+negative_NaNd ()
+{
+  /* 'volatile' works around a GCC bug:
+     <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111655>  */
+  double volatile nan = NaNd ();
+  return (signbit (nan) ? nan : - nan);
+}
+
+
+/* Returns a quiet 'long double' NaN with sign bit == 0.  */
+_GL_UNUSED static long double
+positive_NaNl ()
+{
+  /* 'volatile' works around a GCC bug:
+     <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111655>  */
+  long double volatile nan = NaNl ();
+  return (signbit (nan) ? - nan : nan);
+}
+
+/* Returns a quiet 'long double' NaN with sign bit == 1.  */
+_GL_UNUSED static long double
+negative_NaNl ()
+{
+  /* 'volatile' works around a GCC bug:
+     <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111655>  */
+  long double volatile nan = NaNl ();
+  return (signbit (nan) ? nan : - nan);
+}
index 3d2fae405b53a1ff9788ce952d6b086a27d9614e..2c1daccef286ab475c5ddd0643e6230930a70d24 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "minus-zero.h"
 #include "infinity.h"
+#include "qnan.h"
 #include "macros.h"
 
 float zerof = 0.0f;
@@ -56,17 +57,8 @@ test_signbitf ()
   ASSERT (!signbit (Infinityf ()));
   ASSERT (signbit (- Infinityf ()));
   /* Quiet NaN.  */
-  {
-    float nan = zerof / zerof;
-    float pos_nan;
-    float neg_nan;
-    if (signbit (nan))
-      pos_nan = - nan, neg_nan = nan;
-    else
-      pos_nan = nan, neg_nan = - nan;
-    ASSERT (!signbit (pos_nan));
-    ASSERT (signbit (neg_nan));
-  }
+  ASSERT (!signbit (positive_NaNf ()));
+  ASSERT (signbit (negative_NaNf ()));
 #if defined FLT_EXPBIT0_WORD && defined FLT_EXPBIT0_BIT
   /* Signalling NaN.  */
   {
@@ -111,17 +103,8 @@ test_signbitd ()
   ASSERT (!signbit (Infinityd ()));
   ASSERT (signbit (- Infinityd ()));
   /* Quiet NaN.  */
-  {
-    double nan = zerod / zerod;
-    double pos_nan;
-    double neg_nan;
-    if (signbit (nan))
-      pos_nan = - nan, neg_nan = nan;
-    else
-      pos_nan = nan, neg_nan = - nan;
-    ASSERT (!signbit (pos_nan));
-    ASSERT (signbit (neg_nan));
-  }
+  ASSERT (!signbit (positive_NaNd ()));
+  ASSERT (signbit (negative_NaNd ()));
 #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
   /* Signalling NaN.  */
   {
@@ -164,17 +147,8 @@ test_signbitl ()
   ASSERT (!signbit (Infinityl ()));
   ASSERT (signbit (- Infinityl ()));
   /* Quiet NaN.  */
-  {
-    long double nan = zerol / zerol;
-    long double pos_nan;
-    long double neg_nan;
-    if (signbit (nan))
-      pos_nan = - nan, neg_nan = nan;
-    else
-      pos_nan = nan, neg_nan = - nan;
-    ASSERT (!signbit (pos_nan));
-    ASSERT (signbit (neg_nan));
-  }
+  ASSERT (!signbit (positive_NaNl ()));
+  ASSERT (signbit (negative_NaNl ()));
 #if defined LDBL_EXPBIT0_WORD && defined LDBL_EXPBIT0_BIT
   /* Signalling NaN.  */
   {
index b4a121c2a26c277a9f250c1b5946e1c231f04e1d..b63c7c2211695cc92bc8f102a2c901d852d62a17 100644 (file)
 #include "infinity.h"
 #include "macros.h"
 #include "minus-zero.h"
-#include "nan.h"
+#include "qnan.h"
 
 #ifndef TOTALORDER
 # define TOTALORDER totalorder
 # define TOTALORDER_INF Infinityd
 # define TOTALORDER_MINUS_ZERO minus_zerod
-# define TOTALORDER_NAN NaNd
+# define TOTALORDER_POSITIVE_NAN positive_NaNd
+# define TOTALORDER_NEGATIVE_NAN negative_NaNd
 # 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[] =
     {
-      negative_nan (), -TOTALORDER_INF (), -1e37, -1, -1e-5,
+      TOTALORDER_NEGATIVE_NAN (), -TOTALORDER_INF (), -1e37, -1, -1e-5,
       TOTALORDER_MINUS_ZERO, 0,
-      1e-5, 1, 1e37, TOTALORDER_INF (), positive_nan ()
+      1e-5, 1, 1e37, TOTALORDER_INF (), TOTALORDER_POSITIVE_NAN ()
     };
   int n = sizeof x / sizeof *x;
 
index 2dcb47aca412865766d23f3058511a200b725f9b..5fd27031c0403338ffabaaabf08422ed5f65e094 100644 (file)
@@ -1,6 +1,7 @@
 #define TOTALORDER totalorderf
 #define TOTALORDER_INF Infinityf
 #define TOTALORDER_MINUS_ZERO minus_zerof
-#define TOTALORDER_NAN NaNf
+#define TOTALORDER_POSITIVE_NAN positive_NaNf
+#define TOTALORDER_NEGATIVE_NAN negative_NaNf
 #define TOTALORDER_TYPE float
 #include "test-totalorder.c"
index ac454ad848b6ab0049eb94ae5a1417c46f5aef4e..c8f1fbd4b622ce831ba4f93988c82ebedde97242 100644 (file)
@@ -1,6 +1,7 @@
 #define TOTALORDER totalorderl
 #define TOTALORDER_INF Infinityl
 #define TOTALORDER_MINUS_ZERO minus_zerol
-#define TOTALORDER_NAN NaNl
+#define TOTALORDER_POSITIVE_NAN positive_NaNl
+#define TOTALORDER_NEGATIVE_NAN negative_NaNl
 #define TOTALORDER_TYPE long double
 #include "test-totalorder.c"