GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tests/debug-trace.cpp Lines: 36 38 94.7 %
Date: 2023-03-15 12:04:10 Branches: 72 142 50.7 %

Line Branch Exec Source
1
/* Copyright 2019, LAAS-CNRS
2
 *
3
 * Olivier Stasse
4
 *
5
 */
6
#include <dynamic-graph/entity.h>
7
#include <dynamic-graph/exception-factory.h>
8
9
#include <iostream>
10
#include <sstream>
11
12
#include "dynamic-graph/factory.h"
13
#include "dynamic-graph/pool.h"
14
15
#define VP_DEBUG 1
16
#define VP_DEBUG_MODE 50
17
#define VP_TEMPLATE_DEBUG_MODE 50
18
19
#include <dynamic-graph/debug.h>
20
21
#define BOOST_TEST_MODULE debug - trace
22
23
#if BOOST_VERSION >= 105900
24
#include <boost/test/tools/output_test_stream.hpp>
25
#else
26
#include <boost/test/output_test_stream.hpp>
27
#endif
28
#include <boost/test/unit_test.hpp>
29
30
using boost::test_tools::output_test_stream;
31
32
namespace dynamicgraph {
33
class CustomEntity : public Entity {
34
 public:
35
  static const std::string CLASS_NAME;
36
  virtual const std::string &getClassName() const { return CLASS_NAME; }
37
1
  explicit CustomEntity(const std::string &n) : Entity(n) {
38
1
    dynamicgraph::dgDEBUGFLOW.openFile("/tmp/dynamic-graph-traces.txt");
39
1
  }
40
4
  ~CustomEntity() {
41
2
    dynamicgraph::dgDEBUGFLOW.closeFile("/tmp/dynamic-graph-traces.txt");
42
4
  }
43
1
  void testDebugTrace() {
44
    /// Test debugging information when entering the code.
45
1
    dgDEBUGIN(5);
46
47
    /// Intermediate test.
48
1
    dgDEBUGINOUT(5);
49
50
1
    dgDEBUG(5) << "Here is a test" << std::endl;
51
52
    /// Test debugging information when going out of the code.
53
1
    dgDEBUGOUT(5);
54
1
  }
55
};
56
1
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
57
}  // namespace dynamicgraph
58
59
















4
BOOST_AUTO_TEST_CASE(testDebugTrace) {
60


2
  BOOST_CHECK_EQUAL(dynamicgraph::CustomEntity::CLASS_NAME, "CustomEntity");
61
62
  dynamicgraph::CustomEntity *ptr_entity =
63
      (dynamic_cast<dynamicgraph::CustomEntity *>(
64


4
          dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity",
65
66
2
                                                                 "my-entity")));
67
68
2
  dynamicgraph::CustomEntity &entity = *ptr_entity;
69
2
  entity.testDebugTrace();
70
71
  /// Copy the debug file into the oss_debug_file
72

4
  output_test_stream output;
73
4
  std::fstream the_debug_file;
74
2
  the_debug_file.open(dynamicgraph::DebugTrace::DEBUG_FILENAME_DEFAULT,
75
                      std::ios::in);
76
77
  // Extract the filename and this source file from the output
78
4
  std::string astr;
79
4
  std::ostringstream oss_debug_file;
80

10
  while (std::getline(the_debug_file, astr)) {
81
8
    std::size_t found = astr.find(":");
82
16
    std::string asubstr = astr.substr(found + 1, astr.length());
83
8
    found = asubstr.find(":");
84
16
    std::string asubstr2 = asubstr.substr(found + 1, astr.length());
85
8
    oss_debug_file << asubstr2;
86
  }
87
88
2
  the_debug_file.close();
89
90
  // Compare with the strings put inside this source file
91
  std::string str_to_test =
92
      "# In {"
93
      "# In/Out { }"
94
      "Here is a test"
95
4
      "# Out }";
96
  bool two_sub_string_identical;
97
98
  // Make comparisons.
99
2
  two_sub_string_identical = str_to_test == oss_debug_file.str();
100
101



2
  BOOST_CHECK(two_sub_string_identical);
102
103
2
  delete ptr_entity;
104
2
}