GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tests/command-test.cpp Lines: 123 130 94.6 %
Date: 2023-03-15 12:04:10 Branches: 218 472 46.2 %

Line Branch Exec Source
1
/* Copyright 2019, LAAS-CNRS
2
 *
3
 * Olivier Stasse
4
 *
5
 * See LICENSE file
6
 *
7
 */
8
#include <dynamic-graph/entity.h>
9
#include <dynamic-graph/exception-factory.h>
10
11
#include <iostream>
12
#include <sstream>
13
14
#include "dynamic-graph/command-bind.h"
15
#include "dynamic-graph/factory.h"
16
#include "dynamic-graph/pool.h"
17
18
#define ENABLE_RT_LOG
19
#include <dynamic-graph/logger.h>
20
#include <dynamic-graph/real-time-logger.h>
21
22
#define BOOST_TEST_MODULE debug - logger
23
24
#if BOOST_VERSION >= 105900
25
#include <boost/test/tools/output_test_stream.hpp>
26
#else
27
#include <boost/test/output_test_stream.hpp>
28
#endif
29
#include <boost/test/unit_test.hpp>
30
31
using boost::test_tools::output_test_stream;
32
33
using namespace dynamicgraph::command;
34
35
namespace dynamicgraph {
36
class CustomEntity : public Entity {
37
 public:
38
  static const std::string CLASS_NAME;
39
  bool test_zero_arg_;
40
  bool test_one_arg_;
41
  bool test_two_args_;
42
  bool test_three_args_;
43
  bool test_four_args_;
44
  bool test_one_arg_ret_;
45
  bool test_two_args_ret_;
46
47
  virtual const std::string &getClassName() const { return CLASS_NAME; }
48
1
  explicit CustomEntity(const std::string &n) : Entity(n) {
49
1
    test_zero_arg_ = false;
50
1
    test_one_arg_ = false;
51
1
    test_two_args_ = false;
52
1
    test_three_args_ = false;
53
1
    test_four_args_ = false;
54
1
    test_one_arg_ret_ = false;
55
1
    test_two_args_ret_ = false;
56
57

1
    addCommand("0_arg", makeCommandVoid0(*this, &CustomEntity::zero_arg,
58

2
                                         docCommandVoid0("zero arg")));
59
60

1
    addCommand("1_arg", makeCommandVoid1(*this, &CustomEntity::one_arg,
61

2
                                         docCommandVoid1("one arg", "int")));
62
63

1
    addCommand("2_args",
64
1
               makeCommandVoid2(*this, &CustomEntity::two_args,
65


2
                                docCommandVoid2("two args", "int", "int")));
66
67

1
    addCommand("3_args", makeCommandVoid3(*this, &CustomEntity::three_args,
68


2
                                          docCommandVoid3("three args", "int",
69
                                                          "int", "int")));
70
71

1
    addCommand("4_args",
72
1
               makeCommandVoid4(
73
                   *this, &CustomEntity::four_args,
74



2
                   docCommandVoid4("four args", "int", "int", "int", "int")));
75
76

1
    addCommand("1_arg_r",
77
1
               makeCommandReturnType1(*this, &CustomEntity::one_arg_ret,
78

2
                                      docCommandVoid1("one arg", "int")));
79
80

1
    addCommand("2_args_r", makeCommandReturnType2(
81
                               *this, &CustomEntity::two_args_ret,
82


2
                               docCommandVoid2("two args", "int", "int")));
83
84

1
    addCommand(
85
        "cmd_verbose",
86
1
        makeCommandVerbose(*this, &CustomEntity::cmd_verbose,
87

2
                           docCommandVerbose("Display some information")));
88
89
    /// Generating an exception by adding a command which already exist
90
1
    bool res = false;
91
2
    std::string e_1_arg("1_arg");
92
    try {
93

1
      addCommand(e_1_arg, getNewStyleCommand(e_1_arg));
94
1
    } catch (dynamicgraph::ExceptionFactory &aef) {
95
1
      res = (aef.getCode() == dynamicgraph::ExceptionFactory::OBJECT_CONFLICT);
96
    }
97



1
    BOOST_CHECK(res);
98
1
  }
99
100
4
  ~CustomEntity() {}
101
102
1
  void zero_arg() { test_zero_arg_ = true; }
103
104
1
  void one_arg(const int &) { test_one_arg_ = true; }
105
106
1
  void two_args(const int &, const int &) { test_two_args_ = true; }
107
108
1
  void three_args(const int &, const int &, const int &) {
109
1
    test_three_args_ = true;
110
1
  }
111
112
1
  void four_args(const int &, const int &, const int &, const int &) {
113
1
    test_four_args_ = true;
114
1
  }
115
116
1
  int one_arg_ret(const int &) {
117
1
    test_one_arg_ret_ = true;
118
1
    return 2;
119
  }
120
121
1
  std::string two_args_ret(const int &, const int &) {
122
1
    test_two_args_ret_ = true;
123
1
    return std::string("return");
124
  }
125
126
  void cmd_verbose(std::ostream &oss) {
127
    std::string as("print verbose");
128
    oss << as;
129
  }
130
};
131
1
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity");
132
}  // namespace dynamicgraph
133
134
















