...
 
Commits (5)
Subproject commit 811bfc472940425a0a96cca7e01331f6cf68fced Subproject commit 61344038b1352d5a8de1e20db710c83be805d2eb
...@@ -52,36 +52,6 @@ inline boost::any DefaultCastRegisterer<double>::cast(std::istringstream &iss) { ...@@ -52,36 +52,6 @@ inline boost::any DefaultCastRegisterer<double>::cast(std::istringstream &iss) {
} }
} }
/* Specialize Matrix and Vector traces. */
template <>
void DefaultCastRegisterer<dynamicgraph::Vector>::trace(
const boost::any &object, std::ostream &os) {
const dynamicgraph::Vector &v = boost::any_cast<dynamicgraph::Vector>(object);
for (int i = 0; i < v.size(); ++i) {
os << "\t" << v(i);
}
}
template <>
void DefaultCastRegisterer<dynamicgraph::Matrix>::trace(
const boost::any &object, std::ostream &os) {
const dynamicgraph::Matrix &m = boost::any_cast<dynamicgraph::Matrix>(object);
for (int i = 0; i < m.rows(); ++i)
for (int j = 0; j < m.cols(); ++j) {
os << "\t" << m(i, j);
}
}
// for std::string, do not check failure. If input stream contains an
// empty string, iss.fail() returns true and an exception is thrown
template <>
inline boost::any
DefaultCastRegisterer<std::string>::cast(std::istringstream &iss) {
std::string inst(iss.str());
return inst;
}
// for std::string, do not add std::endl at the end of the stream. // for std::string, do not add std::endl at the end of the stream.
template <> template <>
inline void DefaultCastRegisterer<std::string>::disp(const boost::any &object, inline void DefaultCastRegisterer<std::string>::disp(const boost::any &object,
......
...@@ -42,12 +42,6 @@ struct MyEntity : public dynamicgraph::Entity { ...@@ -42,12 +42,6 @@ struct MyEntity : public dynamicgraph::Entity {
signalRegistration(m_sigdSIN << m_sigdTimeDepSOUT << m_sigdTwoTimeDepSOUT); signalRegistration(m_sigdSIN << m_sigdTimeDepSOUT << m_sigdTwoTimeDepSOUT);
} }
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) { double &update(double &res, const int &inTime) {
const double &aDouble = m_sigdSIN(inTime); const double &aDouble = m_sigdSIN(inTime);
res = aDouble; res = aDouble;
...@@ -64,8 +58,8 @@ BOOST_AUTO_TEST_CASE(test_tracer) { ...@@ -64,8 +58,8 @@ BOOST_AUTO_TEST_CASE(test_tracer) {
TracerRealTime &atracer = *dynamic_cast<TracerRealTime *>( TracerRealTime &atracer = *dynamic_cast<TracerRealTime *>(
FactoryStorage::getInstance()->newEntity("TracerRealTime", "my-tracer")); FactoryStorage::getInstance()->newEntity("TracerRealTime", "my-tracer"));
Entity &entity = MyEntity &entity = *dynamic_cast<MyEntity *>(
*FactoryStorage::getInstance()->newEntity("MyEntity", "my-entity"); FactoryStorage::getInstance()->newEntity("MyEntity", "my-entity"));
std::string rootdir("/tmp"); std::string rootdir("/tmp");
std::string basename("my-tracer"); std::string basename("my-tracer");
...@@ -80,6 +74,8 @@ BOOST_AUTO_TEST_CASE(test_tracer) { ...@@ -80,6 +74,8 @@ BOOST_AUTO_TEST_CASE(test_tracer) {
/// Add trace by name /// Add trace by name
SignalBase<int> &aSignal = entity.getSignal("out2double"); SignalBase<int> &aSignal = entity.getSignal("out2double");
entity.m_sigdTwoTimeDepSOUT.recompute(2);
Signal<double, int> &aSignalInt = Signal<double, int> &aSignalInt =
*(dynamic_cast<Signal<double, int> *>(&entity.getSignal("in_double"))); *(dynamic_cast<Signal<double, int> *>(&entity.getSignal("in_double")));
......
...@@ -15,7 +15,19 @@ ...@@ -15,7 +15,19 @@
using boost::test_tools::output_test_stream; using boost::test_tools::output_test_stream;
using namespace dynamicgraph; using namespace dynamicgraph;
BOOST_AUTO_TEST_CASE(exception_abstract_param) {
/// Test param from Exception
/// Default constructor
ExceptionAbstract::Param aParamSimple;
/// Advanced constructor
ExceptionAbstract::Param aParam(60, "function_test", "my_file");
aParamSimple.initCopy(aParam);
}
BOOST_AUTO_TEST_CASE(exception_abstract) { BOOST_AUTO_TEST_CASE(exception_abstract) {
/// Test exception abstract with a simple message
std::string msg_aea("Test exception abstract"); std::string msg_aea("Test exception abstract");
ExceptionAbstract aEA(10, msg_aea); ExceptionAbstract aEA(10, msg_aea);
...@@ -27,9 +39,22 @@ BOOST_AUTO_TEST_CASE(exception_abstract) { ...@@ -27,9 +39,22 @@ BOOST_AUTO_TEST_CASE(exception_abstract) {
output << aEA; output << aEA;
BOOST_CHECK( BOOST_CHECK(
output.is_equal("AbstractError [#10]: Test exception abstract\n")); output.is_equal("AbstractError [#10]: Test exception abstract\n"));
}
std::string msg_aet("Test exception abstract"); BOOST_AUTO_TEST_CASE(exception_traces) {
ExceptionTraces aET(ExceptionTraces::GENERIC, msg_aet);
std::string msg_aet("Test exception traces simple");
ExceptionTraces aET(ExceptionTraces::GENERIC, msg_aet);
output_test_stream output;
output << aET; output << aET;
BOOST_CHECK(
output.is_equal("TracesError [#300]: Test exception traces simple\n"));
/// Test exception traces with a format.
int a = 2, b = 3;
std::string msg_aet2("Test exception traces ");
ExceptionTraces aET2(ExceptionTraces::GENERIC, msg_aet2, "(%d,%d)", a, b);
output << aET2;
BOOST_CHECK(
output.is_equal("TracesError [#300]: Test exception traces (2,3)\n"));
} }
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include <assert.h> #include <assert.h>
#include <boost/test/output_test_stream.hpp> #include <boost/test/output_test_stream.hpp>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <iostream>
#define BOOST_TEST_MODULE signal_array #define BOOST_TEST_MODULE signal_array
...@@ -144,11 +145,93 @@ BOOST_AUTO_TEST_CASE(test_cast_helper) { ...@@ -144,11 +145,93 @@ BOOST_AUTO_TEST_CASE(test_cast_helper) {
iss.str("1"); iss.str("1");
defaultCR.cast(iss); defaultCR.cast(iss);
bool res = false;
try { try {
std::istringstream iss_fail; std::istringstream iss_fail;
iss.str("test"); iss.str("test");
defaultCR.cast(iss_fail); defaultCR.cast(iss_fail);
} catch (ExceptionSignal &e) { } catch (ExceptionSignal &e) {
// Take int, not string // Take int, not string
res = true;
}
BOOST_CHECK(res);
/// Test cast register with Vector
output_test_stream output;
dynamicgraph::Vector avec;
DefaultCastRegisterer<dynamicgraph::Vector> defaultVR;
avec.resize(4);
avec[0] = 1.0;
avec[1] = 2.0;
avec[2] = 3.0;
avec[3] = 4.0;
res = true;
try {
defaultVR.trace(avec, output);
} catch (ExceptionSignal &e) {
/// Exception in case of wrong cast.
/// This should not happen.
res = false;
}
BOOST_CHECK(res);
/// Test cast register with Matrix
dynamicgraph::Matrix amatrix;
DefaultCastRegisterer<dynamicgraph::Matrix> defaultMR;
amatrix.resize(2, 2);
amatrix(0, 0) = 0.0;
amatrix(0, 1) = 1.0;
amatrix(1, 0) = 2.0;
amatrix(1, 1) = 3.0;
res = true;
try {
defaultMR.trace(amatrix, output);
} catch (ExceptionSignal &e) {
/// Exception in case of wrong cast.
/// This should happen
res = false;
} }
BOOST_CHECK(res);
std::istringstream aiss("test");
DefaultCastRegisterer<std::string> defaultSR;
boost::any aTest = defaultSR.cast(aiss);
}
BOOST_AUTO_TEST_CASE(signal_caster_basics) {
/// Get the singleton on registered types.
SignalCaster *asig_caster = SignalCaster::getInstance();
/// List the registered types.
std::vector<std::string> amap = asig_caster->listTypenames();
for (std::vector<std::string>::iterator it = amap.begin(); it != amap.end();
++it)
std::cout << "signal_caster:listTypename: " << *it << std::endl;
/// Unregister a type
asig_caster->unregisterCast(typeid(double));
/// Unregister the type a second time to generate exception
bool res= false;
try {
asig_caster->unregisterCast(typeid(double));
} catch (ExceptionSignal &aes)
{
res = (aes.getCode() == ExceptionSignal::GENERIC);
}
BOOST_CHECK(res);
/// Get the type cast to generate exception
res= false;
double ad=2.0;
output_test_stream output;
try {
asig_caster->disp(ad,output);
} catch (ExceptionSignal &aes)
{
res = (aes.getCode() == ExceptionSignal::BAD_CAST);
}
BOOST_CHECK(res);
asig_caster->destroy();
BOOST_CHECK(true);
} }
...@@ -139,6 +139,15 @@ BOOST_AUTO_TEST_CASE(standard_double_registerer) { ...@@ -139,6 +139,15 @@ BOOST_AUTO_TEST_CASE(standard_double_registerer) {
BOOST_AUTO_TEST_CASE(custom_vector_registerer) { BOOST_AUTO_TEST_CASE(custom_vector_registerer) {
dynamicgraph::Signal<dynamicgraph::Vector, int> myVectorSignal("vector"); dynamicgraph::Signal<dynamicgraph::Vector, int> myVectorSignal("vector");
/// Create a second local vector registerer to generate an exception.
bool res = false;
try {
EigenCastRegisterer_V myVectorCast2;
} catch (const ExceptionSignal &aes) {
res = (aes.getCode() == ExceptionSignal::GENERIC);
}
// BOOST_CHECK(res);
// Print the signal name. // Print the signal name.
{ {
output_test_stream output; output_test_stream output;
...@@ -214,6 +223,13 @@ BOOST_AUTO_TEST_CASE(custom_vector_registerer) { ...@@ -214,6 +223,13 @@ BOOST_AUTO_TEST_CASE(custom_vector_registerer) {
} catch (ExceptionSignal &e) { } catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[-1] != \")\""; std::cout << "Test passed : ss[-1] != \")\"";
} }
try {
output_test_stream output;
myVectorSignal.trace(output);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[-1] != \")\"";
}
} }
BOOST_AUTO_TEST_CASE(custom_matrix_registerer) { BOOST_AUTO_TEST_CASE(custom_matrix_registerer) {
......
...@@ -155,6 +155,7 @@ BOOST_AUTO_TEST_CASE(normal_test) { ...@@ -155,6 +155,7 @@ BOOST_AUTO_TEST_CASE(normal_test) {
BOOST_CHECK(true); BOOST_CHECK(true);
sigPtrA.setFunction(boost::bind(&DummyClass<double>::fun, &pro3, _1, _2)); sigPtrA.setFunction(boost::bind(&DummyClass<double>::fun, &pro3, _1, _2));
sigPtrA.recompute(3);
/// Plugging signal. /// Plugging signal.
SignalBase<int> &sigRef = sig, sigBase("sigBase"); SignalBase<int> &sigRef = sig, sigBase("sigBase");
...@@ -261,6 +262,9 @@ BOOST_AUTO_TEST_CASE(plug_signal_string) { ...@@ -261,6 +262,9 @@ BOOST_AUTO_TEST_CASE(plug_signal_string) {
Signal<std::string, int> outSig("output"); Signal<std::string, int> outSig("output");
SignalPtr<std::string, int> inSig(NULL, "input"); SignalPtr<std::string, int> inSig(NULL, "input");
Signal<dynamicgraph::Vector, int> outSigVec("outputVec");
SignalPtr<dynamicgraph::Vector, int> inSigVec(NULL, "inputVec");
std::string str("two words"); std::string str("two words");
outSig.setConstant(str); outSig.setConstant(str);
inSig.plug(&outSig); inSig.plug(&outSig);
...@@ -270,6 +274,20 @@ BOOST_AUTO_TEST_CASE(plug_signal_string) { ...@@ -270,6 +274,20 @@ BOOST_AUTO_TEST_CASE(plug_signal_string) {
std::string res(os1.str()); std::string res(os1.str());
BOOST_CHECK(res == str); BOOST_CHECK(res == str);
dynamicgraph::Vector aVec;
aVec.resize(5);
aVec(0) = 1.0;
aVec(1) = 2.0;
aVec(2) = 3.0;
aVec(3) = 4.0;
aVec(4) = 5.0;
outSigVec.setConstant(aVec);
inSigVec.plug(&outSigVec);
inSigVec.recompute(1);
output_test_stream output;
inSigVec.get(output);
BOOST_CHECK(output.is_equal("1\n2\n3\n4\n5\n"));
Signal<std::string, int> s("signal"); Signal<std::string, int> s("signal");
std::ostringstream os2; std::ostringstream os2;
s.setConstant(str); s.setConstant(str);
......
...@@ -64,7 +64,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) { ...@@ -64,7 +64,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) {
bool res = false; bool res = false;
try { try {
int aInt(anet); int aInt(anet);
output << aInt;
} catch (const dg::ExceptionAbstract &aea) { } catch (const dg::ExceptionAbstract &aea) {
output << aea.getExceptionName(); output << aea.getExceptionName();
output2 << aea.what(); output2 << aea.what();
...@@ -79,7 +78,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) { ...@@ -79,7 +78,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) {
res = false; res = false;
try { try {
bool abool(anet); bool abool(anet);
output << abool;
} catch (const dg::ExceptionAbstract &aea) { } catch (const dg::ExceptionAbstract &aea) {
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS); res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
} }
...@@ -90,7 +88,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) { ...@@ -90,7 +88,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) {
res = false; res = false;
try { try {
unsigned int aint(anet); unsigned int aint(anet);
output << aint;
} catch (const dg::ExceptionAbstract &aea) { } catch (const dg::ExceptionAbstract &aea) {
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS); res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
} }
...@@ -101,7 +98,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) { ...@@ -101,7 +98,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) {
res = false; res = false;
try { try {
double adouble(anet); double adouble(anet);
output << adouble;
} catch (const dg::ExceptionAbstract &aea) { } catch (const dg::ExceptionAbstract &aea) {
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS); res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
} }
...@@ -112,7 +108,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) { ...@@ -112,7 +108,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) {
res = false; res = false;
try { try {
float afloat(anet); float afloat(anet);
output << afloat;
} catch (const dg::ExceptionAbstract &aea) { } catch (const dg::ExceptionAbstract &aea) {
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS); res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
} }
...@@ -123,7 +118,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) { ...@@ -123,7 +118,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) {
res = false; res = false;
try { try {
std::string astring(anet); std::string astring(anet);
output << astring;
} catch (const dg::ExceptionAbstract &aea) { } catch (const dg::ExceptionAbstract &aea) {
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS); res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
} }
...@@ -135,7 +129,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) { ...@@ -135,7 +129,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) {
try { try {
dg::Vector avector; dg::Vector avector;
avector = anet; avector = anet;
output << avector;
} catch (const dg::ExceptionAbstract &aea) { } catch (const dg::ExceptionAbstract &aea) {
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS); res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
} }
...@@ -147,7 +140,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) { ...@@ -147,7 +140,6 @@ BOOST_AUTO_TEST_CASE(value_exceptions) {
try { try {
Eigen::MatrixXd amatrixXd; Eigen::MatrixXd amatrixXd;
amatrixXd = anet; amatrixXd = anet;
output << amatrixXd;
} catch (const dg::ExceptionAbstract &aea) { } catch (const dg::ExceptionAbstract &aea) {
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS); res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
} }
......