timestamp.cpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Includes
00010 *****************************************************************************/
00011 
00012 #include <cstdlib>
00013 #include <gtest/gtest.h>
00014 #include "../../include/ecl/time/timestamp.hpp"
00015 
00016 /*****************************************************************************
00017 ** Platform Check
00018 *****************************************************************************/
00019 
00020 #ifdef ECL_HAS_TIMESTAMP
00021 
00022 /*****************************************************************************
00023 ** Using
00024 *****************************************************************************/
00025 
00026 using ecl::TimeStamp;
00027 
00028 /*****************************************************************************
00029 ** Variables
00030 *****************************************************************************/
00031 
00032 bool verbose = false;
00033 
00034 /*****************************************************************************
00035 ** Tests
00036 *****************************************************************************/
00037 
00038 TEST(TimeStampTests,construction) {
00039     TimeStamp time;
00040     SUCCEED();
00041     TimeStamp time_pair(3,123456789L);
00042     EXPECT_FLOAT_EQ(3.123456789,time_pair);
00043     TimeStamp time_double(3.00001);
00044     EXPECT_FLOAT_EQ(3.00001,time_double);
00045 }
00046 
00047 TEST(TimeStampTests,timestamps) {
00048     TimeStamp time;
00049     time.stamp();
00050     long t = time.sec();
00051     t = time.msec();
00052     t = time.usec();
00053     t = time.nsec();
00054     SUCCEED();
00055     time.stamp(3,1425);
00056     EXPECT_FLOAT_EQ(3.000001425,time);
00057     time.stamp(4.00000142);
00058     EXPECT_FLOAT_EQ(4.00000142,time);
00059 }
00060 
00061 TEST(TimeStampTests,copyConstruction) {
00062     TimeStamp time(3,100);
00063         TimeStamp time_copy(time);
00064         EXPECT_EQ(3,time_copy.sec());
00065         EXPECT_EQ(100,time_copy.nsec());
00066 }
00067 
00068 TEST(TimeStampTests,copyAssignment) {
00069     TimeStamp time(3,100);
00070         TimeStamp time_copy;
00071     time_copy = time;
00072         EXPECT_EQ(3,time_copy.sec());
00073         EXPECT_EQ(100,time_copy.nsec());
00074 }
00075 
00076 TEST(TimeStampTests,comparisonOperators) {
00077     TimeStamp time(3,100);
00078         TimeStamp time_copy(time);
00079         EXPECT_TRUE(time==time_copy);
00080         EXPECT_FALSE(time!=time_copy);
00081         EXPECT_TRUE(time<=time_copy);
00082         EXPECT_TRUE(time>=time_copy);
00083         EXPECT_FALSE(time<time_copy);
00084         EXPECT_FALSE(time>time_copy);
00085     time.stamp();
00086         EXPECT_FALSE(time==time_copy);
00087         EXPECT_TRUE(time!=time_copy);
00088         EXPECT_FALSE(time<=time_copy);
00089         EXPECT_TRUE(time>=time_copy);
00090         EXPECT_FALSE(time<time_copy);
00091         EXPECT_TRUE(time>time_copy);
00092 }
00093 
00094 TEST(TimeStampTests,mathematicalOperators) {
00095         TimeStamp time, time_copy;
00096     time.stamp(1,100100100L);
00097     time_copy.stamp(1,900100100L);
00098     time += time_copy;
00099     EXPECT_EQ(3,time.sec());
00100     EXPECT_EQ(200200,time.nsec());
00101     time.stamp(1,100100100L);
00102     time = time + time_copy;
00103     EXPECT_EQ(3,time.sec());
00104     EXPECT_EQ(200200,time.nsec());
00105     time.stamp(2,100100100L);
00106     time -= time_copy;
00107     EXPECT_EQ(0,time.sec());
00108     EXPECT_EQ(200000000,time.nsec());
00109     time.stamp(2,100100100L);
00110     time = time - time_copy;
00111     EXPECT_EQ(0,time.sec());
00112     EXPECT_EQ(200000000,time.nsec());
00113 }
00114 
00115 TEST(TimeStampTests,negatives) {
00116   if ( verbose ) {
00117     std::cout << "*****************************************************************************" << std::endl;
00118     std::cout << "* Negatives" << std::endl;
00119     std::cout << "*****************************************************************************" << std::endl;
00120   }
00121   /****************************************
00122   ** Variables
00123   ****************************************/
00124   ecl::TimeStamp time_2_3(2,300000000L), time_n_2_3(-2, -300000000L);
00125   ecl::TimeStamp time_1_3(1,300000000L), time_n_1_3(-1,-300000000L);
00126   ecl::TimeStamp time_0_3(0,300000000L), time_n_0_3(0,-300000000L);
00127   ecl::TimeStamp time_2_7(2,700000000L), time_n_2_7(-2,-700000000L);
00128   ecl::TimeStamp time_1_7(1,700000000L), time_n_1_7(-1,-700000000L);
00129   ecl::TimeStamp time_0_7(0,700000000L), time_n_0_7(0,-700000000L);
00130   /****************************************
00131   ** Constructor & Operator <<
00132   ****************************************/
00133   std::vector<TimeStamp> timestamps = { TimeStamp(1.7),
00134                                         TimeStamp(0.7),
00135                                         TimeStamp(-0.7),
00136                                         TimeStamp(-1.7),
00137   };
00138   if ( verbose ) {
00139     std::cout << "Operator <<\n  ";
00140     for ( const TimeStamp& timestamp : timestamps) {
00141       std::cout << timestamp << " ";
00142     }
00143     std::cout << std::endl;
00144   }
00145   EXPECT_EQ(1, timestamps[0].sec());
00146   EXPECT_EQ(700000000, timestamps[0].nsec());
00147   EXPECT_EQ(0, timestamps[1].sec());
00148   EXPECT_EQ(700000000, timestamps[1].nsec());
00149   EXPECT_EQ(0, timestamps[2].sec());
00150   EXPECT_EQ(-700000000, timestamps[2].nsec());
00151   EXPECT_EQ(-1, timestamps[3].sec());
00152   EXPECT_EQ(-700000000, timestamps[3].nsec());
00153 
00154   std::vector<float> double_representations;
00155   for ( const TimeStamp& timestamp : timestamps) {
00156     double_representations.push_back(timestamp);
00157   }
00158   if ( verbose ) {
00159     std::cout << "Operator double()\n  ";
00160     for ( const float& d : double_representations ) {
00161       std::cout << d << " ";
00162     }
00163     std::cout << std::endl;
00164   }
00165   EXPECT_FLOAT_EQ(1.7, double_representations[0]);
00166   EXPECT_FLOAT_EQ(0.7, double_representations[1]);
00167   EXPECT_FLOAT_EQ(-0.7, double_representations[2]);
00168   EXPECT_FLOAT_EQ(-1.7, double_representations[3]);
00169 
00170   /****************************************
00171   ** Operator -
00172   ****************************************/
00173   double_representations.clear();
00174   double_representations.push_back(ecl::TimeStamp(1.3) - ecl::TimeStamp(5.1));
00175   double_representations.push_back(ecl::TimeStamp(1.3) - ecl::TimeStamp(5.8));
00176   double_representations.push_back(ecl::TimeStamp(1.3) - ecl::TimeStamp(1.5));
00177   double_representations.push_back(ecl::TimeStamp(-1.3) - ecl::TimeStamp(1.5));
00178   double_representations.push_back(ecl::TimeStamp(-1.3) - ecl::TimeStamp(-0.8));
00179   double_representations.push_back(ecl::TimeStamp(-1.3) - ecl::TimeStamp(-5.8));
00180   if ( verbose ) {
00181     std::cout << "Operator -\n  ";
00182     std::cout << (ecl::TimeStamp(1.3) - ecl::TimeStamp(5.1)) << " ";
00183     std::cout << (ecl::TimeStamp(1.3) - ecl::TimeStamp(5.8)) << " ";
00184     std::cout << (ecl::TimeStamp(1.3) - ecl::TimeStamp(1.5)) << " ";
00185     std::cout << (ecl::TimeStamp(-1.3) - ecl::TimeStamp(1.5)) << " ";
00186     std::cout << (ecl::TimeStamp(-1.3) - ecl::TimeStamp(-0.8)) << " ";
00187     std::cout << (ecl::TimeStamp(-1.3) - ecl::TimeStamp(-5.8)) << " ";
00188     std::cout << std::endl;
00189   }
00190   EXPECT_FLOAT_EQ(-3.8, double_representations[0]);
00191   EXPECT_FLOAT_EQ(-4.5, double_representations[1]);
00192   EXPECT_FLOAT_EQ(-0.2, double_representations[2]);
00193   EXPECT_FLOAT_EQ(-2.8, double_representations[3]);
00194   EXPECT_FLOAT_EQ(-0.5, double_representations[4]);
00195   EXPECT_FLOAT_EQ(4.5, double_representations[5]);
00196 
00197   /****************************************
00198   ** Operator -=
00199   ****************************************/
00200   double_representations.clear();
00201   ecl::TimeStamp t;
00202   t = time_1_3; t -= ecl::TimeStamp(5.1);
00203   double_representations.push_back(t);
00204   t = time_1_3; t -= ecl::TimeStamp(5.8);
00205   double_representations.push_back(t);
00206   t = time_1_3; t -= ecl::TimeStamp(1.5);
00207   double_representations.push_back(t);
00208   t = time_n_1_3; t -= ecl::TimeStamp(1.5);
00209   double_representations.push_back(t);
00210   t = time_n_1_3; t -= ecl::TimeStamp(-0.8);
00211   double_representations.push_back(t);
00212   t = time_n_1_3; t -= ecl::TimeStamp(-5.8);
00213   double_representations.push_back(t);
00214   if ( verbose ) {
00215     std::cout << "Operator -=\n  ";
00216     for (const double& d : double_representations) {
00217       std::cout << d << " ";
00218     }
00219     std::cout << std::endl;
00220   }
00221   EXPECT_FLOAT_EQ(-3.8, double_representations[0]);
00222   EXPECT_FLOAT_EQ(-4.5, double_representations[1]);
00223   EXPECT_FLOAT_EQ(-0.2, double_representations[2]);
00224   EXPECT_FLOAT_EQ(-2.8, double_representations[3]);
00225   EXPECT_FLOAT_EQ(-0.5, double_representations[4]);
00226   EXPECT_FLOAT_EQ(4.5, double_representations[5]);
00227   /****************************************
00228   ** Operator +
00229   ****************************************/
00230   double_representations.clear(); timestamps.clear();
00231   double_representations.push_back(time_1_3 + time_n_2_3); timestamps.push_back(time_1_3 + time_n_2_3);
00232   double_representations.push_back(time_1_3 + time_n_2_7); timestamps.push_back(time_1_3 + time_n_2_7);
00233   double_representations.push_back(time_1_3 + time_n_1_7); timestamps.push_back(time_1_3 + time_n_1_7);
00234   double_representations.push_back(time_0_3 + time_n_0_7); timestamps.push_back(time_0_3 + time_n_0_7);
00235   double_representations.push_back(time_1_3 + time_n_0_7); timestamps.push_back(time_1_3 + time_n_0_7);
00236   double_representations.push_back(time_2_3 + time_n_0_7); timestamps.push_back(time_2_3 + time_n_0_7);
00237   double_representations.push_back(time_n_1_3 + time_n_1_7); timestamps.push_back(time_n_1_3 + time_n_1_7);
00238   double_representations.push_back(time_n_1_7 + time_n_1_7); timestamps.push_back(time_n_1_7 + time_n_1_7);
00239   if ( verbose ) {
00240     std::cout << "Operator +" << std::endl;
00241     std::cout << "  1.3 + (-2.3): " << double_representations[0] << std::endl;
00242     std::cout << "  1.3 + (-2.7): " << double_representations[1] << std::endl;
00243     std::cout << "  1.3 + (-1.7): " << double_representations[2] << std::endl;
00244     std::cout << "  0.3 + (-0.7): " << double_representations[3] << std::endl;
00245     std::cout << "  1.3 + (-0.7): " << double_representations[4] << std::endl;
00246     std::cout << "  2.3 + (-0.7): " << double_representations[5] << std::endl;
00247     std::cout << " -1.3 + (-1.7): " << double_representations[6] << std::endl;
00248     std::cout << " -1.7 + (-1.7): " << double_representations[7] << std::endl;
00249   }
00250   EXPECT_FLOAT_EQ(-1.0, double_representations[0]); EXPECT_EQ(0, timestamps[0].nsec());
00251   EXPECT_FLOAT_EQ(-1.4, double_representations[1]); EXPECT_EQ(-400000000, timestamps[1].nsec());
00252   EXPECT_FLOAT_EQ(-0.4, double_representations[2]); EXPECT_EQ(-400000000, timestamps[2].nsec());
00253   EXPECT_FLOAT_EQ(-0.4, double_representations[3]);  EXPECT_EQ(-400000000, timestamps[3].nsec());
00254   EXPECT_FLOAT_EQ(0.6, double_representations[4]);  EXPECT_EQ(600000000, timestamps[4].nsec());
00255   EXPECT_FLOAT_EQ(1.6, double_representations[5]);  EXPECT_EQ(600000000, timestamps[5].nsec());
00256   EXPECT_FLOAT_EQ(-3.0, double_representations[6]); EXPECT_EQ(000000000, timestamps[6].nsec());
00257   EXPECT_FLOAT_EQ(-3.4, double_representations[7]); EXPECT_EQ(-400000000, timestamps[7].nsec());
00258 
00259   /****************************************
00260   ** Operator +=
00261   ****************************************/
00262   double_representations.clear(); timestamps.clear();
00263   t = time_1_3; t += time_n_2_3;
00264   double_representations.push_back(t); timestamps.push_back(t);
00265   t = time_1_3; t += time_n_2_7;
00266   double_representations.push_back(t); timestamps.push_back(t);
00267   t = time_1_3; t += time_n_1_7;
00268   double_representations.push_back(t); timestamps.push_back(t);
00269   t = time_0_3; t += time_n_0_7;
00270   double_representations.push_back(t); timestamps.push_back(t);
00271   t = time_1_3; t += time_n_0_7;
00272   double_representations.push_back(t); timestamps.push_back(t);
00273   t = time_2_3; t += time_n_0_7;
00274   double_representations.push_back(t); timestamps.push_back(t);
00275   t = time_n_1_3; t += time_n_1_7;
00276   double_representations.push_back(t); timestamps.push_back(t);
00277   t = time_n_1_7; t += time_n_1_7;
00278   double_representations.push_back(t); timestamps.push_back(t);
00279   if ( verbose ) {
00280     std::cout << "Operator +=" << std::endl;
00281     std::cout << "  1.3 + (-2.3): " << double_representations[0] << std::endl;
00282     std::cout << "  1.3 + (-2.7): " << double_representations[1] << std::endl;
00283     std::cout << "  1.3 + (-1.7): " << double_representations[2] << std::endl;
00284     std::cout << "  0.3 + (-0.7): " << double_representations[3] << std::endl;
00285     std::cout << "  1.3 + (-0.7): " << double_representations[4] << std::endl;
00286     std::cout << "  2.3 + (-0.7): " << double_representations[5] << std::endl;
00287     std::cout << " -1.3 + (-1.7): " << double_representations[6] << std::endl;
00288     std::cout << " -1.7 + (-1.7): " << double_representations[7] << std::endl;
00289   }
00290   EXPECT_FLOAT_EQ(-1.0, double_representations[0]); EXPECT_EQ(0, timestamps[0].nsec());
00291   EXPECT_FLOAT_EQ(-1.4, double_representations[1]); EXPECT_EQ(-400000000, timestamps[1].nsec());
00292   EXPECT_FLOAT_EQ(-0.4, double_representations[2]); EXPECT_EQ(-400000000, timestamps[2].nsec());
00293   EXPECT_FLOAT_EQ(-0.4, double_representations[3]);  EXPECT_EQ(-400000000, timestamps[3].nsec());
00294   EXPECT_FLOAT_EQ(0.6, double_representations[4]);  EXPECT_EQ(600000000, timestamps[4].nsec());
00295   EXPECT_FLOAT_EQ(1.6, double_representations[5]);  EXPECT_EQ(600000000, timestamps[5].nsec());
00296   EXPECT_FLOAT_EQ(-3.0, double_representations[6]); EXPECT_EQ(000000000, timestamps[6].nsec());
00297   EXPECT_FLOAT_EQ(-3.4, double_representations[7]); EXPECT_EQ(-400000000, timestamps[7].nsec());
00298 
00299   /****************************************
00300   ** Operator <
00301   ****************************************/
00302   std::vector<bool> comparisons;
00303   comparisons.push_back(time_n_1_3 < time_n_2_3);
00304   comparisons.push_back(time_n_1_3 < time_2_3);
00305   comparisons.push_back(time_1_3 < time_n_2_3);
00306   comparisons.push_back(time_1_3 < time_2_3);
00307   comparisons.push_back(time_n_2_3 < time_n_1_3);
00308   comparisons.push_back(time_n_2_3 < time_1_3);
00309   comparisons.push_back(time_2_3 < time_n_1_3);
00310   comparisons.push_back(time_2_3 < time_1_3);
00311   comparisons.push_back(time_n_0_3 < time_n_0_7);
00312   comparisons.push_back(time_n_0_3 < time_0_7);
00313   comparisons.push_back(time_0_3 < time_n_0_7);
00314   comparisons.push_back(time_0_3 < time_0_7);
00315   comparisons.push_back(time_n_0_7 < time_n_0_3);
00316   comparisons.push_back(time_n_0_7 < time_0_3);
00317   comparisons.push_back(time_0_7 < time_n_0_3);
00318   comparisons.push_back(time_0_7 < time_0_3);
00319   if ( verbose ) {
00320     std::cout << "Operator <" << std::endl;
00321     std::cout << "  -1.3 < -2.3: " << (comparisons[0] ? std::string("true") : std::string("false")) << std::endl;
00322     std::cout << "  -1.3 <  2.3: " << (comparisons[1] ? std::string("true") : std::string("false")) << std::endl;
00323     std::cout << "   1.3 < -2.3: " << (comparisons[2] ? std::string("true") : std::string("false")) << std::endl;
00324     std::cout << "   1.3 <  2.3: " << (comparisons[3] ? std::string("true") : std::string("false")) << std::endl;
00325     std::cout << "  -2.3 < -1.3: " << (comparisons[4] ? std::string("true") : std::string("false")) << std::endl;
00326     std::cout << "  -2.3 <  1.3: " << (comparisons[5] ? std::string("true") : std::string("false")) << std::endl;
00327     std::cout << "   2.3 < -1.3: " << (comparisons[6] ? std::string("true") : std::string("false")) << std::endl;
00328     std::cout << "   2.3 <  1.3: " << (comparisons[7] ? std::string("true") : std::string("false")) << std::endl;
00329     std::cout << "  -0.3 < -0.7: " << (comparisons[8] ? std::string("true") : std::string("false")) << std::endl;
00330     std::cout << "  -0.3 <  0.7: " << (comparisons[9] ? std::string("true") : std::string("false")) << std::endl;
00331     std::cout << "   0.3 < -0.7: " << (comparisons[10] ? std::string("true") : std::string("false")) << std::endl;
00332     std::cout << "   0.3 <  0.7: " << (comparisons[11] ? std::string("true") : std::string("false")) << std::endl;
00333     std::cout << "  -0.7 < -0.3: " << (comparisons[12] ? std::string("true") : std::string("false")) << std::endl;
00334     std::cout << "  -0.7 <  0.3: " << (comparisons[13] ? std::string("true") : std::string("false")) << std::endl;
00335     std::cout << "   0.7 < -0.3: " << (comparisons[14] ? std::string("true") : std::string("false")) << std::endl;
00336     std::cout << "   0.7 <  0.3: " << (comparisons[15] ? std::string("true") : std::string("false")) << std::endl;
00337   }
00338   std::vector<bool> expected = { false, true, false, true, true, true, false, false, false, true, false, true, true, true, false, false };
00339   for ( unsigned int i = 0; i < expected.size(); ++i ) {
00340     if ( expected[i] ) {
00341       EXPECT_TRUE(comparisons[i]);
00342     } else {
00343       EXPECT_FALSE(comparisons[i]);
00344     }
00345   }
00346   // TODO other comparison tests - didn't have to change that code for negative numbers
00347 }
00348 
00349 #endif /* ECL_HAS_TIMESTAMP */
00350 
00351 /*****************************************************************************
00352 ** Main program
00353 *****************************************************************************/
00354 
00355 int main(int argc, char **argv) {
00356 
00357   testing::InitGoogleTest(&argc,argv);
00358   return RUN_ALL_TESTS();
00359 }


ecl_time
Author(s): Daniel Stonier
autogenerated on Mon Jul 3 2017 02:21:18