type_discovery_struct_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 type_discovery_struct_test.cpp
3 
4  type_discovery_struct_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 
22 #include <boost/serialization/vector.hpp>
23 #include <boost/array.hpp>
24 
25 #include <rtt-fwd.hpp>
26 #include <internal/DataSources.hpp>
27 #include <types/type_discovery.hpp>
28 #include <os/fosi.h>
29 
30 #include "datasource_fixture.hpp"
31 #include "types/StructTypeInfo.hpp"
32 #include "types/CArrayTypeInfo.hpp"
35 
36 using namespace boost::archive;
37 using namespace boost::serialization;
38 
40 {
41 public:
44 };
45 
46 // Registers the fixture into the 'registry'
47 BOOST_FIXTURE_TEST_SUITE( TypeArchiveTestSuite, StructTypeTest )
48 
49 // Test the StructTypeInfo for AType
50 // Similar as the above tests, but now through the TypeInfo system.
51 BOOST_AUTO_TEST_CASE( testATypeStruct )
52 {
53  Types()->addType( new StructTypeInfo<AType>("AType") );
54 
56 
57  BOOST_REQUIRE( Types()->type("AType") );
58 
59  // check the part names lookup:
60  vector<string> names = atype->getMemberNames();
61  BOOST_CHECK_EQUAL( atype->getMemberNames().size(), 5 );
62 
63  BOOST_REQUIRE_EQUAL( names.size(), 5);
64  BOOST_REQUIRE( atype->getMember("a") );
65 
66  // Check individual part lookup by name:
70  AssignableDataSource<carray<int> >::shared_ptr ai = AssignableDataSource<carray<int> >::narrow( atype->getMember("ai").get());
71  AssignableDataSource<vector<double> >::shared_ptr vd = AssignableDataSource<vector<double> >::narrow( atype->getMember("vd").get());
72 
73  BOOST_REQUIRE( a );
74  BOOST_REQUIRE( b );
75  BOOST_REQUIRE( c );
76  BOOST_REQUIRE( ai );
77  BOOST_REQUIRE( vd );
78 
79  BOOST_CHECK( !atype->getMember("zort") );
80 
81  // Check reading parts (must equal parent)
82  BOOST_CHECK_EQUAL( a->get(), atype->get().a );
83  BOOST_CHECK_EQUAL( b->get(), atype->get().b );
84  BOOST_CHECK_EQUAL( c->get(), atype->get().c );
85  BOOST_CHECK_EQUAL( ai->get().address()[3], atype->get().ai[3] );
86  BOOST_CHECK_EQUAL( vd->get()[3], atype->get().vd[3] );
87 
88  // Check writing a part (must change in parent too).
89  a->set(10);
90  BOOST_CHECK_EQUAL( a->get(), 10 );
91  BOOST_CHECK_EQUAL( a->get(), atype->get().a );
92 }
93 
95 BOOST_AUTO_TEST_CASE( testCTypeStruct )
96 {
97  Types()->addType( new StructTypeInfo< AType >("AType") );
98  Types()->addType( new StructTypeInfo< BType >("BType") );
99  Types()->addType( new StructTypeInfo< CType >("CType") );
100  Types()->addType( new SequenceTypeInfo< vector<AType> >("as") );
101  Types()->addType( new SequenceTypeInfo< vector<BType> >("bs") );
102  Types()->addType( new CArrayTypeInfo< carray<int> >("cints") );
103  Types()->addType( new BoostArrayTypeInfo< boost::array<int,5> >("int5") );
104  Types()->addType( new SequenceTypeInfo< vector<int> >("ints") );
105 
107 
108  // decompose a complex type
111  AssignableDataSource< vector<AType> >::shared_ptr av = AssignableDataSource< vector<AType> >::narrow( atype->getMember("av").get());
112  AssignableDataSource< vector<BType> >::shared_ptr bv = AssignableDataSource< vector<BType> >::narrow( atype->getMember("bv").get());
113 
114  BOOST_REQUIRE( a );
115  BOOST_REQUIRE( b );
116  BOOST_REQUIRE( av );
117  BOOST_REQUIRE( bv );
118 
119  // Access top level elements
120  BOOST_REQUIRE( a->getMember("ai") );
121  AssignableDataSource<int>::shared_ptr ai3 = dynamic_pointer_cast< AssignableDataSource<int> >( a->getMember("ai")->getMember("3") );
122  BOOST_REQUIRE( b->getMember("ai") );
123  AssignableDataSource<int>::shared_ptr bi3 = dynamic_pointer_cast< AssignableDataSource<int> >( b->getMember("ai")->getMember("3") );
124 
125  // Access elements in sequences:
126  AssignableDataSource<int>::shared_ptr avi3 = dynamic_pointer_cast< AssignableDataSource<int> >( av->getMember("3")->getMember("ai")->getMember("3") );
127  AssignableDataSource<int>::shared_ptr bvi3 = dynamic_pointer_cast< AssignableDataSource<int> >( bv->getMember("3")->getMember("ai")->getMember("3") );
128 
129  BOOST_REQUIRE( ai3 );
130  BOOST_REQUIRE( bi3 );
131  BOOST_REQUIRE( avi3 );
132  BOOST_REQUIRE( bvi3 );
133 
134  // Check reading parts (must equal parent)
135  BOOST_CHECK_EQUAL( a->get(), atype->get().a );
136  BOOST_CHECK_EQUAL( b->get(), atype->get().b );
137  BOOST_CHECK( std::equal(av->set().begin(), av->set().end(), atype->set().av.begin() ) );
138  BOOST_CHECK( std::equal(bv->set().begin(), bv->set().end(), atype->set().bv.begin() ) );
139  BOOST_CHECK_EQUAL( avi3->get(), atype->get().av[3].ai[3] );
140  BOOST_CHECK_EQUAL( bvi3->get(), atype->get().bv[3].ai[3] );
141 
142  // Check writing a part (must change in parent too).
143  avi3->set(10);
144  bvi3->set(20);
145  BOOST_CHECK_EQUAL( avi3->get(), 10 );
146  BOOST_CHECK_EQUAL( avi3->get(), atype->get().av[3].ai[3] );
147  BOOST_CHECK_EQUAL( bvi3->get(), 20 );
148  BOOST_CHECK_EQUAL( bvi3->get(), atype->get().bv[3].ai[3] );
149 }
150 
152 
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
virtual result_t get() const =0
virtual std::vector< std::string > getMemberNames() const
Definition: DataSource.cpp:134
#define BOOST_AUTO_TEST_SUITE_END()
virtual void set(param_t t)=0
vector< AType > av
virtual shared_ptr getMember(const std::string &member_name)
Definition: DataSource.cpp:124
TypeInfoRepository::shared_ptr Types()
Definition: Types.cpp:48
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
Definition: DataSource.hpp:198
vector< double > vd
boost::array< int, 5 > ai
vector< BType > bv
DataSource< T >::result_t get() const
Definition: DataSources.hpp:78
BOOST_AUTO_TEST_CASE(testATypeStruct)


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