GuardTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef Guard_cpp
18 #define Guard_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 <../../include/coil/Guard.h>
33 #include <coil/Guard.h>
34 #include <coil/Task.h>
35 
40 namespace Guard
41 {
42  class GuardTests
43  : public CppUnit::TestFixture ,
44  public coil::Task
45  {
49 
50  private:
51 
52  public:
54 // long sharecount;
56  : public coil::Task
57  {
58  public:
59  static long ShareCount;
60  static coil::Mutex mtx;
61  int svc(void)
62  {
63  clock_t start,end;
64  long lc;
65  long lbuf;
66  coil::Guard<coil::Mutex> guard(mtx);
67  for(lc=0; lc<100; lc++)
68  {
69  lbuf = ShareCount;
70  start = clock();
71  for(;;)
72  {
73  end = clock();
74  if((end-start)>1)
75  {
76  break;
77  }
78  }
79  lbuf++;
80  ShareCount = lbuf;
81  }
82  return 0;
83  }
84 
86  {
87  return ShareCount;
88  }
89  void setShareCount(long lc)
90  {
91  coil::Guard<coil::Mutex> guard(mtx);
92  ShareCount = lc;
93  }
94  };
95 
100  {
101 // sharecount = 0;
102  }
103 
108  {
109  }
110 
114  virtual void setUp()
115  {
116  }
117 
121  virtual void tearDown()
122  {
123  }
124 
125  /* test case */
126  int svc(void)
127  {
128  return 0;
129  }
130  /*
131  ---------------------------------------------------------------------------
132  This function tests coil::Guard.
133  Check exclusive control,when two threads access static variable.
134  ---------------------------------------------------------------------------
135  */
136  void test_case0()
137  {
140  tka.setShareCount(0);
141  long lc;
142  char cstr[256];
143 
144  tka.activate();
145  tkb.activate();
146  tka.wait();
147  tkb.wait();
148  lc = tka.getShareCount();
149  sprintf(cstr, "sharecount:%ld", lc);
150  CPPUNIT_ASSERT_MESSAGE(cstr, (lc== 200) );
151 
152  }
153  };
156 
157 }; // namespace Guard
158 
159 /*
160  * Register test suite
161  */
163 
164 #ifdef LOCAL_MAIN
165 int main(int argc, char* argv[])
166 {
167  CppUnit::TextUi::TestRunner runner;
168  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
169  CppUnit::Outputter* outputter =
170  new CppUnit::TextOutputter(&runner.result(), std::cout);
171  runner.setOutputter(outputter);
172  bool retcode = runner.run();
173  return !retcode;
174 }
175 #endif // MAIN
176 #endif // Guard_cpp
virtual void setUp()
Test initialization.
Definition: GuardTests.cpp:114
int main(int argc, char **argv)
Mutex class.
int svc(void)
Execute thread.
Definition: GuardTests.cpp:126
GuardTests()
Constructor.
Definition: GuardTests.cpp:99
virtual void tearDown()
Test finalization.
Definition: GuardTests.cpp:121
CPPUNIT_TEST(test_case0)
virtual int wait(void)
Waiting for the thread terminate.
CPPUNIT_TEST_SUITE(GuardTests)
~GuardTests()
Destructor.
Definition: GuardTests.cpp:107
coil::Mutex mtx
Definition: GuardTests.cpp:53
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
Definition: stringutil.cpp:593
Task class.
int svc(void)
Execute thread.
Definition: GuardTests.cpp:61
virtual void activate()
Create a thread.
CPPUNIT_TEST_SUITE_REGISTRATION(Guard::GuardTests)


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:25:58