unit-test for tuple initialisation from GenNode

- leave out the type conversion part
- instead verify error handling on some typical corner cases
This commit is contained in:
Fischlurch 2016-01-28 22:39:38 +01:00
parent f2cbac14e2
commit fc193da1ac
2 changed files with 81 additions and 93 deletions

View file

@ -569,7 +569,11 @@ return: 0
END
PLANNED "tuple initialisation from GenNode" TupleRecordInit_test <<END
TEST "tuple initialisation from GenNode" TupleRecordInit_test <<END
out: Rec...GenNode.+DataCap.«string».lalü, GenNode.+DataCap.«int».42
out: Rec...GenNode.+DataCap.«string».lalü, GenNode.+DataCap.«string».lala, GenNode.+DataCap.«int».12, GenNode.+DataCap.«int».34, GenNode.+DataCap.«double».5.6, GenNode.+DataCap.«Time».0:09:08.007
out-lit: «tuple<string, int>»──(lalü,42)
out-lit: «tuple<idi::EntryID<long>, string, int, long, double, TimeVar>»──(ID<long>-lal,lala,12,34,5.6,548s7ms)
return: 0
END

View file

@ -19,24 +19,12 @@
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* *****************************************************/
///@file
/** @file tuple-record-init-test.cpp
** Metaprogramming: how to unload the contents of a runtime typed variant sequence
** into ctor arguments of a (compile time typed) tuple. This involves two problems
** - how to combine iteration, compile-time indexing and run-time access.
** - how to overcome the runtime-to-compiletime barrier, using a pre-generated
** double-dispatch (visitor).
**
** The concrete problem prompting this research is the necessity to receive
** a command invocation parameter tuple from a Record<GenNode>
**
*/
typedef unsigned int uint;
#include "lib/test/run.hpp"
#include "lib/symbol.hpp"
#include "lib/test/test-helper.hpp"
#include "lib/time/timevalue.hpp"
#include "lib/meta/tuple-record-init.hpp"
#include "lib/format-cout.hpp"
@ -57,94 +45,90 @@ using lib::meta::buildTuple;
using lib::time::TimeVar;
using lib::time::Time;
using lumiera::error::LUMIERA_ERROR_INDEX_BOUNDS;
using lumiera::error::LUMIERA_ERROR_WRONG_TYPE;
using std::string;
using std::tuple;
using std::get;
namespace lib {
namespace meta {
namespace test {
////////////////////////TODO reworked for function-closure.hpp
template<typename TYPES, typename ARGS, size_t start>
struct SomeArgs
{
template<class SRC, class TAR, size_t i>
using IdxSelector = typename lib::meta::func::PartiallyInitTuple<SRC, TAR, start>::template IndexMapper<i>;
static Tuple<TYPES>
doIt (Tuple<ARGS> const& args)
{
return lib::meta::TupleConstructor<TYPES, IdxSelector> (args);
}
};
////////////////////////TODO reworked for function-closure.hpp
#define SHOW_TYPE(_TY_) \
cout << "typeof( " << STRINGIFY(_TY_) << " )= " << lib::meta::typeStr<_TY_>() <<endl;
#define EVAL_PREDICATE(_PRED_) \
cout << STRINGIFY(_PRED_) << "\t : " << _PRED_ <<endl;
void
verifyConversions()
{
using lib::meta::GenNodeAccessor;
using std::is_arithmetic;
using std::is_floating_point;
using lib::meta::is_nonFloat;
using lib::hash::LuidH;
EVAL_PREDICATE(is_arithmetic<int> ::value)
EVAL_PREDICATE(is_arithmetic<size_t> ::value)
EVAL_PREDICATE(is_floating_point<size_t>::value)
EVAL_PREDICATE(is_nonFloat<size_t> ::value)
EVAL_PREDICATE(GenNodeAccessor<int> ::allow_Conversion<size_t> ::value)
EVAL_PREDICATE(GenNodeAccessor<int64_t>::allow_Conversion<long int>::value)
EVAL_PREDICATE(GenNodeAccessor<double>::allow_Conversion<int64_t>::value)
EVAL_PREDICATE(GenNodeAccessor<LuidH>::allow_Conversion<int64_t> ::value)
EVAL_PREDICATE(GenNodeAccessor<LuidH>::allow_Conversion<int16_t> ::value)
EVAL_PREDICATE(GenNodeAccessor<LuidH>::allow_Conversion<uint16_t>::value)
EVAL_PREDICATE(GenNodeAccessor<LuidH> ::allow_Conversion<LuidH> ::value)
EVAL_PREDICATE(GenNodeAccessor<int64_t> ::allow_Conversion<LuidH>::value)
EVAL_PREDICATE(GenNodeAccessor<uint64_t>::allow_Conversion<LuidH>::value)
EVAL_PREDICATE(GenNodeAccessor<uint32_t>::allow_Conversion<LuidH>::value)
EVAL_PREDICATE(GenNodeAccessor<int32_t> ::allow_Conversion<LuidH>::value)
cout <<endl<<endl;
}
/*************************************************************************//**
* @test Metaprogramming: how to unload the contents of a runtime typed
* variant sequence into ctor arguments of a (compile time typed) tuple.
*
* This involves two problems
* - how to combine iteration, compile-time indexing and run-time access.
* - how to overcome the runtime-to-compile-time barrier, using a
* pre-generated double-dispatch (visitor).
*
* The concrete problem leading to the development of such a generic
* converter was the necessity to receive a command invocation
* parameter tuple from a Record<GenNode> sent via the UI-Bus.
* @see ElementExtractor
* @see GenNodeAccessor
* @see BusTerm_test::commandInvocation
* @see gui::test::Nexus::prepareDiagnosticCommandHandler
* @see ui-bus.hpp UI-Bus
*/
class TupleRecordInit_test : public Test
{
virtual void
run (Arg)
{
verifyConversions();
using NiceTypes = Types<string, int>;
using UgglyTypes = Types<EntryID<long>, string, int, int64_t, double, TimeVar>;
Rec args = MakeRec().scope("lalü", 42);
Rec urgs = MakeRec().scope("lalü", "lala", 12, 34, 5.6, Time(7,8,9));
cout << args <<endl;
cout << urgs <<endl;
cout << buildTuple<NiceTypes> (args) <<endl;
cout << buildTuple<UgglyTypes> (urgs) <<endl;
cout << SomeArgs<UgglyTypes,NiceTypes,1>::doIt(std::make_tuple("hui", 88)) <<endl;
cout << "\n.gulp.\n";
}
{
show_simpleUsage();
verify_errorHandling();
}
void
show_simpleUsage()
{
using NiceTypes = Types<string, int>;
using UgglyTypes = Types<EntryID<long>, string, int, int64_t, double, TimeVar>;
Rec args = MakeRec().scope("lalü", 42);
Rec urgs = MakeRec().scope("lalü", "lala", 12, 34, 5.6, Time(7,8,9));
cout << args <<endl;
cout << urgs <<endl;
cout << buildTuple<NiceTypes> (args) <<endl;
cout << buildTuple<UgglyTypes> (urgs) <<endl;
}
void
verify_errorHandling()
{
Rec args = MakeRec().scope("surprise", 42);
using TooMany = Types<string, int, long>;
VERIFY_ERROR (INDEX_BOUNDS, buildTuple<TooMany> (args)); // number of types in tuple exceeds capacity of the supplied argument record
using Unsigned = Types<string, uint>;
using Floating = Types<string, float>;
using Narrowing = Types<string, short>;
VERIFY_ERROR (WRONG_TYPE, buildTuple<Unsigned> (args)); // dangerous conversion from signed to unsigned int is prohibited
VERIFY_ERROR (WRONG_TYPE, buildTuple<Floating> (args)); // conversion from integral to floating point element is prohibited
VERIFY_ERROR (WRONG_TYPE, buildTuple<Narrowing> (args)); // narrowing conversion from int to short is prohibited
// yet other (non-numeric) conversions are still possible
Rec arg1 = MakeRec().scope(Time(1,2,3,4));
using TupStr = Types<string>;
Tuple<TupStr> tup = buildTuple<TupStr> (arg1);
CHECK (std::get<string> (tup) == "4:03:02.001");
CHECK (string(Time(1,2,3,4)) == "4:03:02.001");
}
};