1 | /* Functionality for reporting test results. |
2 | Copyright (C) 2016-2021 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 | |
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 | |
67 | |
68 | |
69 | int support_print_failure_impl (const char *file, int line, |
70 | const char *format, ...) |
71 | __attribute__ ((nonnull (1), format (printf, 3, 4))); |
72 | void support_exit_failure_impl (int exit_status, |
73 | const char *file, int line, |
74 | const char *format, ...) |
75 | __attribute__ ((noreturn, nonnull (2), format (printf, 4, 5))); |
76 | void support_test_verify_impl (const char *file, int line, |
77 | const char *expr); |
78 | void support_test_verify_exit_impl (int status, const char *file, int line, |
79 | const char *expr) |
80 | __attribute__ ((noreturn)); |
81 | |
82 | /* Record a test failure. This function returns and does not |
83 | terminate the process. The failure counter is stored in a shared |
84 | memory mapping, so that failures reported in child processes are |
85 | visible to the parent process and test driver. This function |
86 | depends on initialization by an ELF constructor, so it can only be |
87 | invoked after the test driver has run. Note that this function |
88 | does not support reporting failures from a DSO. */ |
89 | void support_record_failure (void); |
90 | |
91 | /* Static assertion, under a common name for both C++ and C11. */ |
92 | #ifdef __cplusplus |
93 | # define support_static_assert static_assert |
94 | #else |
95 | # define support_static_assert _Static_assert |
96 | #endif |
97 | |
98 | /* Compare the two integers LEFT and RIGHT and report failure if they |
99 | are different. */ |
100 | #define TEST_COMPARE(left, right) \ |
101 | ({ \ |
102 | /* + applies the integer promotions, for bitfield support. */ \ |
103 | typedef __typeof__ (+ (left)) __left_type; \ |
104 | typedef __typeof__ (+ (right)) __right_type; \ |
105 | __left_type __left_value = (left); \ |
106 | __right_type __right_value = (right); \ |
107 | int __left_is_positive = __left_value > 0; \ |
108 | int __right_is_positive = __right_value > 0; \ |
109 | /* Prevent use with floating-point types. */ \ |
110 | support_static_assert ((__left_type) 1.0 == (__left_type) 1.5, \ |
111 | "left value has floating-point type"); \ |
112 | support_static_assert ((__right_type) 1.0 == (__right_type) 1.5, \ |
113 | "right value has floating-point type"); \ |
114 | /* Prevent accidental use with larger-than-long long types. */ \ |
115 | support_static_assert (sizeof (__left_value) <= sizeof (long long), \ |
116 | "left value fits into long long"); \ |
117 | support_static_assert (sizeof (__right_value) <= sizeof (long long), \ |
118 | "right value fits into long long"); \ |
119 | /* Compare the value. */ \ |
120 | if (__left_value != __right_value \ |
121 | || __left_is_positive != __right_is_positive) \ |
122 | /* Pass the sign for printing the correct value. */ \ |
123 | support_test_compare_failure \ |
124 | (__FILE__, __LINE__, \ |
125 | #left, __left_value, __left_is_positive, sizeof (__left_type), \ |
126 | #right, __right_value, __right_is_positive, sizeof (__right_type)); \ |
127 | }) |
128 | |
129 | /* Internal implementation of TEST_COMPARE. LEFT_POSITIVE and |
130 | RIGHT_POSITIVE are used to store the sign separately, so that both |
131 | unsigned long long and long long arguments fit into LEFT_VALUE and |
132 | RIGHT_VALUE, and the function can still print the original value. |
133 | LEFT_SIZE and RIGHT_SIZE specify the size of the argument in bytes, |
134 | for hexadecimal formatting. */ |
135 | void support_test_compare_failure (const char *file, int line, |
136 | const char *left_expr, |
137 | long long left_value, |
138 | int left_positive, |
139 | int left_size, |
140 | const char *right_expr, |
141 | long long right_value, |
142 | int right_positive, |
143 | int right_size); |
144 | |
145 | |
146 | /* Compare [LEFT, LEFT + LEFT_LENGTH) with [RIGHT, RIGHT + |
147 | RIGHT_LENGTH) and report a test failure if the arrays are |
148 | different. LEFT_LENGTH and RIGHT_LENGTH are measured in bytes. If |
149 | the length is null, the corresponding pointer is ignored (i.e., it |
150 | can be NULL). The blobs should be reasonably short because on |
151 | mismatch, both are printed. */ |
152 | #define TEST_COMPARE_BLOB(left, left_length, right, right_length) \ |
153 | (support_test_compare_blob (left, left_length, right, right_length, \ |
154 | __FILE__, __LINE__, \ |
155 | #left, #left_length, #right, #right_length)) |
156 | |
157 | void support_test_compare_blob (const void *left, |
158 | unsigned long int left_length, |
159 | const void *right, |
160 | unsigned long int right_length, |
161 | const char *file, int line, |
162 | const char *left_exp, const char *left_len_exp, |
163 | const char *right_exp, |
164 | const char *right_len_exp); |
165 | |
166 | /* Compare the strings LEFT and RIGHT and report a test failure if |
167 | they are different. Also report failure if one of the arguments is |
168 | a null pointer and the other is not. The strings should be |
169 | reasonably short because on mismatch, both are printed. */ |
170 | #define TEST_COMPARE_STRING(left, right) \ |
171 | (support_test_compare_string (left, right, __FILE__, __LINE__, \ |
172 | #left, #right)) |
173 | |
174 | void support_test_compare_string (const char *left, const char *right, |
175 | const char *file, int line, |
176 | const char *left_expr, |
177 | const char *right_expr); |
178 | |
179 | /* Internal function called by the test driver. */ |
180 | int support_report_failure (int status) |
181 | __attribute__ ((weak, warn_unused_result)); |
182 | |
183 | /* Internal function used to test the failure recording framework. */ |
184 | void support_record_failure_reset (void); |
185 | |
186 | /* Returns true or false depending on whether there have been test |
187 | failures or not. */ |
188 | int support_record_failure_is_failed (void); |
189 | |
190 | __END_DECLS |
191 | |
192 | #endif /* SUPPORT_CHECK_H */ |
193 | |