selftest.h revision 1.1.1.4 1 1.1 mrg /* A self-testing framework, for use by -fself-test.
2 1.1.1.4 mrg Copyright (C) 2015-2020 Free Software Foundation, Inc.
3 1.1 mrg
4 1.1 mrg This file is part of GCC.
5 1.1 mrg
6 1.1 mrg GCC is free software; you can redistribute it and/or modify it under
7 1.1 mrg the terms of the GNU General Public License as published by the Free
8 1.1 mrg Software Foundation; either version 3, or (at your option) any later
9 1.1 mrg version.
10 1.1 mrg
11 1.1 mrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 1.1 mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 1.1 mrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 1.1 mrg for more details.
15 1.1 mrg
16 1.1 mrg You should have received a copy of the GNU General Public License
17 1.1 mrg along with GCC; see the file COPYING3. If not see
18 1.1 mrg <http://www.gnu.org/licenses/>. */
19 1.1 mrg
20 1.1 mrg #ifndef GCC_SELFTEST_H
21 1.1 mrg #define GCC_SELFTEST_H
22 1.1 mrg
23 1.1 mrg /* The selftest code should entirely disappear in a production
24 1.1 mrg configuration, hence we guard all of it with #if CHECKING_P. */
25 1.1 mrg
26 1.1 mrg #if CHECKING_P
27 1.1 mrg
28 1.1 mrg namespace selftest {
29 1.1 mrg
30 1.1 mrg /* A struct describing the source-location of a selftest, to make it
31 1.1 mrg easier to track down failing tests. */
32 1.1 mrg
33 1.1.1.4 mrg class location
34 1.1 mrg {
35 1.1.1.4 mrg public:
36 1.1 mrg location (const char *file, int line, const char *function)
37 1.1 mrg : m_file (file), m_line (line), m_function (function) {}
38 1.1 mrg
39 1.1 mrg const char *m_file;
40 1.1 mrg int m_line;
41 1.1 mrg const char *m_function;
42 1.1 mrg };
43 1.1 mrg
44 1.1 mrg /* A macro for use in selftests and by the ASSERT_ macros below,
45 1.1 mrg constructing a selftest::location for the current source location. */
46 1.1 mrg
47 1.1 mrg #define SELFTEST_LOCATION \
48 1.1 mrg (::selftest::location (__FILE__, __LINE__, __FUNCTION__))
49 1.1 mrg
50 1.1 mrg /* The entrypoint for running all tests. */
51 1.1 mrg
52 1.1 mrg extern void run_tests ();
53 1.1 mrg
54 1.1 mrg /* Record the successful outcome of some aspect of the test. */
55 1.1 mrg
56 1.1 mrg extern void pass (const location &loc, const char *msg);
57 1.1 mrg
58 1.1 mrg /* Report the failed outcome of some aspect of the test and abort. */
59 1.1 mrg
60 1.1 mrg extern void fail (const location &loc, const char *msg)
61 1.1 mrg ATTRIBUTE_NORETURN;
62 1.1 mrg
63 1.1 mrg /* As "fail", but using printf-style formatted output. */
64 1.1 mrg
65 1.1 mrg extern void fail_formatted (const location &loc, const char *fmt, ...)
66 1.1 mrg ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN;
67 1.1 mrg
68 1.1 mrg /* Implementation detail of ASSERT_STREQ. */
69 1.1 mrg
70 1.1 mrg extern void assert_streq (const location &loc,
71 1.1.1.3 mrg const char *desc_val1, const char *desc_val2,
72 1.1.1.3 mrg const char *val1, const char *val2);
73 1.1 mrg
74 1.1 mrg /* Implementation detail of ASSERT_STR_CONTAINS. */
75 1.1 mrg
76 1.1 mrg extern void assert_str_contains (const location &loc,
77 1.1 mrg const char *desc_haystack,
78 1.1 mrg const char *desc_needle,
79 1.1 mrg const char *val_haystack,
80 1.1 mrg const char *val_needle);
81 1.1 mrg
82 1.1.1.3 mrg /* Implementation detail of ASSERT_STR_STARTSWITH. */
83 1.1.1.3 mrg
84 1.1.1.3 mrg extern void assert_str_startswith (const location &loc,
85 1.1.1.3 mrg const char *desc_str,
86 1.1.1.3 mrg const char *desc_prefix,
87 1.1.1.3 mrg const char *val_str,
88 1.1.1.3 mrg const char *val_prefix);
89 1.1.1.3 mrg
90 1.1.1.3 mrg
91 1.1 mrg /* A named temporary file for use in selftests.
92 1.1 mrg Usable for writing out files, and as the base class for
93 1.1 mrg temp_source_file.
94 1.1 mrg The file is unlinked in the destructor. */
95 1.1 mrg
96 1.1 mrg class named_temp_file
97 1.1 mrg {
98 1.1 mrg public:
99 1.1 mrg named_temp_file (const char *suffix);
100 1.1 mrg ~named_temp_file ();
101 1.1 mrg const char *get_filename () const { return m_filename; }
102 1.1 mrg
103 1.1 mrg private:
104 1.1 mrg char *m_filename;
105 1.1 mrg };
106 1.1 mrg
107 1.1 mrg /* A class for writing out a temporary sourcefile for use in selftests
108 1.1 mrg of input handling. */
109 1.1 mrg
110 1.1 mrg class temp_source_file : public named_temp_file
111 1.1 mrg {
112 1.1 mrg public:
113 1.1 mrg temp_source_file (const location &loc, const char *suffix,
114 1.1 mrg const char *content);
115 1.1 mrg };
116 1.1 mrg
117 1.1.1.3 mrg /* RAII-style class for avoiding introducing locale-specific differences
118 1.1.1.3 mrg in strings containing localized quote marks, by temporarily overriding
119 1.1.1.3 mrg the "open_quote" and "close_quote" globals to something hardcoded.
120 1.1.1.3 mrg
121 1.1.1.3 mrg Specifically, the C locale's values are used:
122 1.1.1.3 mrg - open_quote becomes "`"
123 1.1.1.3 mrg - close_quote becomes "'"
124 1.1.1.3 mrg for the lifetime of the object. */
125 1.1.1.3 mrg
126 1.1.1.3 mrg class auto_fix_quotes
127 1.1.1.3 mrg {
128 1.1.1.3 mrg public:
129 1.1.1.3 mrg auto_fix_quotes ();
130 1.1.1.3 mrg ~auto_fix_quotes ();
131 1.1.1.3 mrg
132 1.1.1.3 mrg private:
133 1.1.1.3 mrg const char *m_saved_open_quote;
134 1.1.1.3 mrg const char *m_saved_close_quote;
135 1.1.1.3 mrg };
136 1.1.1.3 mrg
137 1.1 mrg /* Various selftests involving location-handling require constructing a
138 1.1 mrg line table and one or more line maps within it.
139 1.1 mrg
140 1.1 mrg For maximum test coverage we want to run these tests with a variety
141 1.1 mrg of situations:
142 1.1 mrg - line_table->default_range_bits: some frontends use a non-zero value
143 1.1 mrg and others use zero
144 1.1 mrg - the fallback modes within line-map.c: there are various threshold
145 1.1.1.3 mrg values for location_t beyond line-map.c changes
146 1.1 mrg behavior (disabling of the range-packing optimization, disabling
147 1.1 mrg of column-tracking). We can exercise these by starting the line_table
148 1.1 mrg at interesting values at or near these thresholds.
149 1.1 mrg
150 1.1 mrg The following struct describes a particular case within our test
151 1.1 mrg matrix. */
152 1.1 mrg
153 1.1.1.4 mrg class line_table_case;
154 1.1 mrg
155 1.1 mrg /* A class for overriding the global "line_table" within a selftest,
156 1.1 mrg restoring its value afterwards. At most one instance of this
157 1.1 mrg class can exist at once, due to the need to keep the old value
158 1.1 mrg of line_table as a GC root. */
159 1.1 mrg
160 1.1 mrg class line_table_test
161 1.1 mrg {
162 1.1 mrg public:
163 1.1 mrg /* Default constructor. Override "line_table", using sane defaults
164 1.1 mrg for the temporary line_table. */
165 1.1 mrg line_table_test ();
166 1.1 mrg
167 1.1 mrg /* Constructor. Override "line_table", using the case described by C. */
168 1.1 mrg line_table_test (const line_table_case &c);
169 1.1 mrg
170 1.1 mrg /* Destructor. Restore the saved line_table. */
171 1.1 mrg ~line_table_test ();
172 1.1 mrg };
173 1.1 mrg
174 1.1.1.4 mrg /* Helper function for selftests that need a function decl. */
175 1.1.1.4 mrg
176 1.1.1.4 mrg extern tree make_fndecl (tree return_type,
177 1.1.1.4 mrg const char *name,
178 1.1.1.4 mrg vec <tree> ¶m_types,
179 1.1.1.4 mrg bool is_variadic = false);
180 1.1.1.4 mrg
181 1.1 mrg /* Run TESTCASE multiple times, once for each case in our test matrix. */
182 1.1 mrg
183 1.1 mrg extern void
184 1.1 mrg for_each_line_table_case (void (*testcase) (const line_table_case &));
185 1.1 mrg
186 1.1 mrg /* Read the contents of PATH into memory, returning a 0-terminated buffer
187 1.1 mrg that must be freed by the caller.
188 1.1 mrg Fail (and abort) if there are any problems, with LOC as the reported
189 1.1 mrg location of the failure. */
190 1.1 mrg
191 1.1 mrg extern char *read_file (const location &loc, const char *path);
192 1.1 mrg
193 1.1 mrg /* A helper function for writing tests that interact with the
194 1.1 mrg garbage collector. */
195 1.1 mrg
196 1.1 mrg extern void forcibly_ggc_collect ();
197 1.1 mrg
198 1.1 mrg /* Convert a path relative to SRCDIR/gcc/testsuite/selftests
199 1.1 mrg to a real path (either absolute, or relative to pwd).
200 1.1 mrg The result should be freed by the caller. */
201 1.1 mrg
202 1.1 mrg extern char *locate_file (const char *path);
203 1.1 mrg
204 1.1 mrg /* The path of SRCDIR/testsuite/selftests. */
205 1.1 mrg
206 1.1 mrg extern const char *path_to_selftest_files;
207 1.1 mrg
208 1.1.1.2 mrg /* selftest::test_runner is an implementation detail of selftest::run_tests,
209 1.1.1.2 mrg exposed here to allow plugins to run their own suites of tests. */
210 1.1.1.2 mrg
211 1.1.1.2 mrg class test_runner
212 1.1.1.2 mrg {
213 1.1.1.2 mrg public:
214 1.1.1.2 mrg test_runner (const char *name);
215 1.1.1.2 mrg ~test_runner ();
216 1.1.1.2 mrg
217 1.1.1.2 mrg private:
218 1.1.1.2 mrg const char *m_name;
219 1.1.1.2 mrg long m_start_time;
220 1.1.1.2 mrg };
221 1.1.1.2 mrg
222 1.1 mrg /* Declarations for specific families of tests (by source file), in
223 1.1 mrg alphabetical order. */
224 1.1.1.2 mrg extern void attribute_c_tests ();
225 1.1 mrg extern void bitmap_c_tests ();
226 1.1.1.3 mrg extern void cgraph_c_tests ();
227 1.1.1.3 mrg extern void convert_c_tests ();
228 1.1 mrg extern void diagnostic_c_tests ();
229 1.1.1.4 mrg extern void diagnostic_format_json_cc_tests ();
230 1.1 mrg extern void diagnostic_show_locus_c_tests ();
231 1.1.1.4 mrg extern void digraph_cc_tests ();
232 1.1.1.3 mrg extern void dumpfile_c_tests ();
233 1.1 mrg extern void edit_context_c_tests ();
234 1.1 mrg extern void et_forest_c_tests ();
235 1.1 mrg extern void fibonacci_heap_c_tests ();
236 1.1.1.3 mrg extern void fold_const_c_tests ();
237 1.1 mrg extern void function_tests_c_tests ();
238 1.1 mrg extern void ggc_tests_c_tests ();
239 1.1.1.3 mrg extern void gimple_c_tests ();
240 1.1 mrg extern void hash_map_tests_c_tests ();
241 1.1 mrg extern void hash_set_tests_c_tests ();
242 1.1 mrg extern void input_c_tests ();
243 1.1.1.3 mrg extern void json_cc_tests ();
244 1.1.1.3 mrg extern void opt_problem_cc_tests ();
245 1.1.1.3 mrg extern void optinfo_emit_json_cc_tests ();
246 1.1.1.4 mrg extern void opts_c_tests ();
247 1.1.1.4 mrg extern void ordered_hash_map_tests_cc_tests ();
248 1.1.1.3 mrg extern void predict_c_tests ();
249 1.1 mrg extern void pretty_print_c_tests ();
250 1.1.1.4 mrg extern void range_tests ();
251 1.1 mrg extern void read_rtl_function_c_tests ();
252 1.1 mrg extern void rtl_tests_c_tests ();
253 1.1.1.3 mrg extern void sbitmap_c_tests ();
254 1.1 mrg extern void selftest_c_tests ();
255 1.1.1.3 mrg extern void simplify_rtx_c_tests ();
256 1.1 mrg extern void spellcheck_c_tests ();
257 1.1 mrg extern void spellcheck_tree_c_tests ();
258 1.1 mrg extern void sreal_c_tests ();
259 1.1 mrg extern void store_merging_c_tests ();
260 1.1 mrg extern void tree_c_tests ();
261 1.1 mrg extern void tree_cfg_c_tests ();
262 1.1.1.4 mrg extern void tree_diagnostic_path_cc_tests ();
263 1.1.1.4 mrg extern void tristate_cc_tests ();
264 1.1.1.3 mrg extern void typed_splay_tree_c_tests ();
265 1.1.1.2 mrg extern void unique_ptr_tests_cc_tests ();
266 1.1 mrg extern void vec_c_tests ();
267 1.1.1.2 mrg extern void vec_perm_indices_c_tests ();
268 1.1.1.3 mrg extern void wide_int_cc_tests ();
269 1.1.1.3 mrg extern void opt_proposer_c_tests ();
270 1.1.1.4 mrg extern void dbgcnt_c_tests ();
271 1.1 mrg
272 1.1 mrg extern int num_passes;
273 1.1 mrg
274 1.1 mrg } /* end of namespace selftest. */
275 1.1 mrg
276 1.1 mrg /* Macros for writing tests. */
277 1.1 mrg
278 1.1 mrg /* Evaluate EXPR and coerce to bool, calling
279 1.1 mrg ::selftest::pass if it is true,
280 1.1 mrg ::selftest::fail if it false. */
281 1.1 mrg
282 1.1 mrg #define ASSERT_TRUE(EXPR) \
283 1.1 mrg ASSERT_TRUE_AT (SELFTEST_LOCATION, (EXPR))
284 1.1 mrg
285 1.1 mrg /* Like ASSERT_TRUE, but treat LOC as the effective location of the
286 1.1 mrg selftest. */
287 1.1 mrg
288 1.1 mrg #define ASSERT_TRUE_AT(LOC, EXPR) \
289 1.1 mrg SELFTEST_BEGIN_STMT \
290 1.1.1.2 mrg const char *desc_ = "ASSERT_TRUE (" #EXPR ")"; \
291 1.1.1.2 mrg bool actual_ = (EXPR); \
292 1.1.1.2 mrg if (actual_) \
293 1.1.1.2 mrg ::selftest::pass ((LOC), desc_); \
294 1.1 mrg else \
295 1.1.1.2 mrg ::selftest::fail ((LOC), desc_); \
296 1.1 mrg SELFTEST_END_STMT
297 1.1 mrg
298 1.1 mrg /* Evaluate EXPR and coerce to bool, calling
299 1.1 mrg ::selftest::pass if it is false,
300 1.1 mrg ::selftest::fail if it true. */
301 1.1 mrg
302 1.1 mrg #define ASSERT_FALSE(EXPR) \
303 1.1 mrg ASSERT_FALSE_AT (SELFTEST_LOCATION, (EXPR))
304 1.1 mrg
305 1.1 mrg /* Like ASSERT_FALSE, but treat LOC as the effective location of the
306 1.1 mrg selftest. */
307 1.1 mrg
308 1.1 mrg #define ASSERT_FALSE_AT(LOC, EXPR) \
309 1.1 mrg SELFTEST_BEGIN_STMT \
310 1.1.1.2 mrg const char *desc_ = "ASSERT_FALSE (" #EXPR ")"; \
311 1.1.1.2 mrg bool actual_ = (EXPR); \
312 1.1.1.2 mrg if (actual_) \
313 1.1.1.2 mrg ::selftest::fail ((LOC), desc_); \
314 1.1.1.2 mrg else \
315 1.1.1.2 mrg ::selftest::pass ((LOC), desc_); \
316 1.1 mrg SELFTEST_END_STMT
317 1.1 mrg
318 1.1.1.3 mrg /* Evaluate VAL1 and VAL2 and compare them with ==, calling
319 1.1 mrg ::selftest::pass if they are equal,
320 1.1 mrg ::selftest::fail if they are non-equal. */
321 1.1 mrg
322 1.1.1.3 mrg #define ASSERT_EQ(VAL1, VAL2) \
323 1.1.1.3 mrg ASSERT_EQ_AT ((SELFTEST_LOCATION), (VAL1), (VAL2))
324 1.1 mrg
325 1.1 mrg /* Like ASSERT_EQ, but treat LOC as the effective location of the
326 1.1 mrg selftest. */
327 1.1 mrg
328 1.1.1.3 mrg #define ASSERT_EQ_AT(LOC, VAL1, VAL2) \
329 1.1 mrg SELFTEST_BEGIN_STMT \
330 1.1.1.3 mrg const char *desc_ = "ASSERT_EQ (" #VAL1 ", " #VAL2 ")"; \
331 1.1.1.3 mrg if ((VAL1) == (VAL2)) \
332 1.1.1.2 mrg ::selftest::pass ((LOC), desc_); \
333 1.1 mrg else \
334 1.1.1.2 mrg ::selftest::fail ((LOC), desc_); \
335 1.1.1.2 mrg SELFTEST_END_STMT
336 1.1.1.2 mrg
337 1.1.1.3 mrg /* Evaluate VAL1 and VAL2 and compare them with known_eq, calling
338 1.1.1.2 mrg ::selftest::pass if they are always equal,
339 1.1.1.2 mrg ::selftest::fail if they might be non-equal. */
340 1.1.1.2 mrg
341 1.1.1.3 mrg #define ASSERT_KNOWN_EQ(VAL1, VAL2) \
342 1.1.1.3 mrg ASSERT_KNOWN_EQ_AT ((SELFTEST_LOCATION), (VAL1), (VAL2))
343 1.1.1.2 mrg
344 1.1.1.2 mrg /* Like ASSERT_KNOWN_EQ, but treat LOC as the effective location of the
345 1.1.1.2 mrg selftest. */
346 1.1.1.2 mrg
347 1.1.1.3 mrg #define ASSERT_KNOWN_EQ_AT(LOC, VAL1, VAL2) \
348 1.1.1.2 mrg SELFTEST_BEGIN_STMT \
349 1.1.1.3 mrg const char *desc = "ASSERT_KNOWN_EQ (" #VAL1 ", " #VAL2 ")"; \
350 1.1.1.3 mrg if (known_eq (VAL1, VAL2)) \
351 1.1.1.2 mrg ::selftest::pass ((LOC), desc); \
352 1.1.1.2 mrg else \
353 1.1.1.2 mrg ::selftest::fail ((LOC), desc); \
354 1.1 mrg SELFTEST_END_STMT
355 1.1 mrg
356 1.1.1.3 mrg /* Evaluate VAL1 and VAL2 and compare them with !=, calling
357 1.1 mrg ::selftest::pass if they are non-equal,
358 1.1 mrg ::selftest::fail if they are equal. */
359 1.1 mrg
360 1.1.1.3 mrg #define ASSERT_NE(VAL1, VAL2) \
361 1.1 mrg SELFTEST_BEGIN_STMT \
362 1.1.1.3 mrg const char *desc_ = "ASSERT_NE (" #VAL1 ", " #VAL2 ")"; \
363 1.1.1.3 mrg if ((VAL1) != (VAL2)) \
364 1.1.1.2 mrg ::selftest::pass (SELFTEST_LOCATION, desc_); \
365 1.1.1.2 mrg else \
366 1.1.1.2 mrg ::selftest::fail (SELFTEST_LOCATION, desc_); \
367 1.1.1.2 mrg SELFTEST_END_STMT
368 1.1.1.2 mrg
369 1.1.1.3 mrg /* Evaluate VAL1 and VAL2 and compare them with maybe_ne, calling
370 1.1.1.2 mrg ::selftest::pass if they might be non-equal,
371 1.1.1.2 mrg ::selftest::fail if they are known to be equal. */
372 1.1.1.2 mrg
373 1.1.1.3 mrg #define ASSERT_MAYBE_NE(VAL1, VAL2) \
374 1.1.1.3 mrg ASSERT_MAYBE_NE_AT ((SELFTEST_LOCATION), (VAL1), (VAL2))
375 1.1.1.2 mrg
376 1.1.1.2 mrg /* Like ASSERT_MAYBE_NE, but treat LOC as the effective location of the
377 1.1.1.2 mrg selftest. */
378 1.1.1.2 mrg
379 1.1.1.3 mrg #define ASSERT_MAYBE_NE_AT(LOC, VAL1, VAL2) \
380 1.1.1.2 mrg SELFTEST_BEGIN_STMT \
381 1.1.1.3 mrg const char *desc = "ASSERT_MAYBE_NE (" #VAL1 ", " #VAL2 ")"; \
382 1.1.1.3 mrg if (maybe_ne (VAL1, VAL2)) \
383 1.1.1.2 mrg ::selftest::pass ((LOC), desc); \
384 1.1.1.2 mrg else \
385 1.1.1.2 mrg ::selftest::fail ((LOC), desc); \
386 1.1.1.2 mrg SELFTEST_END_STMT
387 1.1.1.2 mrg
388 1.1.1.2 mrg /* Evaluate LHS and RHS and compare them with >, calling
389 1.1.1.2 mrg ::selftest::pass if LHS > RHS,
390 1.1.1.2 mrg ::selftest::fail otherwise. */
391 1.1.1.2 mrg
392 1.1.1.2 mrg #define ASSERT_GT(LHS, RHS) \
393 1.1.1.2 mrg ASSERT_GT_AT ((SELFTEST_LOCATION), (LHS), (RHS))
394 1.1.1.2 mrg
395 1.1.1.2 mrg /* Like ASSERT_GT, but treat LOC as the effective location of the
396 1.1.1.2 mrg selftest. */
397 1.1.1.2 mrg
398 1.1.1.2 mrg #define ASSERT_GT_AT(LOC, LHS, RHS) \
399 1.1.1.2 mrg SELFTEST_BEGIN_STMT \
400 1.1.1.2 mrg const char *desc_ = "ASSERT_GT (" #LHS ", " #RHS ")"; \
401 1.1.1.2 mrg if ((LHS) > (RHS)) \
402 1.1.1.2 mrg ::selftest::pass ((LOC), desc_); \
403 1.1 mrg else \
404 1.1.1.2 mrg ::selftest::fail ((LOC), desc_); \
405 1.1.1.2 mrg SELFTEST_END_STMT
406 1.1.1.2 mrg
407 1.1.1.2 mrg /* Evaluate LHS and RHS and compare them with <, calling
408 1.1.1.2 mrg ::selftest::pass if LHS < RHS,
409 1.1.1.2 mrg ::selftest::fail otherwise. */
410 1.1.1.2 mrg
411 1.1.1.2 mrg #define ASSERT_LT(LHS, RHS) \
412 1.1.1.2 mrg ASSERT_LT_AT ((SELFTEST_LOCATION), (LHS), (RHS))
413 1.1.1.2 mrg
414 1.1.1.2 mrg /* Like ASSERT_LT, but treat LOC as the effective location of the
415 1.1.1.2 mrg selftest. */
416 1.1.1.2 mrg
417 1.1.1.2 mrg #define ASSERT_LT_AT(LOC, LHS, RHS) \
418 1.1.1.2 mrg SELFTEST_BEGIN_STMT \
419 1.1.1.2 mrg const char *desc_ = "ASSERT_LT (" #LHS ", " #RHS ")"; \
420 1.1.1.2 mrg if ((LHS) < (RHS)) \
421 1.1.1.2 mrg ::selftest::pass ((LOC), desc_); \
422 1.1.1.2 mrg else \
423 1.1.1.2 mrg ::selftest::fail ((LOC), desc_); \
424 1.1 mrg SELFTEST_END_STMT
425 1.1 mrg
426 1.1.1.3 mrg /* Evaluate VAL1 and VAL2 and compare them with strcmp, calling
427 1.1.1.3 mrg ::selftest::pass if they are equal (and both are non-NULL),
428 1.1.1.3 mrg ::selftest::fail if they are non-equal, or are both NULL. */
429 1.1 mrg
430 1.1.1.3 mrg #define ASSERT_STREQ(VAL1, VAL2) \
431 1.1 mrg SELFTEST_BEGIN_STMT \
432 1.1.1.3 mrg ::selftest::assert_streq (SELFTEST_LOCATION, #VAL1, #VAL2, \
433 1.1.1.3 mrg (VAL1), (VAL2)); \
434 1.1 mrg SELFTEST_END_STMT
435 1.1 mrg
436 1.1 mrg /* Like ASSERT_STREQ, but treat LOC as the effective location of the
437 1.1 mrg selftest. */
438 1.1 mrg
439 1.1.1.3 mrg #define ASSERT_STREQ_AT(LOC, VAL1, VAL2) \
440 1.1 mrg SELFTEST_BEGIN_STMT \
441 1.1.1.3 mrg ::selftest::assert_streq ((LOC), #VAL1, #VAL2, \
442 1.1.1.3 mrg (VAL1), (VAL2)); \
443 1.1 mrg SELFTEST_END_STMT
444 1.1 mrg
445 1.1 mrg /* Evaluate HAYSTACK and NEEDLE and use strstr to determine if NEEDLE
446 1.1 mrg is within HAYSTACK.
447 1.1 mrg ::selftest::pass if NEEDLE is found.
448 1.1 mrg ::selftest::fail if it is not found. */
449 1.1 mrg
450 1.1 mrg #define ASSERT_STR_CONTAINS(HAYSTACK, NEEDLE) \
451 1.1 mrg SELFTEST_BEGIN_STMT \
452 1.1 mrg ::selftest::assert_str_contains (SELFTEST_LOCATION, #HAYSTACK, #NEEDLE, \
453 1.1 mrg (HAYSTACK), (NEEDLE)); \
454 1.1 mrg SELFTEST_END_STMT
455 1.1 mrg
456 1.1.1.3 mrg /* Like ASSERT_STR_CONTAINS, but treat LOC as the effective location of the
457 1.1.1.3 mrg selftest. */
458 1.1.1.3 mrg
459 1.1.1.3 mrg #define ASSERT_STR_CONTAINS_AT(LOC, HAYSTACK, NEEDLE) \
460 1.1.1.3 mrg SELFTEST_BEGIN_STMT \
461 1.1.1.3 mrg ::selftest::assert_str_contains (LOC, #HAYSTACK, #NEEDLE, \
462 1.1.1.3 mrg (HAYSTACK), (NEEDLE)); \
463 1.1.1.3 mrg SELFTEST_END_STMT
464 1.1.1.3 mrg
465 1.1.1.3 mrg /* Evaluate STR and PREFIX and determine if STR starts with PREFIX.
466 1.1.1.3 mrg ::selftest::pass if STR does start with PREFIX.
467 1.1.1.3 mrg ::selftest::fail if does not, or either is NULL. */
468 1.1.1.3 mrg
469 1.1.1.3 mrg #define ASSERT_STR_STARTSWITH(STR, PREFIX) \
470 1.1.1.3 mrg SELFTEST_BEGIN_STMT \
471 1.1.1.3 mrg ::selftest::assert_str_startswith (SELFTEST_LOCATION, #STR, #PREFIX, \
472 1.1.1.3 mrg (STR), (PREFIX)); \
473 1.1.1.3 mrg SELFTEST_END_STMT
474 1.1.1.3 mrg
475 1.1 mrg /* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
476 1.1 mrg ::selftest::fail if it is false. */
477 1.1 mrg
478 1.1.1.2 mrg #define ASSERT_PRED1(PRED1, VAL1) \
479 1.1.1.2 mrg SELFTEST_BEGIN_STMT \
480 1.1.1.2 mrg const char *desc_ = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")"; \
481 1.1.1.2 mrg bool actual_ = (PRED1) (VAL1); \
482 1.1.1.2 mrg if (actual_) \
483 1.1.1.2 mrg ::selftest::pass (SELFTEST_LOCATION, desc_); \
484 1.1.1.2 mrg else \
485 1.1.1.2 mrg ::selftest::fail (SELFTEST_LOCATION, desc_); \
486 1.1 mrg SELFTEST_END_STMT
487 1.1 mrg
488 1.1 mrg #define SELFTEST_BEGIN_STMT do {
489 1.1 mrg #define SELFTEST_END_STMT } while (0)
490 1.1 mrg
491 1.1 mrg #endif /* #if CHECKING_P */
492 1.1 mrg
493 1.1 mrg #endif /* GCC_SELFTEST_H */
494