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

source code of glibc/support/check.h