Skip to content
Snippets Groups Projects
Commit f6423581 authored by Thomas Moulard's avatar Thomas Moulard
Browse files

Convert test_depend example into depend unit test.

parent f118e8fa
No related branches found
No related tags found
No related merge requests found
......@@ -39,7 +39,6 @@ MACRO(DYNAMIC_GRAPH_TEST NAME)
# Link against Boost.
TARGET_LINK_LIBRARIES(${NAME} ${Boost_LIBRARIES})
ENDMACRO(DYNAMIC_GRAPH_TEST)
DYNAMIC_GRAPH_TEST(test_depend)
# Signal cast test.
......@@ -58,4 +57,5 @@ TARGET_LINK_LIBRARIES(${EXECUTABLE_NAME} ${signalcast_libs})
DYNAMIC_GRAPH_TEST(entity)
DYNAMIC_GRAPH_TEST(custom-entity)
DYNAMIC_GRAPH_TEST(factory)
DYNAMIC_GRAPH_TEST(pool)
\ No newline at end of file
DYNAMIC_GRAPH_TEST(pool)
DYNAMIC_GRAPH_TEST(signal-time-dependent)
\ No newline at end of file
// 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 <boost/foreach.hpp>
#include <dynamic-graph/signal.h>
#include <dynamic-graph/signal-time-dependent.h>
#define BOOST_TEST_MODULE signal_time_dependent
#include <boost/test/unit_test.hpp>
#include <boost/test/output_test_stream.hpp>
using boost::test_tools::output_test_stream;
typedef dynamicgraph::SignalTimeDependent<double, int> sigDouble_t;
typedef dynamicgraph::SignalTimeDependent<std::string, int> sigString_t;
template<class T>
class DummyClass
{
public:
std::string proname;
std::list<sigDouble_t*> inputsig;
std::list<sigString_t*> inputsigV;
DummyClass (const std::string& n)
: proname (n),
res (),
call (),
timedata ()
{}
T& fun (T& res, int t)
{
++call;
timedata=t;
BOOST_FOREACH (sigDouble_t* ptr, inputsig)
ptr->access(timedata);
BOOST_FOREACH (sigString_t* ptr,
inputsigV)
ptr->access(timedata);
res = (*this) ();
return res;
}
void add (sigDouble_t& sig)
{
inputsig.push_back (&sig);
}
void add (sigString_t& sig)
{
inputsigV.push_back (&sig);
}
T operator() ();
T res;
int call;
int timedata;
};
template<>
double
DummyClass<double>::operator() ()
{
res=call * timedata;
return res;
}
template<>
std::string
DummyClass<std::string>::operator() ()
{
std::ostringstream oss;
oss << call * timedata;
return oss.str ();
}
template< class T >
T DummyClass<T>::operator() ()
{
return this->res;
}
BOOST_AUTO_TEST_CASE (signaltimedependent)
{
DummyClass<double> pro1 ("pro1"), pro3 ("pro3"), pro5 ("pro5");
DummyClass<std::string> pro2 ("pro2"), pro4 ("pro4"), pro6 ("pro6");
sigDouble_t sig5 ("Sig5");
sigString_t sig6 ("Sig6");
sigString_t sig4(sig5, "Sig4");
sigString_t sig2(sig4 << sig4 << sig4 << sig6, "Sig2");
sigDouble_t sig3(sig2 << sig5 << sig6, "Sig3");
sigDouble_t sig1
(boost::bind (&DummyClass<double>::fun, &pro1, _1, _2),
sig2 << sig3, "Sig1");
sig2.setFunction (boost::bind (&DummyClass<std::string>::fun,&pro2, _1, _2));
sig3.setFunction (boost::bind (&DummyClass<double>::fun,&pro3, _1, _2));
sig4.setFunction (boost::bind (&DummyClass<std::string>::fun,&pro4, _1, _2));
sig5.setFunction (boost::bind (&DummyClass<double>::fun,&pro5, _1, _2));
sig6.setFunction (boost::bind (&DummyClass<std::string>::fun,&pro6, _1, _2));
pro1.add(sig2);
pro1.add(sig3);
pro2.add(sig4);
pro2.add(sig4);
pro2.add(sig4);
pro3.add(sig2);
pro4.add(sig5);
pro2.add(sig6);
pro3.add(sig5);
pro3.add(sig6);
sig5.setDependencyType (dynamicgraph::TimeDependency<int>::ALWAYS_READY);
sig6.setDependencyType (dynamicgraph::TimeDependency<int>::BOOL_DEPENDENT);
sig6.setReady();
{
output_test_stream output;
sig1.displayDependencies (output);
BOOST_CHECK (output.is_equal
("-- Sig:Sig1 (Type Fun) (t=0 (/1) )\n"
" |-- Sig:Sig3 (Type Fun) (t=0 (/1) )\n"
" | |-- Sig:Sig6 (Type Fun) (ready=TRUE)\n"
" | |-- Sig:Sig5 (Type Fun) (A)\n"
" | `-- Sig:Sig2 (Type Fun) (t=0 (/1) )\n"
" | |-- Sig:Sig6 (Type Fun) (ready=TRUE)\n"
" | |-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
" | | `-- Sig:Sig5 (Type Fun) (A)\n"
" | |-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
" | | `-- Sig:Sig5 (Type Fun) (A)\n"
" | `-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
" | `-- Sig:Sig5 (Type Fun) (A)\n"
" `-- Sig:Sig2 (Type Fun) (t=0 (/1) )\n"
" |-- Sig:Sig6 (Type Fun) (ready=TRUE)\n"
" |-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
" | `-- Sig:Sig5 (Type Fun) (A)\n"
" |-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
" | `-- Sig:Sig5 (Type Fun) (A)\n"
" `-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
" `-- Sig:Sig5 (Type Fun) (A)"
));
}
BOOST_CHECK (sig1.needUpdate (2));
sig1.access (2);
{
output_test_stream output;
sig1.displayDependencies (output);
BOOST_CHECK (output.is_equal
(
"-- Sig:Sig1 (Type Fun) (t=2 (/1) )\n"
" |-- Sig:Sig3 (Type Fun) (t=2 (/1) )\n"
" | |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
" | |-- Sig:Sig5 (Type Fun) (A)\n"
" | `-- Sig:Sig2 (Type Fun) (t=2 (/1) )\n"
" | |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
" | |-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
" | | `-- Sig:Sig5 (Type Fun) (A)\n"
" | |-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
" | | `-- Sig:Sig5 (Type Fun) (A)\n"
" | `-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
" | `-- Sig:Sig5 (Type Fun) (A)\n"
" `-- Sig:Sig2 (Type Fun) (t=2 (/1) )\n"
" |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
" |-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
" | `-- Sig:Sig5 (Type Fun) (A)\n"
" |-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
" | `-- Sig:Sig5 (Type Fun) (A)\n"
" `-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
" `-- Sig:Sig5 (Type Fun) (A)"
));
}
sig2.access (4);
{
output_test_stream output;
sig1.displayDependencies (output);
BOOST_CHECK (output.is_equal
(
"-- Sig:Sig1 (Type Fun) (t=2 (/1) )\n"
" |-- Sig:Sig3 (Type Fun) (t=2 (/1) )\n"
" | |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
" | |-- Sig:Sig5 (Type Fun) (A)\n"
" | `-- Sig:Sig2 (Type Fun) (t=4 (/1) )\n"
" | |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
" | |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
" | | `-- Sig:Sig5 (Type Fun) (A)\n"
" | |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
" | | `-- Sig:Sig5 (Type Fun) (A)\n"
" | `-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
" | `-- Sig:Sig5 (Type Fun) (A)\n"
" `-- Sig:Sig2 (Type Fun) (t=4 (/1) )\n"
" |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
" |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
" | `-- Sig:Sig5 (Type Fun) (A)\n"
" |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
" | `-- Sig:Sig5 (Type Fun) (A)\n"
" `-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
" `-- Sig:Sig5 (Type Fun) (A)"
));
}
sig1.access (4);
{
output_test_stream output;
sig1.displayDependencies (output);
BOOST_CHECK (output.is_equal
(
"-- Sig:Sig1 (Type Fun) (t=4 (/1) )\n"
" |-- Sig:Sig3 (Type Fun) (t=4 (/1) )\n"
" | |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
" | |-- Sig:Sig5 (Type Fun) (A)\n"
" | `-- Sig:Sig2 (Type Fun) (t=4 (/1) )\n"
" | |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
" | |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
" | | `-- Sig:Sig5 (Type Fun) (A)\n"
" | |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
" | | `-- Sig:Sig5 (Type Fun) (A)\n"
" | `-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
" | `-- Sig:Sig5 (Type Fun) (A)\n"
" `-- Sig:Sig2 (Type Fun) (t=4 (/1) )\n"
" |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
" |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
" | `-- Sig:Sig5 (Type Fun) (A)\n"
" |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
" | `-- Sig:Sig5 (Type Fun) (A)\n"
" `-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
" `-- Sig:Sig5 (Type Fun) (A)"
));
}
sig1.needUpdate (6);
sig1.needUpdate (6);
}
/*
* Copyright 2010,
* François Bleibel,
* Olivier Stasse,
*
* CNRS/AIST
*
* 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/>.
*/
/* -------------------------------------------------------------------------- */
/* --- INCLUDES ------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
// #include <dynamic-graph/all-signals.h>
#include <dynamic-graph/signal.h>
#include <dynamic-graph/signal-time-dependent.h>
//#include <sot/TimeDependency.h>
#include <dynamic-graph/debug.h>
#include <iostream>
#include <string>
using namespace std;
using namespace dynamicgraph;
template< class Res=double >
class DummyClass
{
public:
std::string proname;
list< SignalTimeDependent<double,int>* > inputsig;
list< SignalTimeDependent<string,int>* > inputsigV;
public:
DummyClass( const std::string& n ) : proname(n),res(),appel(0),timedata(0) {}
Res& fun( Res& res,int t)
{
appel++; timedata=t;
cout << "Inside " << proname << " -> " << this
<< endl;
for( list< SignalTimeDependent<double,int>* >::iterator it=inputsig.begin();
it!=inputsig.end();++it )
{
cout << *(*it) << endl;
(*it)->access(timedata);
}
for( list< SignalTimeDependent<string,int>* >::iterator it=inputsigV.begin();
it!=inputsigV.end();++it )
{ cout << *(*it) << endl; (*it)->access(timedata);}
return res=(*this)();
}
void add( SignalTimeDependent<double,int>& sig )
{ inputsig.push_back(&sig); }
void add( SignalTimeDependent<string,int>& sig )
{ inputsigV.push_back(&sig); }
Res operator() ( void );
Res res;
int appel;
int timedata;
};
template< class Res >
Res DummyClass<Res>::operator() (void)
{ return this->res; }
template<>
double DummyClass<double>::operator() (void)
{
res=appel*timedata; return res;
}
template<>
string DummyClass<string>::operator() (void)
{
ostringstream oss;
oss << appel*timedata;
return oss.str();
}
int main( void )
{
DummyClass<double> pro1("pro1"),pro3("pro3"),pro5("pro5");
DummyClass<string> pro2("pro2"),pro4("pro4"),pro6("pro6");
SignalTimeDependent<double,int> sig5("Sig5");
SignalTimeDependent<string,int> sig6("Sig6");
SignalTimeDependent<string,int> sig4(sig5,"Sig4");
SignalTimeDependent<string,int> sig2(sig4<<sig4<<sig4<<sig6,"Sig2");
SignalTimeDependent<double,int> sig3(sig2<<sig5<<sig6,"Sig3");
SignalTimeDependent<double,int> sig1( boost::bind(&DummyClass<double>::fun,&pro1,_1,_2),
sig2<<sig3,"Sig1");
// cout << "--- Test Array ------ "<<endl;
// SignalArray<int> tarr(12);
// tarr<<sig3<<sig2;//+sig2+sig3;
// dispArray(sig4<<sig2<<sig3);
// dispArray(tarr);
sig2.setFunction( boost::bind(&DummyClass<string>::fun,&pro2,_1,_2) );
sig3.setFunction( boost::bind(&DummyClass<double>::fun,&pro3,_1,_2) );
sig4.setFunction( boost::bind(&DummyClass<string>::fun,&pro4,_1,_2) );
sig5.setFunction( boost::bind(&DummyClass<double>::fun,&pro5,_1,_2) );
sig6.setFunction( boost::bind(&DummyClass<string>::fun,&pro6,_1,_2) );
pro1.add(sig2);
pro1.add(sig3);
pro2.add(sig4);
pro2.add(sig4);
pro2.add(sig4);
pro3.add(sig2);
pro4.add(sig5);
pro2.add(sig6);
pro3.add(sig5);
pro3.add(sig6);
sig5.setDependencyType(TimeDependency<int>::ALWAYS_READY);
sig6.setDependencyType(TimeDependency<int>::BOOL_DEPENDENT);
sig6.setReady();
sig1.displayDependencies(cout)<<endl;
cout << "Needs update?" << endl
<< sig1.needUpdate(2) << endl;
dgDEBUG(1) << "Access sig1(2) "<<endl;
sig1.access(2);
sig1.displayDependencies(cout) << endl;
dgDEBUG(1) << "Access sig2(4) "<<endl;
sig2.access(4);
sig1.displayDependencies(cout)<<endl;
dgDEBUG(1) << "Access sig1(4) "<<endl;
sig1.access(4);
sig1.displayDependencies(cout)<<endl;
sig1.needUpdate(6);
sig1.needUpdate(6);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment