$search
00001 /*************************************************************************** 00002 tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 type_discovery_struct_test.cpp 00003 00004 type_discovery_struct_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 #include "unit.hpp" 00021 00022 #include <boost/serialization/vector.hpp> 00023 #include <boost/array.hpp> 00024 00025 #include <rtt-fwd.hpp> 00026 #include <internal/DataSources.hpp> 00027 #include <types/type_discovery.hpp> 00028 #include <os/fosi.h> 00029 #include <boost/lambda/lambda.hpp> 00030 00031 #include "datasource_fixture.hpp" 00032 #include "types/StructTypeInfo.hpp" 00033 #include "types/CArrayTypeInfo.hpp" 00034 #include "types/SequenceTypeInfo.hpp" 00035 #include "types/BoostArrayTypeInfo.hpp" 00036 00037 using namespace boost::lambda; 00038 using namespace boost::archive; 00039 using namespace boost::serialization; 00040 00041 class StructTypeTest 00042 { 00043 public: 00044 StructTypeTest() { } 00045 ~StructTypeTest() { } 00046 }; 00047 00048 // Registers the fixture into the 'registry' 00049 BOOST_FIXTURE_TEST_SUITE( TypeArchiveTestSuite, StructTypeTest ) 00050 00051 // Test the StructTypeInfo for AType 00052 // Similar as the above tests, but now through the TypeInfo system. 00053 BOOST_AUTO_TEST_CASE( testATypeStruct ) 00054 { 00055 Types()->addType( new StructTypeInfo<AType>("AType") ); 00056 00057 AssignableDataSource<AType>::shared_ptr atype = new ValueDataSource<AType>( AType(true) ); 00058 00059 BOOST_REQUIRE( Types()->type("AType") ); 00060 00061 // check the part names lookup: 00062 vector<string> names = atype->getMemberNames(); 00063 BOOST_CHECK_EQUAL( atype->getMemberNames().size(), 5 ); 00064 00065 // for_each( names.begin(), names.end(), cout << lambda::_1 <<", " ); 00066 // cout <<endl; 00067 00068 BOOST_REQUIRE_EQUAL( names.size(), 5); 00069 BOOST_REQUIRE( atype->getMember("a") ); 00070 00071 // Check individual part lookup by name: 00072 AssignableDataSource<int>::shared_ptr a = AssignableDataSource<int>::narrow( atype->getMember("a").get() ); 00073 AssignableDataSource<double>::shared_ptr b = AssignableDataSource<double>::narrow( atype->getMember("b").get() ); 00074 AssignableDataSource<string>::shared_ptr c = AssignableDataSource<string>::narrow( atype->getMember("c").get()); 00075 AssignableDataSource<carray<int> >::shared_ptr ai = AssignableDataSource<carray<int> >::narrow( atype->getMember("ai").get()); 00076 AssignableDataSource<vector<double> >::shared_ptr vd = AssignableDataSource<vector<double> >::narrow( atype->getMember("vd").get()); 00077 00078 BOOST_REQUIRE( a ); 00079 BOOST_REQUIRE( b ); 00080 BOOST_REQUIRE( c ); 00081 BOOST_REQUIRE( ai ); 00082 BOOST_REQUIRE( vd ); 00083 00084 BOOST_CHECK( !atype->getMember("zort") ); 00085 00086 // Check reading parts (must equal parent) 00087 BOOST_CHECK_EQUAL( a->get(), atype->get().a ); 00088 BOOST_CHECK_EQUAL( b->get(), atype->get().b ); 00089 BOOST_CHECK_EQUAL( c->get(), atype->get().c ); 00090 BOOST_CHECK_EQUAL( ai->get().address()[3], atype->get().ai[3] ); 00091 BOOST_CHECK_EQUAL( vd->get()[3], atype->get().vd[3] ); 00092 00093 // Check writing a part (must change in parent too). 00094 a->set(10); 00095 BOOST_CHECK_EQUAL( a->get(), 10 ); 00096 BOOST_CHECK_EQUAL( a->get(), atype->get().a ); 00097 } 00098 00100 BOOST_AUTO_TEST_CASE( testCTypeStruct ) 00101 { 00102 Types()->addType( new StructTypeInfo< AType >("AType") ); 00103 Types()->addType( new StructTypeInfo< BType >("BType") ); 00104 Types()->addType( new StructTypeInfo< CType >("CType") ); 00105 Types()->addType( new SequenceTypeInfo< vector<AType> >("as") ); 00106 Types()->addType( new SequenceTypeInfo< vector<BType> >("bs") ); 00107 Types()->addType( new CArrayTypeInfo< carray<int> >("cints") ); 00108 Types()->addType( new BoostArrayTypeInfo< boost::array<int,5> >("int5") ); 00109 Types()->addType( new SequenceTypeInfo< vector<int> >("ints") ); 00110 00111 AssignableDataSource<CType>::shared_ptr atype = new ValueDataSource<CType>( CType(true) ); 00112 00113 // decompose a complex type 00114 AssignableDataSource<AType>::shared_ptr a = AssignableDataSource<AType>::narrow( atype->getMember("a").get() ); 00115 AssignableDataSource<BType>::shared_ptr b = AssignableDataSource<BType>::narrow( atype->getMember("b").get() ); 00116 AssignableDataSource< vector<AType> >::shared_ptr av = AssignableDataSource< vector<AType> >::narrow( atype->getMember("av").get()); 00117 AssignableDataSource< vector<BType> >::shared_ptr bv = AssignableDataSource< vector<BType> >::narrow( atype->getMember("bv").get()); 00118 00119 BOOST_REQUIRE( a ); 00120 BOOST_REQUIRE( b ); 00121 BOOST_REQUIRE( av ); 00122 BOOST_REQUIRE( bv ); 00123 00124 // Access top level elements 00125 BOOST_REQUIRE( a->getMember("ai") ); 00126 AssignableDataSource<int>::shared_ptr ai3 = dynamic_pointer_cast< AssignableDataSource<int> >( a->getMember("ai")->getMember("3") ); 00127 BOOST_REQUIRE( b->getMember("ai") ); 00128 AssignableDataSource<int>::shared_ptr bi3 = dynamic_pointer_cast< AssignableDataSource<int> >( b->getMember("ai")->getMember("3") ); 00129 00130 // Access elements in sequences: 00131 AssignableDataSource<int>::shared_ptr avi3 = dynamic_pointer_cast< AssignableDataSource<int> >( av->getMember("3")->getMember("ai")->getMember("3") ); 00132 AssignableDataSource<int>::shared_ptr bvi3 = dynamic_pointer_cast< AssignableDataSource<int> >( bv->getMember("3")->getMember("ai")->getMember("3") ); 00133 00134 BOOST_REQUIRE( ai3 ); 00135 BOOST_REQUIRE( bi3 ); 00136 BOOST_REQUIRE( avi3 ); 00137 BOOST_REQUIRE( bvi3 ); 00138 00139 // Check reading parts (must equal parent) 00140 BOOST_CHECK_EQUAL( a->get(), atype->get().a ); 00141 BOOST_CHECK_EQUAL( b->get(), atype->get().b ); 00142 BOOST_CHECK( std::equal(av->set().begin(), av->set().end(), atype->set().av.begin() ) ); 00143 BOOST_CHECK( std::equal(bv->set().begin(), bv->set().end(), atype->set().bv.begin() ) ); 00144 BOOST_CHECK_EQUAL( avi3->get(), atype->get().av[3].ai[3] ); 00145 BOOST_CHECK_EQUAL( bvi3->get(), atype->get().bv[3].ai[3] ); 00146 00147 // Check writing a part (must change in parent too). 00148 avi3->set(10); 00149 bvi3->set(20); 00150 BOOST_CHECK_EQUAL( avi3->get(), 10 ); 00151 BOOST_CHECK_EQUAL( avi3->get(), atype->get().av[3].ai[3] ); 00152 BOOST_CHECK_EQUAL( bvi3->get(), 20 ); 00153 BOOST_CHECK_EQUAL( bvi3->get(), atype->get().bv[3].ai[3] ); 00154 } 00155 00156 BOOST_AUTO_TEST_SUITE_END() 00157