Branch data Line data Source code
1 : : /* testsuite.h: a generic test suite engine
2 : : *
3 : : * Copyright 1999,2000,2001 BrightStation PLC
4 : : * Copyright 2002,2003,2005,2006,2007,2008,2009 Olly Betts
5 : : *
6 : : * This program is free software; you can redistribute it and/or
7 : : * modify it under the terms of the GNU General Public License as
8 : : * published by the Free Software Foundation; either version 2 of the
9 : : * License, or (at your option) any later version.
10 : : *
11 : : * This program is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : * GNU General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU General Public License
17 : : * along with this program; if not, write to the Free Software
18 : : * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 : : * USA
20 : : */
21 : :
22 : : #ifndef OM_HGUARD_TESTSUITE_H
23 : : #define OM_HGUARD_TESTSUITE_H
24 : :
25 : : #include "noreturn.h"
26 : :
27 : : #include "output.h"
28 : :
29 : : #include "stringutils.h" // For STRINGIZE().
30 : :
31 : : #include <iomanip>
32 : : #include <map>
33 : : #include <sstream>
34 : : #include <string>
35 : : #include <vector>
36 : :
37 : : #include <cfloat> // For DBL_DIG.
38 : :
39 : : /** Class which is thrown when a test case fails.
40 : : */
41 : : class TestFail { };
42 : :
43 : : /** Class which is thrown when a test case is to be skipped.
44 : : *
45 : : * This happens when something can't be tested for some reason, but
46 : : * that reason isn't grounds for causing the test to fail.
47 : : */
48 : : class TestSkip { };
49 : :
50 : : /** Macro used to build a TestFail object and throw it.
51 : : */
52 : : // Don't bracket a, because it may have <<'s in it
53 : : #define FAIL_TEST(a) do { TestFail testfail; \
54 : : if (verbose) { tout << a << '\n'; } \
55 : : throw testfail; } while (0)
56 : :
57 : : /** Macro used to build a TestSkip object and throw it.
58 : : */
59 : : // Don't bracket a, because it may have <<'s in it
60 : : #define SKIP_TEST(a) do { TestSkip testskip; \
61 : : if (verbose) { tout << a << '\n'; } \
62 : : throw testskip; } while (0)
63 : :
64 : : /// Type for a test function.
65 : : typedef bool (*test_func)();
66 : :
67 : : /// Structure holding a description of a test.
68 : : struct test_desc {
69 : : /// The name of the test.
70 : : const char *name;
71 : :
72 : : /// The function to run to perform the test.
73 : : test_func run;
74 : : };
75 : :
76 : : /// The global verbose flag.
77 : : //
78 : : // If verbose is set, then the test harness will display diagnostic output
79 : : // for tests which fail or skip. Individual tests may use this flag to avoid
80 : : // needless generation of diagnostic output in cases when it's expensive.
81 : : extern bool verbose;
82 : :
83 : : /// The exception type we were expecting in TEST_EXCEPTION.
84 : : // Used to detect if such an exception was mishandled by the
85 : : // compiler/runtime.
86 : : extern const char * expected_exception;
87 : :
88 : : /** The output stream. Data written to this stream will only appear
89 : : * when a test fails.
90 : : */
91 : : extern std::ostringstream tout;
92 : :
93 : : /// The test driver. This class takes care of running the tests.
94 : 217 : class test_driver {
95 : : /// Write out anything in tout and clear it.
96 : : void write_and_clear_tout();
97 : :
98 : : public:
99 : : /** A structure used to report the summary of tests passed
100 : : * and failed.
101 : : */
102 : : struct result {
103 : : /// The number of tests which succeeded.
104 : : unsigned int succeeded;
105 : :
106 : : /// The number of tests which failed.
107 : : unsigned int failed;
108 : :
109 : : /// The number of tests which were skipped
110 : : unsigned int skipped;
111 : :
112 : 444 : result() : succeeded(0), failed(0), skipped(0) { }
113 : :
114 : 22 : result & operator+=(const result & o) {
115 : 22 : succeeded += o.succeeded;
116 : 22 : failed += o.failed;
117 : 22 : skipped += o.skipped;
118 : 22 : return *this;
119 : : }
120 : :
121 : : void reset() {
122 : : succeeded = 0;
123 : : failed = 0;
124 : : skipped = 0;
125 : : }
126 : : };
127 : :
128 : : /** Add a test-specific command line option.
129 : : *
130 : : * The recognised option will be described as:
131 : : *
132 : : * -<s> <l>
133 : : *
134 : : * And any value set will be put into arg.
135 : : */
136 : : static void add_command_line_option(const std::string &l, char s,
137 : : std::string * arg);
138 : :
139 : : /** Parse the command line arguments.
140 : : *
141 : : * @param argc The argument count passed into ::main()
142 : : * @param argv The argument list passed into ::main()
143 : : */
144 : : static void parse_command_line(int argc, char **argv);
145 : :
146 : : XAPIAN_NORETURN(static void usage());
147 : :
148 : : static int run(const test_desc *tests);
149 : :
150 : : /** The constructor, which sets up the test driver.
151 : : *
152 : : * @param tests The zero-terminated array of tests to run.
153 : : */
154 : : test_driver(const test_desc *tests_);
155 : :
156 : : /** Run all the tests supplied and return the results
157 : : */
158 : : result run_tests();
159 : :
160 : : /** Run the tests in the list and return the results
161 : : */
162 : : result run_tests(std::vector<std::string>::const_iterator b,
163 : : std::vector<std::string>::const_iterator e);
164 : :
165 : : /** Read srcdir from environment and if not present, make a valiant
166 : : * attempt to guess a value
167 : : */
168 : : static std::string get_srcdir();
169 : :
170 : : // Running subtotal for current backend.
171 : : static result subtotal;
172 : :
173 : : // Running total for the whole test run.
174 : : static result total;
175 : :
176 : : /// Print summary of tests passed, failed, and skipped.
177 : : static void report(const test_driver::result &r, const std::string &desc);
178 : :
179 : : private:
180 : : /** Prevent copying */
181 : : test_driver(const test_driver &);
182 : : test_driver & operator = (const test_driver &);
183 : :
184 : : typedef enum { PASS = 1, FAIL = 0, SKIP = -1 } test_result;
185 : :
186 : : static std::map<int, std::string *> short_opts;
187 : :
188 : : static std::string opt_help;
189 : :
190 : : static std::vector<std::string> test_names;
191 : :
192 : : /** Runs the test function and returns its result. It will
193 : : * also trap exceptions and some memory leaks and force a
194 : : * failure in those cases.
195 : : *
196 : : * @param test A description of the test to run.
197 : : */
198 : : test_result runtest(const test_desc *test);
199 : :
200 : : /** The implementation used by run_tests.
201 : : * it runs test(s) (with runtest()), prints out messages for
202 : : * the user, and tracks the successes and failures.
203 : : *
204 : : * @param b, e If b != e, a vector of the test(s) to run.
205 : : * If b == e, all tests will be run.
206 : : */
207 : : result do_run_tests(std::vector<std::string>::const_iterator b,
208 : : std::vector<std::string>::const_iterator e);
209 : :
210 : : // abort tests at the first failure
211 : : static bool abort_on_error;
212 : :
213 : : // the default stream to output to
214 : : std::ostream out;
215 : :
216 : : // the list of tests to run.
217 : : const test_desc *tests;
218 : :
219 : : // how many test runs we've done - no summary if just one run
220 : : static int runs;
221 : :
222 : : // program name
223 : : static std::string argv0;
224 : :
225 : : // strings to use for colouring - empty if output isn't a tty
226 : : static std::string col_red, col_green, col_yellow, col_reset;
227 : :
228 : : // use \r to not advance a line when a test passes (this only
229 : : // really makes sense if the output is a tty)
230 : : static bool use_cr;
231 : : };
232 : :
233 : : /// Display the location at which a testcase occurred, with an explanation.
234 : : #define TESTCASE_LOCN(a) __FILE__":"STRINGIZE(__LINE__)": "STRINGIZE(a)
235 : :
236 : : /** Test a condition, and display the test with an extra explanation if
237 : : * the condition fails.
238 : : * NB: wrapped in do { ... } while (0) so a trailing ';' works correctly.
239 : : */
240 : : #define TEST_AND_EXPLAIN(a, b) do {\
241 : : if (!(a)) FAIL_TEST(TESTCASE_LOCN(a) << std::endl << b << std::endl);\
242 : : } while (0)
243 : :
244 : : /// Test a condition, without an additional explanation for failure.
245 : : #define TEST(a) TEST_AND_EXPLAIN(a, "")
246 : :
247 : : /// Test for equality of two things.
248 : : #define TEST_EQUAL(a, b) TEST_AND_EXPLAIN(((a) == (b)), \
249 : : "Expected `"STRINGIZE(a)"' and `"STRINGIZE(b)"' to be equal:" \
250 : : " were " << (a) << " and " << (b))
251 : :
252 : : /** Test for equality of two strings.
253 : : *
254 : : * If they aren't equal, show each on a separate line so the difference can
255 : : * be seen clearly.
256 : : */
257 : : #define TEST_STRINGS_EQUAL(a, b) TEST_AND_EXPLAIN(((a) == (b)), \
258 : : "Expected "STRINGIZE(a)" and "STRINGIZE(b)" to be equal, were:\n\"" \
259 : : << (a) << "\"\n\"" << (b) << '"')
260 : :
261 : : /// Helper function for TEST_EQUAL_DOUBLE macro.
262 : : extern bool TEST_EQUAL_DOUBLE_(double a, double b);
263 : :
264 : : /// Test two doubles for near equality.
265 : : #define TEST_EQUAL_DOUBLE(a, b) TEST_AND_EXPLAIN(TEST_EQUAL_DOUBLE_((a), (b)), \
266 : : "Expected `"STRINGIZE(a)"' and `"STRINGIZE(b)"' to be (nearly) equal:" \
267 : : " were " << setprecision(DBL_DIG) << (a) << " and " << (b) << ")" << setprecision(6))
268 : :
269 : : /// Test two doubles for non-near-equality.
270 : : #define TEST_NOT_EQUAL_DOUBLE(a, b) TEST_AND_EXPLAIN(!TEST_EQUAL_DOUBLE_((a), (b)), \
271 : : "Expected `"STRINGIZE(a)"' and `"STRINGIZE(b)"' not to be (nearly) equal:" \
272 : : " were " << setprecision(DBL_DIG) << (a) << " and " << (b) << ")" << setprecision(6))
273 : :
274 : : /// Test for non-equality of two things.
275 : : #define TEST_NOT_EQUAL(a, b) TEST_AND_EXPLAIN(((a) != (b)), \
276 : : "Expected `"STRINGIZE(a)"' and `"STRINGIZE(b)"' not to be equal:" \
277 : : " were " << (a) << " and " << (b))
278 : :
279 : : #define DEFINE_TESTCASE(S,COND) bool test_##S()
280 : :
281 : : // Newer test macros:
282 : : #include "testmacros.h"
283 : :
284 : : #endif // OM_HGUARD_TESTSUITE_H
|