Blame support/check.h

Packit 6c4009
/* Functionality for reporting test results.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef SUPPORT_CHECK_H
Packit 6c4009
#define SUPPORT_CHECK_H
Packit 6c4009
Packit 6c4009
#include <sys/cdefs.h>
Packit 6c4009
Packit 6c4009
__BEGIN_DECLS
Packit 6c4009
Packit 6c4009
/* Record a test failure, print the failure message to standard output
Packit 6c4009
   and return 1.  */
Packit 6c4009
#define FAIL_RET(...) \
Packit 6c4009
  return support_print_failure_impl (__FILE__, __LINE__, __VA_ARGS__)
Packit 6c4009
Packit 6c4009
/* Print the failure message and terminate the process with STATUS.
Packit 6c4009
   Record a the process as failed if STATUS is neither EXIT_SUCCESS
Packit 6c4009
   nor EXIT_UNSUPPORTED.  */
Packit 6c4009
#define FAIL_EXIT(status, ...) \
Packit 6c4009
  support_exit_failure_impl (status, __FILE__, __LINE__, __VA_ARGS__)
Packit 6c4009
Packit 6c4009
/* Record a test failure, print the failure message and terminate with
Packit 6c4009
   exit status 1.  */
Packit 6c4009
#define FAIL_EXIT1(...) \
Packit 6c4009
  support_exit_failure_impl (1, __FILE__, __LINE__, __VA_ARGS__)
Packit 6c4009
Packit 6c4009
/* Print failure message and terminate with as unsupported test (exit
Packit 6c4009
   status of 77).  */
Packit 6c4009
#define FAIL_UNSUPPORTED(...) \
Packit 6c4009
  support_exit_failure_impl (77, __FILE__, __LINE__, __VA_ARGS__)
Packit 6c4009
Packit 6c4009
/* Record a test failure (but continue executing) if EXPR evaluates to
Packit 6c4009
   false.  */
Packit 6c4009
#define TEST_VERIFY(expr)                                       \
Packit 6c4009
  ({                                                            \
Packit 6c4009
    if (expr)                                                   \
Packit 6c4009
      ;                                                         \
Packit 6c4009
    else                                                        \
Packit 6c4009
      support_test_verify_impl (__FILE__, __LINE__, #expr);     \
Packit 6c4009
  })
Packit 6c4009
Packit 6c4009
/* Record a test failure and exit if EXPR evaluates to false.  */
Packit 6c4009
#define TEST_VERIFY_EXIT(expr)                                  \
Packit 6c4009
  ({                                                            \
Packit 6c4009
    if (expr)                                                   \
Packit 6c4009
      ;                                                         \
Packit 6c4009
    else                                                        \
Packit 6c4009
      support_test_verify_exit_impl                             \
Packit 6c4009
        (1, __FILE__, __LINE__, #expr);                         \
Packit 6c4009
  })
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
int support_print_failure_impl (const char *file, int line,
Packit 6c4009
                                const char *format, ...)
Packit 6c4009
  __attribute__ ((nonnull (1), format (printf, 3, 4)));
Packit 6c4009
void support_exit_failure_impl (int exit_status,
Packit 6c4009
                                const char *file, int line,
Packit 6c4009
                                const char *format, ...)
Packit 6c4009
  __attribute__ ((noreturn, nonnull (2), format (printf, 4, 5)));
Packit 6c4009
void support_test_verify_impl (const char *file, int line,
Packit 6c4009
                               const char *expr);
Packit 6c4009
void support_test_verify_exit_impl (int status, const char *file, int line,
Packit 6c4009
                                    const char *expr)
Packit 6c4009
  __attribute__ ((noreturn));
Packit 6c4009
Packit 6c4009
/* Record a test failure.  This function returns and does not
Packit 6c4009
   terminate the process.  The failure counter is stored in a shared
Packit 6c4009
   memory mapping, so that failures reported in child processes are
Packit 6c4009
   visible to the parent process and test driver.  This function
Packit 6c4009
   depends on initialization by an ELF constructor, so it can only be
Packit 6c4009
   invoked after the test driver has run.  Note that this function
Packit 6c4009
   does not support reporting failures from a DSO.  */
Packit 6c4009
void support_record_failure (void);
Packit 6c4009
Packit 6c4009
/* Static assertion, under a common name for both C++ and C11.  */
Packit 6c4009
#ifdef __cplusplus
Packit 6c4009
# define support_static_assert static_assert
Packit 6c4009
#else
Packit 6c4009
# define support_static_assert _Static_assert
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Compare the two integers LEFT and RIGHT and report failure if they
Packit 6c4009
   are different.  */
Packit 6c4009
#define TEST_COMPARE(left, right)                                       \
Packit 6c4009
  ({                                                                    \
Packit 6c4009
    /* + applies the integer promotions, for bitfield support.   */     \
Packit 6c4009
    typedef __typeof__ (+ (left)) __left_type;                          \
Packit 6c4009
    typedef __typeof__ (+ (right)) __right_type;                        \
Packit 6c4009
    __left_type __left_value = (left);                                  \
Packit 6c4009
    __right_type __right_value = (right);                               \
Packit 6c4009
    int __left_is_positive = __left_value > 0;                          \
Packit 6c4009
    int __right_is_positive = __right_value > 0;                        \
Packit 6c4009
    /* Prevent use with floating-point types.  */                       \
Packit 6c4009
    support_static_assert ((__left_type) 1.0 == (__left_type) 1.5,      \
Packit 6c4009
                           "left value has floating-point type");       \
Packit 6c4009
    support_static_assert ((__right_type) 1.0 == (__right_type) 1.5,    \
Packit 6c4009
                           "right value has floating-point type");      \
Packit 6c4009
    /* Prevent accidental use with larger-than-long long types.  */     \
Packit 6c4009
    support_static_assert (sizeof (__left_value) <= sizeof (long long), \
Packit 6c4009
                           "left value fits into long long");           \
Packit 6c4009
    support_static_assert (sizeof (__right_value) <= sizeof (long long), \
Packit 6c4009
                    "right value fits into long long");                 \
Packit 6c4009
    /* Compare the value.  */                                           \
Packit 6c4009
    if (__left_value != __right_value                                   \
Packit 6c4009
        || __left_is_positive != __right_is_positive)                   \
Packit 6c4009
      /* Pass the sign for printing the correct value.  */              \
Packit 6c4009
      support_test_compare_failure                                      \
Packit 6c4009
        (__FILE__, __LINE__,                                            \
Packit 6c4009
         #left, __left_value, __left_is_positive, sizeof (__left_type), \
Packit 6c4009
         #right, __right_value, __right_is_positive, sizeof (__right_type)); \
Packit 6c4009
  })
Packit 6c4009
Packit 6c4009
/* Internal implementation of TEST_COMPARE.  LEFT_POSITIVE and
Packit 6c4009
   RIGHT_POSITIVE are used to store the sign separately, so that both
Packit 6c4009
   unsigned long long and long long arguments fit into LEFT_VALUE and
Packit 6c4009
   RIGHT_VALUE, and the function can still print the original value.
Packit 6c4009
   LEFT_SIZE and RIGHT_SIZE specify the size of the argument in bytes,
Packit 6c4009
   for hexadecimal formatting.  */
Packit 6c4009
void support_test_compare_failure (const char *file, int line,
Packit 6c4009
                                   const char *left_expr,
Packit 6c4009
                                   long long left_value,
Packit 6c4009
                                   int left_positive,
Packit 6c4009
                                   int left_size,
Packit 6c4009
                                   const char *right_expr,
Packit 6c4009
                                   long long right_value,
Packit 6c4009
                                   int right_positive,
Packit 6c4009
                                   int right_size);
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Compare [LEFT, LEFT + LEFT_LENGTH) with [RIGHT, RIGHT +
Packit 6c4009
   RIGHT_LENGTH) and report a test failure if the arrays are
Packit 6c4009
   different.  LEFT_LENGTH and RIGHT_LENGTH are measured in bytes.  If
Packit 6c4009
   the length is null, the corresponding pointer is ignored (i.e., it
Packit 6c4009
   can be NULL).  The blobs should be reasonably short because on
Packit 6c4009
   mismatch, both are printed.  */
Packit 6c4009
#define TEST_COMPARE_BLOB(left, left_length, right, right_length)       \
Packit 6c4009
  (support_test_compare_blob (left, left_length, right, right_length,   \
Packit 6c4009
                              __FILE__, __LINE__,                       \
Packit 6c4009
                              #left, #left_length, #right, #right_length))
Packit 6c4009
Packit 6c4009
void support_test_compare_blob (const void *left,
Packit 6c4009
                                unsigned long int left_length,
Packit 6c4009
                                const void *right,
Packit 6c4009
                                unsigned long int right_length,
Packit 6c4009
                                const char *file, int line,
Packit 6c4009
                                const char *left_exp, const char *left_len_exp,
Packit 6c4009
                                const char *right_exp,
Packit 6c4009
                                const char *right_len_exp);
Packit 6c4009
Packit 6c4009
/* Internal function called by the test driver.  */
Packit 6c4009
int support_report_failure (int status)
Packit 6c4009
  __attribute__ ((weak, warn_unused_result));
Packit 6c4009
Packit 6c4009
/* Internal function used to test the failure recording framework.  */
Packit 6c4009
void support_record_failure_reset (void);
Packit 6c4009
Packit 6c4009
__END_DECLS
Packit 6c4009
Packit 6c4009
#endif /* SUPPORT_CHECK_H */