Blame math/test-fenvinline.c

Packit 6c4009
/* Test for fenv inline implementations.
Packit 6c4009
   Copyright (C) 2015-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef _GNU_SOURCE
Packit 6c4009
# define _GNU_SOURCE
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* To make sure the fenv inline function are used.  */
Packit 6c4009
#undef __NO_MATH_INLINES
Packit 6c4009
Packit 6c4009
#include <fenv.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <math-tests.h>
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
  Since not all architectures might define all exceptions, we define
Packit 6c4009
  a private set and map accordingly.
Packit 6c4009
*/
Packit 6c4009
#define NO_EXC 0
Packit 6c4009
#define INEXACT_EXC 0x1
Packit 6c4009
#define DIVBYZERO_EXC 0x2
Packit 6c4009
#define UNDERFLOW_EXC 0x04
Packit 6c4009
#define OVERFLOW_EXC 0x08
Packit 6c4009
#define INVALID_EXC 0x10
Packit 6c4009
#define ALL_EXC \
Packit 6c4009
        (INEXACT_EXC | DIVBYZERO_EXC | UNDERFLOW_EXC | OVERFLOW_EXC | \
Packit 6c4009
         INVALID_EXC)
Packit 6c4009
static int count_errors;
Packit 6c4009
Packit 6c4009
#if FE_ALL_EXCEPT
Packit 6c4009
static void
Packit 6c4009
test_single_exception_fp_int (int exception,
Packit 6c4009
			      int exc_flag,
Packit 6c4009
			      int fe_flag,
Packit 6c4009
			      const char *flag_name)
