]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/selftest.h
Add selftest for pretty-print.c
[thirdparty/gcc.git] / gcc / selftest.h
CommitLineData
d9b950dd
DM
1/* A self-testing framework, for use by -fself-test.
2 Copyright (C) 2015-2016 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along 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
28namespace selftest {
29
30/* The entrypoint for running all tests. */
31
32extern void run_tests ();
33
34/* Record the successful outcome of some aspect of the test. */
35
36extern void pass (const char *file, int line, const char *msg);
37
38/* Report the failed outcome of some aspect of the test and abort. */
39
40extern void fail (const char *file, int line, const char *msg);
41
42/* Declarations for specific families of tests (by source file), in
43 alphabetical order. */
44extern void bitmap_c_tests ();
45extern void diagnostic_show_locus_c_tests ();
46extern void et_forest_c_tests ();
47extern void fold_const_c_tests ();
48extern void function_tests_c_tests ();
49extern void gimple_c_tests ();
50extern void hash_map_tests_c_tests ();
51extern void hash_set_tests_c_tests ();
52extern void input_c_tests ();
4ccab56d 53extern void pretty_print_c_tests ();
d9b950dd
DM
54extern void rtl_tests_c_tests ();
55extern void spellcheck_c_tests ();
56extern void tree_c_tests ();
57extern void tree_cfg_c_tests ();
58extern void vec_c_tests ();
59extern void wide_int_cc_tests ();
60
61extern int num_passes;
62
63} /* end of namespace selftest. */
64
65/* Macros for writing tests. */
66
67/* Evaluate EXPR and coerce to bool, calling
68 ::selftest::pass if it is true,
69 ::selftest::fail if it false. */
70
71#define ASSERT_TRUE(EXPR) \
72 SELFTEST_BEGIN_STMT \
73 const char *desc = "ASSERT_TRUE (" #EXPR ")"; \
74 bool actual = (EXPR); \
75 if (actual) \
76 ::selftest::pass (__FILE__, __LINE__, desc); \
77 else \
78 ::selftest::fail (__FILE__, __LINE__, desc); \
79 SELFTEST_END_STMT
80
81/* Evaluate EXPR and coerce to bool, calling
82 ::selftest::pass if it is false,
83 ::selftest::fail if it true. */
84
85#define ASSERT_FALSE(EXPR) \
86 SELFTEST_BEGIN_STMT \
87 const char *desc = "ASSERT_FALSE (" #EXPR ")"; \
88 bool actual = (EXPR); \
89 if (actual) \
90 ::selftest::fail (__FILE__, __LINE__, desc); \
91 else \
92 ::selftest::pass (__FILE__, __LINE__, desc); \
93 SELFTEST_END_STMT
94
95/* Evaluate EXPECTED and ACTUAL and compare them with ==, calling
96 ::selftest::pass if they are equal,
97 ::selftest::fail if they are non-equal. */
98
99#define ASSERT_EQ(EXPECTED, ACTUAL) \
100 SELFTEST_BEGIN_STMT \
101 const char *desc = "ASSERT_EQ (" #EXPECTED ", " #ACTUAL ")"; \
102 if ((EXPECTED) == (ACTUAL)) \
103 ::selftest::pass (__FILE__, __LINE__, desc); \
104 else \
105 ::selftest::fail (__FILE__, __LINE__, desc); \
106 SELFTEST_END_STMT
107
108/* Evaluate EXPECTED and ACTUAL and compare them with !=, calling
109 ::selftest::pass if they are non-equal,
110 ::selftest::fail if they are equal. */
111
112#define ASSERT_NE(EXPECTED, ACTUAL) \
113 SELFTEST_BEGIN_STMT \
114 const char *desc = "ASSERT_NE (" #EXPECTED ", " #ACTUAL ")"; \
115 if ((EXPECTED) != (ACTUAL)) \
116 ::selftest::pass (__FILE__, __LINE__, desc); \
117 else \
118 ::selftest::fail (__FILE__, __LINE__, desc); \
119 SELFTEST_END_STMT
120
121/* Evaluate EXPECTED and ACTUAL and compare them with strcmp, calling
122 ::selftest::pass if they are equal,
123 ::selftest::fail if they are non-equal. */
124
125#define ASSERT_STREQ(EXPECTED, ACTUAL) \
126 SELFTEST_BEGIN_STMT \
127 const char *desc = "ASSERT_STREQ (" #EXPECTED ", " #ACTUAL ")"; \
128 const char *expected_ = (EXPECTED); \
129 const char *actual_ = (ACTUAL); \
130 if (0 == strcmp (expected_, actual_)) \
131 ::selftest::pass (__FILE__, __LINE__, desc); \
132 else \
133 ::selftest::fail (__FILE__, __LINE__, desc); \
134 SELFTEST_END_STMT
135
136/* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
137 ::selftest::fail if it is false. */
138
139#define ASSERT_PRED1(PRED1, VAL1) \
140 SELFTEST_BEGIN_STMT \
141 const char *desc = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")"; \
142 bool actual = (PRED1) (VAL1); \
143 if (actual) \
144 ::selftest::pass (__FILE__, __LINE__, desc); \
145 else \
146 ::selftest::fail (__FILE__, __LINE__, desc); \
147 SELFTEST_END_STMT
148
149#define SELFTEST_BEGIN_STMT do {
150#define SELFTEST_END_STMT } while (0)
151
152#endif /* #if CHECKING_P */
153
154#endif /* GCC_SELFTEST_H */