changeset 15842:290f37cfc069

Tests for module 'frexpf'. * modules/frexpf-tests: New file. * tests/test-frexpf.c: New file.
author Bruno Haible <bruno@clisp.org>
date Thu, 06 Oct 2011 11:29:34 +0200
parents d26d12e238e1
children bc22a56bfa43
files ChangeLog modules/frexpf-tests tests/test-frexpf.c
diffstat 3 files changed, 214 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2011-10-06  Bruno Haible  <bruno@clisp.org>
 
+	Tests for module 'frexpf'.
+	* modules/frexpf-tests: New file.
+	* tests/test-frexpf.c: New file.
+
 	New module 'frexpf'.
 	* lib/math.in.h (frexpf): New declaration.
 	* lib/frexpf.c: New file.
new file mode 100644
--- /dev/null
+++ b/modules/frexpf-tests
@@ -0,0 +1,18 @@
+Files:
+tests/test-frexpf.c
+tests/minus-zero.h
+tests/infinity.h
+tests/nan.h
+tests/signature.h
+tests/macros.h
+
+Depends-on:
+isnanf-nolibm
+signbit
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-frexpf
+check_PROGRAMS += test-frexpf
+test_frexpf_LDADD = $(LDADD) @FREXPF_LIBM@
new file mode 100644
--- /dev/null
+++ b/tests/test-frexpf.c
@@ -0,0 +1,192 @@
+/* Test of splitting a float into fraction and mantissa.
+   Copyright (C) 2007-2011 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/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
+
+#include <config.h>
+
+#include <math.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (frexpf, float, (float, int *));
+
+#include <float.h>
+
+#include "isnanf-nolibm.h"
+#include "minus-zero.h"
+#include "infinity.h"
+#include "nan.h"
+#include "macros.h"
+
+/* Avoid some warnings from "gcc -Wshadow".
+   This file doesn't use the exp() function.  */
+#undef exp
+#define exp exponent
+
+static float
+my_ldexp (float x, int d)
+{
+  for (; d > 0; d--)
+    x *= 2.0f;
+  for (; d < 0; d++)
+    x *= 0.5f;
+  return x;
+}
+
+int
+main ()
+{
+  int i;
+  volatile float x;
+
+  { /* NaN.  */
+    int exp = -9999;
+    float mantissa;
+    x = NaNf ();
+    mantissa = frexpf (x, &exp);
+    ASSERT (isnanf (mantissa));
+  }
+
+  { /* Positive infinity.  */
+    int exp = -9999;
+    float mantissa;
+    x = Infinityf ();
+    mantissa = frexpf (x, &exp);
+    ASSERT (mantissa == x);
+  }
+
+  { /* Negative infinity.  */
+    int exp = -9999;
+    float mantissa;
+    x = - Infinityf ();
+    mantissa = frexpf (x, &exp);
+    ASSERT (mantissa == x);
+  }
+
+  { /* Positive zero.  */
+    int exp = -9999;
+    float mantissa;
+    x = 0.0f;
+    mantissa = frexpf (x, &exp);
+    ASSERT (exp == 0);
+    ASSERT (mantissa == x);
+    ASSERT (!signbit (mantissa));
+  }
+
+  { /* Negative zero.  */
+    int exp = -9999;
+    float mantissa;
+    x = minus_zerof;
+    mantissa = frexpf (x, &exp);
+    ASSERT (exp == 0);
+    ASSERT (mantissa == x);
+    ASSERT (signbit (mantissa));
+  }
+
+  for (i = 1, x = 1.0f; i <= FLT_MAX_EXP; i++, x *= 2.0f)
+    {
+      int exp = -9999;
+      float mantissa = frexpf (x, &exp);
+      ASSERT (exp == i);
+      ASSERT (mantissa == 0.5f);
+    }
+  for (i = 1, x = 1.0f; i >= FLT_MIN_EXP; i--, x *= 0.5f)
+    {
+      int exp = -9999;
+      float mantissa = frexpf (x, &exp);
+      ASSERT (exp == i);
+      ASSERT (mantissa == 0.5f);
+    }
+  for (; i >= FLT_MIN_EXP - 100 && x > 0.0f; i--, x *= 0.5f)
+    {
+      int exp = -9999;
+      float mantissa = frexpf (x, &exp);
+      ASSERT (exp == i);
+      ASSERT (mantissa == 0.5f);
+    }
+
+  for (i = 1, x = -1.0f; i <= FLT_MAX_EXP; i++, x *= 2.0f)
+    {
+      int exp = -9999;
+      float mantissa = frexpf (x, &exp);
+      ASSERT (exp == i);
+      ASSERT (mantissa == -0.5f);
+    }
+  for (i = 1, x = -1.0f; i >= FLT_MIN_EXP; i--, x *= 0.5f)
+    {
+      int exp = -9999;
+      float mantissa = frexpf (x, &exp);
+      ASSERT (exp == i);
+      ASSERT (mantissa == -0.5f);
+    }
+  for (; i >= FLT_MIN_EXP - 100 && x < 0.0f; i--, x *= 0.5f)
+    {
+      int exp = -9999;
+      float mantissa = frexpf (x, &exp);
+      ASSERT (exp == i);
+      ASSERT (mantissa == -0.5f);
+    }
+
+  for (i = 1, x = 1.01f; i <= FLT_MAX_EXP; i++, x *= 2.0f)
+    {
+      int exp = -9999;
+      float mantissa = frexpf (x, &exp);
+      ASSERT (exp == i);
+      ASSERT (mantissa == 0.505f);
+    }
+  for (i = 1, x = 1.01f; i >= FLT_MIN_EXP; i--, x *= 0.5f)
+    {
+      int exp = -9999;
+      float mantissa = frexpf (x, &exp);
+      ASSERT (exp == i);
+      ASSERT (mantissa == 0.505f);
+    }
+  for (; i >= FLT_MIN_EXP - 100 && x > 0.0f; i--, x *= 0.5f)
+    {
+      int exp = -9999;
+      float mantissa = frexpf (x, &exp);
+      ASSERT (exp == i);
+      ASSERT (mantissa >= 0.5f);
+      ASSERT (mantissa < 1.0f);
+      ASSERT (mantissa == my_ldexp (x, - exp));
+    }
+
+  for (i = 1, x = 1.73205f; i <= FLT_MAX_EXP; i++, x *= 2.0f)
+    {
+      int exp = -9999;
+      float mantissa = frexpf (x, &exp);
+      ASSERT (exp == i);
+      ASSERT (mantissa == 0.866025f);
+    }
+  for (i = 1, x = 1.73205f; i >= FLT_MIN_EXP; i--, x *= 0.5f)
+    {
+      int exp = -9999;
+      float mantissa = frexpf (x, &exp);
+      ASSERT (exp == i);
+      ASSERT (mantissa == 0.866025f);
+    }
+  for (; i >= FLT_MIN_EXP - 100 && x > 0.0f; i--, x *= 0.5f)
+    {
+      int exp = -9999;
+      float mantissa = frexpf (x, &exp);
+      ASSERT (exp == i || exp == i + 1);
+      ASSERT (mantissa >= 0.5f);
+      ASSERT (mantissa < 1.0f);
+      ASSERT (mantissa == my_ldexp (x, - exp));
+    }
+
+  return 0;
+}