TimeValueTests.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00012 /*
00013  * $Log$
00014  *
00015  */
00016 
00017 #ifndef TimeValue_cpp
00018 #define TimeValue_cpp
00019 
00020 #include <iostream>
00021 #include <iomanip>
00022 #include <string>
00023 #include <stdio.h>
00024 #include <cppunit/ui/text/TestRunner.h>
00025 #include <cppunit/ui/text/TestRunner.h>
00026 #include <cppunit/TextOutputter.h>
00027 #include <cppunit/extensions/TestFactoryRegistry.h>
00028 #include <cppunit/extensions/HelperMacros.h>
00029 #include <cppunit/TestAssert.h>
00030 
00031 //#include <../../include/coil/TimeValue.h>
00032 #include <coil/TimeValue.h>
00033 
00038 namespace TimeValue
00039 {
00040   class TimeValueTests
00041    : public CppUnit::TestFixture
00042   {
00043     CPPUNIT_TEST_SUITE(TimeValueTests);
00044 //    CPPUNIT_TEST(test_case0);
00045     CPPUNIT_TEST(test_init);
00046     CPPUNIT_TEST(test_operatorEQ);
00047     CPPUNIT_TEST(test_operatorPLUS);
00048     CPPUNIT_TEST(test_operatorMINUS);
00049     CPPUNIT_TEST(test_sign);
00050     CPPUNIT_TEST(test_operatordouble);
00051     CPPUNIT_TEST_SUITE_END();
00052   
00053   private:
00054   
00055   public:
00056   
00060     TimeValueTests()
00061     {
00062     }
00063     
00067     ~TimeValueTests()
00068     {
00069     }
00070   
00074     virtual void setUp()
00075     {
00076     }
00077     
00081     virtual void tearDown()
00082     { 
00083     }
00084   
00085     /* test case */
00086     void test_case0()
00087     {
00088     }
00089     /* test case */
00090     /*
00091     ---------------------------------------------------------------------------
00092     This function tests the constructor of TimeValue.
00093      coil::TimeValue(double)
00094     Check that the value of the following tables is correctly set.
00095     ---------------------------------------------------------------------------
00096     */
00097     void test_init()
00098     {
00099         typedef struct data_struct {
00100             double indouble;
00101             long   outlong1;
00102             long   outlong2;
00103         } DATA_STRUCT;
00104 
00105         DATA_STRUCT datatable[] = {
00106             { 0.0 ,       0 , 0 },
00107             { 3.5 ,       3 , 500000 },
00108             { 3.555555 ,  3 , 555555 },
00109             { 3.5999994 , 3 , 599999 },
00110             { 3.5999995 , 3 , 600000 },
00111             { 0.999999 ,  0 , 999999 },
00112             { 1.000000 ,  1 , 0 },
00113             { 1.000001 ,  1 , 1 },
00114             { -1.0 ,      -1 , 0 },
00115             { -0.3 ,      0 , -300000 },
00116         };
00117 
00118         char cstr[256];
00119         short ic;
00120         long lsec,lusec;
00121         long ltvsec,ltvusec;
00122         double dbsec;
00123         coil::TimeValue *tv;
00124         for (ic=0; ic < (sizeof datatable/sizeof datatable[0]); ic++)
00125         {
00126             dbsec = datatable[ic].indouble;
00127             tv = new coil::TimeValue(dbsec);
00128             lsec = datatable[ic].outlong1;
00129             lusec = datatable[ic].outlong2;
00130             ltvsec = tv->sec();
00131             ltvusec = tv->usec();
00132             sprintf(cstr,"loop counter:%d  in:%f  sec():%ld  usec():%ld", ic, dbsec, ltvsec, ltvusec);
00133             CPPUNIT_ASSERT_MESSAGE(cstr, (ltvsec == lsec) & (ltvusec ==lusec) );
00134             delete tv;
00135         }
00136     }
00137     /*
00138     ---------------------------------------------------------------------------
00139     This function tests operator = of TimeValue.
00140     Check that the value of the following tables is correctly set
00141     ---------------------------------------------------------------------------
00142     */
00143     void test_operatorEQ()
00144     {
00145         typedef struct data_struct {
00146             double indouble;
00147             long   outlong1;
00148             long   outlong2;
00149         } DATA_STRUCT;
00150 
00151         DATA_STRUCT datatable[] = {
00152             { 0.0 ,       0 ,  0},
00153             { 3.5 ,       3 , 500000},
00154             { 3.555555 ,  3 , 555555},
00155             { 3.5999994 , 3 , 599999},
00156             { 3.5999995 , 3 , 600000},
00157             { -3.555555 ,  -3 , -555555},
00158             { -3.5999994 , -3 , -599999},
00159             { -3.5999995 , -3 , -600000},
00160         };
00161 
00162         char cstr[256];
00163         short ic;
00164         long lsec,lusec;
00165         long ltvsec,ltvusec;
00166         double dbsec;
00167         coil::TimeValue tv;
00168         for (ic=0; ic < (sizeof datatable/sizeof datatable[0]); ic++)
00169         {
00170             dbsec = datatable[ic].indouble;
00171             tv = dbsec; 
00172             lsec = datatable[ic].outlong1;
00173             lusec = datatable[ic].outlong2;
00174             ltvsec = tv.sec();
00175             ltvusec = tv.usec();
00176             sprintf(cstr,"loop counter:%d  in:%f  sec():%ld  usec():%ld ", ic, dbsec, ltvsec, ltvusec);
00177             CPPUNIT_ASSERT_MESSAGE(cstr, (ltvsec == lsec) & (ltvusec ==lusec));
00178         }
00179     }
00180     /*
00181     ---------------------------------------------------------------------------
00182     This function tests operator + of TimeValue.
00183     Check that the following table values are correctly added.
00184     ---------------------------------------------------------------------------
00185     */
00186     void test_operatorPLUS()
00187     {
00188         typedef struct data_struct {
00189             double indouble1;
00190             double indouble2;
00191             long   outlong1;
00192             long   outlong2;
00193         } DATA_STRUCT;
00194 
00195         DATA_STRUCT datatable[] = {
00196             { 0.0 ,       0.0 ,       0 , 0},
00197             { 3.5 ,       0.5 ,       4 , 0},
00198             { 3.599999 ,  0.000001 ,  3 , 600000},
00199             { 3.5999994 , 0.0000001 , 3 , 599999},
00200             { 3.5999995 , 0.0000001 , 3 , 600000},
00201             { -3.5 ,      -0.5 ,      -4 , 0},
00202             { -3.599999 , -0.000001 , -3 , -600000},
00203             { -3.5999994 ,-0.0000001 ,-3 , -599999},
00204             { -3.5999995 ,-0.0000001 ,-3 , -600000},
00205             { -3.5 ,      3.5 , 0 , 0},
00206             { -3.5 ,      3.499999 , 0 , -1},
00207             { -3.5 ,      3.500001 , 0 , 1},
00208             { 3.5 ,      -3.5 , 0 , 0},
00209             { 3.5 ,      -3.499999 , 0 , 1},
00210             { 3.5 ,      -3.500001 , 0 , -1},
00211         };
00212 
00213         char cstr[256];
00214         short ic;
00215         long lsec,lusec;
00216         long ltvsec,ltvusec;
00217         double dbsec1, dbsec2;
00218         coil::TimeValue tv1;
00219         coil::TimeValue tv2;
00220         for (ic=0; ic < (sizeof datatable/sizeof datatable[0]); ic++)
00221         {
00222             dbsec1 = datatable[ic].indouble1;
00223             tv1 = dbsec1; 
00224             dbsec2 = datatable[ic].indouble2;
00225             tv2 = dbsec2; 
00226             lsec = datatable[ic].outlong1;
00227             lusec = datatable[ic].outlong2;
00228             tv1 = tv1 + tv2;
00229             ltvsec = tv1.sec();
00230             ltvusec = tv1.usec();
00231             sprintf(cstr,"loop counter:%d  in1:%f  in2:%f  sec():%ld  usec():%ld ", ic, dbsec1, dbsec2, ltvsec, ltvusec);
00232             CPPUNIT_ASSERT_MESSAGE(cstr, (ltvsec == lsec) & (ltvusec ==lusec));
00233         }
00234     }
00235     /*
00236     ---------------------------------------------------------------------------
00237     This function tests operator - of TimeValue.
00238     Check that the following table values are correctly subtracted.
00239     ---------------------------------------------------------------------------
00240     */
00241     void test_operatorMINUS()
00242     {
00243         typedef struct data_struct {
00244             double indouble1;
00245             double indouble2;
00246             long   outlong1;
00247             long   outlong2;
00248         } DATA_STRUCT;
00249 
00250         DATA_STRUCT datatable[] = {
00251             { 0.0 ,       0.0 ,       0 , 0},
00252             { 3.5 ,       0.5 ,       3 , 0},
00253             { 3.6 ,       0.000001 ,  3 , 599999},
00254             { 3.6 ,       0.0000001 , 3 , 600000},
00255             { 3.5999995 , 0.0000001 , 3 , 600000},
00256             { 0.5 ,       3.5 ,       -3 , 0},
00257             { -3.6 ,       -0.000001 ,  -3 , -599999},
00258             { -3.6 ,       -0.0000001 , -3 , -600000},
00259             { -3.5999995 , -0.0000001 , -3 , -600000},
00260             { -3.5 ,      3.5 , -7 , 0},
00261             { -3.5 ,      3.499999 , -6 , -999999},
00262             { -3.5 ,      3.500001 , -7 , -1},
00263             { 3.5 ,      -3.5 , 7 , 0},
00264             { 3.5 ,      -3.499999 , 6 , 999999},
00265             { 3.5 ,      -3.500001 , 7 , 1},
00266         };
00267 
00268         char cstr[256];
00269         short ic;
00270         long lsec,lusec;
00271         long ltvsec,ltvusec;
00272         double dbsec1, dbsec2;
00273         coil::TimeValue tv1;
00274         coil::TimeValue tv2;
00275         for (ic=0; ic < (sizeof datatable/sizeof datatable[0]); ic++)
00276         {
00277             dbsec1 = datatable[ic].indouble1;
00278             tv1 = dbsec1; 
00279             dbsec2 = datatable[ic].indouble2;
00280             tv2 = dbsec2; 
00281             lsec = datatable[ic].outlong1;
00282             lusec = datatable[ic].outlong2;
00283             tv1 = tv1 - tv2;
00284             ltvsec = tv1.sec();
00285             ltvusec = tv1.usec();
00286             sprintf(cstr,"loop counter:%d  in1:%f  in2:%f  sec():%ld  usec():%ld ", ic, dbsec1, dbsec2, ltvsec, ltvusec);
00287             CPPUNIT_ASSERT_MESSAGE(cstr, (ltvsec == lsec) & (ltvusec ==lusec));
00288         }
00289     }
00290     /*
00291     ---------------------------------------------------------------------------
00292     This function tests TimeValue::sign.
00293     Check that the sign is correctly judged to the following table values.
00294     ---------------------------------------------------------------------------
00295     */
00296     void test_sign()
00297     {
00298         typedef struct data_struct {
00299             double indouble;
00300             int    outint;
00301         } DATA_STRUCT;
00302 
00303         DATA_STRUCT datatable[] = {
00304             { 0.0 ,       0 },
00305             { 3.5 ,       1 },
00306             { -3.5 ,      -1 },
00307             { 0.5 ,       1 },
00308             { -0.5 ,      -1 },
00309         };
00310 
00311         char cstr[256];
00312         short ic;
00313         int isign;
00314         int itvsign;
00315         double dbsec;
00316         coil::TimeValue tv;
00317         for (ic=0; ic < (sizeof datatable/sizeof datatable[0]); ic++)
00318         {
00319             dbsec = datatable[ic].indouble;
00320             tv = dbsec; 
00321             isign= datatable[ic].outint;
00322             itvsign = tv.sign();
00323             sprintf(cstr,"loop counter:%d  in:%f  sgin():%d", ic, dbsec, itvsign);
00324             CPPUNIT_ASSERT_MESSAGE(cstr, (isign == itvsign) );
00325         }
00326     }
00327     /*
00328     ---------------------------------------------------------------------------
00329     This function tests operator() of TimeValue.
00330     Check that the value of the following tables is correctly set.
00331     ---------------------------------------------------------------------------
00332     */
00333     void test_operatordouble()
00334     {
00335         typedef struct data_struct {
00336             double indouble;
00337             double outdouble;
00338         } DATA_STRUCT;
00339 
00340         DATA_STRUCT datatable[] = {
00341             { 0.0 ,       0.0},
00342             { 3.5 ,       3.5},
00343             { 3.555555 ,  3.555555},
00344             { 3.5999994 , 3.599999},
00345             { 3.5999995 , 3.6},
00346             { -3.555555 ,  -3.555555},
00347             { -3.5999994 , -3.599999},
00348             { -3.5999995 , -3.600000},
00349         };
00350 
00351         char cstr[256];
00352         short ic;
00353         double dbinsec;
00354         double dboutsec;
00355         double dbtvsec;
00356         coil::TimeValue tv;
00357         for (ic=0; ic < (sizeof datatable/sizeof datatable[0]); ic++)
00358         {
00359             dbinsec = datatable[ic].indouble;
00360             tv = dbinsec; 
00361             dboutsec = datatable[ic].outdouble;
00362             dbtvsec = (double)tv;
00363             sprintf(cstr,"loop counter:%d  in:%f  sec:%f ", ic, dbinsec, dbtvsec);
00364             CPPUNIT_ASSERT_MESSAGE(cstr, (dbtvsec == dboutsec) );
00365         }
00366     }
00367   };
00368 }; // namespace TimeValue
00369 
00370 /*
00371  * Register test suite
00372  */
00373 CPPUNIT_TEST_SUITE_REGISTRATION(TimeValue::TimeValueTests);
00374 
00375 #ifdef LOCAL_MAIN
00376 int main(int argc, char* argv[])
00377 {
00378     CppUnit::TextUi::TestRunner runner;
00379     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
00380     CppUnit::Outputter* outputter = 
00381       new CppUnit::TextOutputter(&runner.result(), std::cout);
00382     runner.setOutputter(outputter);
00383     bool retcode = runner.run();
00384     return !retcode;
00385 }
00386 #endif // MAIN
00387 #endif // TimeValue_cpp


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