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

Target

Select target project
  • cberge/dynamic-graph
  • ostasse/dynamic-graph
  • gsaurel/dynamic-graph
  • stack-of-tasks/dynamic-graph
4 results
Show changes
Showing
with 1640 additions and 387 deletions
......@@ -5,22 +5,28 @@
* See LICENSE file
*
*/
#include <sstream>
#include <iostream>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <iostream>
#include <sstream>
#include "dynamic-graph/command-bind.h"
#include "dynamic-graph/factory.h"
#include "dynamic-graph/pool.h"
#include "dynamic-graph/command-bind.h"
#define ENABLE_RT_LOG
#include <dynamic-graph/real-time-logger.h>
#include <dynamic-graph/logger.h>
#include <dynamic-graph/real-time-logger.h>
#define BOOST_TEST_MODULE debug - logger
#include <boost/test/unit_test.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>
using boost::test_tools::output_test_stream;
......@@ -35,26 +41,60 @@ class CustomEntity : public Entity {
bool test_two_args_;
bool test_three_args_;
bool test_four_args_;
bool test_one_arg_ret_;
bool test_two_args_ret_;
virtual const std::string &getClassName() const { return CLASS_NAME; }
CustomEntity(const std::string n) : Entity(n) {
explicit CustomEntity(const std::string &n) : Entity(n) {
test_zero_arg_ = false;
test_one_arg_ = false;
test_two_args_ = false;
test_three_args_ = false;
test_four_args_ = false;
addCommand("0_arg", makeCommandVoid0(*this, &CustomEntity::zero_arg, docCommandVoid0("zero arg")));
addCommand("1_arg", makeCommandVoid1(*this, &CustomEntity::one_arg, docCommandVoid1("one arg", "int")));
addCommand("2_args", makeCommandVoid2(*this, &CustomEntity::two_args, docCommandVoid2("two args", "int", "int")));
addCommand("3_args",
makeCommandVoid3(*this, &CustomEntity::three_args, docCommandVoid3("three args", "int", "int", "int")));
addCommand("4_args", makeCommandVoid4(*this, &CustomEntity::four_args,
docCommandVoid4("four args", "int", "int", "int", "int")));
test_one_arg_ret_ = false;
test_two_args_ret_ = false;
addCommand("0_arg", makeCommandVoid0(*this, &CustomEntity::zero_arg,
docCommandVoid0("zero arg")));
addCommand("1_arg", makeCommandVoid1(*this, &CustomEntity::one_arg,
docCommandVoid1("one arg", "int")));
addCommand("2_args",
makeCommandVoid2(*this, &CustomEntity::two_args,
docCommandVoid2("two args", "int", "int")));
addCommand("3_args", makeCommandVoid3(*this, &CustomEntity::three_args,
docCommandVoid3("three args", "int",
"int", "int")));
addCommand("4_args",
makeCommandVoid4(
*this, &CustomEntity::four_args,
docCommandVoid4("four args", "int", "int", "int", "int")));
addCommand("1_arg_r",
makeCommandReturnType1(*this, &CustomEntity::one_arg_ret,
docCommandVoid1("one arg", "int")));
addCommand("2_args_r", makeCommandReturnType2(
*this, &CustomEntity::two_args_ret,
docCommandVoid2("two args", "int", "int")));
addCommand(
"cmd_verbose",
makeCommandVerbose(*this, &CustomEntity::cmd_verbose,
docCommandVerbose("Display some information")));
/// Generating an exception by adding a command which already exist
bool res = false;
std::string e_1_arg("1_arg");
try {
addCommand(e_1_arg, getNewStyleCommand(e_1_arg));
} catch (dynamicgraph::ExceptionFactory &aef) {
res = (aef.getCode() == dynamicgraph::ExceptionFactory::OBJECT_CONFLICT);
}
BOOST_CHECK(res);
}
~CustomEntity() {}
......@@ -65,18 +105,41 @@ class CustomEntity : public Entity {
void two_args(const int &, const int &) { test_two_args_ = true; }
void three_args(const int &, const int &, const int &) { test_three_args_ = true; }
void three_args(const int &, const int &, const int &) {
test_three_args_ = true;
}
void four_args(const int &, const int &, const int &, const int &) {
test_four_args_ = true;
}
void four_args(const int &, const int &, const int &, const int &) { test_four_args_ = true; }
int one_arg_ret(const int &) {
test_one_arg_ret_ = true;
return 2;
}
std::string two_args_ret(const int &, const int &) {
test_two_args_ret_ = true;
return std::string("return");
}
void cmd_verbose(std::ostream &oss) {
std::string as("print verbose");
oss << as;
}
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
} // namespace dynamicgraph
BOOST_AUTO_TEST_CASE(command_test) {
dynamicgraph::CustomEntity &entity = *(dynamic_cast<dynamicgraph::CustomEntity *>(
dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity", "my-entity")));
dynamicgraph::CustomEntity *ptr_entity =
(dynamic_cast<dynamicgraph::CustomEntity *>(
dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity",
"my-entity")));
dynamicgraph::CustomEntity &entity = *ptr_entity;
std::map<const std::string, Command *> aCommandMap = entity.getNewStyleCommandMap();
std::map<const std::string, Command *> aCommandMap =
entity.getNewStyleCommandMap();
std::map<const std::string, Command *>::iterator it_map;
......@@ -100,10 +163,77 @@ BOOST_AUTO_TEST_CASE(command_test) {
values.push_back(aValue);
it_map->second->setParameterValues(values);
it_map->second->execute();
it_map->second->owner();
it_map->second->getDocstring();
}
BOOST_CHECK(entity.test_one_arg_);
BOOST_CHECK(entity.test_two_args_);
BOOST_CHECK(entity.test_three_args_);
BOOST_CHECK(entity.test_four_args_);
// With return type.
vec_fname.clear();
vec_fname.push_back(std::string("1_arg_r"));
vec_fname.push_back(std::string("2_args_r"));
values.clear();
for (unsigned int i = 0; i < 2; i++) {
it_map = aCommandMap.find(vec_fname[i]);
if (it_map == aCommandMap.end()) {
BOOST_CHECK(false);
exit(-1);
}
values.push_back(aValue);
it_map->second->setParameterValues(values);
Value aValue = it_map->second->execute();
it_map->second->owner();
it_map->second->getDocstring();
}
BOOST_CHECK(entity.test_one_arg_ret_);
BOOST_CHECK(entity.test_two_args_ret_);
std::vector<Value> values_two;
values_two.push_back(aValue);
/// Wrong number of arguments
bool res = false;
it_map = aCommandMap.find(std::string("2_args"));
try {
it_map->second->setParameterValues(values_two);
} catch (const dynamicgraph::ExceptionAbstract &aea) {
res = (aea.getCode() == dynamicgraph::ExceptionAbstract::ABSTRACT);
}
BOOST_CHECK(res);
double snd_arg_db = 10.0;
Value aValue2(snd_arg_db);
values_two.push_back(aValue2);
/// Wrong types of arguments
res = false;
it_map = aCommandMap.find(std::string("2_args"));
try {
it_map->second->setParameterValues(values_two);
} catch (const dynamicgraph::ExceptionAbstract &aea) {
res = (aea.getCode() == dynamicgraph::ExceptionAbstract::TOOLS);
}
BOOST_CHECK(res);
/// Try to find the command 1_arg
res = false;
entity.getNewStyleCommand(vec_fname[0]);
BOOST_CHECK(true);
/// Generate an exception by searching a command with an empty name.w
std::string empty("");
try {
entity.getNewStyleCommand(empty);
} catch (dynamicgraph::ExceptionFactory &aef) {
res = (aef.getCode() == dynamicgraph::ExceptionFactory::UNREFERED_FUNCTION);
}
BOOST_CHECK(res);
/// delete the entity.
delete ptr_entity;
}
// Copyright 2010 Thomas Moulard.
//
#include <sstream>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/pool.h>
#include <sstream>
#define BOOST_TEST_MODULE customEntity
#include <boost/test/unit_test.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>
using boost::test_tools::output_test_stream;
struct CustomEntity : public dynamicgraph::Entity {
static const std::string CLASS_NAME;
virtual const std::string& getClassName() const { return CLASS_NAME; }
virtual const std::string &getClassName() const { return CLASS_NAME; }
CustomEntity(const std::string n) : Entity(n) {}
explicit CustomEntity(const std::string &n) : Entity(n) {}
virtual ~CustomEntity() {}
void display(std::ostream& os) const { os << "custom entity"; }
void display(std::ostream &os) const { os << "custom entity"; }
};
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
......@@ -31,7 +36,9 @@ DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
BOOST_AUTO_TEST_CASE(constructor) {
BOOST_CHECK_EQUAL(CustomEntity::CLASS_NAME, "CustomEntity");
dynamicgraph::Entity* entity = dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity", "my-entity");
dynamicgraph::Entity *entity =
dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity",
"my-entity");
BOOST_CHECK_EQUAL(entity->getName(), "my-entity");
BOOST_CHECK_EQUAL(entity->Entity::getClassName(), "Entity");
BOOST_CHECK_EQUAL(entity->getClassName(), CustomEntity::CLASS_NAME);
......@@ -42,7 +49,9 @@ BOOST_AUTO_TEST_CASE(constructor) {
}
BOOST_AUTO_TEST_CASE(display) {
dynamicgraph::Entity* entity = dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity", "my-entity");
dynamicgraph::Entity *entity =
dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity",
"my-entity");
output_test_stream output;
......
......@@ -5,25 +5,28 @@
* See LICENSE file
*
*/
#include <sstream>
#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"
#include <dynamic-graph/real-time-logger.h>
#include <dynamic-graph/logger.h>
#define BOOST_TEST_MODULE debug - logger
#include <boost/test/unit_test.hpp>
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#include <boost/thread/thread.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;
......@@ -31,8 +34,8 @@ namespace dynamicgraph {
class CustomEntity : public Entity {
public:
static const std::string CLASS_NAME;
virtual const std::string& getClassName() const { return CLASS_NAME; }
CustomEntity(const std::string n) : Entity(n) {
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);
......@@ -46,10 +49,14 @@ class CustomEntity : public Entity {
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);
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();
}
......@@ -58,15 +65,14 @@ DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
} // namespace dynamicgraph
BOOST_AUTO_TEST_CASE(debug_logger_wrong_initialization) {
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-2")));
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();
......
......@@ -5,21 +5,27 @@
* See LICENSE file
*
*/
#include <sstream>
#include <iostream>
#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/real-time-logger.h>
#include <dynamic-graph/logger.h>
#include <dynamic-graph/real-time-logger.h>
#define BOOST_TEST_MODULE debug - logger
#include <boost/test/unit_test.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>
using boost::test_tools::output_test_stream;
......@@ -27,26 +33,93 @@ namespace dynamicgraph {
class CustomEntity : public Entity {
public:
static const std::string CLASS_NAME;
virtual const std::string& getClassName() const { return CLASS_NAME; }
CustomEntity(const std::string n) : Entity(n) {
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);
LoggerVerbosity alv = logger_.getVerbosity();
BOOST_CHECK(alv == 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() {
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_.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();
}
};
......@@ -61,13 +134,15 @@ BOOST_AUTO_TEST_CASE(debug_logger) {
BOOST_CHECK_EQUAL(dynamicgraph::CustomEntity::CLASS_NAME, "CustomEntity");
dynamicgraph::CustomEntity& entity = *(dynamic_cast<dynamicgraph::CustomEntity*>(
dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity", "my-entity")));
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.004);
BOOST_CHECK_EQUAL(entity.getStreamPrintPeriod(), 0.004);
entity.setStreamPrintPeriod(0.002);
BOOST_CHECK_EQUAL(entity.getStreamPrintPeriod(), 0.002);
for (unsigned int i = 0; i < 10000; i++) {
entity.testDebugTrace();
......
/* 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"));
}
......@@ -3,10 +3,12 @@
* Olivier Stasse
*
*/
#include <sstream>
#include <iostream>
#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"
......@@ -18,8 +20,12 @@
#define BOOST_TEST_MODULE debug - trace
#include <boost/test/unit_test.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>
using boost::test_tools::output_test_stream;
......@@ -27,11 +33,13 @@ namespace dynamicgraph {
class CustomEntity : public Entity {
public:
static const std::string CLASS_NAME;
virtual const std::string& getClassName() const { return CLASS_NAME; }
CustomEntity(const std::string n) : Entity(n) {
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"); }
~CustomEntity() {
dynamicgraph::dgDEBUGFLOW.closeFile("/tmp/dynamic-graph-traces.txt");
}
void testDebugTrace() {
/// Test debugging information when entering the code.
dgDEBUGIN(5);
......@@ -51,14 +59,20 @@ DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
BOOST_AUTO_TEST_CASE(testDebugTrace) {
BOOST_CHECK_EQUAL(dynamicgraph::CustomEntity::CLASS_NAME, "CustomEntity");
dynamicgraph::CustomEntity& entity = *(dynamic_cast<dynamicgraph::CustomEntity*>(
dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity", "my-entity")));
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);
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;
......@@ -85,4 +99,6 @@ BOOST_AUTO_TEST_CASE(testDebugTrace) {
two_sub_string_identical = str_to_test == oss_debug_file.str();
BOOST_CHECK(two_sub_string_identical);
delete ptr_entity;
}
......@@ -4,8 +4,6 @@
*
*/
#include <iostream>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/factory.h>
......@@ -14,10 +12,16 @@
#include <dynamic-graph/signal-time-dependent.h>
#include <dynamic-graph/tracer.h>
#include <iostream>
#define BOOST_TEST_MODULE debug - tracer
#include <boost/test/unit_test.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>
namespace dynamicgraph {
struct MyEntity : public dynamicgraph::Entity {
......@@ -25,21 +29,30 @@ struct MyEntity : public dynamicgraph::Entity {
dynamicgraph::Signal<double, int> m_sigdSIN;
dynamicgraph::SignalTimeDependent<double, int> m_sigdTimeDepSOUT;
dynamicgraph::SignalTimeDependent<Vector, int> m_sigVTimeDepSOUT;
dynamicgraph::SignalTimeDependent<double, int> m_sigdTwoTimeDepSOUT;
MyEntity(const std::string &name)
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,
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")
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_sigdTwoTimeDepSOUT);
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 void display(std::ostream &os) const {
os << "Hello! My name is " << getName() << " !" << std::endl;
}
virtual const std::string &getClassName() const { return CLASS_NAME; }
......@@ -48,46 +61,70 @@ struct MyEntity : public dynamicgraph::Entity {
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::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");
dynamicgraph::Entity &entity = *dynamicgraph::FactoryStorage::getInstance()->newEntity("MyEntity", "my-entity");
/// 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")));
*(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);
std::string rootdir("/tmp");
std::string basename("my-tracer");
std::string suffix(".dat");
/// Test openfiles
atracer.openFiles(rootdir, basename, suffix);
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();
}
......@@ -5,99 +5,161 @@
#define ENABLE_RT_LOG
#include <sstream>
#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/factory.h"
#include "dynamic-graph/pool.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
#include <boost/test/unit_test.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>
using boost::test_tools::output_test_stream;
namespace dynamicgraph {
class CustomEntity : public Entity {
public:
dynamicgraph::SignalPtr<double, int> m_sigdSIN;
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; }
CustomEntity(const std::string n)
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_sigdTimeDepSOUT(boost::bind(&CustomEntity::update, this, _1, _2), m_sigdSIN,
"CustomEntity(" + name + ")::input(double)::out_double")
{}
void addSignal() { signalRegistration(m_sigdSIN << m_sigdTimeDepSOUT); }
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);
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) {
BOOST_CHECK_EQUAL(dynamicgraph::CustomEntity::CLASS_NAME, "CustomEntity");
namespace dg = dynamicgraph;
namespace dgc = dynamicgraph::command;
BOOST_CHECK_EQUAL(dg::CustomEntity::CLASS_NAME, "CustomEntity");
dynamicgraph::Entity& entity = *dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity", "my-entity");
dg::Entity &entity = *dg::FactoryStorage::getInstance()->newEntity(
"CustomEntity", "my-entity");
BOOST_CHECK_EQUAL(entity.getName(), "my-entity");
BOOST_CHECK_EQUAL(entity.getClassName(), dynamicgraph::CustomEntity::CLASS_NAME);
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;
dynamicgraph::CustomEntity entity2("");
entity.getDocString();
}
BOOST_AUTO_TEST_CASE(signal) {
dynamicgraph::Entity& entity = dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
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);
} catch (const dynamicgraph::ExceptionFactory &exception) {
BOOST_CHECK_EQUAL(exception.getCode(),
dynamicgraph::ExceptionFactory::UNREFERED_SIGNAL);
}
// Const getter.
try {
const dynamicgraph::Entity& entityConst = entity;
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);
} catch (const dynamicgraph::ExceptionFactory &exception) {
BOOST_CHECK_EQUAL(exception.getCode(),
dynamicgraph::ExceptionFactory::UNREFERED_SIGNAL);
}
// deregistration
try {
dynamicgraph::CustomEntity* customEntity = dynamic_cast<dynamicgraph::CustomEntity*>(&entity);
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);
} 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");
dynamicgraph::Entity &entity =
dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
output_test_stream output;
......@@ -106,7 +168,8 @@ BOOST_AUTO_TEST_CASE(displaySignalList) {
}
BOOST_AUTO_TEST_CASE(display) {
dynamicgraph::Entity& entity = dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
dynamicgraph::Entity &entity =
dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
output_test_stream output;
......@@ -115,13 +178,15 @@ BOOST_AUTO_TEST_CASE(display) {
}
BOOST_AUTO_TEST_CASE(getCommandList) {
dynamicgraph::Entity& entity = dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
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");
dynamicgraph::Entity &entity =
dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
output_test_stream output;
entity.writeGraph(output);
......@@ -130,7 +195,8 @@ BOOST_AUTO_TEST_CASE(writeGraph) {
}
BOOST_AUTO_TEST_CASE(writeCompletionList) {
dynamicgraph::Entity& entity = dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
dynamicgraph::Entity &entity =
dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
output_test_stream output;
entity.writeCompletionList(output);
......@@ -143,25 +209,33 @@ BOOST_AUTO_TEST_CASE(sendMsg) {
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");
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;
dynamicgraph::LoggerVerbosity aLoggerVerbosityLevel =
(dynamicgraph::LoggerVerbosity)i;
entity.setLoggerVerbosityLevel(aLoggerVerbosityLevel);
if (entity.getLoggerVerbosityLevel() != aLoggerVerbosityLevel) output << "Mismatch output";
std::string aBaseMsg = "Auto Test Case";
std::string aMsg = aBaseMsg + " DEBUG";
entity.sendMsg(aMsg, dynamicgraph::MSG_TYPE_DEBUG, __FILE__, __LINE__);
aMsg = aBaseMsg + " INFO";
entity.sendMsg(aMsg, dynamicgraph::MSG_TYPE_INFO, __FILE__, __LINE__);
aMsg = aBaseMsg + " WARNING";
entity.sendMsg(aMsg, dynamicgraph::MSG_TYPE_WARNING, __FILE__, __LINE__);
aMsg = aBaseMsg + " DEBUG";
entity.sendMsg(aMsg, dynamicgraph::MSG_TYPE_ERROR, __FILE__, __LINE__);
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__
};
};
......@@ -171,9 +245,11 @@ BOOST_AUTO_TEST_CASE(sendMsg) {
// WTF?
BOOST_AUTO_TEST_CASE(wtf) {
dynamicgraph::Entity& entity = dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
dynamicgraph::Entity &entity =
dynamicgraph::PoolStorage::getInstance()->getEntity("my-entity");
BOOST_CHECK_EQUAL(entity.test(), static_cast<dynamicgraph::SignalBase<int>*>(0));
BOOST_CHECK_EQUAL(entity.test(),
static_cast<dynamicgraph::SignalBase<int> *>(0));
entity.test2(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 <sstream>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/factory.h>
#include <sstream>
#define BOOST_TEST_MODULE factory
#include <boost/test/unit_test.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>
using boost::test_tools::output_test_stream;
......@@ -17,31 +22,39 @@ namespace dynamicgraph {
class CustomEntity : public Entity {
public:
static const std::string CLASS_NAME;
virtual const std::string& getClassName() const { return CLASS_NAME; }
CustomEntity(const std::string n) : Entity(n) {}
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); }
dynamicgraph::Entity *makeEntity(const std::string &objectName) {
return new dynamicgraph::CustomEntity(objectName);
}
BOOST_AUTO_TEST_CASE(constructor) { dynamicgraph::FactoryStorage::getInstance(); }
BOOST_AUTO_TEST_CASE(constructor) {
dynamicgraph::FactoryStorage::getInstance();
}
BOOST_AUTO_TEST_CASE(registerEntity) {
dynamicgraph::FactoryStorage::getInstance()->registerEntity("myEntity", &makeEntity);
dynamicgraph::FactoryStorage::getInstance()->registerEntity("myEntity",
&makeEntity);
try {
dynamicgraph::FactoryStorage::getInstance()->registerEntity("myEntity", &makeEntity);
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);
} 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);
} catch (const dynamicgraph::ExceptionFactory &exception) {
BOOST_CHECK_EQUAL(exception.getCode(),
dynamicgraph::ExceptionFactory::OBJECT_CONFLICT);
}
}
......@@ -51,51 +64,61 @@ BOOST_AUTO_TEST_CASE(unregisterEntity) {
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);
} catch (const dynamicgraph::ExceptionFactory &exception) {
BOOST_CHECK_EQUAL(exception.getCode(),
dynamicgraph::ExceptionFactory::OBJECT_CONFLICT);
}
try {
dynamicgraph::FactoryStorage::getInstance()->deregisterEntity("I do not exist.");
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);
} 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);
dynamicgraph::FactoryStorage::getInstance()->registerEntity("myEntity",
&makeEntity);
{
boost::shared_ptr<dynamicgraph::Entity> entity(
dynamicgraph::FactoryStorage::getInstance()->newEntity("myEntity", "foo"));
dynamicgraph::FactoryStorage::getInstance()->newEntity("myEntity",
"foo"));
boost::shared_ptr<dynamicgraph::Entity> entity2(
dynamicgraph::FactoryStorage::getInstance()->newEntity("myEntity", "foo2"));
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.", "");
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);
} 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);
} 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("myEntity2"));
BOOST_CHECK(!dynamicgraph::FactoryStorage::getInstance()->existEntity(""));
}
// Copyright 2010 Thomas Moulard.
//
#include <sstream>
#include <dynamic-graph/exception-factory.h>
#include <dynamic-graph/interpreter.h>
#include <dynamic-graph/plugin-loader.h>
#define BOOST_TEST_MODULE tracer
#include <boost/test/unit_test.hpp>
#include <boost/test/output_test_stream.hpp>
#include "interpreter.h"
using boost::test_tools::output_test_stream;
// Check that plug-in loading/unloading is working.
BOOST_AUTO_TEST_CASE(cmd_tracer) {
dynamicgraph::PluginLoader pl;
// Push paths.
{
RUN_COMMAND("pushImportPaths", TESTS_DATADIR);
BOOST_CHECK(output.is_empty());
}
{
RUN_COMMAND("pushImportPaths", TESTS_PLUGINDIR);
BOOST_CHECK(output.is_empty());
}
// Import tracer.dg
{
RUN_COMMAND("import", "interpreter-tracer.dg");
BOOST_CHECK(output.is_empty());
}
}
// Copyright 2010 Thomas Moulard.
//
#include <sstream>
#include <iostream>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/factory.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
#include <boost/test/unit_test.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>
using boost::test_tools::output_test_stream;
......@@ -23,15 +28,18 @@ struct MyEntity : public dynamicgraph::Entity {
dynamicgraph::SignalPtr<double, int> m_sigdSIN;
dynamicgraph::SignalTimeDependent<double, int> m_sigdTimeDepSOUT;
MyEntity(const std::string &name)
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,
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 void display(std::ostream &os) const {
os << "Hello! My name is " << getName() << " !" << std::endl;
}
virtual const std::string &getClassName() const { return CLASS_NAME; }
......@@ -47,12 +55,14 @@ 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");
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");
dg::Entity *entity2 = dg::FactoryStorage::getInstance()->newEntity(
"MyEntity", "MyEntityInst");
bool res2 = (entity2 == entity);
BOOST_CHECK(res2);
......@@ -86,19 +96,23 @@ BOOST_AUTO_TEST_CASE(pool_display) {
BOOST_CHECK(res);
/// Testing entityMap
const dg::PoolStorage::Entities &anEntityMap = dg::PoolStorage::getInstance()->getEntityMap();
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);
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\nprint\nsignals\nsignalDep\n"));
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");
......@@ -112,12 +126,15 @@ BOOST_AUTO_TEST_CASE(pool_display) {
std::string str_to_test =
"/* This graph has been automatically generated.\n"
" 2019 Month: 2 Day: 28 Time: 11:28 */\n"
"digraph \"output\" { graph [ label=\"output\" bgcolor = white rankdir=LR ]\n"
"\t node [ fontcolor = black, color = black, fillcolor = gold1, style=filled, shape=box ] ; \n"
"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"
" fontcolor = black, color = black, fillcolor=cyan, style=filled,"
" shape=box ]\n"
"}\n";
/// Check the two substring (remove the date) -
......@@ -125,22 +142,28 @@ BOOST_AUTO_TEST_CASE(pool_display) {
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::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());
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);
dg::SignalBase<int> &aSignal =
dg::PoolStorage::getInstance()->getSignal(an_iss);
std::string aSignalName = aSignal.getName();
testExistence = aSignalName == "MyEntity(MyEntityInst)::input(double)::in_double";
testExistence =
aSignalName == "MyEntity(MyEntityInst)::input(double)::in_double";
BOOST_CHECK(testExistence);
/// Test name of an unvalid signal.
......@@ -157,7 +180,8 @@ BOOST_AUTO_TEST_CASE(pool_display) {
dg::PoolStorage::getInstance()->deregisterEntity(entity->getName());
/// Testing the existance of an entity
testExistence = dg::PoolStorage::getInstance()->existEntity("MyEntityInst", entity);
testExistence =
dg::PoolStorage::getInstance()->existEntity("MyEntityInst", entity);
BOOST_CHECK(!testExistence);
......
......@@ -13,11 +13,14 @@
#define BOOST_TEST_MODULE real_time_logger
#include <boost/test/unit_test.hpp>
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
#include <boost/test/output_test_stream.hpp>
#include <boost/thread/thread.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;
......@@ -56,10 +59,11 @@ BOOST_AUTO_TEST_CASE(multithread) {
pthread_setschedparam(pthread_self(), threadPolicy, &threadParam);
}
RealTimeLogger& rtl = RealTimeLogger::instance();
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 < 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;
......
......@@ -2,17 +2,21 @@
// Created by corentin on 7/3/19.
//
#include <boost/foreach.hpp>
#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 <dynamic-graph/signal-cast-helper.h>
#include <assert.h>
#include <boost/test/unit_test.hpp>
#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
......@@ -45,22 +49,129 @@ BOOST_AUTO_TEST_CASE(test_array) {
}
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) {
DefaultCastRegisterer<int> defaultCR;
std::istringstream iss;
iss.str("1");
defaultCR.cast(iss);
signal_io<int>::cast(iss);
try {
{
std::istringstream iss_fail;
iss.str("test");
defaultCR.cast(iss_fail);
} catch (ExceptionSignal e) {
// Take int, not string
BOOST_CHECK_THROW(signal_io<int>::cast(iss_fail), ExceptionSignal);
}
}
\ No newline at end of file
/// 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 <string>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <Eigen/Dense>
#include <dynamic-graph/debug.h>
#include <dynamic-graph/eigen-io.h>
#include <dynamic-graph/entity.h>
#include <dynamic-graph/factory.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/eigen-io.h>
#include <dynamic-graph/linear-algebra.h>
#include <dynamic-graph/signal-caster.h>
#include <dynamic-graph/pool.h>
#include <dynamic-graph/signal.h>
#include <dynamic-graph/signal-cast-helper.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
#include <boost/test/unit_test.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>
using boost::test_tools::output_test_stream;
......@@ -32,61 +32,6 @@ using boost::test_tools::output_test_stream;
typedef Eigen::VectorXd Vector;
typedef Eigen::MatrixXd Matrix;
struct EigenCastRegisterer_V : public dynamicgraph::SignalCastRegisterer {
typedef Vector bnuVector;
EigenCastRegisterer_V() : SignalCastRegisterer(typeid(bnuVector), dispVector, castVector, traceVector) {}
static boost::any castVector(std::istringstream& iss) {
bnuVector res;
iss >> res;
return res;
}
static void dispVector(const boost::any& object, std::ostream& os) {
const bnuVector& v = boost::any_cast<bnuVector>(object);
os << "[ ";
for (int i = 0; i < v.size(); ++i) os << v(i) << " ";
os << " ];" << std::endl;
}
static void traceVector(const boost::any& object, std::ostream& os) {
const bnuVector& v = boost::any_cast<bnuVector>(object);
for (int i = 0; i < v.size(); ++i) os << v(i) << " ";
os << std::endl;
}
};
template <typename Derived>
struct EigenCastRegisterer_M : public dynamicgraph::SignalCastRegisterer {
typedef Matrix bnuMatrix;
EigenCastRegisterer_M() : SignalCastRegisterer(typeid(bnuMatrix), dispMatrix, castMatrix, traceMatrix) {}
static boost::any castMatrix(std::istringstream& iss) {
bnuMatrix res;
iss >> res;
return res;
}
static void dispMatrix(const boost::any& object, std::ostream& os) {
const bnuMatrix& m = boost::any_cast<bnuMatrix>(object);
os << m << std::endl;
}
static void traceMatrix(const boost::any& object, std::ostream& os) {
const bnuMatrix& m = boost::any_cast<bnuMatrix>(object);
os << m << std::endl;
}
};
EigenCastRegisterer_V myVectorCast;
EigenCastRegisterer_M<int> myMatrixCast;
// Define a new cast with a type that supports streaming operators to
// and from it (this could be automated with macros).
dynamicgraph::DefaultCastRegisterer<bool> myBooleanCast;
// Check standard double cast registerer.
BOOST_AUTO_TEST_CASE(standard_double_registerer) {
dynamicgraph::Signal<double, int> mySignal("out");
......@@ -94,17 +39,17 @@ BOOST_AUTO_TEST_CASE(standard_double_registerer) {
typedef std::pair<std::string, std::string> test_t;
std::vector<test_t> values;
values.push_back(std::make_pair("42.0", "42\n"));
values.push_back(std::make_pair("42.5", "42.5\n"));
values.push_back(std::make_pair("-12.", "-12\n"));
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\n"));
values.push_back(std::make_pair("-inf", "-inf\n"));
values.push_back(std::make_pair("nan", "nan\n"));
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) {
BOOST_FOREACH (const test_t &test, values) {
// Set
std::istringstream value(test.first);
mySignal.set(value);
......@@ -113,14 +58,14 @@ BOOST_AUTO_TEST_CASE(standard_double_registerer) {
{
output_test_stream output;
mySignal.get(output);
BOOST_CHECK(output.is_equal(test.second));
BOOST_CHECK_EQUAL(output.str(), test.second);
}
// Trace
{
output_test_stream output;
mySignal.trace(output);
BOOST_CHECK(output.is_equal(test.second));
BOOST_CHECK_EQUAL(output.str(), test.second);
}
}
......@@ -154,7 +99,7 @@ BOOST_AUTO_TEST_CASE(custom_vector_registerer) {
output_test_stream output;
myVectorSignal.get(output);
boost::format fmt("[ %d %d %d %d %d ];\n");
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()));
......@@ -166,7 +111,7 @@ BOOST_AUTO_TEST_CASE(custom_vector_registerer) {
try {
std::istringstream ss("test");
myVectorSignal.set(ss);
} catch (ExceptionSignal e) {
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[0] != \"[\"";
}
......@@ -174,7 +119,7 @@ BOOST_AUTO_TEST_CASE(custom_vector_registerer) {
try {
std::istringstream ss("[test");
myVectorSignal.set(ss);
} catch (ExceptionSignal e) {
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[1] != %i";
}
......@@ -182,7 +127,7 @@ BOOST_AUTO_TEST_CASE(custom_vector_registerer) {
try {
std::istringstream ss("[5[");
myVectorSignal.set(ss);
} catch (ExceptionSignal e) {
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[2] != \"]\"";
}
......@@ -190,7 +135,7 @@ BOOST_AUTO_TEST_CASE(custom_vector_registerer) {
try {
std::istringstream ss("[5]test");
myVectorSignal.set(ss);
} catch (ExceptionSignal e) {
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[3] != \"(\"";
}
......@@ -198,15 +143,22 @@ BOOST_AUTO_TEST_CASE(custom_vector_registerer) {
try {
std::istringstream ss("[5](1, ");
myVectorSignal.set(ss);
} catch (ExceptionSignal e) {
BOOST_ERROR("Can't happened");
} 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) {
} 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] != \")\"";
}
}
......@@ -227,7 +179,7 @@ BOOST_AUTO_TEST_CASE(custom_matrix_registerer) {
try {
std::istringstream ss("test");
myMatrixSignal.set(ss);
} catch (ExceptionSignal e) {
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[0] != \"[\"";
}
......@@ -235,7 +187,7 @@ BOOST_AUTO_TEST_CASE(custom_matrix_registerer) {
try {
std::istringstream ss("[test");
myMatrixSignal.set(ss);
} catch (ExceptionSignal e) {
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[1] != %i";
}
......@@ -243,7 +195,7 @@ BOOST_AUTO_TEST_CASE(custom_matrix_registerer) {
try {
std::istringstream ss("[5[");
myMatrixSignal.set(ss);
} catch (ExceptionSignal e) {
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[2] != \",\"";
}
......@@ -251,7 +203,7 @@ BOOST_AUTO_TEST_CASE(custom_matrix_registerer) {
try {
std::istringstream ss("[5,c");
myMatrixSignal.set(ss);
} catch (ExceptionSignal e) {
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[3] != %i";
}
......@@ -259,7 +211,7 @@ BOOST_AUTO_TEST_CASE(custom_matrix_registerer) {
try {
std::istringstream ss("[5,3[");
myMatrixSignal.set(ss);
} catch (ExceptionSignal e) {
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[4] != \"]\"";
}
......@@ -267,7 +219,7 @@ BOOST_AUTO_TEST_CASE(custom_matrix_registerer) {
try {
std::istringstream ss("[5,3]test");
myMatrixSignal.set(ss);
} catch (ExceptionSignal e) {
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[5] != \"(\"";
}
......@@ -275,7 +227,7 @@ BOOST_AUTO_TEST_CASE(custom_matrix_registerer) {
try {
std::istringstream ss("[5,3](test");
myMatrixSignal.set(ss);
} catch (ExceptionSignal e) {
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[6] != \"(\"";
}
......@@ -283,15 +235,15 @@ BOOST_AUTO_TEST_CASE(custom_matrix_registerer) {
try {
std::istringstream ss("[5,3]((1,");
myMatrixSignal.set(ss);
} catch (ExceptionSignal e) {
BOOST_ERROR("Can't happened");
} 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) {
} catch (ExceptionSignal &e) {
std::cout << ("ss[6+n] != \")\"");
}
......@@ -299,7 +251,7 @@ BOOST_AUTO_TEST_CASE(custom_matrix_registerer) {
try {
std::istringstream ss("[5,1]((1)(2)(3[");
myMatrixSignal.set(ss);
} catch (ExceptionSignal e) {
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[5] != \")\"";
}
......@@ -307,7 +259,7 @@ BOOST_AUTO_TEST_CASE(custom_matrix_registerer) {
try {
std::istringstream ss("[5,1]((1)(2)(3)[");
myMatrixSignal.set(ss);
} catch (ExceptionSignal e) {
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[5] != \")\"";
}
......@@ -315,7 +267,7 @@ BOOST_AUTO_TEST_CASE(custom_matrix_registerer) {
try {
std::istringstream ss("[3,1]((1)(2),(3)[");
myMatrixSignal.set(ss);
} catch (ExceptionSignal e) {
} catch (ExceptionSignal &e) {
std::cout << "Test passed : ss[5] != \")\" and ignore \",\"";
}
......
// Copyright 2010 Thomas Moulard.
//
#include <string>
#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 <dynamic-graph/signal-base.h>
#include <dynamic-graph/pool.h>
#include <iostream>
#include <boost/foreach.hpp>
#include <iostream>
#include <string>
#include <boost/test/unit_test_suite.hpp>
#include <boost/test/unit_test.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 <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;
BOOST_AUTO_TEST_CASE(normal_test) {
Signal<double, int> sig("sig");
SignalPtr<double, int> sigPtrA(NULL, "sigPtrA"), sigPtrB(NULL, "sigPtrB");
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");
SignalPtr<double, int> sigPtrAbstract(NULL, "sigPtrAbstract");
const SignalPtr<double, int> cstSigNotPlug(NULL, "sigNotPlug");
try {
sigNotPlug.getPtr();
} catch (ExceptionSignal e) {
} 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";
......@@ -39,21 +156,73 @@ BOOST_AUTO_TEST_CASE(normal_test) {
std::string test = "test";
try {
sig.getClassName(test);
} catch (ExceptionSignal e) {
} catch (ExceptionSignal &e) {
e.getExceptionName();
}
BOOST_CHECK(true);
SignalBase<int> &sigRef = sig;
SignalBase<int> &sigPtrARef = sigPtrA, &sigPtrBRef = sigPtrB, &sigPtrAbstractRef = sigPtrAbstract;
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);
// TODO here
/// 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();
sigPtrAbstract.getAbstractPtr();
/// 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();
......@@ -70,9 +239,17 @@ BOOST_AUTO_TEST_CASE(normal_test) {
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 = ";
......@@ -84,4 +261,60 @@ BOOST_AUTO_TEST_CASE(normal_test) {
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"));
}
}
// Copyright 2010 Thomas Moulard.
//
#include <dynamic-graph/signal-time-dependent.h>
#include <dynamic-graph/signal.h>
#include <boost/foreach.hpp>
#include <dynamic-graph/signal.h>
#include <dynamic-graph/signal-time-dependent.h>
#include <iostream>
#define BOOST_TEST_MODULE signal_time_dependent
#include <boost/test/unit_test.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>
using boost::test_tools::output_test_stream;
......@@ -20,27 +24,26 @@ template <class T>
class DummyClass {
public:
std::string proname;
std::list<sigDouble_t*> inputsig;
std::list<sigString_t*> inputsigV;
std::list<sigDouble_t *> inputsig;
std::list<sigString_t *> inputsigV;
DummyClass(const std::string& n) : proname(n), res(), call(), timedata() {}
explicit DummyClass(const std::string &n)
: proname(n), res(), call(), timedata() {}
T& fun(T& res, int t) {
T &fun(T &res, int t) {
++call;
timedata = t;
BOOST_FOREACH (sigDouble_t* ptr, inputsig)
ptr->access(timedata);
BOOST_FOREACH (sigDouble_t *ptr, inputsig) ptr->access(timedata);
BOOST_FOREACH (sigString_t* ptr, inputsigV)
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); }
void add(sigDouble_t &sig) { inputsig.push_back(&sig); }
void add(sigString_t &sig) { inputsigV.push_back(&sig); }
T operator()();
......@@ -76,7 +79,9 @@ BOOST_AUTO_TEST_CASE(signaltimedependent) {
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");
sigDouble_t sig1(boost::bind(&DummyClass<double>::fun, &pro1, _1, _2),
sig2 << sig3, "Sig1");
sigDouble_t sig7("Sig7");
sig2.setFunction(boost::bind(&DummyClass<std::string>::fun, &pro2, _1, _2));
sig3.setFunction(boost::bind(&DummyClass<double>::fun, &pro3, _1, _2));
......@@ -214,4 +219,22 @@ BOOST_AUTO_TEST_CASE(signaltimedependent) {
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);
}
......@@ -3,16 +3,21 @@
* Olivier Stasse
*
*/
#include <sstream>
#include <fstream>
#include <iostream>
#include <unistd.h>
#include <dynamic-graph/process-list.hh>
#include <fstream>
#include <iostream>
#include <sstream>
#define BOOST_TEST_MODULE debug - trace
#include <boost/test/unit_test.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>
using boost::test_tools::output_test_stream;
......
// Copyright 2011 Florent Lamiraux, Thomas Moulard.
//
#include <iostream>
#include "dynamic-graph/value.h"
#include <dynamic-graph/exception-factory.h>
#include <iostream>
#define BOOST_TEST_MODULE value
#include <boost/test/unit_test.hpp>
#include <boost/version.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>
using boost::test_tools::output_test_stream;
namespace dg = dynamicgraph;
BOOST_AUTO_TEST_CASE(value_none) {
using dg::command::Value;
Value value1;
Value value(value1);
// Similar to NaN != NaN
BOOST_CHECK(!(value1 == value));
{
output_test_stream output;
output << value1;
BOOST_CHECK(output.is_equal("Type=unknown, value="));
}
}
BOOST_AUTO_TEST_CASE(value_bool) {
using dg::command::Value;
bool abool1(false);
Value value1(abool1);
Value value = value1;
BOOST_CHECK(value1 == value);
{
output_test_stream output;
output << value1;
BOOST_CHECK(output.is_equal("Type=bool, value=0"));
}
{
output_test_stream output;
output << value;
BOOST_CHECK(output.is_equal("Type=bool, value=0"));
}
}
BOOST_AUTO_TEST_CASE(value_exceptions) {
using dg::command::Value;
Value value1;
dg::command::EitherType anet(value1);
output_test_stream output, output2;
// Check if the exception is working when calling intValue
// while we are having a none.
bool res = false;
try {
int aInt(anet);
aInt++; // silence unused variable warnings to have a stable release in the
// ros buildfarm
} catch (const dg::ExceptionAbstract &aea) {
output << aea.getExceptionName();
output2 << aea.what();
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
}
BOOST_CHECK(res);
BOOST_CHECK(output.is_equal("Abstract"));
BOOST_CHECK(output2.is_equal("value is not an int"));
// Check if the exception is working when calling boolValue
// while we are having a none.
res = false;
try {
bool abool(anet);
abool = !abool; // silence unused variable warnings to have a stable
// release in the ros buildfarm
} catch (const dg::ExceptionAbstract &aea) {
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
}
BOOST_CHECK(res);
// Check if the exception is working when calling unsignedintValue
// while we are having a none.
res = false;
try {
unsigned int aint(anet);
aint++; // silence unused variable warnings to have a stable release in the
// ros buildfarm
} catch (const dg::ExceptionAbstract &aea) {
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
}
BOOST_CHECK(res);
// Check if the exception is working when calling doubleValue
// while we are having a none.
res = false;
try {
double adouble(anet);
adouble++; // silence unused variable warnings to have a stable release in
// the ros buildfarm
} catch (const dg::ExceptionAbstract &aea) {
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
}
BOOST_CHECK(res);
// Check if the exception is working when calling floatValue
// while we are having a none.
res = false;
try {
float afloat(anet);
afloat++; // silence unused variable warnings to have a stable release in
// the ros buildfarm
} catch (const dg::ExceptionAbstract &aea) {
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
}
BOOST_CHECK(res);
// Check if the exception is working when calling stringValue
// while we are having a none.
res = false;
try {
std::string astring(anet);
} catch (const dg::ExceptionAbstract &aea) {
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
}
BOOST_CHECK(res);
// Check if the exception is working when calling vectorValue
// while we are having a none.
res = false;
try {
dg::Vector avector;
avector = anet;
} catch (const dg::ExceptionAbstract &aea) {
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
}
BOOST_CHECK(res);
// Check if the exception is working when calling matrixXdValue
// while we are having a none.
res = false;
try {
Eigen::MatrixXd amatrixXd;
amatrixXd = anet;
} catch (const dg::ExceptionAbstract &aea) {
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
}
BOOST_CHECK(res);
// Check if the exception is working when calling matrix4dValue
// while we are having a none.
res = false;
try {
Eigen::Matrix4d amatrix4d;
amatrix4d = anet;
} catch (const dg::ExceptionAbstract &aea) {
res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
}
BOOST_CHECK(res);
}
BOOST_AUTO_TEST_CASE(value_unsigned_int) {
using dg::command::Value;
unsigned int aint1(5);
Value value1(aint1);
Value value = value1;
BOOST_CHECK(value1 == value);
{
output_test_stream output;
output << value1;
BOOST_CHECK(output.is_equal("Type=unsigned int, value=5"));
}
{
output_test_stream output;
output << value;
BOOST_CHECK(output.is_equal("Type=unsigned int, value=5"));
}
}
BOOST_AUTO_TEST_CASE(value_int) {
using dg::command::Value;
int aint1(5);
Value value1(aint1);
Value value = value1;
BOOST_CHECK(value1 == value);
{
output_test_stream output;
output << value1;
BOOST_CHECK(output.is_equal("Type=int, value=5"));
}
{
output_test_stream output;
output << value;
BOOST_CHECK(output.is_equal("Type=int, value=5"));
}
}
BOOST_AUTO_TEST_CASE(value_float) {
using dg::command::Value;
float afloat1(0.5);
Value value1(afloat1);
Value value = value1;
BOOST_CHECK(value1 == value);
{
output_test_stream output;
output << value1;
BOOST_CHECK(output.is_equal("Type=float, value=0.5"));
}
{
output_test_stream output;
output << value;
BOOST_CHECK(output.is_equal("Type=float, value=0.5"));
}
}
BOOST_AUTO_TEST_CASE(value_double) {
using dg::command::Value;
double adouble1(0.5);
Value value1(adouble1);
Value value = value1;
BOOST_CHECK(value1 == value);
{
output_test_stream output;
output << value1;
BOOST_CHECK(output.is_equal("Type=double, value=0.5"));
}
{
output_test_stream output;
output << value;
BOOST_CHECK(output.is_equal("Type=double, value=0.5"));
}
}
BOOST_AUTO_TEST_CASE(value_vector) {
using dg::command::Value;
dg::Vector avector1;
avector1.resize(2);
avector1[0] = 0.5;
avector1[1] = 1.5;
Value value1(avector1);
Value value = value1;
BOOST_CHECK(value1 == value);
{
output_test_stream output;
output << value1;
BOOST_CHECK(output.is_equal("Type=vector, value=0.5\n1.5"));
}
{
output_test_stream output;
output << value;
BOOST_CHECK(output.is_equal("Type=vector, value=0.5\n1.5"));
}
}
BOOST_AUTO_TEST_CASE(value_string) {
using dynamicgraph::command::Value;
using dg::command::Value;
std::string str1("value #1");
Value value1(str1);
Value value = value1;
BOOST_CHECK(value1 == value);
{
output_test_stream output;
output << value1;
......@@ -46,3 +325,107 @@ BOOST_AUTO_TEST_CASE(value_string) {
BOOST_CHECK(output.is_equal("Type=string, value=value #2"));
}
}
BOOST_AUTO_TEST_CASE(value_matrixXd) {
using dg::command::Value;
Eigen::MatrixXd avector1;
avector1.resize(2, 2);
avector1(0, 0) = 0.5;
avector1(0, 1) = 1.5;
avector1(1, 0) = 2.5;
avector1(1, 1) = 3.5;
Value value1(avector1);
Value value = value1;
BOOST_CHECK(value1 == value);
{
output_test_stream output;
output << value1;
BOOST_CHECK(output.is_equal("Type=matrixXd, value=0.5 1.5\n2.5 3.5"));
}
{
output_test_stream output;
output << value;
BOOST_CHECK(output.is_equal("Type=matrixXd, value=0.5 1.5\n2.5 3.5"));
}
}
BOOST_AUTO_TEST_CASE(value_matrix4d) {
using dg::command::Value;
Eigen::Matrix4d avector1;
avector1.setZero();
avector1(0, 0) = 0.5;
avector1(0, 1) = 1.5;
avector1(1, 0) = 2.5;
avector1(1, 1) = 3.5;
Value value1(avector1);
Value value = value1;
BOOST_CHECK(value1 == value);
{
output_test_stream output;
output << value1;
BOOST_CHECK(
output.is_equal("Type=matrix4d, value=0.5 1.5 0 0\n"
"2.5 3.5 0 0\n 0 0 0 0\n"
" 0 0 0 0"));
}
{
output_test_stream output;
output << value;
BOOST_CHECK(
output.is_equal("Type=matrix4d, value=0.5 1.5 0 0\n"
"2.5 3.5 0 0\n 0 0 0 0\n"
" 0 0 0 0"));
}
}
BOOST_AUTO_TEST_CASE(value_values) {
using namespace dynamicgraph::command;
std::string s1("value #1");
double d1 = 0.3;
Value vs1(s1);
Value vd1(d1);
Values values;
values.push_back(vs1);
values.push_back(vd1);
Value vvalues(values);
BOOST_CHECK_EQUAL(vvalues.type(), Value::VALUES);
{ // Const ref
const Values &vs = vvalues.constValuesValue();
BOOST_CHECK_EQUAL(vs.size(), values.size());
BOOST_CHECK(vs == values);
}
{
// Cast does not work.
// dg::command::EitherType eitherType (vvalues);
// Values vs = static_cast<Values>(eitherType);
// BOOST_CHECK_EQUAL(vs.size(), values.size());
// BOOST_CHECK(vs == values);
} { // Constructor
Value vvs(vvalues);
BOOST_CHECK(vvs == vvalues);
}
{
output_test_stream output;
output << vvalues;
BOOST_CHECK(
output.is_equal("Type=values, value=[ "
"Value(Type=string, value=value #1), "
"Value(Type=double, value=0.3), "
"]"));
}
}