GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/catkin/dg_control/dynamic_graph_manager/tests/test_device.cpp Lines: 102 102 100.0 %
Date: 2020-04-15 11:50:02 Branches: 300 848 35.4 %

Line Branch Exec Source
1
/**
2
 * @file test_device.cpp
3
 * @author Maximilien Naveau (maximilien.naveau@gmail.com)
4
 * @license License BSD-3-Clause
5
 * @copyright Copyright (c) 2019, New York University and Max Planck Gesellschaft.
6
 * @date 2019-05-22
7
 */
8
9
#include <ostream>
10
#include <cstdlib>      // std::rand, std::srand
11
#include <gtest/gtest.h>
12
#include <yaml-cpp/yaml.h>
13
#include <dynamic-graph/pool.h>
14
#include <dynamic-graph/factory.h>
15
#include <dynamic_graph_manager/device.hh>
16
17
using namespace dynamic_graph;
18
namespace dg = dynamicgraph;
19
20
/**
21
 * @brief The DISABLED_TestDevice class is used to disable test.
22
 */
23
class DISABLED_TestDevice : public ::testing::Test {};
24
25
/**
26
 * @brief The TestDevice class: test suit template for setting up
27
 * the unit tests for the Device.
28
 */
29
7
class TestDevice : public ::testing::Test {
30
31
public:
32
7
   TestDevice(): ::testing::Test(),
33
7
     dg_pool_(dg::g_pool()),
34
14
     entity_map_(dg_pool_.getEntityMap())
35
7
   {}
36
37
protected:
38
  /**
39
   * @brief SetUp, is executed before the unit tests
40
   */
41
7
  void SetUp() {
42
7
    dg_pool_.getInstance();
43

21
    YAML::Node params = YAML::LoadFile(TEST_CONFIG_PATH +
44
21
                            std::string("simple_robot.yaml"));
45

7
    params_ = params["device"];
46
7
  }
47
48
  /**
49
   * @brief TearDown, is executed after teh unit tests
50
   */
51
7
  void TearDown() {
52
7
    dg_pool_.destroy();
53
7
  }
54
55
  YAML::Node params_;
56
  YAML::Node empty_params_;
57
58
  dynamicgraph::PoolStorage& dg_pool_;
59
  const dg::PoolStorage::Entities& entity_map_;
60
};
61
62
/**
63
 * @brief test_start_stop_ros_services, test the start/stop dynamic graph ROS
64
 * services
65
 */
