AsyncInvokerTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef AsyncInvoker_cpp
18 #define AsyncInvoker_cpp
19 
20 #include <iostream>
21 #include <string>
22 #include <functional>
23 
24 #include <cppunit/ui/text/TestRunner.h>
25 #include <cppunit/TextOutputter.h>
26 #include <cppunit/extensions/TestFactoryRegistry.h>
27 #include <cppunit/extensions/HelperMacros.h>
28 #include <cppunit/TestAssert.h>
29 
30 #include <coil/Async.h>
31 #include <coil/Time.h>
32 
33 class A
34 {
35 public:
36  A()
37  : m_hoge(false), m_munya(false), m_addone(false)
38  {}
39 
40  void hoge()
41  {
42  for (int i(0); i < 4; ++i)
43  {
44  std::cout << "," << std::flush;
45  coil::usleep(500000);
46  }
47  m_hoge = true;
48  }
49 
50  bool hoge_invoked() { return m_hoge; }
51 
52  void munya(const char* msg)
53  {
54  for (int i(0); i < 4; ++i)
55  {
56  std::cout << "," << std::flush;
57  coil::usleep(500000);
58  }
59  m_munya = true;
60  }
61 
62  bool munya_invoked() { return m_munya; }
63 
64  int add_one(int val)
65  {
66  for (int i(0); i < 4; ++i)
67  {
68  std::cout << "," << std::flush;
69  coil::usleep(500000);
70  }
71  m_addone = true;
72  return val + 1;
73  }
74 
75  bool add_one_invoked() { return m_addone; }
76 
77 private:
78  bool m_hoge;
79  bool m_munya;
80  bool m_addone;
81 };
82 
84 {
85  int m_val, m_ret;
86 public:
87  add_one_functor(int val) : m_val(val), m_ret(0)
88  {
89  }
90  void operator()(A* obj)
91  {
92  m_ret = obj->add_one(m_val);
93  }
94  int get_ret()
95  {
96  return m_ret;
97  }
98 private:
100 };
101 
102 
107 namespace AsyncInvoker
108 {
110  : public CppUnit::TestFixture
111  {
112  CPPUNIT_TEST_SUITE(AsyncInvokerTests);
113  CPPUNIT_TEST(test_memfun);
114  CPPUNIT_TEST(test_memfun_oneway);
115  CPPUNIT_TEST(test_bind2nd);
116  CPPUNIT_TEST(test_bind2nd_oneway);
117  CPPUNIT_TEST(test_myfunctor);
118  CPPUNIT_TEST(test_myfunctor_oneway);
119  CPPUNIT_TEST_SUITE_END();
120 
121  private:
122 
123  public:
124 
129  {
130  }
131 
136  {
137  }
138 
142  virtual void setUp()
143  {
144  }
145 
149  virtual void tearDown()
150  {
151  }
152 
153  /* test case */
154  void test_memfun()
155  {
156  A a;
157  coil::Async* invoker(coil::AsyncInvoker(&a, std::mem_fun(&A::hoge)));
158 
159  invoker->invoke();
160  CPPUNIT_ASSERT(a.hoge_invoked() == false);
161  invoker->wait();
162  CPPUNIT_ASSERT(a.hoge_invoked() == true);
163  }
164 
166  {
167  {
168  A a;
169  coil::AsyncInvoker(&a, std::mem_fun(&A::hoge), true)->invoke();
170 
171  CPPUNIT_ASSERT(a.hoge_invoked() == false);
172  coil::sleep(3);
173  CPPUNIT_ASSERT(a.hoge_invoked() == true);
174  }
175 
176  {
177  A a;
178  coil::AsyncInvoker(&a, std::mem_fun(&A::hoge), true)->invoke();
179 
180  CPPUNIT_ASSERT(a.hoge_invoked() == false);
181  coil::sleep(3);
182  CPPUNIT_ASSERT(a.hoge_invoked() == true);
183  }
184  }
185 
187  {
188  A a;
189  coil::Async*
190  invoker(coil::AsyncInvoker(&a,
191  std::bind2nd(std::mem_fun(&A::munya),
192  "daradara")));
193  invoker->invoke();
194  CPPUNIT_ASSERT(a.munya_invoked() == false);
195  invoker->wait();
196  CPPUNIT_ASSERT(a.munya_invoked() == true);
197  }
198 
200  {
201  A a;
203  std::bind2nd(std::mem_fun(&A::munya),
204  "daradara"),
205  true)->invoke();
206 
207  CPPUNIT_ASSERT(a.munya_invoked() == false);
208  coil::sleep(3);
209  CPPUNIT_ASSERT(a.munya_invoked() == true);
210  }
211 
213  {
214  const int val(100);
215  A a;
216  add_one_functor aof(100);
217  coil::Async* invoker(coil::AsyncInvoker(&a, &aof));
218 
219  invoker->invoke();
220  CPPUNIT_ASSERT(a.add_one_invoked() == false);
221  invoker->wait();
222  CPPUNIT_ASSERT(a.add_one_invoked() == true);
223  CPPUNIT_ASSERT(aof.get_ret() == (val + 1));
224  }
225 
227  {
228  const int val(100);
229  A a;
230  add_one_functor aof(100);
231  coil::AsyncInvoker(&a, &aof)->invoke();
232 
233  CPPUNIT_ASSERT(a.add_one_invoked() == false);
234  coil::sleep(3);
235  CPPUNIT_ASSERT(a.add_one_invoked() == true);
236  CPPUNIT_ASSERT(aof.get_ret() == (val + 1));
237  }
238  };
239 }; // namespace AsyncInvoker
240 
241 /*
242  * Register test suite
243  */
245 
246 #ifdef LOCAL_MAIN
247 int main(int argc, char* argv[])
248 {
249  CppUnit::TextUi::TestRunner runner;
250  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
251  CppUnit::Outputter* outputter =
252  new CppUnit::TextOutputter(&runner.result(), std::cout);
253  runner.setOutputter(outputter);
254  bool retcode = runner.run();
255  return !retcode;
256 }
257 #endif // MAIN
258 #endif // AsyncInvoker_cpp
int main(int argc, char **argv)
bool m_hoge
void hoge()
virtual void setUp()
Test initialization.
unsigned int sleep(unsigned int seconds)
Stop a processing at specified second time.
Definition: ace/coil/Time.h:40
void munya(const char *msg)
bool munya_invoked()
void operator()(A *obj)
virtual int wait(void)
Waiting for the thread terminate.
bool add_one_invoked()
int add_one(int val)
virtual void tearDown()
Test finalization.
virtual void invoke()=0
Asynchronous invocation.
bool m_addone
CPPUNIT_TEST_SUITE_REGISTRATION(AsyncInvoker::AsyncInvokerTests)
Async class.
Definition: Async.h:41
bool m_munya
bool hoge_invoked()
int usleep(useconds_t usec)
Stop a processing at specified micro second time.
Definition: ace/coil/Time.h:51
Async_t< Object, Func > * AsyncInvoker(Object *obj, Func func, bool auto_delete=false)
Helper function for async member function summons.
Definition: Async.h:550


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:50