aboutsummaryrefslogtreecommitdiff
path: root/gcc/selftest.h
blob: 72de61f620e8227ba8220bd395d5fcc33e0ba663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* A self-testing framework, for use by -fself-test.
   Copyright (C) 2015-2016 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#ifndef GCC_SELFTEST_H
#define GCC_SELFTEST_H

/* The selftest code should entirely disappear in a production
   configuration, hence we guard all of it with #if CHECKING_P.  */

#if CHECKING_P

namespace selftest {

/* A struct describing the source-location of a selftest, to make it
   easier to track down failing tests.  */

struct location
{
  location (const char *file, int line, const char *function)
    : m_file (file), m_line (line), m_function (function) {}

  const char *m_file;
  int m_line;
  const char *m_function;
};

/* A macro for use in selftests and by the ASSERT_ macros below,
   constructing a selftest::location for the current source location.  */

#define SELFTEST_LOCATION \
  (::selftest::location (__FILE__, __LINE__, __FUNCTION__))

/* The entrypoint for running all tests.  */

extern void run_tests ();

/* Record the successful outcome of some aspect of the test.  */

extern void pass (const location &loc, const char *msg);

/* Report the failed outcome of some aspect of the test and abort.  */

extern void fail (const location &loc, const char *msg);

/* As "fail", but using printf-style formatted output.  */

extern void fail_formatted (const location &loc, const char *fmt, ...)
 ATTRIBUTE_PRINTF_2;

/* Implementation detail of ASSERT_STREQ.  */

extern void assert_streq (const location &loc,
			  const char *desc_expected, const char *desc_actual,
			  const char *val_expected, const char *val_actual);

/* A class for writing out a temporary sourcefile for use in selftests
   of input handling.  */

class temp_source_file
{
 public:
  temp_source_file (const location &loc, const char *suffix,
		    const char *content);
  ~temp_source_file ();

  const char *get_filename () const { return m_filename; }

 private:
  char *m_filename;
};

/* Declarations for specific families of tests (by source file), in
   alphabetical order.  */
extern void bitmap_c_tests ();
extern void diagnostic_c_tests ();
extern void diagnostic_show_locus_c_tests ();
extern void et_forest_c_tests ();
extern void fold_const_c_tests ();
extern void fibonacci_heap_c_tests ();
extern void function_tests_c_tests ();
extern void gimple_c_tests ();
extern void ggc_tests_c_tests ();
extern void hash_map_tests_c_tests ();
extern void hash_set_tests_c_tests ();
extern void input_c_tests ();
extern void pretty_print_c_tests ();
extern void rtl_tests_c_tests ();
extern void selftest_c_tests ();
extern void spellcheck_c_tests ();
extern void spellcheck_tree_c_tests ();
extern void sreal_c_tests ();
extern void tree_c_tests ();
extern void tree_cfg_c_tests ();
extern void vec_c_tests ();
extern void wide_int_cc_tests ();

extern int num_passes;

} /* end of namespace selftest.  */

/* Macros for writing tests.  */

/* Evaluate EXPR and coerce to bool, calling
   ::selftest::pass if it is true,
   ::selftest::fail if it false.  */

#define ASSERT_TRUE(EXPR)				\
  ASSERT_TRUE_AT (SELFTEST_LOCATION, (EXPR))

/* Like ASSERT_TRUE, but treat LOC as the effective location of the
   selftest.  */

#define ASSERT_TRUE_AT(LOC, EXPR)			\
  SELFTEST_BEGIN_STMT					\
  const char *desc = "ASSERT_TRUE (" #EXPR ")";		\
  bool actual = (EXPR);					\
  if (actual)						\
    ::selftest::pass ((LOC), desc);			\
  else							\
    ::selftest::fail ((LOC), desc);			\
  SELFTEST_END_STMT

/* Evaluate EXPR and coerce to bool, calling
   ::selftest::pass if it is false,
   ::selftest::fail if it true.  */

#define ASSERT_FALSE(EXPR)					\
  ASSERT_FALSE_AT (SELFTEST_LOCATION, (EXPR))

/* Like ASSERT_FALSE, but treat LOC as the effective location of the
   selftest.  */

#define ASSERT_FALSE_AT(LOC, EXPR)				\
  SELFTEST_BEGIN_STMT						\
  const char *desc = "ASSERT_FALSE (" #EXPR ")";			\
  bool actual = (EXPR);							\
  if (actual)								\
    ::selftest::fail ((LOC), desc);			\
  else									\
    ::selftest::pass ((LOC), desc);					\
  SELFTEST_END_STMT

/* Evaluate EXPECTED and ACTUAL and compare them with ==, calling
   ::selftest::pass if they are equal,
   ::selftest::fail if they are non-equal.  */

#define ASSERT_EQ(EXPECTED, ACTUAL) \
  ASSERT_EQ_AT ((SELFTEST_LOCATION), (EXPECTED), (ACTUAL))

/* Like ASSERT_EQ, but treat LOC as the effective location of the
   selftest.  */

#define ASSERT_EQ_AT(LOC, EXPECTED, ACTUAL)		       \
  SELFTEST_BEGIN_STMT					       \
  const char *desc = "ASSERT_EQ (" #EXPECTED ", " #ACTUAL ")"; \
  if ((EXPECTED) == (ACTUAL))				       \
    ::selftest::pass ((LOC), desc);			       \
  else							       \
    ::selftest::fail ((LOC), desc);			       \
  SELFTEST_END_STMT

/* Evaluate EXPECTED and ACTUAL and compare them with !=, calling
   ::selftest::pass if they are non-equal,
   ::selftest::fail if they are equal.  */

#define ASSERT_NE(EXPECTED, ACTUAL)			       \
  SELFTEST_BEGIN_STMT					       \
  const char *desc = "ASSERT_NE (" #EXPECTED ", " #ACTUAL ")"; \
  if ((EXPECTED) != (ACTUAL))				       \
    ::selftest::pass (SELFTEST_LOCATION, desc);			       \
  else							       \
    ::selftest::fail (SELFTEST_LOCATION, desc);			       \
  SELFTEST_END_STMT

/* Evaluate EXPECTED and ACTUAL and compare them with strcmp, calling
   ::selftest::pass if they are equal,
   ::selftest::fail if they are non-equal.  */

#define ASSERT_STREQ(EXPECTED, ACTUAL)				    \
  SELFTEST_BEGIN_STMT						    \
  ::selftest::assert_streq (SELFTEST_LOCATION, #EXPECTED, #ACTUAL, \
			    (EXPECTED), (ACTUAL));		    \
  SELFTEST_END_STMT

/* Like ASSERT_STREQ, but treat LOC as the effective location of the
   selftest.  */

#define ASSERT_STREQ_AT(LOC, EXPECTED, ACTUAL)			    \
  SELFTEST_BEGIN_STMT						    \
  ::selftest::assert_streq ((LOC), #EXPECTED, #ACTUAL,		    \
			    (EXPECTED), (ACTUAL));		    \
  SELFTEST_END_STMT

/* Evaluate PRED1 (VAL1), calling ::selftest::pass if it is true,
   ::selftest::fail if it is false.  */

#define ASSERT_PRED1(PRED1, VAL1)			\
  SELFTEST_BEGIN_STMT					\
  const char *desc = "ASSERT_PRED1 (" #PRED1 ", " #VAL1 ")";	\
  bool actual = (PRED1) (VAL1);				\
  if (actual)						\
    ::selftest::pass (SELFTEST_LOCATION, desc);			\
  else							\
    ::selftest::fail (SELFTEST_LOCATION, desc);			\
  SELFTEST_END_STMT

#define SELFTEST_BEGIN_STMT do {
#define SELFTEST_END_STMT   } while (0)

#endif /* #if CHECKING_P */

#endif /* GCC_SELFTEST_H */