Newer
Older
// Copyright 2010 Thomas Moulard.
//
// This file is part of dynamic-graph.
// dynamic-graph is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// dynamic-graph 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 Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
#include <sstream>
#include <iostream>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/signal-ptr.h>
#include <dynamic-graph/signal-time-dependent.h>
#define BOOST_TEST_MODULE pool
#include <boost/test/unit_test.hpp>
#include <boost/test/output_test_stream.hpp>
using boost::test_tools::output_test_stream;
struct MyEntity : public dynamicgraph::Entity
{
static const std::string CLASS_NAME;
dynamicgraph::SignalPtr<double, int> m_sigdSIN;
dynamicgraph::SignalTimeDependent<double, int> m_sigdTimeDepSOUT;
MyEntity (const std::string& name)
: Entity (name)
,m_sigdSIN(NULL,"MyEntity("+name+")::input(double)::in_double")
,m_sigdTimeDepSOUT(boost::bind(&MyEntity::update,this,_1,_2),
m_sigdSIN,
"MyEntity("+name+")::input(double)::out_double")
{
signalRegistration(m_sigdSIN << m_sigdTimeDepSOUT);
}
virtual void display (std::ostream& os) const
{
os << "Hello! My name is " << getName () << " !" << std::endl;
}
virtual const std::string& getClassName () const
{
return CLASS_NAME;
}
double & update(double &res, const int &inTime)
{
const double &aDouble = m_sigdSIN(inTime);
res = aDouble;
return res;
}
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN (MyEntity, "MyEntity");
namespace dg = dynamicgraph;
BOOST_AUTO_TEST_CASE (pool_display)
{
// Create Entity
dg::Entity* entity =
dg::FactoryStorage::getInstance()->
newEntity("MyEntity", "MyEntityInst");
// Search for an entity inside the map
output_test_stream output;
dg::Entity& e = dg::PoolStorage::getInstance()->getEntity
e.display(output);
BOOST_CHECK (output.is_equal ("Hello! My name is MyEntityInst !\n"));
// Testing entityMap
const dg::PoolStorage::Entities& anEntityMap =
dg::PoolStorage::getInstance()->getEntityMap();
bool testExistence = anEntityMap.find("MyEntityInst")==anEntityMap.end();
BOOST_CHECK(!testExistence);
// Testing the existence of an entity
testExistence = dg::PoolStorage::getInstance()->existEntity
("MyEntityInst",entity);
BOOST_CHECK(testExistence);
// Testing the completion list of pool storage
dg::PoolStorage::getInstance()->writeCompletionList
(output);
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
BOOST_CHECK (output.is_equal ("MyEntityInst.in_double\nMyEntityInst.out_double\nprint\nsignals\nsignalDep\n"));
// Checking the graph generated by the pool
dg::PoolStorage::getInstance()->writeGraph("output.dot");
std::fstream the_debug_file;
the_debug_file.open("output.dot");
std::ostringstream oss_output_wgph;
oss_output_wgph << the_debug_file.rdbuf();
the_debug_file.close();
/* Use a predefined output */
std::string str_to_test="/* This graph has been automatically generated.\n"
" 2019 Month: 2 Day: 28 Time: 11:28 */\n"
"digraph \"output\" { graph [ label=\"output\" bgcolor = white rankdir=LR ]\n"
"\t node [ fontcolor = black, color = black, fillcolor = gold1, style=filled, shape=box ] ; \n"
"\tsubgraph cluster_Entities { \n"
"\t} \n"
"\"MyEntityInst\" [ label = \"MyEntityInst\" ,\n"
" fontcolor = black, color = black, fillcolor=cyan, style=filled, shape=box ]\n"
"}\n";
/* Check the two substring (remove the date) */
std::string s_output_wgph = oss_output_wgph.str();
std::string s_crmk="*/";
std::size_t find_s_output_wgph = s_output_wgph.find(s_crmk);
std::string sub_s_output_wgph =s_output_wgph.substr(find_s_output_wgph, s_output_wgph.length());
std::size_t find_str_to_test = str_to_test.find(s_crmk);
std::string sub_str_to_test =str_to_test.substr(find_str_to_test, str_to_test.length());
bool two_sub_string_identical;
two_sub_string_identical=sub_str_to_test==sub_s_output_wgph;
BOOST_CHECK(two_sub_string_identical);
// Deregister the entity.
dg::PoolStorage::getInstance()->deregisterEntity
// Testing the existance of an entity
testExistence = dg::PoolStorage::getInstance()->existEntity
("MyEntityInst",entity);
BOOST_CHECK(!testExistence);
dg::PoolStorage::destroy();