changeset 16564:a5e3d6ba8dee

fmod* tests: More tests. * tests/test-fmod.h: New file. * tests/test-fmod.c: Include <float.h> and test-fmod.h. (main): Invoke test_function. * tests/test-fmodf.c: Include <float.h> and test-fmod.h. (main): Invoke test_function. * tests/test-fmodl.c: Include <float.h> and test-fmod.h. (main): Invoke test_function. * modules/fmod-tests (Files): Add tests/test-fmod.h, tests/randomd.c. (Makefile.am): Add randomd.c to test_fmod_SOURCES. * modules/fmodf-tests (Files): Add tests/test-fmod.h, tests/randomf.c. (Makefile.am): Add randomf.c to test_fmodf_SOURCES. * modules/fmodl-tests (Files): Add tests/test-fmod.h, tests/randoml.c. (Depends-on): Add 'float'. (Makefile.am): Add randoml.c to test_fmodl_SOURCES.
author Bruno Haible <bruno@clisp.org>
date Sat, 03 Mar 2012 18:15:16 +0100
parents 446b5d1c7986
children 0badbd4d62e4
files ChangeLog modules/fmod-tests modules/fmodf-tests modules/fmodl-tests tests/test-fmod.c tests/test-fmod.h tests/test-fmodf.c tests/test-fmodl.c
diffstat 8 files changed, 152 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2012-03-03  Bruno Haible  <bruno@clisp.org>
+
+	fmod* tests: More tests.
+	* tests/test-fmod.h: New file.
+	* tests/test-fmod.c: Include <float.h> and test-fmod.h.
+	(main): Invoke test_function.
+	* tests/test-fmodf.c: Include <float.h> and test-fmod.h.
+	(main): Invoke test_function.
+	* tests/test-fmodl.c: Include <float.h> and test-fmod.h.
+	(main): Invoke test_function.
+	* modules/fmod-tests (Files): Add tests/test-fmod.h, tests/randomd.c.
+	(Makefile.am): Add randomd.c to test_fmod_SOURCES.
+	* modules/fmodf-tests (Files): Add tests/test-fmod.h, tests/randomf.c.
+	(Makefile.am): Add randomf.c to test_fmodf_SOURCES.
+	* modules/fmodl-tests (Files): Add tests/test-fmod.h, tests/randoml.c.
+	(Depends-on): Add 'float'.
+	(Makefile.am): Add randoml.c to test_fmodl_SOURCES.
+
 2012-03-03  Bruno Haible  <bruno@clisp.org>
 
 	rint* tests: More tests.
--- a/modules/fmod-tests
+++ b/modules/fmod-tests
@@ -1,7 +1,9 @@
 Files:
 tests/test-fmod.c
+tests/test-fmod.h
 tests/signature.h
 tests/macros.h
+tests/randomd.c
 
 Depends-on:
 
@@ -10,4 +12,5 @@
 Makefile.am:
 TESTS += test-fmod
 check_PROGRAMS += test-fmod
+test_fmod_SOURCES = test-fmod.c randomd.c
 test_fmod_LDADD = $(LDADD) @FMOD_LIBM@
--- a/modules/fmodf-tests
+++ b/modules/fmodf-tests
@@ -1,7 +1,9 @@
 Files:
 tests/test-fmodf.c
+tests/test-fmod.h
 tests/signature.h
 tests/macros.h
+tests/randomf.c
 
 Depends-on:
 
@@ -10,4 +12,5 @@
 Makefile.am:
 TESTS += test-fmodf
 check_PROGRAMS += test-fmodf
+test_fmodf_SOURCES = test-fmodf.c randomf.c
 test_fmodf_LDADD = $(LDADD) @FMODF_LIBM@
--- a/modules/fmodl-tests
+++ b/modules/fmodl-tests
@@ -1,14 +1,18 @@
 Files:
 tests/test-fmodl.c
+tests/test-fmod.h
 tests/signature.h
 tests/macros.h
+tests/randoml.c
 
 Depends-on:
 fpucw
+float
 
 configure.ac:
 
 Makefile.am:
 TESTS += test-fmodl
 check_PROGRAMS += test-fmodl
+test_fmodl_SOURCES = test-fmodl.c randoml.c
 test_fmodl_LDADD = $(LDADD) @FMODL_LIBM@
--- a/tests/test-fmod.c
+++ b/tests/test-fmod.c
@@ -23,11 +23,16 @@
 #include "signature.h"
 SIGNATURE_CHECK (fmod, double, (double, double));
 
+#include <float.h>
+
 #include "macros.h"
 
-volatile double x;
-volatile double y;
-double z;
+#define DOUBLE double
+#define L_(literal) literal
+#define MANT_DIG DBL_MANT_DIG
+#define FMOD fmod
+#define RANDOM randomd
+#include "test-fmod.h"
 
 int
 main ()
@@ -38,5 +43,7 @@
   z = fmod (x, y);
   ASSERT (z >= 2.962721817 && z <= 2.962721819);
 
+  test_function ();
+
   return 0;
 }
