basic-test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  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. check (#FLOAT " inf == inf", Inf_var == Inf_var); \
  59. check (#FLOAT " -inf == -inf", -Inf_var == -Inf_var); \
  60. check (#FLOAT " inf != -inf", Inf_var != -Inf_var); \
  61. check (#FLOAT " NaN != NaN", NaN_var != NaN_var); \
  62. \
  63. /* \
  64. the same tests but this time with NAN from <bits/nan.h> \
  65. NAN is a double const \
  66. */ \
  67. check (#FLOAT " isnan (NAN)", isnan (NAN)); \
  68. check (#FLOAT " isnan (-NAN)", isnan (-NAN)); \
  69. check (#FLOAT " !isinf (NAN)", !(isinf (NAN))); \
  70. check (#FLOAT " !isinf (-NAN)", !(isinf (-NAN))); \
  71. check (#FLOAT " NAN != NAN", NAN != NAN); \
  72. \
  73. /* \
  74. And again with the value returned by the `nan' function. \
  75. */ \
  76. check (#FLOAT " isnan (NAN)", isnan (NANFUNC (""))); \
  77. check (#FLOAT " isnan (-NAN)", isnan (-NANFUNC (""))); \
  78. check (#FLOAT " !isinf (NAN)", !(isinf (NANFUNC ("")))); \
  79. check (#FLOAT " !isinf (-NAN)", !(isinf (-NANFUNC ("")))); \
  80. check (#FLOAT " NAN != NAN", NANFUNC ("") != NANFUNC ("")); \
  81. \
  82. /* test if EPSILON is ok */ \
  83. x1 = 1.0; \
  84. x2 = x1 + EPSILON; \
  85. check (#FLOAT " 1 != 1+EPSILON", x1 != x2); \
  86. \
  87. x1 = 1.0; \
  88. x2 = x1 - EPSILON; \
  89. check (#FLOAT " 1 != 1-EPSILON", x1 != x2); \
  90. \
  91. /* test if HUGE_VALx is ok */ \
  92. x1 = HUGEVAL; \
  93. check (#FLOAT " isinf (HUGE_VALx) == +1", isinf (x1) == +1); \
  94. x1 = - HUGEVAL; \
  95. check (#FLOAT " isinf (-HUGE_VALx) == -1", isinf (x1) == -1); \
  96. }
  97. TEST_FUNC (float_test, float, nanf, FLT_EPSILON, HUGE_VALF)
  98. TEST_FUNC (double_test, double, nan, DBL_EPSILON, HUGE_VAL)
  99. #ifndef NO_LONG_DOUBLE
  100. TEST_FUNC (ldouble_test, long double, nanl, LDBL_EPSILON, HUGE_VALL)
  101. #endif
  102. int
  103. main (void)
  104. {
  105. float_test ();
  106. double_test ();
  107. #ifndef NO_LONG_DOUBLE
  108. ldouble_test ();
  109. #endif
  110. return errors != 0;
  111. }