lumiera_/tests/library/meta/tuple-record-init-test.cpp
Ichthyostega 412abbace2 clean-up: validate use of variadic seq with tuples and generators
Most of the type-list and type-sequence related eccosystem can be
just switched over, after having added the conversion variants for
the new-style variadic type sequences

Again this was used as opportunity to improve readability of related tests
2025-06-03 16:14:13 +02:00

164 lines
5.7 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
TupleRecordInit(Test) - to build a tuple from a GenNode sequence
Copyright (C)
2016, Hermann Vosseler <Ichthyostega@web.de>
  **Lumiera** 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. See the file COPYING for further details.
* *****************************************************************/
/** @file tuple-record-init-test.cpp
** unit test \ref TupleRecordInit_test
*/
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include "lib/time/timevalue.hpp"
#include "lib/meta/tuple-record-init.hpp"
#include "lib/format-cout.hpp"
#include "lib/format-util.hpp"
#include <string>
using lib::Symbol;
using lib::Variant;
using lib::idi::EntryID;
using lib::diff::Rec;
using lib::diff::MakeRec;
using lib::diff::GenNode;
using lib::meta::TySeq;
using lib::meta::Tuple;
using lib::meta::buildTuple;
using lib::time::Duration;
using lib::time::Time;
using lib::hash::LuidH;
using std::string;
using std::tuple;
using std::get;
namespace lib {
namespace meta {
namespace test {
using LERR_(WRONG_TYPE);
/*************************************************************************//**
* @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 stage::test::Nexus::prepareDiagnosticCommandHandler
* @see ui-bus.hpp UI-Bus
*/
class TupleRecordInit_test : public Test
{
virtual void
run (Arg)
{
show_simpleUsage();
verify_errorHandling();
}
void
show_simpleUsage()
{
using NiceTypes = TySeq<string, int>;
using UgglyTypes = TySeq<EntryID<long>, Symbol, int, int64_t, double, Duration>; // various conversions and an immutable type (Duration)
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;
auto argT = buildTuple<NiceTypes> (args);
auto urgT = buildTuple<UgglyTypes> (urgs);
cout << argT <<endl;
cout << urgT <<endl;
CHECK (get<0>(argT) == "lalü"_expect);
CHECK (get<0>(urgT) == "ID<long>-lal"_expect);
CHECK (get<1>(urgT) == "lala"_expect);
}
void
verify_errorHandling()
{
Rec args = MakeRec().scope("surprise", 42);
using TooMany = TySeq<string, int, long>;
VERIFY_ERROR (WRONG_TYPE, buildTuple<TooMany> (args)); // number of types in tuple exceeds capacity of the supplied argument record
using Unsigned = TySeq<string, uint>;
using Floating = TySeq<string, float>;
using Narrowing = TySeq<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 timeArg = MakeRec().scope(Time(1,2,3,4));
using TupStr = TySeq<string>;
Tuple<TupStr> tup = buildTuple<TupStr> (timeArg);
CHECK (std::get<string> (tup) == "4:03:02.001");
CHECK (string(Time(1,2,3,4)) == "4:03:02.001");
// conversions from LUID elements are handled restrictively
Rec hashArg = MakeRec().scope("random", LuidH());
VERIFY_ERROR (WRONG_TYPE, buildTuple<Unsigned> (args));
VERIFY_ERROR (WRONG_TYPE, buildTuple<Floating> (args));
VERIFY_ERROR (WRONG_TYPE, buildTuple<Narrowing> (args));
using ToSizeT = TySeq<string, size_t>;
VERIFY_ERROR (WRONG_TYPE, (buildTuple<ToSizeT> (args))); // not even conversion to size_t is allowed
struct Hashy
{
HashVal hash;
Hashy (LuidH const& luid)
: hash(luid)
{ }
};
using WithHashy = TySeq<string, Hashy>;
Tuple<WithHashy> tup2 = buildTuple<WithHashy> (hashArg); // while any type explicitly constructible from LUID are permitted.
VERIFY_ERROR (WRONG_TYPE, buildTuple<WithHashy> (args)); // building a `Hashy` from int(42) is disallowed, of course
HashVal h = get<Hashy>(tup2).hash;
CHECK (h == hashArg.child(1).data.get<LuidH>()); // note: the narrowing conversion happens within LuidH::operator HashVal()
}
};
/** Register this test class... */
LAUNCHER (TupleRecordInit_test, "unit meta");
}}} // namespace lib::meta::test