sqrt* tests: More tests.
authorBruno Haible <bruno@clisp.org>
Mon, 5 Mar 2012 01:14:30 +0000 (02:14 +0100)
committerBruno Haible <bruno@clisp.org>
Mon, 5 Mar 2012 01:14:53 +0000 (02:14 +0100)
* tests/test-sqrt.h: New file.
* tests/test-sqrt.c: Include <float.h> and test-sqrt.h.
(main): Invoke test_function.
* tests/test-sqrtf.c: Include <float.h> and test-sqrt.h.
(main): Invoke test_function.
* tests/test-sqrtl.c: Include <float.h> 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
modules/sqrt-tests
modules/sqrtf-tests
modules/sqrtl-tests
tests/test-sqrt.c
tests/test-sqrt.h [new file with mode: 0644]
tests/test-sqrtf.c
tests/test-sqrtl.c

index 7a01288..4ec700f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,22 @@
-2012-03-03  Bruno Haible  <bruno@clisp.org>
+2012-03-04  Bruno Haible  <bruno@clisp.org>
+
+       sqrt* tests: More tests.
+       * tests/test-sqrt.h: New file.
+       * tests/test-sqrt.c: Include <float.h> and test-sqrt.h.
+       (main): Invoke test_function.
+       * tests/test-sqrtf.c: Include <float.h> and test-sqrt.h.
+       (main): Invoke test_function.
+       * tests/test-sqrtl.c: Include <float.h> 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  <bruno@clisp.org>
 
        remainder* tests: More tests.
        * tests/test-remainder.h: New file, based on tests/test-fmod.h.
index 86d8176..dad976d 100644 (file)
@@ -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@
index 3cd8d62..7b0525e 100644 (file)
@@ -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@
index eab018b..68c4068 100644 (file)
@@ -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@
index 0de7051..b317eb7 100644 (file)
 #include "signature.h"
 SIGNATURE_CHECK (sqrt, 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 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 (file)
index 0000000..ffd5af1
--- /dev/null
@@ -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 <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_(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;
index 514eb7e..40abde6 100644 (file)
 #include "signature.h"
 SIGNATURE_CHECK (sqrtf, 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 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;
 }
index fa2889c..23dcfe4 100644 (file)
 #include "signature.h"
 SIGNATURE_CHECK (sqrtl, long double, (long double));
 
+#include <float.h>
+
 #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;
 }