From: Bruno Haible Date: Sat, 3 Mar 2012 17:15:16 +0000 (+0100) Subject: fmod* tests: More tests. X-Git-Tag: v0.1~962 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=3261e6f0337130c6bbde8db61e28c87bb901d749;p=gnulib.git fmod* tests: More tests. * tests/test-fmod.h: New file. * tests/test-fmod.c: Include and test-fmod.h. (main): Invoke test_function. * tests/test-fmodf.c: Include and test-fmod.h. (main): Invoke test_function. * tests/test-fmodl.c: Include 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. --- diff --git a/ChangeLog b/ChangeLog index bf20a7aa0a..d51da2f807 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2012-03-03 Bruno Haible + + fmod* tests: More tests. + * tests/test-fmod.h: New file. + * tests/test-fmod.c: Include and test-fmod.h. + (main): Invoke test_function. + * tests/test-fmodf.c: Include and test-fmod.h. + (main): Invoke test_function. + * tests/test-fmodl.c: Include 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 rint* tests: More tests. diff --git a/modules/fmod-tests b/modules/fmod-tests index 7d9c7f39fb..debc2e68bf 100644 --- 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 @@ configure.ac: Makefile.am: TESTS += test-fmod check_PROGRAMS += test-fmod +test_fmod_SOURCES = test-fmod.c randomd.c test_fmod_LDADD = $(LDADD) @FMOD_LIBM@ diff --git a/modules/fmodf-tests b/modules/fmodf-tests index 5f9ca3862e..b42897dd72 100644 --- 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 @@ configure.ac: Makefile.am: TESTS += test-fmodf check_PROGRAMS += test-fmodf +test_fmodf_SOURCES = test-fmodf.c randomf.c test_fmodf_LDADD = $(LDADD) @FMODF_LIBM@ diff --git a/modules/fmodl-tests b/modules/fmodl-tests index 5bd3d8fae7..9470eb85d5 100644 --- 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@ diff --git a/tests/test-fmod.c b/tests/test-fmod.c index 74ee969d8d..27a23e3026 100644 --- a/tests/test-fmod.c +++ b/tests/test-fmod.c @@ -23,11 +23,16 @@ #include "signature.h" SIGNATURE_CHECK (fmod, double, (double, double)); +#include + #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 @@ main () z = fmod (x, y); ASSERT (z >= 2.962721817 && z <= 2.962721819); + test_function (); + return 0; } diff --git a/tests/test-fmod.h b/tests/test-fmod.h new file mode 100644 index 0000000000..3092860d31 --- /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 . */ + +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; diff --git a/tests/test-fmodf.c b/tests/test-fmodf.c index 80d5e45ae7..fcb830bbd9 100644 --- a/tests/test-fmodf.c +++ b/tests/test-fmodf.c @@ -23,11 +23,16 @@ #include "signature.h" SIGNATURE_CHECK (fmodf, float, (float, float)); +#include + #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 @@ main () z = fmodf (x, y); ASSERT (z >= 2.962720f && z <= 2.962722f); + test_function (); + return 0; } diff --git a/tests/test-fmodl.c b/tests/test-fmodl.c index 44dabee5d6..53054d1343 100644 --- 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 + #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 @@ main () z = fmodl (x, y); ASSERT (z >= 2.962721817L && z <= 2.962721819L); + test_function (); + return 0; }