InPortTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00021 /*
00022  * $Log: InPortTests.cpp,v $
00023  * Revision 1.2  2008/03/13 13:12:25  arafune
00024  * Added some new tests.
00025  *
00026  * Revision 1.1  2007/12/20 07:50:19  arafune
00027  * *** empty log message ***
00028  *
00029  * Revision 1.2  2007/01/12 14:51:20  n-ando
00030  * Callback functions' namespace were changed.
00031  *
00032  * Revision 1.1  2006/11/27 08:32:39  n-ando
00033  * TestSuites are devided into each directory.
00034  *
00035  *
00036  */
00037 
00038 #ifndef InPort_cpp
00039 #define InPort_cpp
00040 
00041 #include <cppunit/ui/text/TestRunner.h>
00042 #include <cppunit/TextOutputter.h>
00043 #include <cppunit/extensions/TestFactoryRegistry.h>
00044 #include <cppunit/extensions/HelperMacros.h>
00045 #include <cppunit/TestAssert.h>
00046 
00047 #include <rtm/idl/BasicDataTypeSkel.h>
00048 #include <rtm/idl/DataPortSkel.h>
00049 #include <rtm/InPortConsumer.h>
00050 #include <rtm/InPortProvider.h>
00051 #include <rtm/InPort.h>
00052 #include <rtm/InPortBase.h>
00053 #include <rtm/CorbaConsumer.h>
00054 #include <rtm/OutPortConsumer.h>
00055 #include <rtm/CdrBufferBase.h>
00056 
00057 #include <string>
00058 #include <vector>
00059 #include <coil/Guard.h>
00060 #include <coil/Mutex.h>
00061 #include <rtm/idl/RTCSkel.h>
00062 #include <rtm/CORBA_SeqUtil.h>
00063 #include <rtm/NVUtil.h>
00064 #include <assert.h>
00065 #include <coil/UUID.h>
00066 #include <memory>
00067 #include <rtm/RTC.h>
00068 
00069 #include <coil/Logger.h>
00070 #include <rtm/SystemLogger.h>
00071 #include <rtm/Manager.h>
00072 #include <rtm/PortCallback.h>
00073 #include <rtm/InPortPushConnector.h>
00074 #include <rtm/ConnectorBase.h>
00075 #include <rtm/OutPortConnector.h>
00076 
00077 #define WTIMEOUT_USEC 1000000
00078 #define USEC_PER_SEC 1000000
00079 
00084 namespace InPort
00085 {
00090   template <class DataType>
00091   class InPortMock
00092     : public RTC::InPort<DataType>
00093   {
00094   public:
00099       InPortMock(const char* name, DataType& value,
00100            int bufsize=64, 
00101            bool read_block = false, bool write_block = false,
00102            int read_timeout = 0, int write_timeout = 0)
00103         : RTC::InPort<DataType>(name , value)
00104       {
00105       }
00110       virtual ~InPortMock()
00111       {
00112       }
00117       RTC::CdrBufferBase* get_m_thebuffer()
00118       {
00119           return RTC::InPort<DataType>::m_thebuffer;
00120       }
00125       coil::vstring get_m_providerTypes()
00126       {
00127           return RTC::InPort<DataType>::m_providerTypes;
00128       }
00133       coil::vstring get_m_consumerTypes()
00134       {
00135           return RTC::InPort<DataType>::m_consumerTypes;
00136       }
00141       RTC::InPortConnector*
00142       createConnector_public(RTC::ConnectorProfile& cprof, 
00143                             coil::Properties& prop,
00144                             RTC::InPortProvider* provider)
00145       {
00146         // return RTC::InPort<DataType>::createConnector(cprof, prop, provider);
00147          return RTC::InPortBase::createConnector(cprof, prop, provider);
00148       }
00153       RTC::InPortBase::ConnectorList get_m_connectors()
00154       {
00155           return RTC::InPort<DataType>::m_connectors;
00156       }
00161       coil::Properties get_m_properties()
00162       {
00163           return RTC::InPort<DataType>::m_properties;
00164       }
00165   };
00171   class Logger
00172   {
00173   public:
00174     void log(const std::string& msg)
00175     {
00176       m_log.push_back(msg);
00177     }
00178 
00179     int countLog(const std::string& msg)
00180     {
00181       int count = 0;
00182       for (int i = 0; i < (int) m_log.size(); ++i)
00183         {
00184           if (m_log[i] == msg) ++count;
00185         }
00186       return count;
00187     }
00188                 
00189     void clearLog(void)
00190     {
00191         m_log.clear();
00192     }
00193   private:
00194     std::vector<std::string> m_log;
00195   };
00201   class InPortCorbaCdrProviderMock
00202     : public RTC::InPortProvider,
00203       public virtual POA_OpenRTM::InPortCdr,
00204       public virtual PortableServer::RefCountServantBase
00205   {
00206 
00207   public:
00208     InPortCorbaCdrProviderMock(void)
00209     {
00210       m_logger = NULL;
00211       // PortProfile setting
00212       setInterfaceType("corba_cdr");
00213     
00214       // ConnectorProfile setting
00215       m_objref = this->_this();
00216       
00217       // set InPort's reference
00218       std::vector<std::string> args(coil::split("-ORBendPoint giop:tcp:2809", " "));
00219       // TAO's ORB_init needs argv[0] as command name.
00220       args.insert(args.begin(), "manager");
00221       char** argv = coil::toArgv(args);
00222       int argc(args.size());
00223       
00224       // ORB initialization
00225       CORBA::ORB_ptr orb = CORBA::ORB_init(argc, argv);
00226       
00227       CORBA_SeqUtil::
00228 	push_back(m_properties,
00229                   NVUtil::newNV("dataport.corba_cdr.inport_ior",
00230                                 orb->object_to_string(m_objref.in())));
00231       CORBA_SeqUtil::
00232 	push_back(m_properties,
00233                   NVUtil::newNV("dataport.corba_cdr.inport_ref",
00234                                 m_objref));
00235     }
00236     virtual ~InPortCorbaCdrProviderMock(void)
00237     {
00238       PortableServer::ObjectId_var oid;
00239       oid = _default_POA()->servant_to_id(this);
00240       _default_POA()->deactivate_object(oid);
00241     }
00246     void setBuffer(RTC::BufferBase<cdrMemoryStream>* buffer)
00247     {
00248       if (m_logger != NULL)
00249         {
00250           m_logger->log("InPortCorbaCdrProviderMock::setBuffer");
00251         }
00252     }
00253 
00254     void setListener(RTC::ConnectorInfo& info,
00255                      RTC::ConnectorListeners* listeners)
00256     {
00257       // m_profile = info;
00258       // m_listeners = listeners;
00259     }
00260     void setConnector(RTC::InPortConnector* connector)
00261     {
00262       // m_connector = connector;
00263     }
00268     ::OpenRTM::PortStatus put(const ::OpenRTM::CdrData& data)
00269       throw (CORBA::SystemException)
00270     {
00271       return ::OpenRTM::PORT_OK;
00272     }
00277     void init(coil::Properties& prop)
00278     {
00279       if (m_logger != NULL)
00280         {
00281           m_logger->log("InPortCorbaCdrProviderMock::init");
00282         }
00283     }
00288     RTC::InPortConsumer::ReturnCode put(const cdrMemoryStream& data)
00289     {
00290       return RTC::InPortConsumer::PORT_OK;
00291     }
00296     void publishInterfaceProfile(SDOPackage::NVList& properties)
00297     {
00298       return;
00299     }
00300 
00305     bool subscribeInterface(const SDOPackage::NVList& properties)
00306     {
00307       return true;;
00308     }
00309   
00314     void unsubscribeInterface(const SDOPackage::NVList& properties)
00315     {
00316     }
00317     
00318   
00323 /*
00324       bool publishInterface(SDOPackage::NVList& prop)
00325       {
00326           return true;
00327       }
00328 */
00333     void setLogger(Logger* logger)
00334     {
00335       m_logger = logger;
00336     }
00337   private:
00338     Logger* m_logger;
00339     ::OpenRTM::InPortCdr_var m_objref;
00340     
00341   };
00342   class OutPortCorbaCdrConsumerMock
00343     : public RTC::OutPortConsumer,
00344       public RTC::CorbaConsumer< ::OpenRTM::OutPortCdr >
00345   {
00346 
00347   public:
00348       OutPortCorbaCdrConsumerMock(void)
00349        {
00350           m_logger = NULL;
00351        }
00352       virtual ~OutPortCorbaCdrConsumerMock(void)
00353       {
00354       }
00359       void setBuffer(RTC::BufferBase<cdrMemoryStream>* buffer)
00360       {
00361           if (m_logger != NULL)
00362           {
00363               m_logger->log("OutPortCorbaCdrConsumerMock::setBuffer");
00364           }
00365       }
00366     void setListener(RTC::ConnectorInfo& info,
00367                      RTC::ConnectorListeners* listeners)
00368     {
00369       // m_profile = info;
00370       // m_listeners = listeners;
00371     }
00372     void setConnector(RTC::OutPortConnector* connector)
00373     {
00374       // m_connector = connector;
00375     }
00376 
00381       ::OpenRTM::PortStatus put(const ::OpenRTM::CdrData& data)
00382          throw (CORBA::SystemException)
00383       {
00384           return ::OpenRTM::PORT_OK;
00385       }
00390       void init(coil::Properties& prop)
00391       {
00392           if (m_logger != NULL)
00393           {
00394               m_logger->log("OutPortCorbaCdrConsumerMock::init");
00395           }
00396       }
00401       RTC::OutPortConsumer::ReturnCode put(const cdrMemoryStream& data)
00402       {
00403           return PORT_OK;
00404       }
00409       void publishInterfaceProfile(SDOPackage::NVList& properties)
00410       {
00411           return;
00412       }
00413 
00418       bool subscribeInterface(const SDOPackage::NVList& properties)
00419       {
00420     
00421           return true;;
00422       }
00423   
00428       void unsubscribeInterface(const SDOPackage::NVList& properties)
00429       {
00430       }
00435       virtual ReturnCode get(cdrMemoryStream& data)
00436       {
00437           return PORT_OK;
00438       }
00439   
00440   
00445       void setLogger(Logger* logger)
00446       {
00447           m_logger = logger;
00448       }
00449   private:
00450     Logger* m_logger;
00451 
00452   };
00458   template <class DataType>
00459   class RingBufferMock
00460     : public RTC::BufferBase<DataType>
00461   {
00462   public:
00463     BUFFERSTATUS_ENUM
00464       RingBufferMock(long int length = 8)
00465       {
00466           m_logger = NULL;
00467           logger.log("RingBufferMock::Constructor");
00468           m_read_return_value = BUFFER_OK;
00469           m_readable_return_value = 0;
00470 
00471       }
00472       virtual ~RingBufferMock(void)
00473       {
00474       }
00475   
00476     
00481       void set_read_return_value(::RTC::BufferStatus::Enum value)
00482       {
00483           m_read_return_value = value;
00484       }
00489       virtual void init(const coil::Properties& prop)
00490       {
00491       }
00496       virtual size_t length(void) const
00497       {
00498           return 0;
00499       }
00504       virtual ReturnCode length(size_t n)
00505       {
00506           return ::RTC::BufferStatus::BUFFER_OK; //BUFFER_OK;
00507       }
00512       virtual ReturnCode reset()
00513       {
00514           return ::RTC::BufferStatus::BUFFER_OK; //BUFFER_OK;
00515       }
00520       virtual DataType* wptr(long int n = 0)
00521       {
00522           return &m_data;
00523       }
00528       virtual ReturnCode advanceWptr(long int n = 1)
00529       {
00530           return ::RTC::BufferStatus::BUFFER_OK; //BUFFER_OK;
00531       }
00536       virtual ReturnCode put(const DataType& value)
00537       {
00538           return ::RTC::BufferStatus::BUFFER_OK; //BUFFER_OK;
00539       }
00544       virtual ReturnCode write(const DataType& value,
00545                                long int sec = -1, long int nsec = -1)
00546       {
00547           return ::RTC::BufferStatus::BUFFER_OK; //BUFFER_OK;
00548       }
00553       virtual size_t writable() const
00554       {
00555           return 0;
00556       }
00561       virtual bool full(void) const
00562       {
00563           return true;
00564       }
00569       virtual DataType* rptr(long int n = 0)
00570       {
00571           return &m_data;
00572       }
00577       virtual ReturnCode advanceRptr(long int n = 1)
00578       {
00579           return ::RTC::BufferStatus::BUFFER_OK; //BUFFER_OK;
00580       }
00585       virtual ReturnCode get(DataType& value)
00586       {
00587           return ::RTC::BufferStatus::BUFFER_OK; //BUFFER_OK;
00588       }
00593       virtual DataType&  get()
00594       {
00595           return m_data;
00596       }
00601       virtual ReturnCode read(DataType& value,
00602                               long int sec = -1, long int nsec = -1)
00603       {
00604           if (m_logger != NULL)
00605           {
00606               m_logger->log("RingBufferMock::read");
00607           }
00608           logger.log("RingBufferMock::read");
00609           return m_read_return_value; //BUFFER_OK;
00610       }
00615       virtual size_t readable() const
00616       {
00617           return m_readable_return_value;
00618       }
00623       void set_readable_return_value(size_t value)
00624       {
00625           m_readable_return_value = value;
00626       }
00631       virtual bool empty(void) const
00632       {
00633           return true;
00634       }
00639       void setLogger(Logger* logger)
00640       {
00641           m_logger = logger;
00642       }
00643 
00644       static Logger logger;
00645   private:
00646       DataType m_data;
00647       std::vector<DataType> m_buffer;
00648       Logger* m_logger;
00649       ::RTC::BufferStatus::Enum m_read_return_value;
00650       size_t m_readable_return_value;
00651   };
00652   template <class DataType>
00653   Logger RingBufferMock<DataType>::logger;
00654   typedef RingBufferMock<cdrMemoryStream> CdrRingBufferMock;
00655 };
00656 namespace RTC
00657 {
00661   ::InPort::Logger RTC_logger;
00662   ConnectorBase::ReturnCode InPortPushConnector_read_return_value;
00663   int InPortPushConnector_read;
00673   InPortPushConnector::InPortPushConnector(ConnectorInfo profile, 
00674                                            InPortProvider* provider,
00675                                            ConnectorListeners& listeners,
00676                                            CdrBufferBase* buffer)
00677     : InPortConnector(profile, buffer),
00678       m_listeners(listeners)
00679   {
00680 
00681       InPortPushConnector_read = 0;
00682       InPortPushConnector_read_return_value = PORT_OK;
00683       if(profile.properties["InPortBaseTests"]=="bad_alloc")
00684       {
00685           throw std::bad_alloc();
00686       }
00687 
00688   }
00693   InPortPushConnector::~InPortPushConnector()
00694   {
00695   }
00700   ConnectorBase::ReturnCode InPortPushConnector::disconnect()
00701   {
00702       return PORT_OK;
00703   }
00708   ConnectorBase::ReturnCode
00709   InPortPushConnector::read(cdrMemoryStream& data)
00710   {
00711       RTC_logger.log("InPortPushConnector::read");
00712       RTC::TimedLong td;
00713       td.data = 777;;
00714       td >>= data;
00715       return InPortPushConnector_read_return_value;
00716 
00717      
00718   }
00723   CdrBufferBase* InPortPushConnector::createBuffer(ConnectorInfo& profile)
00724   {
00725       return new ::InPort::CdrRingBufferMock();
00726   }
00734   Manager* Manager::manager = NULL;
00735   coil::Mutex Manager::mutex;
00740   Manager::Manager()
00741     : m_initProc(NULL),
00742       m_logStreamBuf(), rtclog(&m_logStreamBuf),
00743       m_runner(NULL), m_terminator(NULL)
00744   {
00745   }
00750   Manager& Manager::instance()
00751   {
00752     // DCL for singleton
00753     if (!manager)
00754       {
00755         Guard guard(mutex);
00756         if (!manager)
00757           {
00758             manager = new Manager();
00759             manager->initORB();
00760           }
00761       }
00762     return *manager;
00763   }
00768   CORBA::ORB_ptr Manager::getORB()
00769   {
00770     return m_pORB;
00771   }
00776   bool Manager::initORB()
00777   {
00778     // Initialize ORB
00779     try
00780       {
00781         std::vector<std::string> args(coil::split(NULL, " "));
00782         // TAO's ORB_init needs argv[0] as command name.
00783         args.insert(args.begin(), "manager");
00784         char** argv = coil::toArgv(args);
00785         int argc(args.size());
00786         
00787         // ORB initialization
00788         m_pORB = CORBA::ORB_init(argc, argv);
00789         // Get the RootPOA
00790         CORBA::Object_var obj = m_pORB->resolve_initial_references("RootPOA");
00791         PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(obj);
00792         CORBA::PolicyList policies;
00793         policies.length(2);
00794         policies[(CORBA::ULong)0] = rootPOA->create_lifespan_policy(PortableServer::PERSISTENT);
00795         policies[(CORBA::ULong)1] = rootPOA->create_id_assignment_policy(PortableServer::USER_ID);
00796         PortableServer::POAManager_var rootManager = rootPOA->the_POAManager();
00797         m_pPOA = rootPOA->create_POA("persistent", rootManager, policies);
00798         if (CORBA::is_nil(m_pPOA))
00799           {
00800             return false;
00801           }
00802         // Get the POAManager
00803         m_pPOAManager = m_pPOA->the_POAManager();
00804       }
00805     catch (...)
00806       {
00807         RTC_ERROR(("Exception: Caught unknown exception in initORB()." ));
00808         return false;
00809       }
00810     return true;
00811   }
00812   
00820   LogStreamBuf m_logStreamBuf;
00821   const char* Logger::m_levelString[] =
00822     {
00823       " SILENT: ",
00824       " FATAL: ",
00825       " ERROR: ",
00826       " WARNING: ",
00827       " INFO: ",
00828       " DEBUG: ",
00829       " TRACE: ",
00830       " VERBOSE: ",
00831       " PARANOID: "
00832     };
00833 
00834   Logger::Logger(const char* name)
00835     : ::coil::LogStream(&m_logStreamBuf, 0, 8, 0)
00836   {
00837   }
00838 
00839   Logger::Logger(LogStreamBuf* streambuf)
00840     : ::coil::LogStream(&m_logStreamBuf, 0, 8, 0)
00841   {
00842   }
00843 
00844   Logger::~Logger(void)
00845   {
00846   }
00847 
00855   bool Logger::setLevel(const char* level)
00856   {
00857     return true; 
00858   }
00859 
00867   void Logger::setDateFormat(const char* format)
00868   {
00869   }
00870 
00878   void Logger::setName(const char* name)
00879   {
00880   }
00881 
00889   void Logger::header(int level)
00890   {
00891   }
00892 
00900   std::string Logger::getDate(void)
00901   {
00902     const int maxsize = 256;
00903     char buf[maxsize];
00904 
00905     return std::string(buf);
00906   }
00907 
00915   int Logger::strToLevel(const char* level)
00916   {
00917       return 0;
00918   }
00919 
00920 #if 1
00921  /*
00922   *
00923   *
00924   *
00925   *
00926   *
00927   */
00935   InPortProvider::InPortProvider()
00936   {
00937   }
00938   
00946   InPortProvider::~InPortProvider()
00947   {
00948   }
00949   
00957   void InPortProvider::publishInterfaceProfile(SDOPackage::NVList& prop)
00958   {
00959   }
00960   
00968   bool InPortProvider::publishInterface(SDOPackage::NVList& prop)
00969   {
00970     return true;
00971   }
00972 
00973   //----------------------------------------------------------------------
00974   // protected functions
00975   
00983   void InPortProvider::setInterfaceType(const char* interface_type)
00984   {
00985   }
00986   
00994   void InPortProvider::setDataFlowType(const char* dataflow_type)
00995   {
00996   }
00997   
01005   void InPortProvider::setSubscriptionType(const char* subs_type)
01006   {
01007   }
01008 #endif 
01009 }; // namespace RTC
01010 namespace InPort
01011 {
01016   template <class DataType>
01017   class OnReadMock
01018     : public RTC::OnRead<DataType>
01019   {
01020   public:
01025     OnReadMock(void)
01026     {
01027         m_logger = NULL;
01028     }
01033     virtual void operator()()
01034     {
01035       if (m_logger != NULL)
01036       {
01037          m_logger->log("OnReadMock::operator");
01038       }
01039       
01040     }
01041     DataType m_value;
01046     void setLogger(Logger* logger)
01047     {
01048         m_logger = logger;
01049     }
01050   private:
01051     Logger* m_logger;
01052   };
01057   template <class DataType>
01058   struct OnReadConvertMock
01059     : public RTC::OnReadConvert<DataType>
01060   {
01065     OnReadConvertMock(void)
01066     {
01067         m_logger = NULL;
01068     }
01073     virtual DataType operator()(const DataType& value)
01074     {
01075       if (m_logger != NULL)
01076       {
01077          m_logger->log("OnReadConvertMock::operator");
01078       }
01079       return value;
01080     }
01085     void setLogger(Logger* logger)
01086     {
01087         m_logger = logger;
01088     }
01089   private:
01090     Logger* m_logger;
01091   };
01092 
01093 /*
01094   template <class DataType>
01095   class FullBuffer
01096     : public RTC::NullBuffer<DataType>
01097   {
01098   public:
01099     FullBuffer(long int length) {};
01100     virtual bool isFull() const
01101     {
01102       return true;
01103     }
01104   };
01105 */
01106   template <class DataType>
01107   class OnWriteMock
01108     : public RTC::OnWrite<DataType>
01109   {
01110   public:
01111     virtual void operator()(const DataType& value)
01112     {
01113       m_value = value;
01114     }
01115     DataType m_value;
01116   };
01117 
01118   /*
01119   template <class DataType>
01120   class OnOverflowMock
01121     : public RTC::OnOverflow<DataType>
01122   {
01123   public:
01124     virtual void operator()(const DataType& value)
01125     {
01126       m_value = value;
01127     }
01128     DataType m_value;
01129   };
01130   */
01131   /*
01132   class OnWriteConvertMock
01133     : public RTC::OnWriteConvert<double>
01134   {
01135   public:
01136     OnWriteConvertMock(double amplitude)
01137       : m_amplitude(amplitude) {}
01138     virtual double operator()(const double& value)
01139     {
01140       return m_amplitude * value;
01141     }
01142     double m_amplitude;
01143   };
01144   */
01145 
01150   class InPortTests
01151     : public CppUnit::TestFixture
01152   {
01153     CPPUNIT_TEST_SUITE(InPortTests);
01154 
01155     CPPUNIT_TEST(test_name);
01156     CPPUNIT_TEST(test_isNew);
01157     CPPUNIT_TEST(test_isEmpty);
01158     CPPUNIT_TEST(test_read);
01159 //    CPPUNIT_TEST(test_setOnWrite);     //The callback is not used. 
01160 //    CPPUNIT_TEST(test_setOnUnderflow); //The callback is not used. 
01161 
01162 
01163 /*
01164     CPPUNIT_TEST(test_write_and_read);
01165     CPPUNIT_TEST(test_write_OnWrite);
01166     CPPUNIT_TEST(test_write_OnWrite_full);
01167     CPPUNIT_TEST(test_write_OnOverflow);
01168     CPPUNIT_TEST(test_write_OnOverflow_not_full);
01169     CPPUNIT_TEST(test_write_OnWriteConvert);
01170     CPPUNIT_TEST(test_write_timeout);
01171 */
01172     CPPUNIT_TEST_SUITE_END();
01173                 
01174   private:
01175     CORBA::ORB_ptr m_pORB;
01176     PortableServer::POA_ptr m_pPOA;
01177     Logger* m_logger;
01178                 
01179 
01180   public:
01181         
01185     InPortTests()
01186     {
01187       int argc(0);
01188       char** argv(NULL);
01189       m_pORB = CORBA::ORB_init(argc, argv);
01190       m_pPOA = PortableServer::POA::_narrow(
01191                     m_pORB->resolve_initial_references("RootPOA"));
01192       m_pPOA->the_POAManager()->activate();
01193       m_logger = NULL;
01194     }
01195                 
01199     ~InPortTests()
01200     {
01201     }
01202     
01206     virtual void setUp()
01207     {
01208         //既に "corba_cdr" で登録されている場合は削除する。
01209         if( RTC::InPortProviderFactory::instance().hasFactory("corba_cdr") )
01210         {
01211             RTC::InPortProviderFactory::instance().removeFactory("corba_cdr");
01212         }
01213         //"corba_cdr" に InPortCorbaCdrProviderMock を登録する。
01214         RTC::InPortProviderFactory::instance().
01215         addFactory("corba_cdr",
01216                    ::coil::Creator< ::RTC::InPortProvider, 
01217                                     InPortCorbaCdrProviderMock>,
01218                    ::coil::Destructor< ::RTC::InPortProvider, 
01219                                        InPortCorbaCdrProviderMock>);
01220 
01221         //既に "corba_cdr" で登録されている場合は削除する。
01222         if( RTC::OutPortConsumerFactory::instance().hasFactory("corba_cdr") )
01223         {
01224             RTC::OutPortConsumerFactory::instance().removeFactory("corba_cdr");
01225         }
01226         //"corba_cdr" に InPortCorbaCdrProviderMock を登録する。
01227         RTC::OutPortConsumerFactory::instance().
01228         addFactory("corba_cdr",
01229                    ::coil::Creator< ::RTC::OutPortConsumer, 
01230                                     OutPortCorbaCdrConsumerMock>,
01231                    ::coil::Destructor< ::RTC::OutPortConsumer, 
01232                                        OutPortCorbaCdrConsumerMock>);
01233 
01234         //既に "ring_buffer" で登録されている場合は削除する。
01235         if( RTC::CdrBufferFactory::instance().hasFactory("ring_buffer") )
01236         {
01237             RTC::CdrBufferFactory::instance().removeFactory("ring_buffer");
01238         }
01239         //"ring_buffer" に CdrRingBufferMock を登録する。
01240         RTC::CdrBufferFactory::instance().
01241         addFactory("ring_buffer",
01242                    coil::Creator<RTC::CdrBufferBase, CdrRingBufferMock>,
01243                    coil::Destructor<RTC::CdrBufferBase, CdrRingBufferMock>);
01244 
01245     }
01246                 
01250     virtual void tearDown()
01251     {
01252     }
01257     void test_name(void)
01258     {
01259         RTC::TimedLong tl;
01260         RTC::InPort<RTC::TimedLong> inport("test_name0",tl);
01261         CPPUNIT_ASSERT_EQUAL(std::string("test_name0"), 
01262                              std::string(inport.name()));
01263 
01264         m_pPOA->deactivate_object(*m_pPOA->servant_to_id(&inport));
01265     }
01266 
01271     void test_isNew(void)
01272     {
01273         RTC::TimedLong tl;
01274         InPortMock<RTC::TimedLong> inport("test_name0",tl);
01275         coil::Properties prop_;
01276         inport.init(prop_);
01277 
01278         //no connectors
01279         CPPUNIT_ASSERT_EQUAL(false, inport.isNew());
01280 
01281         RTC::ConnectorProfile prof;
01282         coil::Properties prop(inport.properties());
01283         RTC::InPortProvider* provider = new InPortCorbaCdrProviderMock();
01284         RTC::InPortConnector* connector 
01285               = inport.createConnector_public(prof, prop, provider);
01286         CPPUNIT_ASSERT(0!= connector);
01287         //no readable data
01288         CPPUNIT_ASSERT_EQUAL(false, inport.isNew());
01289 
01290         /* Dynamic_cast is done for the test.. */
01291         /* Please do not do usual.             */
01292         CdrRingBufferMock* pbuffer 
01293                  = dynamic_cast<CdrRingBufferMock*>(inport.get_m_thebuffer());
01294 
01295         CPPUNIT_ASSERT(0!= pbuffer);
01296         //no readable data
01297         CPPUNIT_ASSERT_EQUAL(false, inport.isNew());
01298         pbuffer->set_readable_return_value((size_t)5);
01299         //readable data
01300         CPPUNIT_ASSERT_EQUAL(true, inport.isNew());
01301 
01302 
01303         m_pPOA->deactivate_object(*m_pPOA->servant_to_id(&inport));
01304         delete provider;
01305     }
01310     void test_isEmpty(void)
01311     {
01312         RTC::TimedLong tl;
01313         InPortMock<RTC::TimedLong> inport("test_name0",tl);
01314         coil::Properties prop_;
01315         inport.init(prop_);
01316 
01317         //no connectors
01318         CPPUNIT_ASSERT_EQUAL(true, inport.isEmpty());
01319 
01320         RTC::ConnectorProfile prof;
01321         coil::Properties prop(inport.properties());
01322         RTC::InPortProvider* provider = new InPortCorbaCdrProviderMock();
01323         RTC::InPortConnector* connector 
01324               = inport.createConnector_public(prof, prop, provider);
01325         CPPUNIT_ASSERT(0!= connector);
01326 
01327         //no readable data
01328         CPPUNIT_ASSERT_EQUAL(true, inport.isEmpty());
01329 
01330         /* Dynamic_cast is done for the test.. */
01331         /* Please do not do usual.             */
01332         CdrRingBufferMock* pbuffer 
01333                  = dynamic_cast<CdrRingBufferMock*>(inport.get_m_thebuffer());
01334 
01335         CPPUNIT_ASSERT(0!= pbuffer);
01336         //no readable data
01337         CPPUNIT_ASSERT_EQUAL(true, inport.isEmpty());
01338         pbuffer->set_readable_return_value((size_t)5);
01339 
01340         //readable data
01341         CPPUNIT_ASSERT_EQUAL(false, inport.isEmpty());
01342 
01343 
01344         m_pPOA->deactivate_object(*m_pPOA->servant_to_id(&inport));
01345         delete provider;
01346     }
01351     void test_read(void)
01352     {
01353         RTC::TimedLong tl;
01354         tl.data = 123;
01355         InPortMock<RTC::TimedLong> inport("test_read0",tl);
01356         coil::Properties prop_;
01357         inport.init(prop_);
01358 
01359         OnReadMock<RTC::TimedLong> onRead;
01360         Logger logger;
01361         onRead.setLogger(&logger);
01362         inport.setOnRead(&onRead);
01363 
01364         bool ret;
01365         CPPUNIT_ASSERT_EQUAL(0,logger.countLog("OnReadMock::operator"));
01366         ret = inport.read();
01367         //no connectors
01368         CPPUNIT_ASSERT_EQUAL(false, ret);
01369         CPPUNIT_ASSERT_EQUAL((CORBA::Long)123, tl.data);
01370         CPPUNIT_ASSERT_EQUAL(1,logger.countLog("OnReadMock::operator"));
01371 
01372         RTC::ConnectorProfile prof;
01373         coil::Properties prop(inport.properties());
01374         RTC::InPortProvider* provider = new InPortCorbaCdrProviderMock();
01375         RTC::InPortConnector* connector 
01376               = inport.createConnector_public(prof, prop, provider);
01377         CPPUNIT_ASSERT(0!= connector);
01378         //
01379         //
01380         //
01381         OnReadConvertMock<RTC::TimedLong> onReadConvert;
01382         onReadConvert.setLogger(&logger);
01383 
01384         RTC::InPortPushConnector_read_return_value 
01385                                            = RTC::ConnectorBase::PORT_OK;
01386         RTC::TimedLong tl_;
01387         inport.operator>>(tl_);
01388         CPPUNIT_ASSERT_EQUAL((CORBA::Long)777, tl_.data);
01389 
01390         int logcount;
01391         CPPUNIT_ASSERT_EQUAL(0,logger.countLog("OnReadConvertMock::operator"));
01392         logcount = ::RTC::RTC_logger.countLog("InPortPushConnector::read");
01393         ret = inport.read();
01394         //data read succeeded
01395         CPPUNIT_ASSERT_EQUAL(true, ret);
01396         CPPUNIT_ASSERT_EQUAL((CORBA::Long)777, tl.data);
01397         CPPUNIT_ASSERT_EQUAL(logcount+1,
01398                      ::RTC::RTC_logger.countLog("InPortPushConnector::read"));
01399         CPPUNIT_ASSERT_EQUAL(0,logger.countLog("OnReadConvertMock::operator"));
01400 
01401         inport.setOnReadConvert(&onReadConvert);
01402         CPPUNIT_ASSERT_EQUAL(0,logger.countLog("OnReadConvertMock::operator"));
01403         logcount = ::RTC::RTC_logger.countLog("InPortPushConnector::read");
01404         ret = inport.read();
01405         //data read succeeded
01406         CPPUNIT_ASSERT_EQUAL(true, ret);
01407         CPPUNIT_ASSERT_EQUAL((CORBA::Long)777, tl.data);
01408         CPPUNIT_ASSERT_EQUAL(logcount+1,
01409                      ::RTC::RTC_logger.countLog("InPortPushConnector::read"));
01410         CPPUNIT_ASSERT_EQUAL(1,logger.countLog("OnReadConvertMock::operator"));
01411 
01412         //
01413         //
01414         //
01415         RTC::InPortPushConnector_read_return_value 
01416                                            = RTC::ConnectorBase::PORT_OK;
01417         inport.setOnRead(&onRead);
01418 
01419         logcount = ::RTC::RTC_logger.countLog("InPortPushConnector::read");
01420         ret = inport.read();
01421         //data read succeeded
01422         CPPUNIT_ASSERT_EQUAL(true, ret);
01423         CPPUNIT_ASSERT_EQUAL((CORBA::Long)777, tl.data);
01424         CPPUNIT_ASSERT_EQUAL(logcount+1,
01425                      ::RTC::RTC_logger.countLog("InPortPushConnector::read"));
01426         //
01427         //
01428         //
01429         RTC::InPortPushConnector_read_return_value 
01430                                            = RTC::ConnectorBase::BUFFER_EMPTY;
01431         logcount = ::RTC::RTC_logger.countLog("InPortPushConnector::read");
01432         ret = inport.read();
01433         //data read failed, because InPortPushConnectorMock::read() return BUFFER_EMPTY.
01434         CPPUNIT_ASSERT_EQUAL(false, ret);
01435         CPPUNIT_ASSERT_EQUAL((CORBA::Long)777, tl.data);
01436         CPPUNIT_ASSERT_EQUAL(logcount+1,
01437                      ::RTC::RTC_logger.countLog("InPortPushConnector::read"));
01438 
01439         //
01440         //
01441         //
01442         RTC::InPortPushConnector_read_return_value 
01443                                            = RTC::ConnectorBase::BUFFER_TIMEOUT;
01444         logcount = ::RTC::RTC_logger.countLog("InPortPushConnector::read");
01445         ret = inport.read();
01446         //data read failed, because InPortPushConnectorMock::read() return BUFFER_TIMEOUT
01447         CPPUNIT_ASSERT_EQUAL(false, ret);
01448         CPPUNIT_ASSERT_EQUAL((CORBA::Long)777, tl.data);
01449         CPPUNIT_ASSERT_EQUAL(logcount+1,
01450                      ::RTC::RTC_logger.countLog("InPortPushConnector::read"));
01451 
01452         //
01453         //
01454         //
01455         RTC::InPortPushConnector_read_return_value 
01456                                            = RTC::ConnectorBase::UNKNOWN_ERROR;
01457         logcount = ::RTC::RTC_logger.countLog("InPortPushConnector::read");
01458         ret = inport.read();
01459         //data read failed, because InPortPushConnectorMock::read() return UNKNOWN_ERROR
01460         CPPUNIT_ASSERT_EQUAL(false, ret);
01461         CPPUNIT_ASSERT_EQUAL((CORBA::Long)777, tl.data);
01462         CPPUNIT_ASSERT_EQUAL(logcount+1,
01463                      ::RTC::RTC_logger.countLog("InPortPushConnector::read"));
01464 
01465         m_pPOA->deactivate_object(*m_pPOA->servant_to_id(&inport));
01466         delete provider;
01467     }
01472     void test_setOnWrite(void)
01473     {
01474     }
01479     void test_setOnUnderflow()
01480     {
01481     }
01482 
01488 /*
01489     void test_write_and_read()
01490     {
01491       double bindValue;
01492       std::auto_ptr<RTC::InPort<double> > inPort(
01493                                                  new RTC::InPort<double>("InPort", bindValue, 8));
01494                         
01495       for (int i = 0; i < 100; ++i)
01496         {
01497           double writeValue = i * 3.14159265;
01498           CPPUNIT_ASSERT(inPort->write(writeValue));
01499                                 
01500           // write()で書き込んだ値が、read()で正しく読み出されるか?
01501           double readValue = inPort->read();
01502           CPPUNIT_ASSERT_EQUAL(writeValue, readValue);
01503         }
01504     }
01505 */
01506                 
01512 /*
01513     void test_write_OnWrite()
01514     {
01515       double bindValue;
01516       std::auto_ptr<RTC::InPort<double> > inPort(
01517                                                  new RTC::InPort<double>("InPort", bindValue, 8));
01518                         
01519       OnWriteMock<double> onWrite;
01520       onWrite.m_value = 0;
01521       inPort->setOnWrite(&onWrite);
01522                         
01523       // write()メソッドは成功するか?
01524       double writeValue = 3.14159265;
01525       CPPUNIT_ASSERT(inPort->write(writeValue));
01526                         
01527       // あらかじめ設定されたOnWriteコールバックが正しく呼び出されたか?
01528       CPPUNIT_ASSERT_EQUAL(writeValue, onWrite.m_value);
01529     }
01530 */
01531 
01537 /* 
01538    void test_write_OnWrite_full()
01539     {
01540       double bindValue;
01541       std::auto_ptr<RTC::InPort<double, FullBuffer> > inPort(
01542                                                              new RTC::InPort<double, FullBuffer>("InPort", bindValue, 8));
01543                         
01544       OnWriteMock<double> onWrite;
01545       onWrite.m_value = 0;
01546       inPort->setOnWrite(&onWrite);
01547                         
01548       // バッファフルによりwrite()メソッドは意図どおり失敗するか?
01549       double writeValue = 3.14159265;
01550       CPPUNIT_ASSERT(! inPort->write(writeValue));
01551                         
01552       // あらかじめ設定されたOnWriteコールバックが正しく呼び出されたか?
01553       CPPUNIT_ASSERT_EQUAL(writeValue, onWrite.m_value);
01554     }
01555 */
01556 
01562 /* 
01563    void test_write_OnOverflow()
01564     {
01565       // 常にフル状態であるバッファを用いてInPortオブジェクトを生成する
01566       double bindValue;
01567       std::auto_ptr<RTC::InPort<double, FullBuffer> > inPort(
01568                                                              new RTC::InPort<double, FullBuffer>("InPort", bindValue, 8));
01569                         
01570       OnOverflowMock<double> onOverflow;
01571       onOverflow.m_value = 0;
01572       inPort->setOnOverflow(&onOverflow);
01573 
01574       // バッファフルによりwrite()メソッドは意図どおり失敗するか?
01575       double writeValue = 3.14159265;
01576       CPPUNIT_ASSERT(! inPort->write(writeValue));
01577                         
01578       // OutPortに割り当てされたバッファがフルの場合に、あらかじめ設定されたOnOverflowコールバックが正しく呼び出されたか?
01579       CPPUNIT_ASSERT_EQUAL(writeValue, onOverflow.m_value);
01580     }
01581 */
01582 
01588 /*
01589     void test_write_OnOverflow_not_full()
01590     {
01591       double bindValue;
01592       std::auto_ptr<RTC::InPort<double> > inPort(
01593                                                  new RTC::InPort<double>("InPort", bindValue, 8));
01594 
01595       OnOverflowMock<double> onOverflow;
01596       onOverflow.m_value = 0;
01597       inPort->setOnOverflow(&onOverflow);
01598 
01599       // write()メソッドは成功するか?
01600       double writeValue = 3.14159265;
01601       CPPUNIT_ASSERT(inPort->write(writeValue));
01602                         
01603       // バッファフルでない場合、OnOverflowコールバックが意図どおり未呼出のままか?
01604       CPPUNIT_ASSERT_EQUAL((double) 0, onOverflow.m_value);
01605     }
01606 */
01607                 
01613 /*
01614     void test_write_OnWriteConvert()
01615     {
01616       double bindValue;
01617       std::auto_ptr<RTC::InPort<double> > inPort(
01618                                                  new RTC::InPort<double>("InPort", bindValue, 8));
01619                         
01620       double amplitude = 1.41421356;
01621       OnWriteConvertMock onWriteConvert(amplitude);
01622       inPort->setOnWriteConvert(&onWriteConvert);
01623                         
01624       for (int i = 0; i < 100; ++i)
01625         {
01626           double writeValue = i * 3.14159265;
01627           CPPUNIT_ASSERT(inPort->write(writeValue));
01628                                 
01629           double readValue = inPort->read();
01630                                 
01631           // write()で書き込んだ値が、read()で正しく読み出されるか?
01632           double expectedValue = amplitude * writeValue;
01633           CPPUNIT_ASSERT_EQUAL(expectedValue, readValue);
01634         }
01635     }
01636 */
01637 
01644 /*
01645     void test_write_timeout()
01646     {
01647       // 常にフル状態であるバッファを用いてInPortオブジェクトを生成する
01648       bool readBlock = false;
01649       bool writeBlock = true; // ブロッキングモードON
01650       int readTimeout = 0;
01651       int writeTimeout = WTIMEOUT_USEC; // タイムアウト値を指定する
01652 
01653       double bindValue;
01654       std::auto_ptr<RTC::InPort<double, FullBuffer> > inPort(
01655                                                              new RTC::InPort<double, FullBuffer>(
01656                                                                                                  "InPort", bindValue, 8, readBlock, writeBlock, readTimeout, writeTimeout));
01657                         
01658       timeval tm_pre;
01659       gettimeofday(&tm_pre, 0);
01660                         
01661       for (int i = 0; i < 10; ++i) {
01662                                 
01663         double writeValue = i * 3.14159265;
01664                                 
01665         // OutPortに割り当てられたバッファがフルの場合に、write()メソッドが意図どおり失敗するか?
01666         CPPUNIT_ASSERT(! inPort->write(writeValue));
01667                                 
01668         // OutPortに割り当てされたバッファがフルの場合に、指定した時間どおりにwrite()メソッドがタイムアウトしているか?
01669         timeval tm_cur;
01670         gettimeofday(&tm_cur, 0);
01671                                 
01672         timeval tm_diff;
01673         timersub(&tm_cur, &tm_pre, &tm_diff);
01674                                 
01675         double interval = (double) tm_diff.tv_sec
01676           + (double) tm_diff.tv_usec / USEC_PER_SEC;
01677                                         
01678         tm_pre = tm_cur;
01679                                 
01680         CPPUNIT_ASSERT_DOUBLES_EQUAL(
01681                                      (double) WTIMEOUT_USEC / USEC_PER_SEC, interval,
01682                                      0.1 * WTIMEOUT_USEC/USEC_PER_SEC);
01683       }
01684     }
01685 */
01686                 
01687   };
01688 }; // namespace InPort
01689 
01690 /*
01691  * Register test suite
01692  */
01693 CPPUNIT_TEST_SUITE_REGISTRATION(InPort::InPortTests);
01694 
01695 #ifdef LOCAL_MAIN
01696 int main(int argc, char* argv[])
01697 {
01698 
01699   FORMAT format = TEXT_OUT;
01700   int target = 0;
01701   std::string xsl;
01702   std::string ns;
01703   std::string fname;
01704   std::ofstream ofs;
01705 
01706   int i(1);
01707   while (i < argc)
01708     {
01709       std::string arg(argv[i]);
01710       std::string next_arg;
01711       if (i + 1 < argc) next_arg = argv[i + 1];
01712       else              next_arg = "";
01713 
01714       if (arg == "--text") { format = TEXT_OUT; break; }
01715       if (arg == "--xml")
01716         {
01717           if (next_arg == "")
01718             {
01719               fname = argv[0];
01720               fname += ".xml";
01721             }
01722           else
01723             {
01724               fname = next_arg;
01725             }
01726           format = XML_OUT;
01727           ofs.open(fname.c_str());
01728         }
01729       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
01730       if ( arg == "--cerr"      ) { target = 1; break; }
01731       if ( arg == "--xsl"       )
01732         {
01733           if (next_arg == "") xsl = "default.xsl"; 
01734           else                xsl = next_arg;
01735         }
01736       if ( arg == "--namespace" )
01737         {
01738           if (next_arg == "")
01739             {
01740               std::cerr << "no namespace specified" << std::endl;
01741               exit(1); 
01742             }
01743           else
01744             {
01745               xsl = next_arg;
01746             }
01747         }
01748       ++i;
01749     }
01750   CppUnit::TextUi::TestRunner runner;
01751   if ( ns.empty() )
01752     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
01753   else
01754     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
01755   CppUnit::Outputter* outputter = 0;
01756   std::ostream* stream = target ? &std::cerr : &std::cout;
01757   switch ( format )
01758     {
01759     case TEXT_OUT :
01760       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
01761       break;
01762     case XML_OUT :
01763       std::cout << "XML_OUT" << std::endl;
01764       outputter = new CppUnit::XmlOutputter(&runner.result(),
01765                                             ofs, "shift_jis");
01766       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
01767       break;
01768     case COMPILER_OUT :
01769       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
01770       break;
01771     }
01772   runner.setOutputter(outputter);
01773   runner.run();
01774   return 0; // runner.run() ? 0 : 1;
01775 }
01776 #endif // MAIN
01777 #endif // InPort_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:15