DynamicLibTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log$
00014  *
00015  */
00016 
00017 #ifndef DynamicLib_cpp
00018 #define DynamicLib_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 <coil/DynamicLib.h>
00027 
00032 namespace DynamicLib
00033 {
00034 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
00035 // #error WIN32!!
00036   const char * LibName = "DynamicLibTestDll.dll";
00037 //  const char * LibName = "coil.dll";
00038   const char * SymbolName = "ForExternTest";
00039 
00040 #else
00041 // #error POSIX.
00042 //  const char * LibName = "libPluginC.so";
00043   const char * LibName = ".libs/libDynamicLib.so";
00044 //  const char * LibName = "libcoil.so.0";
00045 //  const char * SymbolName = "svc_run";
00046   const char * SymbolName = "ForExternTest";
00047 //  const char * SymbolName = "PluginCInit";
00048 
00049 #endif
00050 
00051   class DynamicLibTests
00052    : public CppUnit::TestFixture
00053   {
00054     CPPUNIT_TEST_SUITE(DynamicLibTests);
00055     CPPUNIT_TEST(test_DynamicLib_1);
00056     CPPUNIT_TEST(test_DynamicLib_2);
00057     CPPUNIT_TEST(test_DynamicLib_3);
00058     CPPUNIT_TEST(test_DynamicLib_4);
00059     CPPUNIT_TEST(test_DynamicLib_open_failure);
00060     CPPUNIT_TEST(test_DynamicLib_open_and_close);
00061     CPPUNIT_TEST(test_DynamicLib_symbol_failure);
00062     CPPUNIT_TEST(test_DynamicLib_symbol);
00063     CPPUNIT_TEST(test_DynamicLib_error);
00064     CPPUNIT_TEST_SUITE_END();
00065   
00066   private:
00067     coil::DynamicLib * dl;
00068 
00069   public:
00070   
00074     DynamicLibTests()
00075     {
00076 
00077     }
00078     
00082     ~DynamicLibTests()
00083     {
00084     }
00085   
00089     virtual void setUp()
00090     {
00091         }
00092     
00096     virtual void tearDown()
00097     { 
00098     }
00099   
00100     /* test case */
00101 
00105         void test_DynamicLib_1()
00106         {
00107           /* 例外が起きなければOKとする */
00108       coil::DynamicLib * dl1 = new coil::DynamicLib(1);
00109       int result = dl1->open(LibName, COIL_DEFAULT_DYNLIB_MODE, 1);
00110       CPPUNIT_ASSERT_EQUAL(0, result);
00111       delete dl1;
00112         }
00113 
00117         void test_DynamicLib_2()
00118         {
00119           /* 例外が起きなければOKとする */
00120       coil::DynamicLib * dl1 = new coil::DynamicLib(LibName, COIL_DEFAULT_DYNLIB_MODE, 1);
00121           delete dl1;
00122         }
00123 
00127         void test_DynamicLib_3()
00128         {
00129           /* 例外が起きなければOKとする */
00130           coil::DynamicLib dl1(LibName, COIL_DEFAULT_DYNLIB_MODE, 1);
00131       coil::DynamicLib dl2(dl1);   // <--- ここで、std::bad_allocに落ちる Kz.080930 ← fix 081006.
00132         }
00133 
00137         void test_DynamicLib_4()
00138         {
00139           /* 例外が起きなければOKとする */
00140       coil::DynamicLib * dl1 = new coil::DynamicLib(LibName, COIL_DEFAULT_DYNLIB_MODE, 1);
00141        coil::DynamicLib dl2 = *dl1;   // <--- ここで、std::bad_allocに落ちる Kz.080930 ← fix 081006.
00142           delete dl1;
00143         }
00144 
00148         void test_DynamicLib_open_failure()
00149         {
00150       coil::DynamicLib * dl1 = new coil::DynamicLib(LibName, COIL_DEFAULT_DYNLIB_MODE, 1);
00151       int result = dl1->open("Hoge", COIL_DEFAULT_DYNLIB_MODE, 1);
00152       CPPUNIT_ASSERT_EQUAL(-1, result);
00153       delete dl1;
00154         }
00155 
00159         void test_DynamicLib_open_and_close()
00160         {
00161       coil::DynamicLib * dl1 = new coil::DynamicLib(LibName, COIL_DEFAULT_DYNLIB_MODE, 1);
00162       int result = dl1->open(LibName, COIL_DEFAULT_DYNLIB_MODE, 1);
00163       CPPUNIT_ASSERT_EQUAL(0, result);
00164 
00165       dl1->close();
00166       
00167       delete dl1;
00168         }
00169 
00174         void test_DynamicLib_symbol_failure()
00175         {
00176       coil::DynamicLib * dl1 = new coil::DynamicLib(LibName, COIL_DEFAULT_DYNLIB_MODE, 1);
00177       dl1->symbol("Hoge");
00178 
00179       int result = dl1->open(LibName, COIL_DEFAULT_DYNLIB_MODE, 1);
00180       CPPUNIT_ASSERT_EQUAL(0, result);
00181 
00182       char * p = (char *)dl1->symbol("HogeHoge");
00183 //      std::cout << "Error : " << dl1->error() << "." << std::endl ;
00184       CPPUNIT_ASSERT(0 == p);
00185 
00186       dl1->close();
00187       delete dl1;
00188         }
00189 
00194         void test_DynamicLib_symbol()
00195         {
00196       coil::DynamicLib * dl1 = new coil::DynamicLib(LibName, COIL_DEFAULT_DYNLIB_MODE, 1);
00197       dl1->symbol("Hoge");
00198 
00199       int result = dl1->open(LibName, COIL_DEFAULT_DYNLIB_MODE, 1);
00200       CPPUNIT_ASSERT_EQUAL(0, result);
00201 
00202       typedef int (*exec)();
00203 
00204       exec func = NULL;
00205       func = (exec)dl1->symbol(SymbolName);
00206 
00207       CPPUNIT_ASSERT(func);
00208 
00209       int ic = func();
00210 
00211       CPPUNIT_ASSERT_EQUAL((int)0xdeadbeef, ic);
00212 
00213       dl1->close();
00214       delete dl1;
00215         }
00216 
00221         void test_DynamicLib_error()
00222         {
00223 //      coil::DynamicLib * dl1 = new coil::DynamicLib(1);  // ↓どちらでも例外は起きない
00224       coil::DynamicLib * dl1 = new coil::DynamicLib(LibName, COIL_DEFAULT_DYNLIB_MODE, 1);
00225       dl1->error();
00226 
00227       dl1->open(LibName, COIL_DEFAULT_DYNLIB_MODE, 1);
00228       CPPUNIT_ASSERT(!dl1->error());    // 正常時には0を返す
00229 
00230       dl1->symbol("HogeHogeHoge");
00231       CPPUNIT_ASSERT(dl1->error());    // 非正常時には非0を返す
00232 //      std::cout << "error() : " << err2 << "." << std::endl;
00233 
00234       dl1->close();
00235       delete dl1;
00236         }
00237 
00238   };
00239 }; // namespace DynamicLib
00240 
00241 /*
00242  * Register test suite
00243  */
00244 CPPUNIT_TEST_SUITE_REGISTRATION(DynamicLib::DynamicLibTests);
00245 
00246 #ifdef LOCAL_MAIN
00247 int main(int argc, char* argv[])
00248 {
00249     CppUnit::TextUi::TestRunner runner;
00250     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00251     CppUnit::Outputter* outputter = 
00252       new CppUnit::TextOutputter(&runner.result(), std::cout);
00253     runner.setOutputter(outputter);
00254     bool retcode = runner.run();
00255     return !retcode;
00256 }
00257 #endif // MAIN
00258 #endif // DynamicLib_cpp


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