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