ObjectManagerTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00021 /*
00022  * $Log: ObjectManagerTests.cpp,v $
00023  * Revision 1.2  2008/04/18 06:23:58  arafune
00024  * Modified / Added some tests.
00025  *
00026  * Revision 1.1  2007/12/20 07:50:18  arafune
00027  * *** empty log message ***
00028  *
00029  * Revision 1.1  2006/11/27 08:26:00  n-ando
00030  * TestSuites are devided into each directory.
00031  *
00032  * Revision 1.2  2006/10/24 03:08:15  kurihara
00033  *
00034  * test program for ObjectManager class
00035  *
00036  */
00037 
00038 #ifndef ObjectManager_cpp
00039 #define ObjectManager_cpp
00040 
00041 #include <cppunit/ui/text/TestRunner.h>
00042 #include <cppunit/TextOutputter.h>
00043 #include <cppunit/extensions/TestFactoryRegistry.h>
00044 #include <cppunit/extensions/HelperMacros.h>
00045 #include <cppunit/TestAssert.h>
00046 
00047 #include <fstream>
00048 #include <string>
00049 
00050 #include <rtm/ObjectManager.h>
00051 
00056 namespace ObjectManagerTests
00057 {
00058   class ObjectMock
00059   {
00060   public:
00061     ObjectMock(const std::string& id) : m_id(id) {}
00062     std::string getId() const { return std::string(m_id); }
00063         
00064   private:
00065     std::string m_id;
00066   };
00067         
00068   class PredicateMock
00069   {
00070   public:
00071     PredicateMock(const ObjectMock* obj) : m_id(obj->getId()) {}
00072     PredicateMock(const std::string& id) : m_id(id) {}
00073     bool operator()(const ObjectMock* obj) { return (obj->getId() == m_id); }
00074         
00075   private:
00076     const std::string m_id;
00077   };
00078         
00079   class ObjectManagerTests
00080     : public CppUnit::TestFixture
00081   {
00082     CPPUNIT_TEST_SUITE(ObjectManagerTests);
00083     CPPUNIT_TEST(test_registerObject);
00084     CPPUNIT_TEST(test_registerObject_with_overlapped_identifier);
00085     CPPUNIT_TEST(test_find);
00086     CPPUNIT_TEST(test_unregisterObject_and_find);
00087     CPPUNIT_TEST(test_getObjects);
00088     CPPUNIT_TEST_SUITE_END();
00089                 
00090   public:    
00094     ObjectManagerTests()
00095     {
00096     }
00097                 
00101     ~ObjectManagerTests()
00102     {
00103     }
00104                 
00108     virtual void setUp()
00109     {
00110     }
00111     
00115     virtual void tearDown()
00116     { 
00117     }
00118     
00124     void test_registerObject()
00125     {
00126       ::ObjectManager<std::string, ObjectMock, PredicateMock> objMgr;
00127 
00128       // オブジェクトを準備しておく
00129       ObjectMock obj1("ID 1");
00130       ObjectMock obj2("ID 2");
00131                         
00132       // オブジェクトを正常に登録できるか?
00133       CPPUNIT_ASSERT_EQUAL(true, objMgr.registerObject(&obj1));
00134       CPPUNIT_ASSERT_EQUAL(true, objMgr.registerObject(&obj2));
00135     }
00136     
00143     void test_registerObject_with_overlapped_identifier()
00144     {
00145       ::ObjectManager<std::string, ObjectMock, PredicateMock> objMgr;
00146 
00147       // 同一の識別子を持つオブジェクトを準備しておく
00148       ObjectMock obj1("ID");
00149       ObjectMock obj2("ID");
00150 
00151       // オブジェクトを登録する
00152       CPPUNIT_ASSERT_EQUAL(true, objMgr.registerObject(&obj1));
00153                         
00154       // 同一のオブジェクト(当然、識別子も同じ)の登録を試みて、意図どおり失敗するか?
00155       CPPUNIT_ASSERT_EQUAL(false, objMgr.registerObject(&obj1));
00156                         
00157       // 同一の識別子を持つ別のオブジェクトの登録を試みて、意図どおり失敗するか?
00158       CPPUNIT_ASSERT_EQUAL(false, objMgr.registerObject(&obj2));
00159     }
00160     
00167     void test_find()
00168     {
00169       ::ObjectManager<std::string, ObjectMock, PredicateMock> objMgr;
00170         
00171       // オブジェクトを準備しておく
00172       ObjectMock obj1("ID 1");
00173       ObjectMock obj2("ID 2");
00174 
00175       // オブジェクトを登録する
00176       CPPUNIT_ASSERT_EQUAL(true, objMgr.registerObject(&obj1));
00177       CPPUNIT_ASSERT_EQUAL(true, objMgr.registerObject(&obj2));
00178                         
00179       // 登録されたオブジェクトをfind()で正しく取得できるか?
00180       ObjectMock* pObjRet1 = objMgr.find("ID 1");
00181       CPPUNIT_ASSERT(pObjRet1 != NULL);
00182       CPPUNIT_ASSERT_EQUAL(std::string("ID 1"), pObjRet1->getId());
00183 
00184       ObjectMock* pObjRet2 = objMgr.find("ID 2");
00185       CPPUNIT_ASSERT(pObjRet2 != NULL);
00186       CPPUNIT_ASSERT_EQUAL(std::string("ID 2"), pObjRet2->getId());
00187                         
00188       // 存在しないIDを指定した場合、意図どおりNULLが取得されるか?
00189       CPPUNIT_ASSERT_EQUAL((ObjectMock*) NULL, objMgr.find("INEXIST ID"));
00190     }
00191     
00199     void test_unregisterObject_and_find()
00200     {
00201       ::ObjectManager<std::string, ObjectMock, PredicateMock> objMgr;
00202         
00203       // オブジェクトを準備しておく
00204       ObjectMock obj1("ID 1");
00205       ObjectMock obj2("ID 2");
00206 
00207       // オブジェクトを登録する
00208       CPPUNIT_ASSERT_EQUAL(true, objMgr.registerObject(&obj1));
00209       CPPUNIT_ASSERT_EQUAL(true, objMgr.registerObject(&obj2));
00210                         
00211       // オブジェクトを登録解除して、そのオブジェクトを正しく取得できるか?
00212       ObjectMock* pObjRet1 = objMgr.unregisterObject("ID 1");
00213       CPPUNIT_ASSERT(pObjRet1 != NULL);
00214       CPPUNIT_ASSERT_EQUAL(std::string("ID 1"), pObjRet1->getId());
00215                         
00216       // 登録解除したオブジェクトのIDを指定してfind()した場合、意図どおりNULLを得るか?
00217       CPPUNIT_ASSERT_EQUAL((ObjectMock*) NULL, objMgr.find("ID 1"));
00218                         
00219       // 登録解除していないオブジェクトは、依然としてfind()で正しく取得できるか?
00220       ObjectMock* pObjRet2 = objMgr.find("ID 2");
00221       CPPUNIT_ASSERT(pObjRet2 != NULL);
00222       CPPUNIT_ASSERT_EQUAL(std::string("ID 2"), pObjRet2->getId());
00223     }
00224     
00230     void test_getObjects()
00231     {
00232       ::ObjectManager<std::string, ObjectMock, PredicateMock> objMgr;
00233         
00234       // オブジェクトを準備しておく
00235       ObjectMock obj1("ID 1");
00236       ObjectMock obj2("ID 2");
00237 
00238       // オブジェクトを登録する
00239       CPPUNIT_ASSERT_EQUAL(true, objMgr.registerObject(&obj1));
00240       CPPUNIT_ASSERT_EQUAL(true, objMgr.registerObject(&obj2));
00241                         
00242       // getObjects()で、登録済みの全オブジェクトを正しく取得できるか?
00243       std::vector<ObjectMock*> objs = objMgr.getObjects();
00244       CPPUNIT_ASSERT_EQUAL(2, (int) objs.size());
00245       CPPUNIT_ASSERT_EQUAL(std::string("ID 1"),
00246                            (*std::find_if(objs.begin(), objs.end(), PredicateMock("ID 1")))->getId());
00247       CPPUNIT_ASSERT_EQUAL(std::string("ID 2"),
00248                            (*std::find_if(objs.begin(), objs.end(), PredicateMock("ID 2")))->getId());
00249     }
00250     
00251   };
00252 }; // namespace ObjectManager
00253 
00254 /*
00255  * Register test suite
00256  */
00257 CPPUNIT_TEST_SUITE_REGISTRATION(ObjectManagerTests::ObjectManagerTests);
00258 
00259 #ifdef LOCAL_MAIN
00260 int main(int argc, char* argv[])
00261 {
00262 
00263   FORMAT format = TEXT_OUT;
00264   int target = 0;
00265   std::string xsl;
00266   std::string ns;
00267   std::string fname;
00268   std::ofstream ofs;
00269 
00270   int i(1);
00271   while (i < argc)
00272     {
00273       std::string arg(argv[i]);
00274       std::string next_arg;
00275       if (i + 1 < argc) next_arg = argv[i + 1];
00276       else              next_arg = "";
00277 
00278       if (arg == "--text") { format = TEXT_OUT; break; }
00279       if (arg == "--xml")
00280         {
00281           if (next_arg == "")
00282             {
00283               fname = argv[0];
00284               fname += ".xml";
00285             }
00286           else
00287             {
00288               fname = next_arg;
00289             }
00290           format = XML_OUT;
00291           ofs.open(fname.c_str());
00292         }
00293       if ( arg == "--compiler"  ) { format = COMPILER_OUT; break; }
00294       if ( arg == "--cerr"      ) { target = 1; break; }
00295       if ( arg == "--xsl"       )
00296         {
00297           if (next_arg == "") xsl = "default.xsl"; 
00298           else                xsl = next_arg;
00299         }
00300       if ( arg == "--namespace" )
00301         {
00302           if (next_arg == "")
00303             {
00304               std::cerr << "no namespace specified" << std::endl;
00305               exit(1); 
00306             }
00307           else
00308             {
00309               xsl = next_arg;
00310             }
00311         }
00312       ++i;
00313     }
00314   CppUnit::TextUi::TestRunner runner;
00315   if ( ns.empty() )
00316     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00317   else
00318     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
00319   CppUnit::Outputter* outputter = 0;
00320   std::ostream* stream = target ? &std::cerr : &std::cout;
00321   switch ( format )
00322     {
00323     case TEXT_OUT :
00324       outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
00325       break;
00326     case XML_OUT :
00327       std::cout << "XML_OUT" << std::endl;
00328       outputter = new CppUnit::XmlOutputter(&runner.result(),
00329                                             ofs, "shift_jis");
00330       static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
00331       break;
00332     case COMPILER_OUT :
00333       outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
00334       break;
00335     }
00336   runner.setOutputter(outputter);
00337   runner.run();
00338   return 0; // runner.run() ? 0 : 1;
00339 }
00340 #endif // MAIN
00341 #endif // ObjectManager_cpp


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:05