test-femode-traps.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Test femode_t functions: test handling of exception traps.
  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. static int
  19. test_ee (int exc1, int exc2)
  20. {
  21. int result = 0;
  22. printf ("testing %x %x\n", (unsigned int) exc1, (unsigned int) exc2);
  23. fedisableexcept (FE_ALL_EXCEPT);
  24. int ret = feenableexcept (exc1);
  25. if (ret == -1)
  26. {
  27. if (EXCEPTION_ENABLE_SUPPORTED (exc1))
  28. {
  29. puts ("first feenableexcept failed unexpectedly");
  30. result = 1;
  31. }
  32. else
  33. puts ("first feenableexcept failed, cannot test");
  34. return result;
  35. }
  36. femode_t saved;
  37. ret = fegetmode (&saved);
  38. if (ret != 0)
  39. {
  40. puts ("fegetmode failed");
  41. result = 1;
  42. return result;
  43. }
  44. fedisableexcept (FE_ALL_EXCEPT);
  45. ret = feenableexcept (exc2);
  46. if (ret == -1)
  47. {
  48. if (EXCEPTION_ENABLE_SUPPORTED (exc2))
  49. {
  50. puts ("second feenableexcept failed unexpectedly");
  51. result = 1;
  52. }
  53. else
  54. puts ("second feenableexcept failed, cannot test");
  55. return result;
  56. }
  57. ret = fesetmode (&saved);
  58. if (ret != 0)
  59. {
  60. puts ("fesetmode failed");
  61. result = 1;
  62. return result;
  63. }
  64. /* Verify that the set of enabled traps was restored. */
  65. ret = fegetexcept ();
  66. if (ret != exc1)
  67. {
  68. printf ("restored enabled traps %x not %x\n", (unsigned int) ret,
  69. (unsigned int) exc1);
  70. result = 1;
  71. }
  72. /* Likewise, with default modes. */
  73. ret = fesetmode (FE_DFL_MODE);
  74. if (ret != 0)
  75. {
  76. puts ("fesetmode (FE_DFL_MODE) failed");
  77. result = 1;
  78. return result;
  79. }
  80. ret = fegetexcept ();
  81. if (ret != 0)
  82. {
  83. printf ("FE_DFL_MODE enabled traps %x not 0\n", (unsigned int) ret);
  84. result = 1;
  85. }
  86. return result;
  87. }
  88. static int
  89. test_e (int exc1)
  90. {
  91. int result = 0;
  92. result |= test_ee (exc1, 0);
  93. result |= test_ee (exc1, FE_ALL_EXCEPT);
  94. #ifdef FE_DIVBYZERO
  95. result |= test_ee (exc1, FE_DIVBYZERO);
  96. #endif
  97. #ifdef FE_INEXACT
  98. result |= test_ee (exc1, FE_INEXACT);
  99. #endif
  100. #ifdef FE_INVALID
  101. result |= test_ee (exc1, FE_INVALID);
  102. #endif
  103. #ifdef FE_OVERFLOW
  104. result |= test_ee (exc1, FE_OVERFLOW);
  105. #endif
  106. #ifdef FE_UNDERFLOW
  107. result |= test_ee (exc1, FE_UNDERFLOW);
  108. #endif
  109. return result;
  110. }
  111. static int
  112. do_test (void)
  113. {
  114. int result = 0;
  115. result |= test_e (0);
  116. result |= test_e (FE_ALL_EXCEPT);
  117. #ifdef FE_DIVBYZERO
  118. result |= test_e (FE_DIVBYZERO);
  119. #endif
  120. #ifdef FE_INEXACT
  121. result |= test_e (FE_INEXACT);
  122. #endif
  123. #ifdef FE_INVALID
  124. result |= test_e (FE_INVALID);
  125. #endif
  126. #ifdef FE_OVERFLOW
  127. result |= test_e (FE_OVERFLOW);
  128. #endif
  129. #ifdef FE_UNDERFLOW
  130. result |= test_e (FE_UNDERFLOW);
  131. #endif
  132. return result;
  133. }
  134. #define TEST_FUNCTION do_test ()
  135. #include "../test-skeleton.c"