test-fetestexceptflag.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Test fetestexceptflag.
  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_one (int exc_test, int exc_set, int exc_save)
  20. {
  21. int result = 0;
  22. printf ("Individual test: %x %x %x\n", (unsigned int) exc_test,
  23. (unsigned int) exc_set, (unsigned int) exc_save);
  24. feclearexcept (FE_ALL_EXCEPT);
  25. int ret = fesetexcept (exc_set);
  26. if (ret != 0)
  27. {
  28. puts ("fesetexcept failed");
  29. if (exc_set == 0 || EXCEPTION_TESTS (float))
  30. {
  31. puts ("failure of fesetexcept was unexpected");
  32. result = 1;
  33. }
  34. else
  35. puts ("failure of fesetexcept OK, skipping further tests");
  36. return result;
  37. }
  38. fexcept_t saved;
  39. ret = fegetexceptflag (&saved, exc_save);
  40. if (ret == 0)
  41. puts ("fegetexceptflag succeeded");
  42. else
  43. {
  44. puts ("fegetexceptflag failed");
  45. result = 1;
  46. return result;
  47. }
  48. ret = fetestexceptflag (&saved, exc_test);
  49. if (ret == (exc_set & exc_test))
  50. puts ("fetestexceptflag result correct");
  51. else
  52. {
  53. printf ("fetestexceptflag returned %x, expected %x\n", ret,
  54. exc_set & exc_test);
  55. result = 1;
  56. }
  57. if (exc_save == FE_ALL_EXCEPT)
  58. {
  59. /* Also test fetestexceptflag testing all exceptions but
  60. possibly with only some set. */
  61. ret = fetestexceptflag (&saved, FE_ALL_EXCEPT);
  62. if (ret == exc_set)
  63. puts ("fetestexceptflag (FE_ALL_EXCEPT) result correct");
  64. else
  65. {
  66. printf ("fetestexceptflag (FE_ALL_EXCEPT) returned %x, expected %x\n",
  67. ret, exc_set);
  68. result = 1;
  69. }
  70. }
  71. return result;
  72. }
  73. static int
  74. test_fetestexceptflag (int exc, const char *exc_name)
  75. {
  76. int result = 0;
  77. printf ("Testing %s\n", exc_name);
  78. /* Test each case of: whether this exception is set or clear;
  79. whether other exceptions are set or clear; whether the whole
  80. state is saved or just the state for this exception. */
  81. result |= test_one (exc, 0, exc);
  82. result |= test_one (exc, 0, FE_ALL_EXCEPT);
  83. result |= test_one (exc, exc, exc);
  84. result |= test_one (exc, exc, FE_ALL_EXCEPT);
  85. result |= test_one (exc, FE_ALL_EXCEPT & ~exc, exc);
  86. result |= test_one (exc, FE_ALL_EXCEPT & ~exc, FE_ALL_EXCEPT);
  87. result |= test_one (exc, FE_ALL_EXCEPT, exc);
  88. result |= test_one (exc, FE_ALL_EXCEPT, FE_ALL_EXCEPT);
  89. return result;
  90. }
  91. static int
  92. do_test (void)
  93. {
  94. int result = 0;
  95. result |= test_fetestexceptflag (0, "0");
  96. result |= test_fetestexceptflag (FE_ALL_EXCEPT, "FE_ALL_EXCEPT");
  97. #ifdef FE_DIVBYZERO
  98. result |= test_fetestexceptflag (FE_DIVBYZERO, "FE_DIVBYZERO");
  99. #endif
  100. #ifdef FE_INEXACT
  101. result |= test_fetestexceptflag (FE_INEXACT, "FE_INEXACT");
  102. #endif
  103. #ifdef FE_INVALID
  104. result |= test_fetestexceptflag (FE_INVALID, "FE_INVALID");
  105. #endif
  106. #ifdef FE_OVERFLOW
  107. result |= test_fetestexceptflag (FE_OVERFLOW, "FE_OVERFLOW");
  108. #endif
  109. #ifdef FE_UNDERFLOW
  110. result |= test_fetestexceptflag (FE_UNDERFLOW, "FE_UNDERFLOW");
  111. #endif
  112. return result;
  113. }
  114. #define TEST_FUNCTION do_test ()
  115. #include "../test-skeleton.c"