user defined literal for expected result strings.On equality comparison to any other string convertible object, the difference to this expected string is printed to STDERR
#ifndef LIB_TEST_TEST_HELPER_H
#define LIB_TEST_TEST_HELPER_H
#include <boost/lexical_cast.hpp>
#include <typeinfo>
#include <cstdlib>
#include <limits>
#include <string>
#include <cmath>
using std::string;
constexpr auto ROUGH_PRECISION = pow (10, -3);
constexpr auto EPSILON_ULP = 5;
template<typename F, typename N>
constexpr inline meta::enable_if< std::is_floating_point<F>,
bool >
roughEQ (F val, N target, F limit =ROUGH_PRECISION)
{
REQUIRE (0 < limit);
return abs (val - target) < limit * abs(target);
}
template<typename F>
constexpr inline meta::enable_if< std::is_floating_point<F>,
F >
ulp (F val)
{
val = fabs (val);
const int exp = val < std::numeric_limits<F>::min()
? std::numeric_limits<F>::min_exponent - 1
: std::ilogb (val);
auto scaledUlp = std::ldexp (std::numeric_limits<F>::epsilon(), exp);
ENSURE (F(0) < scaledUlp);
return scaledUlp;
}
template<typename F, typename N>
constexpr inline meta::enable_if< std::is_floating_point<F>,
bool >
epsEQ (F val, N target, uint ulps =EPSILON_ULP)
{
return abs (val - target) < ulps * ulp<F> (target);
}
string
template<typename T>
inline string
showSizeof (T const* obj =0, const char* name =0)
{
return showSizeof (obj? sizeof(*obj) : sizeof(T),
name? name :
util::typeStr(obj));
}
template<typename T>
inline meta::disable_if<std::is_pointer<T>,
string >
showSizeof (T const& obj, const char* name=0)
{
return showSizeof (&obj, name);
}
template<typename T>
inline string
showSizeof (const char* name)
{
return showSizeof<T> (
nullptr,
name);
}
template<typename R>
string
{
return std::is_lvalue_reference<R>::value? "REF"
: std::is_rvalue_reference<R>::value? "MOV"
: "VAL";
}
template<typename X>
struct TypeDebugger
{
static_assert (not sizeof(X), "### Type Debugging ###");
};
template<typename X>
void
typeDebugger(X&& x)
{
static_assert (not sizeof(X), "### Type Debugging ###");
}
namespace {
template<typename X>
struct TypeDiagnostics
{
using Type = X;
static constexpr auto prefix = "";
static constexpr auto postfix = "";
};
template<typename X>
struct TypeDiagnostics<const X>
{
using Type = X;
static constexpr auto prefix = "const ";
static constexpr auto postfix = "";
};
template<typename X>
struct TypeDiagnostics<X&>
{
using Type = X;
static constexpr auto prefix = "";
static constexpr auto postfix = "&";
};
template<typename X>
struct TypeDiagnostics<X&&>
{
using Type = X;
static constexpr auto prefix = "";
static constexpr auto postfix = " &&";
};
template<typename X>
struct TypeDiagnostics<X const&>
{
using Type = X;
static constexpr auto prefix = "";
static constexpr auto postfix = " const&";
};
template<typename X>
struct TypeDiagnostics<X const&&>
{
using Type = X;
static constexpr auto prefix = "const ";
static constexpr auto postfix = " &&";
};
template<typename X>
struct TypeDiagnostics<X *>
{
using Type = X;
static constexpr auto prefix = "";
static constexpr auto postfix = " *";
};
template<typename X>
struct TypeDiagnostics<const X *>
{
using Type = X;
static constexpr auto prefix = "const ";
static constexpr auto postfix = " *";
};
template<typename X>
struct TypeDiagnostics<const X * const>
{
using Type = X;
static constexpr auto prefix = "const ";
static constexpr auto postfix = " * const";
};
template<typename X>
struct TypeDiagnostics<X * const>
{
using Type = X;
static constexpr auto prefix = "";
static constexpr auto postfix = " * const";
};
template<typename X>
struct TypeDiagnostics<X * const *>
{
using Type = X;
static constexpr auto prefix = "";
static constexpr auto postfix = " * const *";
};
}
template<typename X>
inline string
{
using Case = TypeDiagnostics<X>;
using Type = typename Case::Type;
return Case::prefix
+
meta::humanReadableTypeID (
typeid(Type).
name())
+ Case::postfix;
}
template<typename...TS>
string
showTypes()
{
return "<| " + ((showType<TS>()+", ") + ... + "|>");
}
template<typename... EMPTY>
inline string
{
return " :.";
}
template<typename XX, typename... XS>
inline string
{
return " :---#"
+ " -- Type: " + showType<XX&&>()
+
" \tAdr" +
util::showAdr (x)
+ "\n"
}
{
}
class ExpectString
: public std::string
{
using std::string::string;
template<typename X>
friend bool
operator== (X
const& x, ExpectString
const& expected)
{
return expected.verify (actual);
}
template<typename X>
friend bool
operator== (ExpectString
const& expected, X
const& x)
{
return expected.verify (actual);
}
friend ExpectString
operator+ (std::string&& l, ExpectString&& r)
{
return ExpectString{(l+r).c_str()};
}
bool verify (std::string const& actual) const;
};
}}
operator""_expect (const char* lit, size_t siz)
{
}
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT) \
try \
{ \
ERRONEOUS_STATEMENT ; \
NOTREACHED("expected »%s« failure in: %s", \
#ERROR_ID, #ERRONEOUS_STATEMENT); \
} \
catch (lumiera::Error& ex) \
{ \
CHECK (ex.getID() \
== lib::test::ExpectString{LUMIERA_ERROR_##ERROR_ID} );\
lumiera_error(); \
} \
catch (...) \
{ \
CHECK (lumiera_error_peek() \
== lib::test::ExpectString{LUMIERA_ERROR_##ERROR_ID} ); \
lumiera_error(); \
}
#define VERIFY_FAIL(FAILURE_MSG, ERRONEOUS_STATEMENT) \
try \
{ \
ERRONEOUS_STATEMENT ; \
NOTREACHED("expected »%s«-failure in: %s" \
, FAILURE_MSG, #ERRONEOUS_STATEMENT);\
} \
catch (std::exception& sex) \
{ \
CHECK (util::contains (sex.what(), FAILURE_MSG) \
,"expected failure with »%s« -- but got: %s" \
,FAILURE_MSG, sex.what()); \
lumiera_error(); \
} \
catch (...) \
{ \
NOTREACHED("expected »%s«-failure, " \
"yet something scary happened instead...", \
FAILURE_MSG); \
}
#define MARK_TEST_FUN \
cout << "|" << endl << "| »"<<__FUNCTION__<<"«" <<endl;
#endif