cbrt* tests: More tests.
authorBruno Haible <bruno@clisp.org>
Mon, 5 Mar 2012 13:16:38 +0000 (14:16 +0100)
committerBruno Haible <bruno@clisp.org>
Mon, 5 Mar 2012 13:16:38 +0000 (14:16 +0100)
* tests/test-cbrt.h: New file.
* tests/test-cbrt.c: Include <float.h> and test-cbrt.h.
(main): Invoke test_function.
* tests/test-cbrtf.c: Include <float.h> and test-cbrt.h.
(main): Invoke test_function.
* tests/test-cbrtl.c: Include <float.h> 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
modules/cbrt-tests
modules/cbrtf-tests
modules/cbrtl-tests
tests/test-cbrt.c
tests/test-cbrt.h [new file with mode: 0644]
tests/test-cbrtf.c
tests/test-cbrtl.c

index 09b6051..740e113 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,23 @@
 2012-03-05  Bruno Haible  <bruno@clisp.org>
 
+       cbrt* tests: More tests.
+       * tests/test-cbrt.h: New file.
+       * tests/test-cbrt.c: Include <float.h> and test-cbrt.h.
+       (main): Invoke test_function.
+       * tests/test-cbrtf.c: Include <float.h> and test-cbrt.h.
+       (main): Invoke test_function.
+       * tests/test-cbrtl.c: Include <float.h> 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  <bruno@clisp.org>
+
        hypot* tests: More tests.
        * tests/test-hypot.h: New file, partially extracted from
        tests/test-hypotl.c.
index 47248e0..9384e2a 100644 (file)
@@ -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@
index 31f328d..56f32ce 100644 (file)
@@ -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@
index 1e85c85..03fce4e 100644 (file)
@@ -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@
index e1decb5..562c08c 100644 (file)
 #include "signature.h"
 SIGNATURE_CHECK (cbrt, double, (double));
 
+#include <float.h>
+
 #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 (file)
index 0000000..2240393
--- /dev/null
@@ -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 <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++)
+    {
+      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;
index 53385c7..08c5b88 100644 (file)
 #include "signature.h"
 SIGNATURE_CHECK (cbrtf, float, (float));
 
+#include <float.h>
+
 #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;
 }
index 5f1f0ae..79eddf3 100644 (file)
 #include "signature.h"
 SIGNATURE_CHECK (cbrtl, long double, (long double));
 
+#include <float.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 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;
 }