GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tests/srdfparser.cc Lines: 67 67 100.0 %
Date: 2024-05-05 11:05:40 Branches: 231 460 50.2 %

Line Branch Exec Source
1
// Copyright (c) 2014, LAAS-CNRS
2
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3
//
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
6
// met:
7
//
8
// 1. Redistributions of source code must retain the above copyright
9
//    notice, this list of conditions and the following disclaimer.
10
//
11
// 2. Redistributions in binary form must reproduce the above copyright
12
// notice, this list of conditions and the following disclaimer in the
13
// documentation and/or other materials provided with the distribution.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26
// DAMAGE.
27
28
#define BOOST_TEST_MODULE Parser
29
#include <pinocchio/fwd.hpp>
30
#include <boost/test/included/unit_test.hpp>
31
32
#include <hpp/util/debug.hh>
33
#include "hpp/manipulation/parser/parser.hh"
34
#include "hpp/manipulation/srdf/factories.hh"
35
36
using namespace hpp::manipulation;
37
using namespace hpp::manipulation::srdf;
38
using namespace hpp::manipulation::parser;
39
40
class Test {
41
  public:
42
    int i;
43
    double d;
44
    std::string name;
45
};
46
47
class TestDFactory : public SequenceFactory <double> {
48
  public:
49
2
    static ObjectFactory* create (ObjectFactory* parent = NULL, const XMLElement* element = NULL)
50
    {
51
2
      return new TestDFactory (parent, element);
52
    }
53
54
  protected:
55
2
    TestDFactory (ObjectFactory* parent, const XMLElement* element)
56
2
      : SequenceFactory <double> (parent, element, 1) {}
57
};
58
59
class TestFactory : public ObjectFactory {
60
  public:
61
2
    static ObjectFactory* create (ObjectFactory* parent = NULL, const XMLElement* element = NULL)
62
    {
63
2
      return new TestFactory (parent, element);
64
    }
65
66
    /// Called for each attribute
67
3
    virtual void impl_setAttribute (const XMLAttribute* attr)
68
    {
69
3
      if (strcmp (attr->Name (), "othername") == 0)
70
1
        obj_.name = attr->Value ();
71
3
    }
72
73
2
    virtual void finishTags () {
74
2
     ObjectFactory* o (NULL);
75

2
     if (!getChildOfType ("tag1", o)) {
76
       // There is more than one tag <position>
77
       // o is a pointer to the first one.
78
     }
79
2
     TestDFactory* sf = o->as <TestDFactory > ();
80
2
     obj_.d = sf->values ().front ();
81
2
    }
82
83
    Test* object ()
84
    {
85
      return &obj_;
86
    }
87
88
  protected:
89
2
    TestFactory (ObjectFactory* parent, const XMLElement* element) :
90
2
          ObjectFactory (parent, element) {}
91
92
  private:
93
    Test obj_;
94
};
95
96
















4
BOOST_AUTO_TEST_CASE (testparser)
97
{
98
2
  Parser p (false);
99

2
  p.addObjectFactory ("test", TestFactory::create);
100

2
  p.addObjectFactory ("tag1", TestDFactory::create);
101

2
  p.parseString (
102
      "<?xml version=\"1.0\"?>                     \n"
103
      "<robot name=\"box\">                        \n"
104
      "  <test name=\"handle\">                    \n"
105
      "    <tag1>0</tag1>                          \n"
106
      "    <tag2 name=\"base_link\"/>              \n"
107
      "  </test>                                   \n"
108
      "  <test name=\"handle2\" othername=\"test\">\n"
109
      "    <tag1>0.54326 </tag1>                   \n"
110
      "    <tag2 name=\"test\"/>                   \n"
111
      "  </test>                                   \n"
112
      "</robot>                                    \n",
113
4
      DevicePtr_t());
114
115


2
  BOOST_CHECK_EQUAL (p.objectFactories().size(), 7);
116
117



2
  BOOST_CHECK (p.objectFactories()[1]->as<TestFactory>());
118



2
  BOOST_CHECK (p.objectFactories()[2]->as<TestDFactory>());
119



2
  BOOST_CHECK (p.objectFactories()[4]->as<TestFactory>());
120



2
  BOOST_CHECK (p.objectFactories()[5]->as<TestDFactory>());
121
122


2
  BOOST_TEST_MESSAGE (p);
123
2
}
124
125
5
void checkPosition (const std::string xmlstring, const Transform3f& result)
126
{
127
10
  Parser p (false);
128

5
  p.addObjectFactory ("position", create <PositionFactory>);
129

5
  p.parseString (xmlstring.c_str(), DevicePtr_t());
130


5
  BOOST_REQUIRE_EQUAL (p.objectFactories().size(), 1);
131
132
5
  ObjectFactory* objectFactory = p.objectFactories().front();
133



5
  BOOST_REQUIRE (objectFactory);
134
5
  PositionFactory* positionFactory = objectFactory->as<PositionFactory>();
135



5
  BOOST_REQUIRE (positionFactory);
136
137
5
  Transform3f M = positionFactory->position();
138





5
  BOOST_CHECK_MESSAGE (M.isApprox (result, 1e-5),
139
      "Different transforms:\n"
140
      << M << '\n'
141
      << result);
142
5
}
143
144
















4
BOOST_AUTO_TEST_CASE (position)
145
{
146
2
  value_type w = 0.786066629137,
147
2
             x = 0.167518791246,
148
2
             y = 0.570941471358,
149
2
             z = 0.167518791246;
150


2
  Transform3f result (Eigen::Quaternion<value_type>(w, x, y, z).matrix(), vector3_t (0,1,0));
151
152

4
  std::ostringstream oss1,oss2;
153



2
  oss1 << w << ' ' << x << ' ' << y << ' ' << z;
154
4
  std::string wxyz = oss1.str();
155



2
  oss2 << x << ' ' << y << ' ' << z << ' ' << w;
156
2
  std::string xyzw = oss2.str();
157
158
159

2
  checkPosition ("<position>0 1 0 " + wxyz + "</position>", result);
160

2
  checkPosition ("<position xyz='0 1 0' wxyz='" + wxyz + "' />", result);
161

2
  checkPosition ("<position xyz='0 1 0' xyzw='" + xyzw + "' />", result);
162

2
  checkPosition ("<position xyz='0 1 0' rpy='1 1 1' />", result);
163
164


2
  checkPosition ("<position xyz='0 1 0' />", Transform3f (matrix3_t::Identity(), vector3_t (0,1,0)));
165
2
}
166
167
















4
BOOST_AUTO_TEST_CASE (srdfparser)
168
{
169
4
  Parser p (false);
170

2
  p.addObjectFactory ("handle", create <HandleFactory>);
171

2
  p.addObjectFactory ("local_position", create <PositionFactory>);
172

2
  p.parseFile ("/root/robotpkg/path/hpp-manipulation-urdf/work/hpp-manipulation-urdf-5.0.0/tests/ressources/box.srdf", hpp::manipulation::DevicePtr_t());
173
174
2
  std::cout << p;
175
2
}