SingletonTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef Singleton_cpp
18 #define Singleton_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 <iostream>
27 
28 #include <coil/Singleton.h>
29 #include <coil/Task.h>
30 #include <coil/Time.h>
31 
32 class A
33 {
34 public:
35  A()
36  {
37  std::cout << "A ctor entered" << std::endl;
38  coil::usleep(100000);
39  ++count;
40  std::cout << "A ctor done" << std::endl;
41  }
42  int get_count()
43  {
44  return count;
45  }
46 private:
47  static int count;
48 };
49 
50 int A::count = 0;
51 
52 class B
53 {
54 public:
55  B()
56  {
57  std::cout << "B ctor entered" << std::endl;
58  coil::usleep(100000);
59  ++count;
60  std::cout << "B ctor done" << std::endl;
61  }
62  int get_count()
63  {
64  return count;
65  }
66 private:
67  static int count;
68 };
69 
70 int B::count = 0;
71 
72 class C
73  : public coil::Singleton<C>
74 {
75 public:
76  int get_count()
77  {
78  return count;
79  }
80 private:
81  static int count;
82  C()
83  {
84  std::cout << "C ctor entered" << std::endl;
85  coil::usleep(100000);
86  ++count;
87  std::cout << "C ctor done" << std::endl;
88  }
89  friend class coil::Singleton<C>;
90 };
91 
92 int C::count = 0;
93 
94 class Threaded
95  : public coil::Task
96 {
97 public:
100  virtual int svc()
101  {
102  std::cout << "thread entered" << std::endl;
103  for (int i(0); i < 10; ++i)
104  {
105  std::cout << "Getting A in thread" << std::endl;;
106  A& a(A_::instance());
107  std::cout << "Getting A done in thread" << std::endl;;
108  B& b(B_::instance());
109  CPPUNIT_ASSERT(a.get_count() == 1);
110  CPPUNIT_ASSERT(b.get_count() == 1);
111  coil::usleep(1000);
112  }
113  return 0;
114  }
115 };
116 
121 namespace Singleton
122 {
124  : public CppUnit::TestFixture
125  {
126  CPPUNIT_TEST_SUITE(SingletonTests);
127  CPPUNIT_TEST(test_case0);
128  CPPUNIT_TEST_SUITE_END();
129 
130  private:
131 
132  public:
139  {
140  }
141 
146  {
147  }
148 
152  virtual void setUp()
153  {
154  }
155 
159  virtual void tearDown()
160  {
161  }
162 
163  /* test case */
164  void test_case0()
165  {
166  Threaded t;
167  t.activate();
168  for (int i(0); i < 10; ++i)
169  {
170  std::cout << "Getting A in main" << std::endl;
171  A& a(A_::instance());
172  std::cout << "Getting A done in main" << std::endl;
173 
174  std::cout << "Getting B in main" << std::endl;
175  B& b(B_::instance());
176  std::cout << "Getting B done in main" << std::endl;
177 
178  std::cout << "Getting C in main" << std::endl;
179  C& c(C::instance());
180  std::cout << "Getting C done in main" << std::endl;
181 
182  CPPUNIT_ASSERT(a.get_count() == 1);
183  CPPUNIT_ASSERT(b.get_count() == 1);
184  CPPUNIT_ASSERT(c.get_count() == 1);
185  coil::usleep(1000);
186  }
187 
188 
189 
190  }
191  };
192 }; // namespace Singleton
193 
194 /*
195  * Register test suite
196  */
198 
199 #ifdef LOCAL_MAIN
200 int main(int argc, char* argv[])
201 {
202  CppUnit::TextUi::TestRunner runner;
203  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
204  CppUnit::Outputter* outputter =
205  new CppUnit::TextOutputter(&runner.result(), std::cout);
206  runner.setOutputter(outputter);
207  bool retcode = runner.run();
208  return !retcode;
209 }
210 #endif // MAIN
211 #endif // Singleton_cpp
int main(int argc, char **argv)
static int count
coil::Singleton< A > A_
virtual void tearDown()
Test finalization.
Definition: PluginC.cpp:12
virtual int svc()
Execute thread.
static C & instance()
Create instance.
Definition: Singleton.h:131
coil::Singleton< A > A_
int get_count()
static int count
coil::Singleton< B > B_
Singleton template class.
Definition: Singleton.h:106
int get_count()
coil::Singleton< B > B_
Task class.
static int count
int get_count()
CPPUNIT_TEST_SUITE_REGISTRATION(Singleton::SingletonTests)
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.
virtual void setUp()
Test initialization.


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