test-nan-payload.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Test nan functions payload handling (bug 16961).
  2. Copyright (C) 2015-2016 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 <float.h>
  16. #include <math.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. /* Avoid built-in functions. */
  21. #define WRAP_NAN(FUNC, STR) \
  22. ({ const char *volatile wns = (STR); FUNC (wns); })
  23. #define WRAP_STRTO(FUNC, STR) \
  24. ({ const char *volatile wss = (STR); FUNC (wss, NULL); })
  25. #define CHECK_IS_NAN(TYPE, A) \
  26. do \
  27. { \
  28. if (isnan (A)) \
  29. puts ("PASS: " #TYPE " " #A); \
  30. else \
  31. { \
  32. puts ("FAIL: " #TYPE " " #A); \
  33. result = 1; \
  34. } \
  35. } \
  36. while (0)
  37. #define CHECK_SAME_NAN(TYPE, A, B) \
  38. do \
  39. { \
  40. if (memcmp (&(A), &(B), sizeof (A)) == 0) \
  41. puts ("PASS: " #TYPE " " #A " = " #B); \
  42. else \
  43. { \
  44. puts ("FAIL: " #TYPE " " #A " = " #B); \
  45. result = 1; \
  46. } \
  47. } \
  48. while (0)
  49. #define CHECK_DIFF_NAN(TYPE, A, B) \
  50. do \
  51. { \
  52. if (memcmp (&(A), &(B), sizeof (A)) != 0) \
  53. puts ("PASS: " #TYPE " " #A " != " #B); \
  54. else \
  55. { \
  56. puts ("FAIL: " #TYPE " " #A " != " #B); \
  57. result = 1; \
  58. } \
  59. } \
  60. while (0)
  61. /* Cannot test payloads by memcmp for formats where NaNs have padding
  62. bits. */
  63. #define CAN_TEST_EQ(MANT_DIG) ((MANT_DIG) != 64 && (MANT_DIG) != 106)
  64. #define RUN_TESTS(TYPE, SFUNC, FUNC, MANT_DIG) \
  65. do \
  66. { \
  67. TYPE n123 = WRAP_NAN (FUNC, "123"); \
  68. CHECK_IS_NAN (TYPE, n123); \
  69. TYPE s123 = WRAP_STRTO (SFUNC, "NAN(123)"); \
  70. CHECK_IS_NAN (TYPE, s123); \
  71. TYPE n456 = WRAP_NAN (FUNC, "456"); \
  72. CHECK_IS_NAN (TYPE, n456); \
  73. TYPE s456 = WRAP_STRTO (SFUNC, "NAN(456)"); \
  74. CHECK_IS_NAN (TYPE, s456); \
  75. TYPE n123x = WRAP_NAN (FUNC, "123)"); \
  76. CHECK_IS_NAN (TYPE, n123x); \
  77. TYPE nemp = WRAP_NAN (FUNC, ""); \
  78. CHECK_IS_NAN (TYPE, nemp); \
  79. TYPE semp = WRAP_STRTO (SFUNC, "NAN()"); \
  80. CHECK_IS_NAN (TYPE, semp); \
  81. TYPE sx = WRAP_STRTO (SFUNC, "NAN"); \
  82. CHECK_IS_NAN (TYPE, sx); \
  83. if (CAN_TEST_EQ (MANT_DIG)) \
  84. CHECK_SAME_NAN (TYPE, n123, s123); \
  85. if (CAN_TEST_EQ (MANT_DIG)) \
  86. CHECK_SAME_NAN (TYPE, n456, s456); \
  87. if (CAN_TEST_EQ (MANT_DIG)) \
  88. CHECK_SAME_NAN (TYPE, nemp, semp); \
  89. if (CAN_TEST_EQ (MANT_DIG)) \
  90. CHECK_SAME_NAN (TYPE, n123x, sx); \
  91. CHECK_DIFF_NAN (TYPE, n123, n456); \
  92. CHECK_DIFF_NAN (TYPE, n123, nemp); \
  93. CHECK_DIFF_NAN (TYPE, n123, n123x); \
  94. CHECK_DIFF_NAN (TYPE, n456, nemp); \
  95. CHECK_DIFF_NAN (TYPE, n456, n123x); \
  96. } \
  97. while (0)
  98. static int
  99. do_test (void)
  100. {
  101. int result = 0;
  102. RUN_TESTS (float, strtof, nanf, FLT_MANT_DIG);
  103. RUN_TESTS (double, strtod, nan, DBL_MANT_DIG);
  104. #ifndef NO_LONG_DOUBLE
  105. RUN_TESTS (long double, strtold, nanl, LDBL_MANT_DIG);
  106. #endif
  107. return result;
  108. }
  109. #define TEST_FUNCTION do_test ()
  110. #include "../test-skeleton.c"