value.cpp 9.66 KB
Newer Older
Thomas Moulard's avatar
Thomas Moulard committed
1
2
3
4
// Copyright 2011 Florent Lamiraux, Thomas Moulard.
//

#include "dynamic-graph/value.h"
5
#include <dynamic-graph/exception-factory.h>
6
#include <iostream>
Thomas Moulard's avatar
Thomas Moulard committed
7
8
9
10

#define BOOST_TEST_MODULE value

#include <boost/test/output_test_stream.hpp>
11
#include <boost/test/unit_test.hpp>
Thomas Moulard's avatar
Thomas Moulard committed
12
13
14

using boost::test_tools::output_test_stream;

15
16
17
18
namespace dg = dynamicgraph;

BOOST_AUTO_TEST_CASE(value_none) {
  using dg::command::Value;
19

20
21
  Value value1;
  Value value(value1);
Joseph Mirabel's avatar
Joseph Mirabel committed
22
23
24
25

  // Similar to NaN != NaN
  BOOST_CHECK(!(value1 == value));

26
27
28
  {
    output_test_stream output;
    output << value1;
29
    BOOST_CHECK(output.is_equal("Type=unknown, value="));
30
31
32
  }
}

33
BOOST_AUTO_TEST_CASE(value_bool) {
34
  using dg::command::Value;
35
36
37

  bool abool1(false);
  Value value1(abool1);
38
  Value value = value1;
39

Joseph Mirabel's avatar
Joseph Mirabel committed
40
41
  BOOST_CHECK(value1 == value);

42
43
44
45
46
47
48
49
50
51
52
53
54
  {
    output_test_stream output;
    output << value1;
    BOOST_CHECK(output.is_equal("Type=bool, value=0"));
  }

  {
    output_test_stream output;
    output << value;
    BOOST_CHECK(output.is_equal("Type=bool, value=0"));
  }
}

55
56
57
58
59
BOOST_AUTO_TEST_CASE(value_exceptions) {
  using dg::command::Value;

  Value value1;
  dg::command::EitherType anet(value1);
60
61
  output_test_stream output, output2;

62
63
64
65
66
  // Check if the exception is working when calling intValue
  // while we are having a none.
  bool res = false;
  try {
    int aInt(anet);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
67
68
    aInt++; // silence unused variable warnings to have a stable release in the
            // ros buildfarm
69
  } catch (const dg::ExceptionAbstract &aea) {
70
71
    output << aea.getExceptionName();
    output2 << aea.what();
72
73
74
    res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
  }
  BOOST_CHECK(res);
75
76
  BOOST_CHECK(output.is_equal("Abstract"));
  BOOST_CHECK(output2.is_equal("value is not an int int"));
77
78
79
80
81
82

  // Check if the exception is working when calling boolValue
  // while we are having a none.
  res = false;
  try {
    bool abool(anet);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
83
84
    abool = !abool; // silence unused variable warnings to have a stable release
                    // in the ros buildfarm
85
86
87
88
  } catch (const dg::ExceptionAbstract &aea) {
    res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
  }
  BOOST_CHECK(res);
89

90
91
92
93
94
  // Check if the exception is working when calling unsignedintValue
  // while we are having a none.
  res = false;
  try {
    unsigned int aint(anet);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
95
96
    aint++; // silence unused variable warnings to have a stable release in the
            // ros buildfarm
97
98
99
100
101
102
103
104
105
106
  } catch (const dg::ExceptionAbstract &aea) {
    res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
  }
  BOOST_CHECK(res);

  // Check if the exception is working when calling doubleValue
  // while we are having a none.
  res = false;
  try {
    double adouble(anet);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
107
108
    adouble++; // silence unused variable warnings to have a stable release in
               // the ros buildfarm
109
110
111
112
113
114
115
116
117
118
  } catch (const dg::ExceptionAbstract &aea) {
    res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
  }
  BOOST_CHECK(res);

  // Check if the exception is working when calling floatValue
  // while we are having a none.
  res = false;
  try {
    float afloat(anet);
Guilhem Saurel's avatar
format    
Guilhem Saurel committed
119
120
    afloat++; // silence unused variable warnings to have a stable release in
              // the ros buildfarm
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  } catch (const dg::ExceptionAbstract &aea) {
    res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
  }
  BOOST_CHECK(res);

  // Check if the exception is working when calling stringValue
  // while we are having a none.
  res = false;
  try {
    std::string astring(anet);
  } catch (const dg::ExceptionAbstract &aea) {
    res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
  }
  BOOST_CHECK(res);

  // Check if the exception is working when calling vectorValue
  // while we are having a none.
  res = false;
  try {
    dg::Vector avector;
    avector = anet;
  } catch (const dg::ExceptionAbstract &aea) {
    res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
  }
  BOOST_CHECK(res);

  // Check if the exception is working when calling matrixXdValue
  // while we are having a none.
  res = false;
  try {
    Eigen::MatrixXd amatrixXd;
    amatrixXd = anet;
  } catch (const dg::ExceptionAbstract &aea) {
    res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
  }
  BOOST_CHECK(res);
157

158
159
160
161
162
163
164
165
166
167
168
169
  // Check if the exception is working when calling matrix4dValue
  // while we are having a none.
  res = false;
  try {
    Eigen::Matrix4d amatrix4d;
    amatrix4d = anet;
  } catch (const dg::ExceptionAbstract &aea) {
    res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
  }
  BOOST_CHECK(res);
}

170
BOOST_AUTO_TEST_CASE(value_unsigned_int) {
171
  using dg::command::Value;
172
173
174

  unsigned int aint1(5);
  Value value1(aint1);
175
  Value value = value1;
176

Joseph Mirabel's avatar
Joseph Mirabel committed
177
178
  BOOST_CHECK(value1 == value);

179
180
181
182
183
184
185
186
187
188
189
190
191
192
  {
    output_test_stream output;
    output << value1;
    BOOST_CHECK(output.is_equal("Type=unsigned int, value=5"));
  }

  {
    output_test_stream output;
    output << value;
    BOOST_CHECK(output.is_equal("Type=unsigned int, value=5"));
  }
}

BOOST_AUTO_TEST_CASE(value_int) {
193
  using dg::command::Value;
194
195
196

  int aint1(5);
  Value value1(aint1);
197
  Value value = value1;
198

Joseph Mirabel's avatar
Joseph Mirabel committed
199
200
  BOOST_CHECK(value1 == value);

201
202
203
204
205
206
207
208
209
210
211
212
213
214
  {
    output_test_stream output;
    output << value1;
    BOOST_CHECK(output.is_equal("Type=int, value=5"));
  }

  {
    output_test_stream output;
    output << value;
    BOOST_CHECK(output.is_equal("Type=int, value=5"));
  }
}

BOOST_AUTO_TEST_CASE(value_float) {
215
  using dg::command::Value;
216
217
218

  float afloat1(0.5);
  Value value1(afloat1);
219
  Value value = value1;
220

Joseph Mirabel's avatar
Joseph Mirabel committed
221
222
  BOOST_CHECK(value1 == value);

223
224
225
226
227
228
229
230
231
232
233
234
235
236
  {
    output_test_stream output;
    output << value1;
    BOOST_CHECK(output.is_equal("Type=float, value=0.5"));
  }

  {
    output_test_stream output;
    output << value;
    BOOST_CHECK(output.is_equal("Type=float, value=0.5"));
  }
}

BOOST_AUTO_TEST_CASE(value_double) {
237
  using dg::command::Value;
238
239
240

  double adouble1(0.5);
  Value value1(adouble1);
241
  Value value = value1;
242

Joseph Mirabel's avatar
Joseph Mirabel committed
243
244
  BOOST_CHECK(value1 == value);

245
246
247
248
249
250
251
252
253
254
255
256
257
258
  {
    output_test_stream output;
    output << value1;
    BOOST_CHECK(output.is_equal("Type=double, value=0.5"));
  }

  {
    output_test_stream output;
    output << value;
    BOOST_CHECK(output.is_equal("Type=double, value=0.5"));
  }
}

BOOST_AUTO_TEST_CASE(value_vector) {
259
  using dg::command::Value;
260

261
  dg::Vector avector1;
262
  avector1.resize(2);
263
264
  avector1[0] = 0.5;
  avector1[1] = 1.5;
265
  Value value1(avector1);
266
  Value value = value1;
267

Joseph Mirabel's avatar
Joseph Mirabel committed
268
269
  BOOST_CHECK(value1 == value);

270
271
272
273
274
275
276
277
278
279
280
281
282
  {
    output_test_stream output;
    output << value1;
    BOOST_CHECK(output.is_equal("Type=vector, value=0.5\n1.5"));
  }

  {
    output_test_stream output;
    output << value;
    BOOST_CHECK(output.is_equal("Type=vector, value=0.5\n1.5"));
  }
}

Bergé's avatar
Bergé committed
283
BOOST_AUTO_TEST_CASE(value_string) {
284
  using dg::command::Value;
Thomas Moulard's avatar
Thomas Moulard committed
285
286
287
288
289

  std::string str1("value #1");
  Value value1(str1);
  Value value = value1;

Joseph Mirabel's avatar
Joseph Mirabel committed
290
291
  BOOST_CHECK(value1 == value);

Thomas Moulard's avatar
Thomas Moulard committed
292
293
294
  {
    output_test_stream output;
    output << value1;
Bergé's avatar
Bergé committed
295
    BOOST_CHECK(output.is_equal("Type=string, value=value #1"));
Thomas Moulard's avatar
Thomas Moulard committed
296
297
298
299
300
  }

  {
    output_test_stream output;
    output << value;
Bergé's avatar
Bergé committed
301
    BOOST_CHECK(output.is_equal("Type=string, value=value #1"));
Thomas Moulard's avatar
Thomas Moulard committed
302
303
304
305
306
307
308
309
310
  }

  std::string str2("value #2");
  Value value2(str2);
  value = value2;

  {
    output_test_stream output;
    output << value2;
Bergé's avatar
Bergé committed
311
    BOOST_CHECK(output.is_equal("Type=string, value=value #2"));
Thomas Moulard's avatar
Thomas Moulard committed
312
313
314
315
316
  }

  {
    output_test_stream output;
    output << value;
Bergé's avatar
Bergé committed
317
    BOOST_CHECK(output.is_equal("Type=string, value=value #2"));
Thomas Moulard's avatar
Thomas Moulard committed
318
319
  }
}
320
321

BOOST_AUTO_TEST_CASE(value_matrixXd) {
322
  using dg::command::Value;
323
324

  Eigen::MatrixXd avector1;
325
326
327
328
329
  avector1.resize(2, 2);
  avector1(0, 0) = 0.5;
  avector1(0, 1) = 1.5;
  avector1(1, 0) = 2.5;
  avector1(1, 1) = 3.5;
330
  Value value1(avector1);
331
  Value value = value1;
332

Joseph Mirabel's avatar
Joseph Mirabel committed
333
334
  BOOST_CHECK(value1 == value);

335
336
337
338
339
340
341
342
343
344
345
346
347
348
  {
    output_test_stream output;
    output << value1;
    BOOST_CHECK(output.is_equal("Type=matrixXd, value=0.5 1.5\n2.5 3.5"));
  }

  {
    output_test_stream output;
    output << value;
    BOOST_CHECK(output.is_equal("Type=matrixXd, value=0.5 1.5\n2.5 3.5"));
  }
}

BOOST_AUTO_TEST_CASE(value_matrix4d) {
349
  using dg::command::Value;
350
351
352

  Eigen::Matrix4d avector1;
  avector1.setZero();
353
354
355
356
  avector1(0, 0) = 0.5;
  avector1(0, 1) = 1.5;
  avector1(1, 0) = 2.5;
  avector1(1, 1) = 3.5;
357
  Value value1(avector1);
358
  Value value = value1;
359

Joseph Mirabel's avatar
Joseph Mirabel committed
360
361
  BOOST_CHECK(value1 == value);

362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
  {
    output_test_stream output;
    output << value1;
    BOOST_CHECK(output.is_equal("Type=matrix4d, value=0.5 1.5   0   0\n"
                                "2.5 3.5   0   0\n  0   0   0   0\n"
                                "  0   0   0   0"));
  }

  {
    output_test_stream output;
    output << value;
    BOOST_CHECK(output.is_equal("Type=matrix4d, value=0.5 1.5   0   0\n"
                                "2.5 3.5   0   0\n  0   0   0   0\n"
                                "  0   0   0   0"));
  }
}
378
379
380
381
382
383
384
385
386
387
388

BOOST_AUTO_TEST_CASE(value_values) {
  using namespace dynamicgraph::command;

  std::string s1("value #1");
  double d1 = 0.3;

  Value vs1(s1);
  Value vd1(d1);

  Values values;
Olivier Stasse's avatar
Olivier Stasse committed
389
390
  values.push_back(vs1);
  values.push_back(vd1);
391

Olivier Stasse's avatar
Olivier Stasse committed
392
  Value vvalues(values);
393
394
395

  BOOST_CHECK_EQUAL(vvalues.type(), Value::VALUES);

Joseph Mirabel's avatar
Joseph Mirabel committed
396
  { // Const ref
Olivier Stasse's avatar
Olivier Stasse committed
397
    const Values &vs = vvalues.constValuesValue();
Joseph Mirabel's avatar
Joseph Mirabel committed
398
399
400
    BOOST_CHECK_EQUAL(vs.size(), values.size());
    BOOST_CHECK(vs == values);
  }
Olivier Stasse's avatar
Olivier Stasse committed
401
402
403
404
405
406
407
408
  {
      // Cast does not work.
      // dg::command::EitherType eitherType (vvalues);
      // Values vs = static_cast<Values>(eitherType);
      // BOOST_CHECK_EQUAL(vs.size(), values.size());
      // BOOST_CHECK(vs == values);
  } { // Constructor
    Value vvs(vvalues);
Joseph Mirabel's avatar
Joseph Mirabel committed
409
410
411
412
413
414
415
416
417
418
419
    BOOST_CHECK(vvs == vvalues);
  }

  {
    output_test_stream output;
    output << vvalues;
    BOOST_CHECK(output.is_equal("Type=values, value=[ "
                                "Value(Type=string, value=value #1), "
                                "Value(Type=double, value=0.3), "
                                "]"));
  }
420
}