SingletonTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log$
00014  *
00015  */
00016 
00017 #ifndef Singleton_cpp
00018 #define Singleton_cpp
00019 
00020 #include <cppunit/ui/text/TestRunner.h>
00021 #include <cppunit/TextOutputter.h>
00022 #include <cppunit/extensions/TestFactoryRegistry.h>
00023 #include <cppunit/extensions/HelperMacros.h>
00024 #include <cppunit/TestAssert.h>
00025 
00026 #include <iostream>
00027 
00028 #include <coil/Singleton.h>
00029 #include <coil/Task.h>
00030 #include <coil/Time.h>
00031 
00032 class A
00033 {
00034 public:
00035   A()
00036   {
00037     std::cout << "A ctor entered" << std::endl;
00038     coil::usleep(100000);
00039     ++count;
00040     std::cout << "A ctor done" << std::endl;
00041   }
00042   int get_count()
00043   {
00044     return count;
00045   }
00046 private:
00047   static int count;
00048 };
00049 
00050 int A::count = 0;
00051 
00052 class B
00053 {
00054 public:
00055   B()
00056   {
00057     std::cout << "B ctor entered" << std::endl;
00058     coil::usleep(100000);
00059     ++count;
00060     std::cout << "B ctor done" << std::endl;
00061   }
00062   int get_count()
00063   {
00064     return count;
00065   }
00066 private:
00067   static int count;
00068 };
00069 
00070 int B::count = 0;
00071 
00072 class C
00073   : public coil::Singleton<C>
00074 {
00075 public:
00076   int get_count()
00077   {
00078     return count;
00079   }
00080 private:
00081   static int count;
00082   C()
00083   {
00084     std::cout << "C ctor entered" << std::endl;
00085     coil::usleep(100000);
00086     ++count;
00087     std::cout << "C ctor done" << std::endl;
00088   }
00089   friend class coil::Singleton<C>;
00090 };
00091 
00092 int C::count = 0;
00093 
00094 class Threaded
00095   : public coil::Task
00096 {
00097 public:
00098   typedef coil::Singleton<A> A_;
00099   typedef coil::Singleton<B> B_;
00100   virtual int svc()
00101   {
00102     std::cout << "thread entered" << std::endl;
00103     for (int i(0); i < 10; ++i)
00104       {
00105         std::cout << "Getting A in thread" << std::endl;;
00106         A& a(A_::instance());
00107         std::cout << "Getting A done in thread" << std::endl;;
00108         B& b(B_::instance());
00109         CPPUNIT_ASSERT(a.get_count() == 1);
00110         CPPUNIT_ASSERT(b.get_count() == 1);
00111         coil::usleep(1000);
00112       }
00113     return 0;
00114   }
00115 };
00116 
00121 namespace Singleton
00122 {
00123   class SingletonTests
00124    : public CppUnit::TestFixture
00125   {
00126     CPPUNIT_TEST_SUITE(SingletonTests);
00127     CPPUNIT_TEST(test_case0);
00128     CPPUNIT_TEST_SUITE_END();
00129   
00130   private:
00131   
00132   public:
00133     typedef coil::Singleton<A> A_;
00134     typedef coil::Singleton<B> B_;
00138     SingletonTests()
00139     {
00140     }
00141     
00145     ~SingletonTests()
00146     {
00147     }
00148   
00152     virtual void setUp()
00153     {
00154     }
00155     
00159     virtual void tearDown()
00160     { 
00161     }
00162   
00163     /* test case */
00164     void test_case0()
00165     {
00166       Threaded t;
00167       t.activate();
00168       for (int i(0); i < 10; ++i)
00169         {
00170           std::cout << "Getting A in main" << std::endl;
00171           A& a(A_::instance());
00172           std::cout << "Getting A done in main" << std::endl;
00173 
00174           std::cout << "Getting B in main" << std::endl;
00175           B& b(B_::instance());
00176           std::cout << "Getting B done in main" << std::endl;
00177           
00178           std::cout << "Getting C in main" << std::endl;
00179           C& c(C::instance());
00180           std::cout << "Getting C done in main" << std::endl;
00181 
00182           CPPUNIT_ASSERT(a.get_count() == 1);
00183           CPPUNIT_ASSERT(b.get_count() == 1);
00184           CPPUNIT_ASSERT(c.get_count() == 1);
00185           coil::usleep(1000);
00186         }
00187 
00188 
00189 
00190     }
00191   };
00192 }; // namespace Singleton
00193 
00194 /*
00195  * Register test suite
00196  */
00197 CPPUNIT_TEST_SUITE_REGISTRATION(Singleton::SingletonTests);
00198 
00199 #ifdef LOCAL_MAIN
00200 int main(int argc, char* argv[])
00201 {
00202     CppUnit::TextUi::TestRunner runner;
00203     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00204     CppUnit::Outputter* outputter = 
00205       new CppUnit::TextOutputter(&runner.result(), std::cout);
00206     runner.setOutputter(outputter);
00207     bool retcode = runner.run();
00208     return !retcode;
00209 }
00210 #endif // MAIN
00211 #endif // Singleton_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:17