MutexTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef Mutex_cpp
18 #define Mutex_cpp
19 
20 #include <iostream>
21 #include <iomanip>
22 #include <string>
23 #include <stdio.h>
24 //#include <unistd.h>
25 #include <time.h>
26 #include <cppunit/ui/text/TestRunner.h>
27 #include <cppunit/TextOutputter.h>
28 #include <cppunit/extensions/TestFactoryRegistry.h>
29 #include <cppunit/extensions/HelperMacros.h>
30 #include <cppunit/TestAssert.h>
31 
32 #include <coil/Mutex.h>
33 #include <coil/Task.h>
34 #include <coil/Time.h>
35 
36 
41 namespace Mutex
42 {
43  class MutexTests
44  : public CppUnit::TestFixture
45  {
47 // CPPUNIT_TEST(test_case0);
51 
52  private:
53  public:
55  : public coil::Task
56  {
57  public:
58  static long ShareCount;
59  static coil::Mutex mtx;
60  int svc(void)
61  {
62  clock_t start, end;
63  long lc;
64  long lbuf;
65  for(lc=0; lc<100; lc++)
66  {
67  mtx.lock();
68  lbuf = ShareCount;
69 // usleep(1);
70  start = clock();
71  for(;;)
72  {
73  end = clock();
74  if((double)(end-start)/CLOCKS_PER_SEC>0.000001)
75  {
76  break;
77  }
78  }
79  lbuf++;
80  ShareCount = lbuf;
81  mtx.unlock();
82  }
83  return 0;
84  }
85 
87  {
88  return ShareCount;
89  }
90  void setShareCount(long lc)
91  {
92  mtx.lock();
93  ShareCount = lc;
94  mtx.unlock();
95  }
96  };
98  : public coil::Task
99  {
100  public:
101  static long ShareCount;
102  static coil::Mutex mtx;
103  int svc(void)
104  {
105  clock_t start, end;
106  long lc;
107  long lbuf;
108  int iret;
109  for(lc=0; lc<100; lc++)
110  {
111  for (;;)
112  {
113  iret = mtx.trylock();
114  if(iret == false)
115  {
116  lbuf = ShareCount;
117 // usleep(1);
118  start = clock();
119  for(;;)
120  {
121  end = clock();
122  if((double)(end-start)/CLOCKS_PER_SEC>0.000001)
123  {
124  break;
125  }
126  }
127  lbuf++;
128  ShareCount = lbuf;
129  break;
130  }
131  }
132  mtx.unlock();
133  }
134  return 0;
135  }
136 
138  {
139  return ShareCount;
140  }
141  void setShareCount(long lc)
142  {
143  mtx.lock();
144  ShareCount = lc;
145  mtx.unlock();
146  }
147 
148  };
153  {
154  }
155 
160  {
161  }
162 
166  virtual void setUp()
167  {
168  }
169 
173  virtual void tearDown()
174  {
175  }
176 
177  /* test case */
178  /*
179  ---------------------------------------------------------------------------
180  This function tests the Mutex::lock function.
181  Check exclusive control,when two threads access static variable.
182  ---------------------------------------------------------------------------
183  */
184  void test_lock()
185  {
186  long lc;
187  char cstr[256];
190  tka.setShareCount(0);
191 
192  tka.activate();
193  tkb.activate();
194  tka.wait();
195  tkb.wait();
196  lc = tka.getShareCount();
197  sprintf( cstr, "count %ld" , lc );
198 // std::cout<<cstr<<std::endl;
199  CPPUNIT_ASSERT_MESSAGE(cstr , (lc == 200) );
200 
201  }
202  /*
203  ---------------------------------------------------------------------------
204  This function tests the Mutex::trylock function.
205  Check exclusive control,when two threads access static variable.
206  ---------------------------------------------------------------------------
207  */
209  {
210  long lc;
211  char cstr[256];
214  tka.setShareCount(0);
215 
216  tka.activate();
217  tkb.activate();
218  tka.wait();
219  tkb.wait();
220  lc = tka.getShareCount();
221  sprintf( cstr, "count %ld" , lc );
222 // std::cout<<cstr<<std::endl;
223  CPPUNIT_ASSERT_MESSAGE(cstr , (lc == 200) );
224 
225  }
226  };
231 }; // namespace Mutex
232 
233 /*
234  * Register test suite
235  */
237 
238 #ifdef LOCAL_MAIN
239 int main(int argc, char* argv[])
240 {
241  CppUnit::TextUi::TestRunner runner;
242  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
243  CppUnit::Outputter* outputter =
244  new CppUnit::TextOutputter(&runner.result(), std::cout);
245  runner.setOutputter(outputter);
246  bool retcode = runner.run();
247  return !retcode;
248 }
249 #endif // MAIN
250 #endif // Mutex_cpp
int main(int argc, char **argv)
Mutex class.
int svc(void)
Execute thread.
Definition: MutexTests.cpp:103
CPPUNIT_TEST(test_lock)
virtual int wait(void)
Waiting for the thread terminate.
virtual void tearDown()
Test finalization.
Definition: MutexTests.cpp:173
CPPUNIT_TEST_SUITE_REGISTRATION(Mutex::MutexTests)
MutexTests()
Constructor.
Definition: MutexTests.cpp:152
bool trylock()
int svc(void)
Execute thread.
Definition: MutexTests.cpp:60
~MutexTests()
Destructor.
Definition: MutexTests.cpp:159
CPPUNIT_TEST_SUITE(MutexTests)
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
Definition: stringutil.cpp:598
Task class.
virtual void setUp()
Test initialization.
Definition: MutexTests.cpp:166
virtual void activate()
Create a thread.


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