/cp
[official-gcc.git] / gcc / selftest.h
blob2bc7316c990eaa2e27e11f9d544a1655d9f02bd3
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_show_locus_c_tests ();
76 extern void et_forest_c_tests ();
77 extern void fold_const_c_tests ();
78 extern void function_tests_c_tests ();
79 extern void gimple_c_tests ();
80 extern void ggc_tests_c_tests ();
81 extern void hash_map_tests_c_tests ();
82 extern void hash_set_tests_c_tests ();
83 extern void input_c_tests ();
84 extern void pretty_print_c_tests ();
85 extern void rtl_tests_c_tests ();
86 extern void spellcheck_c_tests ();
87 extern void spellcheck_tree_c_tests ();
88 extern void tree_c_tests ();
89 extern void tree_cfg_c_tests ();
90 extern void vec_c_tests ();
91 extern void wide_int_cc_tests ();
93 extern int num_passes;
95 } /* end of namespace selftest. */
97 /* Macros for writing tests. */
99 /* Evaluate EXPR and coerce to bool, calling
100 ::selftest::pass if it is true,
101 ::selftest::fail if it false. */
103 #define ASSERT_TRUE(EXPR) \
104 SELFTEST_BEGIN_STMT \
105 const char *desc = "ASSERT_TRUE (" #EXPR ")"; \
106 bool actual = (EXPR); \
107 if (actual) \
108 ::selftest::pass (SELFTEST_LOCATION, desc); \
109 else \
110 ::selftest::fail (SELFTEST_LOCATION, desc); \
111 SELFTEST_END_STMT
113 /* Evaluate EXPR and coerce to bool, calling
114 ::selftest::pass if it is false,
115 ::selftest::fail if it true. */
117 #define ASSERT_FALSE(EXPR) \
118 SELFTEST_BEGIN_STMT \
119 const char *desc = "ASSERT_FALSE (" #EXPR ")"; \
120 bool actual = (EXPR); \
121 if (actual) \
122 ::selftest::fail (SELFTEST_LOCATION, desc); \
123 else \
124 ::selftest::pass (SELFTEST_LOCATION, desc); \
125 SELFTEST_END_STMT
127 /* Evaluate EXPECTED and ACTUAL and compare them with ==, calling
128 ::selftest::pass if they are equal,
129 ::selftest::fail if they are non-equal. */
131 #define ASSERT_EQ(EXPECTED, ACTUAL) \
132 SELFTEST_BEGIN_STMT \
133 const char *desc = "ASSERT_EQ (" #EXPECTED ", " #ACTUAL ")"; \
134 if ((EXPECTED) == (ACTUAL)) \
135 ::selftest::pass (SELFTEST_LOCATION, desc); \
136 else \
137 ::selftest::fail (SELFTEST_LOCATION, desc); \
138 SELFTEST_END_STMT
140 /* Evaluate EXPECTED and ACTUAL and compare them with !=, calling
141 ::selftest::pass if they are non-equal,
142 ::selftest::fail if they are equal. */
144 #define ASSERT_NE(EXPECTED, ACTUAL) \
145 SELFTEST_BEGIN_STMT \
146 const char *desc = "ASSERT_NE (" #EXPECTED ", " #ACTUAL ")"; \
147 if ((EXPECTED) != (ACTUAL)) \
148 ::selftest::pass (SELFTEST_LOCATION, desc); \
149 else \
150 ::selftest::fail (SELFTEST_LOCATION, desc); \
151 SELFTEST_END_STMT
153 /* Evaluate EXPECTED and ACTUAL and compare them with strcmp, calling
154 ::selftest::pass if they are equal,
155 ::selftest::fail if they are non-equal. */
157 #define ASSERT_STREQ(EXPECTED, ACTUAL) \
158 SELFTEST_BEGIN_STMT \
159 ::selftest::assert_streq (SELFTEST_LOCATION, #EXPECTED, #ACTUAL, \
160 (EXPECTED), (ACTUAL)); \
161 SELFTEST_END_STMT
163 /* Like ASSERT_STREQ_AT, but treat LOC as the effective location of the
164 selftest. */
166 #define ASSERT_STREQ_AT(LOC, EXPECTED, ACTUAL) \
167 SELFTEST_BEGIN_STMT \
168 ::selftest::assert_streq ((LOC), #EXPECTED, #ACTUAL, \
169 (EXPECTED), (ACTUAL)); \
170 SELFTEST_END_STMT
172 /* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
173 ::selftest::fail if it is false. */
175 #define ASSERT_PRED1(PRED1, VAL1) \
176 SELFTEST_BEGIN_STMT \
177 const char *desc = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")"; \
178 bool actual = (PRED1) (VAL1); \
179 if (actual) \
180 ::selftest::pass (SELFTEST_LOCATION, desc); \
181 else \
182 ::selftest::fail (SELFTEST_LOCATION, desc); \
183 SELFTEST_END_STMT
185 #define SELFTEST_BEGIN_STMT do {
186 #define SELFTEST_END_STMT } while (0)
188 #endif /* #if CHECKING_P */
190 #endif /* GCC_SELFTEST_H */