Commit d8f7e613 authored by Olivier Stasse's avatar Olivier Stasse Committed by olivier stasse

[tests] Improve coverage of signals.

parent f1577a88
...@@ -129,7 +129,7 @@ BOOST_AUTO_TEST_CASE(test_base) { ...@@ -129,7 +129,7 @@ BOOST_AUTO_TEST_CASE(test_base) {
/// Display the signal /// Display the signal
sigB.display(output); sigB.display(output);
BOOST_CHECK(output.is_equal("")); BOOST_CHECK(output.is_equal("Sig:test"));
} }
BOOST_AUTO_TEST_CASE(test_cast_helper) { BOOST_AUTO_TEST_CASE(test_cast_helper) {
......
#pragma GCC diagnostic push
#pragma GCC system_header
#include <Eigen/Dense>
#pragma GCC diagnostic pop
...@@ -6,8 +6,7 @@ ...@@ -6,8 +6,7 @@
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <boost/format.hpp> #include <boost/format.hpp>
#include <Eigen/Dense> #include "signal-cast-register-test.h"
#include <dynamic-graph/debug.h> #include <dynamic-graph/debug.h>
#include <dynamic-graph/eigen-io.h> #include <dynamic-graph/eigen-io.h>
#include <dynamic-graph/entity.h> #include <dynamic-graph/entity.h>
......
...@@ -3,12 +3,15 @@ ...@@ -3,12 +3,15 @@
#include <string> #include <string>
#include <boost/foreach.hpp>
#include <dynamic-graph/debug.h> #include <dynamic-graph/debug.h>
#include <dynamic-graph/entity.h> #include <dynamic-graph/entity.h>
#include <dynamic-graph/factory.h> #include <dynamic-graph/factory.h>
#include <dynamic-graph/pool.h> #include <dynamic-graph/pool.h>
#include <dynamic-graph/signal-base.h> #include <dynamic-graph/signal-base.h>
#include <dynamic-graph/signal-ptr.h> #include <dynamic-graph/signal-ptr.h>
#include <dynamic-graph/signal-time-dependent.h>
#include <dynamic-graph/signal.h> #include <dynamic-graph/signal.h>
#include <iostream> #include <iostream>
...@@ -17,14 +20,61 @@ ...@@ -17,14 +20,61 @@
#include <boost/test/unit_test_suite.hpp> #include <boost/test/unit_test_suite.hpp>
#include <string> #include <string>
using boost::test_tools::output_test_stream;
typedef dynamicgraph::SignalTimeDependent<double, int> sigDouble_t;
typedef dynamicgraph::SignalTimeDependent<std::string, int> sigString_t;
using namespace dynamicgraph; using namespace dynamicgraph;
using std::cout; using std::cout;
BOOST_AUTO_TEST_CASE(normal_test) { template <class T> class DummyClass {
Signal<double, int> sig("sig"); public:
SignalPtr<double, int> sigPtrA(NULL, "sigPtrA"), sigPtrB(NULL, "sigPtrB"); 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(normal_cst_test) {
SignalPtr<double, int> sigNotPlug(NULL, "sigNotPlug"); SignalPtr<double, int> sigNotPlug(NULL, "sigNotPlug");
SignalPtr<double, int> sigPtrAbstract(NULL, "sigPtrAbstract"); const SignalPtr<double, int> cstSigNotPlug(NULL, "sigNotPlug");
try { try {
sigNotPlug.getPtr(); sigNotPlug.getPtr();
...@@ -32,6 +82,69 @@ BOOST_AUTO_TEST_CASE(normal_test) { ...@@ -32,6 +82,69 @@ BOOST_AUTO_TEST_CASE(normal_test) {
cout << "Error catch" << std::endl; cout << "Error catch" << std::endl;
} }
// Test getPtr without plug
/// This create a ExceptionSignal::NOT_INITIALIZED
bool res = false;
try {
// Signal<double, int> * r =
sigNotPlug.getPtr();
}
catch(const ExceptionSignal &aea)
{
res = (aea.getCode() == ExceptionSignal::NOT_INITIALIZED);
}
BOOST_CHECK(res);
/// Testing const getPtr() interface: no plug case
try {
cstSigNotPlug.getPtr();
}
catch(const ExceptionSignal &aea)
{
res = (aea.getCode() == ExceptionSignal::NOT_INITIALIZED);
}
BOOST_CHECK(res);
/// Test needUpdate without plug
res = sigNotPlug.needUpdate(5);
sigNotPlug.getTime();
output_test_stream output;
sigNotPlug.display(output);
cstSigNotPlug.display(output);
/// Testing getAbsatractPtr() interface: no plug
res=false;
try {
sigNotPlug.getAbstractPtr();
}
catch(const ExceptionSignal &aea)
{
res = (aea.getCode() == ExceptionSignal::NOT_INITIALIZED);
}
BOOST_CHECK(res);
/// Testing const getAbstractPtr() interface: no plug case
try {
cstSigNotPlug.getAbstractPtr();
}
catch(const ExceptionSignal &aea)
{
res = (aea.getCode() == ExceptionSignal::NOT_INITIALIZED);
}
BOOST_CHECK(res);
try { sigNotPlug.checkCompatibility(); }
catch(...) { }
BOOST_CHECK(res);
}
BOOST_AUTO_TEST_CASE(normal_test) {
Signal<double, int> sig("sig");
SignalPtr<double, int> sigPtrA(NULL, "sigPtrA"), sigPtrB(NULL, "sigPtrB");
SignalPtr<double, int> sigPtrAbstract(NULL, "sigPtrAbstract");
DummyClass<double> pro3("pro3");
sig.setConstant(1.56); sig.setConstant(1.56);
sig.recompute(2); sig.recompute(2);
std::string name = "sig"; std::string name = "sig";
...@@ -42,17 +155,31 @@ BOOST_AUTO_TEST_CASE(normal_test) { ...@@ -42,17 +155,31 @@ BOOST_AUTO_TEST_CASE(normal_test) {
} catch (ExceptionSignal e) { } catch (ExceptionSignal e) {
e.getExceptionName(); e.getExceptionName();
} }
BOOST_CHECK(true);
sigPtrA.setFunction(boost::bind(&DummyClass<double>::fun, &pro3, _1, _2));
/// Plugging signal.
SignalBase<int> &sigRef = sig; SignalBase<int> &sigRef = sig;
SignalBase<int> &sigPtrARef = sigPtrA, &sigPtrBRef = sigPtrB, SignalBase<int> &sigPtrARef = sigPtrA, &sigPtrBRef = sigPtrB,
&sigPtrAbstractRef = sigPtrAbstract; &sigPtrAbstractRef = sigPtrAbstract;
sigPtrARef.plug(0);
sigPtrARef.plug(&sigRef); sigPtrARef.plug(&sigRef);
sigPtrBRef.plug(&sigPtrARef); sigPtrBRef.plug(&sigPtrARef);
// TODO here /// Plug the signal.
sigPtrAbstractRef.plug(&sigRef); sigPtrAbstractRef.plug(&sigRef);
sigPtrA.getPtr(); sigPtrA.getPtr();
BOOST_CHECK(true);
try { sigPtrARef.checkCompatibility(); }
catch(...) { }
sigPtrA.needUpdate(5);
// BOOST_CHECK(res);
int ltime = sigPtrA.getTime();
sigPtrA.getPluged(); sigPtrA.getPluged();
sigPtrA(ltime);
sigPtrB.getPtr(); sigPtrB.getPtr();
sigPtrAbstract.getAbstractPtr(); sigPtrAbstract.getAbstractPtr();
...@@ -71,10 +198,18 @@ BOOST_AUTO_TEST_CASE(normal_test) { ...@@ -71,10 +198,18 @@ BOOST_AUTO_TEST_CASE(normal_test) {
tab_D[0] = 1.2; tab_D[0] = 1.2;
tab_D[1] = 3.4; tab_D[1] = 3.4;
sigPtrA.setReference(tab_D, NULL); sigPtrA.setReference(tab_D, NULL);
sigPtrA.access(5);
output_test_stream output;
sigPtrA.display(output);
sigPtrA.setReferenceNonConstant(tab_D, NULL);
sigPtrA.access(5);
sigPtrA.display(output);
// getreference // getreference
sigPtrA.operator=(1.2); sigPtrA.operator=(1.2);
// getconstant // getconstant
sigPtrA.displayDependencies(output);
cout << t << std::endl; cout << t << std::endl;
cout << "Sig = "; cout << "Sig = ";
sigRef.get(cout); sigRef.get(cout);
...@@ -85,6 +220,8 @@ BOOST_AUTO_TEST_CASE(normal_test) { ...@@ -85,6 +220,8 @@ BOOST_AUTO_TEST_CASE(normal_test) {
cout << "SigPtrB = "; cout << "SigPtrB = ";
sigPtrBRef.get(cout); sigPtrBRef.get(cout);
cout << std::endl; cout << std::endl;
sigPtrA.unplug();
} }
BOOST_AUTO_TEST_CASE(plug_signal_string) { BOOST_AUTO_TEST_CASE(plug_signal_string) {
......
// Copyright 2010 Thomas Moulard. // Copyright 2010 Thomas Moulard.
// //
#include <iostream>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <dynamic-graph/signal-time-dependent.h> #include <dynamic-graph/signal-time-dependent.h>
...@@ -72,6 +72,7 @@ BOOST_AUTO_TEST_CASE(signaltimedependent) { ...@@ -72,6 +72,7 @@ BOOST_AUTO_TEST_CASE(signaltimedependent) {
sigDouble_t sig3(sig2 << sig5 << sig6, "Sig3"); sigDouble_t sig3(sig2 << sig5 << sig6, "Sig3");
sigDouble_t sig1(boost::bind(&DummyClass<double>::fun, &pro1, _1, _2), sigDouble_t sig1(boost::bind(&DummyClass<double>::fun, &pro1, _1, _2),
sig2 << sig3, "Sig1"); sig2 << sig3, "Sig1");
sigDouble_t sig7("Sig7");
sig2.setFunction(boost::bind(&DummyClass<std::string>::fun, &pro2, _1, _2)); sig2.setFunction(boost::bind(&DummyClass<std::string>::fun, &pro2, _1, _2));
sig3.setFunction(boost::bind(&DummyClass<double>::fun, &pro3, _1, _2)); sig3.setFunction(boost::bind(&DummyClass<double>::fun, &pro3, _1, _2));
...@@ -209,4 +210,23 @@ BOOST_AUTO_TEST_CASE(signaltimedependent) { ...@@ -209,4 +210,23 @@ BOOST_AUTO_TEST_CASE(signaltimedependent) {
sig1.needUpdate(6); sig1.needUpdate(6);
sig1.needUpdate(6); sig1.needUpdate(6);
output_test_stream output;
sig1.writeGraph(output);
BOOST_CHECK(output.is_equal(""));
sig1.removeDependency(sig3);
BOOST_CHECK(true);
const double & avalue =sig1(6);
output << avalue;
BOOST_CHECK(true);
/// Verify check compatibility
try {
sig1.checkCompatibility();
}
// catch(double e)
catch(...)
{
std::cout << "Message: test \n";
}
BOOST_CHECK(true);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment