tests/value.cpp
Go to the documentation of this file.
1 // Copyright 2011 Florent Lamiraux, Thomas Moulard.
2 //
3 
4 #include "dynamic-graph/value.h"
5 
7 
8 #include <iostream>
9 
10 #define BOOST_TEST_MODULE value
11 
12 #include <boost/version.hpp>
13 
14 #if BOOST_VERSION >= 105900
15 #include <boost/test/tools/output_test_stream.hpp>
16 #else
17 #include <boost/test/output_test_stream.hpp>
18 #endif
19 #include <boost/test/unit_test.hpp>
20 
21 using boost::test_tools::output_test_stream;
22 
23 namespace dg = dynamicgraph;
24 
25 BOOST_AUTO_TEST_CASE(value_none) {
26  using dg::command::Value;
27 
28  Value value1;
29  Value value(value1);
30 
31  // Similar to NaN != NaN
32  BOOST_CHECK(!(value1 == value));
33 
34  {
35  output_test_stream output;
36  output << value1;
37  BOOST_CHECK(output.is_equal("Type=unknown, value="));
38  }
39 }
40 
41 BOOST_AUTO_TEST_CASE(value_bool) {
42  using dg::command::Value;
43 
44  bool abool1(false);
45  Value value1(abool1);
46  Value value = value1;
47 
48  BOOST_CHECK(value1 == value);
49 
50  {
51  output_test_stream output;
52  output << value1;
53  BOOST_CHECK(output.is_equal("Type=bool, value=0"));
54  }
55 
56  {
57  output_test_stream output;
58  output << value;
59  BOOST_CHECK(output.is_equal("Type=bool, value=0"));
60  }
61 }
62 
63 BOOST_AUTO_TEST_CASE(value_exceptions) {
64  using dg::command::Value;
65 
66  Value value1;
67  dg::command::EitherType anet(value1);
68  output_test_stream output, output2;
69 
70  // Check if the exception is working when calling intValue
71  // while we are having a none.
72  bool res = false;
73  try {
74  int aInt(anet);
75  aInt++; // silence unused variable warnings to have a stable release in the
76  // ros buildfarm
77  } catch (const dg::ExceptionAbstract &aea) {
78  output << aea.getExceptionName();
79  output2 << aea.what();
80  res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
81  }
82  BOOST_CHECK(res);
83  BOOST_CHECK(output.is_equal("Abstract"));
84  BOOST_CHECK(output2.is_equal("value is not an int"));
85 
86  // Check if the exception is working when calling boolValue
87  // while we are having a none.
88  res = false;
89  try {
90  bool abool(anet);
91  abool = !abool; // silence unused variable warnings to have a stable
92  // release in the ros buildfarm
93  } catch (const dg::ExceptionAbstract &aea) {
94  res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
95  }
96  BOOST_CHECK(res);
97 
98  // Check if the exception is working when calling unsignedintValue
99  // while we are having a none.
100  res = false;
101  try {
102  unsigned int aint(anet);
103  aint++; // silence unused variable warnings to have a stable release in the
104  // ros buildfarm
105  } catch (const dg::ExceptionAbstract &aea) {
106  res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
107  }
108  BOOST_CHECK(res);
109 
110  // Check if the exception is working when calling doubleValue
111  // while we are having a none.
112  res = false;
113  try {
114  double adouble(anet);
115  adouble++; // silence unused variable warnings to have a stable release in
116  // the ros buildfarm
117  } catch (const dg::ExceptionAbstract &aea) {
118  res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
119  }
120  BOOST_CHECK(res);
121 
122  // Check if the exception is working when calling floatValue
123  // while we are having a none.
124  res = false;
125  try {
126  float afloat(anet);
127  afloat++; // silence unused variable warnings to have a stable release in
128  // the ros buildfarm
129  } catch (const dg::ExceptionAbstract &aea) {
130  res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
131  }
132  BOOST_CHECK(res);
133 
134  // Check if the exception is working when calling stringValue
135  // while we are having a none.
136  res = false;
137  try {
138  std::string astring(anet);
139  } catch (const dg::ExceptionAbstract &aea) {
140  res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
141  }
142  BOOST_CHECK(res);
143 
144  // Check if the exception is working when calling vectorValue
145  // while we are having a none.
146  res = false;
147  try {
148  dg::Vector avector;
149  avector = anet;
150  } catch (const dg::ExceptionAbstract &aea) {
151  res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
152  }
153  BOOST_CHECK(res);
154 
155  // Check if the exception is working when calling matrixXdValue
156  // while we are having a none.
157  res = false;
158  try {
159  Eigen::MatrixXd amatrixXd;
160  amatrixXd = anet;
161  } catch (const dg::ExceptionAbstract &aea) {
162  res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
163  }
164  BOOST_CHECK(res);
165 
166  // Check if the exception is working when calling matrix4dValue
167  // while we are having a none.
168  res = false;
169  try {
170  Eigen::Matrix4d amatrix4d;
171  amatrix4d = anet;
172  } catch (const dg::ExceptionAbstract &aea) {
173  res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
174  }
175  BOOST_CHECK(res);
176 }
177 
178 BOOST_AUTO_TEST_CASE(value_unsigned_int) {
179  using dg::command::Value;
180 
181  unsigned int aint1(5);
182  Value value1(aint1);
183  Value value = value1;
184 
185  BOOST_CHECK(value1 == value);
186 
187  {
188  output_test_stream output;
189  output << value1;
190  BOOST_CHECK(output.is_equal("Type=unsigned int, value=5"));
191  }
192 
193  {
194  output_test_stream output;
195  output << value;
196  BOOST_CHECK(output.is_equal("Type=unsigned int, value=5"));
197  }
198 }
199 
201  using dg::command::Value;
202 
203  int aint1(5);
204  Value value1(aint1);
205  Value value = value1;
206 
207  BOOST_CHECK(value1 == value);
208 
209  {
210  output_test_stream output;
211  output << value1;
212  BOOST_CHECK(output.is_equal("Type=int, value=5"));
213  }
214 
215  {
216  output_test_stream output;
217  output << value;
218  BOOST_CHECK(output.is_equal("Type=int, value=5"));
219  }
220 }
221 
222 BOOST_AUTO_TEST_CASE(value_float) {
223  using dg::command::Value;
224 
225  float afloat1(0.5);
226  Value value1(afloat1);
227  Value value = value1;
228 
229  BOOST_CHECK(value1 == value);
230 
231  {
232  output_test_stream output;
233  output << value1;
234  BOOST_CHECK(output.is_equal("Type=float, value=0.5"));
235  }
236 
237  {
238  output_test_stream output;
239  output << value;
240  BOOST_CHECK(output.is_equal("Type=float, value=0.5"));
241  }
242 }
243 
244 BOOST_AUTO_TEST_CASE(value_double) {
245  using dg::command::Value;
246 
247  double adouble1(0.5);
248  Value value1(adouble1);
249  Value value = value1;
250 
251  BOOST_CHECK(value1 == value);
252 
253  {
254  output_test_stream output;
255  output << value1;
256  BOOST_CHECK(output.is_equal("Type=double, value=0.5"));
257  }
258 
259  {
260  output_test_stream output;
261  output << value;
262  BOOST_CHECK(output.is_equal("Type=double, value=0.5"));
263  }
264 }
265 
266 BOOST_AUTO_TEST_CASE(value_vector) {
267  using dg::command::Value;
268 
269  dg::Vector avector1;
270  avector1.resize(2);
271  avector1[0] = 0.5;
272  avector1[1] = 1.5;
273  Value value1(avector1);
274  Value value = value1;
275 
276  BOOST_CHECK(value1 == value);
277 
278  {
279  output_test_stream output;
280  output << value1;
281  BOOST_CHECK(output.is_equal("Type=vector, value=0.5\n1.5"));
282  }
283 
284  {
285  output_test_stream output;
286  output << value;
287  BOOST_CHECK(output.is_equal("Type=vector, value=0.5\n1.5"));
288  }
289 }
290 
291 BOOST_AUTO_TEST_CASE(value_string) {
292  using dg::command::Value;
293 
294  std::string str1("value #1");
295  Value value1(str1);
296  Value value = value1;
297 
298  BOOST_CHECK(value1 == value);
299 
300  {
301  output_test_stream output;
302  output << value1;
303  BOOST_CHECK(output.is_equal("Type=string, value=value #1"));
304  }
305 
306  {
307  output_test_stream output;
308  output << value;
309  BOOST_CHECK(output.is_equal("Type=string, value=value #1"));
310  }
311 
312  std::string str2("value #2");
313  Value value2(str2);
314  value = value2;
315 
316  {
317  output_test_stream output;
318  output << value2;
319  BOOST_CHECK(output.is_equal("Type=string, value=value #2"));
320  }
321 
322  {
323  output_test_stream output;
324  output << value;
325  BOOST_CHECK(output.is_equal("Type=string, value=value #2"));
326  }
327 }
328 
329 BOOST_AUTO_TEST_CASE(value_matrixXd) {
330  using dg::command::Value;
331 
332  Eigen::MatrixXd avector1;
333  avector1.resize(2, 2);
334  avector1(0, 0) = 0.5;
335  avector1(0, 1) = 1.5;
336  avector1(1, 0) = 2.5;
337  avector1(1, 1) = 3.5;
338  Value value1(avector1);
339  Value value = value1;
340 
341  BOOST_CHECK(value1 == value);
342 
343  {
344  output_test_stream output;
345  output << value1;
346  BOOST_CHECK(output.is_equal("Type=matrixXd, value=0.5 1.5\n2.5 3.5"));
347  }
348 
349  {
350  output_test_stream output;
351  output << value;
352  BOOST_CHECK(output.is_equal("Type=matrixXd, value=0.5 1.5\n2.5 3.5"));
353  }
354 }
355 
356 BOOST_AUTO_TEST_CASE(value_matrix4d) {
357  using dg::command::Value;
358 
359  Eigen::Matrix4d avector1;
360  avector1.setZero();
361  avector1(0, 0) = 0.5;
362  avector1(0, 1) = 1.5;
363  avector1(1, 0) = 2.5;
364  avector1(1, 1) = 3.5;
365  Value value1(avector1);
366  Value value = value1;
367 
368  BOOST_CHECK(value1 == value);
369 
370  {
371  output_test_stream output;
372  output << value1;
373  BOOST_CHECK(
374  output.is_equal("Type=matrix4d, value=0.5 1.5 0 0\n"
375  "2.5 3.5 0 0\n 0 0 0 0\n"
376  " 0 0 0 0"));
377  }
378 
379  {
380  output_test_stream output;
381  output << value;
382  BOOST_CHECK(
383  output.is_equal("Type=matrix4d, value=0.5 1.5 0 0\n"
384  "2.5 3.5 0 0\n 0 0 0 0\n"
385  " 0 0 0 0"));
386  }
387 }
388 
389 BOOST_AUTO_TEST_CASE(value_values) {
390  using namespace dynamicgraph::command;
391 
392  std::string s1("value #1");
393  double d1 = 0.3;
394 
395  Value vs1(s1);
396  Value vd1(d1);
397 
398  Values values;
399  values.push_back(vs1);
400  values.push_back(vd1);
401 
402  Value vvalues(values);
403 
404  BOOST_CHECK_EQUAL(vvalues.type(), Value::VALUES);
405 
406  { // Const ref
407  const Values &vs = vvalues.constValuesValue();
408  BOOST_CHECK_EQUAL(vs.size(), values.size());
409  BOOST_CHECK(vs == values);
410  }
411  {
412  // Cast does not work.
413  // dg::command::EitherType eitherType (vvalues);
414  // Values vs = static_cast<Values>(eitherType);
415  // BOOST_CHECK_EQUAL(vs.size(), values.size());
416  // BOOST_CHECK(vs == values);
417  }
418  { // Constructor
419  Value vvs(vvalues);
420  BOOST_CHECK(vvs == vvalues);
421  }
422 
423  {
424  output_test_stream output;
425  output << vvalues;
426  BOOST_CHECK(
427  output.is_equal("Type=values, value=[ "
428  "Value(Type=string, value=value #1), "
429  "Value(Type=double, value=0.3), "
430  "]"));
431  }
432 }
dynamicgraph
dynamicgraph::command::Values
std::vector< Value > Values
Definition: value.h:22
dynamicgraph::command
Definition: command-bind.h:31
dynamicgraph::command::Value::VALUES
@ VALUES
Definition: value.h:66
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(value_none)
Definition: tests/value.cpp:25
dynamicgraph::ExceptionAbstract::TOOLS
@ TOOLS
Definition: exception-abstract.h:72
dynamicgraph::ExceptionAbstract
Abstract root class for all dynamic-graph exceptions.
Definition: exception-abstract.h:31
dynamicgraph::ExceptionAbstract::getCode
int getCode() const
Access to the error code.
Definition: exception-abstract.cpp:24
dynamicgraph::Vector
Eigen::VectorXd Vector
Definition: linear-algebra.h:14
exception-factory.h
dynamicgraph::ExceptionAbstract::what
virtual const char * what() const
Definition: exception-abstract.h:94
dynamicgraph::ExceptionAbstract::getExceptionName
virtual const std::string & getExceptionName() const
Definition: exception-abstract.h:80
value.h


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