property_marsh_test.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: The SourceWorks  Tue Sep 7 00:54:57 CEST 2010  property_marsh_test.cpp
00003 
00004                         property_marsh_test.cpp -  description
00005                            -------------------
00006     begin                : Tue September 07 2010
00007     copyright            : (C) 2010 The SourceWorks
00008     email                : peter@thesourceworks.com
00009 
00010  ***************************************************************************
00011  *                                                                         *
00012  *   This program is free software; you can redistribute it and/or modify  *
00013  *   it under the terms of the GNU General Public License as published by  *
00014  *   the Free Software Foundation; either version 2 of the License, or     *
00015  *   (at your option) any later version.                                   *
00016  *                                                                         *
00017  ***************************************************************************/
00018 
00019 
00020 
00021 #include <marsh/PropertyMarshaller.hpp>
00022 #include <marsh/PropertyDemarshaller.hpp>
00023 #include <Property.hpp>
00024 #include <PropertyBag.hpp>
00025 #include <types/PropertyComposition.hpp>
00026 
00027 #include "unit.hpp"
00028 
00029 class PropertyMarshTest
00030 {
00031 public:
00032     PropertyMarshTest()
00033     {
00034     }
00035     ~PropertyMarshTest()
00036     {
00037     }
00038 };
00039 
00040 
00041 BOOST_FIXTURE_TEST_SUITE( PropertyMarshTestSuite, PropertyMarshTest )
00042 
00043 
00044 BOOST_AUTO_TEST_CASE( testPropMarsh )
00045 {
00046     std::string filename = "testPropMarsh.tst";
00047 
00048     PropertyBag source; // to file
00049     PropertyBag target; // from file
00050 
00051     Property<PropertyBag> b1("b1","b1d");
00052     Property<PropertyBag> b2("b2","b2d");
00053     Property<int> p1("p1","p1d",-1);
00054 
00055     // setup source tree
00056     source.addProperty( b1 );
00057     b1.value().addProperty( b2 );
00058     b2.value().addProperty( p1 );
00059 
00060     {
00061         // scope required such that file is closed
00062         PropertyMarshaller pm( filename );
00063         pm.serialize( source );
00064     }
00065 
00066     {
00067         // scope required such that file is closed
00068         PropertyDemarshaller pd( filename );
00069         BOOST_REQUIRE( pd.deserialize( target ) );
00070     }
00071 
00072     Property<PropertyBag> bag = target.getProperty("b1");
00073     BOOST_REQUIRE( bag.ready() );
00074     BOOST_CHECK( bag.getDescription() == "b1d" );
00075 
00076     bag = bag.rvalue().getProperty("b2");
00077     BOOST_REQUIRE( bag.ready() );
00078     BOOST_CHECK( bag.getDescription() == "b2d" );
00079 
00080     Property<int> pi = bag.rvalue().getProperty("p1");
00081     BOOST_REQUIRE( pi.ready() );
00082     BOOST_CHECK( pi.get() == -1 );
00083     BOOST_CHECK( pi.getDescription() == "p1d" );
00084     deletePropertyBag( target );
00085 }
00086 
00088 BOOST_AUTO_TEST_CASE( testPropMarshVect )
00089 {
00090     std::string filename = "testPropMarshVect.tst";
00091 
00092     PropertyBag source; // to file
00093     PropertyBag target; // from file
00094 
00095     Property<std::vector<double> >* p1 =  new Property<std::vector<double> >("p1","p1d", std::vector<double>(7, 1.234) );
00096 
00097     // setup source tree
00098     source.addProperty( *p1 );
00099 
00100     {
00101         // scope required such that file is closed
00102         PropertyMarshaller pm( filename );
00103         pm.serialize( source );
00104     }
00105 
00106     p1->set() = std::vector<double>(3, 0.234);
00107     {
00108         // scope required such that file is closed
00109         PropertyDemarshaller pd( filename );
00110         BOOST_REQUIRE( pd.deserialize( target ) );
00111     }
00112 
00113     // check bag:
00114     Property<PropertyBag> bag = target.getProperty("p1");
00115     BOOST_REQUIRE( bag.ready() );
00116     BOOST_CHECK( bag.getDescription() == "p1d" );
00117     BOOST_CHECK_EQUAL( bag.rvalue().size(), 7 );
00118 
00119     // update bag -> array.
00120     PropertyBag composed;
00121     BOOST_CHECK( composePropertyBag(target, composed));
00122     BOOST_CHECK( updateProperties( source, composed) );
00123 
00124     //p1 = source.getProperty("p1");
00125     BOOST_REQUIRE( p1->ready() );
00126     BOOST_CHECK_EQUAL( p1->rvalue().size() , 7 );
00127     BOOST_CHECK_EQUAL( p1->rvalue()[0] , 1.234 );
00128 
00129     deletePropertyBag( target );
00130     deletePropertyBag( source );
00131 }
00132 
00134 BOOST_AUTO_TEST_CASE( testPropMarshVectLegacy )
00135 {
00136     PropertyBag target; // to file
00137     PropertyBag source; // from file
00138 
00139     Property<std::vector<double> >* p1 =  new Property<std::vector<double> >("driveLimits","p1d", std::vector<double>(7, 1.234) );
00140 
00141     // setup target tree
00142     target.addProperty( *p1 );
00143     {
00144         // scope required such that file is closed
00145         PropertyDemarshaller pd( "testPropMarshVectLegacy.cpf" );
00146         BOOST_REQUIRE( pd.deserialize( source ) );
00147     }
00148     // Check if the bag was read from file:
00149     Property<PropertyBag> bag = source.getProperty("driveLimits");
00150     BOOST_REQUIRE( bag.ready() );
00151     // check if legacy bag contains 7 elements:
00152     BOOST_CHECK_EQUAL( bag.value().size() , 7 );
00153 
00154     // update bag -> array.
00155     PropertyBag composed;
00156     BOOST_CHECK( composePropertyBag(source, composed));
00157     BOOST_CHECK( refreshProperties( target, composed) );
00158 
00159     //p1 = target.getProperty("p1");
00160     BOOST_REQUIRE( p1->ready() );
00161     //cout << p1 << endl;
00162     // check updated vector size is 6, contents is 1:
00163     BOOST_CHECK_EQUAL( p1->rvalue().size() , 6 );
00164     BOOST_CHECK_EQUAL( p1->rvalue()[0] , 1 );
00165 
00166     deletePropertyBag( source );
00167 }
00168 
00169 BOOST_AUTO_TEST_SUITE_END()


rtt
Author(s): RTT Developers
autogenerated on Sat Jun 8 2019 18:46:17