OSTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef OS_cpp
18 #define OS_cpp
19 
20 #include <cppunit/ui/text/TestRunner.h>
21 #include <cppunit/TextOutputter.h>
22 #include <cppunit/extensions/TestFactoryRegistry.h>
23 #include <cppunit/extensions/HelperMacros.h>
24 #include <cppunit/TestAssert.h>
25 
26 #include <coil/OS.h>
27 
33 /* Global Valiables */
34 
35 
36 namespace OS
37 {
38  class OSTests
39  : public CppUnit::TestFixture
40  {
42 // CPPUNIT_TEST(test_case0);
50 
51  private:
52 
53  public:
54 
59  {
60  }
61 
66  {
67  }
68 
72  virtual void setUp()
73  {
74 // opterr = 0;
75  }
76 
80  virtual void tearDown()
81  {
82  }
83 
84  /* test case */
85  void test_case0()
86  {
87  char * const vv[] = {
88  (char *)"me",
89  (char *)"-a",
90  (char *)"Hoge",
91  (char *)"-b",
92  (char *)"--Huga",
93  (char *)"-Foo",
94  (char *)"-d",
95  (char *)"Hey",
96  };
97  const char * opt = "a:bH::F:d";
98  coil::GetOpt go(7, vv, opt, 0);
99  int ii;
100  while ((ii = go()) != -1) {
101 // fprintf(stderr, "0x%X : optind[%d](%s), optarg : %s.\n", ii, optind, vv[optind], go.optarg);
102  }
103  fprintf(stderr, "ii : 0x%X.\n", ii);
104  }
105 
107  {
108  char * const vv[] = { (char *)"Hoge", (char *)"-a", (char *)"-b", (char *)"-c" };
109  const char * opt = "abc";
110  coil::GetOpt go(4, vv, opt, 0);
111  int result;
112 
113 // optind = 1; // reset searching getopt()
114 
115  result = go();
116  CPPUNIT_ASSERT_EQUAL((int)'a', result);
117  CPPUNIT_ASSERT(!go.optarg);
118 
119  result = go();
120  CPPUNIT_ASSERT_EQUAL((int)'b', result);
121  CPPUNIT_ASSERT(!go.optarg);
122 
123  result = go();
124  CPPUNIT_ASSERT_EQUAL((int)'c', result);
125  CPPUNIT_ASSERT(!go.optarg);
126 
127  result = go();
128  CPPUNIT_ASSERT_EQUAL(-1, result);
129  }
130 
132  {
133  char * const vv[] = { (char *)"Hoge", (char *)"-a", (char *)"-b", (char *)"-c" };
134  const char * opt = "ac";
135  coil::GetOpt go(4, vv, opt, 0);
136  int result;
137 
138 // optind = 1; // reset searching getopt()
139 
140  result = go();
141  CPPUNIT_ASSERT_EQUAL((int)'a', result);
142  CPPUNIT_ASSERT(!go.optarg);
143 
144  // opterr = 1; // <-- Remove comment out to display error message.
145  result = go();
146  CPPUNIT_ASSERT_EQUAL((int)'?', result);
147  CPPUNIT_ASSERT(!go.optarg);
148 
149  result = go();
150  CPPUNIT_ASSERT_EQUAL((int)'c', result);
151  CPPUNIT_ASSERT(!go.optarg);
152 
153  result = go();
154  CPPUNIT_ASSERT_EQUAL(-1, result);
155  }
156 
158  {
159  char * const vv[] = { (char *)"Hoge", (char *)"-a", (char *)"Huga", (char *)"-c" };
160  const char * opt = "ac";
161  coil::GetOpt go(4, vv, opt, 0);
162  int result;
163 
164 // optind = 1; // reset searching getopt()
165 
166  result = go();
167  CPPUNIT_ASSERT_EQUAL((int)'a', result);
168  CPPUNIT_ASSERT(!go.optarg);
169 
170  result = go();
171  CPPUNIT_ASSERT_EQUAL((int)'c', result);
172  CPPUNIT_ASSERT(!go.optarg);
173 
174  result = go();
175  CPPUNIT_ASSERT_EQUAL(-1, result);
176  }
177 
179  {
180  char * const vv[] = { (char *)"Hoge", (char *)"-a", (char *)"-Foe", (char *)"-c" };
181  const char * opt = "aF:c";
182  coil::GetOpt go(4, vv, opt, 0);
183  int result;
184 
185 // optind = 1; // reset searching getopt()
186 
187  result = go();
188  CPPUNIT_ASSERT_EQUAL((int)'a', result);
189  CPPUNIT_ASSERT(!go.optarg);
190 
191  result = go();
192  CPPUNIT_ASSERT_EQUAL((int)'F', result);
193  CPPUNIT_ASSERT_EQUAL('o', go.optarg[0]);
194  CPPUNIT_ASSERT_EQUAL('e', go.optarg[1]);
195  CPPUNIT_ASSERT_EQUAL('\0', go.optarg[2]);
196 
197  result = go();
198  CPPUNIT_ASSERT_EQUAL((int)'c', result);
199  CPPUNIT_ASSERT(!go.optarg);
200 
201  result = go();
202  CPPUNIT_ASSERT_EQUAL(-1, result);
203  }
204  /*
205  ---------------------------------------------------------------------------
206  This function tests the Task::open function and the coill::uname function.
207  -coil::uname
208  -coil::getpid
209  ---------------------------------------------------------------------------
210  */
211  void test_uname()
212  {
213  coil::utsname sysinfo;
214  int iret;
215  iret = coil::uname(&sysinfo);
216 
217  coil::pid_t pid = coil::getpid();
218  char pidc[8];
219  sprintf(pidc, "%d", pid);
220 
221  std::cout<<"manager.os.name:"<<sysinfo.sysname<<std::endl;
222  std::cout<<"manager.os.release:"<<sysinfo.release<<std::endl;
223  std::cout<<"manager.os.version:"<<sysinfo.version<<std::endl;
224  std::cout<<"manager.os.arch:"<<sysinfo.machine<<std::endl;
225  std::cout<<"manager.os.hostname:"<<sysinfo.nodename<<std::endl;
226  std::cout<<"manager.pid:"<<pidc<<std::endl;
227 
228 
229  CPPUNIT_ASSERT(iret == 0 );
230 
231  }
232  /*
233  ---------------------------------------------------------------------------
234  This function tests the Task::open function and the coill::getenv function.
235 
236  Please confirm environment variable and 'const char* config_file_env'
237  before executing this test.
238  ---------------------------------------------------------------------------
239  */
240  void test_getenv()
241  {
242  // Environment value to specify configuration file
243 // const char* config_file_env = "JAVA_HOME";
244  const char* config_file_env = "PATH";
245 
246  // Search rtc configuration file from environment variable
247  char* env = getenv(config_file_env);
248 
249  if (env != NULL)
250  {
251  std::cout<<env<<std::endl;
252  }
253  CPPUNIT_ASSERT(env != NULL );
254 
255 
256  }
257 
258  };
259 }; // namespace OS
260 
261 /*
262  * Register test suite
263  */
265 
266 #ifdef LOCAL_MAIN
267 int main(int argc, char* argv[])
268 {
269  CppUnit::TextUi::TestRunner runner;
270  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
271  CppUnit::Outputter* outputter =
272  new CppUnit::TextOutputter(&runner.result(), std::cout);
273  runner.setOutputter(outputter);
274  bool retcode = runner.run();
275  return !retcode;
276 }
277 #endif // MAIN
278 #endif // OS_cpp
char version[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:54
int main(int argc, char **argv)
void test_invalid_option()
Definition: OSTests.cpp:131
virtual void tearDown()
Test finalization.
Definition: OSTests.cpp:80
void test_getenv()
Definition: OSTests.cpp:240
Definition: OSTests.cpp:36
GetOpt class.
void test_case0()
Definition: OSTests.cpp:85
void test_not_option()
Definition: OSTests.cpp:157
char sysname[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:51
CPPUNIT_TEST(test_simple_case)
void test_arg_option()
Definition: OSTests.cpp:178
void test_uname()
Definition: OSTests.cpp:211
char nodename[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:52
env
ネームサーバー定義 env = RtmEnv(sys.argv, ["localhost:2809"]) list0 = env.name_space["localhost:2809"].list_obj() env.name_space[&#39;localhost:2809&#39;].rtc_handles.keys() ns = env.name_space[&#39;localhost:2809&#39;]
Definition: ConnectTest.py:25
::pid_t pid_t
Get process ID of the caller process.
Definition: ace/coil/OS.h:38
int uname(utsname *name)
Get System information.
Definition: ace/coil/OS.h:33
void test_simple_case()
Definition: OSTests.cpp:106
pid_t getpid()
Definition: ace/coil/OS.h:39
char * getenv(const char *name)
Get environment variable.
Definition: ace/coil/OS.h:48
CPPUNIT_TEST_SUITE(OSTests)
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
Definition: stringutil.cpp:593
CPPUNIT_TEST_SUITE_REGISTRATION(OS::OSTests)
virtual void setUp()
Test initialization.
Definition: OSTests.cpp:72
char machine[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:55
OSTests()
Constructor.
Definition: OSTests.cpp:58
CPPUNIT_TEST_SUITE_END()
char release[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:53
~OSTests()
Destructor.
Definition: OSTests.cpp:65


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:25:59