From fa7f705fd0edcbdb89b812842c40952ef7f22bf2 Mon Sep 17 00:00:00 2001 From: Olivier Stasse <ostasse@laas.fr> Date: Fri, 1 Mar 2019 01:22:55 +0100 Subject: [PATCH] [tests] debug-trace.cpp: detect robustly trace output. pool.cpp: test exception catching. --- tests/CMakeLists.txt | 1 + tests/debug-trace.cpp | 113 ++++++++++++++++++++++++++++++++++++++++++ tests/tracer.cpp | 62 +++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 tests/debug-trace.cpp create mode 100644 tests/tracer.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 36cd5d1..a3c2dfe 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -68,3 +68,4 @@ DYNAMIC_GRAPH_TEST(signal-time-dependent) DYNAMIC_GRAPH_TEST(value) DYNAMIC_GRAPH_TEST(signal-ptr) DYNAMIC_GRAPH_TEST(real-time-logger) +DYNAMIC_GRAPH_TEST(debug-trace) diff --git a/tests/debug-trace.cpp b/tests/debug-trace.cpp new file mode 100644 index 0000000..33bf8e7 --- /dev/null +++ b/tests/debug-trace.cpp @@ -0,0 +1,113 @@ +/* Copyright 2019, LAAS-CNRS + * + * Olivier Stasse + * + * See LICENSE file + * + */ +#include <sstream> +#include <iostream> +#include <dynamic-graph/entity.h> +#include <dynamic-graph/exception-factory.h> +#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 + +#include <boost/test/unit_test.hpp> +#include <boost/test/output_test_stream.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; + } + 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"); +} + +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"))); + 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. + std::cout << str_to_test << std::endl; + std::cout << oss_debug_file.str() << std::endl; + two_sub_string_identical = str_to_test == oss_debug_file.str(); + + BOOST_CHECK(two_sub_string_identical); + +} + + diff --git a/tests/tracer.cpp b/tests/tracer.cpp new file mode 100644 index 0000000..f7f7a26 --- /dev/null +++ b/tests/tracer.cpp @@ -0,0 +1,62 @@ +/* Copyright 2019, LAAS-CNRS + * + * Olivier Stasse + * + * See LICENSE file + * + */ + +#include <dynamic-graph/entity.h> +#include <dynamic-graph/exception-factory.h> +#include <dynamic-graph/factory.h> +#include <dynamic-graph/pool.h> + +#include <dynamic-graph/tracer.h> + +struct MyEntity : public dynamicgraph::Entity +{ + static const std::string CLASS_NAME; + + dynamicgraph::SignalPtr<double, int> m_sigdSIN; + dynamicgraph::SignalTimeDependent<double, int> m_sigdTimeDepSOUT; + + 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; + } + +}; + +BOOST_AUTO_TEST_CASE(test_tracer) +{ + + // Creates a tracer. + dynamicgraph::Entity& entity = + *dynamicgraph::FactoryStorage::getInstance()->newEntity("Tracer", + "my-tracer"); + + + +} -- GitLab