re PR tree-optimization/71522 (Wrong optimization of memcpy through a var of type...
[platform/upstream/gcc.git] / gcc / selftest.h
1 /* A self-testing framework, for use by -fself-test.
2    Copyright (C) 2015-2016 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>.  */
19
20 #ifndef GCC_SELFTEST_H
21 #define GCC_SELFTEST_H
22
23 /* The selftest code should entirely disappear in a production
24    configuration, hence we guard all of it with #if CHECKING_P.  */
25
26 #if CHECKING_P
27
28 namespace selftest {
29
30 /* A struct describing the source-location of a selftest, to make it
31    easier to track down failing tests.  */
32
33 struct location
34 {
35   location (const char *file, int line, const char *function)
36     : m_file (file), m_line (line), m_function (function) {}
37
38   const char *m_file;
39   int m_line;
40   const char *m_function;
41 };
42
43 /* A macro for use in selftests and by the ASSERT_ macros below,
44    constructing a selftest::location for the current source location.  */
45
46 #define SELFTEST_LOCATION \
47   (::selftest::location (__FILE__, __LINE__, __FUNCTION__))
48
49 /* The entrypoint for running all tests.  */
50
51 extern void run_tests ();
52
53 /* Record the successful outcome of some aspect of the test.  */
54
55 extern void pass (const location &loc, const char *msg);
56
57 /* Report the failed outcome of some aspect of the test and abort.  */
58
59 extern void fail (const location &loc, const char *msg);
60
61 /* As "fail", but using printf-style formatted output.  */
62
63 extern void fail_formatted (const location &loc, const char *fmt, ...)
64  ATTRIBUTE_PRINTF_2;
65
66 /* Implementation detail of ASSERT_STREQ.  */
67
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);
71
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 ();
91
92 extern int num_passes;
93
94 } /* end of namespace selftest.  */
95
96 /* Macros for writing tests.  */
97
98 /* Evaluate EXPR and coerce to bool, calling
99    ::selftest::pass if it is true,
100    ::selftest::fail if it false.  */
101
102 #define ASSERT_TRUE(EXPR)                               \
103   SELFTEST_BEGIN_STMT                                   \
104   const char *desc = "ASSERT_TRUE (" #EXPR ")";         \
105   bool actual = (EXPR);                                 \
106   if (actual)                                           \
107     ::selftest::pass (SELFTEST_LOCATION, desc); \
108   else                                                  \
109     ::selftest::fail (SELFTEST_LOCATION, desc);         \
110   SELFTEST_END_STMT
111
112 /* Evaluate EXPR and coerce to bool, calling
113    ::selftest::pass if it is false,
114    ::selftest::fail if it true.  */
115
116 #define ASSERT_FALSE(EXPR)                                      \
117   SELFTEST_BEGIN_STMT                                           \
118   const char *desc = "ASSERT_FALSE (" #EXPR ")";                \
119   bool actual = (EXPR);                                 \
120   if (actual)                                                   \
121     ::selftest::fail (SELFTEST_LOCATION, desc);                         \
122   else                                                          \
123     ::selftest::pass (SELFTEST_LOCATION, desc);                         \
124   SELFTEST_END_STMT
125
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.  */
129
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);                        \
135   else                                                         \
136     ::selftest::fail (SELFTEST_LOCATION, desc);                        \
137   SELFTEST_END_STMT
138
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.  */
142
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);                        \
148   else                                                         \
149     ::selftest::fail (SELFTEST_LOCATION, desc);                        \
150   SELFTEST_END_STMT
151
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.  */
155
156 #define ASSERT_STREQ(EXPECTED, ACTUAL)                              \
157   SELFTEST_BEGIN_STMT                                               \
158   ::selftest::assert_streq (SELFTEST_LOCATION, #EXPECTED, #ACTUAL, \
159                             (EXPECTED), (ACTUAL));                  \
160   SELFTEST_END_STMT
161
162 /* Like ASSERT_STREQ_AT, but treat LOC as the effective location of the
163    selftest.  */
164
165 #define ASSERT_STREQ_AT(LOC, EXPECTED, ACTUAL)                      \
166   SELFTEST_BEGIN_STMT                                               \
167   ::selftest::assert_streq ((LOC), #EXPECTED, #ACTUAL,              \
168                             (EXPECTED), (ACTUAL));                  \
169   SELFTEST_END_STMT
170
171 /* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
172    ::selftest::fail if it is false.  */
173
174 #define ASSERT_PRED1(PRED1, VAL1)                       \
175   SELFTEST_BEGIN_STMT                                   \
176   const char *desc = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")";    \
177   bool actual = (PRED1) (VAL1);                         \
178   if (actual)                                           \
179     ::selftest::pass (SELFTEST_LOCATION, desc);                 \
180   else                                                  \
181     ::selftest::fail (SELFTEST_LOCATION, desc);                 \
182   SELFTEST_END_STMT
183
184 #define SELFTEST_BEGIN_STMT do {
185 #define SELFTEST_END_STMT   } while (0)
186
187 #endif /* #if CHECKING_P */
188
189 #endif /* GCC_SELFTEST_H */