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 "lib/random.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... TS>
string showTypes ()
 
template<typename... EMPTY>
string showVariadicTypes ()
 helper for investigating a variadic argument pack More...
 
template<typename XX , typename... XS>
string showVariadicTypes (XX &&x, XS &&... 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:133
Interface and Base definition for all Lumiera Exceptions.
Definition: error.hpp:62

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 387 of file test-helper.hpp.

Referenced by CustomSharedPtr_test::check_ordering(), ScopePath_test::check_RefcountProtection(), IterCoreAdapter_test::checked_and_protected(), EntryID_test::checkErasure(), TestHelper_test::checkThrowChecker(), TimeValue_test::checkTimeHash(), CreateAsset_test::createMedia(), DiffIgnoreChanges_test::fail_or_ignore(), DispatcherInterface_test::resolveModelPort(), IterZip_test::simpleUsage(), IterCoreAdapter_test::value_and_reference_yield(), 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 411 of file test-helper.hpp.

Referenced by DataCSV_test::demonnstrate_CSV_Notation().

◆ MARK_TEST_FUN

#define MARK_TEST_FUN   cout << "|" << endl << "| »"<<__FUNCTION__<<"«" <<endl;

Macro to mark the current test function in STDOUT.

This can be helpful to digest a long test output dump

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

Referenced by BusTerm_test::attachNewBusTerm(), BusTerm_test::captureStateMark(), BusTerm_test::clearStates(), BusTerm_test::commandInvocation(), SchedulerCommutator_test::demonstrateSimpleUsage(), SchedulerCommutator_test::integratedWorkCycle(), SchedulerStress_test::investigateWorkProcessing(), AbstractTangible_test::markState(), AbstractTangible_test::mutate(), TreeMutatorBinding_test::mutateAttribute(), TreeMutatorBinding_test::mutateCollection(), TreeMutatorBinding_test::mutateDummy(), TreeMutatorBinding_test::mutateGenNode(), AbstractTangible_test::notify(), SchedulerService_test::processSchedule(), BusTerm_test::pushDiff(), BusTerm_test::replayStateMark(), AbstractTangible_test::revealer(), SchedulerStress_test::search_breaking_point(), SchedulerStress_test::setup_systematicSchedule(), SchedulerStress_test::smokeTest(), SchedulerCommutator_test::torture_GroomingToken(), SchedulerCommutator_test::verify_dispatch(), SchedulerCommutator_test::verify_findWork(), SchedulerCommutator_test::verify_GroomingGuard(), SchedulerCommutator_test::verify_GroomingToken(), SchedulerStress_test::verify_instrumentation(), AbstractTangible_test::verify_mockManipulation(), SchedulerCommutator_test::verify_postChain(), SchedulerCommutator_test::verify_Significance(), BusTerm_test::verifyNotifications(), and SchedulerStress_test::watch_expenseFunction().

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 49 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 107 of file test-helper.hpp.

◆ 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 256 of file test-helper.hpp.

References lib::test::showType().

Referenced by lib::test::showType(), IterCoreAdapter_test::value_and_reference_yield(), HeteroData_test::verify_ChainBlock(), and HeteroData_test::verify_FrontBlock().

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

◆ showVariadicTypes()

string lib::test::showVariadicTypes ( )
inline

helper for investigating a variadic argument pack

Remarks
you can pass arguments directly to this function, but be sure to use std::forward<ARGS>(args)... when forwarding universal references received from a variadic argument pack ARG.
Note
due to reference collapsing it is not possible to distinguish receiving a value from receiving a rvalue-reference.
See also
test-helper-variadic-test.cpp
Examples:
/Werk/devel/lumi/src/lib/test/test-helper.hpp.

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

Referenced by TestHelperVariadic_test::forwardFunction().

+ 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 308 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 61 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: