TimeValueTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log$
14  *
15  */
16 
17 #ifndef TimeValue_cpp
18 #define TimeValue_cpp
19 
20 #include <iostream>
21 #include <iomanip>
22 #include <string>
23 #include <stdio.h>
24 #include <cppunit/ui/text/TestRunner.h>
25 #include <cppunit/ui/text/TestRunner.h>
26 #include <cppunit/TextOutputter.h>
27 #include <cppunit/extensions/TestFactoryRegistry.h>
28 #include <cppunit/extensions/HelperMacros.h>
29 #include <cppunit/TestAssert.h>
30 
31 //#include <../../include/coil/TimeValue.h>
32 #include <coil/TimeValue.h>
33 
38 namespace TimeValue
39 {
41  : public CppUnit::TestFixture
42  {
44 // CPPUNIT_TEST(test_case0);
52 
53  private:
54 
55  public:
56 
61  {
62  }
63 
68  {
69  }
70 
74  virtual void setUp()
75  {
76  }
77 
81  virtual void tearDown()
82  {
83  }
84 
85  /* test case */
86  void test_case0()
87  {
88  }
89  /* test case */
90  /*
91  ---------------------------------------------------------------------------
92  This function tests the constructor of TimeValue.
93  coil::TimeValue(double)
94  Check that the value of the following tables is correctly set.
95  ---------------------------------------------------------------------------
96  */
97  void test_init()
98  {
99  typedef struct data_struct {
100  double indouble;
101  long outlong1;
102  long outlong2;
103  } DATA_STRUCT;
104 
105  DATA_STRUCT datatable[] = {
106  { 0.0 , 0 , 0 },
107  { 3.5 , 3 , 500000 },
108  { 3.555555 , 3 , 555555 },
109  { 3.5999994 , 3 , 599999 },
110  { 3.5999995 , 3 , 600000 },
111  { 0.999999 , 0 , 999999 },
112  { 1.000000 , 1 , 0 },
113  { 1.000001 , 1 , 1 },
114  { -1.0 , -1 , 0 },
115  { -0.3 , 0 , -300000 },
116  };
117 
118  char cstr[256];
119  short ic;
120  long lsec,lusec;
121  long ltvsec,ltvusec;
122  double dbsec;
123  coil::TimeValue *tv;
124  for (ic=0; ic < (sizeof datatable/sizeof datatable[0]); ic++)
125  {
126  dbsec = datatable[ic].indouble;
127  tv = new coil::TimeValue(dbsec);
128  lsec = datatable[ic].outlong1;
129  lusec = datatable[ic].outlong2;
130  ltvsec = tv->sec();
131  ltvusec = tv->usec();
132  sprintf(cstr,"loop counter:%d in:%f sec():%ld usec():%ld", ic, dbsec, ltvsec, ltvusec);
133  CPPUNIT_ASSERT_MESSAGE(cstr, (ltvsec == lsec) & (ltvusec ==lusec) );
134  delete tv;
135  }
136  }
137  /*
138  ---------------------------------------------------------------------------
139  This function tests operator = of TimeValue.
140  Check that the value of the following tables is correctly set
141  ---------------------------------------------------------------------------
142  */
144  {
145  typedef struct data_struct {
146  double indouble;
147  long outlong1;
148  long outlong2;
149  } DATA_STRUCT;
150 
151  DATA_STRUCT datatable[] = {
152  { 0.0 , 0 , 0},
153  { 3.5 , 3 , 500000},
154  { 3.555555 , 3 , 555555},
155  { 3.5999994 , 3 , 599999},
156  { 3.5999995 , 3 , 600000},
157  { -3.555555 , -3 , -555555},
158  { -3.5999994 , -3 , -599999},
159  { -3.5999995 , -3 , -600000},
160  };
161 
162  char cstr[256];
163  short ic;
164  long lsec,lusec;
165  long ltvsec,ltvusec;
166  double dbsec;
167  coil::TimeValue tv;
168  for (ic=0; ic < (sizeof datatable/sizeof datatable[0]); ic++)
169  {
170  dbsec = datatable[ic].indouble;
171  tv = dbsec;
172  lsec = datatable[ic].outlong1;
173  lusec = datatable[ic].outlong2;
174  ltvsec = tv.sec();
175  ltvusec = tv.usec();
176  sprintf(cstr,"loop counter:%d in:%f sec():%ld usec():%ld ", ic, dbsec, ltvsec, ltvusec);
177  CPPUNIT_ASSERT_MESSAGE(cstr, (ltvsec == lsec) & (ltvusec ==lusec));
178  }
179  }
180  /*
181  ---------------------------------------------------------------------------
182  This function tests operator + of TimeValue.
183  Check that the following table values are correctly added.
184  ---------------------------------------------------------------------------
185  */
187  {
188  typedef struct data_struct {
189  double indouble1;
190  double indouble2;
191  long outlong1;
192  long outlong2;
193  } DATA_STRUCT;
194 
195  DATA_STRUCT datatable[] = {
196  { 0.0 , 0.0 , 0 , 0},
197  { 3.5 , 0.5 , 4 , 0},
198  { 3.599999 , 0.000001 , 3 , 600000},
199  { 3.5999994 , 0.0000001 , 3 , 599999},
200  { 3.5999995 , 0.0000001 , 3 , 600000},
201  { -3.5 , -0.5 , -4 , 0},
202  { -3.599999 , -0.000001 , -3 , -600000},
203  { -3.5999994 ,-0.0000001 ,-3 , -599999},
204  { -3.5999995 ,-0.0000001 ,-3 , -600000},
205  { -3.5 , 3.5 , 0 , 0},
206  { -3.5 , 3.499999 , 0 , -1},
207  { -3.5 , 3.500001 , 0 , 1},
208  { 3.5 , -3.5 , 0 , 0},
209  { 3.5 , -3.499999 , 0 , 1},
210  { 3.5 , -3.500001 , 0 , -1},
211  };
212 
213  char cstr[256];
214  short ic;
215  long lsec,lusec;
216  long ltvsec,ltvusec;
217  double dbsec1, dbsec2;
218  coil::TimeValue tv1;
219  coil::TimeValue tv2;
220  for (ic=0; ic < (sizeof datatable/sizeof datatable[0]); ic++)
221  {
222  dbsec1 = datatable[ic].indouble1;
223  tv1 = dbsec1;
224  dbsec2 = datatable[ic].indouble2;
225  tv2 = dbsec2;
226  lsec = datatable[ic].outlong1;
227  lusec = datatable[ic].outlong2;
228  tv1 = tv1 + tv2;
229  ltvsec = tv1.sec();
230  ltvusec = tv1.usec();
231  sprintf(cstr,"loop counter:%d in1:%f in2:%f sec():%ld usec():%ld ", ic, dbsec1, dbsec2, ltvsec, ltvusec);
232  CPPUNIT_ASSERT_MESSAGE(cstr, (ltvsec == lsec) & (ltvusec ==lusec));
233  }
234  }
235  /*
236  ---------------------------------------------------------------------------
237  This function tests operator - of TimeValue.
238  Check that the following table values are correctly subtracted.
239  ---------------------------------------------------------------------------
240  */
242  {
243  typedef struct data_struct {
244  double indouble1;
245  double indouble2;
246  long outlong1;
247  long outlong2;
248  } DATA_STRUCT;
249 
250  DATA_STRUCT datatable[] = {
251  { 0.0 , 0.0 , 0 , 0},
252  { 3.5 , 0.5 , 3 , 0},
253  { 3.6 , 0.000001 , 3 , 599999},
254  { 3.6 , 0.0000001 , 3 , 600000},
255  { 3.5999995 , 0.0000001 , 3 , 600000},
256  { 0.5 , 3.5 , -3 , 0},
257  { -3.6 , -0.000001 , -3 , -599999},
258  { -3.6 , -0.0000001 , -3 , -600000},
259  { -3.5999995 , -0.0000001 , -3 , -600000},
260  { -3.5 , 3.5 , -7 , 0},
261  { -3.5 , 3.499999 , -6 , -999999},
262  { -3.5 , 3.500001 , -7 , -1},
263  { 3.5 , -3.5 , 7 , 0},
264  { 3.5 , -3.499999 , 6 , 999999},
265  { 3.5 , -3.500001 , 7 , 1},
266  };
267 
268  char cstr[256];
269  short ic;
270  long lsec,lusec;
271  long ltvsec,ltvusec;
272  double dbsec1, dbsec2;
273  coil::TimeValue tv1;
274  coil::TimeValue tv2;
275  for (ic=0; ic < (sizeof datatable/sizeof datatable[0]); ic++)
276  {
277  dbsec1 = datatable[ic].indouble1;
278  tv1 = dbsec1;
279  dbsec2 = datatable[ic].indouble2;
280  tv2 = dbsec2;
281  lsec = datatable[ic].outlong1;
282  lusec = datatable[ic].outlong2;
283  tv1 = tv1 - tv2;
284  ltvsec = tv1.sec();
285  ltvusec = tv1.usec();
286  sprintf(cstr,"loop counter:%d in1:%f in2:%f sec():%ld usec():%ld ", ic, dbsec1, dbsec2, ltvsec, ltvusec);
287  CPPUNIT_ASSERT_MESSAGE(cstr, (ltvsec == lsec) & (ltvusec ==lusec));
288  }
289  }
290  /*
291  ---------------------------------------------------------------------------
292  This function tests TimeValue::sign.
293  Check that the sign is correctly judged to the following table values.
294  ---------------------------------------------------------------------------
295  */
296  void test_sign()
297  {
298  typedef struct data_struct {
299  double indouble;
300  int outint;
301  } DATA_STRUCT;
302 
303  DATA_STRUCT datatable[] = {
304  { 0.0 , 0 },
305  { 3.5 , 1 },
306  { -3.5 , -1 },
307  { 0.5 , 1 },
308  { -0.5 , -1 },
309  };
310 
311  char cstr[256];
312  short ic;
313  int isign;
314  int itvsign;
315  double dbsec;
316  coil::TimeValue tv;
317  for (ic=0; ic < (sizeof datatable/sizeof datatable[0]); ic++)
318  {
319  dbsec = datatable[ic].indouble;
320  tv = dbsec;
321  isign= datatable[ic].outint;
322  itvsign = tv.sign();
323  sprintf(cstr,"loop counter:%d in:%f sgin():%d", ic, dbsec, itvsign);
324  CPPUNIT_ASSERT_MESSAGE(cstr, (isign == itvsign) );
325  }
326  }
327  /*
328  ---------------------------------------------------------------------------
329  This function tests operator() of TimeValue.
330  Check that the value of the following tables is correctly set.
331  ---------------------------------------------------------------------------
332  */
334  {
335  typedef struct data_struct {
336  double indouble;
337  double outdouble;
338  } DATA_STRUCT;
339 
340  DATA_STRUCT datatable[] = {
341  { 0.0 , 0.0},
342  { 3.5 , 3.5},
343  { 3.555555 , 3.555555},
344  { 3.5999994 , 3.599999},
345  { 3.5999995 , 3.6},
346  { -3.555555 , -3.555555},
347  { -3.5999994 , -3.599999},
348  { -3.5999995 , -3.600000},
349  };
350 
351  char cstr[256];
352  short ic;
353  double dbinsec;
354  double dboutsec;
355  double dbtvsec;
356  coil::TimeValue tv;
357  for (ic=0; ic < (sizeof datatable/sizeof datatable[0]); ic++)
358  {
359  dbinsec = datatable[ic].indouble;
360  tv = dbinsec;
361  dboutsec = datatable[ic].outdouble;
362  dbtvsec = (double)tv;
363  sprintf(cstr,"loop counter:%d in:%f sec:%f ", ic, dbinsec, dbtvsec);
364  CPPUNIT_ASSERT_MESSAGE(cstr, (dbtvsec == dboutsec) );
365  }
366  }
367  };
368 }; // namespace TimeValue
369 
370 /*
371  * Register test suite
372  */
374 
375 #ifdef LOCAL_MAIN
376 int main(int argc, char* argv[])
377 {
378  CppUnit::TextUi::TestRunner runner;
379  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
380  CppUnit::Outputter* outputter =
381  new CppUnit::TextOutputter(&runner.result(), std::cout);
382  runner.setOutputter(outputter);
383  bool retcode = runner.run();
384  return !retcode;
385 }
386 #endif // MAIN
387 #endif // TimeValue_cpp
int main(int argc, char **argv)
long int sec() const
Get value of second time scale.
Definition: TimeValue.h:110
virtual void setUp()
Test initialization.
virtual void tearDown()
Test finalization.
CPPUNIT_TEST_SUITE(TimeValueTests)
int sign() const
Sign judgment.
Definition: TimeValue.cpp:166
TimeValue class.
Definition: TimeValue.h:40
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
Definition: stringutil.cpp:593
CPPUNIT_TEST_SUITE_REGISTRATION(TimeValue::TimeValueTests)
long int usec() const
Get value of micro second time scale.
Definition: TimeValue.h:131


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:56