2009-06-19 05:57:06 +02:00
|
|
|
|
/*
|
|
|
|
|
|
CommandMutation(Test) - checking the functor and undo-functor used within Proc-commands
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
GNU General Public License for more details.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
2009-08-10 01:30:59 +02:00
|
|
|
|
#include "lib/test/test-helper.hpp"
|
2009-06-19 14:47:37 +02:00
|
|
|
|
#include "proc/control/command-mutation.hpp"
|
2009-09-20 18:37:20 +02:00
|
|
|
|
#include "proc/control/command-argument-holder.hpp"
|
2009-07-06 03:48:45 +02:00
|
|
|
|
#include "proc/control/memento-tie.hpp"
|
2009-06-19 19:11:33 +02:00
|
|
|
|
#include "lib/meta/typelist.hpp"
|
2009-06-19 05:57:06 +02:00
|
|
|
|
#include "lib/meta/tuple.hpp"
|
|
|
|
|
|
|
2009-06-19 19:11:33 +02:00
|
|
|
|
#include <tr1/functional>
|
2009-06-19 05:57:06 +02:00
|
|
|
|
#include <iostream>
|
2009-06-24 05:51:02 +02:00
|
|
|
|
#include <cstdlib>
|
2009-06-19 05:57:06 +02:00
|
|
|
|
#include <string>
|
|
|
|
|
|
|
2009-06-19 19:11:33 +02:00
|
|
|
|
using std::tr1::function;
|
2009-06-19 05:57:06 +02:00
|
|
|
|
using std::string;
|
2009-06-24 05:51:02 +02:00
|
|
|
|
using std::rand;
|
2009-06-19 05:57:06 +02:00
|
|
|
|
using std::cout;
|
|
|
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace control {
|
|
|
|
|
|
namespace test {
|
2009-06-19 19:11:33 +02:00
|
|
|
|
|
2009-07-10 19:04:01 +02:00
|
|
|
|
using namespace lumiera::typelist;
|
2009-06-19 19:11:33 +02:00
|
|
|
|
using control::CmdClosure;
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
2009-07-10 19:04:01 +02:00
|
|
|
|
namespace { /* ======= test functions to bind ========= */
|
|
|
|
|
|
|
2009-06-20 07:13:20 +02:00
|
|
|
|
int testVal=0; ///< used to verify the effect of testFunc
|
2009-07-10 19:04:01 +02:00
|
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
|
void
|
2009-06-19 19:11:33 +02:00
|
|
|
|
testFunc (int val)
|
2009-06-19 05:57:06 +02:00
|
|
|
|
{
|
2009-06-19 19:11:33 +02:00
|
|
|
|
testVal += val;
|
2009-06-19 05:57:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2009-06-19 19:11:33 +02:00
|
|
|
|
int
|
|
|
|
|
|
capture ()
|
2009-06-19 05:57:06 +02:00
|
|
|
|
{
|
2009-06-19 19:11:33 +02:00
|
|
|
|
return testVal;
|
2009-06-19 05:57:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
|
|
* Verify the behaviour of the type erased closure, which is used
|
|
|
|
|
|
* by Proc-Layer commands to implement the capturing and later
|
|
|
|
|
|
* re-invocation of a function.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @see control::Command
|
|
|
|
|
|
* @see control::CommandDef
|
|
|
|
|
|
* @see control::Mutation
|
|
|
|
|
|
* @see control::UndoMutation
|
|
|
|
|
|
* @see command-basic-test.hpp
|
|
|
|
|
|
*/
|
|
|
|
|
|
class CommandMutation_test : public Test
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
|
|
|
|
|
checkMutation();
|
|
|
|
|
|
checkUndoMutation();
|
2009-07-10 19:04:01 +02:00
|
|
|
|
checkStateCapturingMechanism();
|
2009-06-19 05:57:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test check the Mutation functor which is bound to our \c testFunc(int) .
|
2009-09-20 18:37:20 +02:00
|
|
|
|
* Then create a argument closure and use this to invoke the Mutation
|
|
|
|
|
|
* and verify actually \c testFunc(param) is executed.
|
2009-06-19 05:57:06 +02:00
|
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
|
|
|
checkMutation ()
|
|
|
|
|
|
{
|
2009-09-21 03:15:06 +02:00
|
|
|
|
typedef void SIG_fun(int);
|
|
|
|
|
|
function<SIG_fun> funky = testFunc;
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
|
|
|
|
|
Mutation functor (funky);
|
|
|
|
|
|
|
2009-09-21 03:15:06 +02:00
|
|
|
|
MissingArguments<SIG_fun> nullClosure;
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!nullClosure);
|
2009-09-20 18:37:20 +02:00
|
|
|
|
cout << "empty placeholder closure: " << nullClosure << endl;
|
|
|
|
|
|
VERIFY_ERROR (UNBOUND_ARGUMENTS, functor(nullClosure) );
|
|
|
|
|
|
|
|
|
|
|
|
// now create a real closure....
|
2009-06-19 19:11:33 +02:00
|
|
|
|
Tuple<Types<int> > param = tuple::make(23);
|
|
|
|
|
|
Closure<void(int)> close_over (param);
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
2009-09-20 18:37:20 +02:00
|
|
|
|
CmdClosure& closure (close_over);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (closure);
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
2009-09-20 18:37:20 +02:00
|
|
|
|
cout << "param values: " << closure << endl;
|
2009-06-24 05:51:02 +02:00
|
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
|
testVal = 0;
|
2009-09-20 18:37:20 +02:00
|
|
|
|
functor(closure);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (testVal == 23);
|
2009-09-20 18:37:20 +02:00
|
|
|
|
functor(closure);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (testVal == 2*23);
|
2009-06-19 05:57:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test check the special Mutation which is used to \em undo a command.
|
|
|
|
|
|
* This time, we use our \c testFunc(int) as implementation of the
|
|
|
|
|
|
* "undo" function; thus its parameter has now the meaning of an
|
|
|
|
|
|
* captured state value. Consequently this time the \em operation
|
|
|
|
|
|
* which is to be undone would have the signature \c void(void) .
|
2009-07-06 03:48:45 +02:00
|
|
|
|
* Obviously this is a rather silly "undo" function, but it is
|
2009-07-10 19:04:01 +02:00
|
|
|
|
* easy to check for unit testing. To carry out this test, we
|
|
|
|
|
|
* first have to trigger the state capturing mechanism; after that,
|
2009-07-06 03:48:45 +02:00
|
|
|
|
* invoking the UndoMutation will call the testFunc with the
|
|
|
|
|
|
* previously captured state.
|
|
|
|
|
|
* @note Mutation and UndoMutation are value objects, but they refer
|
|
|
|
|
|
* to a common command state, which for this test is modelled
|
|
|
|
|
|
* by local variables and which for the real commands is
|
|
|
|
|
|
* contained in a CommandArgumentHolder
|
2009-06-19 05:57:06 +02:00
|
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
|
|
|
checkUndoMutation ()
|
|
|
|
|
|
{
|
2009-07-10 19:04:01 +02:00
|
|
|
|
function<void(int)> undo_func = testFunc;
|
|
|
|
|
|
function<int(void)> cap_func = capture;
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
2009-07-08 05:36:02 +02:00
|
|
|
|
typedef MementoTie<void(),int> MemHolder;
|
|
|
|
|
|
|
|
|
|
|
|
MemHolder mementoHolder (undo_func,cap_func);
|
2009-07-06 03:48:45 +02:00
|
|
|
|
UndoMutation undoFunctor (mementoHolder);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!mementoHolder);
|
2009-09-20 18:37:20 +02:00
|
|
|
|
|
2009-09-21 03:15:06 +02:00
|
|
|
|
MissingArguments<void(void)> nullClosure;
|
2009-09-20 18:37:20 +02:00
|
|
|
|
VERIFY_ERROR (UNBOUND_ARGUMENTS, undoFunctor(nullClosure) );
|
|
|
|
|
|
VERIFY_ERROR (UNBOUND_ARGUMENTS, undoFunctor.captureState(nullClosure) );
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
2009-06-19 19:11:33 +02:00
|
|
|
|
Tuple<Types<> > param;
|
|
|
|
|
|
Closure<void()> clo (param);
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!mementoHolder);
|
2009-09-20 18:37:20 +02:00
|
|
|
|
VERIFY_ERROR (MISSING_MEMENTO, undoFunctor (clo) );
|
2009-07-08 05:36:02 +02:00
|
|
|
|
VERIFY_ERROR (MISSING_MEMENTO, mementoHolder.getState() );
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
|
|
|
|
|
testVal = 11;
|
2009-09-20 18:37:20 +02:00
|
|
|
|
undoFunctor.captureState(clo);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (mementoHolder);
|
|
|
|
|
|
CHECK (testVal == 11);
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
2009-07-06 03:48:45 +02:00
|
|
|
|
int mem = mementoHolder.getState();
|
2009-06-19 05:57:06 +02:00
|
|
|
|
cout << "saved state: " << mem << endl;
|
|
|
|
|
|
|
2009-09-20 18:37:20 +02:00
|
|
|
|
undoFunctor(clo);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (testVal == 11 + 11);
|
2009-09-20 18:37:20 +02:00
|
|
|
|
undoFunctor(clo);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (testVal == 11 + 11 + 11);
|
2009-09-20 18:37:20 +02:00
|
|
|
|
undoFunctor.captureState(clo);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (33 == mementoHolder.getState());
|
2009-09-20 18:37:20 +02:00
|
|
|
|
undoFunctor(clo);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (testVal == 33 + 33);
|
2009-06-19 05:57:06 +02:00
|
|
|
|
testVal = 9;
|
2009-09-20 18:37:20 +02:00
|
|
|
|
undoFunctor(clo);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (testVal == 42);
|
2009-06-19 05:57:06 +02:00
|
|
|
|
}
|
2009-06-24 05:51:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
2009-07-10 19:04:01 +02:00
|
|
|
|
/** @test check the undo memento capturing mechanism in isolation
|
|
|
|
|
|
* @see memento-tie-test.cpp more in-depth coverage */
|
|
|
|
|
|
void
|
|
|
|
|
|
checkStateCapturingMechanism ()
|
|
|
|
|
|
{
|
|
|
|
|
|
typedef MementoTie<void(),int> MemHolder;
|
|
|
|
|
|
|
|
|
|
|
|
MemHolder mementoHolder (testFunc, capture);
|
|
|
|
|
|
|
|
|
|
|
|
function<void()> bound_undo_func = mementoHolder.tieUndoFunc();
|
|
|
|
|
|
function<void()> bound_cap_func = mementoHolder.tieCaptureFunc();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int rr (rand() % 100);
|
|
|
|
|
|
testVal = rr;
|
|
|
|
|
|
bound_cap_func(); // invoke state capturing
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (rr == mementoHolder.getState());
|
2009-07-10 19:04:01 +02:00
|
|
|
|
|
|
|
|
|
|
testVal = 10; // meanwhile "somehow" mutate the state
|
|
|
|
|
|
bound_undo_func(); // invoking the undo() feeds back the memento
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (testVal == 10+rr);
|
2009-07-10 19:04:01 +02:00
|
|
|
|
}
|
2009-06-19 05:57:06 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
|
LAUNCHER (CommandMutation_test, "unit controller");
|
2009-07-10 19:04:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
|
}} // namespace control::test
|