Blame support/check.h

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