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
|
//-----------------------------------------------------------------------------
/** @file libboardgame_test/Test.h
Provides functionality similar to Boost.Test.
@author Markus Enzenberger
@copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------
#ifndef LIBBOARDGAME_TEST_TEST_H
#define LIBBOARDGAME_TEST_TEST_H
#include <cmath>
#include <functional>
#include <sstream>
#include <stdexcept>
#include <string>
namespace libboardgame_test {
using namespace std;
//-----------------------------------------------------------------------------
using TestFunction = function<void()>;
//-----------------------------------------------------------------------------
class TestFail
: public logic_error
{
public:
TestFail(const char* file, int line, const string& s);
};
//-----------------------------------------------------------------------------
void add_test(const string& name, const TestFunction& function);
bool run_all_tests();
bool run_test(const string& name);
//-----------------------------------------------------------------------------
/** Helper class that automatically adds a test when an instance is
declared. */
struct TestRegistrar
{
TestRegistrar(const string& name, const TestFunction& function)
{
add_test(name, function);
}
};
//-----------------------------------------------------------------------------
} // namespace libboardgame_test
//-----------------------------------------------------------------------------
#define LIBBOARDGAME_TEST_CASE(name) \
static void name(); \
static libboardgame_test::TestRegistrar name##_registrar(#name, name); \
void name()
#define LIBBOARDGAME_CHECK(expr) \
if (! (expr)) \
throw libboardgame_test::TestFail(__FILE__, __LINE__, "check failed");
#define LIBBOARDGAME_CHECK_EQUAL(expr1, expr2) \
{ \
using libboardgame_test::TestFail; \
auto result1 = (expr1); \
auto result2 = (expr2); \
if (result1 != result2) \
{ \
ostringstream msg; \
msg << "'" << result1 << "' != '" << result2 << "'"; \
throw TestFail(__FILE__, __LINE__, msg.str()); \
} \
}
#define LIBBOARDGAME_CHECK_THROW(expr, exception) \
{ \
using libboardgame_test::TestFail; \
bool was_thrown = false; \
try \
{ \
expr; \
} \
catch (const exception&) \
{ \
was_thrown = true; \
} \
if (! was_thrown) \
{ \
ostringstream msg; \
msg << "Exception '" << #exception << "' was not thrown"; \
throw TestFail(__FILE__, __LINE__, msg.str()); \
} \
}
#define LIBBOARDGAME_CHECK_NO_THROW(expr) \
{ \
using libboardgame_test::TestFail; \
try \
{ \
expr; \
} \
catch (...) \
{ \
throw TestFail(__FILE__, __LINE__, \
"Unexpected exception was thrown"); \
} \
}
/** Compare floating points using a tolerance in percent. */
#define LIBBOARDGAME_CHECK_CLOSE(expr1, expr2, tolerance) \
{ \
using libboardgame_test::TestFail; \
auto result1 = (expr1); \
auto result2 = (expr2); \
if (fabs(result1 - result2) > (tolerance) * result1 / 100) \
{ \
ostringstream msg; \
msg << "Difference between " << result1 << " and " \
<< result2 << " exceeds " << ((tolerance) / 100 ) \
<< " percent"; \
throw TestFail(__FILE__, __LINE__, msg.str()); \
} \
}
/** Compare floating points using an epsilon. */
#define LIBBOARDGAME_CHECK_CLOSE_EPS(expr1, expr2, epsilon) \
{ \
using libboardgame_test::TestFail; \
auto result1 = (expr1); \
auto result2 = (expr2); \
if (fabs(result1 - result2) > (epsilon)) \
{ \
ostringstream msg; \
msg << "Difference between " << result1 << " and " \
<< result2 << " exceeds " << (epsilon); \
throw TestFail(__FILE__, __LINE__, msg.str()); \
} \
}
//-----------------------------------------------------------------------------
#endif // LIBBOARDGAME_TEST_TEST_H
|