c99_test.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <math.h>
  2. #include <float.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <limits.h>
  6. #include <stdio.h>
  7. #define check_d1(func, param, expected) \
  8. do { \
  9. int err; hex_union ur; hex_union up; \
  10. double result = func(param); up.f = param; ur.f = result; \
  11. errors += (err = (result != (expected))); \
  12. err \
  13. ? printf("FAIL: %s(%g/"HEXFMT")=%g/"HEXFMT" (expected %g)\n", \
  14. #func, (double)(param), (long long)up.hex, result, (long long)ur.hex, (double)(expected)) \
  15. : printf("PASS: %s(%g)=%g\n", #func, (double)(param), result); \
  16. } while (0)
  17. #define check_i1(func, param, expected) \
  18. do { \
  19. int err; hex_union up; \
  20. long long result = func(param); up.f = param; \
  21. errors += (err = (result != (expected))); \
  22. err \
  23. ? printf("FAIL: %s(%g/"HEXFMT")=%lld/%llu (expected %llu)\n", \
  24. #func, (double)(param), (long long)up.hex, result, result, (long long)(expected)) \
  25. : printf("PASS: %s(%g)=%lld/%llu\n", #func, (double)(param), result, result); \
  26. } while (0)
  27. #define HEXFMT "%08llx"
  28. typedef union {
  29. double f;
  30. uint64_t hex;
  31. } hex_union;
  32. double zero = 0.0;
  33. double minus_zero = 0.0;
  34. double nan_value = 0.0;
  35. int errors = 0;
  36. int main(void)
  37. {
  38. nan_value /= nan_value;
  39. minus_zero = copysign(zero, -1.0);
  40. check_i1(isfinite, 1.0, 1);
  41. check_i1(isfinite, 2.0, 1);
  42. check_i1(isfinite, 3.0, 1);
  43. check_i1(isfinite, DBL_MAX, 1);
  44. check_i1(isfinite, FLT_MAX, 1);
  45. check_i1(isfinite, HUGE_VAL, 0);
  46. check_i1(isfinite, HUGE_VALF, 0);
  47. check_i1(isfinite, HUGE_VALL, 0);
  48. check_i1(isfinite, nan_value, 0);
  49. check_i1(isfinite, nan_value, 0);
  50. check_i1(isfinite, nan_value, 0);
  51. check_i1(isnan, 1.0, 0);
  52. check_i1(isnan, 2.0, 0);
  53. check_i1(isnan, 3.0, 0);
  54. check_i1(isnan, DBL_MAX, 0);
  55. check_i1(isnan, FLT_MAX, 0);
  56. check_i1(isnan, HUGE_VAL, 0);
  57. check_i1(isnan, HUGE_VALF, 0);
  58. check_i1(isnan, HUGE_VALL, 0);
  59. check_i1(isnan, (float)HUGE_VALL, 0);
  60. check_i1(isnan, nan_value, 1);
  61. check_i1(isnan, nan_value, 1);
  62. check_i1(isnan, nan_value, 1);
  63. check_i1(isinf, 1.0, 0);
  64. check_i1(isinf, 2.0, 0);
  65. check_i1(isinf, 3.0, 0);
  66. check_i1(isinf, DBL_MAX, 0);
  67. check_i1(isinf, FLT_MAX, 0);
  68. check_i1(isinf, (float)DBL_MAX, 1);
  69. check_i1(isinf, HUGE_VAL, 1);
  70. check_i1(isinf, HUGE_VALF, 1);
  71. check_i1(isinf, HUGE_VALL, 1);
  72. check_i1(isinf, (float)HUGE_VALL, 1);
  73. check_i1(isinf, nan_value, 0);
  74. check_i1(isinf, nan_value, 0);
  75. check_i1(isinf, nan_value, 0);
  76. check_i1(fpclassify, minus_zero, FP_ZERO);
  77. check_i1(fpclassify, 0.0, FP_ZERO);
  78. check_i1(fpclassify, 1.0, FP_NORMAL);
  79. check_i1(fpclassify, 2.0, FP_NORMAL);
  80. check_i1(fpclassify, 3.0, FP_NORMAL);
  81. check_i1(fpclassify, DBL_MIN/1.01, FP_SUBNORMAL);
  82. check_i1(fpclassify, DBL_MIN, FP_NORMAL);
  83. check_i1(fpclassify, DBL_MAX, FP_NORMAL);
  84. check_i1(fpclassify, FLT_MAX, FP_NORMAL);
  85. check_i1(fpclassify, DBL_MAX, FP_NORMAL);
  86. check_i1(fpclassify, DBL_MAX*1.01, FP_INFINITE);
  87. check_i1(fpclassify, HUGE_VAL, FP_INFINITE);
  88. check_i1(fpclassify, HUGE_VALF, FP_INFINITE);
  89. check_i1(fpclassify, HUGE_VALL, FP_INFINITE);
  90. check_i1(fpclassify, (float)HUGE_VALL, FP_INFINITE);
  91. check_i1(fpclassify, nan_value, FP_NAN);
  92. check_i1(fpclassify, nan_value, FP_NAN);
  93. check_i1(fpclassify, nan_value, FP_NAN);
  94. check_i1(!!signbit, -1.0, 1);
  95. check_i1(!!signbit, minus_zero, 1);
  96. check_i1(!!signbit, 0.0, 0);
  97. check_i1(!!signbit, HUGE_VAL, 0);
  98. check_i1(!!signbit, HUGE_VALF, 0);
  99. check_i1(!!signbit, HUGE_VALL, 0);
  100. check_i1(!!signbit, -HUGE_VAL, 1);
  101. check_i1(!!signbit, -HUGE_VALF, 1);
  102. check_i1(!!signbit, -HUGE_VALL, 1);
  103. printf("Errors: %d\n", errors);
  104. return errors;
  105. }