OutPortTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log: OutPortTests.cpp,v $
00014  * Revision 1.2  2008/02/19 08:13:36  arafune
00015  * Some tests were added and orthogonalized.
00016  *
00017  * Revision 1.1  2007/12/20 07:50:18  arafune
00018  * *** empty log message ***
00019  *
00020  * Revision 1.2  2006/12/02 18:52:54  n-ando
00021  * Some tests were added.
00022  *
00023  * Revision 1.1  2006/11/29 04:26:28  n-ando
00024  * CppUnit tests for OutPort.
00025  *
00026  *
00027  */
00028 
00029 #ifndef OutPort_cpp
00030 #define OutPort_cpp
00031 
00032 #include <cppunit/ui/text/TestRunner.h>
00033 #include <cppunit/TextOutputter.h>
00034 #include <cppunit/extensions/TestFactoryRegistry.h>
00035 #include <cppunit/extensions/HelperMacros.h>
00036 #include <cppunit/TestAssert.h>
00037 
00038 #include <rtm/idl/BasicDataTypeSkel.h>
00039 #include <rtm/idl/DataPortSkel.h>
00040 #include <rtm/DataPortStatus.h>
00041 #include <rtm/OutPort.h>
00042 #include <rtm/OutPortBase.h>
00043 #include <rtm/OutPortPushConnector.h>
00044 #include <rtm/InPort.h>
00045 #include <rtm/CorbaConsumer.h>
00046 
00047 #define WTIMEOUT_USEC 1000000
00048 #define USEC_PER_SEC 1000000
00049 
00054 namespace OutPort
00055 {
00060   template <class DataType>
00061   class OutPortMock
00062     : public RTC::OutPort<DataType>
00063   {
00064   public:
00069       OutPortMock(const char* name, DataType& value)
00070        : RTC::OutPort<DataType>(name, value)
00071       {
00072       }
00077       virtual ~OutPortMock()
00078       {
00079       }
00084       RTC::OutPortConnector*
00085       createConnector_public(RTC::ConnectorProfile& cprof, 
00086                             coil::Properties& prop,
00087                             RTC::OutPortProvider* provider)
00088       {
00089          return RTC::OutPort<DataType>::createConnector(cprof, prop, provider);
00090       }
00095       RTC::OutPortConnector*
00096       createConnector_public(RTC::ConnectorProfile& cprof, 
00097                             coil::Properties& prop,
00098                             RTC::InPortConsumer* consumer)
00099       {
00100          return RTC::OutPort<DataType>::createConnector(cprof, prop, consumer);
00101       }
00102   };
00108   class Logger
00109   {
00110   public:
00111     void log(const std::string& msg)
00112     {
00113       m_log.push_back(msg);
00114     }
00115 
00116     int countLog(const std::string& msg)
00117     {
00118       int count = 0;
00119 
00120       for (int i = 0; i < (int) m_log.size(); ++i)
00121         {
00122           if (m_log[i] == msg) ++count;
00123         }
00124       return count;
00125     }
00126                 
00127     void clearLog(void)
00128     {
00129         m_log.clear();
00130     }
00131   private:
00132     std::vector<std::string> m_log;
00133   };
00139   class InPortCorbaCdrConsumerMock
00140     : public RTC::InPortConsumer,
00141       public RTC::CorbaConsumer< ::OpenRTM::InPortCdr >
00142   {
00143 
00144   public:
00145       InPortCorbaCdrConsumerMock(void)
00146        {
00147           m_logger = NULL;
00148        }
00149       virtual ~InPortCorbaCdrConsumerMock(void)
00150       {
00151       }
00156       void init(coil::Properties& prop)
00157       {
00158           if (m_logger != NULL)
00159           {
00160               m_logger->log("InPortCorbaCdrConsumerMock::init");
00161           }
00162       }
00167       RTC::InPortConsumer::ReturnCode put(const cdrMemoryStream& data)
00168       {
00169           return PORT_OK;
00170       }
00175       void publishInterfaceProfile(SDOPackage::NVList& properties)
00176       {
00177           return;
00178       }
00179 
00184       bool subscribeInterface(const SDOPackage::NVList& properties)
00185       {
00186     
00187           return true;;
00188       }
00189   
00194       void unsubscribeInterface(const SDOPackage::NVList& properties)
00195       {
00196       }
00197   
00198   
00203       void setLogger(Logger* logger)
00204       {
00205           m_logger = logger;
00206       }
00207   private:
00208     Logger* m_logger;
00209 
00210   };
00216   class OutPortCorbaCdrProviderMock
00217     : public RTC::OutPortProvider,
00218       public virtual ::POA_OpenRTM::OutPortCdr,
00219       public virtual PortableServer::RefCountServantBase
00220   {
00221 
00222   public:
00223       OutPortCorbaCdrProviderMock(void)
00224        {
00225           m_logger = NULL;
00226        }
00227       virtual ~OutPortCorbaCdrProviderMock(void)
00228       {
00229       }
00234       void init(coil::Properties& prop)
00235       {
00236           if (m_logger != NULL)
00237           {
00238               m_logger->log("OutPortCorbaCdrProviderMock::init");
00239           }
00240       }
00245       virtual ::OpenRTM::PortStatus get(::OpenRTM::CdrData_out data)
00246       {
00247           return ::OpenRTM::PORT_OK;
00248       }
00253       RTC::InPortConsumer::ReturnCode put(const cdrMemoryStream& data)
00254       {
00255           return PORT_OK;
00256       }
00261       void publishInterfaceProfile(SDOPackage::NVList& properties)
00262       {
00263           return;
00264       }
00265 
00270       bool subscribeInterface(const SDOPackage::NVList& properties)
00271       {
00272     
00273           return true;;
00274       }
00275   
00280       void unsubscribeInterface(const SDOPackage::NVList& properties)
00281       {
00282       }
00283   
00284   
00289       void setLogger(Logger* logger)
00290       {
00291           m_logger = logger;
00292       }
00293   private:
00294     Logger* m_logger;
00295 
00296   };
00297 /****
00298   template <class DataType>
00299   class FullBuffer
00300     : public RTC::NullBuffer<DataType>
00301   {
00302   public:
00303     FullBuffer(long int length) {};
00304     virtual bool isFull() const
00305     {
00306       return true;
00307     }
00308   };
00309 
00310   template <class DataType>
00311   class OnOverflowMock
00312     : public RTC::OnOverflow<DataType>
00313   {
00314   public:
00315     virtual void operator()(const DataType& value)
00316     {
00317       m_value = value;
00318     }
00319     DataType m_value;
00320   };
00321 ****/
00322 
00328   template <class DataType>
00329   class RingBufferMock
00330     : public RTC::BufferBase<DataType>
00331   {
00332   public:
00333     BUFFERSTATUS_ENUM
00334       RingBufferMock(long int length = 8)
00335       {
00336           m_logger = NULL;
00337           logger.log("RingBufferMock::Constructor");
00338           m_read_return_value = BUFFER_OK;
00339           m_readable_return_value = 0;
00340 
00341       }
00342       virtual ~RingBufferMock(void)
00343       {
00344       }
00345   
00346     
00351       void set_read_return_value(::RTC::BufferStatus::Enum value)
00352       {
00353           m_read_return_value = value;
00354       }
00359       virtual void init(const coil::Properties& prop)
00360       {
00361       }
00366       virtual size_t length(void) const
00367       {
00368           return 0;
00369       }
00374       virtual ReturnCode length(size_t n)
00375       {
00376           return ::RTC::BufferStatus::BUFFER_OK; //BUFFER_OK;
00377       }
00382       virtual ReturnCode reset()
00383       {
00384           return ::RTC::BufferStatus::BUFFER_OK; //BUFFER_OK;
00385       }
00390       virtual DataType* wptr(long int n = 0)
00391       {
00392           return &m_data;
00393       }
00398       virtual ReturnCode advanceWptr(long int n = 1)
00399       {
00400           return ::RTC::BufferStatus::BUFFER_OK; //BUFFER_OK;
00401       }
00406       virtual ReturnCode put(const DataType& value)
00407       {
00408           return ::RTC::BufferStatus::BUFFER_OK; //BUFFER_OK;
00409       }
00414       virtual ReturnCode write(const DataType& value,
00415                                long int sec = -1, long int nsec = -1)
00416       {
00417           return ::RTC::BufferStatus::BUFFER_OK; //BUFFER_OK;
00418       }
00423       virtual size_t writable() const
00424       {
00425           return 0;
00426       }
00431       virtual bool full(void) const
00432       {
00433           return true;
00434       }
00439       virtual DataType* rptr(long int n = 0)
00440       {
00441           return &m_data;
00442       }
00447       virtual ReturnCode advanceRptr(long int n = 1)
00448       {
00449           return ::RTC::BufferStatus::BUFFER_OK; //BUFFER_OK;
00450       }
00455       virtual ReturnCode get(DataType& value)
00456       {
00457           return ::RTC::BufferStatus::BUFFER_OK; //BUFFER_OK;
00458       }
00463       virtual DataType&  get()
00464       {
00465           return m_data;
00466       }
00471       virtual ReturnCode read(DataType& value,
00472                               long int sec = -1, long int nsec = -1)
00473       {
00474           if (m_logger != NULL)
00475           {
00476               m_logger->log("RingBufferMock::read");
00477           }
00478           logger.log("RingBufferMock::read");
00479           return m_read_return_value; //BUFFER_OK;
00480       }
00485       virtual size_t readable() const
00486       {
00487           return m_readable_return_value;
00488       }
00493       void set_readable_return_value(size_t value)
00494       {
00495           m_readable_return_value = value;
00496       }
00501       virtual bool empty(void) const
00502       {
00503           return true;
00504       }
00509       void setLogger(Logger* logger)
00510       {
00511           m_logger = logger;
00512       }
00513 
00514       static Logger logger;
00515   private:
00516       DataType m_data;
00517       std::vector<DataType> m_buffer;
00518       Logger* m_logger;
00519       ::RTC::BufferStatus::Enum m_read_return_value;
00520       size_t m_readable_return_value;
00521   };
00522   template <class DataType>
00523   Logger RingBufferMock<DataType>::logger;
00524   typedef RingBufferMock<cdrMemoryStream> CdrRingBufferMock;
00530   class PublisherFlushMock
00531     : public RTC::PublisherBase
00532   {
00533   public:
00534       PublisherFlushMock()
00535       {
00536           m_logger = NULL;
00537       }
00538       ~PublisherFlushMock()
00539       {
00540       }
00545       RTC::PublisherBase::ReturnCode write(const cdrMemoryStream& data,
00546                                            unsigned long sec,
00547                                            unsigned long usec)
00548       {
00549           cdrMemoryStream cdr(data);
00550           CORBA::ULong inlen = cdr.bufSize();
00551 
00552           CORBA::Octet oct[8];
00553           cdr.get_octet_array (oct, (int)inlen);
00554           long lval(0);
00555           for(int ic(0);ic<(int)inlen;++ic)
00556           {
00557               lval = lval+(int)(oct[ic]<<(ic*8));
00558           }
00559           std::stringstream ss;
00560           ss << lval;
00561           if (m_logger != NULL)
00562           {
00563               m_logger->log("PublisherFlushMock::write");
00564               m_logger->log(ss.str());
00565           }
00566           logger.log("PublisherFlushMock::write");
00567           logger.log(ss.str());
00568           return PORT_OK;
00569       }
00574       bool isActive()
00575       {
00576           return true;
00577       }
00578 
00583       RTC::PublisherBase::ReturnCode activate()
00584       {
00585           if (m_logger != NULL)
00586           {
00587               m_logger->log("PublisherFlushMock::activate");
00588           }
00589           logger.log("PublisherFlushMock::activate");
00590           return PORT_OK;
00591       }
00592 
00597       RTC::PublisherBase::ReturnCode deactivate()
00598       {
00599           if (m_logger != NULL)
00600           {
00601               m_logger->log("PublisherFlushMock::deactivate");
00602           }
00603           logger.log("PublisherFlushMock::deactivate");
00604           return PORT_OK;
00605       }
00610       RTC::PublisherBase::ReturnCode init(coil::Properties& prop)
00611       {
00612           return PORT_OK;
00613       }
00618       RTC::PublisherBase::ReturnCode setConsumer(RTC::InPortConsumer* consumer)
00619       {
00620           if (m_logger != NULL)
00621           {
00622               m_logger->log("PublisherFlushMock::setConsumer");
00623           }
00624           logger.log("PublisherFlushMock::setConsumer");
00625           return PORT_OK;
00626       }
00627 
00632       RTC::PublisherBase::ReturnCode setBuffer(RTC::CdrBufferBase* buffer)
00633       {
00634           if (m_logger != NULL)
00635           {
00636               m_logger->log("PublisherFlushMock::setBuffer");
00637               if(buffer == NULL)
00638               {
00639                   m_logger->log("buffer NG");
00640               }
00641               else
00642               {
00643                   m_logger->log("buffer OK");
00644               }
00645           }
00646           logger.log("PublisherFlushMock::setBuffer");
00647           if(buffer == NULL)
00648           {
00649               logger.log("buffer NG");
00650           }
00651           else
00652           {
00653               logger.log("buffer OK");
00654           }
00655           return PORT_OK;
00656       }
00657 
00662       ::RTC::DataPortStatus::Enum setListener(RTC::ConnectorInfo& info,
00663                                   RTC::ConnectorListeners* listeners)
00664       {
00665           if (m_logger != NULL)
00666           {
00667               m_logger->log("PublisherFlushMock::setListener");
00668               if (listeners == 0)
00669               {
00670                   m_logger->log("listeners NG");
00671               }
00672               else
00673               {
00674                   m_logger->log("listeners OK");
00675               }
00676           }
00677           logger.log("PublisherFlushMock::setListener");
00678           if (listeners == 0)
00679           {
00680               logger.log("listeners NG");
00681           }
00682           else
00683           {
00684               logger.log("listeners OK");
00685           }
00686           return PORT_OK;
00687       }
00688 
00693       void setLogger(Logger* logger)
00694       {
00695           m_logger = logger;
00696       }
00697     static Logger logger;
00698   private:
00699     Logger* m_logger;
00700   };
00701   Logger PublisherFlushMock::logger;
00702         
00703 };
00711 namespace RTC
00712 {
00720   OutPort::Logger RTC_logger;
00721   ConnectorBase::ReturnCode OutPortPushConnector_write_return_value;
00734   OutPortPushConnector::OutPortPushConnector(ConnectorInfo info, 
00735                                              InPortConsumer* consumer,
00736                                              ConnectorListeners& listeners,
00737                                              CdrBufferBase* buffer)
00738     : OutPortConnector(info),
00739       m_consumer(consumer), m_publisher(0), m_listeners(listeners), m_buffer(buffer)
00740   {
00741 
00742   }
00750   OutPortPushConnector::~OutPortPushConnector()
00751   {
00752   }
00760   ConnectorBase::ReturnCode OutPortPushConnector::disconnect()
00761   {
00762     return PORT_OK;
00763   }
00768  CdrBufferBase* OutPortPushConnector::getBuffer()
00769  {
00770    return m_buffer;
00771  }
00779   ConnectorBase::ReturnCode
00780   OutPortPushConnector::write(const cdrMemoryStream& data)
00781   {
00782       RTC_logger.log("OutPortPushConnector::write");
00783       RTC::TimedDouble td; 
00784       cdrMemoryStream cdr(data);
00785       td <<= cdr;
00786       std::ostringstream os;
00787       os<<td.data;
00788       RTC_logger.log(os.str());
00789 //      ::CORBA::Any any;
00790 //      any <<= cdr;
00791 //      std::cout<<any.type()->kind()<<std::endl;
00792 
00793       return OutPortPushConnector_write_return_value;
00794   }
00799     void OutPortPushConnector::activate()
00800     {
00801     }
00806     void OutPortPushConnector::deactivate()
00807     {
00808     }
00813   CdrBufferBase* OutPortPushConnector::createBuffer(ConnectorInfo& info)
00814   {
00815       return new ::OutPort::CdrRingBufferMock();
00816   }
00821   PublisherBase* OutPortPushConnector::createPublisher(ConnectorInfo& info)
00822   {
00823       return new ::OutPort::PublisherFlushMock();
00824   }
00825 
00826 
00827 
00835   Manager* Manager::manager = NULL;
00836   coil::Mutex Manager::mutex;
00841   Manager::Manager()
00842     : m_initProc(NULL),
00843       m_logStreamBuf(), rtclog(&m_logStreamBuf),
00844       m_runner(NULL), m_terminator(NULL)
00845   {
00846   }
00851   Manager& Manager::instance()
00852   {
00853     // DCL for singleton
00854     if (!manager)
00855       {
00856         Guard guard(mutex);
00857         if (!manager)
00858           {
00859             manager = new Manager();
00860             manager->initORB();
00861           }
00862       }
00863     return *manager;
00864   }
00869   CORBA::ORB_ptr Manager::getORB()
00870   {
00871     return m_pORB;
00872   }
00877   bool Manager::initORB()
00878   {
00879     // Initialize ORB
00880     try
00881       {
00882         std::vector<std::string> args(coil::split(NULL, " "));
00883         // TAO's ORB_init needs argv[0] as command name.
00884         args.insert(args.begin(), "manager");
00885         char** argv = coil::toArgv(args);
00886         int argc(args.size());
00887         
00888         // ORB initialization
00889         m_pORB = CORBA::ORB_init(argc, argv);
00890         // Get the RootPOA
00891         CORBA::Object_var obj = m_pORB->resolve_initial_references("RootPOA");
00892         PortableServer::POA_var rootPOA = PortableServer::POA::_narrow(obj);
00893         CORBA::PolicyList policies;
00894         policies.length(2);
00895         policies[(CORBA::ULong)0] = rootPOA->create_lifespan_policy(PortableServer::PERSISTENT);
00896         policies[(CORBA::ULong)1] = rootPOA->create_id_assignment_policy(PortableServer::USER_ID);
00897         PortableServer::POAManager_var rootManager = rootPOA->the_POAManager();
00898         m_pPOA = rootPOA->create_POA("persistent", rootManager, policies);
00899         if (CORBA::is_nil(m_pPOA))
00900           {
00901             return false;
00902           }
00903         // Get the POAManager
00904         m_pPOAManager = m_pPOA->the_POAManager();
00905       }
00906     catch (...)
00907       {
00908         RTC_ERROR(("Exception: Caught unknown exception in initORB()." ));
00909         return false;
00910       }
00911     return true;
00912   }
00913   
00921   LogStreamBuf m_logStreamBuf;
00922   const char* Logger::m_levelString[] =
00923     {
00924       " SILENT: ",
00925       " FATAL: ",
00926       " ERROR: ",
00927       " WARNING: ",
00928       " INFO: ",
00929       " DEBUG: ",
00930       " TRACE: ",
00931       " VERBOSE: ",
00932       " PARANOID: "
00933     };
00934 
00935   Logger::Logger(const char* name)
00936     : ::coil::LogStream(&m_logStreamBuf, 0, 8, 0)
00937   {
00938   }
00939 
00940   Logger::Logger(LogStreamBuf* streambuf)
00941     : ::coil::LogStream(&m_logStreamBuf, 0, 8, 0)
00942   {
00943   }
00944 
00945   Logger::~Logger(void)
00946   {
00947   }
00948 
00956   bool Logger::setLevel(const char* level)
00957   {
00958     return true; 
00959   }
00960 
00968   void Logger::setDateFormat(const char* format)
00969   {
00970   }
00971 
00979   void Logger::setName(const char* name)
00980   {
00981   }
00982 
00990   void Logger::header(int level)
00991   {
00992   }
00993 
01001   std::string Logger::getDate(void)
01002   {
01003     const int maxsize = 256;
01004     char buf[maxsize];
01005 
01006     return std::string(buf);
01007   }
01008 
01016   int Logger::strToLevel(const char* level)
01017   {
01018       return 0;
01019   }
01027   OutPortProvider::~OutPortProvider(void)
01028   {
01029   }
01030 
01031   void OutPortProvider::init(coil::Properties& prop)
01032   {
01033   }
01034 
01035   void OutPortProvider::setBuffer(BufferBase<cdrMemoryStream>* buffer)
01036   {
01037   }
01038 
01046   void OutPortProvider::publishInterfaceProfile(SDOPackage::NVList& prop)
01047   {
01048     NVUtil::appendStringValue(prop, "dataport.interface_type",
01049                               m_interfaceType.c_str());
01050     NVUtil::append(prop, m_properties);
01051   }
01052   
01060   bool OutPortProvider::publishInterface(SDOPackage::NVList& prop)
01061   {
01062 
01063     if (!NVUtil::isStringValue(prop,
01064                                "dataport.interface_type",
01065                                m_interfaceType.c_str()))
01066       {
01067         return false;
01068       }
01069     
01070     NVUtil::append(prop, m_properties);
01071     return true;
01072   }
01073   
01081   void OutPortProvider::setPortType(const char* port_type)
01082   {
01083     m_portType = port_type;
01084   }
01085   
01093   void OutPortProvider::setDataType(const char* data_type)
01094   {
01095     m_dataType = data_type;
01096   }
01097   
01105   void OutPortProvider::setInterfaceType(const char* interface_type)
01106   {
01107     m_interfaceType = interface_type;
01108   }
01109   
01117   void OutPortProvider::setDataFlowType(const char* dataflow_type)
01118   {
01119     m_dataflowType = dataflow_type;
01120   }
01121   
01129   void OutPortProvider::setSubscriptionType(const char* subs_type)
01130   {
01131     m_subscriptionType = subs_type;
01132   }
01133 
01134 
01135 }; // namespace RTC
01143 namespace OutPort
01144 {       
01150   template <class DataType>
01151   class OnWriteMock
01152     : public RTC::OnWrite<DataType>
01153   {
01154   public:
01159     OnWriteMock(void)
01160     {
01161         m_logger = NULL;
01162     }
01167     virtual void operator()(const DataType& value)
01168     {
01169       m_value = value;
01170       if (m_logger != NULL)
01171       {
01172          m_logger->log("OnWriteMock::operator");
01173       }
01174     }
01175     DataType m_value;
01180     void setLogger(Logger* logger)
01181     {
01182         m_logger = logger;
01183     }
01184   private:
01185     Logger* m_logger;
01186   };
01191   template <class DataType>
01192   struct OnWriteConvertMock
01193     : public RTC::OnWriteConvert<DataType>
01194   {
01195   public:
01200     OnWriteConvertMock(RTC::TimedDouble amplitude)
01201       : m_amplitude(amplitude)
01202     {
01203         m_logger = NULL;
01204     }
01209     virtual DataType operator()(const DataType& value)
01210     {
01211       DataType td;   
01212       td.data = m_amplitude.data * value.data;
01213       if (m_logger != NULL)
01214       {
01215          m_logger->log("OnWriteConvertMock::operator");
01216       }
01217       return td;
01218     }
01219     RTC::TimedDouble m_amplitude;
01224     void setLogger(Logger* logger)
01225     {
01226         m_logger = logger;
01227     }
01228   private:
01229     Logger* m_logger;
01230   };
01235   class OutPortTests
01236     : public CppUnit::TestFixture
01237   {
01238     CPPUNIT_TEST_SUITE(OutPortTests);
01239     CPPUNIT_TEST(test_write);  // include onWrite and OnWriteConver
01240 /*
01241     CPPUNIT_TEST(test_write_OnWrite);
01242     CPPUNIT_TEST(test_write_OnWriteConvert);
01243     CPPUNIT_TEST(test_write_OnWrite_full);
01244     CPPUNIT_TEST(test_write_OnOverflow);
01245     CPPUNIT_TEST(test_write_OnOverflow_not_full);
01246     CPPUNIT_TEST(test_write_timeout);
01247 */
01248     CPPUNIT_TEST_SUITE_END();
01249                 
01250   private:
01251     CORBA::ORB_ptr m_pORB;
01252     PortableServer::POA_ptr m_pPOA;
01253     Logger* m_logger;
01254                 
01255   public:
01256         
01260     OutPortTests()
01261     {
01262       int argc(0);
01263       char** argv(NULL);
01264       m_pORB = CORBA::ORB_init(argc, argv);
01265       m_pPOA = PortableServer::POA::_narrow(
01266                     m_pORB->resolve_initial_references("RootPOA"));
01267       m_pPOA->the_POAManager()->activate();
01268       m_logger = NULL;
01269 
01270     }
01271                 
01275     ~OutPortTests()
01276     {
01277     }
01278                 
01282     virtual void setUp()
01283     {
01284     }
01285                 
01289     virtual void tearDown()
01290     { 
01291     }
01292 
01297     void test_write(void)
01298     {
01299         RTC::TimedDouble td;
01300         td.data = 123;
01301         OutPortMock<RTC::TimedDouble> outport("testi_write0",td);
01302 
01303         OnWriteMock<RTC::TimedDouble> onWrite;
01304         Logger logger;
01305         onWrite.setLogger(&logger);
01306         outport.setOnWrite(&onWrite);
01307 
01308         bool ret;
01309         CPPUNIT_ASSERT_EQUAL(0,logger.countLog("OnWriteMock::operator"));
01310         ret = outport.write();
01311         //no connectors was return false
01312         CPPUNIT_ASSERT_EQUAL(false, ret);
01313         CPPUNIT_ASSERT_EQUAL(1,logger.countLog("OnWriteMock::operator"));
01314 
01315         RTC::ConnectorProfile prof;
01316         coil::Properties prop(outport.properties());
01317         RTC::InPortConsumer* consumer = new InPortCorbaCdrConsumerMock();
01318         RTC::OutPortConnector* connector 
01319               = outport.createConnector_public(prof, prop, consumer);
01320         CPPUNIT_ASSERT(0!= connector);
01321         //
01322         //
01323         //
01324         RTC::TimedDouble amplitude;
01325 //        amplitude.data = 1.41421356;
01326         amplitude.data = 1;
01327         OnWriteConvertMock<RTC::TimedDouble> onWriteConvert(amplitude);
01328         onWriteConvert.setLogger(&logger);
01329 
01330         RTC::OutPortPushConnector_write_return_value 
01331                                            = RTC::ConnectorBase::PORT_OK;
01332         int logcount;
01333         int writedatacount;
01334 
01335         CPPUNIT_ASSERT_EQUAL(0,logger.countLog("OnWriteConvertMock::operator"));
01336         logcount = ::RTC::RTC_logger.countLog("OntPortPushConnector::write");
01337         ret = outport.write();
01338         //data read succeeded
01339         CPPUNIT_ASSERT_EQUAL(true, ret);
01340         CPPUNIT_ASSERT_EQUAL(logcount+1,
01341                      ::RTC::RTC_logger.countLog("OutPortPushConnector::write"));
01342         CPPUNIT_ASSERT_EQUAL(0,logger.countLog("OnReadConvertMock::operator"));
01343 
01344         outport.setOnWriteConvert(&onWriteConvert);
01345         CPPUNIT_ASSERT_EQUAL(0,logger.countLog("OnReadConvertMock::operator"));
01346         logcount = ::RTC::RTC_logger.countLog("OutPortPushConnector::write");
01347         ret = outport.write();
01348         //data read succeeded
01349         CPPUNIT_ASSERT_EQUAL(true, ret);
01350         CPPUNIT_ASSERT_EQUAL(logcount+1,
01351                      ::RTC::RTC_logger.countLog("OutPortPushConnector::write"));
01352         CPPUNIT_ASSERT_EQUAL(1,logger.countLog("OnWriteConvertMock::operator"));
01353 
01354         //
01355         //
01356         //
01357         RTC::OutPortPushConnector_write_return_value 
01358                                            = RTC::ConnectorBase::PORT_OK;
01359         outport.setOnWrite(&onWrite);
01360 
01361         logcount = ::RTC::RTC_logger.countLog("OutPortPushConnector::write");
01362         writedatacount = ::RTC::RTC_logger.countLog("123");
01363         ret = outport.write();
01364         //data read succeeded
01365         CPPUNIT_ASSERT_EQUAL(true, ret);
01366         CPPUNIT_ASSERT_EQUAL(logcount+1,
01367                      ::RTC::RTC_logger.countLog("OutPortPushConnector::write"));
01368         CPPUNIT_ASSERT_EQUAL(writedatacount+1,
01369                       ::RTC::RTC_logger.countLog("123"));
01370         //
01371         //
01372         //
01373 /*** check OK
01374         RTC::OutPortPushConnector_write_return_value 
01375                                            = RTC::ConnectorBase::PRECONDITION_NOT_MET;
01376         logcount = ::RTC::RTC_logger.countLog("OutPortPushConnector::write");
01377         ret = outport.write();
01378         //data read succeeded
01379         CPPUNIT_ASSERT_EQUAL(false, ret);
01380         CPPUNIT_ASSERT_EQUAL(logcount+1,
01381                      ::RTC::RTC_logger.countLog("OutPortPushConnector::write"));
01382 ***/
01383 
01384         //
01385         //
01386         //
01387 /*** check OK
01388         RTC::OutPortPushConnector_write_return_value 
01389                                            = RTC::ConnectorBase::CONNECTION_LOST;
01390         logcount = ::RTC::RTC_logger.countLog("OutPortPushConnector::write");
01391         ret = outport.write();
01392         //data read succeeded
01393         CPPUNIT_ASSERT_EQUAL(false, ret);
01394         CPPUNIT_ASSERT_EQUAL(logcount+1,
01395                      ::RTC::RTC_logger.countLog("OutPortPushConnector::write"));
01396 ***/
01397 
01398         //
01399         //
01400         //
01401 /*** check OK
01402         RTC::OutPortPushConnector_write_return_value 
01403                                            = RTC::ConnectorBase::BUFFER_FULL;
01404         logcount = ::RTC::RTC_logger.countLog("OutPortPushConnector::write");
01405         ret = outport.write();
01406         //data read succeeded
01407         CPPUNIT_ASSERT_EQUAL(false, ret);
01408         CPPUNIT_ASSERT_EQUAL(logcount+1,
01409                      ::RTC::RTC_logger.countLog("OutPortPushConnector::write"));
01410 ***/
01411 
01412         //
01413         //
01414         //
01415 
01416         RTC::OutPortPushConnector_write_return_value 
01417                                            = RTC::ConnectorBase::PORT_OK;
01418 
01419         outport.setOnWrite(&onWrite);
01420 
01421         logcount = ::RTC::RTC_logger.countLog("OutPortPushConnector::write");
01422         writedatacount = ::RTC::RTC_logger.countLog("55");
01423         RTC::TimedDouble doublevalue;
01424         doublevalue.data = 55;
01425 
01426         ret = outport.write(doublevalue);
01427         //data write succeeded
01428         CPPUNIT_ASSERT_EQUAL(true, ret);
01429         CPPUNIT_ASSERT_EQUAL(logcount+1,
01430                      ::RTC::RTC_logger.countLog("OutPortPushConnector::write"));
01431         CPPUNIT_ASSERT_EQUAL(writedatacount+1,
01432                       ::RTC::RTC_logger.countLog("55"));
01433 
01434         m_pPOA->deactivate_object(*m_pPOA->servant_to_id(&outport));
01435         delete consumer;
01436     }
01437 
01443     void test_write_OnWrite()
01444     {
01445       RTC::TimedDouble bindValue;
01446       OutPortMock<RTC::TimedDouble>* outPort
01447         = new OutPortMock<RTC::TimedDouble>("OutPort", bindValue);
01448 
01449                         
01450       OnWriteMock<RTC::TimedDouble> onWrite;
01451       onWrite.m_value.data = 0;
01452       outPort->setOnWrite(&onWrite);
01453                         
01454       // write()メソッドは成功するか?
01455       RTC::TimedDouble writeValue;
01456       writeValue.data = 3.14159265;
01457       CPPUNIT_ASSERT(outPort->write(writeValue));
01458                         
01459       // あらかじめ設定されたOnWriteコールバックが正しく呼び出されたか?
01460       CPPUNIT_ASSERT_EQUAL(writeValue.data, onWrite.m_value.data);
01461 
01462       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(outPort));
01463       delete outPort;
01464     }
01465                 
01471     void test_write_OnWrite_full()
01472     {
01473       RTC::TimedDouble bindValue;
01474       RTC::OutPort<RTC::TimedDouble>* outPort
01475         = new RTC::OutPort<RTC::TimedDouble>("OutPort", bindValue);
01476                         
01477       OnWriteMock<RTC::TimedDouble> onWrite;
01478       onWrite.m_value.data = 0;
01479       outPort->setOnWrite(&onWrite);
01480                         
01481       RTC::PortService_var oportref = outPort->get_port_profile()->port_ref;
01482 
01483       RTC::TimedDouble InbindValue;
01484       RTC::InPort<RTC::TimedDouble>* inPort
01485         = new RTC::InPort<RTC::TimedDouble>("InPort", InbindValue);
01486 
01487       coil::Properties dummy;
01488       inPort->init(dummy);
01489       RTC::PortService_var iportref = inPort->get_port_profile()->port_ref;
01490 
01491       RTC::ConnectorProfile prof;
01492       prof.connector_id = "";
01493       prof.name = CORBA::string_dup("connector0");
01494       prof.ports.length(2);
01495       prof.ports[0] = oportref;
01496       prof.ports[1] = iportref;
01497 
01498       CORBA_SeqUtil::push_back(prof.properties,
01499                                NVUtil::newNV("dataport.interface_type",
01500                                              "corba_cdr"));
01501 
01502       CORBA_SeqUtil::push_back(prof.properties,
01503                                NVUtil::newNV("dataport.dataflow_type",
01504                                              "push"));
01505       CORBA_SeqUtil::push_back(prof.properties,
01506                                NVUtil::newNV("dataport.subscription_type",
01507                                              "new"));
01508       inPort->connect(prof);
01509 
01510       // バッファフルによりwrite()メソッドは意図どおり失敗するか?
01511       RTC::TimedDouble writeValue;
01512       writeValue.data = 3.14159265;
01513       for(int ic(0);ic<8;++ic)
01514         {
01515           outPort->write(writeValue);
01516         }
01517       CPPUNIT_ASSERT(! outPort->write(writeValue));
01518                         
01519       // あらかじめ設定されたOnWriteコールバックが正しく呼び出されたか?
01520       CPPUNIT_ASSERT_EQUAL(writeValue.data, onWrite.m_value.data);
01521       delete inPort;
01522       delete outPort;
01523     }
01524                 
01530     void test_write_OnOverflow()
01531     {
01532       // 常にフル状態であるバッファを用いてOutPortオブジェクトを生成する
01533       RTC::TimedDouble bindValue;
01534       RTC::OutPort<RTC::TimedDouble>* outPort
01535         = new RTC::OutPort<RTC::TimedDouble>("OutPort", bindValue);
01536                         
01537 //      OnOverflowMock<RTC::TimedDouble> onOverflow;
01538 //      onOverflow.m_value.data = 0;
01539 //      outPort->setOnOverflow(&onOverflow);
01540 
01541       RTC::PortService_var oportref = outPort->get_port_profile()->port_ref;
01542       RTC::TimedDouble InbindValue;
01543       RTC::InPort<RTC::TimedDouble>* inPort
01544         = new RTC::InPort<RTC::TimedDouble>("InPort", InbindValue);
01545 
01546       coil::Properties dummy;
01547       inPort->init(dummy);
01548       RTC::PortService_var iportref = inPort->get_port_profile()->port_ref;
01549 
01550       RTC::ConnectorProfile prof;
01551       prof.connector_id = "";
01552       prof.name = CORBA::string_dup("connector0");
01553       prof.ports.length(2);
01554       prof.ports[0] = oportref;
01555       prof.ports[1] = iportref;
01556 
01557       CORBA_SeqUtil::push_back(prof.properties,
01558                                NVUtil::newNV("dataport.interface_type",
01559                                              "corba_cdr"));
01560 
01561       CORBA_SeqUtil::push_back(prof.properties,
01562                                NVUtil::newNV("dataport.dataflow_type",
01563                                              "push"));
01564       CORBA_SeqUtil::push_back(prof.properties,
01565                                NVUtil::newNV("dataport.subscription_type",
01566                                              "new"));
01567       inPort->connect(prof);
01568 
01569       // バッファフルによりwrite()メソッドは意図どおり失敗するか?
01570       RTC::TimedDouble writeValue;
01571       writeValue.data = 3.14159265;
01572       for(int ic(0);ic<8;++ic)
01573         {
01574           outPort->write(writeValue);
01575         }
01576       CPPUNIT_ASSERT(! outPort->write(writeValue));
01577                         
01578       // OutPortに割り当てされたバッファがフルの場合に、あらかじめ設定されたOnOverflowコールバックが正しく呼び出されたか?
01579 //      CPPUNIT_ASSERT_EQUAL(writeValue.data, onOverflow.m_value.data);
01580       delete inPort;
01581       delete outPort;
01582     }
01583 
01589     void test_write_OnOverflow_not_full()
01590     {
01591       RTC::TimedDouble bindValue;
01592       RTC::OutPort<RTC::TimedDouble>* outPort
01593         = new RTC::OutPort<RTC::TimedDouble>("OutPort", bindValue);
01594 
01595 //      OnOverflowMock<RTC::TimedDouble> onOverflow;
01596 //      onOverflow.m_value.data = 0;
01597 //      outPort->setOnOverflow(&onOverflow);
01598 
01599       RTC::PortService_var oportref = outPort->get_port_profile()->port_ref;
01600 
01601       RTC::TimedDouble InbindValue;
01602       RTC::InPort<RTC::TimedDouble>* inPort
01603         = new RTC::InPort<RTC::TimedDouble>("InPort", InbindValue);
01604 
01605       coil::Properties dummy;
01606       inPort->init(dummy);
01607       RTC::PortService_var iportref = inPort->get_port_profile()->port_ref;
01608 
01609       RTC::ConnectorProfile prof;
01610       prof.connector_id = "";
01611       prof.name = CORBA::string_dup("connector0");
01612       prof.ports.length(2);
01613       prof.ports[0] = oportref;
01614       prof.ports[1] = iportref;
01615 
01616       CORBA_SeqUtil::push_back(prof.properties,
01617                                NVUtil::newNV("dataport.interface_type",
01618                                              "corba_cdr"));
01619 
01620       CORBA_SeqUtil::push_back(prof.properties,
01621                                NVUtil::newNV("dataport.dataflow_type",
01622                                              "push"));
01623       CORBA_SeqUtil::push_back(prof.properties,
01624                                NVUtil::newNV("dataport.subscription_type",
01625                                              "new"));
01626       inPort->connect(prof);
01627       // write()メソッドは成功するか?
01628       RTC::TimedDouble writeValue;
01629       writeValue.data = 3.14159265;
01630       CPPUNIT_ASSERT(outPort->write(writeValue));
01631                         
01632       // バッファフルでない場合、OnOverflowコールバックが意図どおり未呼出のままか?
01633 //      CPPUNIT_ASSERT_EQUAL((double) 0, onOverflow.m_value.data);
01634       delete inPort;
01635       delete outPort;
01636     }
01637                 
01638 
01639     void test_write_OnWriteConvert()
01640     {
01641       RTC::TimedDouble bindValue;
01642       OutPortMock<RTC::TimedDouble>* outPort
01643         = new OutPortMock<RTC::TimedDouble>("OutPort", bindValue);
01644                         
01645         RTC::ConnectorProfile prof;
01646         coil::Properties prop(outPort->properties());
01647         RTC::InPortConsumer* consumer = new InPortCorbaCdrConsumerMock();
01648         RTC::OutPortConnector* connector 
01649               = outPort->createConnector_public(prof, prop, consumer);
01650 
01651       RTC::TimedDouble amplitude;
01652       amplitude.data = 1.41421356;
01653       OnWriteConvertMock<RTC::TimedDouble> onWriteConvert(amplitude);
01654       outPort->setOnWriteConvert(&onWriteConvert);
01655                         
01656       for (int i = 0; i < 100; ++i)
01657         {
01658           RTC::TimedDouble writeValue;
01659           writeValue.data = i * 3.14159265;
01660           RTC::TimedDouble expectedValue;
01661           expectedValue.data = amplitude.data * writeValue.data;
01662 
01663           std::ostringstream os;
01664           os << expectedValue.data;
01665           int logcount;
01666           logcount = ::RTC::RTC_logger.countLog(os.str());
01667           CPPUNIT_ASSERT(outPort->write(writeValue));
01668           CPPUNIT_ASSERT_EQUAL(logcount+1,
01669                         ::RTC::RTC_logger.countLog(os.str()));
01670                                 
01671                                 
01672         }
01673       m_pPOA->deactivate_object(*m_pPOA->servant_to_id(outPort));
01674       delete consumer;
01675       delete outPort;
01676     }
01677 
01678                 
01685     void test_write_timeout()
01686     {
01687       // 常にフル状態であるバッファを用いてOutPortオブジェクトを生成する
01688       RTC::TimedDouble bindValue;
01689       RTC::OutPort<RTC::TimedDouble>* outPort
01690         = new RTC::OutPort<RTC::TimedDouble>("OutPort", bindValue);
01691                         
01692       RTC::PortService_var oportref = outPort->get_port_profile()->port_ref;
01693 
01694       RTC::TimedDouble InbindValue;
01695       RTC::InPort<RTC::TimedDouble>* inPort
01696         = new RTC::InPort<RTC::TimedDouble>("InPort", InbindValue);
01697 
01698       coil::Properties dummy;
01699       inPort->init(dummy);
01700       RTC::PortService_var iportref = inPort->get_port_profile()->port_ref;
01701 
01702       RTC::ConnectorProfile prof;
01703       prof.connector_id = "";
01704       prof.name = CORBA::string_dup("connector0");
01705       prof.ports.length(2);
01706       prof.ports[0] = oportref;
01707       prof.ports[1] = iportref;
01708 
01709       CORBA_SeqUtil::push_back(prof.properties,
01710                                NVUtil::newNV("dataport.interface_type",
01711                                              "corba_cdr"));
01712 
01713       CORBA_SeqUtil::push_back(prof.properties,
01714                                NVUtil::newNV("dataport.dataflow_type",
01715                                              "push"));
01716       CORBA_SeqUtil::push_back(prof.properties,
01717                                NVUtil::newNV("dataport.subscription_type",
01718                                              "new"));
01719       inPort->connect(prof);
01720 
01721       // OutPortオブジェクトに対して、ブロッキングモードONを指定する
01722 //      outPort->setWriteBlock(true);
01723                         
01724       // OutPortオブジェクトに対して、タイムアウト値を指定する
01725 //      outPort->setWriteTimeout(WTIMEOUT_USEC);
01726                         
01727       timeval tm_pre;
01728       gettimeofday(&tm_pre, 0);
01729                         
01730       for(int ic(0);ic<8;++ic)
01731         {
01732           RTC::TimedDouble writeValue;
01733           writeValue.data = 3.14159265;
01734           outPort->write(writeValue);
01735         }
01736       for (int i = 0; i < 10; ++i) {
01737                                 
01738         RTC::TimedDouble writeValue;
01739         writeValue.data = i * 3.14159265;
01740                                 
01741         // OutPortに割り当てられたバッファがフルの場合に、write()メソッドが意図どおり失敗するか?
01742         CPPUNIT_ASSERT(! outPort->write(writeValue));
01743                                 
01744         // OutPortに割り当てされたバッファがフルの場合に、指定した時間どおりにwrite()メソッドがタイムアウトしているか?
01745         timeval tm_cur;
01746         gettimeofday(&tm_cur, 0);
01747                                 
01748         timeval tm_diff;
01749         timersub(&tm_cur, &tm_pre, &tm_diff);
01750                                 
01751         double interval = (double) tm_diff.tv_sec
01752           + (double) tm_diff.tv_usec / USEC_PER_SEC;
01753                                         
01754         tm_pre = tm_cur;
01755                                 
01756         CPPUNIT_ASSERT_DOUBLES_EQUAL(
01757                                      (double) WTIMEOUT_USEC / USEC_PER_SEC, interval,
01758                                      0.1 * WTIMEOUT_USEC/USEC_PER_SEC);
01759       }
01760       delete inPort;
01761       delete outPort;
01762     }
01763                 
01764   };
01765 }; // namespace OutPort
01766 
01767 /*
01768  * Register test suite
01769  */
01770 CPPUNIT_TEST_SUITE_REGISTRATION(OutPort::OutPortTests);
01771 
01772 #ifdef LOCAL_MAIN
01773 int main(int argc, char* argv[])
01774 {
01775 
01776   FORMAT format = TEXT_OUT;
01777   int target = 0;
01778   std::string xsl;
01779   std::string ns;
01780   std::string fname;
01781   std::ofstream ofs;
01782 
01783   int i(1);
01784   while (i < argc)
01785     {
01786       std::string arg(argv[i]);
01787       std::string next_arg;
01788       if (i + 1 < argc) next_arg = argv[i + 1];
01789       else              next_arg = "";
01790 
01791       if (arg == "--text") { format = TEXT_OUT; break; }
01792       if (arg == "--xml")
01793         {
01794           if (next_arg == "")
01795             {
01796               fname = argv[0];
01797               fname += ".xml";
01798             }
01799           else
01800             {
01801               fname = next_arg;
01802             }
01803           format = XML_OUT;
01804           ofs.open(fname.c_str());
01805         }
01806       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
01807       if ( arg == "--cerr"      ) { target = 1; break; }
01808       if ( arg == "--xsl"       )
01809         {
01810           if (next_arg == "") xsl = "default.xsl"; 
01811           else                xsl = next_arg;
01812         }
01813       if ( arg == "--namespace" )
01814         {
01815           if (next_arg == "")
01816             {
01817               std::cerr << "no namespace specified" << std::endl;
01818               exit(1); 
01819             }
01820           else
01821             {
01822               xsl = next_arg;
01823             }
01824         }
01825       ++i;
01826     }
01827   CppUnit::TextUi::TestRunner runner;
01828   if ( ns.empty() )
01829     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
01830   else
01831     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
01832   CppUnit::Outputter* outputter = 0;
01833   std::ostream* stream = target ? &std::cerr : &std::cout;
01834   switch ( format )
01835     {
01836     case TEXT_OUT :
01837       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
01838       break;
01839     case XML_OUT :
01840       std::cout << "XML_OUT" << std::endl;
01841       outputter = new CppUnit::XmlOutputter(&runner.result(),
01842                                             ofs, "shift_jis");
01843       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
01844       break;
01845     case COMPILER_OUT :
01846       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
01847       break;
01848     }
01849   runner.setOutputter(outputter);
01850   runner.run();
01851   return 0; // runner.run() ? 0 : 1;
01852 }
01853 #endif // MAIN
01854 #endif // OutPort_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:05