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 #ifdef __QNX__
36 using std::fprintf;
37 using std::sprintf;
38 #endif
39 
40 namespace OS
41 {
42  class OSTests
43  : public CppUnit::TestFixture
44  {
46 // CPPUNIT_TEST(test_case0);
54 
55  private:
56 
57  public:
58 
63  {
64  }
65 
70  {
71  }
72 
76  virtual void setUp()
77  {
78 // opterr = 0;
79  }
80 
84  virtual void tearDown()
85  {
86  }
87 
88  /* test case */
89  void test_case0()
90  {
91  char * const vv[] = {
92  (char *)"me",
93  (char *)"-a",
94  (char *)"Hoge",
95  (char *)"-b",
96  (char *)"--Huga",
97  (char *)"-Foo",
98  (char *)"-d",
99  (char *)"Hey",
100  };
101  const char * opt = "a:bH::F:d";
102  coil::GetOpt go(7, vv, opt, 0);
103  int ii;
104  while ((ii = go()) != -1) {
105 // fprintf(stderr, "0x%X : optind[%d](%s), optarg : %s.\n", ii, optind, vv[optind], go.optarg);
106  }
107  fprintf(stderr, "ii : 0x%X.\n", ii);
108  }
109 
111  {
112  char * const vv[] = { (char *)"Hoge", (char *)"-a", (char *)"-b", (char *)"-c" };
113  const char * opt = "abc";
114  coil::GetOpt go(4, vv, opt, 0);
115  int result;
116 
117 // optind = 1; // reset searching getopt()
118 
119  result = go();
120  CPPUNIT_ASSERT_EQUAL((int)'a', result);
121  CPPUNIT_ASSERT(!go.optarg);
122 
123  result = go();
124  CPPUNIT_ASSERT_EQUAL((int)'b', result);
125  CPPUNIT_ASSERT(!go.optarg);
126 
127  result = go();
128  CPPUNIT_ASSERT_EQUAL((int)'c', result);
129  CPPUNIT_ASSERT(!go.optarg);
130 
131  result = go();
132  CPPUNIT_ASSERT_EQUAL(-1, result);
133  }
134 
136  {
137  char * const vv[] = { (char *)"Hoge", (char *)"-a", (char *)"-b", (char *)"-c" };
138  const char * opt = "ac";
139  coil::GetOpt go(4, vv, opt, 0);
140  int result;
141 
142 // optind = 1; // reset searching getopt()
143 
144  result = go();
145  CPPUNIT_ASSERT_EQUAL((int)'a', result);
146  CPPUNIT_ASSERT(!go.optarg);
147 
148  // opterr = 1; // <-- Remove comment out to display error message.
149  result = go();
150  CPPUNIT_ASSERT_EQUAL((int)'?', result);
151  CPPUNIT_ASSERT(!go.optarg);
152 
153  result = go();
154  CPPUNIT_ASSERT_EQUAL((int)'c', result);
155  CPPUNIT_ASSERT(!go.optarg);
156 
157  result = go();
158  CPPUNIT_ASSERT_EQUAL(-1, result);
159  }
160 
162  {
163  char * const vv[] = { (char *)"Hoge", (char *)"-a", (char *)"Huga", (char *)"-c" };
164  const char * opt = "ac";
165  coil::GetOpt go(4, vv, opt, 0);
166  int result;
167 
168 // optind = 1; // reset searching getopt()
169 
170  result = go();
171  CPPUNIT_ASSERT_EQUAL((int)'a', result);
172  CPPUNIT_ASSERT(!go.optarg);
173 
174  result = go();
175  CPPUNIT_ASSERT_EQUAL((int)'c', result);
176  CPPUNIT_ASSERT(!go.optarg);
177 
178  result = go();
179  CPPUNIT_ASSERT_EQUAL(-1, result);
180  }
181 
183  {
184  char * const vv[] = { (char *)"Hoge", (char *)"-a", (char *)"-Foe", (char *)"-c" };
185  const char * opt = "aF:c";
186  coil::GetOpt go(4, vv, opt, 0);
187  int result;
188 
189 // optind = 1; // reset searching getopt()
190 
191  result = go();
192  CPPUNIT_ASSERT_EQUAL((int)'a', result);
193  CPPUNIT_ASSERT(!go.optarg);
194 
195  result = go();
196  CPPUNIT_ASSERT_EQUAL((int)'F', result);
197  CPPUNIT_ASSERT_EQUAL('o', go.optarg[0]);
198  CPPUNIT_ASSERT_EQUAL('e', go.optarg[1]);
199  CPPUNIT_ASSERT_EQUAL('\0', go.optarg[2]);
200 
201  result = go();
202  CPPUNIT_ASSERT_EQUAL((int)'c', result);
203  CPPUNIT_ASSERT(!go.optarg);
204 
205  result = go();
206  CPPUNIT_ASSERT_EQUAL(-1, result);
207  }
208  /*
209  ---------------------------------------------------------------------------
210  This function tests the Task::open function and the coill::uname function.
211  -coil::uname
212  -coil::getpid
213  ---------------------------------------------------------------------------
214  */
215  void test_uname()
216  {
217  coil::utsname sysinfo;
218  int iret;
219  iret = coil::uname(&sysinfo);
220 
221  coil::pid_t pid = coil::getpid();
222  char pidc[8];
223  sprintf(pidc, "%d", pid);
224 
225  std::cout<<"manager.os.name:"<<sysinfo.sysname<<std::endl;
226  std::cout<<"manager.os.release:"<<sysinfo.release<<std::endl;
227  std::cout<<"manager.os.version:"<<sysinfo.version<<std::endl;
228  std::cout<<"manager.os.arch:"<<sysinfo.machine<<std::endl;
229  std::cout<<"manager.os.hostname:"<<sysinfo.nodename<<std::endl;
230  std::cout<<"manager.pid:"<<pidc<<std::endl;
231 
232 
233  CPPUNIT_ASSERT(iret == 0 );
234 
235  }
236  /*
237  ---------------------------------------------------------------------------
238  This function tests the Task::open function and the coill::getenv function.
239 
240  Please confirm environment variable and 'const char* config_file_env'
241  before executing this test.
242  ---------------------------------------------------------------------------
243  */
244  void test_getenv()
245  {
246  // Environment value to specify configuration file
247 // const char* config_file_env = "JAVA_HOME";
248  const char* config_file_env = "PATH";
249 
250  // Search rtc configuration file from environment variable
251  char* env = getenv(config_file_env);
252 
253  if (env != NULL)
254  {
255  std::cout<<env<<std::endl;
256  }
257  CPPUNIT_ASSERT(env != NULL );
258 
259 
260  }
261 
262  };
263 }; // namespace OS
264 
265 /*
266  * Register test suite
267  */
269 
270 #ifdef LOCAL_MAIN
271 int main(int argc, char* argv[])
272 {
273  CppUnit::TextUi::TestRunner runner;
274  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
275  CppUnit::Outputter* outputter =
276  new CppUnit::TextOutputter(&runner.result(), std::cout);
277  runner.setOutputter(outputter);
278  bool retcode = runner.run();
279  return !retcode;
280 }
281 #endif // MAIN
282 #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:135
virtual void tearDown()
Test finalization.
Definition: OSTests.cpp:84
void test_getenv()
Definition: OSTests.cpp:244
Definition: OSTests.cpp:40
GetOpt class.
void test_case0()
Definition: OSTests.cpp:89
void test_not_option()
Definition: OSTests.cpp:161
char sysname[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:51
CPPUNIT_TEST(test_simple_case)
void test_arg_option()
Definition: OSTests.cpp:182
void test_uname()
Definition: OSTests.cpp:215
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:110
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:598
CPPUNIT_TEST_SUITE_REGISTRATION(OS::OSTests)
virtual void setUp()
Test initialization.
Definition: OSTests.cpp:76
char machine[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:55
OSTests()
Constructor.
Definition: OSTests.cpp:62
CPPUNIT_TEST_SUITE_END()
char release[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:53
~OSTests()
Destructor.
Definition: OSTests.cpp:69


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