4
BOOST_AUTO_TEST_CASE(command_test) {
135
  dynamicgraph::CustomEntity *ptr_entity =
136
      (dynamic_cast<dynamicgraph::CustomEntity *>(
137


4
          dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity",
138
2
                                                                 "my-entity")));
139
2
  dynamicgraph::CustomEntity &entity = *ptr_entity;
140
141
  std::map<const std::string, Command *> aCommandMap =
142
4
      entity.getNewStyleCommandMap();
143
144
2
  std::map<const std::string, Command *>::iterator it_map;
145
146

2
  it_map = aCommandMap.find("0_arg");
147



2
  if (it_map == aCommandMap.end()) BOOST_CHECK(false);
148
2
  it_map->second->execute();
149



2
  BOOST_CHECK(entity.test_zero_arg_);
150
151
2
  int first_arg = 1;
152
4
  Value aValue(first_arg);
153
4
  std::vector<std::string> vec_fname(4);
154
2
  vec_fname[0] = "1_arg";
155
2
  vec_fname[1] = "2_args";
156
2
  vec_fname[2] = "3_args";
157
2
  vec_fname[3] = "4_args";
158
4
  std::vector<Value> values;
159
160
10
  for (unsigned int i = 0; i < 4; i++) {
161
8
    it_map = aCommandMap.find(vec_fname[i]);
162



8
    if (it_map == aCommandMap.end()) BOOST_CHECK(false);
163
8
    values.push_back(aValue);
164
8
    it_map->second->setParameterValues(values);
165
8
    it_map->second->execute();
166
8
    it_map->second->owner();
167
8
    it_map->second->getDocstring();
168
  }
169
170



2
  BOOST_CHECK(entity.test_one_arg_);
171



2
  BOOST_CHECK(entity.test_two_args_);
172



2
  BOOST_CHECK(entity.test_three_args_);
173



2
  BOOST_CHECK(entity.test_four_args_);
174
175
  // With return type.
176
2
  vec_fname.clear();
177

2
  vec_fname.push_back(std::string("1_arg_r"));
178

2
  vec_fname.push_back(std::string("2_args_r"));
179
2
  values.clear();
180
181
6
  for (unsigned int i = 0; i < 2; i++) {
182
4
    it_map = aCommandMap.find(vec_fname[i]);
183
4
    if (it_map == aCommandMap.end()) {
184
      BOOST_CHECK(false);
185
      exit(-1);
186
    }
187
4
    values.push_back(aValue);
188
4
    it_map->second->setParameterValues(values);
189
4
    Value aValue = it_map->second->execute();
190
4
    it_map->second->owner();
191
4
    it_map->second->getDocstring();
192
  }
193
194



2
  BOOST_CHECK(entity.test_one_arg_ret_);
195



2
  BOOST_CHECK(entity.test_two_args_ret_);
196
197
4
  std::vector<Value> values_two;
198
2
  values_two.push_back(aValue);
199
  /// Wrong number of arguments
200
2
  bool res = false;
201

2
  it_map = aCommandMap.find(std::string("2_args"));
202
  try {
203
2
    it_map->second->setParameterValues(values_two);
204
2
  } catch (const dynamicgraph::ExceptionAbstract &aea) {
205
2
    res = (aea.getCode() == dynamicgraph::ExceptionAbstract::ABSTRACT);
206
  }
207



2
  BOOST_CHECK(res);
208
209
2
  double snd_arg_db = 10.0;
210
4
  Value aValue2(snd_arg_db);
211
2
  values_two.push_back(aValue2);
212
213
  /// Wrong types of arguments
214
2
  res = false;
215

2
  it_map = aCommandMap.find(std::string("2_args"));
216
  try {
217
2
    it_map->second->setParameterValues(values_two);
218
2
  } catch (const dynamicgraph::ExceptionAbstract &aea) {
219
2
    res = (aea.getCode() == dynamicgraph::ExceptionAbstract::TOOLS);
220
  }
221



2
  BOOST_CHECK(res);
222
223
  /// Try to find the command 1_arg
224
2
  res = false;
225
2
  entity.getNewStyleCommand(vec_fname[0]);
226



2
  BOOST_CHECK(true);
227
228
  /// Generate an exception by searching a command with an empty name.w
229
4
  std::string empty("");
230
  try {
231
2
    entity.getNewStyleCommand(empty);
232
2
  } catch (dynamicgraph::ExceptionFactory &aef) {
233
2
    res = (aef.getCode() == dynamicgraph::ExceptionFactory::UNREFERED_FUNCTION);
234
  }
235



2
  BOOST_CHECK(res);
236
237
  /// delete the entity.
238
2
  delete ptr_entity;
239
2
}