2009-06-19 05:57:06 +02:00
|
|
|
/*
|
|
|
|
|
CommandMutation(Test) - checking the functor and undo-functor used within Proc-commands
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
|
|
|
|
|
|
This program 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.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
|
|
|
|
#include "lib/test/test-helper.hpp"
|
|
|
|
|
//#include "proc/asset/media.hpp"
|
|
|
|
|
//#include "proc/mobject/session.hpp"
|
|
|
|
|
//#include "proc/mobject/session/edl.hpp"
|
|
|
|
|
//#include "proc/mobject/session/testclip.hpp"
|
|
|
|
|
//#include "proc/mobject/test-dummy-mobject.hpp"
|
|
|
|
|
//#include "lib/p.hpp"
|
|
|
|
|
//#include "proc/mobject/placement.hpp"
|
|
|
|
|
//#include "proc/mobject/placement-index.hpp"
|
|
|
|
|
//#include "proc/mobject/explicitplacement.hpp"
|
2009-06-19 14:47:37 +02:00
|
|
|
#include "proc/control/command-mutation.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"
|
|
|
|
|
//#include "lib/lumitime.hpp"
|
|
|
|
|
//#include "lib/util.hpp"
|
|
|
|
|
|
2009-06-19 19:11:33 +02:00
|
|
|
#include <tr1/functional>
|
2009-06-19 05:57:06 +02:00
|
|
|
//#include <boost/format.hpp>
|
|
|
|
|
#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::bind;
|
2009-06-19 05:57:06 +02:00
|
|
|
//using std::tr1::placeholders::_1;
|
|
|
|
|
//using std::tr1::placeholders::_2;
|
2009-06-19 19:11:33 +02:00
|
|
|
using std::tr1::function;
|
2009-06-19 05:57:06 +02:00
|
|
|
//using boost::format;
|
|
|
|
|
//using lumiera::Time;
|
|
|
|
|
//using util::contains;
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
|
|
using lib::test::showSizeof;
|
|
|
|
|
|
|
|
|
|
// using session::test::TestClip;
|
|
|
|
|
// using lumiera::P;
|
2009-06-19 19:11:33 +02:00
|
|
|
using namespace lumiera::typelist;
|
2009-06-19 05:57:06 +02:00
|
|
|
using lumiera::typelist::Tuple;
|
2009-06-19 19:11:33 +02:00
|
|
|
|
|
|
|
|
using control::CmdClosure;
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
2009-06-19 19:11:33 +02:00
|
|
|
|
2009-06-20 07:13:20 +02:00
|
|
|
int testVal=0; ///< used to verify the effect of testFunc
|
2009-06-19 19:11:33 +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-06 03:48:45 +02:00
|
|
|
checkStateCapturingMechanism();
|
2009-06-19 05:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test check the Mutation functor which is bound to our \c testFunc(int) .
|
|
|
|
|
* Then close this Mutation by providing a parameter tuple.
|
|
|
|
|
* Verify finally that by invoking the Mutation functor
|
|
|
|
|
* actually \c testFunc(param) is executed.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
checkMutation ()
|
|
|
|
|
{
|
2009-06-19 19:11:33 +02:00
|
|
|
function<void(int)> funky = bind (testFunc, _1);
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
|
|
|
Mutation functor (funky);
|
|
|
|
|
ASSERT (!functor);
|
|
|
|
|
VERIFY_ERROR (UNBOUND_ARGUMENTS, functor() );
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
CmdClosure& clo (close_over);
|
|
|
|
|
functor.close(clo);
|
|
|
|
|
ASSERT (functor);
|
|
|
|
|
|
2009-06-24 05:51:02 +02:00
|
|
|
cout << "param values: " << clo << endl;
|
|
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
testVal = 0;
|
|
|
|
|
functor();
|
|
|
|
|
ASSERT (testVal == 23);
|
|
|
|
|
functor();
|
|
|
|
|
ASSERT (testVal == 2*23);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @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
|
|
|
|
|
* easy to check in a unit test. To carry out this test, we first
|
|
|
|
|
* have to trigger the state capturing mechanism; after that,
|
|
|
|
|
* 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-06-19 19:11:33 +02:00
|
|
|
function<void(int)> undo_func = bind (&testFunc,_1);
|
|
|
|
|
function<int(void)> cap_func = bind (&capture );
|
2009-06-19 05:57:06 +02:00
|
|
|
|
2009-07-06 03:48:45 +02:00
|
|
|
MementoTie mementoHolder (undo_func,cap_func);
|
|
|
|
|
UndoMutation undoFunctor (mementoHolder);
|
2009-06-19 05:57:06 +02:00
|
|
|
ASSERT (!undoFunctor);
|
|
|
|
|
VERIFY_ERROR (UNBOUND_ARGUMENTS, undoFunctor() );
|
|
|
|
|
|
2009-06-19 19:11:33 +02:00
|
|
|
Tuple<Types<> > param;
|
|
|
|
|
Closure<void()> clo (param);
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
|
|
|
undoFunctor.close(clo);
|
|
|
|
|
ASSERT (!undoFunctor);
|
|
|
|
|
VERIFY_ERROR (MISSING_MEMENTO, undoFunctor() );
|
2009-07-06 03:48:45 +02:00
|
|
|
VERIFY_ERROR (MISSING_MEMENTO, mementoHoder.getState() );
|
2009-06-19 05:57:06 +02:00
|
|
|
|
|
|
|
|
testVal = 11;
|
|
|
|
|
undoFunctor.captureState();
|
|
|
|
|
ASSERT (undoFunctor);
|
|
|
|
|
ASSERT (testVal == 11);
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
undoFunctor();
|
|
|
|
|
ASSERT (testVal == 11 + 11);
|
|
|
|
|
undoFunctor();
|
|
|
|
|
ASSERT (testVal == 11 + 11 + 11);
|
|
|
|
|
undoFunctor.captureState();
|
2009-07-06 03:48:45 +02:00
|
|
|
ASSERT (33 == mementoHolder.getState());
|
2009-06-19 05:57:06 +02:00
|
|
|
undoFunctor();
|
|
|
|
|
ASSERT (testVal == 33 + 33);
|
|
|
|
|
testVal = 9;
|
|
|
|
|
undoFunctor();
|
|
|
|
|
ASSERT (testVal == 42);
|
|
|
|
|
|
2009-07-06 03:48:45 +02:00
|
|
|
UndoMutation clonedFunc (undoFunctor); // refers to the same state
|
|
|
|
|
ASSERT (clonedFunc);
|
2009-06-19 05:57:06 +02:00
|
|
|
|
2009-07-06 03:48:45 +02:00
|
|
|
ASSERT (33 == mementoHolder.getState());
|
2009-06-19 05:57:06 +02:00
|
|
|
clonedFunc.captureState();
|
2009-07-06 03:48:45 +02:00
|
|
|
ASSERT (42 == mementoHolder.getState()); // and captures into the same storage
|
|
|
|
|
|
2009-06-19 05:57:06 +02:00
|
|
|
testVal = 0;
|
|
|
|
|
clonedFunc();
|
|
|
|
|
ASSERT (testVal == 42);
|
|
|
|
|
}
|
2009-06-24 05:51:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2009-07-06 03:48:45 +02:00
|
|
|
checkStateCapturingMechanism ()
|
2009-06-24 05:51:02 +02:00
|
|
|
{
|
|
|
|
|
function<void(int)> undo_func = bind (&testFunc,_1);
|
|
|
|
|
function<int(void)> cap_func = bind (&capture );
|
2009-06-26 05:27:24 +02:00
|
|
|
|
2009-07-06 03:48:45 +02:00
|
|
|
MementoTie mementoHolder (undo_func,cap_func);
|
2009-06-24 05:51:02 +02:00
|
|
|
|
2009-07-06 03:48:45 +02:00
|
|
|
function<void()> bound_undo_func = mementoHolder.tieUndoFunc();
|
|
|
|
|
function<void()> bound_cap_func = mementoHolder.tieCaptureFunc();
|
2009-06-24 05:51:02 +02:00
|
|
|
|
2009-07-06 03:48:45 +02:00
|
|
|
VERIFY_ERROR (MISSING_MEMENTO, bound_undo_func() );
|
|
|
|
|
VERIFY_ERROR (MISSING_MEMENTO, mementoHoder.getState() );
|
2009-06-24 05:51:02 +02:00
|
|
|
|
|
|
|
|
int rr (rand() %100);
|
|
|
|
|
testVal = rr;
|
2009-07-06 03:48:45 +02:00
|
|
|
bound_cap_func(); // invoke state capturing
|
2009-06-24 05:51:02 +02:00
|
|
|
|
2009-07-06 03:48:45 +02:00
|
|
|
ASSERT (rr == mementoHolder.getState());
|
2009-06-24 05:51:02 +02:00
|
|
|
|
|
|
|
|
testVal = -10; // meanwhile "somehow" mutate the state
|
2009-07-06 03:48:45 +02:00
|
|
|
bound_undo_func(); // invoking the undo() feeds back the memento
|
|
|
|
|
ASSERT (rr-10 == testVal);
|
2009-06-24 05:51:02 +02:00
|
|
|
|
|
|
|
|
// this cycle can be repeated with different state values
|
|
|
|
|
rr = (rand() %100);
|
|
|
|
|
testVal = rr;
|
2009-07-06 03:48:45 +02:00
|
|
|
bound_cap_func(); // capture new state
|
|
|
|
|
ASSERT (rr == mementoHolder.getState());
|
|
|
|
|
|
2009-06-24 05:51:02 +02:00
|
|
|
testVal = -20;
|
2009-07-06 03:48:45 +02:00
|
|
|
bound_undo_func();
|
|
|
|
|
ASSERT (rr-20 == testVal);
|
2009-06-24 05:51:02 +02:00
|
|
|
}
|
2009-06-19 05:57:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
LAUNCHER (CommandMutation_test, "unit controller");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace control::test
|