value.cpp 9.26 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 67 68
  // Check if the exception is working when calling intValue
  // while we are having a none.
  bool res = false;
  try {
    int aInt(anet);
    output << aInt;
  } 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 82 83 84 85 86

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

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  // Check if the exception is working when calling unsignedintValue
  // while we are having a none.
  res = false;
  try {
    unsigned int aint(anet);
    output << aint;
  } 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);
    output << adouble;
  } 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);
115
    output << afloat;
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 152 153 154
  } 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);
    output << astring;
  } 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;
    output << avector;
  } 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;
    output << amatrixXd;
  } catch (const dg::ExceptionAbstract &aea) {
    res = (aea.getCode() == dg::ExceptionAbstract::TOOLS);
  }
  BOOST_CHECK(res);
155

156 157 158 159 160 161 162 163 164 165 166 167
  // 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);
}

168
BOOST_AUTO_TEST_CASE(value_unsigned_int) {
169
  using dg::command::Value;
170 171 172

  unsigned int aint1(5);
  Value value1(aint1);
173
  Value value = value1;
174

Joseph Mirabel's avatar
Joseph Mirabel committed
175 176
  BOOST_CHECK(value1 == value);

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

  int aint1(5);
  Value value1(aint1);
195
  Value value = value1;
196

Joseph Mirabel's avatar
Joseph Mirabel committed
197 198
  BOOST_CHECK(value1 == value);

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

  float afloat1(0.5);
  Value value1(afloat1);
217
  Value value = value1;
218

Joseph Mirabel's avatar
Joseph Mirabel committed
219 220
  BOOST_CHECK(value1 == value);

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

  double adouble1(0.5);
  Value value1(adouble1);
239
  Value value = value1;
240

Joseph Mirabel's avatar
Joseph Mirabel committed
241 242
  BOOST_CHECK(value1 == value);

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

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

Joseph Mirabel's avatar
Joseph Mirabel committed
266 267
  BOOST_CHECK(value1 == value);

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

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

Joseph Mirabel's avatar
Joseph Mirabel committed
288 289
  BOOST_CHECK(value1 == value);

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

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

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

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

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

BOOST_AUTO_TEST_CASE(value_matrixXd) {
320
  using dg::command::Value;
321 322

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

Joseph Mirabel's avatar
Joseph Mirabel committed
331 332
  BOOST_CHECK(value1 == value);

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

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

Joseph Mirabel's avatar
Joseph Mirabel committed
358 359
  BOOST_CHECK(value1 == value);

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
  {
    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"));
  }
}
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393

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

  Value vvalues (values);

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

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