$search
00001 /*************************************************************************** 00002 tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 type_discovery_container_test.cpp 00003 00004 type_discovery_container_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 SequenceTypeTest 00042 { 00043 public: 00044 SequenceTypeTest() { } 00045 ~SequenceTypeTest() { } 00046 }; 00047 00048 // Registers the fixture into the 'registry' 00049 BOOST_FIXTURE_TEST_SUITE( SequenceTypeInfoTestSuite, SequenceTypeTest ) 00050 00051 // Test the CArrayTypeInfo for ints 00052 BOOST_AUTO_TEST_CASE( testCTypeArray ) 00053 { 00054 Types()->addType( new CArrayTypeInfo< carray<int> >("cints") ); 00055 int tester[3] = { 3, 2, 1 }; 00056 00057 AssignableDataSource< carray<int> >::shared_ptr atype = new ValueDataSource< carray<int> >( carray<int>(tester, 3) ); 00058 00059 BOOST_REQUIRE( Types()->type("cints") ); 00060 00061 // check the part names lookup: 00062 vector<string> names = atype->getMemberNames(); 00063 BOOST_CHECK_EQUAL( atype->getMemberNames().size(), 2 ); // capacity,size 00064 00065 // for_each( names.begin(), names.end(), cout << lambda::_1 <<", " ); 00066 // cout <<endl; 00067 00068 BOOST_REQUIRE_EQUAL( names.size(), 2); 00069 BOOST_REQUIRE( atype->getMember("0") ); 00070 00071 // Check individual part lookup by index: 00072 AssignableDataSource<int>::shared_ptr a0 = AssignableDataSource<int>::narrow( atype->getMember("0").get() ); 00073 AssignableDataSource<int>::shared_ptr a1 = AssignableDataSource<int>::narrow( atype->getMember("1").get() ); 00074 AssignableDataSource<int>::shared_ptr a2 = AssignableDataSource<int>::narrow( atype->getMember("2").get() ); 00075 00076 BOOST_REQUIRE( a0 ); 00077 BOOST_REQUIRE( a1 ); 00078 BOOST_REQUIRE( a2 ); 00079 00080 BOOST_CHECK( !atype->getMember("zort") ); 00081 00082 // Check reading parts (must equal parent) 00083 BOOST_CHECK_EQUAL( a0->get(), tester[0] ); 00084 BOOST_CHECK_EQUAL( a1->get(), tester[1] ); 00085 BOOST_CHECK_EQUAL( a2->get(), tester[2] ); 00086 00087 // Check writing a part (must change in parent too). 00088 a0->set(30); 00089 a1->set(20); 00090 a2->set(10); 00091 BOOST_CHECK_EQUAL( a0->get(), tester[0] ); 00092 BOOST_CHECK_EQUAL( a1->get(), tester[1] ); 00093 BOOST_CHECK_EQUAL( a2->get(), tester[2] ); 00094 } 00095 00096 // Test the SequenceTypeInfo for ints 00097 BOOST_AUTO_TEST_CASE( testContainerType ) 00098 { 00099 Types()->addType( new SequenceTypeInfo< std::vector<int> >("ints") ); 00100 vector<int> tester; 00101 tester.push_back( 3 ); 00102 tester.push_back( 2 ); 00103 tester.push_back( 1 ); 00104 00105 AssignableDataSource< vector<int> >::shared_ptr atype = new ReferenceDataSource< vector<int> >( tester ); 00106 00107 BOOST_REQUIRE( Types()->type("ints") == atype->getTypeInfo() ); 00108 00109 // check the part names lookup: 00110 vector<string> names = atype->getMemberNames(); 00111 BOOST_CHECK_EQUAL( atype->getMemberNames().size(), 2 ); // capacity,size 00112 00113 // for_each( names.begin(), names.end(), cout << lambda::_1 <<", " ); 00114 // cout <<endl; 00115 00116 BOOST_REQUIRE_EQUAL( names.size(), 2); 00117 BOOST_REQUIRE( atype->getMember("0") ); 00118 00119 // Check individual part lookup by index: 00120 AssignableDataSource<int>::shared_ptr a0 = dynamic_pointer_cast< AssignableDataSource<int> >( atype->getMember("0") ); 00121 AssignableDataSource<int>::shared_ptr a1 = dynamic_pointer_cast< AssignableDataSource<int> >( atype->getMember("1") ); 00122 AssignableDataSource<int>::shared_ptr a2 = dynamic_pointer_cast< AssignableDataSource<int> >( atype->getMember("2") ); 00123 DataSource<int>::shared_ptr siz = dynamic_pointer_cast< DataSource<int> >( atype->getMember("size") ); 00124 DataSource<int>::shared_ptr cap = dynamic_pointer_cast< DataSource<int> >( atype->getMember("capacity") ); 00125 00126 BOOST_REQUIRE( a0 ); 00127 BOOST_REQUIRE( a1 ); 00128 BOOST_REQUIRE( a2 ); 00129 BOOST_REQUIRE( siz ); 00130 BOOST_REQUIRE( cap ); 00131 00132 BOOST_CHECK( !atype->getMember("zort") ); 00133 00134 // Check reading parts (must equal parent) 00135 BOOST_CHECK_EQUAL( a0->get(), tester[0] ); 00136 BOOST_CHECK_EQUAL( a1->get(), tester[1] ); 00137 BOOST_CHECK_EQUAL( a2->get(), tester[2] ); 00138 00139 // Check modifying size/capacity. 00140 tester.reserve(33); 00141 BOOST_CHECK_EQUAL( cap->get(), tester.capacity() ); 00142 00143 tester.push_back(4); 00144 tester.push_back(5); 00145 tester.push_back(6); 00146 BOOST_CHECK_EQUAL( siz->get(), tester.size() ); 00147 00148 00149 // Check writing a part (must change in parent too). 00150 a0->set(30); 00151 a1->set(20); 00152 a2->set(10); 00153 BOOST_CHECK_EQUAL( a0->get(), tester[0] ); 00154 BOOST_CHECK_EQUAL( a1->get(), tester[1] ); 00155 BOOST_CHECK_EQUAL( a2->get(), tester[2] ); 00156 } 00157 00158 // Test the SequenceTypeInfo for chars (std::string) 00159 BOOST_AUTO_TEST_CASE( testStringContainerType ) 00160 { 00161 string tester = "tester"; 00162 00163 AssignableDataSource< string >::shared_ptr atype = new ReferenceDataSource< string >( tester ); 00164 00165 BOOST_REQUIRE( Types()->type("string") == atype->getTypeInfo() ); 00166 00167 // check the part names lookup: 00168 vector<string> names = atype->getMemberNames(); 00169 BOOST_CHECK_EQUAL( atype->getMemberNames().size(), 2 ); // capacity,size 00170 00171 // for_each( names.begin(), names.end(), cout << lambda::_1 <<", " ); 00172 // cout <<endl; 00173 00174 BOOST_REQUIRE_EQUAL( names.size(), 2); 00175 BOOST_REQUIRE( atype->getMember("0") ); 00176 00177 // test use of 'getAssignable()' to narrow: 00178 DataSourceBase::shared_ptr ds0 = atype->getMember("0"); 00179 BOOST_REQUIRE( ds0 ); 00180 AssignableDataSource<char>::shared_ptr a0 = AssignableDataSource<char>::narrow( ds0.get() ); 00181 BOOST_REQUIRE( a0 ); 00182 00183 // Check individual part lookup by index: 00184 AssignableDataSource<char>::shared_ptr a1 = dynamic_pointer_cast< AssignableDataSource<char> >( atype->getMember("1") ); 00185 AssignableDataSource<char>::shared_ptr a2 = dynamic_pointer_cast< AssignableDataSource<char> >( atype->getMember("2") ); 00186 DataSource<int>::shared_ptr siz = dynamic_pointer_cast< DataSource<int> >( atype->getMember("size") ); 00187 DataSource<int>::shared_ptr cap = dynamic_pointer_cast< DataSource<int> >( atype->getMember("capacity") ); 00188 00189 BOOST_REQUIRE( a1 ); 00190 BOOST_REQUIRE( a2 ); 00191 BOOST_REQUIRE( siz ); 00192 BOOST_REQUIRE( cap ); 00193 00194 BOOST_CHECK( !atype->getMember("zort") ); 00195 00196 // Check reading parts (must equal parent) 00197 BOOST_CHECK_EQUAL( a0->get(), tester[0] ); 00198 BOOST_CHECK_EQUAL( a1->get(), tester[1] ); 00199 BOOST_CHECK_EQUAL( a2->get(), tester[2] ); 00200 00201 // Check modifying size/capacity. 00202 tester.reserve(33); 00203 BOOST_CHECK_EQUAL( cap->get(), tester.capacity() ); 00204 00205 tester.push_back(4); 00206 tester.push_back(5); 00207 tester.push_back(6); 00208 BOOST_CHECK_EQUAL( siz->get(), tester.size() ); 00209 00210 00211 // Check writing a part (must change in parent too). 00212 a0->set(30); 00213 a1->set(20); 00214 a2->set(10); 00215 BOOST_CHECK_EQUAL( a0->get(), tester[0] ); 00216 BOOST_CHECK_EQUAL( a1->get(), tester[1] ); 00217 BOOST_CHECK_EQUAL( a2->get(), tester[2] ); 00218 } 00219 00220 BOOST_AUTO_TEST_SUITE_END() 00221