ConditionTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef Condition_cpp
18 #define Condition_cpp
19 
20 #include <cppunit/ui/text/TestRunner.h>
21 #include <cppunit/TextOutputter.h>
22 #include <cppunit/extensions/TestFactoryRegistry.h>
23 #include <cppunit/extensions/HelperMacros.h>
24 #include <cppunit/TestAssert.h>
25 
26 #include <coil/Condition.h>
27 #include <coil/Mutex.h>
28 #include <coil/Guard.h>
29 #include <coil/Task.h>
30 #include <coil/Async.h>
31 #include <coil/Time.h>
32 #include <coil/TimeValue.h>
33 
35 
36 class A
37 {
38 public:
40  : m_cond(cond), m_mutex(mutex) {}
41  void signal(int usec)
42  {
43  coil::usleep(usec);
44  m_mutex.lock();
45  m_cond.signal();
46  m_mutex.unlock();
47  }
50 };
51 
56 namespace Condition
57 {
58 
63  {
64  public:
67  coil::Condition<coil::Mutex> & aCondition, int anId)
68  : mutex(&aMutex), cond(&aCondition), id(anId)
69  {
70  // std::cout << "Task(" << id << ") created." << std::endl;
71  }
72 
74  {
75  };
76 
77  virtual int svc()
78  {
79  Guard guard(*mutex);
80  // mutex->lock();
81  cond->wait();
82  ConditionStatus += id;
83  // mutex->unlock();
84  return ConditionStatus;
85  }
89  static void resteStatus() { ConditionStatus = 0; }
93  static int getStatus() { return ConditionStatus; }
94  private:
95 // static int status; // <- undefined reference
98  int id;
99  static int ConditionStatus;
100  }; // class ConditionCheckTask
101 
102  int ConditionCheckTask::ConditionStatus(0);
103 
104 
106  : public CppUnit::TestFixture
107  {
108  CPPUNIT_TEST_SUITE(ConditionTests);
109  CPPUNIT_TEST(test_Condition_wait_and_signal);
110  CPPUNIT_TEST(test_Condition_wait_and_broadcast);
111  CPPUNIT_TEST(test_Condition_wait_with_time);
112  CPPUNIT_TEST_SUITE_END();
113 
114  private:
116 
117  public:
122  {
123  }
124 
129  {
130  }
131 
132 
136  virtual void setUp()
137  {
138  mu = new coil::Mutex;
139  }
140 
144  virtual void tearDown()
145  {
146  delete mu;
147  }
148 
149  /* test case */
150 
155  {
156  coil::Mutex mu1;
157  coil::Condition<coil::Mutex> cond1(mu1);
158  coil::Mutex mu2;
159  coil::Condition<coil::Mutex> cond2(mu2);
160  int id1(0x02);
161  int id2(0x08);
162 
163  ConditionCheckTask::resteStatus();
164  ConditionCheckTask cct1(mu1, cond1, id1);
165  ConditionCheckTask cct2(mu1, cond1, id2);
166 
167  cct1.activate();
168  cct2.activate();
169 
170  coil::usleep(10000); // give cpu time to tasks
171 
172  CPPUNIT_ASSERT_EQUAL(0x00, ConditionCheckTask::getStatus());
173 
174  {
175  Guard guard(mu1);
176  // mu1.lock();
177  cond1.signal();
178  // mu1.unlock();
179  }
180 
181  coil::usleep(10000); // give cpu time to tasks
182 
183  CPPUNIT_ASSERT_EQUAL(id1, ConditionCheckTask::getStatus());
184 
185  {
186  Guard guard(mu1);
187  // mu1.lock();
188  cond1.signal();
189  // mu1.unlock();
190  }
191 
192  coil::usleep(10000); // give cpu time to tasks
193 
194  CPPUNIT_ASSERT_EQUAL(id1 + id2, ConditionCheckTask::getStatus());
195 
196  // cct1.wait();
197  // cct2.wait();
198  }
199 
204  {
206  coil::Mutex mu2;
207  coil::Condition<coil::Mutex> cond2(mu2);
208 
209  ConditionCheckTask::resteStatus();
210  ConditionCheckTask cct6(*mu, cd, 0x20);
211  ConditionCheckTask cct5(*mu, cd, 0x10);
212  ConditionCheckTask cct4(*mu, cd, 0x08);
213  ConditionCheckTask cct3(*mu, cd, 0x04);
214  ConditionCheckTask cct2(*mu, cd, 0x02);
215  ConditionCheckTask cct1(*mu, cd, 0x01);
216 
217  cct6.activate();
218  cct5.activate();
219  cct4.activate();
220  cct3.activate();
221  cct2.activate();
222  cct1.activate();
223 
224  CPPUNIT_ASSERT_EQUAL(0, ConditionCheckTask::getStatus());
225 
226  {
227  Guard guard(mu2);
228  // mu2.lock();
229  cond2.wait(1);
230  // mu2.unlock();
231  }
232 
233  {
234  Guard guard(*mu);
235  // mu->lock();
236  cd.broadcast();
237  // mu->unlock();
238  }
239 
240  {
241  Guard guard(mu2);
242  // mu2.lock();
243  cond2.wait(1);
244  // mu2.unlock();
245  }
246 
247  CPPUNIT_ASSERT_EQUAL(0x3f, ConditionCheckTask::getStatus());
248 
249  CPPUNIT_ASSERT(true);
250  }
251 
256  {
257  int waitSec(2);
259  // std::cout << "Before wait " << waitSec << " sec." << std::endl << std::flush;
260  bool result;
261  {
262  Guard guard(*mu);
263  // mu->lock();
264  result = cd.wait(waitSec);
265  // mu->unlock();
266  }
267 
268  // result = false (timeout)
269  // if (result) { std::cout << "signaled..ERROR" << std::endl; }
270  // else { std::cout << "timeout...OK" << std::endl; }
271  CPPUNIT_ASSERT(!result);
272 
273 
274  A a(cd, *mu);
275  coil::Async*
276  invoker(coil::AsyncInvoker(&a,
277  std::bind2nd(std::mem_fun(&A::signal),
278  1000000)));
279  invoker->invoke();
280  {
281  Guard guard(*mu);
282  // mu->lock();
283  result = cd.wait(waitSec);
284  // mu->unlock();
285  }
286  invoker->wait();
287 
288  // result = true (signal)
289  // if (result) { std::cout << "signaled..OK" << std::endl; }
290  // else { std::cout << "timeout...ERROR" << std::endl; }
291  CPPUNIT_ASSERT(result);
292  }
293 
294  };
295 }; // namespace Condition
296 
297 /*
298  * Register test suite
299  */
301 
302 #ifdef LOCAL_MAIN
303 int main(int argc, char* argv[])
304 {
305  CppUnit::TextUi::TestRunner runner;
306  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
307  CppUnit::Outputter* outputter =
308  new CppUnit::TextOutputter(&runner.result(), std::cout);
309  runner.setOutputter(outputter);
310  bool retcode = runner.run();
311  return !retcode;
312 }
313 #endif // MAIN
314 #endif // Condition_cpp
A(coil::Condition< coil::Mutex > &cond, coil::Mutex &mutex)
試験用タスク。起動するとwaitして、起こされると生成時に渡された値をConditionStatusに加算する。
int main(int argc, char **argv)
ConditionCheckTask(coil::Mutex &aMutex, coil::Condition< coil::Mutex > &aCondition, int anId)
virtual int svc()
Execute thread.
Mutex class.
static int getStatus()
結果情報のConditionStatusの現在値を返す
void test_Condition_wait_and_broadcast()
wati with time.
void test_Condition_wait_and_signal()
wati with time.
virtual int wait(void)
Waiting for the thread terminate.
void test_Condition_wait_with_time()
wati with time.
coil::Guard< coil::Mutex > Guard
virtual void setUp()
Test initialization.
coil::Mutex & m_mutex
coil::Condition< coil::Mutex > & m_cond
static void resteStatus()
結果情報のConditionStatusをクリアする
virtual void invoke()=0
Asynchronous invocation.
CPPUNIT_TEST_SUITE_REGISTRATION(Condition::ConditionTests)
Task class.
Async class.
Definition: Async.h:41
virtual void tearDown()
Test finalization.
coil::Condition< coil::Mutex > * cond
void signal(int usec)
int usleep(useconds_t usec)
Stop a processing at specified micro second time.
Definition: ace/coil/Time.h:51
virtual void activate()
Create a thread.
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 Feb 28 2022 23:00:42