Lumiera  0.pre.03
»edit your freedom«
/Werk/devel/lumi/src/lib/test/test-helper.hpp

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

CHECK (result23 == "[-100..100]"_expect);
/*
TEST-HELPER.hpp - collection of functions supporting unit testing
Copyright (C) Lumiera.org
2009, Hermann Vosseler <Ichthyostega@web.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef LIB_TEST_TEST_HELPER_H
#define LIB_TEST_TEST_HELPER_H
#include "lib/symbol.hpp"
#include <boost/lexical_cast.hpp>
#include <typeinfo>
#include <cstdlib>
#include <limits>
#include <string>
#include <cmath>
namespace lib {
namespace test{
using lib::Literal;
using std::string;
using std::rand;
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 - F(1)) < limit;
}
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 // fixed exponent for subnormals
: 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
showSizeof (size_t siz, string name);
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 > // note:: force invocations with pointer to the first overload
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... EMPTY>
inline string
{
return " :.";
}
template<typename X, typename... XS>
inline string
showVariadicTypes (X const& x, XS const&... xs)
{
return " :---#"
+ boost::lexical_cast<string>(1 + sizeof...(xs))
+ " -- Type: " + util::typeStr(x)
+ " " + showRefKind<X>()
+ " Address* " + boost::lexical_cast<string>(&x)
+ "\n"
+ showVariadicTypes<XS...> (xs...);
}
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 { // helper for printing type diagnostics
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;
}
{
return lib::time::Time (500 * (rand() % 2), (rand() % 600) + 1);
}
string randStr (size_t len);
class ExpectString
: public std::string
{
using std::string::string;
template<typename X>
friend bool
operator== (X const& x, ExpectString const& expected)
{
std::string actual{util::StringConv<X>::invoke (x)};
return expected.verify (actual);
}
template<typename X>
friend bool
operator== (ExpectString const& expected, X const& x)
{
std::string actual{util::StringConv<X>::invoke (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;
};
}} // namespace lib::test
operator""_expect (const char* lit, size_t siz)
{
return lib::test::ExpectString{lit, siz};
}
/* === test helper macros === */
#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 /*LIB_TEST_TEST_HELPER_H*/