usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-fenv-except-trapping-1.c
blob8d9f3e91115cf6effab85e37b0ceede0665768c3
1 /* Test of turning floating-point exceptions into traps (signals).
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2023. */
19 #include <config.h>
21 /* Specification. */
22 #include <fenv.h>
24 #include <stdio.h>
26 #include "macros.h"
28 /* Check of the Boole-an algebra. */
30 /* Converts 5 bits to an exceptions bit mask. */
31 static int
32 uint_to_exceptions (unsigned int a)
34 return (a & 0x01 ? FE_INVALID : 0)
35 | (a & 0x02 ? FE_DIVBYZERO : 0)
36 | (a & 0x04 ? FE_OVERFLOW : 0)
37 | (a & 0x08 ? FE_UNDERFLOW : 0)
38 | (a & 0x10 ? FE_INEXACT : 0);
41 int
42 main ()
44 unsigned int a, b;
46 /* Run through all possible valid arguments to feenableexcept and
47 fedisableexcept.
48 An alternative way of coding this iteration, without the uint_to_exceptions
49 function, would be using the trick from
50 Jörg Arndt: Matters Computational <https://www.jjj.de/fxt/fxtbook.pdf>
51 § 1.25.1 Generating bit subsets of a given word */
52 for (a = 0; a < 0x20; a++)
53 for (b = 0; b < 0x20; b++)
55 unsigned int c = a & ~b;
57 if (fedisableexcept (FE_ALL_EXCEPT) == -1
58 || feenableexcept (uint_to_exceptions (a)) == -1)
60 if (test_exit_status != EXIT_SUCCESS)
61 return test_exit_status;
62 fputs ("Skipping test: trapping floating-point exceptions are not supported on this machine.\n", stderr);
63 return 77;
65 ASSERT (fedisableexcept (uint_to_exceptions (b))
66 == uint_to_exceptions (a));
67 /* Check fegetexcept. */
68 ASSERT (fegetexcept () == uint_to_exceptions (c));
69 /* Check the return value of feenableexcept. It should be consistent
70 with fegetexcept. */
71 ASSERT (feenableexcept (0) == uint_to_exceptions (c));
74 return test_exit_status;