Lumiera  0.pre.03
»edit your freedom«
test-helper.hpp File Reference

Go to the source code of this file.

Description

A collection of frequently used helper functions to support unit testing.

Some are test data generators, some are diagnostics helpers to produce readable output. Some of these support meta programming to figure out the actual reference kind (value, lvalue, rvalue) of a template parameter instantiation. For GNU compatible compilers, we expose also the interface to the internal ABI for demangling type names.

Note
this header is included into a large number of tests.
See also
TestHelper_test
TestHelperDemangling_test

Definition in file test-helper.hpp.

#include "lib/symbol.hpp"
#include "lib/meta/trait.hpp"
#include "lib/time/timevalue.hpp"
#include "lib/test/transiently.hpp"
#include "lib/format-obj.hpp"
#include <boost/lexical_cast.hpp>
#include <typeinfo>
#include <cstdlib>
#include <limits>
#include <string>
#include <cmath>

Classes

class  ExpectString
 Helper to produce better diagnostic messages when comparing to an expected result string. More...
 
struct  TypeDebugger< X >
 Helper to show types involved in metaprogramming. More...
 
struct  TypeDiagnostics< X >
 
struct  TypeDiagnostics< const X * >
 
struct  TypeDiagnostics< const X *const >
 
struct  TypeDiagnostics< const X >
 
struct  TypeDiagnostics< X & >
 
struct  TypeDiagnostics< X && >
 
struct  TypeDiagnostics< X * >
 
struct  TypeDiagnostics< X *const * >
 
struct  TypeDiagnostics< X *const >
 
struct  TypeDiagnostics< X const & >
 
struct  TypeDiagnostics< X const && >
 

Macros

#define MARK_TEST_FUN   cout << "|" << endl << "| »"<<__FUNCTION__<<"«" <<endl;
 Macro to mark the current test function in STDOUT. More...
 
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
 Macro to verify that a statement indeed raises an exception. More...
 
#define VERIFY_FAIL(FAILURE_MSG, ERRONEOUS_STATEMENT)
 Macro to verify that a statement indeed raises a std::exception, which additionally contains some FAILURE_MSG in its description. More...
 

Functions

template<typename F , typename N >
constexpr meta::enable_if< std::is_floating_point< F >, bool > epsEQ (F val, N target, uint ulps=EPSILON_ULP)
 
lib::test::ExpectString operator""_expect (const char *lit, size_t siz)
 
string randStr (size_t len)
 create garbage string of given length More...
 
lib::time::Time randTime ()
 create a random but not insane Time value between 1s ... More...
 
template<typename F , typename N >
constexpr meta::enable_if< std::is_floating_point< F >, bool > roughEQ (F val, N target, F limit=ROUGH_PRECISION)
 
template<typename R >
string showRefKind ()
 helper to discern the kind of reference of the argument type
 
string showSizeof (size_t siz, string name)
 for printing sizeof(). More...
 
template<typename T >
string showSizeof (T const *obj=0, const char *name=0)
 for printing sizeof(), possibly figuring out the type name automatically More...
 
template<typename T >
meta::disable_if< std::is_pointer< T >, string > showSizeof (T const &obj, const char *name=0)
 
template<typename T >
string showSizeof (const char *name)
 
template<typename X >
string showType ()
 diagnostic type output, including const and similar adornments More...
 
template<typename... EMPTY>
string showVariadicTypes ()
 helper for investigating a variadic argument pack More...
 
template<typename X , typename... XS>
string showVariadicTypes (X const &x, XS const &... xs)
 
template<typename X >
void typeDebugger (X &&x)
 
template<typename F >
constexpr meta::enable_if< std::is_floating_point< F >, F > ulp (F val)
 

Variables

constexpr auto EPSILON_ULP = 5
 
constexpr auto ROUGH_PRECISION = pow (10, -3)
 

Namespaces

 lib
 Implementation namespace for support and library code.
 

Macro Definition Documentation

◆ VERIFY_ERROR

#define VERIFY_ERROR (   ERROR_ID,
  ERRONEOUS_STATEMENT 
)
Value:
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(); \
}
Helper to produce better diagnostic messages when comparing to an expected result string...
lumiera_err lumiera_error_peek(void)
Check current error state without clearing it Please avoid this function and use lumiera_error() if p...
Definition: error-state.c:142
Interface and Base definition for all Lumiera Exceptions.
Definition: error.hpp:71

