value.cpp 9.57 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);
Thomas PEYRUCAIN's avatar
Thomas PEYRUCAIN committed
67
    aInt++; // silence unused variable warnings to have a stable release in the ros buildfarm
68
  } catch (const dg::ExceptionAbstract &aea) {
69
70
    output << aea.getExceptionName();
    output2 << aea.what();
71
72
73
    res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
  }
  BOOST_CHECK(res);
74
75
  BOOST_CHECK(output.is_equal("Abstract"));
  BOOST_CHECK(output2.is_equal("value is not an int int"));
76
77
78
79
80
81

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

88
89
90
91
92
  // Check if the exception is working when calling unsignedintValue
  // while we are having a none.
  res = false;
  try {
    unsigned int aint(anet);
Thomas PEYRUCAIN's avatar
Thomas PEYRUCAIN committed
93
    aint++; // silence unused variable warnings to have a stable release in the ros buildfarm
94
95
96
97
98
99
100
101
102
103
  } 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);
Thomas PEYRUCAIN's avatar
Thomas PEYRUCAIN committed
104
    adouble++; // silence unused variable warnings to have a stable release in the ros buildfarm
105
106
107
108
109
110
111
112
113
114
  } 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);
Thomas PEYRUCAIN's avatar
Thomas PEYRUCAIN committed
115
    afloat++; // silence unused variable warnings to have a stable release in the ros buildfarm
116
117
118
119
120
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
  } 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);
152

153
154
155
156
157
158
159
160
161
162
163
164
  // 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);
}

165
BOOST_AUTO_TEST_CASE(value_unsigned_int) {
166
  using dg::command::Value;
167
168
169

  unsigned int aint1(5);
  Value value1(aint1);
170
  Value value = value1;
171

Joseph Mirabel's avatar
Joseph Mirabel committed
172
173
  BOOST_CHECK(value1 == value);

174
175
176
177
178
179
180
181
182
183
184
185
186
187
  {
    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) {
188
  using dg::command::Value;
189
190
191

  int aint1(5);
  Value value1(aint1);
192
  Value value = value1;
193

Joseph Mirabel's avatar
Joseph Mirabel committed
194
195
  BOOST_CHECK(value1 == value);

196
197
198
199
200
201
202
203
204
205
206
207
208
209
  {
    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) {
210
  using dg::command::Value;
211
212
213

  float afloat1(0.5);
  Value value1(afloat1);
214
  Value value = value1;
215

Joseph Mirabel's avatar
Joseph Mirabel committed
216
217
  BOOST_CHECK(value1 == value);

218
219
220
221
222
223
224
225
226
227
228
229
230
231
  {
    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) {
232
  using dg::command::Value;
233
234
235

  double adouble1(0.5);
  Value value1(adouble1);
236
  Value value = value1;
237

Joseph Mirabel's avatar
Joseph Mirabel committed
238
239
  BOOST_CHECK(value1 == value);

240
241
242
243
244
245
246
247
248
249
250
251
252
253
  {
    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) {
254
  using dg::command::Value;
255

256
  dg::Vector avector1;
257
  avector1.resize(2);
258
259
  avector1[0] = 0.5;
  avector1[1] = 1.5;
260
  Value value1(avector1);
261
  Value value = value1;
262

Joseph Mirabel's avatar
Joseph Mirabel committed
263
264
  BOOST_CHECK(value1 == value);

265
266
267
268
269
270
271
272
273
274
275
276
277
  {
    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
278
BOOST_AUTO_TEST_CASE(value_string) {
279
  using dg::command::Value;
Thomas Moulard's avatar
Thomas Moulard committed
280
281
282
283
284

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

Joseph Mirabel's avatar
Joseph Mirabel committed
285
286
  BOOST_CHECK(value1 == value);

Thomas Moulard's avatar
Thomas Moulard committed
287
288
289
  {
    output_test_stream output;
    output << value1;
Bergé's avatar
Bergé committed
290
    BOOST_CHECK(output.is_equal("Type=string, value=value #1"));
Thomas Moulard's avatar
Thomas Moulard committed
291
292
293
294
295
  }

  {
    output_test_stream output;
    output << value;
Bergé's avatar
Bergé committed
296
    BOOST_CHECK(output.is_equal("Type=string, value=value #1"));
Thomas Moulard's avatar
Thomas Moulard committed
297
298
299
300
301
302
303
304
305
  }

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

  {
    output_test_stream output;
    output << value2;
Bergé's avatar
Bergé committed
306
    BOOST_CHECK(output.is_equal("Type=string, value=value #2"));
Thomas Moulard's avatar
Thomas Moulard committed
307
308
309
310
311
  }

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

BOOST_AUTO_TEST_CASE(value_matrixXd) {
317
  using dg::command::Value;
318
319

  Eigen::MatrixXd avector1;
320
321
322
323
324
  avector1.resize(2, 2);
  avector1(0, 0) = 0.5;
  avector1(0, 1) = 1.5;
  avector1(1, 0) = 2.5;
  avector1(1, 1) = 3.5;
325
  Value value1(avector1);
326
  Value value = value1;
327

Joseph Mirabel's avatar
Joseph Mirabel committed
328
329
  BOOST_CHECK(value1 == value);

330
331
332
333
334
335
336
337
338
339
340
341
342
343
  {
    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) {
344
  using dg::command::Value;
345
346
347

  Eigen::Matrix4d avector1;
  avector1.setZero();
348
349
350
351
  avector1(0, 0) = 0.5;
  avector1(0, 1) = 1.5;
  avector1(1, 0) = 2.5;
  avector1(1, 1) = 3.5;
352
  Value value1(avector1);
353
  Value value = value1;
354

Joseph Mirabel's avatar
Joseph Mirabel committed
355
356
  BOOST_CHECK(value1 == value);

357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
  {
    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"));
  }
}
373
374
375
376
377
378
379
380
381
382
383

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
384
385
  values.push_back(vs1);
  values.push_back(vd1);
386

Olivier Stasse's avatar
Olivier Stasse committed
387
  Value vvalues(values);
388
389
390

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

Joseph Mirabel's avatar
Joseph Mirabel committed
391
  { // Const ref
Olivier Stasse's avatar
Olivier Stasse committed
392
    const Values &vs = vvalues.constValuesValue();
Joseph Mirabel's avatar
Joseph Mirabel committed
393
394
395
    BOOST_CHECK_EQUAL(vs.size(), values.size());
    BOOST_CHECK(vs == values);
  }
Olivier Stasse's avatar
Olivier Stasse committed
396
397
398
399
400
401
402
403
  {
      // 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
404
405
406
407
408
409
410
411
412
413
414
    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), "
                                "]"));
  }
415
}