coil/tests/Factory/FactoryTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef Factory_cpp
18 #define Factory_cpp
19 
20 #define LIBRARY_EXPORTS
21 #include <string>
22 
23 #include <cppunit/ui/text/TestRunner.h>
24 #include <cppunit/TextOutputter.h>
25 #include <cppunit/extensions/TestFactoryRegistry.h>
26 #include <cppunit/extensions/HelperMacros.h>
27 #include <cppunit/TestAssert.h>
28 
29 #include <coil/Factory.h>
30 #include <coil/DynamicLib.h>
31 
32 #include "MyFactory.h"
33 
34 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
35  const char * LibName = "FactoryTest_libPluginC.dll";
36 #else
37  const char * LibName = "./.libs/libPluginC.so";
38 #endif /* Windows */
39 
40 //class Base
41 //{
42 //public:
43 // virtual ~Base(){}
44 // virtual std::string name() = 0;
45 //};
46 
47 class A
48  : public Base
49 {
50 public:
51  virtual std::string name()
52  {
53  return "A";
54  }
55 };
56 
57 class B
58  : public Base
59 {
60 public:
61  virtual std::string name()
62  {
63  return "B";
64  }
65 };
66 
67 //typedef coil::GlobalFactory<Base> g_factory;
68 
73 namespace Factory
74 {
76  : public CppUnit::TestFixture
77  {
78  CPPUNIT_TEST_SUITE(FactoryTests);
79  CPPUNIT_TEST(test_case0);
80  CPPUNIT_TEST(test_case1);
81  CPPUNIT_TEST(test_case2);
82  CPPUNIT_TEST_SUITE_END();
83 
84  private:
86  public:
87 
92  {
93  }
94 
99  {
100  }
101 
105  virtual void setUp()
106  {
107  }
108 
112  virtual void tearDown()
113  {
114  }
115 
116  /* test case */
117  void test_case0()
118  {
119  std::string Astr("A");
120  std::string Bstr("B");
121  m_factory.addFactory("A",
122  coil::Creator<Base, A>,
123  coil::Destructor<Base, A>);
124  m_factory.addFactory("B",
125  coil::Creator<Base, B>,
126  coil::Destructor<Base, B>);
127 
128 
129 
130  for (int i(0); i < 1000; ++i)
131  {
132  Base* a = m_factory.createObject("A");
133  CPPUNIT_ASSERT(a->name() == "A");
134 
135  Base* b = m_factory.createObject("B");
136  CPPUNIT_ASSERT(b->name() == "B");
137 
138  m_factory.deleteObject(a);
139  CPPUNIT_ASSERT(a == 0);
140 
141  m_factory.deleteObject(b);
142  CPPUNIT_ASSERT(b == 0);
143  }
144  }
145 
146  void test_case1()
147  {
148  std::string Astr("A");
149  std::string Bstr("B");
150  MyFactory::instance().addFactory("A",
151  coil::Creator<Base, A>,
152  coil::Destructor<Base, A>);
153  MyFactory::instance().addFactory("B",
154  coil::Creator<Base, B>,
155  coil::Destructor<Base, B>);
156 
157  coil::DynamicLib dl;
158  if (dl.open(LibName) < 0)
159  {
160  std::cout << "dl.open error" << std::endl;
161  }
162 
163  typedef void (*InitFunc)();
164  InitFunc init_func = (InitFunc)dl.symbol("PluginCInit");
165  if (init_func == NULL)
166  {
167  std::cout << "synbol is NULL" << std::endl;
168  }
169  (*init_func)();
170 
171  if (MyFactory::instance().hasFactory("C"))
172  {
173  std::cout << "Factory C exists" << std::endl;
174  }
175  else
176  {
177  std::cout << "Factory C does not exist" << std::endl;
178  }
179 
180  std::vector<std::string> ids(MyFactory::instance().getIdentifiers());
181  std::cout << "IDs -> ";
182  for (int i(0), len(ids.size()); i < len; ++i)
183  {
184  std::cout << ids[i] << ", ";
185  }
187  for (int i(0); i < 1000; ++i)
188  {
189  Base* a = MyFactory::instance().createObject("A");
190  CPPUNIT_ASSERT(a->name() == "A");
191 
192  Base* b = MyFactory::instance().createObject("B");
193  CPPUNIT_ASSERT(b->name() == "B");
194 
195  Base* c = MyFactory::instance().createObject("C");
196  CPPUNIT_ASSERT(c->name() == "C");
197 
198  MyFactory::instance().deleteObject(a);
199  CPPUNIT_ASSERT(a == 0);
200 
201  MyFactory::instance().deleteObject(b);
202  CPPUNIT_ASSERT(b == 0);
203 
204  MyFactory::instance().deleteObject(c);
205  CPPUNIT_ASSERT(c == 0);
206 
207  CPPUNIT_ASSERT(&g == &MyFactory::instance());
208  }
209  }
210 
214  void test_case2()
215  {
216  std::string Astr("A");
217  std::string Bstr("B");
218  MyFactory::instance().addFactory("A",
219  coil::Creator<Base, A>,
220  coil::Destructor<Base, A>);
221  //Factory A exists//
222  CPPUNIT_ASSERT(MyFactory::instance().hasFactory("A"));
223 
224  MyFactory::instance().addFactory("B",
225  coil::Creator<Base, B>,
226  coil::Destructor<Base, B>);
227  //Factory B exists//
228  CPPUNIT_ASSERT(MyFactory::instance().hasFactory("B"));
229 
230  coil::DynamicLib dl;
231  if (dl.open(LibName) < 0)
232  {
233  std::cout << "dl.open error" << std::endl;
234  }
235 
236  typedef void (*InitFunc)();
237  InitFunc init_func = (InitFunc)dl.symbol("PluginCInit");
238  if (init_func == NULL)
239  {
240  std::cout << "synbol is NULL" << std::endl;
241  }
242  (*init_func)();
243 
244  //Factory C exists//
245  CPPUNIT_ASSERT(MyFactory::instance().hasFactory("C"));
246 
247  std::vector<std::string> ids(MyFactory::instance().getIdentifiers());
248  for (int i(0), len(ids.size()); i < len; ++i)
249  {
251  ret = MyFactory::instance().removeFactory(ids[i]);
252  CPPUNIT_ASSERT(ret == MyFactory::FACTORY_OK);
253  CPPUNIT_ASSERT(!MyFactory::instance().hasFactory(ids[i]));
254  ret = MyFactory::instance().removeFactory(ids[i]);
255  CPPUNIT_ASSERT(ret == MyFactory::NOT_FOUND);
256  }
257 
258  }
259  };
260 }; // namespace Factory
261 
262 /*
263  * Register test suite
264  */
266 
267 #ifdef LOCAL_MAIN
268 int main(int argc, char* argv[])
269 {
270  CppUnit::TextUi::TestRunner runner;
271  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
272  CppUnit::Outputter* outputter =
273  new CppUnit::TextOutputter(&runner.result(), std::cout);
274  runner.setOutputter(outputter);
275  bool retcode = runner.run();
276  return !retcode;
277 }
278 #endif // MAIN
279 #endif // Factory_cpp
CPPUNIT_TEST_SUITE_REGISTRATION(Factory::FactoryTests)
Definition: MyFactory.h:5
int main(int argc, char **argv)
virtual std::string name()=0
void * symbol(const char *symbol_name)
Return an address of the memory where a symbol was loaded.
virtual std::string name()
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
ReturnCode addFactory(const Identifier &id, Creator creator, Destructor destructor)
Add factory.
static GlobalFactory< AbstractClass, Identifier, Compare, Creator, Destructor > & instance()
Create instance.
Definition: Singleton.h:131
GlobalFactory template class.
virtual void tearDown()
Test finalization.
DynamicLib class.
const char * LibName
void deleteObject(const Identifier &id, AbstractClass *&obj)
Delete factory object.
AbstractClass * createObject(const Identifier &id)
Create factory object.
virtual void setUp()
Test initialization.
virtual std::string name()
virtual int open(const char *dll_name, int open_mode=COIL_DEFAULT_DYNLIB_MODE, int close_handle_on_destruction=1)
Load of the Dynamic link library.


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