FactoryTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log$
00014  *
00015  */
00016 
00017 #ifndef Factory_cpp
00018 #define Factory_cpp
00019 
00020 #define LIBRARY_EXPORTS
00021 #include <string>
00022 
00023 #include <cppunit/ui/text/TestRunner.h>
00024 #include <cppunit/TextOutputter.h>
00025 #include <cppunit/extensions/TestFactoryRegistry.h>
00026 #include <cppunit/extensions/HelperMacros.h>
00027 #include <cppunit/TestAssert.h>
00028 
00029 #include <coil/Factory.h>
00030 #include <coil/DynamicLib.h>
00031 
00032 #include "MyFactory.h"
00033 
00034 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
00035   const char * LibName = "FactoryTest_libPluginC.dll";
00036 #else 
00037   const char * LibName = "./.libs/libPluginC.so";
00038 #endif /* Windows */
00039 
00040 //class Base
00041 //{
00042 //public:
00043 //  virtual ~Base(){}
00044 //  virtual std::string name() = 0;
00045 //};
00046 
00047 class A
00048   : public Base
00049 {
00050 public:
00051   virtual std::string name()
00052   {
00053     return "A";
00054   }
00055 };
00056 
00057 class B
00058   : public Base
00059 {
00060 public:
00061   virtual std::string name()
00062   {
00063     return "B";
00064   }
00065 };
00066 
00067 //typedef coil::GlobalFactory<Base> g_factory;
00068 
00073 namespace Factory
00074 {
00075   class FactoryTests
00076    : public CppUnit::TestFixture
00077   {
00078     CPPUNIT_TEST_SUITE(FactoryTests);
00079     CPPUNIT_TEST(test_case0);
00080     CPPUNIT_TEST(test_case1);
00081     CPPUNIT_TEST(test_case2);
00082     CPPUNIT_TEST_SUITE_END();
00083   
00084   private:
00085     coil::Factory<Base> m_factory;
00086   public:
00087   
00091     FactoryTests()
00092     {
00093     }
00094     
00098     ~FactoryTests()
00099     {
00100     }
00101   
00105     virtual void setUp()
00106     {
00107     }
00108     
00112     virtual void tearDown()
00113     { 
00114     }
00115   
00116     /* test case */
00117     void test_case0()
00118     {
00119       std::string Astr("A");
00120       std::string Bstr("B");
00121       m_factory.addFactory("A",
00122                            coil::Creator<Base, A>,
00123                            coil::Destructor<Base, A>);
00124       m_factory.addFactory("B",
00125                            coil::Creator<Base, B>,
00126                            coil::Destructor<Base, B>);
00127 
00128 
00129 
00130       for (int i(0); i < 1000; ++i)
00131         {
00132           Base* a = m_factory.createObject("A");
00133           CPPUNIT_ASSERT(a->name() == "A");
00134 
00135           Base* b = m_factory.createObject("B");
00136           CPPUNIT_ASSERT(b->name() == "B");
00137           
00138           m_factory.deleteObject(a);
00139           CPPUNIT_ASSERT(a == 0);
00140 
00141           m_factory.deleteObject(b);
00142           CPPUNIT_ASSERT(b == 0);
00143         }
00144     }
00145 
00146     void test_case1()
00147     {
00148       std::string Astr("A");
00149       std::string Bstr("B");
00150       MyFactory::instance().addFactory("A",
00151                            coil::Creator<Base, A>,
00152                            coil::Destructor<Base, A>);
00153       MyFactory::instance().addFactory("B",
00154                            coil::Creator<Base, B>,
00155                            coil::Destructor<Base, B>);
00156 
00157       coil::DynamicLib dl;
00158       if (dl.open(LibName) < 0)
00159         {
00160           std::cout << "dl.open error" << std::endl;
00161         }
00162 
00163       typedef void (*InitFunc)();
00164       InitFunc init_func = (InitFunc)dl.symbol("PluginCInit");
00165       if (init_func == NULL)
00166         {
00167           std::cout << "synbol is NULL" << std::endl;
00168         }
00169       (*init_func)();
00170 
00171       if (MyFactory::instance().hasFactory("C"))
00172         {
00173           std::cout << "Factory C exists" << std::endl;
00174         }
00175       else
00176         {
00177           std::cout << "Factory C does not exist" << std::endl;
00178         }
00179 
00180       std::vector<std::string> ids(MyFactory::instance().getIdentifiers());
00181       std::cout << "IDs -> ";
00182       for (int i(0), len(ids.size()); i < len; ++i)
00183         {
00184           std::cout << ids[i] << ", ";
00185         }
00186       MyFactory& g(MyFactory::instance());
00187       for (int i(0); i < 1000; ++i)
00188         {
00189           Base* a = MyFactory::instance().createObject("A");
00190           CPPUNIT_ASSERT(a->name() == "A");
00191 
00192           Base* b = MyFactory::instance().createObject("B");
00193           CPPUNIT_ASSERT(b->name() == "B");
00194                     
00195           Base* c = MyFactory::instance().createObject("C");
00196           CPPUNIT_ASSERT(c->name() == "C");
00197 
00198           MyFactory::instance().deleteObject(a);
00199           CPPUNIT_ASSERT(a == 0);
00200 
00201           MyFactory::instance().deleteObject(b);
00202           CPPUNIT_ASSERT(b == 0);
00203 
00204           MyFactory::instance().deleteObject(c);
00205           CPPUNIT_ASSERT(c == 0);
00206 
00207           CPPUNIT_ASSERT(&g == &MyFactory::instance());
00208         }
00209     }
00210 
00214     void test_case2()
00215     {
00216       std::string Astr("A");
00217       std::string Bstr("B");
00218       MyFactory::instance().addFactory("A",
00219                            coil::Creator<Base, A>,
00220                            coil::Destructor<Base, A>);
00221       //Factory A exists//
00222       CPPUNIT_ASSERT(MyFactory::instance().hasFactory("A"));
00223 
00224       MyFactory::instance().addFactory("B",
00225                            coil::Creator<Base, B>,
00226                            coil::Destructor<Base, B>);
00227       //Factory B exists//
00228       CPPUNIT_ASSERT(MyFactory::instance().hasFactory("B"));
00229 
00230       coil::DynamicLib dl;
00231       if (dl.open(LibName) < 0)
00232         {
00233           std::cout << "dl.open error" << std::endl;
00234         }
00235 
00236       typedef void (*InitFunc)();
00237       InitFunc init_func = (InitFunc)dl.symbol("PluginCInit");
00238       if (init_func == NULL)
00239         {
00240           std::cout << "synbol is NULL" << std::endl;
00241         }
00242       (*init_func)();
00243 
00244       //Factory C exists//
00245       CPPUNIT_ASSERT(MyFactory::instance().hasFactory("C"));
00246 
00247       std::vector<std::string> ids(MyFactory::instance().getIdentifiers());
00248       for (int i(0), len(ids.size()); i < len; ++i)
00249         {
00250           MyFactory::ReturnCode ret;
00251           ret = MyFactory::instance().removeFactory(ids[i]);
00252           CPPUNIT_ASSERT(ret == MyFactory::FACTORY_OK);
00253           CPPUNIT_ASSERT(!MyFactory::instance().hasFactory(ids[i]));
00254           ret = MyFactory::instance().removeFactory(ids[i]);
00255           CPPUNIT_ASSERT(ret == MyFactory::NOT_FOUND);
00256         }
00257 
00258     }
00259   };
00260 }; // namespace Factory
00261 
00262 /*
00263  * Register test suite
00264  */
00265 CPPUNIT_TEST_SUITE_REGISTRATION(Factory::FactoryTests);
00266 
00267 #ifdef LOCAL_MAIN
00268 int main(int argc, char* argv[])
00269 {
00270     CppUnit::TextUi::TestRunner runner;
00271     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00272     CppUnit::Outputter* outputter = 
00273       new CppUnit::TextOutputter(&runner.result(), std::cout);
00274     runner.setOutputter(outputter);
00275     bool retcode = runner.run();
00276     return !retcode;
00277 }
00278 #endif // MAIN
00279 #endif // Factory_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Aug 27 2015 14:16:37