TaskTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef Task_cpp
18 #define Task_cpp
19 
20 #include <iostream>
21 #include <iomanip>
22 #include <string>
23 #include <stdio.h>
24 #include <time.h>
25 #include <cppunit/ui/text/TestRunner.h>
26 #include <cppunit/TextOutputter.h>
27 #include <cppunit/extensions/TestFactoryRegistry.h>
28 #include <cppunit/extensions/HelperMacros.h>
29 #include <cppunit/TestAssert.h>
30 
31 //#include <../../include/coil/Task.h>
32 #include <coil/Task.h>
33 #include <coil/Time.h>
34 
39 namespace Task
40 {
41  class TaskTests
42  : public CppUnit::TestFixture ,
43  public coil::Task
44  {
46 // CPPUNIT_TEST(test_case0);
54 
55  private:
56  bool m_statflag;
57  short m_tasknumber;
58  short m_threadcmd;
59  short m_threadcnt[256];
60  public:
66  {
67  short ic;
68  for (ic=0; ic<256; ic++)
69  {
70  m_threadcnt[ic] = 0;
71  }
72  m_statflag = false;
73  m_tasknumber =0;
74  m_threadcmd = 0;
75  }
76 
81  {
82  }
83 
87  virtual void setUp()
88  {
89  }
90 
94  virtual void tearDown()
95  {
96  }
97 
98  /*
99  ---------------------------------------------------------------------------
100  ---------------------------------------------------------------------------
101  */
102  int svc(void)
103  {
104  short ic;
105  m_statflag = true;
106  switch(m_threadcmd)
107  {
108  case 0:
109  std::cout<<"/"<<std::endl;
110  m_tasknumber ++;
111  for(;;)
112  {
113  if(m_statflag != true)
114  {
115  break;
116  }
117  m_threadcnt[m_tasknumber-1]++;
118  }
119  break;
120  case 1:
121  std::cout<<"/"<<std::endl;
122  for(ic=0;ic<10;ic++){
123  ;;
124  }
125  break;
126  default:
127  break;
128  }
129  return 0;
130  }
131  /* test case */
132  void test_case0()
133  {
134  }
135  /*
136  ---------------------------------------------------------------------------
137  This function tests the Task::open function and the Task::close function.
138  Check that the open function and the close function return 0.
139  ---------------------------------------------------------------------------
140  */
142  {
143  int iret;
144  iret = 1;
145  iret = open(0);
146  CPPUNIT_ASSERT_MESSAGE("open", (iret == 0) );
147 
148  iret = 1;
149  iret = close(0);
150  CPPUNIT_ASSERT_MESSAGE("close", (iret == 0) );
151 
152  }
153  /*
154  ---------------------------------------------------------------------------
155  This function tests the Task::activate function.
156  Check that the thread makes only one even if the activate function is
157  called two or more times.
158  ---------------------------------------------------------------------------
159  */
161  {
162 
163  time_t tmstart, tmend;
164  char cstr[256];
165  short ic;
166  if ( m_statflag == true )
167  {
168  m_statflag = false;
169  }
170  m_threadcmd = 0;
171  m_tasknumber = 0;
172  //Start 10 threads. & Check that only 1 thread start.
173  for (ic=0; ic<10; ic++)
174  {
175  //Start a thread.
176  activate();
177  time(&tmstart);
178  for(;;)
179  {
180  time(&tmend);
181  if(difftime(tmend,tmstart)>=1.0)
182  {
183  break;
184  }
185  }
186  sprintf(cstr, "counter:%d ", m_tasknumber);
187  //Check that a thread start.
188  CPPUNIT_ASSERT_MESSAGE(cstr , (m_tasknumber == 1) );
189  }
190  m_statflag = false;
191  wait();
192 
193  }
200  {
201 
202  time_t tmstart, tmend;
203  char cstr[256];
204  short ic;
205  if ( m_statflag == true )
206  {
207  m_statflag = false;
208  }
209  m_threadcmd = 0;
210  m_tasknumber = 0;
211  //Start 10 threads. & Check that 10 thread start.
212  for (ic=0; ic<10; ic++)
213  {
214  //Start a thread.
215  activate();
216  time(&tmstart);
217  for(;;)
218  {
219  time(&tmend);
220  if(difftime(tmend,tmstart)>=1.0)
221  {
222  break;
223  }
224  }
225  sprintf(cstr, "m_tasknumber:%d (ic+1):%d", m_tasknumber,ic+1);
226  //Check that a thread start.
227  CPPUNIT_ASSERT_MESSAGE(cstr , (m_tasknumber == ic+1) );
228  m_statflag = false;
229  wait();
230  }
231 
232  }
233  /*
234  ---------------------------------------------------------------------------
235  ---------------------------------------------------------------------------
236  */
237  void test_wait()
238  {
239  wait(); //If Segmentation fault is not caused, it is OK.
240  m_threadcmd = 1;
241  activate();
242  wait();
243  }
244  /*
245  ---------------------------------------------------------------------------
246  This function tests the Task::suspend function.
247  Check that the suspend function returns 0.
248  ---------------------------------------------------------------------------
249  */
251  {
252  int iret;
253  iret = 1;
254  iret = suspend();
255  CPPUNIT_ASSERT_MESSAGE("suspend", (iret == 0) );
256  }
257  /*
258  ---------------------------------------------------------------------------
259  This function tests the Task::resume function.
260  Check that the resume function returns 0.
261  ---------------------------------------------------------------------------
262  */
263  void test_resume()
264  {
265  int iret;
266  iret = 1;
267  iret = resume();
268  CPPUNIT_ASSERT_MESSAGE("resume", (iret == 0) );
269  }
270  };
271 }; // namespace Task
272 
273 /*
274  * Register test suite
275  */
277 
278 #ifdef LOCAL_MAIN
279 int main(int argc, char* argv[])
280 {
281  CppUnit::TextUi::TestRunner runner;
282  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
283  CppUnit::Outputter* outputter =
284  new CppUnit::TextOutputter(&runner.result(), std::cout);
285  runner.setOutputter(outputter);
286  bool retcode = runner.run();
287  return !retcode;
288 }
289 #endif // MAIN
290 #endif // Task_cpp
int main(int argc, char **argv)
int svc(void)
Execute thread.
Definition: TaskTests.cpp:102
virtual void tearDown()
Test finalization.
Definition: TaskTests.cpp:94
TaskTests()
Constructor.
Definition: TaskTests.cpp:65
coil::Task * testtk
Definition: TaskTests.cpp:61
virtual int wait(void)
Waiting for the thread terminate.
virtual int suspend(void)
Suspending the task.
virtual int resume(void)
Resuming the suspended task.
short m_threadcnt[256]
Definition: TaskTests.cpp:59
short m_tasknumber
Definition: TaskTests.cpp:57
virtual void setUp()
Test initialization.
Definition: TaskTests.cpp:87
CPPUNIT_TEST(test_open_close)
void test_activate2()
activate()
Definition: TaskTests.cpp:199
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
Definition: stringutil.cpp:598
void test_activate()
Definition: TaskTests.cpp:160
void test_resume()
Definition: TaskTests.cpp:263
Task class.
virtual int open(void *args=0)
Task open.
CPPUNIT_TEST_SUITE_REGISTRATION(Task::TaskTests)
void test_suspend()
Definition: TaskTests.cpp:250
virtual int close(unsigned long flags=0)
Task close.
~TaskTests()
Destructor.
Definition: TaskTests.cpp:80
CPPUNIT_TEST_SUITE(TaskTests)
void test_open_close()
Definition: TaskTests.cpp:141
virtual void activate()
Create a thread.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:45