$search
00001 /*************************************************************************** 00002 tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 datasource_test.cpp 00003 00004 datasource_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/array.hpp> 00023 00024 #include <rtt-fwd.hpp> 00025 00026 #include <internal/DataSources.hpp> 00027 #include <internal/PartDataSource.hpp> 00028 #include <internal/ArrayPartDataSource.hpp> 00029 //#include <internal/OffsetPartDataSource.hpp> 00030 #include <internal/carray.hpp> 00031 00032 #include <boost/serialization/array.hpp> 00033 00034 #include <os/fosi.h> 00035 #include <boost/lambda/lambda.hpp> 00036 #include "datasource_fixture.hpp" 00037 00038 using namespace boost::lambda; 00039 00040 class DataSourceTest 00041 { 00042 public: 00043 AType atype; 00044 AType atype_orig; 00045 00046 BType btype; 00047 BType btype_orig; 00048 DataSourceTest() { 00049 atype.init(); 00050 atype_orig.init(); 00051 btype.init(); 00052 btype_orig.init(); 00053 } 00054 ~DataSourceTest() { } 00055 }; 00056 00057 // Registers the fixture into the 'registry' 00058 BOOST_FIXTURE_TEST_SUITE( DataSourceTestSuite, DataSourceTest ) 00059 00060 // Test semantics of ValueDataSource and test fixture. 00061 BOOST_AUTO_TEST_CASE( testValueDataSource ) 00062 { 00063 AssignableDataSource<AType>::shared_ptr d = new ValueDataSource<AType>( atype ); 00064 00065 // Test fixture: 00066 BOOST_CHECK_EQUAL( atype, atype ); 00067 BOOST_CHECK_EQUAL( atype, atype_orig ); 00068 00069 // Do not take by reference: 00070 BOOST_CHECK_EQUAL( d->set(), atype ); 00071 atype.a = -atype.a; 00072 BOOST_CHECK_EQUAL( d->set(), atype_orig ); 00073 } 00074 00075 // Test constant DataSource. 00076 BOOST_AUTO_TEST_CASE( testConstantDataSource ) 00077 { 00078 DataSource<AType>::shared_ptr d = new ConstantDataSource<AType>( atype ); 00079 00080 // Do not take by reference: 00081 BOOST_CHECK_EQUAL( d->get(), atype ); 00082 atype.a = -atype.a; 00083 BOOST_CHECK_EQUAL( d->get(), atype_orig ); 00084 } 00085 00086 // Test reference to C++ data 00087 BOOST_AUTO_TEST_CASE( testReferenceDataSource ) 00088 { 00089 AssignableDataSource<AType>::shared_ptr d = new ReferenceDataSource<AType>( atype ); 00090 00091 // Take by reference: 00092 BOOST_CHECK_EQUAL( d->set(), atype ); 00093 atype.a = -atype.a; 00094 BOOST_CHECK_EQUAL( d->set(), atype ); 00095 00096 d->set().a = 55; 00097 BOOST_CHECK_EQUAL( d->set(), atype ); 00098 } 00099 00100 // Test const reference to C++ data 00101 BOOST_AUTO_TEST_CASE( testConstReferenceDataSource ) 00102 { 00103 DataSource<AType>::shared_ptr d = new ConstReferenceDataSource<AType>( atype ); 00104 00105 // Take by reference: 00106 BOOST_CHECK_EQUAL( d->get(), atype ); 00107 atype.a = -atype.a; 00108 BOOST_CHECK_EQUAL( d->get(), atype ); 00109 } 00110 00111 // Test part reference to C++ data 00112 BOOST_AUTO_TEST_CASE( testPartDataSource ) 00113 { 00114 DataSource<AType>::shared_ptr abase = new UpdatedReferenceDataSource<AType>( atype ); 00115 AssignableDataSource<string>::shared_ptr d = new PartDataSource<string>( atype.c, abase ); 00116 00117 // Take string by reference: 00118 BOOST_CHECK_EQUAL( d->set(), atype.c ); 00119 atype.c = "world"; 00120 BOOST_CHECK_EQUAL( d->set(), atype.c ); 00121 00122 // Test both versions of set(). This also checks updated(): 00123 d->set( "Long gone!" ); 00124 BOOST_CHECK_EQUAL( d->set(), abase->get().c ); 00125 BOOST_CHECK_EQUAL( d->set(), atype.c ); 00126 00127 d->set() = "And back!"; 00128 d->updated(); 00129 BOOST_CHECK_EQUAL( d->set(), abase->get().c ); 00130 BOOST_CHECK_EQUAL( d->set(), atype.c ); 00131 } 00132 00135 BOOST_AUTO_TEST_CASE( testArrayDataSource ) 00136 { 00137 DataSource<BType>::shared_ptr abase = new UpdatedReferenceDataSource<BType>( btype ); 00138 AssignableDataSource<carray<char> >::shared_ptr d = 00139 new ValueDataSource<carray<char> >( carray<char>(btype.c, 10) ); 00140 00141 // Take string by reference (!): 00142 BOOST_CHECK_EQUAL( d->set().address(), btype.c ); 00143 strcpy( btype.c, "world"); 00144 BOOST_CHECK_EQUAL( "world", btype.c ); 00145 BOOST_CHECK_EQUAL( d->set().address(), btype.c ); 00146 00147 // Test both versions of set(). This also checks updated(): 00148 *d->set().address() = 'L'; 00149 d->updated(); 00150 BOOST_CHECK_EQUAL( "Lorld", btype.c ); 00151 BOOST_CHECK_EQUAL( "Lorld", d->get().address() ); 00152 BOOST_CHECK_EQUAL( d->set().address(), btype.c ); 00153 } 00154 00157 BOOST_AUTO_TEST_CASE( testArrayPartDataSource ) 00158 { 00159 AssignableDataSource<unsigned int>::shared_ptr index = new ValueDataSource<unsigned int>(0); 00160 DataSource<BType>::shared_ptr abase = new UpdatedReferenceDataSource<BType>( btype ); 00161 AssignableDataSource<char>::shared_ptr d = new ArrayPartDataSource<char>( *btype.c, index, abase, sizeof(btype.c) ); 00162 00163 // Take string by reference: 00164 BOOST_CHECK_EQUAL( &d->set(), btype.c ); 00165 strcpy( btype.c, "world"); 00166 BOOST_CHECK_EQUAL( "world", btype.c ); 00167 BOOST_CHECK_EQUAL( &d->set(), btype.c ); 00168 00169 // Test both versions of set(). This also checks updated(): 00170 d->set( 'L' ); 00171 BOOST_CHECK_EQUAL( "Lorld", btype.c ); 00172 BOOST_CHECK( strcmp( &d->set(), abase->get().c) == 0 ); 00173 BOOST_CHECK_EQUAL( &d->set(), btype.c ); 00174 00175 d->set() = 'W'; 00176 d->updated(); 00177 BOOST_CHECK_EQUAL( "World", btype.c ); 00178 BOOST_CHECK( strcmp( &d->set(), abase->get().c) == 0 ); 00179 BOOST_CHECK_EQUAL( &d->set(), btype.c ); 00180 00181 index->set( 3 ); 00182 d->set( 'L' ); 00183 BOOST_CHECK_EQUAL( "WorLd", btype.c ); 00184 BOOST_CHECK( strcmp( &d->set(), &abase->get().c[3]) == 0 ); 00185 BOOST_CHECK_EQUAL( &d->set(), &btype.c[3] ); 00186 } 00187 00188 00189 BOOST_AUTO_TEST_SUITE_END() 00190