Add ggc-tests.c
[official-gcc.git] / gcc / selftest.h
blobd1f8accfe126144ec80e4f7a2d047404157711a5
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 /* The entrypoint for running all tests. */
32 extern void run_tests ();
34 /* Record the successful outcome of some aspect of the test. */
36 extern void pass (const char *file, int line, const char *msg);
38 /* Report the failed outcome of some aspect of the test and abort. */
40 extern void fail (const char *file, int line, const char *msg);
42 /* Declarations for specific families of tests (by source file), in
43 alphabetical order. */
44 extern void bitmap_c_tests ();
45 extern void diagnostic_show_locus_c_tests ();
46 extern void et_forest_c_tests ();
47 extern void fold_const_c_tests ();
48 extern void function_tests_c_tests ();
49 extern void gimple_c_tests ();
50 extern void ggc_tests_c_tests ();
51 extern void hash_map_tests_c_tests ();
52 extern void hash_set_tests_c_tests ();
53 extern void input_c_tests ();
54 extern void pretty_print_c_tests ();
55 extern void rtl_tests_c_tests ();
56 extern void spellcheck_c_tests ();
57 extern void tree_c_tests ();
58 extern void tree_cfg_c_tests ();
59 extern void vec_c_tests ();
60 extern void wide_int_cc_tests ();
62 extern int num_passes;
64 } /* end of namespace selftest. */
66 /* Macros for writing tests. */
68 /* Evaluate EXPR and coerce to bool, calling
69 ::selftest::pass if it is true,
70 ::selftest::fail if it false. */
72 #define ASSERT_TRUE(EXPR) \
73 SELFTEST_BEGIN_STMT \
74 const char *desc = "ASSERT_TRUE (" #EXPR ")"; \
75 bool actual = (EXPR); \
76 if (actual) \
77 ::selftest::pass (__FILE__, __LINE__, desc); \
78 else \
79 ::selftest::fail (__FILE__, __LINE__, desc); \
80 SELFTEST_END_STMT
82 /* Evaluate EXPR and coerce to bool, calling
83 ::selftest::pass if it is false,
84 ::selftest::fail if it true. */
86 #define ASSERT_FALSE(EXPR) \
87 SELFTEST_BEGIN_STMT \
88 const char *desc = "ASSERT_FALSE (" #EXPR ")"; \
89 bool actual = (EXPR); \
90 if (actual) \
91 ::selftest::fail (__FILE__, __LINE__, desc); \
92 else \
93 ::selftest::pass (__FILE__, __LINE__, desc); \
94 SELFTEST_END_STMT
96 /* Evaluate EXPECTED and ACTUAL and compare them with ==, calling
97 ::selftest::pass if they are equal,
98 ::selftest::fail if they are non-equal. */
100 #define ASSERT_EQ(EXPECTED, ACTUAL) \
101 SELFTEST_BEGIN_STMT \
102 const char *desc = "ASSERT_EQ (" #EXPECTED ", " #ACTUAL ")"; \
103 if ((EXPECTED) == (ACTUAL)) \
104 ::selftest::pass (__FILE__, __LINE__, desc); \
105 else \
106 ::selftest::fail (__FILE__, __LINE__, desc); \
107 SELFTEST_END_STMT
109 /* Evaluate EXPECTED and ACTUAL and compare them with !=, calling
110 ::selftest::pass if they are non-equal,
111 ::selftest::fail if they are equal. */
113 #define ASSERT_NE(EXPECTED, ACTUAL) \
114 SELFTEST_BEGIN_STMT \
115 const char *desc = "ASSERT_NE (" #EXPECTED ", " #ACTUAL ")"; \
116 if ((EXPECTED) != (ACTUAL)) \
117 ::selftest::pass (__FILE__, __LINE__, desc); \
118 else \
119 ::selftest::fail (__FILE__, __LINE__, desc); \
120 SELFTEST_END_STMT
122 /* Evaluate EXPECTED and ACTUAL and compare them with strcmp, calling
123 ::selftest::pass if they are equal,
124 ::selftest::fail if they are non-equal. */
126 #define ASSERT_STREQ(EXPECTED, ACTUAL) \
127 SELFTEST_BEGIN_STMT \
128 const char *desc = "ASSERT_STREQ (" #EXPECTED ", " #ACTUAL ")"; \
129 const char *expected_ = (EXPECTED); \
130 const char *actual_ = (ACTUAL); \
131 if (0 == strcmp (expected_, actual_)) \
132 ::selftest::pass (__FILE__, __LINE__, desc); \
133 else \
134 ::selftest::fail (__FILE__, __LINE__, desc); \
135 SELFTEST_END_STMT
137 /* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
138 ::selftest::fail if it is false. */
140 #define ASSERT_PRED1(PRED1, VAL1) \
141 SELFTEST_BEGIN_STMT \
142 const char *desc = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")"; \
143 bool actual = (PRED1) (VAL1); \
144 if (actual) \
145 ::selftest::pass (__FILE__, __LINE__, desc); \
146 else \
147 ::selftest::fail (__FILE__, __LINE__, desc); \
148 SELFTEST_END_STMT
150 #define SELFTEST_BEGIN_STMT do {
151 #define SELFTEST_END_STMT } while (0)
153 #endif /* #if CHECKING_P */
155 #endif /* GCC_SELFTEST_H */