The fenv-exceptions-trapping module, compared to the fpe-trapping module, not only works on all platforms. It also supports all exception types, not only FE_INVALID. This can be used to strengthen the fenv-exceptions-tracking-c99 tests.
2023-11-05 Bruno Haible <br...@clisp.org> fenv-exceptions-tracking-c99 tests: Enhance tests. * tests/test-fenv-except-tracking-3.sh: Test not only FE_INVALID, but also FE_DIVBYZERO, FE_OVERFLOW, FE_UNDERFLOW, FE_INEXACT. * tests/test-fenv-except-tracking-3.c: Include <stdlib.h>, <string.h>. Don't include fpe-trapping.h. Assume HAVE_FPE_TRAPPING is 1. (main): Receive the exception to test as first argument. diff --git a/tests/test-fenv-except-tracking-3.c b/tests/test-fenv-except-tracking-3.c index 63fabad48f..e2f16efa65 100644 --- a/tests/test-fenv-except-tracking-3.c +++ b/tests/test-fenv-except-tracking-3.c @@ -22,33 +22,49 @@ #include <fenv.h> #include <stdio.h> +#include <stdlib.h> +#include <string.h> -#include "fpe-trapping.h" #include "macros.h" /* musl libc does not support floating-point exception trapping, even where the hardware supports it. See <https://wiki.musl-libc.org/functional-differences-from-glibc.html> */ -#if HAVE_FPE_TRAPPING && (!MUSL_LIBC || GNULIB_FEENABLEEXCEPT) +#if !MUSL_LIBC || GNULIB_FEENABLEEXCEPT /* Check that feraiseexcept() can trigger a trap. */ int -main () +main (int argc, char *argv[]) { - /* Clear FE_INVALID exceptions from past operations. */ - feclearexcept (FE_INVALID); - - /* An FE_INVALID exception shall trigger a SIGFPE signal, which by default - terminates the program. */ - if (sigfpe_on_invalid () < 0) + if (argc > 1) { - fputs ("Skipping test: trapping floating-point exceptions are not supported on this machine.\n", stderr); - return 77; + int exception; + + if (STREQ (argv[1], "FE_INVALID")) exception = FE_INVALID; else + if (STREQ (argv[1], "FE_DIVBYZERO")) exception = FE_DIVBYZERO; else + if (STREQ (argv[1], "FE_OVERFLOW")) exception = FE_OVERFLOW; else + if (STREQ (argv[1], "FE_UNDERFLOW")) exception = FE_UNDERFLOW; else + if (STREQ (argv[1], "FE_INEXACT")) exception = FE_INEXACT; else + { + printf ("Invalid argument: %s\n", argv[1]); + exit (1); + } + + /* Clear FE_XX exceptions from past operations. */ + feclearexcept (exception); + + /* An FE_XX exception shall trigger a SIGFPE signal, which by default + terminates the program. */ + if (feenableexcept (exception) == -1) + { + fputs ("Skipping test: trapping floating-point exceptions are not supported on this machine.\n", stderr); + return 77; + } + + feraiseexcept (exception); } - feraiseexcept (FE_INVALID); - return 0; } diff --git a/tests/test-fenv-except-tracking-3.sh b/tests/test-fenv-except-tracking-3.sh index f44e43a59b..263af596b3 100755 --- a/tests/test-fenv-except-tracking-3.sh +++ b/tests/test-fenv-except-tracking-3.sh @@ -4,15 +4,17 @@ final_rc=0 -${CHECKER} ./test-fenv-except-tracking-3${EXEEXT} -rc=$? -if test $rc = 77; then - final_rc=77 -else - if test $rc = 0; then - echo "Failed: ./test-fenv-except-tracking-3" 1>&2 - exit 1 +for arg in FE_INVALID FE_DIVBYZERO FE_OVERFLOW FE_UNDERFLOW FE_INEXACT; do + ${CHECKER} ./test-fenv-except-tracking-3${EXEEXT} $arg + rc=$? + if test $rc = 77; then + final_rc=77 + else + if test $rc = 0; then + echo "Failed: ./test-fenv-except-tracking-3 $arg" 1>&2 + exit 1 + fi fi -fi +done exit $final_rc