2009-08-11 08:35:37 +02:00
|
|
|
|
/*
|
|
|
|
|
|
CommandRegistry(Test) - verify command registration and allocation
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
2009-08-11 08:35:37 +02:00
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
2009-08-11 08:35:37 +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-08-11 08:35:37 +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-08-11 08:35:37 +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-08-11 08:35:37 +02:00
|
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
|
|
|
|
|
#include "lib/test/test-helper.hpp"
|
|
|
|
|
|
#include "proc/control/command-def.hpp"
|
2009-10-03 06:05:43 +02:00
|
|
|
|
#include "proc/control/command-registry.hpp"
|
2009-09-24 23:02:40 +02:00
|
|
|
|
#include "lib/symbol.hpp"
|
2009-08-11 08:35:37 +02:00
|
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include "proc/control/test-dummy-commands.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include <tr1/functional>
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-02 17:50:44 +01:00
|
|
|
|
namespace proc {
|
2009-08-11 08:35:37 +02:00
|
|
|
|
namespace control {
|
|
|
|
|
|
namespace test {
|
2009-10-03 06:05:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
2009-08-11 08:35:37 +02:00
|
|
|
|
using std::tr1::function;
|
|
|
|
|
|
using util::isSameObject;
|
2009-09-24 23:02:40 +02:00
|
|
|
|
using lib::Symbol;
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace { // test data and helpers...
|
|
|
|
|
|
|
|
|
|
|
|
Symbol TEST_CMD = "test.command1.1";
|
|
|
|
|
|
Symbol TEST_CMD2 = "test.command1.2";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
* @test verify sane behaviour of the relevant operations on the CommandRegistry
|
|
|
|
|
|
* interface. Add/remove a command instance to the index, allocate an
|
|
|
|
|
|
* CommandImpl frame and verify it is removed properly on ref count zero.
|
|
|
|
|
|
* @note this test covers the internal bits of functionality,
|
|
|
|
|
|
* not the behaviour of the (integrated) command framework
|
|
|
|
|
|
*
|
|
|
|
|
|
* @see Command
|
|
|
|
|
|
* @see CommandRegistry
|
|
|
|
|
|
* @see command.cpp
|
|
|
|
|
|
* @see command-use1-test.cpp
|
|
|
|
|
|
*/
|
|
|
|
|
|
class CommandRegistry_test : public Test
|
|
|
|
|
|
{
|
2009-10-05 06:07:21 +02:00
|
|
|
|
|
2009-08-11 08:35:37 +02:00
|
|
|
|
uint cnt_defs;
|
|
|
|
|
|
uint cnt_inst;
|
2009-10-05 06:07:21 +02:00
|
|
|
|
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
|
|
|
|
|
CommandRegistry& registry = CommandRegistry::instance();
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (®istry);
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
|
|
|
|
|
cnt_defs = registry.index_size();
|
|
|
|
|
|
cnt_inst = registry.instance_count();
|
|
|
|
|
|
|
|
|
|
|
|
// prepare a command definition (prototype)
|
|
|
|
|
|
CommandDef (TEST_CMD)
|
|
|
|
|
|
.operation (command1::operate)
|
|
|
|
|
|
.captureUndo (command1::capture)
|
|
|
|
|
|
.undoOperation (command1::undoIt)
|
|
|
|
|
|
.bind(123);
|
|
|
|
|
|
|
|
|
|
|
|
// this command definition is
|
|
|
|
|
|
// represented internally by a prototype instance
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (++cnt_inst == registry.instance_count());
|
|
|
|
|
|
CHECK (++cnt_defs == registry.index_size());
|
2009-09-24 05:09:51 +02:00
|
|
|
|
|
2009-08-11 08:35:37 +02:00
|
|
|
|
checkRegistration (registry);
|
|
|
|
|
|
checkAllocation(registry);
|
|
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (cnt_inst == registry.instance_count());
|
|
|
|
|
|
CHECK (cnt_defs == registry.index_size());
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
|
|
|
|
|
Command::remove (TEST_CMD);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (--cnt_inst == registry.instance_count());
|
2009-08-11 08:35:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-05 06:07:21 +02:00
|
|
|
|
|
|
|
|
|
|
/** @test verify the index operation.
|
|
|
|
|
|
* Add, search, remove, store copy.
|
|
|
|
|
|
*/
|
2009-08-11 08:35:37 +02:00
|
|
|
|
void
|
|
|
|
|
|
checkRegistration (CommandRegistry& registry)
|
|
|
|
|
|
{
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (cnt_inst == registry.instance_count());
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
|
|
|
|
|
Command cmd1 = registry.queryIndex (TEST_CMD);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (cmd1);
|
|
|
|
|
|
CHECK (TEST_CMD == registry.findDefinition(cmd1));
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
|
|
|
|
|
Command nonexistant = registry.queryIndex("miraculous");
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!nonexistant);
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
|
|
|
|
|
// now create a clone, registered under a different ID
|
|
|
|
|
|
Command cmd2 = cmd1.storeDef(TEST_CMD2);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (cmd2 == cmd1);
|
2009-10-03 06:05:43 +02:00
|
|
|
|
cmd2.bind(54321);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (cmd2 != cmd1);
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
|
|
|
|
|
// this created exactly one additional instance allocation:
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (1+cnt_inst == registry.instance_count());
|
|
|
|
|
|
CHECK (1+cnt_defs == registry.index_size());
|
2009-10-03 06:05:43 +02:00
|
|
|
|
// ...and another index entry
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Command cmdX = registry.queryIndex(TEST_CMD2);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (cmdX == cmd2);
|
|
|
|
|
|
CHECK (cmdX != cmd1);
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (registry.remove(TEST_CMD2));
|
|
|
|
|
|
CHECK (!registry.queryIndex(TEST_CMD2));
|
|
|
|
|
|
CHECK (cnt_defs == registry.index_size()); // removed from index
|
|
|
|
|
|
CHECK (1+cnt_inst == registry.instance_count()); //...but still alive
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
|
|
|
|
|
// create a new registration..
|
2009-10-09 02:53:39 +02:00
|
|
|
|
registry.track(TEST_CMD2, cmd2);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (registry.queryIndex(TEST_CMD2));
|
|
|
|
|
|
CHECK (1+cnt_defs == registry.index_size()); // again holding two distinct entries
|
|
|
|
|
|
CHECK (cmdX == cmd2);
|
|
|
|
|
|
CHECK (cmdX != cmd1);
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (TEST_CMD == registry.findDefinition(cmd1));
|
|
|
|
|
|
CHECK (TEST_CMD2 == registry.findDefinition(cmd2));
|
|
|
|
|
|
CHECK (TEST_CMD2 == registry.findDefinition(cmdX));
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK ( registry.remove(TEST_CMD2));
|
|
|
|
|
|
CHECK (!registry.remove("miraculous"));
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!registry.queryIndex(TEST_CMD2));
|
|
|
|
|
|
CHECK ( registry.queryIndex(TEST_CMD));
|
|
|
|
|
|
CHECK (cnt_defs == registry.index_size()); // the index entry is gone,
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (1+cnt_inst == registry.instance_count()); // but the allocation still lives
|
2009-10-03 06:05:43 +02:00
|
|
|
|
cmdX.close();
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (1+cnt_inst == registry.instance_count());
|
2009-10-03 06:05:43 +02:00
|
|
|
|
cmd2.close();
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (0+cnt_inst == registry.instance_count()); // ...as long as it's still referred
|
2009-08-11 08:35:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-05 06:07:21 +02:00
|
|
|
|
|
|
|
|
|
|
/** @test verify the allocation/de-allocation handling as
|
|
|
|
|
|
* embedded into the CommandRegistry operation.
|
|
|
|
|
|
* Simulates on low level what normally happens
|
|
|
|
|
|
* during command lifecycle.
|
|
|
|
|
|
*/
|
2009-08-11 08:35:37 +02:00
|
|
|
|
void
|
|
|
|
|
|
checkAllocation (CommandRegistry& registry)
|
|
|
|
|
|
{
|
|
|
|
|
|
// simulate what normally happens within a CommandDef
|
|
|
|
|
|
typedef void Sig_oper(int);
|
|
|
|
|
|
typedef long Sig_capt(int);
|
|
|
|
|
|
typedef void Sig_undo(int,long);
|
|
|
|
|
|
|
|
|
|
|
|
function<Sig_oper> o_Fun (command1::operate);
|
|
|
|
|
|
function<Sig_capt> c_Fun (command1::capture);
|
|
|
|
|
|
function<Sig_undo> u_Fun (command1::undoIt);
|
|
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (o_Fun && c_Fun && u_Fun);
|
|
|
|
|
|
CHECK (cnt_inst == registry.instance_count());
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
|
|
|
|
|
// when the CommandDef is complete, it issues the
|
|
|
|
|
|
// allocation call to the registry behind the scenes....
|
|
|
|
|
|
|
|
|
|
|
|
typedef shared_ptr<CommandImpl> PImpl;
|
|
|
|
|
|
|
|
|
|
|
|
PImpl pImpl = registry.newCommandImpl(o_Fun,c_Fun,u_Fun);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (1+cnt_inst == registry.instance_count());
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (pImpl);
|
|
|
|
|
|
CHECK (pImpl->isValid());
|
|
|
|
|
|
CHECK (!pImpl->canExec());
|
|
|
|
|
|
CHECK (1 == pImpl.use_count()); // no magic involved, we hold the only instance
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
|
|
|
|
|
PImpl clone = registry.createCloneImpl(*pImpl);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (clone->isValid());
|
|
|
|
|
|
CHECK (!clone->canExec());
|
|
|
|
|
|
CHECK (1 == clone.use_count());
|
|
|
|
|
|
CHECK (1 == pImpl.use_count());
|
|
|
|
|
|
CHECK (2+cnt_inst == registry.instance_count());
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!isSameObject (*pImpl, *clone));
|
|
|
|
|
|
CHECK (*pImpl == *clone);
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!pImpl->canExec());
|
2009-08-11 08:35:37 +02:00
|
|
|
|
typedef Types<int> ArgType;
|
|
|
|
|
|
TypedArguments<Tuple<ArgType> > arg (Tuple<ArgType>(98765));
|
|
|
|
|
|
pImpl->setArguments(arg);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (pImpl->canExec());
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!clone->canExec()); // this proves the clone has indeed a separate identity
|
|
|
|
|
|
CHECK (*pImpl != *clone);
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
|
|
|
|
|
// discard the first clone and overwrite with a new one
|
|
|
|
|
|
clone = registry.createCloneImpl(*pImpl);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (2+cnt_inst == registry.instance_count());
|
|
|
|
|
|
CHECK (*pImpl == *clone);
|
|
|
|
|
|
CHECK (clone->canExec());
|
2009-08-11 08:35:37 +02:00
|
|
|
|
|
|
|
|
|
|
clone.reset();
|
|
|
|
|
|
pImpl.reset();
|
|
|
|
|
|
// corresponding allocation slots cleared automatically
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (cnt_inst == registry.instance_count());
|
2009-08-11 08:35:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
|
LAUNCHER (CommandRegistry_test, "function controller");
|
2009-10-05 06:07:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
2011-12-02 17:50:44 +01:00
|
|
|
|
}}} // namespace proc::control::test
|