Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • cberge/dynamic-graph
  • ostasse/dynamic-graph
  • gsaurel/dynamic-graph
  • stack-of-tasks/dynamic-graph
4 results
Select Git revision
Show changes
Showing
with 2107 additions and 0 deletions
import "tracer"
new Tracer tracer
tracer.open /tmp debug.dat
tracer.add tracer.triger
tracer.start
tracer.stop
tracer.clear
tracer.record
tracer.close
/* Copyright 2019, LAAS-CNRS
*
* Olivier Stasse
*
* See LICENSE file
*
*/
#include <iostream>
#include <sstream>
#define ENABLE_RT_LOG
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/logger.h>
#include <dynamic-graph/real-time-logger.h>
#include "dynamic-graph/factory.h"
#include "dynamic-graph/pool.h"
#define BOOST_TEST_MODULE debug - logger
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#endif
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/thread/thread.hpp>
using boost::test_tools::output_test_stream;
namespace dynamicgraph {
class CustomEntity : public Entity {
public:
static const std::string CLASS_NAME;
virtual const std::string &getClassName() const { return CLASS_NAME; }
explicit CustomEntity(const std::string &n) : Entity(n) {
logger_.setTimeSample(0.001);
logger_.setStreamPrintPeriod(0.005);
logger_.setVerbosity(VERBOSITY_ALL);
LoggerVerbosity alv = logger_.getVerbosity();
BOOST_CHECK(alv == VERBOSITY_ALL);
}
~CustomEntity() {}
void testDebugTrace() {
sendMsg("This is a message of level MSG_TYPE_DEBUG", MSG_TYPE_DEBUG);
sendMsg("This is a message of level MSG_TYPE_INFO", MSG_TYPE_INFO);
sendMsg("This is a message of level MSG_TYPE_WARNING", MSG_TYPE_WARNING);
sendMsg("This is a message of level MSG_TYPE_ERROR", MSG_TYPE_ERROR);
sendMsg("This is a message of level MSG_TYPE_DEBUG_STREAM",
MSG_TYPE_DEBUG_STREAM);
sendMsg("This is a message of level MSG_TYPE_INFO_STREAM",
MSG_TYPE_INFO_STREAM);
sendMsg("This is a message of level MSG_TYPE_WARNING_STREAM",
MSG_TYPE_WARNING_STREAM);
sendMsg("This is a message of level MSG_TYPE_ERROR_STREAM",
MSG_TYPE_ERROR_STREAM);
logger_.countdown();
}
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
} // namespace dynamicgraph
BOOST_AUTO_TEST_CASE(debug_logger_wrong_initialization) {
dynamicgraph::RealTimeLogger::instance();
BOOST_CHECK_EQUAL(dynamicgraph::CustomEntity::CLASS_NAME, "CustomEntity");
dynamicgraph::CustomEntity &entity =
*(dynamic_cast<dynamicgraph::CustomEntity *>(
dynamicgraph::FactoryStorage::getInstance()->newEntity(
"CustomEntity", "my-entity-2")));
for (unsigned int i = 0; i < 1000; i++) {
entity.testDebugTrace();
}
dynamicgraph::RealTimeLogger::destroy();
}
/* Copyright 2019, LAAS-CNRS
*
* Olivier Stasse
*
* See LICENSE file
*
*/
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <iostream>
#include <sstream>
#include "dynamic-graph/factory.h"
#include "dynamic-graph/pool.h"
#define ENABLE_RT_LOG
#include <dynamic-graph/logger.h>
#include <dynamic-graph/real-time-logger.h>
#define BOOST_TEST_MODULE debug - logger
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#endif
#include <boost/test/unit_test.hpp>
using boost::test_tools::output_test_stream;
namespace dynamicgraph {
class CustomEntity : public Entity {
public:
static const std::string CLASS_NAME;
virtual const std::string &getClassName() const { return CLASS_NAME; }
explicit CustomEntity(const std::string &n) : Entity(n) {
logger_.setTimeSample(0.001);
logger_.setStreamPrintPeriod(0.005);
logger_.setVerbosity(VERBOSITY_NONE);
BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_NONE);
BOOST_CHECK(logger_.stream(MSG_TYPE_DEBUG).isNull());
BOOST_CHECK(logger_.stream(MSG_TYPE_INFO).isNull());
BOOST_CHECK(logger_.stream(MSG_TYPE_WARNING).isNull());
BOOST_CHECK(logger_.stream(MSG_TYPE_ERROR).isNull());
logger_.setVerbosity(VERBOSITY_ERROR);
BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_ERROR);
BOOST_CHECK(logger_.stream(MSG_TYPE_DEBUG).isNull());
BOOST_CHECK(logger_.stream(MSG_TYPE_INFO).isNull());
BOOST_CHECK(logger_.stream(MSG_TYPE_WARNING).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR).isNull());
logger_.setVerbosity(VERBOSITY_WARNING_ERROR);
BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_WARNING_ERROR);
BOOST_CHECK(logger_.stream(MSG_TYPE_DEBUG).isNull());
BOOST_CHECK(logger_.stream(MSG_TYPE_INFO).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR).isNull());
logger_.setVerbosity(VERBOSITY_INFO_WARNING_ERROR);
BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_INFO_WARNING_ERROR);
BOOST_CHECK(logger_.stream(MSG_TYPE_DEBUG).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_INFO).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR).isNull());
logger_.setVerbosity(VERBOSITY_ALL);
BOOST_CHECK_EQUAL(logger_.getVerbosity(), VERBOSITY_ALL);
BOOST_CHECK(!logger_.stream(MSG_TYPE_DEBUG).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_INFO).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_WARNING).isNull());
BOOST_CHECK(!logger_.stream(MSG_TYPE_ERROR).isNull());
}
~CustomEntity() {}
void testDebugTrace() {
logger_.stream(MSG_TYPE_DEBUG)
<< "This is a message of level MSG_TYPE_DEBUG\n";
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_INFO)
<< "This is a message of level MSG_TYPE_INFO\n";
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_WARNING)
<< "This is a message of level MSG_TYPE_WARNING\n";
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_ERROR)
<< "This is a message of level MSG_TYPE_ERROR\n";
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_DEBUG_STREAM)
<< "This is a message of level MSG_TYPE_DEBUG_STREAM\n";
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_INFO_STREAM)
<< "This is a message of level MSG_TYPE_INFO_STREAM\n";
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_WARNING_STREAM)
<< "This is a message of level MSG_TYPE_WARNING_STREAM\n";
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_ERROR_STREAM)
<< "This is a message of level MSG_TYPE_ERROR_STREAM\n";
/* Add test toString */
dynamicgraph::RealTimeLogger::instance().spinOnce();
double q = 1.0;
logger_.stream() << "Value to display: " + toString(q) << '\n';
dynamicgraph::RealTimeLogger::instance().spinOnce();
std::vector<double> vq;
vq.resize(3);
vq[0] = 1.0;
vq[1] = 2.0;
vq[2] = 3.0;
logger_.stream(MSG_TYPE_INFO)
<< "Value to display: " << toString(vq) << '\n';
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.stream(MSG_TYPE_INFO)
<< "Value to display: " << toString(vq, 3, 10) << '\n';
dynamicgraph::RealTimeLogger::instance().spinOnce();
Eigen::Matrix<double, 3, 3> an_eig_m;
an_eig_m.setOnes();
logger_.stream(MSG_TYPE_INFO)
<< "Value to display: " << toString(an_eig_m) << '\n';
dynamicgraph::RealTimeLogger::instance().spinOnce();
logger_.countdown();
}
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
} // namespace dynamicgraph
BOOST_AUTO_TEST_CASE(debug_logger) {
std::ofstream of;
dynamicgraph::RealTimeLogger::instance();
of.open("/tmp/dg-LOGS.txt", std::ofstream::out | std::ofstream::app);
dgADD_OSTREAM_TO_RTLOG(of);
BOOST_CHECK_EQUAL(dynamicgraph::CustomEntity::CLASS_NAME, "CustomEntity");
dynamicgraph::CustomEntity &entity =
*(dynamic_cast<dynamicgraph::CustomEntity *>(
dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity",
"my-entity")));
entity.setTimeSample(0.002);
BOOST_CHECK_EQUAL(entity.getTimeSample(), 0.002);
entity.setStreamPrintPeriod(0.002);
BOOST_CHECK_EQUAL(entity.getStreamPrintPeriod(), 0.002);
for (unsigned int i = 0; i < 10000; i++) {
entity.testDebugTrace();
}
dynamicgraph::RealTimeLogger::destroy();
}
/* Copyright 2019, LAAS-CNRS
*
* Olivier Stasse
*
*/
#include <dynamic-graph/command.h>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/signal-ptr.h>
#include <dynamic-graph/signal-time-dependent.h>
#include <dynamic-graph/tracer-real-time.h>
#include <iostream>
#define BOOST_TEST_MODULE debug - tracer
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#endif
#include <boost/test/unit_test.hpp>
using boost::test_tools::output_test_stream;
namespace dynamicgraph {
struct MyEntity : public dynamicgraph::Entity {
static const std::string CLASS_NAME;
dynamicgraph::Signal<double, int> m_sigdSIN;
dynamicgraph::SignalTimeDependent<double, int> m_sigdTimeDepSOUT;
dynamicgraph::SignalTimeDependent<double, int> m_sigdTwoTimeDepSOUT;
explicit MyEntity(const std::string &name)
: Entity(name),
m_sigdSIN("MyEntity(" + name + ")::input(double)::in_double"),
m_sigdTimeDepSOUT(boost::bind(&MyEntity::update, this, _1, _2),
m_sigdSIN,
"MyEntity(" + name + ")::input(double)::out_double"),
m_sigdTwoTimeDepSOUT(
boost::bind(&MyEntity::update, this, _1, _2), m_sigdSIN,
"MyEntity(" + name + ")::input(double)::out2double")
{
signalRegistration(m_sigdSIN << m_sigdTimeDepSOUT << m_sigdTwoTimeDepSOUT);
}
double &update(double &res, const int &inTime) {
const double &aDouble = m_sigdSIN(inTime);
res = aDouble;
return res;
}
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(MyEntity, "MyEntity");
} // namespace dynamicgraph
BOOST_AUTO_TEST_CASE(test_tracer) {
using namespace dynamicgraph;
// Creates a tracer.
TracerRealTime &atracer = *dynamic_cast<TracerRealTime *>(
FactoryStorage::getInstance()->newEntity("TracerRealTime", "my-tracer"));
MyEntity &entity = *dynamic_cast<MyEntity *>(
FactoryStorage::getInstance()->newEntity("MyEntity", "my-entity"));
std::string rootdir("/tmp");
std::string basename("my-tracer");
std::string suffix(".dat");
atracer.setBufferSize(1 << 14);
// Check that an exception is thrown if the filename is invalid.
atracer.openFiles(rootdir, "invalid/filename", suffix);
BOOST_CHECK_THROW(
atracer.addSignalToTraceByName("my-entity.out_double", "output"),
ExceptionTraces);
// Test openfiles
atracer.openFiles(rootdir, basename, suffix);
// Add trace by name
atracer.addSignalToTraceByName("my-entity.out_double", "output");
/// Add trace by name
SignalBase<int> &out_double = entity.getSignal("out_double");
SignalBase<int> &out_double_2 = entity.getSignal("out2double");
Signal<double, int> &in_double =
*(dynamic_cast<Signal<double, int> *>(&entity.getSignal("in_double")));
in_double.setConstant(1.5);
atracer.start();
std::string emptybuf_cmd_str("empty");
command::Command *acmd = atracer.getNewStyleCommand(emptybuf_cmd_str);
acmd->execute();
for (int i = 0; i < 1000; i++) {
in_double.setTime(i);
out_double.recompute(i);
out_double_2.recompute(i);
atracer.recordTrigger(i, i);
}
output_test_stream output;
atracer.display(output);
atracer.stop();
atracer.trace();
atracer.clearSignalToTrace();
atracer.closeFiles();
acmd->execute();
atracer.record();
BOOST_CHECK(output.is_equal(
"TracerRealTime my-tracer [mode=play] : \n"
" - Dep list: \n"
" -> MyEntity(my-entity)::input(double)::out_double (in output)"
" [8Ko/16Ko] \n"));
}
/* Copyright 2019, LAAS-CNRS
*
* Olivier Stasse
*
*/
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <iostream>
#include <sstream>
#include "dynamic-graph/factory.h"
#include "dynamic-graph/pool.h"
#define VP_DEBUG 1
#define VP_DEBUG_MODE 50
#define VP_TEMPLATE_DEBUG_MODE 50
#include <dynamic-graph/debug.h>
#define BOOST_TEST_MODULE debug - trace
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#endif
#include <boost/test/unit_test.hpp>
using boost::test_tools::output_test_stream;
namespace dynamicgraph {
class CustomEntity : public Entity {
public:
static const std::string CLASS_NAME;
virtual const std::string &getClassName() const { return CLASS_NAME; }
explicit CustomEntity(const std::string &n) : Entity(n) {
dynamicgraph::dgDEBUGFLOW.openFile("/tmp/dynamic-graph-traces.txt");
}
~CustomEntity() {
dynamicgraph::dgDEBUGFLOW.closeFile("/tmp/dynamic-graph-traces.txt");
}
void testDebugTrace() {
/// Test debugging information when entering the code.
dgDEBUGIN(5);
/// Intermediate test.
dgDEBUGINOUT(5);
dgDEBUG(5) << "Here is a test" << std::endl;
/// Test debugging information when going out of the code.
dgDEBUGOUT(5);
}
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
} // namespace dynamicgraph
BOOST_AUTO_TEST_CASE(testDebugTrace) {
BOOST_CHECK_EQUAL(dynamicgraph::CustomEntity::CLASS_NAME, "CustomEntity");
dynamicgraph::CustomEntity *ptr_entity =
(dynamic_cast<dynamicgraph::CustomEntity *>(
dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity",
"my-entity")));
dynamicgraph::CustomEntity &entity = *ptr_entity;
entity.testDebugTrace();
/// Copy the debug file into the oss_debug_file
output_test_stream output;
std::fstream the_debug_file;
the_debug_file.open(dynamicgraph::DebugTrace::DEBUG_FILENAME_DEFAULT,
std::ios::in);
// Extract the filename and this source file from the output
std::string astr;
std::ostringstream oss_debug_file;
while (std::getline(the_debug_file, astr)) {
std::size_t found = astr.find(":");
std::string asubstr = astr.substr(found + 1, astr.length());
found = asubstr.find(":");
std::string asubstr2 = asubstr.substr(found + 1, astr.length());
oss_debug_file << asubstr2;
}
the_debug_file.close();
// Compare with the strings put inside this source file
std::string str_to_test =
"# In {"
"# In/Out { }"
"Here is a test"
"# Out }";
bool two_sub_string_identical;
// Make comparisons.
two_sub_string_identical = str_to_test == oss_debug_file.str();
BOOST_CHECK(two_sub_string_identical);
delete ptr_entity;
}
/* Copyright 2019, LAAS-CNRS
*
* Olivier Stasse
*
*/
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/signal-ptr.h>
#include <dynamic-graph/signal-time-dependent.h>
#include <dynamic-graph/tracer.h>
#include <iostream>
#define BOOST_TEST_MODULE debug - tracer
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#endif
#include <boost/test/unit_test.hpp>
namespace dynamicgraph {
struct MyEntity : public dynamicgraph::Entity {
static const std::string CLASS_NAME;
dynamicgraph::Signal<double, int> m_sigdSIN;
dynamicgraph::SignalTimeDependent<double, int> m_sigdTimeDepSOUT;
dynamicgraph::SignalTimeDependent<Vector, int> m_sigVTimeDepSOUT;
dynamicgraph::SignalTimeDependent<double, int> m_sigdTwoTimeDepSOUT;
explicit MyEntity(const std::string &name)
: Entity(name),
m_sigdSIN("MyEntity(" + name + ")::input(double)::in_double"),
m_sigdTimeDepSOUT(boost::bind(&MyEntity::update, this, _1, _2),
m_sigdSIN,
"MyEntity(" + name + ")::input(double)::out_double"),
m_sigVTimeDepSOUT(boost::bind(&MyEntity::updateVector, this, _1, _2),
m_sigdSIN,
"MyEntity(" + name + ")::input(vector)::out_vector"),
m_sigdTwoTimeDepSOUT(
boost::bind(&MyEntity::update, this, _1, _2), m_sigdSIN,
"MyEntity(" + name + ")::input(double)::out2double")
{
signalRegistration(m_sigdSIN << m_sigdTimeDepSOUT << m_sigVTimeDepSOUT
<< 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) {
const double &aDouble = m_sigdSIN(inTime);
res = aDouble;
return res;
}
Vector &updateVector(Vector &res, const int &inTime) {
const double &aDouble = m_sigdSIN(inTime);
res.resize(2);
res << aDouble, 2 * aDouble;
return res;
}
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(MyEntity, "MyEntity");
} // namespace dynamicgraph
BOOST_AUTO_TEST_CASE(test_tracer) {
using dynamicgraph::Vector;
// Creates a tracer.
dynamicgraph::Tracer &atracer = *dynamic_cast<dynamicgraph::Tracer *>(
dynamicgraph::FactoryStorage::getInstance()->newEntity("Tracer",
"my-tracer"));
dynamicgraph::Entity &entity =
*dynamicgraph::FactoryStorage::getInstance()->newEntity("MyEntity",
"my-entity");
std::string rootdir("/tmp");
std::string basename("my-tracer");
std::string suffix(".dat");
/// Test openfiles
atracer.openFiles(rootdir, basename, suffix);
/// Add trace by name
atracer.addSignalToTraceByName("my-entity.out_double", "output");
/// Add trace by name
atracer.addSignalToTraceByName("my-entity.out_vector", "output-vector");
dynamicgraph::SignalBase<int> &aSignal = entity.getSignal("out2double");
dynamicgraph::Signal<double, int> &aSignalInt =
*(dynamic_cast<dynamicgraph::Signal<double, int> *>(
&entity.getSignal("in_double")));
dynamicgraph::Signal<Vector, int> &aSignalVector =
*(dynamic_cast<dynamicgraph::Signal<Vector, int> *>(
&entity.getSignal("out_vector")));
/// Add trace by signal object
atracer.addSignalToTrace(aSignal, "output2");
aSignalInt.setConstant(1.5);
atracer.start();
for (int i = 0; i < 1000; i++) {
aSignal.setTime(i);
aSignalInt.access(i);
aSignalInt.setTime(i);
aSignalVector.recompute(i);
aSignalVector.setTime(i);
atracer.recordTrigger(i, i);
}
atracer.stop();
atracer.clearSignalToTrace();
atracer.closeFiles();
atracer.record();
}
/* Copyright 2010-2019 LAAS, CNRS
* Thomas Moulard.
*
*/
#define ENABLE_RT_LOG
#include <dynamic-graph/command-direct-getter.h>
#include <dynamic-graph/command-direct-setter.h>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/real-time-logger.h>
#include <dynamic-graph/signal-ptr.h>
#include <dynamic-graph/signal-time-dependent.h>
#include <sstream>
#include "dynamic-graph/factory.h"
#include "dynamic-graph/pool.h"
#define BOOST_TEST_MODULE entity
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#endif
#include <boost/test/unit_test.hpp>
using boost::test_tools::output_test_stream;
namespace dynamicgraph {
class CustomEntity : public Entity {
public:
dynamicgraph::SignalPtr<double, int> m_sigdSIN, m_sigdSIN2;
dynamicgraph::SignalTimeDependent<double, int> m_sigdTimeDepSOUT;
static const std::string CLASS_NAME;
virtual const std::string &getClassName() const { return CLASS_NAME; }
explicit CustomEntity(const std::string &n)
: Entity(n),
m_sigdSIN(NULL, "CustomEntity(" + name + ")::input(double)::in_double"),
m_sigdSIN2(NULL,
"CustomEntity(" + name + ")::input(double)::in_double"),
m_sigdTimeDepSOUT(
boost::bind(&CustomEntity::update, this, _1, _2), m_sigdSIN,
"CustomEntity(" + name + ")::input(double)::out_double"),
m_value(0.0) {}
~CustomEntity() { entityDeregistration(); }
void addSignal() {
signalRegistration(m_sigdSIN << m_sigdTimeDepSOUT);
/// Try a second time to generate an exception
try {
signalRegistration(m_sigdSIN2 << m_sigdTimeDepSOUT);
} catch (ExceptionFactory &aef) {
BOOST_CHECK_EQUAL(aef.getCode(),
dynamicgraph::ExceptionFactory::SIGNAL_CONFLICT);
}
}
void rmValidSignal() {
signalDeregistration("in_double");
signalDeregistration("out_double");
}
double &update(double &res, const int &inTime) {
const double &aDouble = m_sigdSIN(inTime);
res = aDouble;
return res;
}
public:
double m_value;
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
} // namespace dynamicgraph
BOOST_AUTO_TEST_CASE(constructor) {
namespace dg = dynamicgraph;
namespace dgc = dynamicgraph::command;
BOOST_CHECK_EQUAL(dg::CustomEntity::CLASS_NAME, "CustomEntity");
dg::Entity &entity = *dg::FactoryStorage::getInstance()->newEntity(
"CustomEntity", "my-entity");
BOOST_CHECK_EQUAL(entity.getName(), "my-entity");
BOOST_CHECK_EQUAL(entity.getClassName(), dg::CustomEntity::CLASS_NAME);
dg::CustomEntity entity2("");
// Test Commands
dgc::DirectGetter<dg::CustomEntity, double> a_direct_getter(
entity2, &entity2.m_value,
dgc::docDirectGetter("Get value m_value", "double"));
dgc::DirectSetter<dg::CustomEntity, double> a_direct_setter(
entity2, &entity2.m_value,
dgc::docDirectSetter("Set value m_value", "double"));
dgc::Value aValue(2.0);
std::vector<dgc::Value> values;
values.push_back(aValue);
a_direct_setter.setParameterValues(values);
a_direct_setter.execute();
a_direct_getter.execute();
output_test_stream output;
output << entity2.m_value;
output << entity2;
entity.getDocString();
}
BOOST_AUTO_TEST_CASE(signal) {
dynamicgraph::Entity &entity =
dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
// Non const getter.
try {
entity.getSignal("I do not exist");
BOOST_ERROR("Should never happen.");
} catch (const dynamicgraph::ExceptionFactory &exception) {
BOOST_CHECK_EQUAL(exception.getCode(),
dynamicgraph::ExceptionFactory::UNREFERED_SIGNAL);
}
// Const getter.
try {
const dynamicgraph::Entity &entityConst = entity;
entityConst.getSignal("I do not exist");
BOOST_ERROR("Should never happen.");
} catch (const dynamicgraph::ExceptionFactory &exception) {
BOOST_CHECK_EQUAL(exception.getCode(),
dynamicgraph::ExceptionFactory::UNREFERED_SIGNAL);
}
// deregistration
try {
dynamicgraph::CustomEntity *customEntity =
dynamic_cast<dynamicgraph::CustomEntity *>(&entity);
customEntity->addSignal();
std::string signame("CustomEntity(my-entity)::input(double)::in_double");
customEntity->Entity::hasSignal(signame);
output_test_stream output;
customEntity->Entity::displaySignalList(output);
dynamicgraph::Entity::SignalMap asigmap = customEntity->getSignalMap();
output << customEntity;
// Removing signals is working the first time
customEntity->rmValidSignal();
// Removing signals generates an exception the second time.
customEntity->rmValidSignal();
BOOST_ERROR("Should never happen.");
} catch (const dynamicgraph::ExceptionFactory &exception) {
BOOST_CHECK_EQUAL(exception.getCode(),
dynamicgraph::ExceptionFactory::UNREFERED_SIGNAL);
}
}
BOOST_AUTO_TEST_CASE(displaySignalList) {
dynamicgraph::Entity &entity =
dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
output_test_stream output;
entity.displaySignalList(output);
BOOST_CHECK(output.is_equal("--- <my-entity> signal list: \n"));
}
BOOST_AUTO_TEST_CASE(display) {
dynamicgraph::Entity &entity =
dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
output_test_stream output;
entity.display(output);
BOOST_CHECK(output.is_equal("CustomEntity: my-entity"));
}
BOOST_AUTO_TEST_CASE(getCommandList) {
dynamicgraph::Entity &entity =
dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
BOOST_CHECK_EQUAL(entity.getCommandList(), "print\nsignals\nsignalDep");
}
BOOST_AUTO_TEST_CASE(writeGraph) {
dynamicgraph::Entity &entity =
dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
output_test_stream output;
entity.writeGraph(output);
BOOST_CHECK(output.is_equal(""));
}
BOOST_AUTO_TEST_CASE(writeCompletionList) {
dynamicgraph::Entity &entity =
dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
output_test_stream output;
entity.writeCompletionList(output);
BOOST_CHECK(output.is_equal("print\nsignals\nsignalDep\n"));
}
BOOST_AUTO_TEST_CASE(sendMsg) {
std::ofstream of;
of.open("/tmp/dg-LOGS.txt", std::ofstream::out | std::ofstream::app);
dgADD_OSTREAM_TO_RTLOG(of);
dynamicgraph::Entity &entity =
dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
output_test_stream output;
for (unsigned int i = 0; i < 4; i++) {
for (unsigned int j = 0; j < 2000; j++) {
dynamicgraph::LoggerVerbosity aLoggerVerbosityLevel =
(dynamicgraph::LoggerVerbosity)i;
entity.setLoggerVerbosityLevel(aLoggerVerbosityLevel);
if (entity.getLoggerVerbosityLevel() != aLoggerVerbosityLevel)
output << "Mismatch output";
#define __FILELINE__ __FILE__ BOOST_PP_STRINGIZE(__LINE__)
entity.logger().stream(dynamicgraph::MSG_TYPE_DEBUG, __FILELINE__)
<< "Auto Test Case"
<< " DEBUG" << '\n';
entity.logger().stream(dynamicgraph::MSG_TYPE_INFO, __FILELINE__)
<< "Auto Test Case"
<< " INFO" << '\n';
entity.logger().stream(dynamicgraph::MSG_TYPE_WARNING, __FILELINE__)
<< "Auto Test Case"
<< " WARNING" << '\n';
entity.logger().stream(dynamicgraph::MSG_TYPE_ERROR, __FILELINE__)
<< "Auto Test Case"
<< " ERROR" << '\n';
#undef __FILELINE__
};
};
BOOST_CHECK(output.is_equal(""));
dynamicgraph::RealTimeLogger::destroy();
}
// WTF?
BOOST_AUTO_TEST_CASE(wtf) {
dynamicgraph::Entity &entity =
dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
BOOST_CHECK_EQUAL(entity.test(),
static_cast<dynamicgraph::SignalBase<int> *>(0));
entity.test2(static_cast<dynamicgraph::SignalBase<int> *>(0));
}
// Copyright 2020 Olivier Stasse
// LAAS, CNRS
#include <dynamic-graph/exception-abstract.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/exception-signal.h>
#include <dynamic-graph/exception-traces.h>
#include <boost/version.hpp>
#include <sstream>
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#endif
#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_suite.hpp>
using boost::test_tools::output_test_stream;
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) {
/// Test exception abstract with a simple message
std::string msg_aea("Test exception abstract");
ExceptionAbstract aEA(10, msg_aea);
const char *aC = aEA.getMessage();
output_test_stream output;
output << aC;
BOOST_CHECK(output.is_equal("Test exception abstract"));
output << aEA;
BOOST_CHECK(
output.is_equal("AbstractError [#10]: Test exception abstract\n"));
}
BOOST_AUTO_TEST_CASE(exception_traces) {
std::string msg_aet("Test exception traces simple");
ExceptionTraces aET(ExceptionTraces::GENERIC, msg_aet);
output_test_stream output;
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"));
}
// Copyright 2010 Thomas Moulard.
//
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/factory.h>
#include <sstream>
#define BOOST_TEST_MODULE factory
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#endif
#include <boost/test/unit_test.hpp>
using boost::test_tools::output_test_stream;
namespace dynamicgraph {
class CustomEntity : public Entity {
public:
static const std::string CLASS_NAME;
virtual const std::string &getClassName() const { return CLASS_NAME; }
explicit CustomEntity(const std::string &n) : Entity(n) {}
};
const std::string CustomEntity::CLASS_NAME = "CustomEntity";
} // namespace dynamicgraph
dynamicgraph::Entity *makeEntity(const std::string &objectName) {
return new dynamicgraph::CustomEntity(objectName);
}
BOOST_AUTO_TEST_CASE(constructor) {
dynamicgraph::FactoryStorage::getInstance();
}
BOOST_AUTO_TEST_CASE(registerEntity) {
dynamicgraph::FactoryStorage::getInstance()->registerEntity("myEntity",
&makeEntity);
try {
dynamicgraph::FactoryStorage::getInstance()->registerEntity("myEntity",
&makeEntity);
BOOST_ERROR("Should never happen.");
} catch (const dynamicgraph::ExceptionFactory &exception) {
BOOST_CHECK_EQUAL(exception.getCode(),
dynamicgraph::ExceptionFactory::OBJECT_CONFLICT);
}
try {
dynamicgraph::FactoryStorage::getInstance()->registerEntity("myEntity", 0);
BOOST_ERROR("Should never happen.");
} catch (const dynamicgraph::ExceptionFactory &exception) {
BOOST_CHECK_EQUAL(exception.getCode(),
dynamicgraph::ExceptionFactory::OBJECT_CONFLICT);
}
}
BOOST_AUTO_TEST_CASE(unregisterEntity) {
dynamicgraph::FactoryStorage::getInstance()->deregisterEntity("myEntity");
try {
dynamicgraph::FactoryStorage::getInstance()->deregisterEntity("myEntity");
BOOST_ERROR("Should never happen.");
} catch (const dynamicgraph::ExceptionFactory &exception) {
BOOST_CHECK_EQUAL(exception.getCode(),
dynamicgraph::ExceptionFactory::OBJECT_CONFLICT);
}
try {
dynamicgraph::FactoryStorage::getInstance()->deregisterEntity(
"I do not exist.");
BOOST_ERROR("Should never happen.");
} catch (const dynamicgraph::ExceptionFactory &exception) {
BOOST_CHECK_EQUAL(exception.getCode(),
dynamicgraph::ExceptionFactory::OBJECT_CONFLICT);
}
}
BOOST_AUTO_TEST_CASE(newEntity) {
dynamicgraph::FactoryStorage::getInstance()->registerEntity("myEntity",
&makeEntity);
{
boost::shared_ptr<dynamicgraph::Entity> entity(
dynamicgraph::FactoryStorage::getInstance()->newEntity("myEntity",
"foo"));
boost::shared_ptr<dynamicgraph::Entity> entity2(
dynamicgraph::FactoryStorage::getInstance()->newEntity("myEntity",
"foo2"));
boost::shared_ptr<dynamicgraph::Entity> entity3(
dynamicgraph::FactoryStorage::getInstance()->newEntity("myEntity", ""));
}
try {
dynamicgraph::FactoryStorage::getInstance()->newEntity("I do not exist.",
"");
BOOST_ERROR("Should never happen.");
} catch (const dynamicgraph::ExceptionFactory &exception) {
BOOST_CHECK_EQUAL(exception.getCode(),
dynamicgraph::ExceptionFactory::UNREFERED_OBJECT);
}
try {
dynamicgraph::FactoryStorage::getInstance()->destroy();
dynamicgraph::FactoryStorage::getInstance()->existEntity("myEntity");
// BOOST_ERROR ("Should never happen.");
} catch (const dynamicgraph::ExceptionFactory &exception) {
BOOST_CHECK_EQUAL(exception.getCode(),
dynamicgraph::ExceptionFactory::UNREFERED_OBJECT);
}
}
BOOST_AUTO_TEST_CASE(existEntity) {
// BOOST_CHECK (dynamicgraph::FactoryStorage::getInstance()->existEntity
// ("myEntity"));
BOOST_CHECK(
!dynamicgraph::FactoryStorage::getInstance()->existEntity("myEntity2"));
BOOST_CHECK(!dynamicgraph::FactoryStorage::getInstance()->existEntity(""));
}
// Copyright 2010 Thomas Moulard.
//
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/signal-ptr.h>
#include <dynamic-graph/signal-time-dependent.h>
#include <iostream>
#include <sstream>
#define BOOST_TEST_MODULE pool
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#endif
#include <boost/test/unit_test.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;
explicit 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");
/// Test exception catching when registering Entity
bool res = false;
try {
dg::Entity *entity2 = dg::FactoryStorage::getInstance()->newEntity(
"MyEntity", "MyEntityInst");
bool res2 = (entity2 == entity);
BOOST_CHECK(res2);
} catch (const dg::ExceptionFactory &aef) {
res = (aef.getCode() == dg::ExceptionFactory::OBJECT_CONFLICT);
}
BOOST_CHECK(res);
/// Test exception catching when deregistering Entity
res = false;
try {
dg::FactoryStorage::getInstance()->deregisterEntity("MyEntityInstFailure");
} catch (const dg::ExceptionFactory &aef) {
res = (aef.getCode() == dg::ExceptionFactory::OBJECT_CONFLICT);
}
BOOST_CHECK(res);
/// Search for an entity inside the map
output_test_stream output;
dg::Entity &e = dg::PoolStorage::getInstance()->getEntity("MyEntityInst");
e.display(output);
BOOST_CHECK(output.is_equal("Hello! My name is MyEntityInst !\n"));
/// Search for an entity inside the map
res = false;
try {
dg::PoolStorage::getInstance()->getEntity("MyEntityInstFailure");
} catch (const dg::ExceptionFactory &aef) {
res = (aef.getCode() == dg::ExceptionFactory::UNREFERED_OBJECT);
}
BOOST_CHECK(res);
/// 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);
BOOST_CHECK(
output.is_equal("MyEntityInst.in_double\nMyEntityInst.out_double\n"
"print\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\" { \t 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;
std::cout << sub_str_to_test << std::endl;
std::cout << sub_s_output_wgph << std::endl;
std::cout << sub_str_to_test.compare(sub_s_output_wgph) << std::endl;
BOOST_CHECK(two_sub_string_identical);
/// Test name of a valid signal.
std::istringstream an_iss("MyEntityInst.in_double");
dg::SignalBase<int> &aSignal =
dg::PoolStorage::getInstance()->getSignal(an_iss);
std::string aSignalName = aSignal.getName();
testExistence =
aSignalName == "MyEntity(MyEntityInst)::input(double)::in_double";
BOOST_CHECK(testExistence);
/// Test name of an unvalid signal.
an_iss.str("MyEntityInst.in2double");
try {
dg::PoolStorage::getInstance()->getSignal(an_iss);
} catch (const dg::ExceptionFactory &aef) {
res = (aef.getCode() == dg::ExceptionFactory::UNREFERED_SIGNAL);
}
BOOST_CHECK(res);
/// Deregister the entity.
dg::PoolStorage::getInstance()->deregisterEntity(entity->getName());
/// Testing the existance of an entity
testExistence =
dg::PoolStorage::getInstance()->existEntity("MyEntityInst", entity);
BOOST_CHECK(!testExistence);
/// Create Entity
std::string name_entity("MyEntityInst2");
dg::PoolStorage::getInstance()->clearPlugin(name_entity);
dg::PoolStorage::destroy();
}
/*
* Copyright 2018,
* Joseph Mirabel
*
* LAAS-CNRS
*
*/
#include <iostream>
#define ENABLE_RT_LOG
#include <dynamic-graph/real-time-logger.h>
#define BOOST_TEST_MODULE real_time_logger
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#endif
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/thread/thread.hpp>
using namespace dynamicgraph;
BOOST_AUTO_TEST_CASE(monothread) {
RealTimeLogger rtl(10);
rtl.addOutputStream(LoggerStreamPtr_t(new LoggerIOStream(std::cout)));
for (int i = 0; i < 9; ++i) rtl.front() << "Call number " << i << '\n';
BOOST_CHECK(rtl.full());
rtl.front() << "This call should not appear in the output" << '\n';
rtl.spinOnce();
BOOST_CHECK(!rtl.full());
rtl.front() << "This call should appear in the output" << '\n';
int spinNb = 0;
while (rtl.spinOnce()) {
spinNb++;
}
BOOST_CHECK_EQUAL(spinNb, 9);
rtl.front() << "This msg should be short." << '\n';
rtl.spinOnce();
}
BOOST_AUTO_TEST_CASE(multithread) {
// The part of the code changing priority will only be effective
// if this test is run as root. Otherwise it behaves like a classical thread.
// Test confirms that in this case, it runs with a priority -51
// and that the thread for logging is running on SCHED_OTHER
// with a nice priority (20).
int threadPolicy;
struct sched_param threadParam;
if (pthread_getschedparam(pthread_self(), &threadPolicy, &threadParam) == 0) {
threadPolicy = SCHED_RR;
threadParam.sched_priority = 50;
pthread_setschedparam(pthread_self(), threadPolicy, &threadParam);
}
RealTimeLogger &rtl = RealTimeLogger::instance();
dgADD_OSTREAM_TO_RTLOG(std::cout);
for (std::size_t i = 0; i < rtl.getBufferSize() - 1; ++i)
dgRTLOG() << "Call number " << i << '\n';
for (std::size_t i = 0; i < 12; ++i) {
boost::this_thread::sleep(boost::posix_time::milliseconds(20));
dgRTLOG() << "Call number " << i << std::endl;
BOOST_CHECK(!rtl.full());
}
dgRTLOG() << "This call should appear in the output" << '\n';
RealTimeLogger::destroy();
}
//
// Created by corentin on 7/3/19.
//
#include <assert.h>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/signal-array.h>
#include <dynamic-graph/signal-caster.h>
#include <dynamic-graph/tracer.h>
#include <boost/foreach.hpp>
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#endif
#include <boost/test/unit_test.hpp>
#include <iostream>
#define BOOST_TEST_MODULE signal_array
using boost::test_tools::output_test_stream;
dynamicgraph::SignalArray_const<double> sig;
using namespace std;
using namespace dynamicgraph;
using namespace dynamicgraph::command;
BOOST_AUTO_TEST_CASE(test_array) {
SignalBase<int> sigBa("add test");
SignalArray_const<int> sigArr_C(1);
sigArr_C.operator<<(sigBa);
sigArr_C.operator<<(sigBa);
BOOST_CHECK_EQUAL(2, sigArr_C.getSize());
SignalArray<int> sigArr(1);
sigArr.operator<<(sigBa);
sigArr.operator<<(sigBa);
BOOST_CHECK_EQUAL(2, sigArr.getSize());
SignalBase<int> sigB("constructor test");
SignalArray<int> sigA(2);
sigA << sigB;
sigA.operator<<(sigB);
SignalArray_const<int> sig_C(sigA);
BOOST_CHECK_EQUAL(sigA.getSize(), sig_C.getSize());
}
BOOST_AUTO_TEST_CASE(test_base) {
SignalBase<int> sigA("testA");
SignalBase<int> sigB("test");
sigB.setReady();
BOOST_CHECK_EQUAL(true, sigB.getReady());
// Does nothing, just check that the interface
// still exist at the abstract level.
sigB.setPeriodTime(1);
sigB.getPeriodTime();
sigB.addDependency(sigA);
sigB.removeDependency(sigA);
sigB.clearDependencies();
BOOST_CHECK_EQUAL(true, sigB.needUpdate(10));
output_test_stream output;
sigB.writeGraph(output);
BOOST_CHECK(output.is_equal(""));
/// Verify plug operations
bool res = false;
try {
sigB.plug(&sigA);
} catch (const ExceptionSignal &aea) {
res = (aea.getCode() == ExceptionSignal::PLUG_IMPOSSIBLE);
}
BOOST_CHECK(res);
res = false;
try {
sigB.unplug();
} catch (const ExceptionSignal &aea) {
res = (aea.getCode() == ExceptionSignal::PLUG_IMPOSSIBLE);
}
BOOST_CHECK(res);
res = false;
try {
sigB.setConstantDefault();
} catch (const ExceptionSignal &aea) {
res = (aea.getCode() == ExceptionSignal::PLUG_IMPOSSIBLE);
}
BOOST_CHECK(res);
res = false;
try {
/// Check signal compatibility.
sigB.checkCompatibility();
} catch (const ExceptionSignal &aea) {
res = (aea.getCode() == ExceptionSignal::PLUG_IMPOSSIBLE);
}
/// Verify set command value
/// set
std::istringstream iss("empty");
res = false;
try {
sigB.set(iss);
} catch (const ExceptionSignal &aea) {
res = (aea.getCode() == ExceptionSignal::SET_IMPOSSIBLE);
}
BOOST_CHECK(res);
/// get a value
res = false;
std::ostringstream oss;
try {
sigB.get(oss);
} catch (const ExceptionSignal &aea) {
res = (aea.getCode() == ExceptionSignal::SET_IMPOSSIBLE);
}
BOOST_CHECK(res);
/// Trigger revaluation of the signal
res = false;
try {
sigB.recompute(100);
} catch (const ExceptionSignal &aea) {
res = (aea.getCode() == ExceptionSignal::SET_IMPOSSIBLE);
}
BOOST_CHECK(res);
/// Trace the signal
res = false;
try {
sigB.trace(oss);
} catch (const ExceptionSignal &aea) {
res = (aea.getCode() == ExceptionSignal::SET_IMPOSSIBLE);
}
BOOST_CHECK(res);
/// Display the signal
sigB.display(output);
BOOST_CHECK(output.is_equal("Sig:test"));
}
BOOST_AUTO_TEST_CASE(test_cast_helper) {
std::istringstream iss;
iss.str("1");
signal_io<int>::cast(iss);
{
std::istringstream iss_fail;
iss.str("test");
BOOST_CHECK_THROW(signal_io<int>::cast(iss_fail), ExceptionSignal);
}
/// Test cast register with Vector
output_test_stream output;
dynamicgraph::Vector avec;
avec.resize(4);
avec[0] = 1.0;
avec[1] = 2.0;
avec[2] = 3.0;
avec[3] = 4.0;
BOOST_CHECK_NO_THROW(signal_io<Vector>::trace(avec, output));
/// Test cast register with Matrix
dynamicgraph::Matrix amatrix;
amatrix.resize(2, 2);
amatrix(0, 0) = 0.0;
amatrix(0, 1) = 1.0;
amatrix(1, 0) = 2.0;
amatrix(1, 1) = 3.0;
BOOST_CHECK_NO_THROW(signal_io<Matrix>::trace(amatrix, output));
std::istringstream aiss("test");
signal_io<std::string>::cast(aiss);
}
#include <Eigen/Dense>
// Copyright 2010 Thomas Moulard.
//
#include "signal-cast-registerer-libA.hh"
vec_type vA;
// Copyright 2010 Thomas Moulard.
//
#include <Eigen/Dense>
typedef Eigen::VectorXd vec_type;
extern vec_type vA;
// Copyright 2010 Thomas Moulard.
//
#include "signal-cast-registerer-libB.hh"
vec_type vB;
// Copyright 2010 Thomas Moulard.
//
#include <Eigen/Core>
typedef Eigen::VectorXd vec_type;
extern vec_type vB;
// Copyright 2010 Thomas Moulard.
//
#include <dynamic-graph/debug.h>
#include <dynamic-graph/eigen-io.h>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/linear-algebra.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/signal.h>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <string>
#include "signal-cast-register-test.h"
#include "signal-cast-registerer-libA.hh"
#include "signal-cast-registerer-libB.hh"
#define BOOST_TEST_MODULE signal_cast_registerer
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#endif
#include <boost/test/unit_test.hpp>
#include <iostream>
using boost::test_tools::output_test_stream;
typedef Eigen::VectorXd Vector;
typedef Eigen::MatrixXd Matrix;
// Check standard double cast registerer.
BOOST_AUTO_TEST_CASE(standard_double_registerer) {
dynamicgraph::Signal<double, int> mySignal("out");
typedef std::pair<std::string, std::string> test_t;
std::vector<test_t> values;
values.push_back(std::make_pair("42.0", "42"));
values.push_back(std::make_pair("42.5", "42.5"));
values.push_back(std::make_pair("-12.", "-12"));
// Double special values.
// FIXME: these tests are failing :(
values.push_back(std::make_pair("inf", "inf"));
values.push_back(std::make_pair("-inf", "-inf"));
values.push_back(std::make_pair("nan", "nan"));
BOOST_FOREACH (const test_t &test, values) {
// Set
std::istringstream value(test.first);
mySignal.set(value);
// Get
{
output_test_stream output;
mySignal.get(output);
BOOST_CHECK_EQUAL(output.str(), test.second);
}
// Trace
{
output_test_stream output;
mySignal.trace(output);
BOOST_CHECK_EQUAL(output.str(), test.second);
}
}
// Check invalid values.
// FIXME: this test is failing for now.
std::istringstream value("This is not a valid double.");
BOOST_CHECK_THROW(mySignal.set(value), std::exception);
}
// Check a custom cast registerer for Boost uBLAS vectors.
BOOST_AUTO_TEST_CASE(custom_vector_registerer) {
dynamicgraph::Signal<dynamicgraph::Vector, int> myVectorSignal("vector");
// Print the signal name.
{
output_test_stream output;
output << myVectorSignal;
BOOST_CHECK(output.is_equal("Sig:vector (Type Cst)"));
}
for (unsigned int i = 0; i < 5; ++i) {
Vector v = Vector::Unit(5, i);
std::ostringstream os;
os << v;
std::istringstream ss("[5](" + os.str() + ")");
// Set signal value.
myVectorSignal.set(ss);
// Print out signal value.
output_test_stream output;
myVectorSignal.get(output);
boost::format fmt("%d %d %d %d %d");
fmt % (i == 0) % (i == 1) % (i == 2) % (i == 3) % (i == 4);
BOOST_CHECK(output.is_equal(fmt.str()));
}
// Catch Exception of ss (not good input)
// ss[0] != "["
try {
std::istringstream ss("test");
myVectorSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[0] != \"[\"";
}
// ss[1] != %i
try {
std::istringstream ss("[test");
myVectorSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[1] != %i";
}
// ss[2] != "]"
try {
std::istringstream ss("[5[");
myVectorSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[2] != \"]\"";
}
// ss[3] != "("
try {
std::istringstream ss("[5]test");
myVectorSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[3] != \"(\"";
}
// ss[4] != ' ' || ','
try {
std::istringstream ss("[5](1, ");
myVectorSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[4] != \" \" || \",\"";
}
// ss[-1] != ")"
try {
std::istringstream ss("[5](1,2,3,4,5]");
myVectorSignal.set(ss);
} catch (ExceptionSignal &e) {
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) {
dynamicgraph::Signal<dynamicgraph::Matrix, int> myMatrixSignal("matrix");
// Print the signal name.
{
output_test_stream output;
output << myMatrixSignal;
BOOST_CHECK(output.is_equal("Sig:matrix (Type Cst)"));
}
// Catch Exception of ss (not good input)
// ss[0] != "["
try {
std::istringstream ss("test");
myMatrixSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[0] != \"[\"";
}
// ss[1] != %i
try {
std::istringstream ss("[test");
myMatrixSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[1] != %i";
}
// ss[2] != ","
try {
std::istringstream ss("[5[");
myMatrixSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[2] != \",\"";
}
// ss[3] != %i
try {
std::istringstream ss("[5,c");
myMatrixSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[3] != %i";
}
// ss[4] != "]"
try {
std::istringstream ss("[5,3[");
myMatrixSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[4] != \"]\"";
}
// ss[5] != "("
try {
std::istringstream ss("[5,3]test");
myMatrixSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[5] != \"(\"";
}
// ss[6] != "("
try {
std::istringstream ss("[5,3](test");
myMatrixSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[6] != \"(\"";
}
// ss[8] != " " || ","
try {
std::istringstream ss("[5,3]((1,");
myMatrixSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[8] != \" \" || \",\"";
}
// ss[6+n] != ")"
try {
std::istringstream ss("[5,3]((1,2,3]");
myMatrixSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << ("ss[6+n] != \")\"");
}
// ss[-3] != ")"
try {
std::istringstream ss("[5,1]((1)(2)(3[");
myMatrixSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[5] != \")\"";
}
// ss[-3] != ")"
try {
std::istringstream ss("[5,1]((1)(2)(3)[");
myMatrixSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[5] != \")\"";
}
// ss[-1]!= ")"
try {
std::istringstream ss("[3,1]((1)(2),(3)[");
myMatrixSignal.set(ss);
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[5] != \")\" and ignore \",\"";
}
//[...]((...))
}
// Copyright 2010 Thomas Moulard.
//
#include <dynamic-graph/debug.h>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/signal-base.h>
#include <dynamic-graph/signal-ptr.h>
#include <dynamic-graph/signal-time-dependent.h>
#include <dynamic-graph/signal.h>
#include <boost/foreach.hpp>
#include <iostream>
#include <string>
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#endif
#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_suite.hpp>
#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 std::cout;
template <class T>
class DummyClass {
public:
std::string proname;
std::list<sigDouble_t *> inputsig;
std::list<sigString_t *> inputsigV;
explicit 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");
const SignalPtr<double, int> cstSigNotPlug(NULL, "sigNotPlug");
try {
sigNotPlug.getPtr();
} catch (ExceptionSignal &e) {
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) == false);
BOOST_CHECK(res);
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");
Signal<int, int> sigint("sig");
Signal<std::string, int> sigstr("sig_str");
SignalPtr<double, int> sigPtrA(NULL, "sigPtrA"), sigPtrB(NULL, "sigPtrB");
SignalPtr<double, int> sigPtrAbstract(NULL, "sigPtrAbstract");
DummyClass<double> pro3("pro3");
sig.setConstant(1.56);
sig.recompute(2);
std::string name = "sig";
sig.getClassName(name);
std::string test = "test";
try {
sig.getClassName(test);
} catch (ExceptionSignal &e) {
e.getExceptionName();
}
BOOST_CHECK(true);
sigPtrA.setFunction(boost::bind(&DummyClass<double>::fun, &pro3, _1, _2));
sigPtrA.recompute(3);
/// Plugging signal.
SignalBase<int> &sigRef = sig, sigBase("sigBase");
SignalBase<int> &sigPtrARef = sigPtrA, &sigPtrBRef = sigPtrB,
&sigPtrAbstractRef = sigPtrAbstract;
sigPtrARef.plug(0);
sigPtrARef.plug(&sigRef);
sigPtrBRef.plug(&sigPtrARef);
/// Try to plug an incompatible signal.
/// leave
bool res = false;
try {
sigPtrARef.plug(&sigstr);
} catch (const ExceptionSignal &aes) {
res = (aes.getCode() == ExceptionSignal::PLUG_IMPOSSIBLE);
}
BOOST_CHECK(res);
/// Plug the signal.
sigPtrAbstractRef.plug(&sigRef);
sigPtrA.getPtr();
BOOST_CHECK(true);
try {
sigPtrARef.checkCompatibility();
} catch (const ExceptionSignal &aes) {
/// Should be NOT_INITIALIZED becase the last plug
/// on sigstr failed.
res = (aes.getCode() == ExceptionSignal::NOT_INITIALIZED);
} catch (const std::exception &e) {
std::cout << "Standard Exception:" << e.what() << std::endl;
} catch (...) {
std::cout << "Anything else: " << std::endl;
}
sigPtrA.needUpdate(5);
BOOST_CHECK(true);
int ltime = sigPtrA.getTime();
sigPtrA.getPluged();
sigPtrA(ltime);
BOOST_CHECK(true);
sigPtrB.getPtr();
/// Test sigPtrAbstract with a normal plug.
res = false;
try {
sigPtrAbstract.getAbstractPtr();
} catch (ExceptionSignal &aes) {
/// Should be NOT_INITIALIZED becase the last plug
/// on sigstr failed.
std::cout << "Code: " << aes.getCode() << std::endl;
res = (aes.getCode() == ExceptionSignal::NOT_INITIALIZED);
} catch (...) {
std::cout << "Anything else with sigPtrAbstract.getAbstractPtr()"
<< std::endl;
}
BOOST_CHECK(true);
/// Test the case where the plug ref is zero.
sigPtrAbstractRef.plug(0);
BOOST_CHECK(true);
assert(sigRef.isPlugged() != true);
SignalBase<int> *t = sigRef.getPluged();
// assert(sigPtrA.get()=false);
// TODO Can't check if the constant change
sigPtrA.setConstantDefault(1.2);
// getconstant
sigPtrA.setConstantDefault();
// getconstant
sigPtrA.setConstant(3.4);
// getconstant
double tab_D[2];
tab_D[0] = 1.2;
tab_D[1] = 3.4;
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
sigPtrA.operator=(1.2);
// getconstant
sigPtrA.displayDependencies(output);
cout << t << std::endl;
cout << "Sig = ";
sigRef.get(cout);
cout << std::endl;
cout << "SigPtrA = ";
sigPtrARef.get(cout);
cout << std::endl;
cout << "SigPtrB = ";
sigPtrBRef.get(cout);
cout << std::endl;
sigPtrA.unplug();
}
BOOST_AUTO_TEST_CASE(plug_signal_string) {
Signal<std::string, int> outSig("output");
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");
outSig.setConstant(str);
inSig.plug(&outSig);
inSig.recompute(1);
std::ostringstream os1;
inSig.get(os1);
std::string res(os1.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 2 3 4 5"));
Signal<std::string, int> s("signal");
std::ostringstream os2;
s.setConstant(str);
os2.clear();
s.get(os2);
res = os2.str();
std::cout << "res=" << res << std::endl;
BOOST_CHECK(res == str);
}
BOOST_AUTO_TEST_CASE(set_signal_string) {
Signal<std::string, int> s("signal");
std::string str("");
std::ostringstream os;
os << str;
std::istringstream value(os.str());
try {
s.set(value);
} catch (const std::exception &exc) {
std::cout << exc.what() << std::endl;
BOOST_CHECK(!(bool)("Tentative to set signal to empty string"));
}
}