23 #ifndef XAPIAN_INCLUDED_TESTSUITE_H
24 #define XAPIAN_INCLUDED_TESTSUITE_H
26 #ifndef XAPIAN_UNITTEST
28 # define UNITTEST_CHECK_EXCEPTION
53 #define THROW_TEST_(EXCEPTION, MSG) \
56 tout << __FILE__ ":" STRINGIZE(__LINE__) ": " << MSG << '\n'; \
65 #define FAIL_TEST(MSG) THROW_TEST_(TestFail, MSG)
71 #define SKIP_TEST(MSG) THROW_TEST_(TestSkip, MSG)
102 extern std::ostringstream
tout;
188 result
run_tests(std::vector<std::string>::const_iterator b,
189 std::vector<std::string>::const_iterator e);
236 std::vector<std::string>::const_iterator e);
265 #define TEST_AND_EXPLAIN(a, b) do {\
266 bool test_and_explain_fail_ = !(a);\
267 UNITTEST_CHECK_EXCEPTION\
268 if (test_and_explain_fail_)\
269 FAIL_TEST(STRINGIZE(a) << '\n' << b << '\n');\
273 #define TEST(a) TEST_AND_EXPLAIN(a, "")
276 #define TEST_EQUAL(a, b) TEST_AND_EXPLAIN(((a) == (b)), \
277 "Expected '" STRINGIZE(a) "' and '" STRINGIZE(b) "' to be equal:" \
278 " were " << (a) << " and " << (b))
285 #define TEST_STRINGS_EQUAL(a, b) TEST_AND_EXPLAIN(((a) == (b)), \
286 "Expected " STRINGIZE(a) " and " STRINGIZE(b) " to be equal, were:\n\"" \
287 << (a) << "\"\n\"" << (b) << '"')
290 extern bool TEST_EQUAL_DOUBLE_(double a, double b);
293 #define TEST_EQUAL_DOUBLE(a, b) TEST_AND_EXPLAIN(TEST_EQUAL_DOUBLE_((a), (b)), \
294 "Expected '" STRINGIZE(a) "' and '" STRINGIZE(b) "' to be (nearly) equal:" \
295 " were " << setprecision(DBL_DIG) << (a) << " and " << (b) << ")" << setprecision(6))
298 #define TEST_NOT_EQUAL_DOUBLE(a, b) TEST_AND_EXPLAIN(!TEST_EQUAL_DOUBLE_((a), (b)), \
299 "Expected '" STRINGIZE(a) "' and '" STRINGIZE(b) "' not to be (nearly) equal:" \
300 " were " << setprecision(DBL_DIG) << (a) << " and " << (b) << ")" << setprecision(6))
303 #define TEST_NOT_EQUAL(a, b) TEST_AND_EXPLAIN(((a) != (b)), \
304 "Expected '" STRINGIZE(a) "' and '" STRINGIZE(b) "' not to be equal:" \
305 " were " << (a) << " and " << (b))
307 // Newer test macros:
308 #include "testmacros.h"
328 inline void XFAIL(const char* msg) {
329 expected_failure = msg;
332 #endif // XAPIAN_INCLUDED_TESTSUITE_H
Class which is thrown when a test case fails.
Class which is thrown when a test case is to be skipped.
The test driver. This class takes care of running the tests.
static std::string col_yellow
test_driver & operator=(const test_driver &)
static bool abort_on_error
static void report(const test_driver::result &r, const std::string &desc)
Print summary of tests passed, failed, and skipped.
static std::string col_green
static std::vector< std::string > test_names
test_result runtest(const test_desc *test)
Runs the test function and returns its result.
static std::string col_red
static std::string col_reset
static std::string opt_help
result run_tests()
Run all the tests supplied and return the results.
static void parse_command_line(int argc, char **argv)
Parse the command line arguments.
static std::string get_srcdir()
Read srcdir from environment and if not present, make a valiant attempt to guess a value.
test_driver(const test_desc *tests_)
The constructor, which sets up the test driver.
static int run(const test_desc *tests)
static void add_command_line_option(const std::string &l, char s, std::string *arg)
Add a test-specific command line option.
result do_run_tests(std::vector< std::string >::const_iterator b, std::vector< std::string >::const_iterator e)
The implementation used by run_tests.
static std::map< int, std::string * > short_opts
void write_and_clear_tout()
Write out anything in tout and clear it.
test_driver(const test_driver &)
Prevent copying.
std::ostream operator<< template for Xapian objects
Various handy string-related helpers.
Structure holding a description of a test.
const char * name
The name of the test.
void(* run)()
The function to run to perform the test.
A structure used to report the summary of tests passed and failed.
unsigned int failed
The number of tests which failed.
unsigned int skipped
The number of tests which were skipped.
unsigned int xfailed
Number of tests with result XFAIL.
unsigned int xpassed
Number of tests with result XFAIL.
result & operator+=(const result &o)
unsigned int succeeded
The number of tests which succeeded.
int verbose
The global verbose flag.
const char * expected_failure
Set to a string explanation for testcases expected to fail.
const char * expected_exception
The exception type we were expecting in TEST_EXCEPTION.
std::ostringstream tout
The output stream.