test_sot_loader.cpp
Go to the documentation of this file.
1 /*
2  * License BSD3
3  * Copyright 2021,
4  * Maximilien Naveau,
5  *
6  * CNRS
7  *
8  */
9 
10 #define BOOST_TEST_MODULE test_sot_loader
11 #include <dynamic-graph/entity.h>
12 #include <dynamic-graph/factory.h>
13 #include <dynamic-graph/pool.h>
14 
15 #include <boost/test/included/unit_test.hpp>
16 #include <fstream>
17 #include <iostream>
18 
19 #include "sot/core/debug.hh"
20 #include "sot/core/device.hh"
21 #include "sot/core/sot-loader.hh"
22 
23 using namespace dynamicgraph;
24 using namespace dynamicgraph::sot;
25 namespace dg = dynamicgraph;
26 
27 class TestDevice : public dg::sot::Device {
28  public:
29  TestDevice(const std::string &RobotName) : Device(RobotName) {
30  timestep_ = 0.001;
31  }
33 };
34 
35 struct TestFixture {
37  asotei_lib_file_ = LIB_PLUGIN_ABSTRACT_PATH;
38  good_python_script_ = TEST_GOOD_PYTHON_SCRIPT;
39  bad_python_script_ = TEST_BAD_PYTHON_SCRIPT;
40  }
43  std::string asotei_lib_file_;
45  std::string good_python_script_;
47  std::string bad_python_script_;
49  std::map<std::string, SensorValues> sensors;
51  std::map<std::string, ControlValues> controls;
52 };
53 
54 BOOST_AUTO_TEST_SUITE(sot_loader_test_suite)
55 
56 BOOST_FIXTURE_TEST_CASE(test_plugin_existance, TestFixture) {
57  std::ifstream file(asotei_lib_file_);
58  bool file_exists = false;
59  if (file) {
60  file_exists = true;
61  }
62  BOOST_CHECK(file_exists);
63  BOOST_CHECK(asotei_lib_file_ != "");
64 }
65 
66 BOOST_FIXTURE_TEST_CASE(test_default_behavior, TestFixture) {
67  SotLoader sot_loader;
68  sot_loader.initialization();
69  BOOST_CHECK(sot_loader.isDynamicGraphStopped());
70 }
71 
72 BOOST_FIXTURE_TEST_CASE(test_start_stop_dg, TestFixture) {
73  SotLoader sot_loader;
74  sot_loader.initialization();
75  BOOST_CHECK(sot_loader.isDynamicGraphStopped());
76  sot_loader.startDG();
77  BOOST_CHECK(!sot_loader.isDynamicGraphStopped());
78  sot_loader.stopDG();
79  BOOST_CHECK(sot_loader.isDynamicGraphStopped());
80 }
81 
82 BOOST_FIXTURE_TEST_CASE(test_option_parsing_input_file, TestFixture) {
83  SotLoader sot_loader;
84  sot_loader.setDynamicLibraryName(asotei_lib_file_);
85 
86  char argv0[] = "test_sot_loader";
87  char argv1[] = "--input-file";
88  char argv2[] = LIB_PLUGIN_ABSTRACT_PATH;
89  char *argv[] = {argv0, argv1, argv2, NULL};
90  sot_loader.parseOptions(3, argv);
91  BOOST_CHECK(sot_loader.initialization());
92 }
93 
94 BOOST_FIXTURE_TEST_CASE(test_option_parsing_sot_dynamic_library, TestFixture) {
95  SotLoader sot_loader;
96  sot_loader.setDynamicLibraryName(asotei_lib_file_);
97 
98  char argv0[] = "test_sot_loader";
99  char argv1[] = "--sot-dynamic-library";
100  char argv2[] = LIB_PLUGIN_ABSTRACT_PATH;
101  char *argv[] = {argv0, argv1, argv2, NULL};
102  sot_loader.parseOptions(3, argv);
103  BOOST_CHECK(sot_loader.initialization());
104 }
105 
106 BOOST_FIXTURE_TEST_CASE(test_sot_loader_set_dynamic_library_name, TestFixture) {
107  SotLoader sot_loader;
108  sot_loader.setDynamicLibraryName(asotei_lib_file_);
109  BOOST_CHECK(sot_loader.initialization());
110 }
111 
112 BOOST_FIXTURE_TEST_CASE(test_sot_loader_one_iteration, TestFixture) {
113  std::vector<double> controls_values;
114  SotLoader sot_loader;
115  sot_loader.setDynamicLibraryName(asotei_lib_file_);
116  sot_loader.initialization();
117  // Without running the graph:
118  sot_loader.oneIteration(sensors, controls, 1e-3);
119  BOOST_CHECK(controls.find("ctrl_map_name") == controls.end());
120  // With the graph running:
121  sot_loader.startDG();
122  std::cout << "running the graph" << std::endl;
123  sot_loader.oneIteration(sensors, controls, 1e-3);
124  std::cout << "running the graph ... done" << std::endl;
125  controls_values = controls["ctrl_map_name"].getValues();
126  BOOST_CHECK_EQUAL(controls_values.size(), 5);
127  for (auto value : controls_values) {
128  BOOST_CHECK_EQUAL(value, 3.1415);
129  }
130 }
131 
132 BOOST_FIXTURE_TEST_CASE(test_cleanup_no_init, TestFixture) {
133  SotLoader sot_loader;
134  sot_loader.cleanUp();
135 }
136 
138  SotLoader sot_loader;
139  sot_loader.setDynamicLibraryName(asotei_lib_file_);
140  sot_loader.initialization();
141  sot_loader.cleanUp();
142 }
143 
144 BOOST_FIXTURE_TEST_CASE(test_run_python_command, TestFixture) {
145  SotLoader sot_loader;
146  sot_loader.setDynamicLibraryName(asotei_lib_file_);
147  sot_loader.initialization();
148  std::string res, out, err;
149  sot_loader.runPythonCommand("1+1", res, out, err);
150  BOOST_CHECK_EQUAL(res, "2");
151  BOOST_CHECK_EQUAL(out, "");
152  BOOST_CHECK_EQUAL(err, "");
153 }
154 
155 BOOST_FIXTURE_TEST_CASE(test_run_python_command_error, TestFixture) {
156  SotLoader sot_loader;
157  sot_loader.setDynamicLibraryName(asotei_lib_file_);
158  sot_loader.initialization();
159  std::string res, out, err;
160  sot_loader.runPythonCommand("print(a)", res, out, err);
161  std::cout << std::quoted(err) << std::endl;
162  BOOST_CHECK_EQUAL(res, "");
163  BOOST_CHECK_EQUAL(out, "");
164  BOOST_CHECK_EQUAL(err,
165  "Traceback (most recent call last):\n"
166  " File \"<string>\", line 1, in <module>\n"
167  "NameError: name 'a' is not defined\n");
168 }
169 
170 BOOST_FIXTURE_TEST_CASE(test_run_python_scripts, TestFixture) {
171  SotLoader sot_loader;
172  sot_loader.setDynamicLibraryName(asotei_lib_file_);
173  sot_loader.initialization();
174  std::string err;
175  sot_loader.runPythonFile(good_python_script_, err);
176  BOOST_CHECK_EQUAL(err, "");
177 }
178 
179 BOOST_FIXTURE_TEST_CASE(test_run_python_scripts_failure, TestFixture) {
180  SotLoader sot_loader;
181  sot_loader.setDynamicLibraryName(asotei_lib_file_);
182  sot_loader.initialization();
183  std::string err;
184  sot_loader.runPythonFile(bad_python_script_, err);
185  BOOST_CHECK_EQUAL(err,
186  "Traceback (most recent call last):\n"
187  " File \"" +
188  bad_python_script_ +
189  "\", line 2, in <module>\n"
190  " print(b)\n"
191  "NameError: name 'b' is not defined\n");
192 }
193 
194 BOOST_FIXTURE_TEST_CASE(test_load_device_in_python, TestFixture) {
195  SotLoader sot_loader;
196  sot_loader.setDynamicLibraryName(asotei_lib_file_);
197  sot_loader.initialization();
198  Device device("device_name");
199  sot_loader.loadDeviceInPython(device.getName());
200  std::string res, out, err;
201  sot_loader.runPythonCommand("print(device_cpp_object.name)", res, out, err);
202  BOOST_CHECK_EQUAL(res, "None");
203  BOOST_CHECK_EQUAL(out, "device_name\n");
204  BOOST_CHECK_EQUAL(err, "");
205 }
206 
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END() MatrixHomogeneous randomM()
Definition: test_feature_generic.cpp:284
dynamicgraph::sot::SotLoader::runPythonFile
void runPythonFile(std::string ifilename, std::string &err)
Run a python script inside the embeded python interpreter.
Definition: sot-loader.hh:109
TestFixture::~TestFixture
~TestFixture()
Definition: test_sot_loader.cpp:41
dynamicgraph::sot::SotLoader::runPythonCommand
void runPythonCommand(const std::string &command, std::string &result, std::string &out, std::string &err)
Run a python command inside the embeded python interpreter.
Definition: sot-loader.cpp:165
dynamicgraph::sot::SotLoader::setDynamicLibraryName
void setDynamicLibraryName(std::string &afilename)
Specify the name of the dynamic library.
Definition: sot-loader.hh:100
dynamicgraph::sot::Device
Definition: device.hh:47
dynamicgraph
TestFixture::TestFixture
TestFixture()
Definition: test_sot_loader.cpp:36
TestDevice::~TestDevice
~TestDevice()
Definition: test_sot_loader.cpp:32
value
value
dynamicgraph::Entity::getName
const std::string & getName() const
debug.hh
res
res
dynamicgraph::sot::SotLoader
This class is loading the control part of the Stack-Of-Tasks.
Definition: sot-loader.hh:47
device.hh
dynamicgraph::sot::SotLoader::initialization
bool initialization()
Prepare the SoT framework.
Definition: sot-loader.cpp:70
dynamicgraph::sot::SotLoader::parseOptions
int parseOptions(int argc, char *argv[])
Read user input to extract the path of the SoT dynamic library.
Definition: sot-loader.cpp:43
dynamicgraph::sot::SotLoader::loadDeviceInPython
void loadDeviceInPython(const std::string &device_name)
Load the Device entity in the python global scope.
Definition: sot-loader.cpp:187
dynamicgraph::sot::SotLoader::cleanUp
void cleanUp()
Unload the library which handles the robot device.
Definition: sot-loader.cpp:131
TestFixture::sensors
std::map< std::string, SensorValues > sensors
Sensor values container.
Definition: test_sot_loader.cpp:49
TestDevice
Definition: test_sot_loader.cpp:27
TestFixture::controls
std::map< std::string, ControlValues > controls
Control values container.
Definition: test_sot_loader.cpp:51
dynamicgraph::sot::SotLoader::oneIteration
void oneIteration(std::map< std::string, SensorValues > &sensors_in, std::map< std::string, ControlValues > &control_values, const double &period)
Compute one iteration of control. Basically executes fillSensors, the SoT and the readControl.
Definition: sot-loader.cpp:171
BOOST_FIXTURE_TEST_CASE
BOOST_FIXTURE_TEST_CASE(test_plugin_existance, TestFixture)
Definition: test_sot_loader.cpp:56
TestFixture::good_python_script_
std::string good_python_script_
Path to a python srcipt to parse without error.
Definition: test_sot_loader.cpp:45
err
err
dynamicgraph::sot::SotLoader::isDynamicGraphStopped
bool isDynamicGraphStopped()
Get Status of dg.
Definition: sot-loader.hh:91
sot-loader.hh
dynamicgraph::sot
TestFixture
Definition: test_sot_loader.cpp:35
dynamicgraph::sot::SotLoader::stopDG
void stopDG()
Get Status of dg.
Definition: sot-loader.hh:97
dynamicgraph::sot::SotLoader::startDG
void startDG()
Get Status of dg.
Definition: sot-loader.hh:94
TestFixture::bad_python_script_
std::string bad_python_script_
Path to a python srcipt to parse with error.
Definition: test_sot_loader.cpp:47
TestDevice::TestDevice
TestDevice(const std::string &RobotName)
Definition: test_sot_loader.cpp:29
TestFixture::asotei_lib_file_
std::string asotei_lib_file_
Path to the AbstractSotExternalInterface test library.
Definition: test_sot_loader.cpp:43


sot-core
Author(s): Olivier Stasse, ostasse@laas.fr
autogenerated on Tue Oct 24 2023 02:26:32