basic-test.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Copyright (C) 1999 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Andreas Jaeger <aj@suse.de>, 1999.
  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. #define _ISOC99_SOURCE
  16. #include <math.h>
  17. #include <float.h>
  18. #include <stdio.h>
  19. static int errors = 0;
  20. static void
  21. check (const char *testname, int result)
  22. {
  23. if (!result) {
  24. printf ("Failure: %s\n", testname);
  25. errors++;
  26. }
  27. }
  28. #define TEST_FUNC(NAME, FLOAT, NANFUNC, EPSILON, HUGEVAL) \
  29. static void \
  30. NAME (void) \
  31. { \
  32. /* Variables are declared volatile to forbid some compiler \
  33. optimizations. */ \
  34. volatile FLOAT Inf_var, NaN_var, zero_var, one_var; \
  35. FLOAT x1, x2; \
  36. \
  37. zero_var = 0.0; \
  38. one_var = 1.0; \
  39. NaN_var = zero_var/zero_var; \
  40. Inf_var = one_var / zero_var; \
  41. \
  42. (void) &zero_var; \
  43. (void) &one_var; \
  44. (void) &NaN_var; \
  45. (void) &Inf_var; \
  46. \
  47. \
  48. check (#FLOAT " isinf (inf) == 1", isinf (Inf_var) == 1); \
  49. check (#FLOAT " isinf (-inf) == -1", isinf (-Inf_var) == -1); \
  50. check (#FLOAT " !isinf (1)", !(isinf (one_var))); \
  51. check (#FLOAT " !isinf (NaN)", !(isinf (NaN_var))); \
  52. \
  53. check (#FLOAT " isnan (NaN)", isnan (NaN_var)); \
  54. check (#FLOAT " isnan (-NaN)", isnan (-NaN_var)); \
  55. check (#FLOAT " !isnan (1)", !(isnan (one_var))); \
  56. check (#FLOAT " !isnan (inf)", !(isnan (Inf_var))); \
  57. \
  58. /* \
  59. the same tests but this time with NAN from <bits/nan.h> \
  60. NAN is a double const \
  61. */ \
  62. check (#FLOAT " isnan (NAN)", isnan (NAN)); \
  63. check (#FLOAT " isnan (-NAN)", isnan (-NAN)); \
  64. check (#FLOAT " !isinf (NAN)", !(isinf (NAN))); \
  65. check (#FLOAT " !isinf (-NAN)", !(isinf (-NAN))); \
  66. \
  67. /* \
  68. And again with the value returned by the `nan' function. \
  69. */ \
  70. check (#FLOAT " isnan (NAN)", isnan (NANFUNC (""))); \
  71. check (#FLOAT " isnan (-NAN)", isnan (-NANFUNC (""))); \
  72. check (#FLOAT " !isinf (NAN)", !(isinf (NANFUNC ("")))); \
  73. check (#FLOAT " !isinf (-NAN)", !(isinf (-NANFUNC ("")))); \
  74. check (#FLOAT " NAN != NAN", NANFUNC ("") != NANFUNC ("")); \
  75. \
  76. /* test if HUGE_VALx is ok */ \
  77. x1 = HUGEVAL; \
  78. check (#FLOAT " isinf (HUGE_VALx) == +1", isinf (x1) == +1); \
  79. x1 = - HUGEVAL; \
  80. check (#FLOAT " isinf (-HUGE_VALx) == -1", isinf (x1) == -1); \
  81. }
  82. #ifndef DO_C99_MATH
  83. # undef TEST_FUNC
  84. # define TEST_FUNC(NAME, FLOAT, NANFUNC, EPSILON, HUGEVAL) \
  85. static void \
  86. NAME(void) \
  87. { /* nothing */ }
  88. #endif
  89. #define TEST_VAL(NAME, FLOAT, NANFUNC, EPSILON, HUGEVAL) \
  90. static void \
  91. NAME (void) \
  92. { \
  93. /* Variables are declared volatile to forbid some compiler \
  94. optimizations. */ \
  95. volatile FLOAT Inf_var, NaN_var, zero_var, one_var; \
  96. FLOAT x1, x2; \
  97. \
  98. zero_var = 0.0; \
  99. one_var = 1.0; \
  100. NaN_var = zero_var/zero_var; \
  101. Inf_var = one_var / zero_var; \
  102. \
  103. (void) &zero_var; \
  104. (void) &one_var; \
  105. (void) &NaN_var; \
  106. (void) &Inf_var; \
  107. \
  108. \
  109. check (#FLOAT " inf == inf", Inf_var == Inf_var); \
  110. check (#FLOAT " -inf == -inf", -Inf_var == -Inf_var); \
  111. check (#FLOAT " inf != -inf", Inf_var != -Inf_var); \
  112. check (#FLOAT " NaN != NaN", NaN_var != NaN_var); \
  113. \
  114. check (#FLOAT " NAN != NAN", NAN != NAN); \
  115. \
  116. \
  117. /* test if EPSILON is ok */ \
  118. x1 = 1.0; \
  119. x2 = x1 + EPSILON; \
  120. check (#FLOAT " 1 != 1+EPSILON", x1 != x2); \
  121. \
  122. x1 = 1.0; \
  123. x2 = x1 - EPSILON; \
  124. check (#FLOAT " 1 != 1-EPSILON", x1 != x2); \
  125. \
  126. }
  127. TEST_VAL (float_test_value, float, nanf, FLT_EPSILON, HUGE_VALF)
  128. TEST_FUNC (float_test_call, float, nanf, FLT_EPSILON, HUGE_VALF)
  129. TEST_VAL (double_test_value, double, nan, DBL_EPSILON, HUGE_VAL)
  130. TEST_FUNC (double_test_call, double, nan, DBL_EPSILON, HUGE_VAL)
  131. #ifndef NO_LONG_DOUBLE
  132. TEST_VAL (ldouble_test_value, long double, nanl, LDBL_EPSILON, HUGE_VALL)
  133. TEST_FUNC (ldouble_test_call, long double, nanl, LDBL_EPSILON, HUGE_VALL)
  134. #endif
  135. int
  136. main (void)
  137. {
  138. float_test_value ();
  139. float_test_call ();
  140. double_test_value ();
  141. double_test_call ();
  142. #ifndef NO_LONG_DOUBLE
  143. ldouble_test_value ();
  144. ldouble_test_call ();
  145. #endif
  146. return errors != 0;
  147. }