1/* Functionality for reporting test results.
2 Copyright (C) 2016-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#ifndef SUPPORT_CHECK_H
20#define SUPPORT_CHECK_H
21
22#include <sys/cdefs.h>
23
24__BEGIN_DECLS
25
26/* Record a test failure, print the failure message to standard output
27 and return 1. */
28#define FAIL_RET(...) \
29 return support_print_failure_impl (__FILE__, __LINE__, __VA_ARGS__)
30
31/* Print the failure message and terminate the process with STATUS.
32 Record a the process as failed if STATUS is neither EXIT_SUCCESS
33 nor EXIT_UNSUPPORTED. */
34#define FAIL_EXIT(status, ...) \
35 support_exit_failure_impl (status, __FILE__, __LINE__, __VA_ARGS__)
36
37/* Record a test failure, print the failure message and terminate with
38 exit status 1. */
39#define FAIL_EXIT1(...) \
40 support_exit_failure_impl (1, __FILE__, __LINE__, __VA_ARGS__)
41
42/* Print failure message and terminate with as unsupported test (exit
43 status of 77). */
44#define FAIL_UNSUPPORTED(...) \
45 support_exit_failure_impl (77, __FILE__, __LINE__, __VA_ARGS__)
46
47/* Record a test failure (but continue executing) if EXPR evaluates to
48 false. */
49#define TEST_VERIFY(expr) \
50 ({ \
51 if (expr) \
52 ; \
53 else \
54 support_test_verify_impl (__FILE__, __LINE__, #expr); \
55 })
56
57/* Record a test failure and exit if EXPR evaluates to false. */
58#define TEST_VERIFY_EXIT(expr) \
59 ({ \
60 if (expr) \
61 ; \
62 else \
63 support_test_verify_exit_impl \
64 (1, __FILE__, __LINE__, #expr); \
65 })
66
67int support_print_failure_impl (const char *file, int line,
68 const char *format, ...)
69 __attribute__ ((nonnull (1), format (printf, 3, 4)));
70void support_exit_failure_impl (int exit_status,
71 const char *file, int line,
72 const char *format, ...)
73 __attribute__ ((noreturn, nonnull (2), format (printf, 4, 5)));
74void support_test_verify_impl (const char *file, int line,
75 const char *expr);
76void support_test_verify_exit_impl (int status, const char *file, int line,
77 const char *expr)
78 __attribute__ ((noreturn));
79
80/* Record a test failure. This function returns and does not
81 terminate the process. The failure counter is stored in a shared
82 memory mapping, so that failures reported in child processes are
83 visible to the parent process and test driver. This function
84 depends on initialization by an ELF constructor, so it can only be
85 invoked after the test driver has run. Note that this function
86 does not support reporting failures from a DSO. */
87void support_record_failure (void);
88
89/* Static assertion, under a common name for both C++ and C11. */
90#ifdef __cplusplus
91# define support_static_assert static_assert
92#else
93# define support_static_assert _Static_assert
94#endif
95
96/* Compare the two integers LEFT and RIGHT and report failure if they
97 are different. */
98#define TEST_COMPARE(left, right) \
99 ({ \
100 /* + applies the integer promotions, for bitfield support. */ \
101 typedef __typeof__ (+ (left)) __left_type; \
102 typedef __typeof__ (+ (right)) __right_type; \
103 __left_type __left_value = (left); \
104 __right_type __right_value = (right); \
105 int __left_is_positive = __left_value > 0; \
106 int __right_is_positive = __right_value > 0; \
107 /* Prevent use with floating-point types. */ \
108 support_static_assert ((__left_type) 1.0 == (__left_type) 1.5, \
109 "left value has floating-point type"); \
110 support_static_assert ((__right_type) 1.0 == (__right_type) 1.5, \
111 "right value has floating-point type"); \
112 /* Prevent accidental use with larger-than-long long types. */ \
113 support_static_assert (sizeof (__left_value) <= sizeof (long long), \
114 "left value fits into long long"); \
115 support_static_assert (sizeof (__right_value) <= sizeof (long long), \
116 "right value fits into long long"); \
117 /* Compare the value. */ \
118 if (__left_value != __right_value \
119 || __left_is_positive != __right_is_positive) \
120 /* Pass the sign for printing the correct value. */ \
121 support_test_compare_failure \
122 (__FILE__, __LINE__, \
123 #left, __left_value, __left_is_positive, sizeof (__left_type), \
124 #right, __right_value, __right_is_positive, sizeof (__right_type)); \
125 })
126
127/* Internal implementation of TEST_COMPARE. LEFT_POSITIVE and
128 RIGHT_POSITIVE are used to store the sign separately, so that both
129 unsigned long long and long long arguments fit into LEFT_VALUE and
130 RIGHT_VALUE, and the function can still print the original value.
131 LEFT_SIZE and RIGHT_SIZE specify the size of the argument in bytes,
132 for hexadecimal formatting. */
133void support_test_compare_failure (const char *file, int line,
134 const char *left_expr,
135 long long left_value,
136 int left_positive,
137 int left_size,
138 const char *right_expr,
139 long long right_value,
140 int right_positive,
141 int right_size);
142
143
144/* Internal function called by the test driver. */
145int support_report_failure (int status)
146 __attribute__ ((weak, warn_unused_result));
147
148/* Internal function used to test the failure recording framework. */
149void support_record_failure_reset (void);
150
151__END_DECLS
152
153#endif /* SUPPORT_CHECK_H */
154