24 #ifndef OM_HGUARD_TESTSUITE_H
25 #define OM_HGUARD_TESTSUITE_H
29 #ifndef XAPIAN_UNITTEST
31 # define UNITTEST_CHECK_EXCEPTION
56 #define THROW_TEST_(EXCEPTION, MSG) \
59 tout << __FILE__ ":" STRINGIZE(__LINE__) ": " << MSG << '\n'; \
68 #define FAIL_TEST(MSG) THROW_TEST_(TestFail, MSG)
74 #define SKIP_TEST(MSG) THROW_TEST_(TestSkip, MSG)
105 extern std::ostringstream
tout;
174 XAPIAN_NORETURN(
static void usage());
190 result
run_tests(std::vector<std::string>::const_iterator b,
191 std::vector<std::string>::const_iterator e);
238 std::vector<std::string>::const_iterator e);
267 #define TEST_AND_EXPLAIN(a, b) do {\
268 bool test_and_explain_fail_ = !(a);\
269 UNITTEST_CHECK_EXCEPTION\
270 if (test_and_explain_fail_)\
271 FAIL_TEST(STRINGIZE(a) << '\n' << b << '\n');\
275 #define TEST(a) TEST_AND_EXPLAIN(a, "")
278 #define TEST_EQUAL(a, b) TEST_AND_EXPLAIN(((a) == (b)), \
279 "Expected '" STRINGIZE(a) "' and '" STRINGIZE(b) "' to be equal:" \
280 " were " << (a) << " and " << (b))
287 #define TEST_STRINGS_EQUAL(a, b) TEST_AND_EXPLAIN(((a) == (b)), \
288 "Expected " STRINGIZE(a) " and " STRINGIZE(b) " to be equal, were:\n\"" \
289 << (a) << "\"\n\"" << (b) << '"')
292 extern bool TEST_EQUAL_DOUBLE_(double a, double b);
295 #define TEST_EQUAL_DOUBLE(a, b) TEST_AND_EXPLAIN(TEST_EQUAL_DOUBLE_((a), (b)), \
296 "Expected '" STRINGIZE(a) "' and '" STRINGIZE(b) "' to be (nearly) equal:" \
297 " were " << setprecision(DBL_DIG) << (a) << " and " << (b) << ")" << setprecision(6))
300 #define TEST_NOT_EQUAL_DOUBLE(a, b) TEST_AND_EXPLAIN(!TEST_EQUAL_DOUBLE_((a), (b)), \
301 "Expected '" STRINGIZE(a) "' and '" STRINGIZE(b) "' not to be (nearly) equal:" \
302 " were " << setprecision(DBL_DIG) << (a) << " and " << (b) << ")" << setprecision(6))
305 #define TEST_NOT_EQUAL(a, b) TEST_AND_EXPLAIN(((a) != (b)), \
306 "Expected '" STRINGIZE(a) "' and '" STRINGIZE(b) "' not to be equal:" \
307 " were " << (a) << " and " << (b))
309 #define DEFINE_TESTCASE(S,COND) void test_##S()
311 // Newer test macros:
312 #include "testmacros.h"
332 inline void XFAIL(const char* msg) {
333 expected_failure = msg;
336 #endif // OM_HGUARD_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.
Define the XAPIAN_NORETURN macro.
std::ostream operator<< template for Xapian objects
Various handy helpers which std::string really should provide.
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.