lumiera_/tests/core/steam/control/command-argument-test.cpp

350 lines
10 KiB
C++
Raw Normal View History

/*
CommandArgument(Test) - checking storage of specifically typed command arguments
Copyright: clarify and simplify the file headers * Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
Copyright (C)
2009, Hermann Vosseler <Ichthyostega@web.de>
Copyright: clarify and simplify the file headers * Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
  **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.
Copyright: clarify and simplify the file headers * Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
* *****************************************************************/
/** @file command-argument-test.cpp
** unit test \ref CommandArgument_test
*/
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include "steam/control/command-storage-holder.hpp"
2009-07-11 20:06:35 +02:00
#include "lib/scoped-ptrvect.hpp"
#include "lib/format-string.hpp"
#include "lib/format-cout.hpp"
#include "lib/util-foreach.hpp"
2009-07-11 20:06:35 +02:00
#include "lib/util.hpp"
#include <functional>
#include <sstream>
#include <cstdlib>
#include <string>
#include <tuple>
#include <array>
using util::_Fmt;
2009-07-11 20:06:35 +02:00
using util::isnil;
using util::for_each;
using lib::time::Time;
using lib::time::TimeVar;
using lib::time::TimeValue;
using std::string;
using std::ostringstream;
using std::make_tuple;
using std::rand;
namespace steam {
namespace control {
namespace test {
using lib::test::showSizeof;
using lib::test::randTime;
2011-12-03 02:56:50 +01:00
using namespace lib::meta;
using LERR_(MISSING_MEMENTO);
2009-07-11 20:06:35 +02:00
namespace { // test helpers
ostringstream protocol; ///< used to verify the test function calls
/**
* watching the instance creation
* of some parameter values
*/
2009-07-11 20:06:35 +02:00
template<typename TY>
struct Tracker
{
TY element_;
static int instanceCnt;
Tracker (TY init = TY()) : element_(init) { ++instanceCnt; }
Tracker (Tracker const& otr) : element_(otr.element_) { ++instanceCnt; }
~Tracker() { --instanceCnt; }
Tracker& operator= (Tracker const&) = default;
TY&
operator* ()
{
return element_;
}
operator string() const { return element_; }
friend bool
operator== (Tracker const& tra1, Tracker const& tra2)
{
return tra1.element_ == tra2.element_;
}
2009-07-11 20:06:35 +02:00
};
2009-07-11 20:06:35 +02:00
template<typename TY>
int Tracker<TY>::instanceCnt (0);
/** Dummy custom memento datatype
* @note memento needs to be equality comparable
*/
struct Sint5
{
std::array<int,5> i5i;
friend bool
operator== (Sint5 const& i1, Sint5 const& i2)
{
return i1.i5i == i2.i5i;
}
};
/* === functions to implement test-"operation" & UNDO === */
2009-07-11 20:06:35 +02:00
void
doIt (Tracker<TimeVar> time, Tracker<string> str, int rand)
2009-07-11 20:06:35 +02:00
{
static _Fmt fmt ("doIt( Time=%s \"%s\" rand=%2d )");
cout << "invoke operation..." << endl;
2009-07-11 20:06:35 +02:00
protocol << fmt % *time % *str % rand;
}
2009-07-11 20:06:35 +02:00
Tracker<string>
captureState (Tracker<TimeVar>, Tracker<string> xstr, int)
2009-07-11 20:06:35 +02:00
{
cout << "capture state..." << endl;
return protocol.str() + *xstr;
2009-07-11 20:06:35 +02:00
}
2009-07-11 20:06:35 +02:00
void
undoIt (Tracker<TimeVar> time, Tracker<string>, int, Tracker<string> memento)
2009-07-11 20:06:35 +02:00
{
cout << "undo... memento=" << memento << endl;
protocol << "undoIt(time="<<time<<")----memento-:"<< *memento;
2009-07-11 20:06:35 +02:00
}
/// another dummy-UNDO function
void dummyU (int,int,int) { }
int dummyC (int u,int o) { return u + rani(o-u+1); }
2009-07-11 20:06:35 +02:00
void
showIt (CmdClosure& clo)
{
cout << clo << endl;
}
2009-07-11 20:06:35 +02:00
void
checkSerialisation (CmdClosure& clo)
{
TODO ("implement serialisation/de-serialisation-Check");
cout << "would be serialised....." << clo << endl;
2009-07-11 20:06:35 +02:00
// serialise, then de-serialise into a new instance and compare both
}
int
twoRandomDigits()
{
return 10 + rani(90);
}
2009-07-11 20:06:35 +02:00
} // test-helper implementation
2009-07-11 20:06:35 +02:00
typedef lib::ScopedPtrVect<CmdClosure> ArgTuples;
/***********************************************************************//**
2009-07-11 20:06:35 +02:00
* @test Check storage handling of the command parameters and state memento.
*
* @see control::CommandStorageHolder
* @see command-basic-test.hpp
*/
class CommandArgument_test : public Test
{
virtual void
run (Arg)
{
seedRand();
2009-07-11 20:06:35 +02:00
ArgTuples testTuples;
Tracker<TimeVar>::instanceCnt = 0;
2009-07-11 20:06:35 +02:00
Tracker<string>::instanceCnt = 0;
createTuples (testTuples);
serialiseArgTuples (testTuples);
testTuples.clear();
simulateCmdLifecycle();
// verify all dtors properly called...
CHECK (0 == Tracker<TimeVar>::instanceCnt);
CHECK (0 == Tracker<string>::instanceCnt);
2009-07-11 20:06:35 +02:00
}
typedef Tracker<TimeVar> TTime;
typedef Tracker<string> Tstr;
2009-07-11 20:06:35 +02:00
/** @test create various argument tuples and re-access their contents */
void
createTuples (ArgTuples& tup)
{
typedef StorageHolder<void(), bool> A1;
typedef StorageHolder<void(int), void*> A2;
typedef StorageHolder<void(int,TimeVar), int> A3;
typedef StorageHolder<void(int,TimeVar), Sint5> A4;
typedef StorageHolder<void(TTime,Tstr,int), Tstr> A5;
A1* arg1 = new A1(); tup.manage (arg1);
A2* arg2 = new A2(); tup.manage (arg2);
A3* arg3 = new A3(); tup.manage (arg3);
A4* arg4 = new A4(); tup.manage (arg4);
A5* arg5 = new A5(); tup.manage (arg5);
CHECK (isnil (*arg1));
CHECK (isnil (*arg2));
CHECK (isnil (*arg3));
CHECK (isnil (*arg4));
CHECK (isnil (*arg5));
2009-07-11 20:06:35 +02:00
for_each (tup, showIt);
arg1->storeTuple (std::tuple<>());
arg2->storeTuple (make_tuple (rani(10)));
arg3->storeTuple (make_tuple (rani(10), TimeVar(randTime())));
arg4->storeTuple (make_tuple (rani(10), TimeVar(randTime())));
arg5->storeTuple (make_tuple (TTime (randTime()), Tstr("glorious"), twoRandomDigits() ));
CHECK (!arg5->canUndo());
arg5->tie(undoIt, captureState)
.tieCaptureFunc() // bind capturing function to memento storage,
(TTime(), Tstr("destruction"), 11); // then invoke the bound capturing mechanism
CHECK (arg5->canUndo());
CHECK (*arg5->memento() == "destruction");
VERIFY_ERROR(MISSING_MEMENTO, arg4->memento().i5i[3] = 513 );
for_each (tup, showIt);
2009-07-11 20:06:35 +02:00
}
2009-07-11 20:06:35 +02:00
/** @test serialise and de-serialise each tuple and check validity
* @todo unimplemented, waiting on Serialiser
*/
void
2009-07-11 20:06:35 +02:00
serialiseArgTuples (ArgTuples& tup)
{
for_each (tup, checkSerialisation);
}
/** @test simulate a complete command lifecycle with regards to the
* storage handling of the command parameters and state memento.
2009-07-11 20:06:35 +02:00
*/
void
simulateCmdLifecycle()
{
typedef void SIG_do(Tracker<TimeVar>, Tracker<string>, int);
using Args = StorageHolder<SIG_do, Tracker<string>>;
using MemHolder = MementoTie<SIG_do, Tracker<string>>;
2009-07-11 20:06:35 +02:00
Args args;
CHECK (isnil (args));
2009-07-11 20:06:35 +02:00
cout << showSizeof(args) << endl;
// store a set of parameter values, later to be used on invocation
args.storeTuple (
make_tuple (TTime(randTime()), Tstr("Lumiera rocks"), twoRandomDigits() ));
CHECK (!isnil (args));
2009-07-11 20:06:35 +02:00
cout << args << endl;
CHECK (!args.canUndo());
VERIFY_ERROR(MISSING_MEMENTO, args.memento() );
2009-07-11 20:06:35 +02:00
MemHolder& memHolder = args.tie(undoIt,captureState);
CHECK (!memHolder); // no stored memento....
CHECK (!args.canUndo());
2009-07-11 20:06:35 +02:00
function<SIG_do> doItFun = doIt;
function<SIG_do> undoFun = memHolder.tieUndoFunc();
function<SIG_do> captFun = memHolder.tieCaptureFunc();
typedef function<void()> OpFun;
// now close all the functions with the stored parameter values...
OpFun bound_doItFun = std::bind (&CmdClosure::invoke, args, CmdFunctor(doItFun));
OpFun bound_undoFun = std::bind (&CmdClosure::invoke, args, CmdFunctor(undoFun));
OpFun bound_captFun = std::bind (&CmdClosure::invoke, args, CmdFunctor(captFun));
2009-07-11 20:06:35 +02:00
protocol.seekp(0);
protocol << "START...";
2009-07-11 20:06:35 +02:00
bound_captFun();
cout << "captured state: " << args.memento() << endl;
CHECK (memHolder);
CHECK (!isnil (*args.memento()));
CHECK (args.canUndo());
cout << args << endl;
2009-07-11 20:06:35 +02:00
bound_doItFun();
cout << protocol.str() << endl;
bound_undoFun();
cout << protocol.str() << endl;
2009-07-12 18:55:33 +02:00
// Commands can serve as prototype to be copied....
2009-07-11 20:06:35 +02:00
Args argsCopy (args);
bound_captFun();
protocol.seekp(0);
protocol << "RESET...";
args.storeTuple (
make_tuple (TTime(TimeValue(123456)), Tstr("unbelievable"), twoRandomDigits() ));
cout << "modified: " << args << endl;
cout << "copied : " << argsCopy << endl; // holds still the old params & memento
2009-07-11 20:06:35 +02:00
bound_undoFun();
cout << protocol.str() << endl;
}
};
/** Register this test class... */
LAUNCHER (CommandArgument_test, "unit controller");
}}} // namespace steam::control::test