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

239 lines
8.1 KiB
C++
Raw Normal View History

2009-08-11 08:35:37 +02:00
/*
CommandRegistry(Test) - verify command registration and allocation
2010-12-17 23:28:49 +01:00
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>
2010-12-17 23:28:49 +01:00
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.
2010-12-17 23:28:49 +01:00
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
* *****************************************************************/
2009-08-11 08:35:37 +02:00
/** @file command-registry-test.cpp
** unit test \ref CommandRegistry_test
*/
2009-08-11 08:35:37 +02:00
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include "steam/control/command-def.hpp"
#include "steam/control/command-registry.hpp"
#include "lib/symbol.hpp"
2009-08-11 08:35:37 +02:00
#include "lib/util.hpp"
#include "steam/control/test-dummy-commands.hpp"
2009-08-11 08:35:37 +02:00
#include <functional>
2009-08-11 08:35:37 +02:00
namespace steam {
2009-08-11 08:35:37 +02:00
namespace control {
namespace test {
using std::function;
2009-08-11 08:35:37 +02:00
using util::isSameObject;
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";
}
/***************************************************************************//**
2009-08-11 08:35:37 +02:00
* @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
2009-08-11 08:35:37 +02:00
*
* @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)
2009-08-11 08:35:37 +02:00
{
CommandRegistry& registry = CommandRegistry::instance();
CHECK (&registry);
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
CHECK (++cnt_inst == registry.instance_count());
CHECK (++cnt_defs == registry.index_size());
2009-08-11 08:35:37 +02:00
checkRegistration (registry);
checkAllocation(registry);
CHECK (cnt_inst == registry.instance_count());
CHECK (cnt_defs == registry.index_size());
2009-08-11 08:35:37 +02:00
Command::remove (TEST_CMD);
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)
{
CHECK (cnt_inst == registry.instance_count());
2009-08-11 08:35:37 +02:00
Command cmd1 = registry.queryIndex (TEST_CMD);
CHECK (cmd1);
CHECK (TEST_CMD == registry.findDefinition(cmd1));
2009-08-11 08:35:37 +02:00
Command nonexistant = registry.queryIndex("miraculous");
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);
CHECK (cmd2 != cmd1); // note: while they are equivalent, they are not identical
Command cm2x = cmd2.bind(54321);
CHECK (cm2x != cmd1);
CHECK (cm2x == cmd2);
2009-08-11 08:35:37 +02:00
// this created exactly one additional instance allocation:
CHECK (1+cnt_inst == registry.instance_count());
CHECK (1+cnt_defs == registry.index_size());
// ...and another index entry
2009-08-11 08:35:37 +02:00
Command cmdX = registry.queryIndex(TEST_CMD2);
CHECK (cmdX == cmd2);
CHECK (cmdX != cmd1);
2009-08-11 08:35:37 +02: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
CHECK (cmdX.isAnonymous());
CHECK (cmd2.isAnonymous()); //......they got deached
CHECK (!cmd1.isAnonymous());
2009-08-11 08:35:37 +02:00
// create a new registration..
registry.track(TEST_CMD2, cmd2);
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
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
CHECK ( registry.remove(TEST_CMD2));
CHECK (!registry.remove("miraculous"));
2009-08-11 08:35:37 +02: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
CHECK (1+cnt_inst == registry.instance_count()); // but the allocation still lives
cmdX.close();
CHECK (1+cnt_inst == registry.instance_count());
cmd2.close();
cm2x.close();
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);
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);
CHECK (1+cnt_inst == registry.instance_count());
2009-08-11 08:35:37 +02: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);
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
CHECK (!isSameObject (*pImpl, *clone));
2009-08-11 08:35:37 +02:00
CHECK (!pImpl->canExec());
typedef TyOLD<int> ArgType;
2016-12-23 04:23:03 +01:00
TypedArguments<Tuple<ArgType>> arg{Tuple<ArgType>(98765)};
2009-08-11 08:35:37 +02:00
pImpl->setArguments(arg);
CHECK (pImpl->canExec());
2009-08-11 08:35:37 +02:00
CHECK (!clone->canExec()); // this proves the clone has indeed a separate identity
2009-08-11 08:35:37 +02:00
// discard the first clone and overwrite with a new one
clone = registry.createCloneImpl(*pImpl);
CHECK (2+cnt_inst == registry.instance_count());
CHECK (clone->canExec());
2009-08-11 08:35:37 +02:00
clone.reset();
pImpl.reset();
// corresponding allocation slots cleared automatically
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
}}} // namespace steam::control::test