property_loader_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 property_loader_test.cpp
3 
4  property_loader_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 #include "unit.hpp"
21 #include "marsh/PropertyLoader.hpp"
22 #include "TaskContext.hpp"
23 
24 struct LoaderTest {
25  LoaderTest() : tc("tc"), pl(&tc),
26  pstring("pstring","pstringd","Hello World"),
27  pchar("pchar","pchard",'H'),
28  pint("pint", "pintd", -2),
29  puint("puint", "puintd", 3),
30  pllong("pllong", "pllongd", 2147483648),
31  pullong("pullong", "pullongd", 18446744073709551615ull),
32  pdouble("pdouble", "pdoubled", 1.23456),
33  pbag("pbag","pbagd"),
34  pints("pints", "pintsd", vector<int>(3,4)),
35  pdoubles("pdoubles", "pdoublesd", vector<double>(3,4.123))
36  {
37 
38  }
40  PropertyLoader pl;
52 };
53 
54 
55 BOOST_FIXTURE_TEST_SUITE( PropertyLoaderTest, LoaderTest )
56 
57 
61 BOOST_AUTO_TEST_CASE( testPropSaveLoad )
62 {
63  std::string filename = "property_writing.tst";
72  tc.addProperty("newbag", bag).doc("newbag doc"); // takes reference to bag (PropertyBag&)
75  bag.addProperty( pbag ); // takes reference to bpag (PropertyBase&)
76  pbag.value().addProperty( pchar );
77 
78  // deep copy original property values
79  RTT::PropertyBag orig;
81 
82  // save all to fresh file
83  BOOST_CHECK( pl.save(filename, true) );
84  // configure all from file.
85  BOOST_CHECK( pl.configure(filename, true) );
86 
87  // compare property values
88  BOOST_CHECK_EQUAL( pstring.value(), orig.getPropertyType<std::string>("pstring")->value() );
89  BOOST_CHECK_EQUAL( pchar.value(), orig.getPropertyType<char>("pchar")->value() );
90  BOOST_CHECK_EQUAL( pint.value(), orig.getPropertyType<int>("pint")->value() );
91  BOOST_CHECK_EQUAL( puint.value(), orig.getPropertyType<unsigned int>("puint")->value() );
92  BOOST_CHECK_EQUAL( pllong.value(), orig.getPropertyType<long long>("pllong")->value() );
93  BOOST_CHECK_EQUAL( pullong.value(), orig.getPropertyType<unsigned long long>("pullong")->value() );
94  BOOST_CHECK_EQUAL( pdouble.value(), orig.getPropertyType<double>("pdouble")->value() );
95  BOOST_CHECK_EQUAL_COLLECTIONS(
96  pdoubles.value().begin(),
97  pdoubles.value().end(),
98  orig.getPropertyType< vector<double> >("pdoubles")->value().begin(),
99  orig.getPropertyType< vector<double> >("pdoubles")->value().end()
100  );
101  BOOST_CHECK_EQUAL( bag.getPropertyType<double>("pdouble")->value(), orig.getPropertyType<RTT::PropertyBag>("newbag")->value().getPropertyType<double>("pdouble")->value() );
102  BOOST_CHECK_EQUAL_COLLECTIONS(
103  bag.getPropertyType< vector<double> >("pdoubles")->value().begin(),
104  bag.getPropertyType< vector<double> >("pdoubles")->value().end(),
105  orig.getPropertyType<RTT::PropertyBag>("newbag")->value().getPropertyType< vector<double> >("pdoubles")->value().begin(),
106  orig.getPropertyType<RTT::PropertyBag>("newbag")->value().getPropertyType< vector<double> >("pdoubles")->value().end()
107  );
108  BOOST_CHECK_EQUAL( pchar.value(), orig.getPropertyType<RTT::PropertyBag>("newbag")->value().getPropertyType<RTT::PropertyBag>("pbag")->value().getPropertyType<char>("pchar")->value() );
109 
110  // configure all fails with one missing element.
112 
113  // TEST FAILS : TO BE FIXED in updateProperties()
114  //BOOST_REQUIRE( !pl.configure(filename, true) );
115 
116  // configure some does not fail with one missing element.
117  BOOST_CHECK( pl.configure(filename, false) );
118 
119  // save some (does not add new props to file:
120  BOOST_CHECK( pl.save(filename, false) );
121 
122  // TEST FAILS : TO BE FIXED in updateProperties()
123  //BOOST_REQUIRE( !pl.configure(filename, true) ); // must still fail, not all were saved.
124 
125  // save all:
126  BOOST_CHECK( pl.save(filename, true) );
127  BOOST_CHECK( pl.configure(filename, true) ); // all were saved.
128 }
129 
133 BOOST_AUTO_TEST_CASE( testPropUnknown )
134 {
135  std::string filename = "property_unknown.tst";
137 
138  BOOST_CHECK( pl.save(filename, true) ); // produces empty file.
139  BOOST_CHECK( !pl.configure(filename, true) ); // must fail, was not serialized !
140  BOOST_CHECK( pl.configure(filename, false) );
141 
142  // test unknown in bag:
144  tc.addProperty( "bag", bag ).doc( "bag doc" );
146 
147  BOOST_CHECK( pl.save(filename, true) ); // produces file with bag.
148 
149  // TEST FAILS : TO BE FIXED in updateProperties()
150  //BOOST_CHECK( !pl.configure(filename, true) ); // must fail, was not serialized !
151 
152  BOOST_CHECK( pl.configure(filename, false) );
153 }
154 
155 BOOST_AUTO_TEST_CASE( testPropLoading )
156 {
157  std::string filename = "property_loading.cpf";
158  BOOST_REQUIRE( pl.load(filename) );
159 
160  BOOST_CHECK( tc.provides()->hasProperty("load1") );
161  BOOST_CHECK( tc.provides()->hasProperty("load2") );
162  BOOST_CHECK( tc.provides()->hasProperty("bag1"));
163  BOOST_CHECK( tc.provides()->hasProperty("pint2") );
164  BOOST_CHECK( tc.provides()->hasProperty("puint2") );
165  BOOST_CHECK( tc.provides()->hasProperty("pllong2") );
166  BOOST_CHECK( tc.provides()->hasProperty("pullong2") );
167 
169  BOOST_REQUIRE( bag1.ready() );
170  BOOST_CHECK( bag1.getDescription() == "Bag1");
171  PropertyBag& bag = bag1.value();
172  BOOST_CHECK_EQUAL( bag.size(), 2);
173  BOOST_CHECK( bag.find("bagload1"));
174  BOOST_CHECK( bag.find("bagload2"));
175  Property<int> bagload1 = bag.getProperty("bagload1");
176  Property<string> bagload2 = bag.getProperty("bagload2");
177 
178  BOOST_REQUIRE(bagload1.ready());
179  BOOST_CHECK_EQUAL(bagload1.get(), 3);
180  BOOST_CHECK_EQUAL(bagload2.get(), "3 3 3");
181 
182  BOOST_CHECK_EQUAL( tc.properties()->getPropertyType<int>("pint2")->value(), -2 );
183  BOOST_CHECK_EQUAL( tc.properties()->getPropertyType<unsigned int>("puint2")->value(), 3 );
184  BOOST_CHECK_EQUAL( tc.properties()->getPropertyType<long long>("pllong2")->value(), 9223372036854775807ll );
185  BOOST_CHECK_EQUAL( tc.properties()->getPropertyType<unsigned long long>("pullong2")->value(), 18446744073709551615ull );
186 }
187 
188 BOOST_AUTO_TEST_CASE( testPropStoring )
189 {
194  tc.addProperty("newbag", bag).doc("newbag doc"); // takes reference to bag (PropertyBag&)
197  bag.addProperty( pbag ); // takes reference to bpag (PropertyBase&)
198  pbag.value().addProperty( pchar );
199 
200  std::string filename = "property_storing.tst";
201  BOOST_REQUIRE( pl.store(filename) );
202 
203  tc.properties()->clear();
204 
205  // check by using load:
206  BOOST_REQUIRE( pl.load(filename) );
207 
208  BOOST_CHECK( tc.provides()->hasProperty("pstring") );
209  BOOST_CHECK( tc.provides()->hasProperty("pchar") );
210  BOOST_CHECK( tc.provides()->hasProperty("pdouble") );
211  BOOST_CHECK( tc.provides()->hasProperty("pdoubles") );
212  BOOST_CHECK( tc.provides()->hasProperty("newbag"));
213  Property<PropertyBag> bag1 = tc.properties()->getProperty("newbag");
214  BOOST_REQUIRE( bag1.ready() );
215  BOOST_CHECK( bag1.getDescription() == "newbag doc");
216  PropertyBag& bag = bag1.value();
217  BOOST_CHECK_EQUAL( bag.size(), 3);
218  BOOST_CHECK( bag.find("pdouble"));
219  BOOST_CHECK( bag.find("pdoubles"));
220  BOOST_CHECK( bag.find("pbag"));
221  Property<double> bagdouble = bag.getProperty("pdouble");
222  Property<vector<double> > bagvector = bag.getProperty("pdoubles");
223 
224  BOOST_REQUIRE(bagdouble.ready());
225  BOOST_CHECK_EQUAL( bagdouble.value(), 1.23456);
226  BOOST_REQUIRE(bagvector.ready());
227  BOOST_REQUIRE_EQUAL(bagvector.value().size(), 3);
228  BOOST_CHECK_EQUAL(bagvector.value()[0], 4.123);
229  BOOST_CHECK_EQUAL(bagvector.value()[1], 4.123);
230  BOOST_CHECK_EQUAL(bagvector.value()[2], 4.123);
231 }
232 
Property< T > & addProperty(const std::string &name, T &attr)
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
DataSourceType get() const
Definition: Property.hpp:246
Property< unsigned long long > pullong
Property< T > * getPropertyType(const std::string &name) const
Service::shared_ptr provides()
Property< int > pint
#define BOOST_AUTO_TEST_SUITE_END()
bool copyProperties(PropertyBag &target, const PropertyBag &source)
Property< char > pchar
A container for holding references to properties.
Definition: PropertyBag.hpp:96
reference_t value()
Definition: Property.hpp:277
BOOST_AUTO_TEST_CASE(testPropSaveLoad)
Property< double > pdouble
Property< unsigned int > puint
Property< T > & addProperty(const std::string &name, T &attr)
Property< std::vector< int > > pints
bool removeProperty(base::PropertyBase *p)
Property< std::vector< double > > pdoubles
Property< PropertyBag > pbag
base::PropertyBase * getProperty(const std::string &name) const
PropertyBag * properties()
Property< long long > pllong
Property< string > pstring
PropertyLoader pl


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