property_marsh_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 property_marsh_test.cpp
3 
4  property_marsh_test.cpp - description
5  -------------------
6  begin : Tue September 07 2010
7  copyright : (C) 2010 The SourceWorks
8  email : peter@thesourceworks.com
9 
10  ***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 
20 
23 #include <Property.hpp>
24 #include <PropertyBag.hpp>
26 
27 #include "unit.hpp"
28 
30 {
31 public:
33  {
34  }
36  {
37  }
38 };
39 
40 
41 BOOST_FIXTURE_TEST_SUITE( PropertyMarshTestSuite, PropertyMarshTest )
42 
43 BOOST_AUTO_TEST_CASE( testPropMarsh )
45 {
46  std::string filename = "testPropMarsh.tst";
47 
48  PropertyBag source; // to file
49  PropertyBag target; // from file
50 
51  Property<PropertyBag> b1("b1","b1d");
52  Property<PropertyBag> b2("b2","b2d");
53  Property<int> p1("p1","p1d",-1);
54 
55  // setup source tree
56  source.addProperty( b1 );
57  b1.value().addProperty( b2 );
58  b2.value().addProperty( p1 );
59 
60  {
61  // scope required such that file is closed
62  PropertyMarshaller pm( filename );
63  pm.serialize( source );
64  }
65 
66  {
67  // scope required such that file is closed
68  PropertyDemarshaller pd( filename );
69  BOOST_REQUIRE( pd.deserialize( target ) );
70  }
71 
72  Property<PropertyBag> bag = target.getProperty("b1");
73  BOOST_REQUIRE( bag.ready() );
74  BOOST_CHECK( bag.getDescription() == "b1d" );
75 
76  bag = bag.rvalue().getProperty("b2");
77  BOOST_REQUIRE( bag.ready() );
78  BOOST_CHECK( bag.getDescription() == "b2d" );
79 
80  Property<int> pi = bag.rvalue().getProperty("p1");
81  BOOST_REQUIRE( pi.ready() );
82  BOOST_CHECK( pi.get() == -1 );
83  BOOST_CHECK( pi.getDescription() == "p1d" );
84  deletePropertyBag( target );
85 }
86 
88 BOOST_AUTO_TEST_CASE( testPropMarshVect )
89 {
90  std::string filename = "testPropMarshVect.tst";
91 
92  PropertyBag source; // to file
93  PropertyBag target; // from file
94 
95  Property<std::vector<double> >* p1 = new Property<std::vector<double> >("p1","p1d", std::vector<double>(7, 1.234) );
96 
97  // setup source tree
98  source.addProperty( *p1 );
99 
100  {
101  // scope required such that file is closed
102  PropertyMarshaller pm( filename );
103  pm.serialize( source );
104  }
105 
106  p1->set() = std::vector<double>(3, 0.234);
107  {
108  // scope required such that file is closed
109  PropertyDemarshaller pd( filename );
110  BOOST_REQUIRE( pd.deserialize( target ) );
111  }
112 
113  // check bag:
114  Property<PropertyBag> bag = target.getProperty("p1");
115  BOOST_REQUIRE( bag.ready() );
116  BOOST_CHECK( bag.getDescription() == "p1d" );
117  BOOST_CHECK_EQUAL( bag.rvalue().size(), 7 );
118 
119  // update bag -> array.
120  PropertyBag composed;
121  BOOST_CHECK( composePropertyBag(target, composed));
122  BOOST_CHECK( updateProperties( source, composed) );
123 
124  //p1 = source.getProperty("p1");
125  BOOST_REQUIRE( p1->ready() );
126  BOOST_CHECK_EQUAL( p1->rvalue().size() , 7 );
127  BOOST_CHECK_EQUAL( p1->rvalue()[0] , 1.234 );
128 
129  deletePropertyBag( target );
130  deletePropertyBag( source );
131 }
132 
134 BOOST_AUTO_TEST_CASE( testPropMarshVectLegacy )
135 {
136  PropertyBag target; // to file
137  PropertyBag source; // from file
138 
139  Property<std::vector<double> >* p1 = new Property<std::vector<double> >("driveLimits","p1d", std::vector<double>(7, 1.234) );
140 
141  // setup target tree
142  target.addProperty( *p1 );
143  {
144  // scope required such that file is closed
145  PropertyDemarshaller pd( "testPropMarshVectLegacy.cpf" );
146  BOOST_REQUIRE( pd.deserialize( source ) );
147  }
148  // Check if the bag was read from file:
149  Property<PropertyBag> bag = source.getProperty("driveLimits");
150  BOOST_REQUIRE( bag.ready() );
151  // check if legacy bag contains 7 elements:
152  BOOST_CHECK_EQUAL( bag.value().size() , 7 );
153 
154  // update bag -> array.
155  PropertyBag composed;
156  BOOST_CHECK( composePropertyBag(source, composed));
157  BOOST_CHECK( refreshProperties( target, composed) );
158 
159  //p1 = target.getProperty("p1");
160  BOOST_REQUIRE( p1->ready() );
161  //cout << p1 << endl;
162  // check updated vector size is 6, contents is 1:
163  BOOST_CHECK_EQUAL( p1->rvalue().size() , 6 );
164  BOOST_CHECK_EQUAL( p1->rvalue()[0] , 1 );
165 
166  deletePropertyBag( source );
167 }
168 
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
bool updateProperties(PropertyBag &target, const PropertyBag &source)
bool refreshProperties(const PropertyBag &target, const PropertyBag &source, bool allprops)
#define BOOST_AUTO_TEST_SUITE_END()
bool RTT_API composePropertyBag(PropertyBag const &sourcebag, PropertyBag &target)
const_reference_t rvalue() const
Definition: Property.hpp:285
A container for holding references to properties.
Definition: PropertyBag.hpp:96
reference_t value()
Definition: Property.hpp:277
virtual bool deserialize(PropertyBag &v)
void deletePropertyBag(PropertyBag &target)
Property< T > & addProperty(const std::string &name, T &attr)
The default Orocos demarshaller for extracting properties and property bags from a property file...
base::PropertyBase * getProperty(const std::string &name) const
reference_t set()
Definition: Property.hpp:257
virtual void serialize(base::PropertyBase *v)
BOOST_AUTO_TEST_CASE(testPropMarsh)
Test writing some properties and reading the same file back in.


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:34