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
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
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. */
30 /* A struct describing the source-location of a selftest, to make it
31 easier to track down failing tests. */
35 location (const char *file
, int line
, const char *function
)
36 : m_file (file
), m_line (line
), m_function (function
) {}
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
, ...)
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 function_tests_c_tests ();
80 extern void gimple_c_tests ();
81 extern void ggc_tests_c_tests ();
82 extern void hash_map_tests_c_tests ();
83 extern void hash_set_tests_c_tests ();
84 extern void input_c_tests ();
85 extern void pretty_print_c_tests ();
86 extern void rtl_tests_c_tests ();
87 extern void spellcheck_c_tests ();
88 extern void spellcheck_tree_c_tests ();
89 extern void tree_c_tests ();
90 extern void tree_cfg_c_tests ();
91 extern void vec_c_tests ();
92 extern void wide_int_cc_tests ();
94 extern int num_passes
;
96 } /* end of namespace selftest. */
98 /* Macros for writing tests. */
100 /* Evaluate EXPR and coerce to bool, calling
101 ::selftest::pass if it is true,
102 ::selftest::fail if it false. */
104 #define ASSERT_TRUE(EXPR) \
105 SELFTEST_BEGIN_STMT \
106 const char *desc = "ASSERT_TRUE (" #EXPR ")"; \
107 bool actual = (EXPR); \
109 ::selftest::pass (SELFTEST_LOCATION, desc); \
111 ::selftest::fail (SELFTEST_LOCATION, desc); \
114 /* Evaluate EXPR and coerce to bool, calling
115 ::selftest::pass if it is false,
116 ::selftest::fail if it true. */
118 #define ASSERT_FALSE(EXPR) \
119 SELFTEST_BEGIN_STMT \
120 const char *desc = "ASSERT_FALSE (" #EXPR ")"; \
121 bool actual = (EXPR); \
123 ::selftest::fail (SELFTEST_LOCATION, desc); \
125 ::selftest::pass (SELFTEST_LOCATION, desc); \
128 /* Evaluate EXPECTED and ACTUAL and compare them with ==, calling
129 ::selftest::pass if they are equal,
130 ::selftest::fail if they are non-equal. */
132 #define ASSERT_EQ(EXPECTED, ACTUAL) \
133 SELFTEST_BEGIN_STMT \
134 const char *desc = "ASSERT_EQ (" #EXPECTED ", " #ACTUAL ")"; \
135 if ((EXPECTED) == (ACTUAL)) \
136 ::selftest::pass (SELFTEST_LOCATION, desc); \
138 ::selftest::fail (SELFTEST_LOCATION, desc); \
141 /* Evaluate EXPECTED and ACTUAL and compare them with !=, calling
142 ::selftest::pass if they are non-equal,
143 ::selftest::fail if they are equal. */
145 #define ASSERT_NE(EXPECTED, ACTUAL) \
146 SELFTEST_BEGIN_STMT \
147 const char *desc = "ASSERT_NE (" #EXPECTED ", " #ACTUAL ")"; \
148 if ((EXPECTED) != (ACTUAL)) \
149 ::selftest::pass (SELFTEST_LOCATION, desc); \
151 ::selftest::fail (SELFTEST_LOCATION, desc); \
154 /* Evaluate EXPECTED and ACTUAL and compare them with strcmp, calling
155 ::selftest::pass if they are equal,
156 ::selftest::fail if they are non-equal. */
158 #define ASSERT_STREQ(EXPECTED, ACTUAL) \
159 SELFTEST_BEGIN_STMT \
160 ::selftest::assert_streq (SELFTEST_LOCATION, #EXPECTED, #ACTUAL, \
161 (EXPECTED), (ACTUAL)); \
164 /* Like ASSERT_STREQ_AT, but treat LOC as the effective location of the
167 #define ASSERT_STREQ_AT(LOC, EXPECTED, ACTUAL) \
168 SELFTEST_BEGIN_STMT \
169 ::selftest::assert_streq ((LOC), #EXPECTED, #ACTUAL, \
170 (EXPECTED), (ACTUAL)); \
173 /* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
174 ::selftest::fail if it is false. */
176 #define ASSERT_PRED1(PRED1, VAL1) \
177 SELFTEST_BEGIN_STMT \
178 const char *desc = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")"; \
179 bool actual = (PRED1) (VAL1); \
181 ::selftest::pass (SELFTEST_LOCATION, desc); \
183 ::selftest::fail (SELFTEST_LOCATION, desc); \
186 #define SELFTEST_BEGIN_STMT do {
187 #define SELFTEST_END_STMT } while (0)
189 #endif /* #if CHECKING_P */
191 #endif /* GCC_SELFTEST_H */