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_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 tree_c_tests ();
88 extern void tree_cfg_c_tests ();
89 extern void vec_c_tests ();
90 extern void wide_int_cc_tests ();
92 extern int num_passes;
94 } /* end of namespace selftest. */
96 /* Macros for writing tests. */
98 /* Evaluate EXPR and coerce to bool, calling
99 ::selftest::pass if it is true,
100 ::selftest::fail if it false. */
102 #define ASSERT_TRUE(EXPR) \
103 SELFTEST_BEGIN_STMT \
104 const char *desc = "ASSERT_TRUE (" #EXPR ")"; \
105 bool actual = (EXPR); \
107 ::selftest::pass (SELFTEST_LOCATION, desc); \
109 ::selftest::fail (SELFTEST_LOCATION, desc); \
112 /* Evaluate EXPR and coerce to bool, calling
113 ::selftest::pass if it is false,
114 ::selftest::fail if it true. */
116 #define ASSERT_FALSE(EXPR) \
117 SELFTEST_BEGIN_STMT \
118 const char *desc = "ASSERT_FALSE (" #EXPR ")"; \
119 bool actual = (EXPR); \
121 ::selftest::fail (SELFTEST_LOCATION, desc); \
123 ::selftest::pass (SELFTEST_LOCATION, desc); \
126 /* Evaluate EXPECTED and ACTUAL and compare them with ==, calling
127 ::selftest::pass if they are equal,
128 ::selftest::fail if they are non-equal. */
130 #define ASSERT_EQ(EXPECTED, ACTUAL) \
131 SELFTEST_BEGIN_STMT \
132 const char *desc = "ASSERT_EQ (" #EXPECTED ", " #ACTUAL ")"; \
133 if ((EXPECTED) == (ACTUAL)) \
134 ::selftest::pass (SELFTEST_LOCATION, desc); \
136 ::selftest::fail (SELFTEST_LOCATION, desc); \
139 /* Evaluate EXPECTED and ACTUAL and compare them with !=, calling
140 ::selftest::pass if they are non-equal,
141 ::selftest::fail if they are equal. */
143 #define ASSERT_NE(EXPECTED, ACTUAL) \
144 SELFTEST_BEGIN_STMT \
145 const char *desc = "ASSERT_NE (" #EXPECTED ", " #ACTUAL ")"; \
146 if ((EXPECTED) != (ACTUAL)) \
147 ::selftest::pass (SELFTEST_LOCATION, desc); \
149 ::selftest::fail (SELFTEST_LOCATION, desc); \
152 /* Evaluate EXPECTED and ACTUAL and compare them with strcmp, calling
153 ::selftest::pass if they are equal,
154 ::selftest::fail if they are non-equal. */
156 #define ASSERT_STREQ(EXPECTED, ACTUAL) \
157 SELFTEST_BEGIN_STMT \
158 ::selftest::assert_streq (SELFTEST_LOCATION, #EXPECTED, #ACTUAL, \
159 (EXPECTED), (ACTUAL)); \
162 /* Like ASSERT_STREQ_AT, but treat LOC as the effective location of the
165 #define ASSERT_STREQ_AT(LOC, EXPECTED, ACTUAL) \
166 SELFTEST_BEGIN_STMT \
167 ::selftest::assert_streq ((LOC), #EXPECTED, #ACTUAL, \
168 (EXPECTED), (ACTUAL)); \
171 /* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
172 ::selftest::fail if it is false. */
174 #define ASSERT_PRED1(PRED1, VAL1) \
175 SELFTEST_BEGIN_STMT \
176 const char *desc = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")"; \
177 bool actual = (PRED1) (VAL1); \
179 ::selftest::pass (SELFTEST_LOCATION, desc); \
181 ::selftest::fail (SELFTEST_LOCATION, desc); \
184 #define SELFTEST_BEGIN_STMT do {
185 #define SELFTEST_END_STMT } while (0)
187 #endif /* #if CHECKING_P */
189 #endif /* GCC_SELFTEST_H */