type_discovery_container_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 type_discovery_container_test.cpp
3 
4  type_discovery_container_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( SequenceTypeInfoTestSuite, SequenceTypeTest )
48 
49 // Test the CArrayTypeInfo for ints
50 BOOST_AUTO_TEST_CASE( testCTypeArray )
51 {
52  Types()->addType( new CArrayTypeInfo< carray<int> >("cints") );
53  int tester[3] = { 3, 2, 1 };
54 
55  AssignableDataSource< carray<int> >::shared_ptr atype = new ValueDataSource< carray<int> >( carray<int>(tester, 3) );
56 
57  BOOST_REQUIRE( Types()->type("cints") );
58 
59  // check the part names lookup:
60  vector<string> names = atype->getMemberNames();
61  BOOST_CHECK_EQUAL( atype->getMemberNames().size(), 2 ); // capacity,size
62 
63  BOOST_REQUIRE_EQUAL( names.size(), 2);
64  BOOST_REQUIRE( atype->getMember("0") );
65 
66  // Check individual part lookup by index:
70 
71  BOOST_REQUIRE( a0 );
72  BOOST_REQUIRE( a1 );
73  BOOST_REQUIRE( a2 );
74 
75  BOOST_CHECK( !atype->getMember("zort") );
76 
77  // Check reading parts (must equal parent)
78  BOOST_CHECK_EQUAL( a0->get(), tester[0] );
79  BOOST_CHECK_EQUAL( a1->get(), tester[1] );
80  BOOST_CHECK_EQUAL( a2->get(), tester[2] );
81 
82  // Check writing a part (must change in parent too).
83  a0->set(30);
84  a1->set(20);
85  a2->set(10);
86  BOOST_CHECK_EQUAL( a0->get(), tester[0] );
87  BOOST_CHECK_EQUAL( a1->get(), tester[1] );
88  BOOST_CHECK_EQUAL( a2->get(), tester[2] );
89 }
90 
91 // Test the SequenceTypeInfo for ints
92 BOOST_AUTO_TEST_CASE( testContainerType )
93 {
94  Types()->addType( new SequenceTypeInfo< std::vector<int> >("ints") );
95  vector<int> tester;
96  tester.push_back( 3 );
97  tester.push_back( 2 );
98  tester.push_back( 1 );
99 
100  AssignableDataSource< vector<int> >::shared_ptr atype = new ReferenceDataSource< vector<int> >( tester );
101 
102  BOOST_REQUIRE( Types()->type("ints") == atype->getTypeInfo() );
103 
104  // check the part names lookup:
105  vector<string> names = atype->getMemberNames();
106  BOOST_CHECK_EQUAL( atype->getMemberNames().size(), 2 ); // capacity,size
107 
108  BOOST_REQUIRE_EQUAL( names.size(), 2);
109  BOOST_REQUIRE( atype->getMember("0") );
110 
111  // Check individual part lookup by index:
112  AssignableDataSource<int>::shared_ptr a0 = dynamic_pointer_cast< AssignableDataSource<int> >( atype->getMember("0") );
113  AssignableDataSource<int>::shared_ptr a1 = dynamic_pointer_cast< AssignableDataSource<int> >( atype->getMember("1") );
114  AssignableDataSource<int>::shared_ptr a2 = dynamic_pointer_cast< AssignableDataSource<int> >( atype->getMember("2") );
115  DataSource<int>::shared_ptr siz = dynamic_pointer_cast< DataSource<int> >( atype->getMember("size") );
116  DataSource<int>::shared_ptr cap = dynamic_pointer_cast< DataSource<int> >( atype->getMember("capacity") );
117 
118  BOOST_REQUIRE( a0 );
119  BOOST_REQUIRE( a1 );
120  BOOST_REQUIRE( a2 );
121  BOOST_REQUIRE( siz );
122  BOOST_REQUIRE( cap );
123 
124  BOOST_CHECK( !atype->getMember("zort") );
125 
126  // Check reading parts (must equal parent)
127  BOOST_CHECK_EQUAL( a0->get(), tester[0] );
128  BOOST_CHECK_EQUAL( a1->get(), tester[1] );
129  BOOST_CHECK_EQUAL( a2->get(), tester[2] );
130 
131  // Check modifying size/capacity.
132  tester.reserve(33);
133  BOOST_CHECK_EQUAL( cap->get(), tester.capacity() );
134 
135  tester.push_back(4);
136  tester.push_back(5);
137  tester.push_back(6);
138  BOOST_CHECK_EQUAL( siz->get(), tester.size() );
139 
140 
141  // Check writing a part (must change in parent too).
142  a0->set(30);
143  a1->set(20);
144  a2->set(10);
145  BOOST_CHECK_EQUAL( a0->get(), tester[0] );
146  BOOST_CHECK_EQUAL( a1->get(), tester[1] );
147  BOOST_CHECK_EQUAL( a2->get(), tester[2] );
148 }
149 
150 // Test the SequenceTypeInfo for chars (std::string)
151 BOOST_AUTO_TEST_CASE( testStringContainerType )
152 {
153  string tester = "tester";
154 
156 
157  BOOST_REQUIRE( Types()->type("string") == atype->getTypeInfo() );
158 
159  // check the part names lookup:
160  vector<string> names = atype->getMemberNames();
161  BOOST_CHECK_EQUAL( atype->getMemberNames().size(), 2 ); // capacity,size
162 
163  BOOST_REQUIRE_EQUAL( names.size(), 2);
164  BOOST_REQUIRE( atype->getMember("0") );
165 
166  // test use of 'getAssignable()' to narrow:
167  DataSourceBase::shared_ptr ds0 = atype->getMember("0");
168  BOOST_REQUIRE( ds0 );
170  BOOST_REQUIRE( a0 );
171 
172  // Check individual part lookup by index:
173  AssignableDataSource<char>::shared_ptr a1 = dynamic_pointer_cast< AssignableDataSource<char> >( atype->getMember("1") );
174  AssignableDataSource<char>::shared_ptr a2 = dynamic_pointer_cast< AssignableDataSource<char> >( atype->getMember("2") );
175  DataSource<int>::shared_ptr siz = dynamic_pointer_cast< DataSource<int> >( atype->getMember("size") );
176  DataSource<int>::shared_ptr cap = dynamic_pointer_cast< DataSource<int> >( atype->getMember("capacity") );
177 
178  BOOST_REQUIRE( a1 );
179  BOOST_REQUIRE( a2 );
180  BOOST_REQUIRE( siz );
181  BOOST_REQUIRE( cap );
182 
183  BOOST_CHECK( !atype->getMember("zort") );
184 
185  // Check reading parts (must equal parent)
186  BOOST_CHECK_EQUAL( a0->get(), tester[0] );
187  BOOST_CHECK_EQUAL( a1->get(), tester[1] );
188  BOOST_CHECK_EQUAL( a2->get(), tester[2] );
189 
190  // Check modifying size/capacity.
191  tester.reserve(33);
192  BOOST_CHECK_EQUAL( cap->get(), tester.capacity() );
193 
194  tester.push_back(4);
195  tester.push_back(5);
196  tester.push_back(6);
197  BOOST_CHECK_EQUAL( siz->get(), tester.size() );
198 
199 
200  // Check writing a part (must change in parent too).
201  a0->set(30);
202  a1->set(20);
203  a2->set(10);
204  BOOST_CHECK_EQUAL( a0->get(), tester[0] );
205  BOOST_CHECK_EQUAL( a1->get(), tester[1] );
206  BOOST_CHECK_EQUAL( a2->get(), tester[2] );
207 }
208 
210 
#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
BOOST_AUTO_TEST_CASE(testCTypeArray)
virtual const types::TypeInfo * getTypeInfo() const
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
boost::intrusive_ptr< DataSourceBase > shared_ptr


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