signal-cast-registerer.cpp
Go to the documentation of this file.
1 // Copyright 2010 Thomas Moulard.
2 //
3 
4 #include <dynamic-graph/debug.h>
6 #include <dynamic-graph/entity.h>
9 #include <dynamic-graph/pool.h>
10 #include <dynamic-graph/signal.h>
11 
12 #include <boost/foreach.hpp>
13 #include <boost/format.hpp>
14 #include <string>
15 
19 
20 #define BOOST_TEST_MODULE signal_cast_registerer
21 
22 #if BOOST_VERSION >= 105900
23 #include <boost/test/tools/output_test_stream.hpp>
24 #else
25 #include <boost/test/output_test_stream.hpp>
26 #endif
27 #include <boost/test/unit_test.hpp>
28 #include <iostream>
29 
30 using boost::test_tools::output_test_stream;
31 
32 typedef Eigen::VectorXd Vector;
33 typedef Eigen::MatrixXd Matrix;
34 
35 // Check standard double cast registerer.
36 BOOST_AUTO_TEST_CASE(standard_double_registerer) {
37  dynamicgraph::Signal<double, int> mySignal("out");
38 
39  typedef std::pair<std::string, std::string> test_t;
40  std::vector<test_t> values;
41 
42  values.push_back(std::make_pair("42.0", "42"));
43  values.push_back(std::make_pair("42.5", "42.5"));
44  values.push_back(std::make_pair("-12.", "-12"));
45 
46  // Double special values.
47  // FIXME: these tests are failing :(
48  values.push_back(std::make_pair("inf", "inf"));
49  values.push_back(std::make_pair("-inf", "-inf"));
50  values.push_back(std::make_pair("nan", "nan"));
51 
52  BOOST_FOREACH (const test_t &test, values) {
53  // Set
54  std::istringstream value(test.first);
55  mySignal.set(value);
56 
57  // Get
58  {
59  output_test_stream output;
60  mySignal.get(output);
61  BOOST_CHECK_EQUAL(output.str(), test.second);
62  }
63 
64  // Trace
65  {
66  output_test_stream output;
67  mySignal.trace(output);
68  BOOST_CHECK_EQUAL(output.str(), test.second);
69  }
70  }
71 
72  // Check invalid values.
73  // FIXME: this test is failing for now.
74  std::istringstream value("This is not a valid double.");
75  BOOST_CHECK_THROW(mySignal.set(value), std::exception);
76 }
77 
78 // Check a custom cast registerer for Boost uBLAS vectors.
79 BOOST_AUTO_TEST_CASE(custom_vector_registerer) {
80  dynamicgraph::Signal<dynamicgraph::Vector, int> myVectorSignal("vector");
81 
82  // Print the signal name.
83  {
84  output_test_stream output;
85  output << myVectorSignal;
86  BOOST_CHECK(output.is_equal("Sig:vector (Type Cst)"));
87  }
88 
89  for (unsigned int i = 0; i < 5; ++i) {
90  Vector v = Vector::Unit(5, i);
91  std::ostringstream os;
92  os << v;
93  std::istringstream ss("[5](" + os.str() + ")");
94 
95  // Set signal value.
96  myVectorSignal.set(ss);
97 
98  // Print out signal value.
99  output_test_stream output;
100  myVectorSignal.get(output);
101 
102  boost::format fmt("%d %d %d %d %d");
103  fmt % (i == 0) % (i == 1) % (i == 2) % (i == 3) % (i == 4);
104 
105  BOOST_CHECK(output.is_equal(fmt.str()));
106  }
107 
108  // Catch Exception of ss (not good input)
109 
110  // ss[0] != "["
111  try {
112  std::istringstream ss("test");
113  myVectorSignal.set(ss);
114  } catch (ExceptionSignal &e) {
115  std::cout << "Test passed : ss[0] != \"[\"";
116  }
117 
118  // ss[1] != %i
119  try {
120  std::istringstream ss("[test");
121  myVectorSignal.set(ss);
122  } catch (ExceptionSignal &e) {
123  std::cout << "Test passed : ss[1] != %i";
124  }
125 
126  // ss[2] != "]"
127  try {
128  std::istringstream ss("[5[");
129  myVectorSignal.set(ss);
130  } catch (ExceptionSignal &e) {
131  std::cout << "Test passed : ss[2] != \"]\"";
132  }
133 
134  // ss[3] != "("
135  try {
136  std::istringstream ss("[5]test");
137  myVectorSignal.set(ss);
138  } catch (ExceptionSignal &e) {
139  std::cout << "Test passed : ss[3] != \"(\"";
140  }
141 
142  // ss[4] != ' ' || ','
143  try {
144  std::istringstream ss("[5](1, ");
145  myVectorSignal.set(ss);
146  } catch (ExceptionSignal &e) {
147  std::cout << "Test passed : ss[4] != \" \" || \",\"";
148  }
149 
150  // ss[-1] != ")"
151  try {
152  std::istringstream ss("[5](1,2,3,4,5]");
153  myVectorSignal.set(ss);
154  } catch (ExceptionSignal &e) {
155  std::cout << "Test passed : ss[-1] != \")\"";
156  }
157 
158  try {
159  output_test_stream output;
160  myVectorSignal.trace(output);
161  } catch (ExceptionSignal &e) {
162  std::cout << "Test passed : ss[-1] != \")\"";
163  }
164 }
165 
166 BOOST_AUTO_TEST_CASE(custom_matrix_registerer) {
167  dynamicgraph::Signal<dynamicgraph::Matrix, int> myMatrixSignal("matrix");
168 
169  // Print the signal name.
170  {
171  output_test_stream output;
172  output << myMatrixSignal;
173  BOOST_CHECK(output.is_equal("Sig:matrix (Type Cst)"));
174  }
175 
176  // Catch Exception of ss (not good input)
177 
178  // ss[0] != "["
179  try {
180  std::istringstream ss("test");
181  myMatrixSignal.set(ss);
182  } catch (ExceptionSignal &e) {
183  std::cout << "Test passed : ss[0] != \"[\"";
184  }
185 
186  // ss[1] != %i
187  try {
188  std::istringstream ss("[test");
189  myMatrixSignal.set(ss);
190  } catch (ExceptionSignal &e) {
191  std::cout << "Test passed : ss[1] != %i";
192  }
193 
194  // ss[2] != ","
195  try {
196  std::istringstream ss("[5[");
197  myMatrixSignal.set(ss);
198  } catch (ExceptionSignal &e) {
199  std::cout << "Test passed : ss[2] != \",\"";
200  }
201 
202  // ss[3] != %i
203  try {
204  std::istringstream ss("[5,c");
205  myMatrixSignal.set(ss);
206  } catch (ExceptionSignal &e) {
207  std::cout << "Test passed : ss[3] != %i";
208  }
209 
210  // ss[4] != "]"
211  try {
212  std::istringstream ss("[5,3[");
213  myMatrixSignal.set(ss);
214  } catch (ExceptionSignal &e) {
215  std::cout << "Test passed : ss[4] != \"]\"";
216  }
217 
218  // ss[5] != "("
219  try {
220  std::istringstream ss("[5,3]test");
221  myMatrixSignal.set(ss);
222  } catch (ExceptionSignal &e) {
223  std::cout << "Test passed : ss[5] != \"(\"";
224  }
225 
226  // ss[6] != "("
227  try {
228  std::istringstream ss("[5,3](test");
229  myMatrixSignal.set(ss);
230  } catch (ExceptionSignal &e) {
231  std::cout << "Test passed : ss[6] != \"(\"";
232  }
233 
234  // ss[8] != " " || ","
235  try {
236  std::istringstream ss("[5,3]((1,");
237  myMatrixSignal.set(ss);
238  } catch (ExceptionSignal &e) {
239  std::cout << "Test passed : ss[8] != \" \" || \",\"";
240  }
241 
242  // ss[6+n] != ")"
243  try {
244  std::istringstream ss("[5,3]((1,2,3]");
245  myMatrixSignal.set(ss);
246  } catch (ExceptionSignal &e) {
247  std::cout << ("ss[6+n] != \")\"");
248  }
249 
250  // ss[-3] != ")"
251  try {
252  std::istringstream ss("[5,1]((1)(2)(3[");
253  myMatrixSignal.set(ss);
254  } catch (ExceptionSignal &e) {
255  std::cout << "Test passed : ss[5] != \")\"";
256  }
257 
258  // ss[-3] != ")"
259  try {
260  std::istringstream ss("[5,1]((1)(2)(3)[");
261  myMatrixSignal.set(ss);
262  } catch (ExceptionSignal &e) {
263  std::cout << "Test passed : ss[5] != \")\"";
264  }
265 
266  // ss[-1]!= ")"
267  try {
268  std::istringstream ss("[3,1]((1)(2),(3)[");
269  myMatrixSignal.set(ss);
270  } catch (ExceptionSignal &e) {
271  std::cout << "Test passed : ss[5] != \")\" and ignore \",\"";
272  }
273 
274  //[...]((...))
275 }
entity.h
dynamicgraph::Signal
Signals link I/O ports of entities. They can be constant-valued signals, or copy the value of a heap ...
Definition: fwd.hh:44
linear-algebra.h
dynamicgraph::Signal::get
virtual void get(std::ostream &value) const
Definition: signal.t.cpp:37
signal-cast-register-test.h
signal-cast-registerer-libB.hh
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(standard_double_registerer)
Definition: signal-cast-registerer.cpp:36
eigen-io.h
dynamicgraph::Signal::trace
virtual void trace(std::ostream &os) const
Definition: signal.t.cpp:42
signal-cast-registerer-libA.hh
factory.h
dynamicgraph::Signal::set
virtual void set(std::istringstream &value)
Definition: signal.t.cpp:32
dynamicgraph::ExceptionSignal
Exceptions raised when an error related to signals happen.
Definition: exception-signal.h:19
Matrix
Eigen::MatrixXd Matrix
Definition: signal-cast-registerer.cpp:33
Vector
Eigen::VectorXd Vector
Definition: signal-cast-registerer.cpp:32
signal.h
debug.h
pool.h


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Fri Aug 2 2024 08:36:38