[RS6000] TOC refs generated during reload
[official-gcc.git] / gcc / selftest.h
blob397e998048b9ac007701c1da13505211d9f16181
1 /* A self-testing framework, for use by -fself-test.
2 Copyright (C) 2015-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef GCC_SELFTEST_H
21 #define GCC_SELFTEST_H
23 /* The selftest code should entirely disappear in a production
24 configuration, hence we guard all of it with #if CHECKING_P. */
26 #if CHECKING_P
28 namespace selftest {
30 /* A struct describing the source-location of a selftest, to make it
31 easier to track down failing tests. */
33 struct location
35 location (const char *file, int line, const char *function)
36 : m_file (file), m_line (line), m_function (function) {}
38 const char *m_file;
39 int m_line;
40 const char *m_function;
43 /* A macro for use in selftests and by the ASSERT_ macros below,
44 constructing a selftest::location for the current source location. */
46 #define SELFTEST_LOCATION \
47 (::selftest::location (__FILE__, __LINE__, __FUNCTION__))
49 /* The entrypoint for running all tests. */
51 extern void run_tests ();
53 /* Record the successful outcome of some aspect of the test. */
55 extern void pass (const location &loc, const char *msg);
57 /* Report the failed outcome of some aspect of the test and abort. */
59 extern void fail (const location &loc, const char *msg);
61 /* As "fail", but using printf-style formatted output. */
63 extern void fail_formatted (const location &loc, const char *fmt, ...)
64 ATTRIBUTE_PRINTF_2;
66 /* Implementation detail of ASSERT_STREQ. */
68 extern void assert_streq (const location &loc,
69 const char *desc_expected, const char *desc_actual,
70 const char *val_expected, const char *val_actual);
72 /* Declarations for specific families of tests (by source file), in
73 alphabetical order. */
74 extern void bitmap_c_tests ();
75 extern void diagnostic_c_tests ();
76 extern void diagnostic_show_locus_c_tests ();
77 extern void et_forest_c_tests ();
78 extern void fold_const_c_tests ();
79 extern void fibonacci_heap_c_tests ();
80 extern void function_tests_c_tests ();
81 extern void gimple_c_tests ();
82 extern void ggc_tests_c_tests ();
83 extern void hash_map_tests_c_tests ();
84 extern void hash_set_tests_c_tests ();
85 extern void input_c_tests ();
86 extern void pretty_print_c_tests ();
87 extern void rtl_tests_c_tests ();
88 extern void spellcheck_c_tests ();
89 extern void spellcheck_tree_c_tests ();
90 extern void sreal_c_tests ();
91 extern void tree_c_tests ();
92 extern void tree_cfg_c_tests ();
93 extern void vec_c_tests ();
94 extern void wide_int_cc_tests ();
96 extern int num_passes;
98 } /* end of namespace selftest. */
100 /* Macros for writing tests. */
102 /* Evaluate EXPR and coerce to bool, calling
103 ::selftest::pass if it is true,
104 ::selftest::fail if it false. */
106 #define ASSERT_TRUE(EXPR) \
107 ASSERT_TRUE_AT (SELFTEST_LOCATION, (EXPR))
109 /* Like ASSERT_TRUE, but treat LOC as the effective location of the
110 selftest. */
112 #define ASSERT_TRUE_AT(LOC, EXPR) \
113 SELFTEST_BEGIN_STMT \
114 const char *desc = "ASSERT_TRUE (" #EXPR ")"; \
115 bool actual = (EXPR); \
116 if (actual) \
117 ::selftest::pass ((LOC), desc); \
118 else \
119 ::selftest::fail ((LOC), desc); \
120 SELFTEST_END_STMT
122 /* Evaluate EXPR and coerce to bool, calling
123 ::selftest::pass if it is false,
124 ::selftest::fail if it true. */
126 #define ASSERT_FALSE(EXPR) \
127 ASSERT_FALSE_AT (SELFTEST_LOCATION, (EXPR))
129 /* Like ASSERT_FALSE, but treat LOC as the effective location of the
130 selftest. */
132 #define ASSERT_FALSE_AT(LOC, EXPR) \
133 SELFTEST_BEGIN_STMT \
134 const char *desc = "ASSERT_FALSE (" #EXPR ")"; \
135 bool actual = (EXPR); \
136 if (actual) \
137 ::selftest::fail ((LOC), desc); \
138 else \
139 ::selftest::pass ((LOC), desc); \
140 SELFTEST_END_STMT
142 /* Evaluate EXPECTED and ACTUAL and compare them with ==, calling
143 ::selftest::pass if they are equal,
144 ::selftest::fail if they are non-equal. */
146 #define ASSERT_EQ(EXPECTED, ACTUAL) \
147 ASSERT_EQ_AT ((SELFTEST_LOCATION), (EXPECTED), (ACTUAL))
149 /* Like ASSERT_EQ, but treat LOC as the effective location of the
150 selftest. */
152 #define ASSERT_EQ_AT(LOC, EXPECTED, ACTUAL) \
153 SELFTEST_BEGIN_STMT \
154 const char *desc = "ASSERT_EQ (" #EXPECTED ", " #ACTUAL ")"; \
155 if ((EXPECTED) == (ACTUAL)) \
156 ::selftest::pass ((LOC), desc); \
157 else \
158 ::selftest::fail ((LOC), desc); \
159 SELFTEST_END_STMT
161 /* Evaluate EXPECTED and ACTUAL and compare them with !=, calling
162 ::selftest::pass if they are non-equal,
163 ::selftest::fail if they are equal. */
165 #define ASSERT_NE(EXPECTED, ACTUAL) \
166 SELFTEST_BEGIN_STMT \
167 const char *desc = "ASSERT_NE (" #EXPECTED ", " #ACTUAL ")"; \
168 if ((EXPECTED) != (ACTUAL)) \
169 ::selftest::pass (SELFTEST_LOCATION, desc); \
170 else \
171 ::selftest::fail (SELFTEST_LOCATION, desc); \
172 SELFTEST_END_STMT
174 /* Evaluate EXPECTED and ACTUAL and compare them with strcmp, calling
175 ::selftest::pass if they are equal,
176 ::selftest::fail if they are non-equal. */
178 #define ASSERT_STREQ(EXPECTED, ACTUAL) \
179 SELFTEST_BEGIN_STMT \
180 ::selftest::assert_streq (SELFTEST_LOCATION, #EXPECTED, #ACTUAL, \
181 (EXPECTED), (ACTUAL)); \
182 SELFTEST_END_STMT
184 /* Like ASSERT_STREQ, but treat LOC as the effective location of the
185 selftest. */
187 #define ASSERT_STREQ_AT(LOC, EXPECTED, ACTUAL) \
188 SELFTEST_BEGIN_STMT \
189 ::selftest::assert_streq ((LOC), #EXPECTED, #ACTUAL, \
190 (EXPECTED), (ACTUAL)); \
191 SELFTEST_END_STMT
193 /* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
194 ::selftest::fail if it is false. */
196 #define ASSERT_PRED1(PRED1, VAL1) \
197 SELFTEST_BEGIN_STMT \
198 const char *desc = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")"; \
199 bool actual = (PRED1) (VAL1); \
200 if (actual) \
201 ::selftest::pass (SELFTEST_LOCATION, desc); \
202 else \
203 ::selftest::fail (SELFTEST_LOCATION, desc); \
204 SELFTEST_END_STMT
206 #define SELFTEST_BEGIN_STMT do {
207 #define SELFTEST_END_STMT } while (0)
209 #endif /* #if CHECKING_P */
211 #endif /* GCC_SELFTEST_H */