From c5bc2aeaf6f4ca2d1e25f58b78a655f6211b50a6 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 5 Mar 2012 02:14:30 +0100 Subject: [PATCH] sqrt* tests: More tests. * tests/test-sqrt.h: New file. * tests/test-sqrt.c: Include and test-sqrt.h. (main): Invoke test_function. * tests/test-sqrtf.c: Include and test-sqrt.h. (main): Invoke test_function. * tests/test-sqrtl.c: Include and test-sqrt.h. (main): Invoke test_function. * modules/sqrt-tests (Files): Add tests/test-sqrt.h, tests/randomd.c. (Makefile.am): Add randomd.c to test_sqrt_SOURCES. * modules/sqrtf-tests (Files): Add tests/test-sqrt.h, tests/randomf.c. (Makefile.am): Add randomf.c to test_sqrtf_SOURCES. * modules/sqrtl-tests (Files): Add tests/test-sqrt.h, tests/randoml.c. (Depends-on): Add 'float'. (Makefile.am): Add randoml.c to test_sqrtl_SOURCES. --- ChangeLog | 20 ++++++++++++++- modules/sqrt-tests | 3 +++ modules/sqrtf-tests | 3 +++ modules/sqrtl-tests | 4 +++ tests/test-sqrt.c | 12 +++++++-- tests/test-sqrt.h | 59 +++++++++++++++++++++++++++++++++++++++++++++ tests/test-sqrtf.c | 12 +++++++-- tests/test-sqrtl.c | 12 +++++++-- 8 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 tests/test-sqrt.h diff --git a/ChangeLog b/ChangeLog index 7a01288dd7..4ec700fd59 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,22 @@ -2012-03-03 Bruno Haible +2012-03-04 Bruno Haible + + sqrt* tests: More tests. + * tests/test-sqrt.h: New file. + * tests/test-sqrt.c: Include and test-sqrt.h. + (main): Invoke test_function. + * tests/test-sqrtf.c: Include and test-sqrt.h. + (main): Invoke test_function. + * tests/test-sqrtl.c: Include and test-sqrt.h. + (main): Invoke test_function. + * modules/sqrt-tests (Files): Add tests/test-sqrt.h, tests/randomd.c. + (Makefile.am): Add randomd.c to test_sqrt_SOURCES. + * modules/sqrtf-tests (Files): Add tests/test-sqrt.h, tests/randomf.c. + (Makefile.am): Add randomf.c to test_sqrtf_SOURCES. + * modules/sqrtl-tests (Files): Add tests/test-sqrt.h, tests/randoml.c. + (Depends-on): Add 'float'. + (Makefile.am): Add randoml.c to test_sqrtl_SOURCES. + +2012-03-04 Bruno Haible remainder* tests: More tests. * tests/test-remainder.h: New file, based on tests/test-fmod.h. diff --git a/modules/sqrt-tests b/modules/sqrt-tests index 86d8176159..dad976d7c9 100644 --- a/modules/sqrt-tests +++ b/modules/sqrt-tests @@ -1,7 +1,9 @@ Files: tests/test-sqrt.c +tests/test-sqrt.h tests/signature.h tests/macros.h +tests/randomd.c Depends-on: @@ -10,4 +12,5 @@ configure.ac: Makefile.am: TESTS += test-sqrt check_PROGRAMS += test-sqrt +test_sqrt_SOURCES = test-sqrt.c randomd.c test_sqrt_LDADD = $(LDADD) @SQRT_LIBM@ diff --git a/modules/sqrtf-tests b/modules/sqrtf-tests index 3cd8d62c56..7b0525eb82 100644 --- a/modules/sqrtf-tests +++ b/modules/sqrtf-tests @@ -1,7 +1,9 @@ Files: tests/test-sqrtf.c +tests/test-sqrt.h tests/signature.h tests/macros.h +tests/randomf.c Depends-on: @@ -10,4 +12,5 @@ configure.ac: Makefile.am: TESTS += test-sqrtf check_PROGRAMS += test-sqrtf +test_sqrtf_SOURCES = test-sqrtf.c randomf.c test_sqrtf_LDADD = $(LDADD) @SQRTF_LIBM@ diff --git a/modules/sqrtl-tests b/modules/sqrtl-tests index eab018b718..68c4068f00 100644 --- a/modules/sqrtl-tests +++ b/modules/sqrtl-tests @@ -1,14 +1,18 @@ Files: tests/test-sqrtl.c +tests/test-sqrt.h tests/signature.h tests/macros.h +tests/randoml.c Depends-on: fpucw +float configure.ac: Makefile.am: TESTS += test-sqrtl check_PROGRAMS += test-sqrtl +test_sqrtl_SOURCES = test-sqrtl.c randoml.c test_sqrtl_LDADD = $(LDADD) @SQRTL_LIBM@ diff --git a/tests/test-sqrt.c b/tests/test-sqrt.c index 0de7051e69..b317eb7ccf 100644 --- a/tests/test-sqrt.c +++ b/tests/test-sqrt.c @@ -23,10 +23,16 @@ #include "signature.h" SIGNATURE_CHECK (sqrt, double, (double)); +#include + #include "macros.h" -volatile double x; -double y; +#define DOUBLE double +#define L_(literal) literal +#define MANT_DIG DBL_MANT_DIG +#define SQRT sqrt +#define RANDOM randomd +#include "test-sqrt.h" int main () @@ -36,5 +42,7 @@ main () y = sqrt (x); ASSERT (y >= 0.7745966692 && y <= 0.7745966693); + test_function (); + return 0; } diff --git a/tests/test-sqrt.h b/tests/test-sqrt.h new file mode 100644 index 0000000000..ffd5af1a7f --- /dev/null +++ b/tests/test-sqrt.h @@ -0,0 +1,59 @@ +/* Test of sqrt*() 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++) + { + DOUBLE x = L_(16.0) * RANDOM[i]; /* 0.0 <= x <= 16.0 */ + DOUBLE y = SQRT (x); + DOUBLE z = y * y - x; + ASSERT (z > - L_(16.0) / TWO_MANT_DIG + && z < L_(16.0) / TWO_MANT_DIG); + } + + for (i = 0; i < SIZEOF (RANDOM) / 4; i++) + for (j = 0; j < SIZEOF (RANDOM) / 4; j++) + { + DOUBLE x = L_(16.0) * RANDOM[i]; /* 0.0 <= x <= 16.0 */ + DOUBLE y = L_(16.0) * RANDOM[j]; /* 0.0 <= y <= 16.0 */ + if (x > L_(0.0) && y > L_(0.0)) + { + DOUBLE z = L_(1.0) / (x * y); + /* Approximately x * y * z = 1. */ + DOUBLE p = SQRT (x) * SQRT (y) * SQRT (z); + ASSERT (p > L_(1.0) - L_(4.0) / TWO_MANT_DIG + && p < L_(1.0) + L_(4.0) / TWO_MANT_DIG); + } + } +} + +volatile DOUBLE x; +DOUBLE y; diff --git a/tests/test-sqrtf.c b/tests/test-sqrtf.c index 514eb7e5f2..40abde6cf5 100644 --- a/tests/test-sqrtf.c +++ b/tests/test-sqrtf.c @@ -23,10 +23,16 @@ #include "signature.h" SIGNATURE_CHECK (sqrtf, float, (float)); +#include + #include "macros.h" -volatile float x; -float y; +#define DOUBLE float +#define L_(literal) literal##f +#define MANT_DIG FLT_MANT_DIG +#define SQRT sqrtf +#define RANDOM randomf +#include "test-sqrt.h" int main () @@ -36,5 +42,7 @@ main () y = sqrtf (x); ASSERT (y >= 0.7745966f && y <= 0.7745967f); + test_function (); + return 0; } diff --git a/tests/test-sqrtl.c b/tests/test-sqrtl.c index fa2889cb99..23dcfe43ae 100644 --- a/tests/test-sqrtl.c +++ b/tests/test-sqrtl.c @@ -23,11 +23,17 @@ #include "signature.h" SIGNATURE_CHECK (sqrtl, long double, (long double)); +#include + #include "fpucw.h" #include "macros.h" -volatile long double x; -long double y; +#define DOUBLE long double +#define L_(literal) literal##L +#define MANT_DIG DBL_MANT_DIG +#define SQRT sqrtl +#define RANDOM randoml +#include "test-sqrt.h" int main () @@ -41,5 +47,7 @@ main () y = sqrtl (x); ASSERT (y >= 0.7745966692L && y <= 0.7745966693L); + test_function (); + return 0; } -- 2.39.5