fmod* tests: More tests.
authorBruno Haible <bruno@clisp.org>
Sun, 4 Mar 2012 19:59:05 +0000 (20:59 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 4 Mar 2012 19:59:05 +0000 (20:59 +0100)
* tests/test-fmod.h (my_ldexp): New function.
(test_function): Reduce amount of random numbers to test. Add tests
of very large quotients x / y.
* tests/test-fmod.c (MAX_EXP): New macro.
* tests/test-fmodf.c (MAX_EXP): Likewise.
* tests/test-fmodl.c (MAX_EXP): Likewise.

ChangeLog
tests/test-fmod.c
tests/test-fmod.h
tests/test-fmodf.c
tests/test-fmodl.c

index c55974f..f840f5c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2012-03-04  Bruno Haible  <bruno@clisp.org>
 
+       fmod* tests: More tests.
+       * tests/test-fmod.h (my_ldexp): New function.
+       (test_function): Reduce amount of random numbers to test. Add tests
+       of very large quotients x / y.
+       * tests/test-fmod.c (MAX_EXP): New macro.
+       * tests/test-fmodf.c (MAX_EXP): Likewise.
+       * tests/test-fmodl.c (MAX_EXP): Likewise.
+
+2012-03-04  Bruno Haible  <bruno@clisp.org>
+
        fmod, fmodl: Fix computation for large quotients x / y.
        * lib/fmod.c: Completely rewritten.
        * lib/fmodl.c (fmodl): Use implementation of fmod.c with
index 27a23e3..36799f1 100644 (file)
@@ -30,6 +30,7 @@ SIGNATURE_CHECK (fmod, double, (double, double));
 #define DOUBLE double
 #define L_(literal) literal
 #define MANT_DIG DBL_MANT_DIG
+#define MAX_EXP DBL_MAX_EXP
 #define FMOD fmod
 #define RANDOM randomd
 #include "test-fmod.h"
index 3092860..a513857 100644 (file)
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+static DOUBLE
+my_ldexp (DOUBLE x, int d)
+{
+  for (; d > 0; d--)
+    x *= L_(2.0);
+  for (; d < 0; d++)
+    x *= L_(0.5);
+  return x;
+}
+
 static void
 test_function (void)
 {
@@ -30,8 +40,8 @@ test_function (void)
     * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
 
   /* Randomized tests.  */
-  for (i = 0; i < SIZEOF (RANDOM); i++)
-    for (j = 0; j < SIZEOF (RANDOM); j++)
+  for (i = 0; i < SIZEOF (RANDOM) / 5; i++)
+    for (j = 0; j < SIZEOF (RANDOM) / 5; j++)
       {
         DOUBLE x = L_(16.0) * RANDOM[i]; /* 0.0 <= x <= 16.0 */
         DOUBLE y = RANDOM[j]; /* 0.0 <= y < 1.0 */
@@ -53,8 +63,8 @@ test_function (void)
           }
       }
 
-  for (i = 0; i < SIZEOF (RANDOM); i++)
-    for (j = 0; j < SIZEOF (RANDOM); j++)
+  for (i = 0; i < SIZEOF (RANDOM) / 5; i++)
+    for (j = 0; j < SIZEOF (RANDOM) / 5; j++)
       {
         DOUBLE x = L_(1.0e9) * RANDOM[i]; /* 0.0 <= x <= 10^9 */
         DOUBLE y = RANDOM[j]; /* 0.0 <= y < 1.0 */
@@ -87,6 +97,25 @@ test_function (void)
                         && z < - y + L_(2.0) * L_(1.0e9) / TWO_MANT_DIG));
           }
       }
+
+  {
+    int large_exp = (MAX_EXP - 1 < 1000 ? MAX_EXP - 1 : 1000);
+    DOUBLE large = my_ldexp (L_(1.0), large_exp); /* = 2^large_exp */
+    for (i = 0; i < SIZEOF (RANDOM) / 10; i++)
+      for (j = 0; j < SIZEOF (RANDOM) / 10; j++)
+        {
+          DOUBLE x = large * RANDOM[i]; /* 0.0 <= x <= 2^large_exp */
+          DOUBLE y = RANDOM[j]; /* 0.0 <= y < 1.0 */
+          if (y > L_(0.0))
+            {
+              DOUBLE z = FMOD (x, y);
+              /* Regardless how large the rounding errors are, the result
+                 must be >= 0, < y.  */
+              ASSERT (z >= L_(0.0));
+              ASSERT (z < y);
+            }
+        }
+  }
 }
 
 volatile DOUBLE x;
index fcb830b..f968b09 100644 (file)
@@ -30,6 +30,7 @@ SIGNATURE_CHECK (fmodf, float, (float, float));
 #define DOUBLE float
 #define L_(literal) literal##f
 #define MANT_DIG FLT_MANT_DIG
+#define MAX_EXP FLT_MAX_EXP
 #define FMOD fmodf
 #define RANDOM randomf
 #include "test-fmod.h"
index 53054d1..95a38b8 100644 (file)
@@ -31,6 +31,7 @@ SIGNATURE_CHECK (fmodl, long double, (long double, long double));
 #define DOUBLE long double
 #define L_(literal) literal##L
 #define MANT_DIG LDBL_MANT_DIG
+#define MAX_EXP LDBL_MAX_EXP
 #define FMOD fmodl
 #define RANDOM randoml
 #include "test-fmod.h"