basic-test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #define _ISOC99_SOURCE
  17. #include <math.h>
  18. #include <float.h>
  19. #include <stdio.h>
  20. static int errors = 0;
  21. static void
  22. check (const char *testname, int result)
  23. {
  24. if (!result) {
  25. printf ("Failure: %s\n", testname);
  26. errors++;
  27. }
  28. }
  29. #define TEST_FUNC(NAME, FLOAT, NANFUNC, EPSILON, HUGEVAL) \
  30. static void \
  31. NAME (void) \
  32. { \
  33. /* Variables are declared volatile to forbid some compiler \
  34. optimizations. */ \
  35. volatile FLOAT Inf_var, NaN_var, zero_var, one_var; \
  36. FLOAT x1, x2; \
  37. \
  38. zero_var = 0.0; \
  39. one_var = 1.0; \
  40. NaN_var = zero_var/zero_var; \
  41. Inf_var = one_var / zero_var; \
  42. \
  43. (void) &zero_var; \
  44. (void) &one_var; \
  45. (void) &NaN_var; \
  46. (void) &Inf_var; \
  47. \
  48. \
  49. check (#FLOAT " isinf (inf) == 1", isinf (Inf_var) == 1); \
  50. check (#FLOAT " isinf (-inf) == -1", isinf (-Inf_var) == -1); \
  51. check (#FLOAT " !isinf (1)", !(isinf (one_var))); \
  52. check (#FLOAT " !isinf (NaN)", !(isinf (NaN_var))); \
  53. \
  54. check (#FLOAT " isnan (NaN)", isnan (NaN_var)); \
  55. check (#FLOAT " isnan (-NaN)", isnan (-NaN_var)); \
  56. check (#FLOAT " !isnan (1)", !(isnan (one_var))); \
  57. check (#FLOAT " !isnan (inf)", !(isnan (Inf_var))); \
  58. \
  59. check (#FLOAT " inf == inf", Inf_var == Inf_var); \
  60. check (#FLOAT " -inf == -inf", -Inf_var == -Inf_var); \
  61. check (#FLOAT " inf != -inf", Inf_var != -Inf_var); \
  62. check (#FLOAT " NaN != NaN", NaN_var != NaN_var); \
  63. \
  64. /* \
  65. the same tests but this time with NAN from <bits/nan.h> \
  66. NAN is a double const \
  67. */ \
  68. check (#FLOAT " isnan (NAN)", isnan (NAN)); \
  69. check (#FLOAT " isnan (-NAN)", isnan (-NAN)); \
  70. check (#FLOAT " !isinf (NAN)", !(isinf (NAN))); \
  71. check (#FLOAT " !isinf (-NAN)", !(isinf (-NAN))); \
  72. check (#FLOAT " NAN != NAN", NAN != NAN); \
  73. \
  74. /* \
  75. And again with the value returned by the `nan' function. \
  76. */ \
  77. check (#FLOAT " isnan (NAN)", isnan (NANFUNC (""))); \
  78. check (#FLOAT " isnan (-NAN)", isnan (-NANFUNC (""))); \
  79. check (#FLOAT " !isinf (NAN)", !(isinf (NANFUNC ("")))); \
  80. check (#FLOAT " !isinf (-NAN)", !(isinf (-NANFUNC ("")))); \
  81. check (#FLOAT " NAN != NAN", NANFUNC ("") != NANFUNC ("")); \
  82. \
  83. /* test if EPSILON is ok */ \
  84. x1 = 1.0; \
  85. x2 = x1 + EPSILON; \
  86. check (#FLOAT " 1 != 1+EPSILON", x1 != x2); \
  87. \
  88. x1 = 1.0; \
  89. x2 = x1 - EPSILON; \
  90. check (#FLOAT " 1 != 1-EPSILON", x1 != x2); \
  91. \
  92. /* test if HUGE_VALx is ok */ \
  93. x1 = HUGEVAL; \
  94. check (#FLOAT " isinf (HUGE_VALx) == +1", isinf (x1) == +1); \
  95. x1 = - HUGEVAL; \
  96. check (#FLOAT " isinf (-HUGE_VALx) == -1", isinf (x1) == -1); \
  97. }
  98. TEST_FUNC (float_test, float, nanf, FLT_EPSILON, HUGE_VALF)
  99. TEST_FUNC (double_test, double, nan, DBL_EPSILON, HUGE_VAL)
  100. #ifndef NO_LONG_DOUBLE
  101. TEST_FUNC (ldouble_test, long double, nanl, LDBL_EPSILON, HUGE_VALL)
  102. #endif
  103. int
  104. main (void)
  105. {
  106. float_test ();
  107. double_test ();
  108. #ifndef NO_LONG_DOUBLE
  109. ldouble_test ();
  110. #endif
  111. return errors != 0;
  112. }