66
5
TEST_F(TestDevice, test_get_class_name)
67
{
68

2
  Device device("banana");
69
1
  device.initialize(empty_params_);
70



1
  ASSERT_EQ("Device", device.CLASS_NAME);
71



1
  ASSERT_EQ("Device", device.getClassName());
72




1
  ASSERT_EQ("banana", device.getName());
73
}
74
75
5
TEST_F(TestDevice, test_constructor)
76
{
77

2
  Device device("banana");
78
1
  device.initialize(empty_params_);
79



1
  ASSERT_EQ(device.sensors_map_.size(), 0);
80



1
  ASSERT_EQ(device.sensors_out_.size(), 0);
81



1
  ASSERT_EQ(device.motor_controls_map_.size(), 0);
82




1
  ASSERT_EQ(device.motor_controls_in_.size(), 0);
83
}
84
85
5
TEST_F(TestDevice, test_destructor)
86
{
87
  {
88

2
    Device device("banana");
89
1
    device.initialize(empty_params_);
90





1
    ASSERT_EQ(entity_map_.count("banana"), 1);
91
  }
92




1
  ASSERT_EQ(entity_map_.count("banana"), 0);
93
}
94
95
5
TEST_F(TestDevice, test_parse_yaml_file)
96
{
97

2
  Device device("simple_robot");
98
1
  device.initialize(params_);
99

2
  const Device::SignalMap& sig_map = device.getSignalMap();
100




1
  ASSERT_EQ(sig_map.count("encoders"), 1);
101




1
  ASSERT_EQ(sig_map.count("imu_accelerometer"), 1);
102




1
  ASSERT_EQ(sig_map.count("imu_gyroscope"), 1);
103




1
  ASSERT_EQ(sig_map.count("imu"), 1);
104




1
  ASSERT_EQ(sig_map.count("torques"), 1);
105




1
  ASSERT_EQ(sig_map.count("positions"), 1);
106





1
  ASSERT_EQ(device.sensors_out_["encoders"]->accessCopy().size(), 5);
107





1
  ASSERT_EQ(device.sensors_out_["imu_accelerometer"]->accessCopy().size(), 3);
108





1
  ASSERT_EQ(device.sensors_out_["imu_gyroscope"]->accessCopy().size(), 3);
109





1
  ASSERT_EQ(device.sensors_out_["imu"]->accessCopy().size(), 6);
110





1
  ASSERT_EQ(device.motor_controls_in_["torques"]->accessCopy().size(), 5);
111






1
  ASSERT_EQ(device.motor_controls_in_["positions"]->accessCopy().size(), 5);
112
}
113
114
5
TEST_F(TestDevice, test_set_sensors_from_map)
115
{
116
  // create the device
117

2
  Device device("simple_robot");
118
1
  device.initialize_maps(params_);
119
120
  // prepare some randomness
121
1
  srand(static_cast<unsigned>(time(nullptr)));
122
2
  std::vector<double> rand_vec;
123
124
  // create the sensors map
125
2
  VectorDGMap sensors;
126
127
  // fill in the sensor map
128


1
  sensors["encoders"] = dg::Vector::Random(5);
129


1
  sensors["imu_accelerometer"] = dg::Vector::Random(3);
130


1
  sensors["imu_gyroscope"] = dg::Vector::Random(3);
131


1
  sensors["imu"] = dg::Vector::Random(6);
132
133
1
  device.set_sensors_from_map(sensors);
134
135






1
  ASSERT_TRUE(sensors["encoders"].isApprox(
136
        device.sensors_out_["encoders"]->accessCopy()));
137






1
  ASSERT_TRUE(sensors["imu_accelerometer"].isApprox(
138
        device.sensors_out_["imu_accelerometer"]->accessCopy()));
139






1
  ASSERT_TRUE(sensors["imu_gyroscope"].isApprox(
140
        device.sensors_out_["imu_gyroscope"]->accessCopy()));
141







1
  ASSERT_TRUE(sensors["imu"].isApprox(
142
        device.sensors_out_["imu"]->accessCopy()));
143
}
144
145
1
class SimpleRobot: public Device
146
{
147
public:
148
  static const std::string CLASS_NAME;
149
1
  SimpleRobot(std::string RobotName):
150
1
    Device(RobotName)
151
  {
152


1
    initialize(YAML::LoadFile(TEST_CONFIG_PATH + std::string("simple_robot.yaml")));
153

1
    sig_before_.reset(new OutSignal("SimpleRobot::before::sigbefore"));
154

1
    sig_after_.reset(new OutSignal("SimpleRobot::after::sigafter"));
155

1
    sig_before_->setConstant(dg::Vector(3));
156

1
    sig_after_->setConstant(dg::Vector(3));
157

1
    signalRegistration(*sig_after_ << *sig_before_);
158

1
    periodic_call_before_.addSignal(name + ".sigbefore");
159

1
    periodic_call_after_.addSignal(name + ".sigafter");
160
1
  }
161
  std::unique_ptr<OutSignal> sig_before_;
162
  std::unique_ptr<OutSignal> sig_after_;
163
};
164
165
5
TEST_F(TestDevice, test_execute_graph)
166
{
167
  // create the device
168

2
  SimpleRobot device("simple_robot");
169
1
  device.initialize_maps(params_);
170
171
  // setup the controls
172


1
  device.motor_controls_in_["torques"]->setConstant(dg::Vector::Random(5));
173


1
  device.motor_controls_in_["positions"]->setConstant(dg::Vector::Random(5));
174
175




1
  ASSERT_EQ(device.sig_after_->getTime(), device.sig_before_->getTime());
176
1
  double time_before = device.sig_after_->getTime();
177
178
1
  device.execute_graph();
179




1
  ASSERT_EQ(device.sig_after_->getTime(), device.sig_before_->getTime());
180




1
  ASSERT_EQ(device.sig_after_->getTime(), time_before + 1);
181




1
  ASSERT_EQ(device.sig_before_->getTime(), time_before + 1);
182
}
183
184
5
TEST_F(TestDevice, test_get_controls_to_map)
185
{
186
  // create the device
187

2
  Device device("simple_robot");
188
1
  device.initialize_maps(params_);
189
190
  // prepare some randomness
191
1
  srand(static_cast<unsigned>(time(nullptr)));
192
2
  std::vector<double> rand_vec;
193
194
  // create the controls map
195
2
  VectorDGMap motor_controls;
196

1
  motor_controls["torques"].resize(5);
197

1
  motor_controls["positions"].resize(5);
198
199
  // setup the controls
200


1
  device.motor_controls_in_["torques"]->setConstant(dg::Vector::Random(5));
201


1
  device.motor_controls_in_["positions"]->setConstant(dg::Vector::Random(5));
202
203
1
  device.get_controls_to_map(motor_controls);
204
205


1
  Eigen::Map<dg::Vector> torques(&motor_controls["torques"][0], 5);
206


1
  Eigen::Map<dg::Vector> positions(&motor_controls["positions"][0], 5);
207
208





1
  ASSERT_TRUE(torques.isApprox(
209
                device.motor_controls_in_["torques"]->accessCopy()));
210






1
  ASSERT_TRUE(positions.isApprox(
211
                device.motor_controls_in_["positions"]->accessCopy()));
212

3
}
213