new file mode 100644
--- /dev/null
+++ b/tests/test-fmod.h
@@ -0,0 +1,94 @@
+/* Test of fmod*() function family.
+   Copyright (C) 2012 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 <http://www.gnu.org/licenses/>.  */
+
+static void
+test_function (void)
+{
+  int i;
+  int j;
+  const DOUBLE TWO_MANT_DIG =
+    /* Assume MANT_DIG <= 5 * 31.
+       Use the identity
+         n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5).  */
+    (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
+    * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
+    * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
+    * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
+    * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
+
+  /* Randomized tests.  */
+  for (i = 0; i < SIZEOF (RANDOM); i++)
+    for (j = 0; j < SIZEOF (RANDOM); j++)
+      {
+        DOUBLE x = L_(16.0) * RANDOM[i]; /* 0.0 <= x <= 16.0 */
+        DOUBLE y = RANDOM[j]; /* 0.0 <= y < 1.0 */
+        if (y > L_(0.0))
+          {
+            DOUBLE z = FMOD (x, y);
+            ASSERT (z >= L_(0.0));
+            ASSERT (z < y);
+            z -= x - (int) (x / y) * y;
+            ASSERT (/* The common case.  */
+                    (z > - L_(16.0) / TWO_MANT_DIG
+                     && z < L_(16.0) / TWO_MANT_DIG)
+                    || /* rounding error: x / y computed too large */
+                       (z > y - L_(16.0) / TWO_MANT_DIG
+                        && z < y + L_(16.0) / TWO_MANT_DIG)
+                    || /* rounding error: x / y computed too small */
+                       (z > - y - L_(16.0) / TWO_MANT_DIG
+                        && z < - y + L_(16.0) / TWO_MANT_DIG));
+          }
+      }
+
+  for (i = 0; i < SIZEOF (RANDOM); i++)
+    for (j = 0; j < SIZEOF (RANDOM); j++)
+      {
+        DOUBLE x = L_(1.0e9) * RANDOM[i]; /* 0.0 <= x <= 10^9 */
+        DOUBLE y = RANDOM[j]; /* 0.0 <= y < 1.0 */
+        if (y > L_(0.0))
+          {
+            DOUBLE z = FMOD (x, y);
+            DOUBLE r;
+            ASSERT (z >= L_(0.0));
+            ASSERT (z < y);
+            {
+              /* Determine the quotient x / y in two steps, because it
+                 may be > 2^31.  */
+              int q1 = (int) (x / y / L_(65536.0));
+              int q2 = (int) ((x - q1 * L_(65536.0) * y) / y);
+              DOUBLE q = (DOUBLE) q1 * L_(65536.0) + (DOUBLE) q2;
+              r = x - q * y;
+            }
+            /* The absolute error of z can be up to 1e9/2^MANT_DIG.
+               The absolute error of r can also be up to 1e9/2^MANT_DIG.
+               Therefore the error of z - r can be twice as large.  */
+            z -= r;
+            ASSERT (/* The common case.  */
+                    (z > - L_(2.0) * L_(1.0e9) / TWO_MANT_DIG
+                     && z < L_(2.0) * L_(1.0e9) / TWO_MANT_DIG)
+                    || /* rounding error: x / y computed too large */
+                       (z > y - L_(2.0) * L_(1.0e9) / TWO_MANT_DIG
+                        && z < y + L_(2.0) * L_(1.0e9) / TWO_MANT_DIG)
+                    || /* rounding error: x / y computed too small */
+                       (z > - y - L_(2.0) * L_(1.0e9) / TWO_MANT_DIG
+                        && z < - y + L_(2.0) * L_(1.0e9) / TWO_MANT_DIG));
+          }
+      }
+}
+
+volatile DOUBLE x;
+volatile DOUBLE y;
+DOUBLE z;
--- a/tests/test-fmodf.c
+++ b/tests/test-fmodf.c
@@ -23,11 +23,16 @@
 #include "signature.h"
 SIGNATURE_CHECK (fmodf, float, (float, float));
 
+#include <float.h>
+
 #include "macros.h"
 
-volatile float x;
-volatile float y;
-float z;
+#define DOUBLE float
+#define L_(literal) literal##f
+#define MANT_DIG FLT_MANT_DIG
+#define FMOD fmodf
+#define RANDOM randomf
+#include "test-fmod.h"
 
 int
 main ()
@@ -38,5 +43,7 @@
   z = fmodf (x, y);
   ASSERT (z >= 2.962720f && z <= 2.962722f);
 
+  test_function ();
+
   return 0;
 }
--- a/tests/test-fmodl.c
+++ b/tests/test-fmodl.c
@@ -23,12 +23,17 @@
 #include "signature.h"
 SIGNATURE_CHECK (fmodl, long double, (long double, long double));
 
+#include <float.h>
+
 #include "fpucw.h"
 #include "macros.h"
 
-volatile long double x;
-volatile long double y;
-long double z;
+#define DOUBLE long double
+#define L_(literal) literal##L
+#define MANT_DIG LDBL_MANT_DIG
+#define FMOD fmodl
+#define RANDOM randoml
+#include "test-fmod.h"
 
 int
 main ()
@@ -43,5 +48,7 @@
   z = fmodl (x, y);
   ASSERT (z >= 2.962721817L && z <= 2.962721819L);
 
+  test_function ();
+
   return 0;
 }