AsyncInvokerTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log$
00014  *
00015  */
00016 
00017 #ifndef AsyncInvoker_cpp
00018 #define AsyncInvoker_cpp
00019 
00020 #include <iostream>
00021 #include <string>
00022 #include <functional>
00023 
00024 #include <cppunit/ui/text/TestRunner.h>
00025 #include <cppunit/TextOutputter.h>
00026 #include <cppunit/extensions/TestFactoryRegistry.h>
00027 #include <cppunit/extensions/HelperMacros.h>
00028 #include <cppunit/TestAssert.h>
00029 
00030 #include <coil/Async.h>
00031 #include <coil/Time.h>
00032 
00033 class A
00034 {
00035 public:
00036   A()
00037     : m_hoge(false), m_munya(false), m_addone(false)
00038   {}
00039 
00040   void hoge()
00041   {
00042     for (int i(0); i < 4; ++i)
00043       {
00044         std::cout << "," << std::flush;
00045         coil::usleep(500000);
00046       }
00047     m_hoge = true;
00048   }
00049 
00050   bool hoge_invoked() { return m_hoge; }
00051 
00052   void munya(const char* msg)
00053   {
00054     for (int i(0); i < 4; ++i)
00055       {
00056         std::cout << "," << std::flush;
00057         coil::usleep(500000);
00058       }
00059     m_munya = true;
00060   }
00061 
00062   bool munya_invoked() { return m_munya; }
00063 
00064   int add_one(int val)
00065   {
00066     for (int i(0); i < 4; ++i)
00067       {
00068         std::cout << "," << std::flush;
00069         coil::usleep(500000);
00070       }
00071     m_addone = true;
00072     return val + 1;
00073   }
00074 
00075   bool add_one_invoked() { return m_addone; }
00076 
00077 private:
00078   bool m_hoge;
00079   bool m_munya;
00080   bool m_addone;
00081 };
00082 
00083 class add_one_functor
00084 {
00085   int m_val, m_ret;
00086 public:
00087   add_one_functor(int val) : m_val(val), m_ret(0)
00088   {
00089   }
00090   void operator()(A* obj)
00091   {
00092     m_ret = obj->add_one(m_val);
00093   }
00094   int get_ret()
00095   {
00096     return m_ret;
00097   }
00098 private:
00099   add_one_functor(const add_one_functor& x);
00100 };
00101 
00102 
00107 namespace AsyncInvoker
00108 {
00109   class AsyncInvokerTests
00110    : public CppUnit::TestFixture
00111   {
00112     CPPUNIT_TEST_SUITE(AsyncInvokerTests);
00113     CPPUNIT_TEST(test_memfun);
00114     CPPUNIT_TEST(test_memfun_oneway);
00115     CPPUNIT_TEST(test_bind2nd);
00116     CPPUNIT_TEST(test_bind2nd_oneway);
00117     CPPUNIT_TEST(test_myfunctor);
00118     CPPUNIT_TEST(test_myfunctor_oneway);
00119     CPPUNIT_TEST_SUITE_END();
00120   
00121   private:
00122   
00123   public:
00124   
00128     AsyncInvokerTests()
00129     {
00130     }
00131     
00135     ~AsyncInvokerTests()
00136     {
00137     }
00138   
00142     virtual void setUp()
00143     {
00144     }
00145     
00149     virtual void tearDown()
00150     { 
00151     }
00152   
00153     /* test case */
00154     void test_memfun()
00155     {
00156       A a;
00157       coil::Async* invoker(coil::AsyncInvoker(&a, std::mem_fun(&A::hoge)));
00158       
00159       invoker->invoke();
00160       CPPUNIT_ASSERT(a.hoge_invoked() == false);
00161       invoker->wait();
00162       CPPUNIT_ASSERT(a.hoge_invoked() == true);
00163     }
00164 
00165     void test_memfun_oneway()
00166     {
00167       {
00168         A a;
00169         coil::AsyncInvoker(&a, std::mem_fun(&A::hoge), true)->invoke();
00170         
00171         CPPUNIT_ASSERT(a.hoge_invoked() == false);
00172         coil::sleep(3);
00173         CPPUNIT_ASSERT(a.hoge_invoked() == true);
00174       }
00175 
00176       {
00177         A a;
00178         coil::AsyncInvoker(&a, std::mem_fun(&A::hoge), true)->invoke();
00179         
00180         CPPUNIT_ASSERT(a.hoge_invoked() == false);
00181         coil::sleep(3);
00182         CPPUNIT_ASSERT(a.hoge_invoked() == true);
00183       }
00184     }
00185 
00186     void test_bind2nd()
00187     {
00188       A a;
00189       coil::Async*
00190         invoker(coil::AsyncInvoker(&a,
00191                                    std::bind2nd(std::mem_fun(&A::munya),
00192                                                 "daradara")));
00193       invoker->invoke();
00194       CPPUNIT_ASSERT(a.munya_invoked() == false);
00195       invoker->wait();
00196       CPPUNIT_ASSERT(a.munya_invoked() == true);
00197     }
00198 
00199     void test_bind2nd_oneway()
00200     {
00201       A a;
00202       coil::AsyncInvoker(&a,
00203                          std::bind2nd(std::mem_fun(&A::munya),
00204                                       "daradara"),
00205                          true)->invoke();
00206       
00207       CPPUNIT_ASSERT(a.munya_invoked() == false);
00208       coil::sleep(3);
00209       CPPUNIT_ASSERT(a.munya_invoked() == true);
00210     }
00211 
00212     void test_myfunctor()
00213     {
00214       const int val(100);
00215       A a;
00216       add_one_functor aof(100);
00217       coil::Async* invoker(coil::AsyncInvoker(&a, &aof));
00218 
00219       invoker->invoke();
00220       CPPUNIT_ASSERT(a.add_one_invoked() == false);
00221       invoker->wait();
00222       CPPUNIT_ASSERT(a.add_one_invoked() == true);
00223       CPPUNIT_ASSERT(aof.get_ret() == (val + 1));
00224     }
00225 
00226     void test_myfunctor_oneway()
00227     {
00228       const int val(100);
00229       A a;
00230       add_one_functor aof(100);
00231       coil::AsyncInvoker(&a, &aof)->invoke();
00232 
00233       CPPUNIT_ASSERT(a.add_one_invoked() == false);
00234       coil::sleep(3);
00235       CPPUNIT_ASSERT(a.add_one_invoked() == true);
00236       CPPUNIT_ASSERT(aof.get_ret() == (val + 1));
00237     }
00238   };
00239 }; // namespace AsyncInvoker
00240 
00241 /*
00242  * Register test suite
00243  */
00244 CPPUNIT_TEST_SUITE_REGISTRATION(AsyncInvoker::AsyncInvokerTests);
00245 
00246 #ifdef LOCAL_MAIN
00247 int main(int argc, char* argv[])
00248 {
00249     CppUnit::TextUi::TestRunner runner;
00250     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00251     CppUnit::Outputter* outputter = 
00252       new CppUnit::TextOutputter(&runner.result(), std::cout);
00253     runner.setOutputter(outputter);
00254     bool retcode = runner.run();
00255     return !retcode;
00256 }
00257 #endif // MAIN
00258 #endif // AsyncInvoker_cpp


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