check.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Functionality for reporting test results.
  2. Copyright (C) 2016-2019 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. #ifndef SUPPORT_CHECK_H
  16. #define SUPPORT_CHECK_H
  17. #include <sys/cdefs.h>
  18. __BEGIN_DECLS
  19. /* Record a test failure, print the failure message to standard output
  20. and return 1. */
  21. #define FAIL_RET(...) \
  22. return support_print_failure_impl (__FILE__, __LINE__, __VA_ARGS__)
  23. /* Print the failure message and terminate the process with STATUS.
  24. Record a the process as failed if STATUS is neither EXIT_SUCCESS
  25. nor EXIT_UNSUPPORTED. */
  26. #define FAIL_EXIT(status, ...) \
  27. support_exit_failure_impl (status, __FILE__, __LINE__, __VA_ARGS__)
  28. /* Record a test failure, print the failure message and terminate with
  29. exit status 1. */
  30. #define FAIL_EXIT1(...) \
  31. support_exit_failure_impl (1, __FILE__, __LINE__, __VA_ARGS__)
  32. /* Print failure message and terminate with as unsupported test (exit
  33. status of 77). */
  34. #define FAIL_UNSUPPORTED(...) \
  35. support_exit_failure_impl (77, __FILE__, __LINE__, __VA_ARGS__)
  36. /* Record a test failure (but continue executing) if EXPR evaluates to
  37. false. */
  38. #define TEST_VERIFY(expr) \
  39. ({ \
  40. if (expr) \
  41. ; \
  42. else \
  43. support_test_verify_impl (__FILE__, __LINE__, #expr); \
  44. })
  45. /* Record a test failure and exit if EXPR evaluates to false. */
  46. #define TEST_VERIFY_EXIT(expr) \
  47. ({ \
  48. if (expr) \
  49. ; \
  50. else \
  51. support_test_verify_exit_impl \
  52. (1, __FILE__, __LINE__, #expr); \
  53. })
  54. int support_print_failure_impl (const char *file, int line,
  55. const char *format, ...)
  56. __attribute__ ((nonnull (1), format (printf, 3, 4)));
  57. void support_exit_failure_impl (int exit_status,
  58. const char *file, int line,
  59. const char *format, ...)
  60. __attribute__ ((noreturn, nonnull (2), format (printf, 4, 5)));
  61. void support_test_verify_impl (const char *file, int line,
  62. const char *expr);
  63. void support_test_verify_exit_impl (int status, const char *file, int line,
  64. const char *expr)
  65. __attribute__ ((noreturn));
  66. /* Record a test failure. This function returns and does not
  67. terminate the process. The failure counter is stored in a shared
  68. memory mapping, so that failures reported in child processes are
  69. visible to the parent process and test driver. This function
  70. depends on initialization by an ELF constructor, so it can only be
  71. invoked after the test driver has run. Note that this function
  72. does not support reporting failures from a DSO. */
  73. void support_record_failure (void);
  74. /* Static assertion, under a common name for both C++ and C11. */
  75. #ifdef __cplusplus
  76. # define support_static_assert static_assert
  77. #else
  78. # define support_static_assert _Static_assert
  79. #endif
  80. /* Compare the two integers LEFT and RIGHT and report failure if they
  81. are different. */
  82. #define TEST_COMPARE(left, right) \
  83. ({ \
  84. /* + applies the integer promotions, for bitfield support. */ \
  85. typedef __typeof__ (+ (left)) __left_type; \
  86. typedef __typeof__ (+ (right)) __right_type; \
  87. __left_type __left_value = (left); \
  88. __right_type __right_value = (right); \
  89. int __left_is_positive = __left_value > 0; \
  90. int __right_is_positive = __right_value > 0; \
  91. /* Prevent use with floating-point types. */ \
  92. support_static_assert ((__left_type) 1.0 == (__left_type) 1.5, \
  93. "left value has floating-point type"); \
  94. support_static_assert ((__right_type) 1.0 == (__right_type) 1.5, \
  95. "right value has floating-point type"); \
  96. /* Prevent accidental use with larger-than-long long types. */ \
  97. support_static_assert (sizeof (__left_value) <= sizeof (long long), \
  98. "left value fits into long long"); \
  99. support_static_assert (sizeof (__right_value) <= sizeof (long long), \
  100. "right value fits into long long"); \
  101. /* Compare the value. */ \
  102. if (__left_value != __right_value \
  103. || __left_is_positive != __right_is_positive) \
  104. /* Pass the sign for printing the correct value. */ \
  105. support_test_compare_failure \
  106. (__FILE__, __LINE__, \
  107. #left, __left_value, __left_is_positive, sizeof (__left_type), \
  108. #right, __right_value, __right_is_positive, sizeof (__right_type)); \
  109. })
  110. /* Internal implementation of TEST_COMPARE. LEFT_POSITIVE and
  111. RIGHT_POSITIVE are used to store the sign separately, so that both
  112. unsigned long long and long long arguments fit into LEFT_VALUE and
  113. RIGHT_VALUE, and the function can still print the original value.
  114. LEFT_SIZE and RIGHT_SIZE specify the size of the argument in bytes,
  115. for hexadecimal formatting. */
  116. void support_test_compare_failure (const char *file, int line,
  117. const char *left_expr,
  118. long long left_value,
  119. int left_positive,
  120. int left_size,
  121. const char *right_expr,
  122. long long right_value,
  123. int right_positive,
  124. int right_size);
  125. /* Compare [LEFT, LEFT + LEFT_LENGTH) with [RIGHT, RIGHT +
  126. RIGHT_LENGTH) and report a test failure if the arrays are
  127. different. LEFT_LENGTH and RIGHT_LENGTH are measured in bytes. If
  128. the length is null, the corresponding pointer is ignored (i.e., it
  129. can be NULL). The blobs should be reasonably short because on
  130. mismatch, both are printed. */
  131. #define TEST_COMPARE_BLOB(left, left_length, right, right_length) \
  132. (support_test_compare_blob (left, left_length, right, right_length, \
  133. __FILE__, __LINE__, \
  134. #left, #left_length, #right, #right_length))
  135. void support_test_compare_blob (const void *left,
  136. unsigned long int left_length,
  137. const void *right,
  138. unsigned long int right_length,
  139. const char *file, int line,
  140. const char *left_exp, const char *left_len_exp,
  141. const char *right_exp,
  142. const char *right_len_exp);
  143. /* Compare the strings LEFT and RIGHT and report a test failure if
  144. they are different. Also report failure if one of the arguments is
  145. a null pointer and the other is not. The strings should be
  146. reasonably short because on mismatch, both are printed. */
  147. #define TEST_COMPARE_STRING(left, right) \
  148. (support_test_compare_string (left, right, __FILE__, __LINE__, \
  149. #left, #right))
  150. void support_test_compare_string (const char *left, const char *right,
  151. const char *file, int line,
  152. const char *left_expr,
  153. const char *right_expr);
  154. /* Internal function called by the test driver. */
  155. int support_report_failure (int status)
  156. __attribute__ ((weak, warn_unused_result));
  157. /* Internal function used to test the failure recording framework. */
  158. void support_record_failure_reset (void);
  159. /* Returns true or false depending on whether there have been test
  160. failures or not. */
  161. int support_record_failure_is_failed (void);
  162. __END_DECLS
  163. #endif /* SUPPORT_CHECK_H */