Packit 6c4009
{
Packit 6c4009
  if (exception & exc_flag)
Packit 6c4009
    {
Packit 6c4009
      if (fetestexcept (fe_flag))
Packit 6c4009
        printf ("  Pass: Exception \"%s\" is set\n", flag_name);
Packit 6c4009
      else
Packit 6c4009
        {
Packit 6c4009
          printf ("  Fail: Exception \"%s\" is not set\n", flag_name);
Packit 6c4009
          ++count_errors;
Packit 6c4009
        }
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (fetestexcept (fe_flag))
Packit 6c4009
        {
Packit 6c4009
          printf ("  Fail: Exception \"%s\" is set\n", flag_name);
Packit 6c4009
          ++count_errors;
Packit 6c4009
        }
Packit 6c4009
      else
Packit 6c4009
        printf ("  Pass: Exception \"%s\" is not set\n", flag_name);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
/* Test whether a given exception was raised.  */
Packit 6c4009
static void
Packit 6c4009
test_single_exception_fp_double (int exception,
Packit 6c4009
				 int exc_flag,
Packit 6c4009
				 double fe_flag,
Packit 6c4009
				 const char *flag_name)
Packit 6c4009
{
Packit 6c4009
  if (exception & exc_flag)
Packit 6c4009
    {
Packit 6c4009
      if (fetestexcept (fe_flag))
Packit 6c4009
        printf ("  Pass: Exception \"%s\" is set\n", flag_name);
Packit 6c4009
      else
Packit 6c4009
        {
Packit 6c4009
          printf ("  Fail: Exception \"%s\" is not set\n", flag_name);
Packit 6c4009
          ++count_errors;
Packit 6c4009
        }
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (fetestexcept (fe_flag))
Packit 6c4009
        {
Packit 6c4009
          printf ("  Fail: Exception \"%s\" is set\n", flag_name);
Packit 6c4009
          ++count_errors;
Packit 6c4009
        }
Packit 6c4009
      else
Packit 6c4009
        printf ("  Pass: Exception \"%s\" is not set\n", flag_name);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
test_exceptions (const char *test_name, int exception)
Packit 6c4009
{
Packit 6c4009
  printf ("Test: %s\n", test_name);
Packit 6c4009
#ifdef FE_DIVBYZERO
Packit 6c4009
  test_single_exception_fp_double (exception, DIVBYZERO_EXC, FE_DIVBYZERO,
Packit 6c4009
				   "DIVBYZERO");
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_INVALID
Packit 6c4009
  test_single_exception_fp_double (exception, INVALID_EXC, FE_INVALID,
Packit 6c4009
				   "INVALID");
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_INEXACT
Packit 6c4009
  test_single_exception_fp_double (exception, INEXACT_EXC, FE_INEXACT,
Packit 6c4009
				   "INEXACT");
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_UNDERFLOW
Packit 6c4009
  test_single_exception_fp_double (exception, UNDERFLOW_EXC, FE_UNDERFLOW,
Packit 6c4009
				   "UNDERFLOW");
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_OVERFLOW
Packit 6c4009
  test_single_exception_fp_double (exception, OVERFLOW_EXC, FE_OVERFLOW,
Packit 6c4009
				   "OVERFLOW");
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
test_exceptionflag (void)
Packit 6c4009
{
Packit 6c4009
  printf ("Test: fegetexceptionflag (FE_ALL_EXCEPT)\n");
Packit 6c4009
#if FE_ALL_EXCEPT
Packit 6c4009
  fexcept_t excepts;
Packit 6c4009
Packit 6c4009
  feclearexcept (FE_ALL_EXCEPT);
Packit 6c4009
Packit 6c4009
  feraiseexcept (FE_INVALID);
Packit 6c4009
  fegetexceptflag (&excepts, FE_ALL_EXCEPT);
Packit 6c4009
Packit 6c4009
  feclearexcept (FE_ALL_EXCEPT);
Packit 6c4009
  feraiseexcept (FE_OVERFLOW | FE_INEXACT);
Packit 6c4009
Packit 6c4009
  fesetexceptflag (&excepts, FE_ALL_EXCEPT);
Packit 6c4009
Packit 6c4009
  test_single_exception_fp_int (INVALID_EXC, INVALID_EXC, FE_INVALID,
Packit 6c4009
				"INVALID (int)");
Packit 6c4009
  test_single_exception_fp_int (INVALID_EXC, OVERFLOW_EXC, FE_OVERFLOW,
Packit 6c4009
				"OVERFLOW (int)");
Packit 6c4009
  test_single_exception_fp_int (INVALID_EXC, INEXACT_EXC, FE_INEXACT,
Packit 6c4009
				"INEXACT (int)");
Packit 6c4009
Packit 6c4009
  /* Same test, but using double as argument  */
Packit 6c4009
  feclearexcept (FE_ALL_EXCEPT);
Packit 6c4009
Packit 6c4009
  feraiseexcept (FE_INVALID);
Packit 6c4009
  fegetexceptflag (&excepts, (double)FE_ALL_EXCEPT);
Packit 6c4009
Packit 6c4009
  feclearexcept (FE_ALL_EXCEPT);
Packit 6c4009
  feraiseexcept (FE_OVERFLOW | FE_INEXACT);
Packit 6c4009
Packit 6c4009
  fesetexceptflag (&excepts, (double)FE_ALL_EXCEPT);
Packit 6c4009
Packit 6c4009
  test_single_exception_fp_double (INVALID_EXC, INVALID_EXC, FE_INVALID,
Packit 6c4009
				   "INVALID (double)");
Packit 6c4009
  test_single_exception_fp_double (INVALID_EXC, OVERFLOW_EXC, FE_OVERFLOW,
Packit 6c4009
				   "OVERFLOW (double)");
Packit 6c4009
  test_single_exception_fp_double (INVALID_EXC, INEXACT_EXC, FE_INEXACT,
Packit 6c4009
				   "INEXACT (double)");
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
test_fesetround (void)
Packit 6c4009
{
Packit 6c4009
#if defined FE_TONEAREST && defined FE_TOWARDZERO
Packit 6c4009
  int res1;
Packit 6c4009
  int res2;
Packit 6c4009
Packit 6c4009
  printf ("Tests for fesetround\n");
Packit 6c4009
Packit 6c4009
  /* The fesetround should not itself cause the test to fail, however it
Packit 6c4009
     should either succeed for both 'int' and 'double' argument, or fail
Packit 6c4009
     for both.  */
Packit 6c4009
  res1 = fesetround ((int) FE_TOWARDZERO);
Packit 6c4009
  res2 = fesetround ((double) FE_TOWARDZERO);
Packit 6c4009
  if (res1 != res2)
Packit 6c4009
    {
Packit 6c4009
      printf ("fesetround (FE_TOWARDZERO) failed: %d, %d\n", res1, res2);
Packit 6c4009
      ++count_errors;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  res1 = fesetround ((int) FE_TONEAREST);
Packit 6c4009
  res2 = fesetround ((double) FE_TONEAREST);
Packit 6c4009
  if (res1 != res2)
Packit 6c4009
    {
Packit 6c4009
      printf ("fesetround (FE_TONEAREST) failed: %d, %d\n", res1, res2);
Packit 6c4009
      ++count_errors;
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#if FE_ALL_EXCEPT
Packit 6c4009
/* Tests for feenableexcept/fedisableexcept.  */
Packit 6c4009
static void
Packit 6c4009
feenable_test (const char *flag_name, fexcept_t fe_exc)
Packit 6c4009
{
Packit 6c4009
  int fe_exci = fe_exc;
Packit 6c4009
  double fe_excd = fe_exc;
Packit 6c4009
  int excepts;
Packit 6c4009
Packit 6c4009
  /* First disable all exceptions.  */
Packit 6c4009
  if (fedisableexcept (FE_ALL_EXCEPT) == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("Test: fedisableexcept (FE_ALL_EXCEPT) failed\n");
Packit 6c4009
      ++count_errors;
Packit 6c4009
      /* If this fails, the other tests don't make sense.  */
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Test for inline macros using integer argument.  */
Packit 6c4009
  excepts = feenableexcept (fe_exci);
Packit 6c4009
  if (!EXCEPTION_ENABLE_SUPPORTED (fe_exci) && excepts == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("Test: not testing feenableexcept, it isn't implemented.\n");
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
  if (excepts == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("Test: feenableexcept (%s) failed\n", flag_name);
Packit 6c4009
      ++count_errors;
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
  if (excepts != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("Test: feenableexcept (%s) failed, return should be 0, is %x\n",
Packit 6c4009
              flag_name, excepts);
Packit 6c4009
      ++count_errors;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* And now disable the exception again.  */
Packit 6c4009
  excepts = fedisableexcept (fe_exc);
Packit 6c4009
  if (excepts == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("Test: fedisableexcept (%s) failed\n", flag_name);
Packit 6c4009
      ++count_errors;
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
  if (excepts != fe_exc)
Packit 6c4009
    {
Packit 6c4009
      printf ("Test: fedisableexcept (%s) failed, return should be 0x%x, is 0x%x\n",
Packit 6c4009
              flag_name, (unsigned int)fe_exc, excepts);
Packit 6c4009
      ++count_errors;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Test for inline macros using double argument.  */
Packit 6c4009
  excepts = feenableexcept (fe_excd);
Packit 6c4009
  if (!EXCEPTION_ENABLE_SUPPORTED (fe_excd) && excepts == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("Test: not testing feenableexcept, it isn't implemented.\n");
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
  if (excepts == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("Test: feenableexcept (%s) failed\n", flag_name);
Packit 6c4009
      ++count_errors;
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
  if (excepts != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("Test: feenableexcept (%s) failed, return should be 0, is %x\n",
Packit 6c4009
              flag_name, excepts);
Packit 6c4009
      ++count_errors;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* And now disable the exception again.  */
Packit 6c4009
  excepts = fedisableexcept (fe_exc);
Packit 6c4009
  if (excepts == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("Test: fedisableexcept (%s) failed\n", flag_name);
Packit 6c4009
      ++count_errors;
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
  if (excepts != fe_exc)
Packit 6c4009
    {
Packit 6c4009
      printf ("Test: fedisableexcept (%s) failed, return should be 0x%x, is 0x%x\n",
Packit 6c4009
              flag_name, (unsigned int)fe_exc, excepts);
Packit 6c4009
      ++count_errors;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
test_feenabledisable (void)
Packit 6c4009
{
Packit 6c4009
  printf ("Tests for feenableexcepts/fedisableexcept\n");
Packit 6c4009
Packit 6c4009
  /* We might have some exceptions still set.  */
Packit 6c4009
  feclearexcept (FE_ALL_EXCEPT);
Packit 6c4009
Packit 6c4009
#ifdef FE_DIVBYZERO
Packit 6c4009
  feenable_test ("FE_DIVBYZERO", FE_DIVBYZERO);
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_INVALID
Packit 6c4009
  feenable_test ("FE_INVALID", FE_INVALID);
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_INEXACT
Packit 6c4009
  feenable_test ("FE_INEXACT", FE_INEXACT);
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_UNDERFLOW
Packit 6c4009
  feenable_test ("FE_UNDERFLOW", FE_UNDERFLOW);
Packit 6c4009
#endif
Packit 6c4009
#ifdef FE_OVERFLOW
Packit 6c4009
  feenable_test ("FE_OVERFLOW", FE_OVERFLOW);
Packit 6c4009
#endif
Packit 6c4009
  fesetenv (FE_DFL_ENV);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  /* clear all exceptions and test if all are cleared  */
Packit 6c4009
  feclearexcept (FE_ALL_EXCEPT);
Packit 6c4009
  test_exceptions ("feclearexcept (FE_ALL_EXCEPT) clears all exceptions",
Packit 6c4009
                   NO_EXC);
Packit 6c4009
Packit 6c4009
  /* raise all exceptions and test if all are raised  */
Packit 6c4009
  feraiseexcept (FE_ALL_EXCEPT);
Packit 6c4009
  if (EXCEPTION_TESTS (float))
Packit 6c4009
    test_exceptions ("feraiseexcept (FE_ALL_EXCEPT) raises all exceptions",
Packit 6c4009
		     ALL_EXC);
Packit 6c4009
Packit 6c4009
  /* Same test, but using double as argument  */
Packit 6c4009
  feclearexcept ((double)FE_ALL_EXCEPT);
Packit 6c4009
  test_exceptions ("feclearexcept ((double)FE_ALL_EXCEPT) clears all exceptions",
Packit 6c4009
                   NO_EXC);
Packit 6c4009
Packit 6c4009
  feraiseexcept ((double)FE_ALL_EXCEPT);
Packit 6c4009
  if (EXCEPTION_TESTS (float))
Packit 6c4009
    test_exceptions ("feraiseexcept ((double)FE_ALL_EXCEPT) raises all exceptions",
Packit 6c4009
		     ALL_EXC);
Packit 6c4009
Packit 6c4009
  if (EXCEPTION_TESTS (float))
Packit 6c4009
    test_exceptionflag ();
Packit 6c4009
Packit 6c4009
  test_fesetround ();
Packit 6c4009
Packit 6c4009
  test_feenabledisable ();
Packit 6c4009
Packit 6c4009
  return count_errors;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"