test-fexcept.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Test fegetexceptflag and fesetexceptflag.
  2. Copyright (C) 2016-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <fenv.h>
  16. #include <stdio.h>
  17. #include <math-tests.h>
  18. /* Like feraiseexcept, but raise exactly the specified exceptions EXC,
  19. without possibly raising "inexact" together with "overflow" or
  20. "underflow" as permitted by ISO C. (This is not used with traps
  21. enabled, so side-effects from raising and then clearing "inexact"
  22. are irrelevant.) */
  23. static int
  24. feraiseexcept_exact (int exc)
  25. {
  26. #ifdef FE_INEXACT
  27. int mask = 0;
  28. #ifdef FE_OVERFLOW
  29. mask |= FE_OVERFLOW;
  30. #endif
  31. #ifdef FE_UNDERFLOW
  32. mask |= FE_UNDERFLOW;
  33. #endif
  34. if ((exc & FE_INEXACT) != 0
  35. || (exc & mask) == 0
  36. || fetestexcept (FE_INEXACT) != 0)
  37. return feraiseexcept (exc);
  38. int ret = feraiseexcept (exc);
  39. feclearexcept (FE_INEXACT);
  40. return ret;
  41. #else
  42. return feraiseexcept (exc);
  43. #endif
  44. }
  45. static int
  46. test_set (int initial, const fexcept_t *saved, int mask, int expected)
  47. {
  48. int result = 0;
  49. feclearexcept (FE_ALL_EXCEPT);
  50. printf ("Testing set: initial exceptions %x, mask %x, expected %x\n",
  51. (unsigned int) initial, (unsigned int) mask,
  52. (unsigned int) expected);
  53. int ret = feraiseexcept_exact (initial);
  54. if (ret != 0)
  55. {
  56. puts ("feraiseexcept failed");
  57. if (initial == 0 || EXCEPTION_TESTS (float))
  58. {
  59. puts ("failure of feraiseexcept was unexpected");
  60. result = 1;
  61. }
  62. else
  63. puts ("failure of feraiseexcept OK, skipping further tests");
  64. return result;
  65. }
  66. ret = fesetexceptflag (saved, mask);
  67. if (ret != 0)
  68. {
  69. puts ("fesetexceptflag failed");
  70. result = 1;
  71. }
  72. else
  73. puts ("fesetexceptflag succeeded");
  74. ret = fetestexcept (FE_ALL_EXCEPT);
  75. if (ret != expected)
  76. {
  77. printf ("raised exceptions %x, expected %x\n",
  78. (unsigned int) ret, (unsigned int) expected);
  79. result = 1;
  80. }
  81. return result;
  82. }
  83. static int
  84. test_except (int exc, const char *exc_name)
  85. {
  86. int result = 0;
  87. printf ("Testing %s\n", exc_name);
  88. feclearexcept (FE_ALL_EXCEPT);
  89. fexcept_t clear_saved_exc, clear_saved_all;
  90. int ret = fegetexceptflag (&clear_saved_exc, exc);
  91. if (ret == 0)
  92. printf ("fegetexceptflag (%s) succeeded\n", exc_name);
  93. else
  94. {
  95. printf ("fegetexceptflag (%s) failed\n", exc_name);
  96. result = 1;
  97. return result;
  98. }
  99. ret = fegetexceptflag (&clear_saved_all, FE_ALL_EXCEPT);
  100. if (ret == 0)
  101. puts ("fegetexceptflag (FE_ALL_EXCEPT) succeeded");
  102. else
  103. {
  104. puts ("fegetexceptflag (FE_ALL_EXCEPT) failed");
  105. result = 1;
  106. return result;
  107. }
  108. ret = feraiseexcept_exact (exc);
  109. if (ret == 0)
  110. printf ("feraiseexcept (%s) succeeded\n", exc_name);
  111. else
  112. {
  113. printf ("feraiseexcept (%s) failed\n", exc_name);
  114. if (exc == 0 || EXCEPTION_TESTS (float))
  115. {
  116. puts ("failure of feraiseexcept was unexpected");
  117. result = 1;
  118. }
  119. else
  120. puts ("failure of feraiseexcept OK, skipping further tests");
  121. return result;
  122. }
  123. fexcept_t set_saved_exc, set_saved_all;
  124. ret = fegetexceptflag (&set_saved_exc, exc);
  125. if (ret == 0)
  126. printf ("fegetexceptflag (%s) succeeded\n", exc_name);
  127. else
  128. {
  129. printf ("fegetexceptflag (%s) failed\n", exc_name);
  130. result = 1;
  131. return result;
  132. }
  133. ret = fegetexceptflag (&set_saved_all, FE_ALL_EXCEPT);
  134. if (ret == 0)
  135. puts ("fegetexceptflag (FE_ALL_EXCEPT) succeeded");
  136. else
  137. {
  138. puts ("fegetexceptflag (FE_ALL_EXCEPT) failed");
  139. result = 1;
  140. return result;
  141. }
  142. result |= test_set (0, &set_saved_exc, exc, exc);
  143. result |= test_set (0, &set_saved_all, exc, exc);
  144. result |= test_set (0, &set_saved_all, FE_ALL_EXCEPT, exc);
  145. result |= test_set (0, &clear_saved_exc, exc, 0);
  146. result |= test_set (0, &clear_saved_all, exc, 0);
  147. result |= test_set (0, &clear_saved_all, FE_ALL_EXCEPT, 0);
  148. result |= test_set (exc, &set_saved_exc, exc, exc);
  149. result |= test_set (exc, &set_saved_all, exc, exc);
  150. result |= test_set (exc, &set_saved_all, FE_ALL_EXCEPT, exc);
  151. result |= test_set (exc, &clear_saved_exc, exc, 0);
  152. result |= test_set (exc, &clear_saved_all, exc, 0);
  153. result |= test_set (exc, &clear_saved_all, FE_ALL_EXCEPT, 0);
  154. result |= test_set (FE_ALL_EXCEPT, &set_saved_exc, exc, FE_ALL_EXCEPT);
  155. result |= test_set (FE_ALL_EXCEPT, &set_saved_all, exc, FE_ALL_EXCEPT);
  156. result |= test_set (FE_ALL_EXCEPT, &set_saved_all, FE_ALL_EXCEPT, exc);
  157. result |= test_set (FE_ALL_EXCEPT, &clear_saved_exc, exc,
  158. FE_ALL_EXCEPT & ~exc);
  159. result |= test_set (FE_ALL_EXCEPT, &clear_saved_all, exc,
  160. FE_ALL_EXCEPT & ~exc);
  161. result |= test_set (FE_ALL_EXCEPT, &clear_saved_all, FE_ALL_EXCEPT, 0);
  162. return result;
  163. }
  164. static int
  165. do_test (void)
  166. {
  167. int result = 0;
  168. result |= test_except (0, "0");
  169. result |= test_except (FE_ALL_EXCEPT, "FE_ALL_EXCEPT");
  170. #ifdef FE_DIVBYZERO
  171. result |= test_except (FE_DIVBYZERO, "FE_DIVBYZERO");
  172. #endif
  173. #ifdef FE_INEXACT
  174. result |= test_except (FE_INEXACT, "FE_INEXACT");
  175. #endif
  176. #ifdef FE_INVALID
  177. result |= test_except (FE_INVALID, "FE_INVALID");
  178. #endif
  179. #ifdef FE_OVERFLOW
  180. result |= test_except (FE_OVERFLOW, "FE_OVERFLOW");
  181. #endif
  182. #ifdef FE_UNDERFLOW
  183. result |= test_except (FE_UNDERFLOW, "FE_UNDERFLOW");
  184. #endif
  185. return result;
  186. }
  187. #define TEST_FUNCTION do_test ()
  188. #include "../test-skeleton.c"