test.cpp
Go to the documentation of this file.
00001 #define OROCOS_TARGET gnulinux
00002 #include <rtt/types/TypekitPlugin.hpp>
00003 #include <rtt/typekit/RealTimeTypekit.hpp>
00004 #include "simple/Types.hpp"
00005 #include "simple/Plugin.hpp"
00006 #include <string.h>
00007 
00008 #ifdef WITH_CORBA
00009 #include <omniORB4/CORBA.h>
00010 #include <rtt/transports/corba/CorbaLib.hpp>
00011 #include <rtt/transports/corba/CorbaTypeTransporter.hpp>
00012 #include "transports/corba/simpleTypesC.h"
00013 #include "transports/corba/TransportPlugin.hpp"
00014 #endif
00015 
00016 #ifdef WITH_TYPELIB
00017 #include "transports/typelib/TransportPlugin.hpp"
00018 #include <rtt/typelib/TypelibMarshallerBase.hpp>
00019 #endif
00020 
00021 #include <rtt/os/main.h>
00022 #include <rtt/types/Types.hpp>
00023 #include <rtt/PropertyBag.hpp>
00024 #include <rtt/Property.hpp>
00025 #include <rtt/internal/DataSource.hpp>
00026 #include <rtt/types/PropertyDecomposition.hpp>
00027 #include <rtt/marsh/PropertyMarshaller.hpp>
00028 #include <rtt/marsh/PropertyDemarshaller.hpp>
00029 #include <iostream>
00030 #include <fstream>
00031 #include <typeinfo>
00032 #include <stdexcept>
00033 
00034 using namespace RTT;
00035 using namespace RTT::types;
00036 using namespace RTT::internal;
00037 using namespace RTT::marsh;
00038 using std::cerr;
00039 using std::endl;
00040 
00041 TypeInfo* assert_typeinfo_is_available(std::string const& name)
00042 {
00043     TypeInfoRepository::shared_ptr ti = TypeInfoRepository::Instance();
00044     TypeInfo* type = ti->type(name);
00045     if (! type)
00046     {
00047         cerr << "cannot find " << name << " in the type info repository" << endl;
00048         throw std::runtime_error("failed assertion");
00049     }
00050     return type;
00051 }
00052 
00053 #ifdef WITH_CORBA
00054 template<typename T>
00055 bool generic_corba_test(T const& testValue, RTT::types::TypeInfo const& ti)
00056 {
00057     boost::intrusive_ptr< ValueDataSource<T> > source(new ValueDataSource<T>(testValue));
00058     source->ref();
00059 
00060     RTT::corba::CorbaTypeTransporter* transport =
00061         dynamic_cast<RTT::corba::CorbaTypeTransporter*>(ti.getProtocol(ORO_CORBA_PROTOCOL_ID));
00062 
00063     cerr << "- testing CORBA marshalling/unmarshalling ..." << endl;
00064     {
00065         CORBA::Any* result = transport->createAny(source);
00066 
00067         ValueDataSource<T>* reader = new ValueDataSource<T>();
00068         reader->ref();
00069         transport->updateFromAny(result, reader);
00070 
00071         T result_value = reader->get();
00072         if (!(result_value == testValue))
00073         {
00074             cerr << "discrepancy between original and unmarshalled values" << endl;
00075             return false;
00076         }
00077     }
00078     cerr << "  done" << endl;
00079     return true;
00080 }
00081 #endif
00082 
00083 #ifdef WITH_TYPELIB
00084 template<typename T>
00085 bool generic_typelib_test(T const& testValue, TypeInfo const& ti)
00086 {
00087     cerr << "- testing Typelib marshalling/unmarshalling ..." << endl;
00088     ConstantDataSource<T>* source
00089         = new ConstantDataSource<T>(testValue);
00090     source->ref();
00091 
00092     orogen_transports::TypelibMarshallerBase* transport =
00093         dynamic_cast<orogen_transports::TypelibMarshallerBase*>(ti.getProtocol(orogen_transports::TYPELIB_MARSHALLER_ID));
00094 
00095     std::vector<uint8_t> buffer;
00096     orogen_transports::TypelibMarshallerBase::Handle* handle =
00097         transport->createSample();
00098     transport->readDataSource(*source, handle);
00099     transport->marshal(buffer, handle);
00100     transport->deleteHandle(handle);
00101     source->deref();
00102 
00103     handle = transport->createSample();
00104     transport->unmarshal(buffer, handle);
00105     T unmarshalled =
00106         reinterpret_cast<T const&>(*transport->getTypelibSample(handle));
00107     transport->deleteHandle(handle);
00108 
00109     if (!(unmarshalled == testValue))
00110     {
00111         cerr << "unmarshalled Typelib data does not match original data" << endl;
00112         return false;
00113     }
00114     return true;
00115 }
00116 #endif
00117 
00118 
00119 template<typename T>
00120 bool generic_type_handling_test(std::string const& name, T const& testValue, TypeInfo const& ti)
00121 {
00122     cerr << "- testing composition/decomposition is disabled" << endl;
00123     //cerr << "- testing decomposition to XML formats" << endl;
00124     //ValueDataSource<T>* source
00125     //    = new ValueDataSource<T>(testValue);
00126     //source->ref();
00127 
00128     //PropertyBag bag;
00129     //if (!typeDecomposition(source, bag))
00130     //{
00131     //    cerr << "  decomposition failed" << endl;
00132     //    return false;
00133     //}
00134     //cerr << "  done decomposition" << endl;
00135 
00136     //std::vector<std::string> fields = bag.list();
00137     //cerr << "  " << fields.size() << " element(s) in the bag:";
00138     //for (int i = 0; i < fields.size(); ++i)
00139     //    cerr << " " << fields[i];
00140     //cerr << endl;
00141 
00144     //PropertyMarshaller cpf_output(name + ".cpf");
00145     //cpf_output.serialize(bag);
00146     //cpf_output.flush();
00147     //cerr << "  done CPF serialization" << endl;
00148 
00149     //cerr << "- testing unmarshalling from XML" << endl;
00150     //PropertyBag input_bag;
00151     //PropertyDemarshaller cpf_input(name + ".cpf");
00152     //cpf_input.deserialize(input_bag);
00153 
00154     //{ ValueDataSource<T>* reader = new ValueDataSource<T>();
00155     //    reader->ref();
00156     //    Property<PropertyBag> bag("", "", input_bag);
00157     //    if (!ti.composeType(bag.getDataSource(), reader))
00158     //    {
00159     //        cerr << "cannot recompose type" << endl;
00160     //        return false;
00161     //    }
00162 
00163     //    T value = reader->get();
00164     //    if (!(value == testValue))
00165     //    {
00166     //        cerr << "error, expected and actual values differ" << endl;
00167     //        return false;
00168     //    }
00169     //}
00170     //cerr << "  done" << endl;
00171 
00172 #ifdef WITH_CORBA
00173     if (!generic_corba_test(testValue, ti))
00174         return false;
00175 #endif
00176 #ifdef WITH_TYPELIB
00177     if (!generic_typelib_test(testValue, ti))
00178         return false;
00179 #endif
00180     return true;
00181 }
00182 
00183 bool test_base_types()
00184 {
00185     cerr << "Test handling of BaseTypes" << endl;
00186     TypeInfo* type = assert_typeinfo_is_available("/Test/BaseTypes");
00187 
00188     // Create a structure of type Test::BaseTypes, inject it into Orocos and check
00189     // that it is able to generate a XML representation of it
00190     Test::BaseTypes testValue;
00191     testValue.v0 = true;
00192     testValue.v1 = 'a';
00193     testValue.v5 = -100000;
00194     testValue.v6 = 3000000000UL;
00195     testValue.e   = Test::VALUE_20;
00196     for (int i = 0; i < 20; ++i)
00197         testValue.a[i] = 'a' + i;
00198 
00199     if (!generic_type_handling_test("basic", testValue, *type))
00200         return false;
00201     return true;
00202 }
00203 
00204 bool test_handling_of_invalid_enum_values()
00205 {
00206     cerr << "- testing composition/decomposition is disabled" << endl;
00207     //cerr << "test_handling_of_invalid_enum_values" << endl;
00208     //TypeInfo* type = assert_typeinfo_is_available("/Test/BaseTypes");
00209     //Test::BaseTypes testValue;
00210     //memset(&testValue, 0, sizeof(testValue));
00211     //testValue.e   = (Test::BASIC_ENUM)999;
00212 
00213     //boost::intrusive_ptr< ValueDataSource<Test::BaseTypes> > source(new ValueDataSource<Test::BaseTypes>(testValue));
00214     //source->ref();
00215 
00217     //cerr << "- testing that decomposition is forbidden" << endl;
00218     //PropertyBag bag;
00219     //if (typeDecomposition(source, bag))
00220     //{
00221     //    cerr << "  FAILED" << endl;
00222     //    return false;
00223     //}
00224 
00225     // TODO: test that CORBA fails as well
00226     return true;
00227 }
00228 
00229 bool test_64bit_handling()
00230 {
00231     cerr << "- testing composition/decomposition is disabled" << endl;
00232     //cerr << "Testing 64bit handling" << endl;
00233     TypeInfo* type = assert_typeinfo_is_available("/Test/Test64BitHandling");
00234 
00235     Test::Test64BitHandling testValue;
00236     memset(&testValue, 0, sizeof(testValue));
00237     testValue.base.v0 = true;
00238     testValue.base.v1 = 'a';
00239     testValue.base.v5 = -100000;
00240     testValue.base.v6 = 3000000000UL;
00241     testValue.base.e   = Test::VALUE_20;
00242     for (int i = 0; i < 20; ++i)
00243         testValue.base.a[i] = 'a' + i;
00244     testValue.ll  = -(1LL << 40);
00245     testValue.ull = -(1LL << 40);
00246 
00247     //boost::intrusive_ptr< ValueDataSource<Test::Test64BitHandling> > source(new ValueDataSource<Test::Test64BitHandling>(testValue));
00248     //source->ref();
00249 
00251     //cerr << "- testing that decomposition is forbidden" << endl;
00252     //PropertyBag bag;
00253     //if (typeDecomposition(source, bag))
00254     //{
00255     //    cerr << "  FAILED" << endl;
00256     //    return false;
00257     //}
00258 
00259     // But CORBA and typelib marshalling should work just fine
00260 #ifdef WITH_CORBA
00261     if (!generic_corba_test(testValue, *type))
00262         return false;
00263 #endif
00264 #ifdef WITH_TYPELIB
00265     if (!generic_typelib_test(testValue, *type))
00266         return false;
00267 #endif
00268     return true;
00269 }
00270 
00271 bool test_simple_vector()
00272 {
00273     cerr << "Testing simple structure with vector<>" << endl;
00274     TypeInfo* type = assert_typeinfo_is_available("/Test/SimpleVector");
00275 
00276     Test::SimpleVector testValue;
00277     testValue.field = 10;
00278     for (int i = 0; i < 20; ++i)
00279         testValue.data.push_back('a' + i);
00280 
00281     return generic_type_handling_test("simple_vector", testValue, *type);
00282 }
00283 
00284 bool test_complex_vector()
00285 {
00286     cerr << "Testing complex structure-in-vector<>" << endl;
00287     TypeInfo* type = assert_typeinfo_is_available("/Test/ComplexVector");
00288 
00289     Test::ComplexVector testValue;
00290     testValue.field = 10;
00291     testValue.data.resize(20);
00292     for (int i = 0; i < 20; ++i)
00293     {
00294         Test::SimpleVector& el = testValue.data[i];
00295         el.field = i;
00296         for (int j = 0; j < i + 5; ++j)
00297             el.data.push_back(i * 20 + j);
00298     }
00299 
00300     return generic_type_handling_test("complex_vector", testValue, *type);
00301 }
00302 
00303 bool test_complex_array()
00304 {
00305     cerr << "Testing complex structure-in-array" << endl;
00306     TypeInfo* type = assert_typeinfo_is_available("/Test/ComplexArray");
00307 
00308     Test::ComplexArray testValue;
00309     testValue.field = 10;
00310     for (int i = 0; i < 10; ++i)
00311     {
00312         Test::SimpleVector& el = testValue.data[i];
00313         el.field = i;
00314         for (int j = 0; j < i + 5; ++j)
00315             el.data.push_back(i * 20 + j);
00316     }
00317 
00318     return generic_type_handling_test("complex_array", testValue, *type);
00319 }
00320 
00321 
00322 
00323 
00324 int ORO_main(int argc, char** argv)
00325 {
00326     log().setLogLevel( Logger::Debug );
00327     RTT::types::TypekitRepository::Import( new RTT::types::RealTimeTypekitPlugin );
00328     RTT::types::TypekitRepository::Import( new orogen_typekits::simpleTypekitPlugin );
00329 #ifdef WITH_CORBA
00330     RTT::types::TypekitRepository::Import( new orogen_typekits::simpleCorbaTransportPlugin );
00331 #endif
00332 #ifdef WITH_TYPELIB
00333     RTT::types::TypekitRepository::Import( new orogen_typekits::simpleTypelibTransportPlugin );
00334 #endif
00335 
00336     TypeInfoRepository::shared_ptr ti = TypeInfoRepository::Instance();
00337 
00338     if (!test_base_types())
00339         return 1;
00340 
00341     cerr << endl;
00342     if (!test_handling_of_invalid_enum_values())
00343         return 1;
00344 
00345     cerr << endl;
00346     if (!test_64bit_handling())
00347         return 1;
00348 
00349     cerr << endl;
00350     if (!test_simple_vector())
00351         return 1;
00352 
00353     cerr << endl;
00354     if (!test_complex_vector())
00355         return 1;
00356 
00357     cerr << endl;
00358     if (!test_complex_array())
00359         return 1;
00360 
00361     return 0;
00362 }
00363 


orogen
Author(s): Sylvain Joyeux/sylvain.joyeux@m4x.org
autogenerated on Thu Jan 2 2014 11:38:57