test-fesetexcept.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Test fesetexcept.
  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_fesetexcept (int exc, const char *exc_name)
  20. {
  21. int result = 0;
  22. printf ("Testing %s\n", exc_name);
  23. feclearexcept (FE_ALL_EXCEPT);
  24. int ret = fesetexcept (exc);
  25. if (ret == 0)
  26. printf ("fesetexcept (%s) succeeded\n", exc_name);
  27. else
  28. {
  29. printf ("fesetexcept (%s) failed\n", exc_name);
  30. if (exc == 0 || EXCEPTION_TESTS (float))
  31. {
  32. puts ("failure of fesetexcept was unexpected");
  33. result = 1;
  34. }
  35. else
  36. puts ("failure of fesetexcept OK, skipping further tests");
  37. return result;
  38. }
  39. ret = fetestexcept (FE_ALL_EXCEPT);
  40. if (ret != exc)
  41. {
  42. printf ("raised exceptions %x, expected %x\n",
  43. (unsigned int) ret, (unsigned int) exc);
  44. result = 1;
  45. }
  46. ret = feraiseexcept (FE_ALL_EXCEPT);
  47. if (ret != 0)
  48. {
  49. if (exc == 0 && !EXCEPTION_TESTS (float))
  50. {
  51. puts ("feraiseexcept (FE_ALL_EXCEPT) failed, skipping further tests");
  52. return result;
  53. }
  54. puts ("feraiseexcept (FE_ALL_EXCEPT) unexpectedly failed");
  55. result = 1;
  56. }
  57. ret = fesetexcept (exc);
  58. if (ret != 0)
  59. {
  60. puts ("fesetexcept (second test) unexpectedly failed");
  61. result = 1;
  62. }
  63. ret = fetestexcept (FE_ALL_EXCEPT);
  64. if (ret != FE_ALL_EXCEPT)
  65. {
  66. printf ("raised exceptions (second test) %x, expected %x\n",
  67. (unsigned int) ret, (unsigned int) FE_ALL_EXCEPT);
  68. result = 1;
  69. }
  70. feclearexcept (FE_ALL_EXCEPT);
  71. ret = feraiseexcept (FE_ALL_EXCEPT & ~exc);
  72. if (ret != 0)
  73. {
  74. puts ("feraiseexcept (third test) unexpectedly failed");
  75. result = 1;
  76. }
  77. ret = fesetexcept (exc);
  78. if (ret != 0)
  79. {
  80. puts ("fesetexcept (third test) unexpectedly failed");
  81. result = 1;
  82. }
  83. ret = fetestexcept (FE_ALL_EXCEPT);
  84. if (ret != FE_ALL_EXCEPT)
  85. {
  86. printf ("raised exceptions (third test) %x, expected %x\n",
  87. (unsigned int) ret, (unsigned int) FE_ALL_EXCEPT);
  88. result = 1;
  89. }
  90. return result;
  91. }
  92. static int
  93. do_test (void)
  94. {
  95. int result = 0;
  96. result |= test_fesetexcept (0, "0");
  97. result |= test_fesetexcept (FE_ALL_EXCEPT, "FE_ALL_EXCEPT");
  98. #ifdef FE_DIVBYZERO
  99. result |= test_fesetexcept (FE_DIVBYZERO, "FE_DIVBYZERO");
  100. #endif
  101. #ifdef FE_INEXACT
  102. result |= test_fesetexcept (FE_INEXACT, "FE_INEXACT");
  103. #endif
  104. #ifdef FE_INVALID
  105. result |= test_fesetexcept (FE_INVALID, "FE_INVALID");
  106. #endif
  107. #ifdef FE_OVERFLOW
  108. result |= test_fesetexcept (FE_OVERFLOW, "FE_OVERFLOW");
  109. #endif
  110. #ifdef FE_UNDERFLOW
  111. result |= test_fesetexcept (FE_UNDERFLOW, "FE_UNDERFLOW");
  112. #endif
  113. return result;
  114. }
  115. #define TEST_FUNCTION do_test ()
  116. #include "../test-skeleton.c"