annotate gcc/selftest.h @ 120:f93fa5091070

fix conv1.c
author mir3636
date Thu, 08 Mar 2018 14:53:42 +0900
parents 04ced10e8804
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* A self-testing framework, for use by -fself-test.
kono
parents:
diff changeset
2 Copyright (C) 2015-2017 Free Software Foundation, Inc.
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 #ifndef GCC_SELFTEST_H
kono
parents:
diff changeset
21 #define GCC_SELFTEST_H
kono
parents:
diff changeset
22
kono
parents:
diff changeset
23 /* The selftest code should entirely disappear in a production
kono
parents:
diff changeset
24 configuration, hence we guard all of it with #if CHECKING_P. */
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 #if CHECKING_P
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 namespace selftest {
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 /* A struct describing the source-location of a selftest, to make it
kono
parents:
diff changeset
31 easier to track down failing tests. */
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 struct location
kono
parents:
diff changeset
34 {
kono
parents:
diff changeset
35 location (const char *file, int line, const char *function)
kono
parents:
diff changeset
36 : m_file (file), m_line (line), m_function (function) {}
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 const char *m_file;
kono
parents:
diff changeset
39 int m_line;
kono
parents:
diff changeset
40 const char *m_function;
kono
parents:
diff changeset
41 };
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 /* A macro for use in selftests and by the ASSERT_ macros below,
kono
parents:
diff changeset
44 constructing a selftest::location for the current source location. */
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 #define SELFTEST_LOCATION \
kono
parents:
diff changeset
47 (::selftest::location (__FILE__, __LINE__, __FUNCTION__))
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 /* The entrypoint for running all tests. */
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 extern void run_tests ();
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 /* Record the successful outcome of some aspect of the test. */
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 extern void pass (const location &loc, const char *msg);
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 /* Report the failed outcome of some aspect of the test and abort. */
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 extern void fail (const location &loc, const char *msg)
kono
parents:
diff changeset
60 ATTRIBUTE_NORETURN;
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 /* As "fail", but using printf-style formatted output. */
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 extern void fail_formatted (const location &loc, const char *fmt, ...)
kono
parents:
diff changeset
65 ATTRIBUTE_PRINTF_2 ATTRIBUTE_NORETURN;
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 /* Implementation detail of ASSERT_STREQ. */
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 extern void assert_streq (const location &loc,
kono
parents:
diff changeset
70 const char *desc_expected, const char *desc_actual,
kono
parents:
diff changeset
71 const char *val_expected, const char *val_actual);
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 /* Implementation detail of ASSERT_STR_CONTAINS. */
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 extern void assert_str_contains (const location &loc,
kono
parents:
diff changeset
76 const char *desc_haystack,
kono
parents:
diff changeset
77 const char *desc_needle,
kono
parents:
diff changeset
78 const char *val_haystack,
kono
parents:
diff changeset
79 const char *val_needle);
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 /* A named temporary file for use in selftests.
kono
parents:
diff changeset
82 Usable for writing out files, and as the base class for
kono
parents:
diff changeset
83 temp_source_file.
kono
parents:
diff changeset
84 The file is unlinked in the destructor. */
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 class named_temp_file
kono
parents:
diff changeset
87 {
kono
parents:
diff changeset
88 public:
kono
parents:
diff changeset
89 named_temp_file (const char *suffix);
kono
parents:
diff changeset
90 ~named_temp_file ();
kono
parents:
diff changeset
91 const char *get_filename () const { return m_filename; }
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 private:
kono
parents:
diff changeset
94 char *m_filename;
kono
parents:
diff changeset
95 };
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 /* A class for writing out a temporary sourcefile for use in selftests
kono
parents:
diff changeset
98 of input handling. */
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 class temp_source_file : public named_temp_file
kono
parents:
diff changeset
101 {
kono
parents:
diff changeset
102 public:
kono
parents:
diff changeset
103 temp_source_file (const location &loc, const char *suffix,
kono
parents:
diff changeset
104 const char *content);
kono
parents:
diff changeset
105 };
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 /* Various selftests involving location-handling require constructing a
kono
parents:
diff changeset
108 line table and one or more line maps within it.
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 For maximum test coverage we want to run these tests with a variety
kono
parents:
diff changeset
111 of situations:
kono
parents:
diff changeset
112 - line_table->default_range_bits: some frontends use a non-zero value
kono
parents:
diff changeset
113 and others use zero
kono
parents:
diff changeset
114 - the fallback modes within line-map.c: there are various threshold
kono
parents:
diff changeset
115 values for source_location/location_t beyond line-map.c changes
kono
parents:
diff changeset
116 behavior (disabling of the range-packing optimization, disabling
kono
parents:
diff changeset
117 of column-tracking). We can exercise these by starting the line_table
kono
parents:
diff changeset
118 at interesting values at or near these thresholds.
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 The following struct describes a particular case within our test
kono
parents:
diff changeset
121 matrix. */
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 struct line_table_case;
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 /* A class for overriding the global "line_table" within a selftest,
kono
parents:
diff changeset
126 restoring its value afterwards. At most one instance of this
kono
parents:
diff changeset
127 class can exist at once, due to the need to keep the old value
kono
parents:
diff changeset
128 of line_table as a GC root. */
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 class line_table_test
kono
parents:
diff changeset
131 {
kono
parents:
diff changeset
132 public:
kono
parents:
diff changeset
133 /* Default constructor. Override "line_table", using sane defaults
kono
parents:
diff changeset
134 for the temporary line_table. */
kono
parents:
diff changeset
135 line_table_test ();
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 /* Constructor. Override "line_table", using the case described by C. */
kono
parents:
diff changeset
138 line_table_test (const line_table_case &c);
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 /* Destructor. Restore the saved line_table. */
kono
parents:
diff changeset
141 ~line_table_test ();
kono
parents:
diff changeset
142 };
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 /* Run TESTCASE multiple times, once for each case in our test matrix. */
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 extern void
kono
parents:
diff changeset
147 for_each_line_table_case (void (*testcase) (const line_table_case &));
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 /* Read the contents of PATH into memory, returning a 0-terminated buffer
kono
parents:
diff changeset
150 that must be freed by the caller.
kono
parents:
diff changeset
151 Fail (and abort) if there are any problems, with LOC as the reported
kono
parents:
diff changeset
152 location of the failure. */
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 extern char *read_file (const location &loc, const char *path);
kono
parents:
diff changeset
155
kono
parents:
diff changeset
156 /* A helper function for writing tests that interact with the
kono
parents:
diff changeset
157 garbage collector. */
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 extern void forcibly_ggc_collect ();
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 /* Convert a path relative to SRCDIR/gcc/testsuite/selftests
kono
parents:
diff changeset
162 to a real path (either absolute, or relative to pwd).
kono
parents:
diff changeset
163 The result should be freed by the caller. */
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 extern char *locate_file (const char *path);
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 /* The path of SRCDIR/testsuite/selftests. */
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 extern const char *path_to_selftest_files;
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 /* Declarations for specific families of tests (by source file), in
kono
parents:
diff changeset
172 alphabetical order. */
kono
parents:
diff changeset
173 extern void bitmap_c_tests ();
kono
parents:
diff changeset
174 extern void sbitmap_c_tests ();
kono
parents:
diff changeset
175 extern void diagnostic_c_tests ();
kono
parents:
diff changeset
176 extern void diagnostic_show_locus_c_tests ();
kono
parents:
diff changeset
177 extern void edit_context_c_tests ();
kono
parents:
diff changeset
178 extern void et_forest_c_tests ();
kono
parents:
diff changeset
179 extern void fold_const_c_tests ();
kono
parents:
diff changeset
180 extern void fibonacci_heap_c_tests ();
kono
parents:
diff changeset
181 extern void function_tests_c_tests ();
kono
parents:
diff changeset
182 extern void gimple_c_tests ();
kono
parents:
diff changeset
183 extern void ggc_tests_c_tests ();
kono
parents:
diff changeset
184 extern void hash_map_tests_c_tests ();
kono
parents:
diff changeset
185 extern void hash_set_tests_c_tests ();
kono
parents:
diff changeset
186 extern void input_c_tests ();
kono
parents:
diff changeset
187 extern void pretty_print_c_tests ();
kono
parents:
diff changeset
188 extern void read_rtl_function_c_tests ();
kono
parents:
diff changeset
189 extern void rtl_tests_c_tests ();
kono
parents:
diff changeset
190 extern void selftest_c_tests ();
kono
parents:
diff changeset
191 extern void spellcheck_c_tests ();
kono
parents:
diff changeset
192 extern void spellcheck_tree_c_tests ();
kono
parents:
diff changeset
193 extern void sreal_c_tests ();
kono
parents:
diff changeset
194 extern void store_merging_c_tests ();
kono
parents:
diff changeset
195 extern void typed_splay_tree_c_tests ();
kono
parents:
diff changeset
196 extern void tree_c_tests ();
kono
parents:
diff changeset
197 extern void tree_cfg_c_tests ();
kono
parents:
diff changeset
198 extern void unique_ptr_tests_cc_tests ();
kono
parents:
diff changeset
199 extern void vec_c_tests ();
kono
parents:
diff changeset
200 extern void wide_int_cc_tests ();
kono
parents:
diff changeset
201 extern void predict_c_tests ();
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 extern int num_passes;
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 } /* end of namespace selftest. */
kono
parents:
diff changeset
206
kono
parents:
diff changeset
207 /* Macros for writing tests. */
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 /* Evaluate EXPR and coerce to bool, calling
kono
parents:
diff changeset
210 ::selftest::pass if it is true,
kono
parents:
diff changeset
211 ::selftest::fail if it false. */
kono
parents:
diff changeset
212
kono
parents:
diff changeset
213 #define ASSERT_TRUE(EXPR) \
kono
parents:
diff changeset
214 ASSERT_TRUE_AT (SELFTEST_LOCATION, (EXPR))
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 /* Like ASSERT_TRUE, but treat LOC as the effective location of the
kono
parents:
diff changeset
217 selftest. */
kono
parents:
diff changeset
218
kono
parents:
diff changeset
219 #define ASSERT_TRUE_AT(LOC, EXPR) \
kono
parents:
diff changeset
220 SELFTEST_BEGIN_STMT \
kono
parents:
diff changeset
221 const char *desc = "ASSERT_TRUE (" #EXPR ")"; \
kono
parents:
diff changeset
222 bool actual = (EXPR); \
kono
parents:
diff changeset
223 if (actual) \
kono
parents:
diff changeset
224 ::selftest::pass ((LOC), desc); \
kono
parents:
diff changeset
225 else \
kono
parents:
diff changeset
226 ::selftest::fail ((LOC), desc); \
kono
parents:
diff changeset
227 SELFTEST_END_STMT
kono
parents:
diff changeset
228
kono
parents:
diff changeset
229 /* Evaluate EXPR and coerce to bool, calling
kono
parents:
diff changeset
230 ::selftest::pass if it is false,
kono
parents:
diff changeset
231 ::selftest::fail if it true. */
kono
parents:
diff changeset
232
kono
parents:
diff changeset
233 #define ASSERT_FALSE(EXPR) \
kono
parents:
diff changeset
234 ASSERT_FALSE_AT (SELFTEST_LOCATION, (EXPR))
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 /* Like ASSERT_FALSE, but treat LOC as the effective location of the
kono
parents:
diff changeset
237 selftest. */
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 #define ASSERT_FALSE_AT(LOC, EXPR) \
kono
parents:
diff changeset
240 SELFTEST_BEGIN_STMT \
kono
parents:
diff changeset
241 const char *desc = "ASSERT_FALSE (" #EXPR ")"; \
kono
parents:
diff changeset
242 bool actual = (EXPR); \
kono
parents:
diff changeset
243 if (actual) \
kono
parents:
diff changeset
244 ::selftest::fail ((LOC), desc); \
kono
parents:
diff changeset
245 else \
kono
parents:
diff changeset
246 ::selftest::pass ((LOC), desc); \
kono
parents:
diff changeset
247 SELFTEST_END_STMT
kono
parents:
diff changeset
248
kono
parents:
diff changeset
249 /* Evaluate EXPECTED and ACTUAL and compare them with ==, calling
kono
parents:
diff changeset
250 ::selftest::pass if they are equal,
kono
parents:
diff changeset
251 ::selftest::fail if they are non-equal. */
kono
parents:
diff changeset
252
kono
parents:
diff changeset
253 #define ASSERT_EQ(EXPECTED, ACTUAL) \
kono
parents:
diff changeset
254 ASSERT_EQ_AT ((SELFTEST_LOCATION), (EXPECTED), (ACTUAL))
kono
parents:
diff changeset
255
kono
parents:
diff changeset
256 /* Like ASSERT_EQ, but treat LOC as the effective location of the
kono
parents:
diff changeset
257 selftest. */
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 #define ASSERT_EQ_AT(LOC, EXPECTED, ACTUAL) \
kono
parents:
diff changeset
260 SELFTEST_BEGIN_STMT \
kono
parents:
diff changeset
261 const char *desc = "ASSERT_EQ (" #EXPECTED ", " #ACTUAL ")"; \
kono
parents:
diff changeset
262 if ((EXPECTED) == (ACTUAL)) \
kono
parents:
diff changeset
263 ::selftest::pass ((LOC), desc); \
kono
parents:
diff changeset
264 else \
kono
parents:
diff changeset
265 ::selftest::fail ((LOC), desc); \
kono
parents:
diff changeset
266 SELFTEST_END_STMT
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 /* Evaluate EXPECTED and ACTUAL and compare them with !=, calling
kono
parents:
diff changeset
269 ::selftest::pass if they are non-equal,
kono
parents:
diff changeset
270 ::selftest::fail if they are equal. */
kono
parents:
diff changeset
271
kono
parents:
diff changeset
272 #define ASSERT_NE(EXPECTED, ACTUAL) \
kono
parents:
diff changeset
273 SELFTEST_BEGIN_STMT \
kono
parents:
diff changeset
274 const char *desc = "ASSERT_NE (" #EXPECTED ", " #ACTUAL ")"; \
kono
parents:
diff changeset
275 if ((EXPECTED) != (ACTUAL)) \
kono
parents:
diff changeset
276 ::selftest::pass (SELFTEST_LOCATION, desc); \
kono
parents:
diff changeset
277 else \
kono
parents:
diff changeset
278 ::selftest::fail (SELFTEST_LOCATION, desc); \
kono
parents:
diff changeset
279 SELFTEST_END_STMT
kono
parents:
diff changeset
280
kono
parents:
diff changeset
281 /* Evaluate EXPECTED and ACTUAL and compare them with strcmp, calling
kono
parents:
diff changeset
282 ::selftest::pass if they are equal,
kono
parents:
diff changeset
283 ::selftest::fail if they are non-equal. */
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 #define ASSERT_STREQ(EXPECTED, ACTUAL) \
kono
parents:
diff changeset
286 SELFTEST_BEGIN_STMT \
kono
parents:
diff changeset
287 ::selftest::assert_streq (SELFTEST_LOCATION, #EXPECTED, #ACTUAL, \
kono
parents:
diff changeset
288 (EXPECTED), (ACTUAL)); \
kono
parents:
diff changeset
289 SELFTEST_END_STMT
kono
parents:
diff changeset
290
kono
parents:
diff changeset
291 /* Like ASSERT_STREQ, but treat LOC as the effective location of the
kono
parents:
diff changeset
292 selftest. */
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 #define ASSERT_STREQ_AT(LOC, EXPECTED, ACTUAL) \
kono
parents:
diff changeset
295 SELFTEST_BEGIN_STMT \
kono
parents:
diff changeset
296 ::selftest::assert_streq ((LOC), #EXPECTED, #ACTUAL, \
kono
parents:
diff changeset
297 (EXPECTED), (ACTUAL)); \
kono
parents:
diff changeset
298 SELFTEST_END_STMT
kono
parents:
diff changeset
299
kono
parents:
diff changeset
300 /* Evaluate HAYSTACK and NEEDLE and use strstr to determine if NEEDLE
kono
parents:
diff changeset
301 is within HAYSTACK.
kono
parents:
diff changeset
302 ::selftest::pass if NEEDLE is found.
kono
parents:
diff changeset
303 ::selftest::fail if it is not found. */
kono
parents:
diff changeset
304
kono
parents:
diff changeset
305 #define ASSERT_STR_CONTAINS(HAYSTACK, NEEDLE) \
kono
parents:
diff changeset
306 SELFTEST_BEGIN_STMT \
kono
parents:
diff changeset
307 ::selftest::assert_str_contains (SELFTEST_LOCATION, #HAYSTACK, #NEEDLE, \
kono
parents:
diff changeset
308 (HAYSTACK), (NEEDLE)); \
kono
parents:
diff changeset
309 SELFTEST_END_STMT
kono
parents:
diff changeset
310
kono
parents:
diff changeset
311 /* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
kono
parents:
diff changeset
312 ::selftest::fail if it is false. */
kono
parents:
diff changeset
313
kono
parents:
diff changeset
314 #define ASSERT_PRED1(PRED1, VAL1) \
kono
parents:
diff changeset
315 SELFTEST_BEGIN_STMT \
kono
parents:
diff changeset
316 const char *desc = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")"; \
kono
parents:
diff changeset
317 bool actual = (PRED1) (VAL1); \
kono
parents:
diff changeset
318 if (actual) \
kono
parents:
diff changeset
319 ::selftest::pass (SELFTEST_LOCATION, desc); \
kono
parents:
diff changeset
320 else \
kono
parents:
diff changeset
321 ::selftest::fail (SELFTEST_LOCATION, desc); \
kono
parents:
diff changeset
322 SELFTEST_END_STMT
kono
parents:
diff changeset
323
kono
parents:
diff changeset
324 #define SELFTEST_BEGIN_STMT do {
kono
parents:
diff changeset
325 #define SELFTEST_END_STMT } while (0)
kono
parents:
diff changeset
326
kono
parents:
diff changeset
327 #endif /* #if CHECKING_P */
kono
parents:
diff changeset
328
kono
parents:
diff changeset
329 #endif /* GCC_SELFTEST_H */