Macro to verify that a statement indeed raises an exception.

If no exception is thrown, the #NOTREACHED macro will trigger an assertion failure. In case of exception, the lumiera_error state is checked, cleared and verified.

Definition at line 383 of file test-helper.hpp.

Referenced by CustomSharedPtr_test::check_ordering(), ScopePath_test::check_RefcountProtection(), EntryID_test::checkErasure(), TestHelper_test::checkThrowChecker(), TimeValue_test::checkTimeHash(), CreateAsset_test::createMedia(), DiffIgnoreChanges_test::fail_or_ignore(), DispatcherInterface_test::resolveModelPort(), LazyInit_test::verify_complexUsageWithCopy(), DiagnosticContext_test::verify_simpleAccess(), WLink_test::verify_standardUsage(), DependencyConfiguration_test::verify_SubclassSingleton(), and ItemWrapper_test::verifyFunctionResult().

◆ VERIFY_FAIL

#define VERIFY_FAIL (   FAILURE_MSG,
  ERRONEOUS_STATEMENT 
)
Value:
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); \
}

Macro to verify that a statement indeed raises a std::exception, which additionally contains some FAILURE_MSG in its description.

Definition at line 407 of file test-helper.hpp.

Referenced by DataCSV_test::demonnstrate_CSV_Notation().

◆ MARK_TEST_FUN

Function Documentation

◆ showSizeof() [1/2]

string showSizeof ( size_t  siz,
string  name 
)

for printing sizeof().

prints the given size and name literally, without any further magic

Examples:
/Werk/devel/lumi/src/lib/test/test-helper.hpp.

Definition at line 57 of file test-helper.cpp.

References lib::test::showSizeof().

Referenced by TestHelper_test::checkTypeDisplay(), and lib::test::showSizeof().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ showSizeof() [2/2]

string lib::test::showSizeof ( T const *  obj = 0,
const char *  name = 0 
)
inline

for printing sizeof(), possibly figuring out the type name automatically

Parameters
namewhen given, this name will be used for display, instead of auto detecting the type

Definition at line 115 of file test-helper.hpp.

◆ showVariadicTypes()

string lib::test::showVariadicTypes ( )
inline

helper for investigating a variadic argument pack

Warning
always spell out the template arguments explicitly when invoking this diagnostics, e.g. showVariadicTypes<ARGS...>(args...) otherwise the template argument matching for functions might mess up the kind of reference you'll see in the diagnostics.
See also
test-helper-variadic-test.cpp
Examples:
/Werk/devel/lumi/src/lib/test/test-helper.hpp.

Definition at line 157 of file test-helper.hpp.

Referenced by TestHelperVariadic_test::forwardFunction().

+ Here is the caller graph for this function:

◆ showType()

string lib::test::showType ( )
inline

diagnostic type output, including const and similar adornments

Warning
operates after-the-fact and relies on mangled type names plus several heuristics. Output might thus not be entirely correct, especially when several levels of const, pointer and references are involved. If in doubt, place the TypeDebugger<T> to reveal the type as the compiler sees it.
Remarks
the function lib::meta::typeStr removes adornments and does not work on all kinds of reference. This helper attempts to work around those limitations.
Examples:
/Werk/devel/lumi/src/lib/test/test-helper.hpp.

Definition at line 289 of file test-helper.hpp.

References lib::test::showType().

Referenced by lib::test::showType().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ randTime()

lib::time::Time lib::test::randTime ( )
inline

create a random but not insane Time value between 1s ...

10min + 500ms

Examples:
/Werk/devel/lumi/src/lib/test/test-helper.hpp.

Definition at line 304 of file test-helper.hpp.

References lib::test::randTime().

Referenced by lib::test::randTime().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ randStr()

string randStr ( size_t  len)

create garbage string of given length

Todo:
probably this can be done in a more clever way.

Anyone...?

Returns
string containing arbitrary lower case letters and numbers
Examples:
/Werk/devel/lumi/src/lib/test/test-helper.hpp.

Definition at line 69 of file test-helper.cpp.

References lib::test::randStr().

Referenced by TestHelper_test::checkTypeDisplay(), TestSource::nextResult(), and lib::test::randStr().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: