]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/selftest.h
Selftest framework
[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 ();
53extern void rtl_tests_c_tests ();
54extern void spellcheck_c_tests ();
55extern void tree_c_tests ();
56extern void tree_cfg_c_tests ();
57extern void vec_c_tests ();
58extern void wide_int_cc_tests ();
59
60extern int num_passes;
61
62} /* end of namespace selftest. */
63
64/* Macros for writing tests. */
65
66/* Evaluate EXPR and coerce to bool, calling
67 ::selftest::pass if it is true,
68 ::selftest::fail if it false. */
69
70#define ASSERT_TRUE(EXPR) \
71 SELFTEST_BEGIN_STMT \
72 const char *desc = "ASSERT_TRUE (" #EXPR ")"; \
73 bool actual = (EXPR); \
74 if (actual) \
75 ::selftest::pass (__FILE__, __LINE__, desc); \
76 else \
77 ::selftest::fail (__FILE__, __LINE__, desc); \
78 SELFTEST_END_STMT
79
80/* Evaluate EXPR and coerce to bool, calling
81 ::selftest::pass if it is false,
82 ::selftest::fail if it true. */
83
84#define ASSERT_FALSE(EXPR) \
85 SELFTEST_BEGIN_STMT \
86 const char *desc = "ASSERT_FALSE (" #EXPR ")"; \
87 bool actual = (EXPR); \
88 if (actual) \
89 ::selftest::fail (__FILE__, __LINE__, desc); \
90 else \
91 ::selftest::pass (__FILE__, __LINE__, desc); \
92 SELFTEST_END_STMT
93
94/* Evaluate EXPECTED and ACTUAL and compare them with ==, calling
95 ::selftest::pass if they are equal,
96 ::selftest::fail if they are non-equal. */
97
98#define ASSERT_EQ(EXPECTED, ACTUAL) \
99 SELFTEST_BEGIN_STMT \
100 const char *desc = "ASSERT_EQ (" #EXPECTED ", " #ACTUAL ")"; \
101 if ((EXPECTED) == (ACTUAL)) \
102 ::selftest::pass (__FILE__, __LINE__, desc); \
103 else \
104 ::selftest::fail (__FILE__, __LINE__, desc); \
105 SELFTEST_END_STMT
106
107/* Evaluate EXPECTED and ACTUAL and compare them with !=, calling
108 ::selftest::pass if they are non-equal,
109 ::selftest::fail if they are equal. */
110
111#define ASSERT_NE(EXPECTED, ACTUAL) \
112 SELFTEST_BEGIN_STMT \
113 const char *desc = "ASSERT_NE (" #EXPECTED ", " #ACTUAL ")"; \
114 if ((EXPECTED) != (ACTUAL)) \
115 ::selftest::pass (__FILE__, __LINE__, desc); \
116 else \
117 ::selftest::fail (__FILE__, __LINE__, desc); \
118 SELFTEST_END_STMT
119
120/* Evaluate EXPECTED and ACTUAL and compare them with strcmp, calling
121 ::selftest::pass if they are equal,
122 ::selftest::fail if they are non-equal. */
123
124#define ASSERT_STREQ(EXPECTED, ACTUAL) \
125 SELFTEST_BEGIN_STMT \
126 const char *desc = "ASSERT_STREQ (" #EXPECTED ", " #ACTUAL ")"; \
127 const char *expected_ = (EXPECTED); \
128 const char *actual_ = (ACTUAL); \
129 if (0 == strcmp (expected_, actual_)) \
130 ::selftest::pass (__FILE__, __LINE__, desc); \
131 else \
132 ::selftest::fail (__FILE__, __LINE__, desc); \
133 SELFTEST_END_STMT
134
135/* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
136 ::selftest::fail if it is false. */
137
138#define ASSERT_PRED1(PRED1, VAL1) \
139 SELFTEST_BEGIN_STMT \
140 const char *desc = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")"; \
141 bool actual = (PRED1) (VAL1); \
142 if (actual) \
143 ::selftest::pass (__FILE__, __LINE__, desc); \
144 else \
145 ::selftest::fail (__FILE__, __LINE__, desc); \
146 SELFTEST_END_STMT
147
148#define SELFTEST_BEGIN_STMT do {
149#define SELFTEST_END_STMT } while (0)
150
151#endif /* #if CHECKING_P */
152
153#endif /* GCC_SELFTEST_H */