DataInOutPortTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log: DataInOutPortTests.cpp,v $
00014  * Revision 1.2  2008/07/18 04:32:00  arafune
00015  * *** empty log message ***
00016  *
00017  * Revision 1.1  2007/12/20 07:50:18  arafune
00018  * *** empty log message ***
00019  *
00020  * Revision 1.1  2007/01/06 18:04:51  n-ando
00021  * The first commitment.
00022  *
00023  *
00024  */
00025 
00026 #ifndef DataInOutPort_cpp
00027 #define DataInOutPort_cpp
00028 
00029 #include <iostream>
00030 
00031 #include <cppunit/ui/text/TestRunner.h>
00032 #include <cppunit/TextOutputter.h>
00033 #include <cppunit/extensions/TestFactoryRegistry.h>
00034 #include <cppunit/extensions/HelperMacros.h>
00035 #include <cppunit/TestAssert.h>
00036 
00037 #include <rtm/idl/BasicDataTypeSkel.h>
00038 #include <rtm/idl/RTCSkel.h>
00039 #include <rtm/DataOutPort.h>
00040 #include <rtm/OutPort.h>
00041 #include <rtm/DataInPort.h>
00042 #include <rtm/InPort.h>
00043 #include <coil/Properties.h>
00044 
00049 namespace DataInOutPort
00050 {  
00051   template <class DataType>
00052   struct HogeCovnert : public RTC::OnReadConvert<DataType>
00053   {
00054     DataType operator()(const DataType& value)
00055     {
00056       DataType d(value);
00057       d.data = value.data * value.data;
00058       return d;
00059     }
00060   };
00061 
00062   class DataInOutPortTests
00063    : public CppUnit::TestFixture
00064   {
00065     CPPUNIT_TEST_SUITE(DataInOutPortTests);
00066     CPPUNIT_TEST(test_connect);
00067     CPPUNIT_TEST_SUITE_END();
00068   
00069   private:
00070     RTC::OutPort<RTC::TimedFloat> m_outport;
00071     RTC::TimedFloat m_ofloat;
00072     RTC::PortService_var m_oportref;
00073 
00074     RTC::InPort<RTC::TimedFloat> m_inport;
00075     RTC::TimedFloat m_ifloat;
00076     RTC::PortService_var m_iportref;
00077 
00078     HogeCovnert<RTC::TimedFloat>* m_conv;
00079     CORBA::ORB_ptr m_pORB;
00080     PortableServer::POA_ptr m_pPOA;
00081   public:
00082   
00086     DataInOutPortTests()
00087       : m_outport("fout", m_ofloat),
00088         m_inport("fin", m_ifloat)
00089     {
00090       m_conv = new HogeCovnert<RTC::TimedFloat>();
00091 
00092       int argc(0);
00093       char** argv(NULL);
00094       m_pORB = CORBA::ORB_init(argc, argv);
00095       m_pPOA = PortableServer::POA::_narrow(
00096                     m_pORB->resolve_initial_references("RootPOA"));
00097       m_pPOA->the_POAManager()->activate();
00098 
00099       coil::Properties dummy;
00100       m_inport.init(dummy);
00101       m_outport.init(dummy);
00102       m_oportref = m_outport.get_port_profile()->port_ref;
00103       m_iportref = m_inport.get_port_profile()->port_ref;
00104 
00105     }
00106     
00110     ~DataInOutPortTests()
00111     {
00112       delete m_conv;
00113     }
00114   
00118     virtual void setUp()
00119     {
00120     }
00121     
00125     virtual void tearDown()
00126     { 
00127     }
00128   
00129     /* test case */
00130     void test_connect()
00131     {
00132       RTC::ConnectorProfile prof;
00133       prof.connector_id = "";
00134       prof.name = CORBA::string_dup("connector0");
00135       prof.ports.length(2);
00136       prof.ports[0] = m_oportref;
00137       prof.ports[1] = m_iportref;
00138 
00139       CORBA_SeqUtil::push_back(prof.properties,
00140                                NVUtil::newNV("dataport.interface_type",
00141                                              "corba_cdr"));
00142 
00143       CORBA_SeqUtil::push_back(prof.properties,
00144                                NVUtil::newNV("dataport.dataflow_type",
00145                                              "push"));
00146       CORBA_SeqUtil::push_back(prof.properties,
00147                                NVUtil::newNV("dataport.subscription_type",
00148                                              "flush"));
00149       RTC::ReturnCode_t ret= m_inport.connect(prof);
00150       CPPUNIT_ASSERT(ret == RTC::RTC_OK);
00151       
00152       RTC::ConnectorProfileList* iprof;
00153       iprof = m_inport.get_connector_profiles();
00154 
00155       RTC::ConnectorProfileList* oprof;
00156       oprof = m_outport.get_connector_profiles();
00157 
00158 #ifdef DEBUG
00159       std::cout << "Returned  connector ID"
00160                 << prof.connector_id << std::endl;
00161       std::cout << "InPort's  connector ID" 
00162                 << (*iprof)[0].connector_id << std::endl;
00163       std::cout << "OutPort's connector ID"
00164                 << (*oprof)[0].connector_id << std::endl;
00165 #endif
00166       std::string c_id, i_id, o_id;
00167       c_id = prof.connector_id;
00168       i_id = (*iprof)[0].connector_id;
00169       o_id = (*oprof)[0].connector_id;
00170 
00171       CPPUNIT_ASSERT(c_id == o_id);
00172       CPPUNIT_ASSERT(c_id == i_id);
00173       CPPUNIT_ASSERT(o_id == i_id);
00174 
00175       for (int i = 0; i < 100; ++i)
00176         {
00177           m_ofloat.data = 1.234567 * i;
00178           m_outport.write();
00179 
00180           m_inport.read();
00181 #ifdef DEBUG
00182           sleep(1);
00183           std::cout <<  m_ofloat.data << " <=> " << m_ifloat.data << std::endl;
00184 #endif
00185           CPPUNIT_ASSERT(m_ofloat.data == m_ifloat.data);
00186         }
00187 
00188         m_pPOA->deactivate_object(*m_pPOA->servant_to_id(&m_inport));
00189         m_pPOA->deactivate_object(*m_pPOA->servant_to_id(&m_outport));
00190 
00191     }
00192   };
00193 }; // namespace DataInOutPort
00194 
00195 /*
00196  * Register test suite
00197  */
00198 CPPUNIT_TEST_SUITE_REGISTRATION(DataInOutPort::DataInOutPortTests);
00199 
00200 #ifdef LOCAL_MAIN
00201 int main(int argc, char* argv[])
00202 {
00203 
00204   FORMAT format = TEXT_OUT;
00205   int target = 0;
00206   std::string xsl;
00207   std::string ns;
00208   std::string fname;
00209   std::ofstream ofs;
00210 
00211   int i(1);
00212   while (i < argc)
00213     {
00214       std::string arg(argv[i]);
00215       std::string next_arg;
00216       if (i + 1 < argc) next_arg = argv[i + 1];
00217       else              next_arg = "";
00218 
00219       if (arg == "--text") { format = TEXT_OUT; break; }
00220       if (arg == "--xml")
00221         {
00222           if (next_arg == "")
00223             {
00224               fname = argv[0];
00225               fname += ".xml";
00226             }
00227           else
00228             {
00229               fname = next_arg;
00230             }
00231           format = XML_OUT;
00232           ofs.open(fname.c_str());
00233         }
00234       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00235       if ( arg == "--cerr"      ) { target = 1; break; }
00236       if ( arg == "--xsl"       )
00237         {
00238           if (next_arg == "") xsl = "default.xsl"; 
00239           else                xsl = next_arg;
00240         }
00241       if ( arg == "--namespace" )
00242         {
00243           if (next_arg == "")
00244             {
00245               std::cerr << "no namespace specified" << std::endl;
00246               exit(1); 
00247             }
00248           else
00249             {
00250               xsl = next_arg;
00251             }
00252         }
00253       ++i;
00254     }
00255   CppUnit::TextUi::TestRunner runner;
00256   if ( ns.empty() )
00257     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00258   else
00259     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00260   CppUnit::Outputter* outputter = 0;
00261   std::ostream* stream = target ? &std::cerr : &std::cout;
00262   switch ( format )
00263     {
00264     case TEXT_OUT :
00265       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00266       break;
00267     case XML_OUT :
00268       std::cout << "XML_OUT" << std::endl;
00269       outputter = new CppUnit::XmlOutputter(&runner.result(),
00270                                             ofs, "shift_jis");
00271       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00272       break;
00273     case COMPILER_OUT :
00274       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00275       break;
00276     }
00277   runner.setOutputter(outputter);
00278   runner.run();
00279   return 0; // runner.run() ? 0 : 1;
00280 }
00281 #endif // MAIN
00282 #endif // DataInOutPort_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Aug 27 2015 14:16:37