OSTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log$
00014  *
00015  */
00016 
00017 #ifndef OS_cpp
00018 #define OS_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/OS.h>
00027 
00033 /* Global Valiables */
00034 
00035 
00036 namespace OS
00037 {
00038   class OSTests
00039    : public CppUnit::TestFixture
00040   {
00041     CPPUNIT_TEST_SUITE(OSTests);
00042 //    CPPUNIT_TEST(test_case0);
00043     CPPUNIT_TEST(test_simple_case);
00044     CPPUNIT_TEST(test_invalid_option);
00045     CPPUNIT_TEST(test_not_option);
00046     CPPUNIT_TEST(test_arg_option);
00047     CPPUNIT_TEST(test_uname);
00048     CPPUNIT_TEST(test_getenv);
00049     CPPUNIT_TEST_SUITE_END();
00050   
00051   private:
00052   
00053   public:
00054   
00058     OSTests()
00059     {
00060     }
00061     
00065     ~OSTests()
00066     {
00067     }
00068   
00072     virtual void setUp()
00073     {
00074 //      opterr = 0;
00075     }
00076     
00080     virtual void tearDown()
00081     { 
00082     }
00083   
00084     /* test case */
00085     void test_case0()
00086     {
00087       char * const vv[] = {
00088         (char *)"me",
00089         (char *)"-a",
00090         (char *)"Hoge",
00091         (char *)"-b",
00092         (char *)"--Huga",
00093         (char *)"-Foo",
00094         (char *)"-d",
00095         (char *)"Hey",
00096       };
00097       const char * opt = "a:bH::F:d";
00098       coil::GetOpt go(7, vv, opt, 0);
00099       int ii;
00100       while ((ii = go()) != -1) {
00101 //          fprintf(stderr, "0x%X : optind[%d](%s), optarg : %s.\n", ii, optind, vv[optind], go.optarg);
00102       }
00103       fprintf(stderr, "ii : 0x%X.\n", ii);
00104     }
00105 
00106     void test_simple_case()
00107     {
00108       char * const vv[] = { (char *)"Hoge", (char *)"-a", (char *)"-b", (char *)"-c" };
00109       const char * opt = "abc";
00110       coil::GetOpt go(4, vv, opt, 0);
00111       int result;
00112       
00113 //      optind = 1;  // reset searching getopt() 
00114 
00115       result = go();
00116       CPPUNIT_ASSERT_EQUAL((int)'a', result);
00117       CPPUNIT_ASSERT(!go.optarg);
00118       
00119       result = go();
00120       CPPUNIT_ASSERT_EQUAL((int)'b', result);
00121       CPPUNIT_ASSERT(!go.optarg);
00122       
00123       result = go();
00124       CPPUNIT_ASSERT_EQUAL((int)'c', result);
00125       CPPUNIT_ASSERT(!go.optarg);
00126       
00127       result = go();
00128       CPPUNIT_ASSERT_EQUAL(-1, result);
00129     }
00130 
00131     void test_invalid_option()
00132     {
00133       char * const vv[] = { (char *)"Hoge", (char *)"-a", (char *)"-b", (char *)"-c" };
00134       const char * opt = "ac";
00135       coil::GetOpt go(4, vv, opt, 0);
00136       int result;
00137       
00138 //      optind = 1;  // reset searching getopt() 
00139 
00140       result = go();
00141       CPPUNIT_ASSERT_EQUAL((int)'a', result);
00142       CPPUNIT_ASSERT(!go.optarg);
00143       
00144       // opterr = 1;    // <-- Remove comment out to display error message.
00145       result = go();
00146       CPPUNIT_ASSERT_EQUAL((int)'?', result);
00147       CPPUNIT_ASSERT(!go.optarg);
00148       
00149       result = go();
00150       CPPUNIT_ASSERT_EQUAL((int)'c', result);
00151       CPPUNIT_ASSERT(!go.optarg);
00152       
00153       result = go();
00154       CPPUNIT_ASSERT_EQUAL(-1, result);
00155     }
00156 
00157     void test_not_option()
00158     {
00159       char * const vv[] = { (char *)"Hoge", (char *)"-a", (char *)"Huga", (char *)"-c" };
00160       const char * opt = "ac";
00161       coil::GetOpt go(4, vv, opt, 0);
00162       int result;
00163       
00164 //      optind = 1;  // reset searching getopt() 
00165 
00166       result = go();
00167       CPPUNIT_ASSERT_EQUAL((int)'a', result);
00168       CPPUNIT_ASSERT(!go.optarg);
00169       
00170       result = go();
00171       CPPUNIT_ASSERT_EQUAL((int)'c', result);
00172       CPPUNIT_ASSERT(!go.optarg);
00173       
00174       result = go();
00175       CPPUNIT_ASSERT_EQUAL(-1, result);
00176     }
00177 
00178     void test_arg_option()
00179     {
00180       char * const vv[] = { (char *)"Hoge", (char *)"-a", (char *)"-Foe", (char *)"-c" };
00181       const char * opt = "aF:c";
00182       coil::GetOpt go(4, vv, opt, 0);
00183       int result;
00184       
00185 //      optind = 1;  // reset searching getopt() 
00186 
00187       result = go();
00188       CPPUNIT_ASSERT_EQUAL((int)'a', result);
00189       CPPUNIT_ASSERT(!go.optarg);
00190 
00191       result = go();
00192       CPPUNIT_ASSERT_EQUAL((int)'F', result);
00193       CPPUNIT_ASSERT_EQUAL('o', go.optarg[0]);
00194       CPPUNIT_ASSERT_EQUAL('e', go.optarg[1]);
00195       CPPUNIT_ASSERT_EQUAL('\0', go.optarg[2]);
00196 
00197       result = go();
00198       CPPUNIT_ASSERT_EQUAL((int)'c', result);
00199       CPPUNIT_ASSERT(!go.optarg);
00200       
00201       result = go();
00202       CPPUNIT_ASSERT_EQUAL(-1, result);
00203     }
00204     /*
00205     ---------------------------------------------------------------------------
00206     This function tests the Task::open function and the coill::uname function.
00207      -coil::uname
00208      -coil::getpid
00209     ---------------------------------------------------------------------------
00210     */
00211     void test_uname()
00212     {
00213       coil::utsname  sysinfo;
00214       int iret;
00215       iret = coil::uname(&sysinfo);
00216 
00217       coil::pid_t pid = coil::getpid();
00218       char pidc[8];
00219       sprintf(pidc, "%d", pid);
00220 
00221       std::cout<<"manager.os.name:"<<sysinfo.sysname<<std::endl;
00222       std::cout<<"manager.os.release:"<<sysinfo.release<<std::endl;
00223       std::cout<<"manager.os.version:"<<sysinfo.version<<std::endl;
00224       std::cout<<"manager.os.arch:"<<sysinfo.machine<<std::endl;
00225       std::cout<<"manager.os.hostname:"<<sysinfo.nodename<<std::endl;
00226       std::cout<<"manager.pid:"<<pidc<<std::endl;
00227       
00228 
00229       CPPUNIT_ASSERT(iret == 0 );
00230 
00231     }
00232     /*
00233     ---------------------------------------------------------------------------
00234     This function tests the Task::open function and the coill::getenv function.
00235 
00236     Please confirm environment variable and 'const char* config_file_env' 
00237     before executing this test.  
00238     ---------------------------------------------------------------------------
00239     */
00240     void test_getenv()
00241     {
00242       // Environment value to specify configuration file
00243 //      const char* config_file_env = "JAVA_HOME";
00244       const char* config_file_env = "PATH";
00245 
00246       // Search rtc configuration file from environment variable
00247       char* env = getenv(config_file_env);
00248 
00249       if (env != NULL)
00250       {
00251         std::cout<<env<<std::endl;
00252       }
00253       CPPUNIT_ASSERT(env != NULL );
00254 
00255       
00256     }
00257 
00258   };
00259 }; // namespace OS
00260 
00261 /*
00262  * Register test suite
00263  */
00264 CPPUNIT_TEST_SUITE_REGISTRATION(OS::OSTests);
00265 
00266 #ifdef LOCAL_MAIN
00267 int main(int argc, char* argv[])
00268 {
00269     CppUnit::TextUi::TestRunner runner;
00270     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00271     CppUnit::Outputter* outputter = 
00272       new CppUnit::TextOutputter(&runner.result(), std::cout);
00273     runner.setOutputter(outputter);
00274     bool retcode = runner.run();
00275     return !retcode;
00276 }
00277 #endif // MAIN
00278 #endif // OS_cpp


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