From 4dda933b73d7768c4824f13c46fef6fd0e9dfb73 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 5 Mar 2012 14:16:38 +0100 Subject: [PATCH] cbrt* tests: More tests. * tests/test-cbrt.h: New file. * tests/test-cbrt.c: Include and test-cbrt.h. (main): Invoke test_function. * tests/test-cbrtf.c: Include and test-cbrt.h. (main): Invoke test_function. * tests/test-cbrtl.c: Include and test-cbrt.h. (main): Invoke test_function. * modules/cbrt-tests (Files): Add tests/test-cbrt.h, tests/randomd.c. (Makefile.am): Add randomd.c to test_cbrt_SOURCES. * modules/cbrtf-tests (Files): Add tests/test-cbrt.h, tests/randomf.c. (Makefile.am): Add randomf.c to test_cbrtf_SOURCES. * modules/cbrtl-tests (Files): Add tests/test-cbrt.h, tests/randoml.c. (Depends-on): Add 'float'. (Makefile.am): Add randoml.c to test_cbrtl_SOURCES. --- ChangeLog | 18 ++++++++++++++++ modules/cbrt-tests | 3 +++ modules/cbrtf-tests | 3 +++ modules/cbrtl-tests | 4 ++++ tests/test-cbrt.c | 12 +++++++++-- tests/test-cbrt.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test-cbrtf.c | 12 +++++++++-- tests/test-cbrtl.c | 12 +++++++++-- 8 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 tests/test-cbrt.h diff --git a/ChangeLog b/ChangeLog index 09b6051e0..740e11315 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,23 @@ 2012-03-05 Bruno Haible + cbrt* tests: More tests. + * tests/test-cbrt.h: New file. + * tests/test-cbrt.c: Include and test-cbrt.h. + (main): Invoke test_function. + * tests/test-cbrtf.c: Include and test-cbrt.h. + (main): Invoke test_function. + * tests/test-cbrtl.c: Include and test-cbrt.h. + (main): Invoke test_function. + * modules/cbrt-tests (Files): Add tests/test-cbrt.h, tests/randomd.c. + (Makefile.am): Add randomd.c to test_cbrt_SOURCES. + * modules/cbrtf-tests (Files): Add tests/test-cbrt.h, tests/randomf.c. + (Makefile.am): Add randomf.c to test_cbrtf_SOURCES. + * modules/cbrtl-tests (Files): Add tests/test-cbrt.h, tests/randoml.c. + (Depends-on): Add 'float'. + (Makefile.am): Add randoml.c to test_cbrtl_SOURCES. + +2012-03-05 Bruno Haible + hypot* tests: More tests. * tests/test-hypot.h: New file, partially extracted from tests/test-hypotl.c. diff --git a/modules/cbrt-tests b/modules/cbrt-tests index 47248e056..9384e2a7c 100644 --- a/modules/cbrt-tests +++ b/modules/cbrt-tests @@ -1,7 +1,9 @@ Files: tests/test-cbrt.c +tests/test-cbrt.h tests/signature.h tests/macros.h +tests/randomd.c Depends-on: @@ -10,4 +12,5 @@ configure.ac: Makefile.am: TESTS += test-cbrt check_PROGRAMS += test-cbrt +test_cbrt_SOURCES = test-cbrt.c randomd.c test_cbrt_LDADD = $(LDADD) @CBRT_LIBM@ diff --git a/modules/cbrtf-tests b/modules/cbrtf-tests index 31f328d29..56f32ce37 100644 --- a/modules/cbrtf-tests +++ b/modules/cbrtf-tests @@ -1,7 +1,9 @@ Files: tests/test-cbrtf.c +tests/test-cbrt.h tests/signature.h tests/macros.h +tests/randomf.c Depends-on: @@ -10,4 +12,5 @@ configure.ac: Makefile.am: TESTS += test-cbrtf check_PROGRAMS += test-cbrtf +test_cbrtf_SOURCES = test-cbrtf.c randomf.c test_cbrtf_LDADD = $(LDADD) @CBRTF_LIBM@ diff --git a/modules/cbrtl-tests b/modules/cbrtl-tests index 1e85c8592..03fce4e50 100644 --- a/modules/cbrtl-tests +++ b/modules/cbrtl-tests @@ -1,13 +1,17 @@ Files: tests/test-cbrtl.c +tests/test-cbrt.h tests/signature.h tests/macros.h +tests/randoml.c Depends-on: +float configure.ac: Makefile.am: TESTS += test-cbrtl check_PROGRAMS += test-cbrtl +test_cbrtl_SOURCES = test-cbrtl.c randoml.c test_cbrtl_LDADD = $(LDADD) @CBRTL_LIBM@ diff --git a/tests/test-cbrt.c b/tests/test-cbrt.c index e1decb51c..562c08cd4 100644 --- a/tests/test-cbrt.c +++ b/tests/test-cbrt.c @@ -23,10 +23,16 @@ #include "signature.h" SIGNATURE_CHECK (cbrt, 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 CBRT cbrt +#define RANDOM randomd +#include "test-cbrt.h" int main () @@ -36,5 +42,7 @@ main () y = cbrt (x); ASSERT (y >= 0.8434326653 && y <= 0.8434326654); + test_function (); + return 0; } diff --git a/tests/test-cbrt.h b/tests/test-cbrt.h new file mode 100644 index 000000000..22403931f --- /dev/null +++ b/tests/test-cbrt.h @@ -0,0 +1,59 @@ +/* Test of cbrt*() 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_(32.0) * RANDOM[i] - L_(16.0); /* -16.0 <= x <= 16.0 */ + DOUBLE y = CBRT (x); + DOUBLE err = y * y * y - x; + ASSERT (err > - L_(4.0) * L_(16.0) / TWO_MANT_DIG + && err < L_(4.0) * L_(16.0) / TWO_MANT_DIG); + } + + for (i = 0; i < SIZEOF (RANDOM) / 5; i++) + for (j = 0; j < SIZEOF (RANDOM) / 5; j++) + { + DOUBLE x = L_(32.0) * RANDOM[i] - L_(16.0); /* -16.0 <= x <= 16.0 */ + DOUBLE y = L_(32.0) * RANDOM[j] - L_(16.0); /* -16.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 = CBRT (x) * CBRT (y) * CBRT (z); + ASSERT (p > L_(1.0) - L_(8.0) / TWO_MANT_DIG + && p < L_(1.0) + L_(8.0) / TWO_MANT_DIG); + } + } +} + +volatile DOUBLE x; +DOUBLE y; diff --git a/tests/test-cbrtf.c b/tests/test-cbrtf.c index 53385c754..08c5b883d 100644 --- a/tests/test-cbrtf.c +++ b/tests/test-cbrtf.c @@ -23,10 +23,16 @@ #include "signature.h" SIGNATURE_CHECK (cbrtf, 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 CBRT cbrtf +#define RANDOM randomf +#include "test-cbrt.h" int main () @@ -36,5 +42,7 @@ main () y = cbrtf (x); ASSERT (y >= 0.84343266f && y <= 0.84343270f); + test_function (); + return 0; } diff --git a/tests/test-cbrtl.c b/tests/test-cbrtl.c index 5f1f0aefb..79eddf369 100644 --- a/tests/test-cbrtl.c +++ b/tests/test-cbrtl.c @@ -23,10 +23,16 @@ #include "signature.h" SIGNATURE_CHECK (cbrtl, long double, (long double)); +#include + #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 CBRT cbrtl +#define RANDOM randoml +#include "test-cbrt.h" int main () @@ -36,5 +42,7 @@ main () y = cbrtl (x); ASSERT (y >= 0.8434326653L && y <= 0.8434326654L); + test_function (); + return 0; } -- 2.11.0