TimeTest.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: TimeTest.cpp 3140 2012-06-18 15:03:02Z susancummins $"
00002 
00003 //============================================================================
00004 //
00005 //  This file is part of GPSTk, the GPS Toolkit.
00006 //
00007 //  The GPSTk is free software; you can redistribute it and/or modify
00008 //  it under the terms of the GNU Lesser General Public License as published
00009 //  by the Free Software Foundation; either version 2.1 of the License, or
00010 //  any later version.
00011 //
00012 //  The GPSTk is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU Lesser General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU Lesser General Public
00018 //  License along with GPSTk; if not, write to the Free Software Foundation,
00019 //  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
00020 //  
00021 //  Copyright 2009, The University of Texas at Austin
00022 //
00023 //============================================================================
00024 
00025 #include <iostream>
00026 
00027 #include <BasicFramework.hpp>
00028 
00029 #include <TimeString.hpp>
00030 #include <TimeConstants.hpp>
00031 
00032 #include <ANSITime.hpp>
00033 #include <CivilTime.hpp>
00034 #include <GPSEpochWeekSecond.hpp>
00035 #include <GPSWeekSecond.hpp>
00036 #include <GPSWeekZcount.hpp>
00037 #include <GPSZcount29.hpp>
00038 #include <GPSZcount32.hpp>
00039 #include <JulianDate.hpp>
00040 #include <MJD.hpp>
00041 #include <UnixTime.hpp>
00042 #include <YDSTime.hpp>
00043 #include <SystemTime.hpp>
00044 
00045 using namespace std;
00046 using namespace gpstk;
00047 using namespace gpstk::StringUtils;
00048 
00049 
00050    // The following constants are as captured from the timcvt program output.
00051    //
00052    //  Month/Day/Year                  7/26/2005
00053    //  Hour:Min:Sec                    17:59:42
00054    //  Modified Julian Date            53577.7497975392
00055    //  GPSweek DayOfWeek SecOfWeek     309 2 237582.507390
00056    //  FullGPSweek Zcount              1333 158388
00057    //  Year DayOfYear SecondOfDay      2005 207 64782.507390
00058    //  Unix_sec Unix_usec              1122400782 507390
00059    //  FullZcount                      162163380
00060 
00061 const int defaultMonth = 7;
00062 const int defaultDay = 26;
00063 const int defaultYear = 2005;
00064 const int defaultHour = 17;
00065 const int defaultMinute = 59;
00066 const int defaultSecond = 42;
00067 const double defaultSecondD = 42.507390;
00068 
00069 const long double defaultMJD = 53577.7497975392;
00070 const long double defaultJD = (defaultMJD + MJD_TO_JD);
00071 
00072 const int defaultWeek = 309;
00073 const int defaultDow = 2;
00074 const double defaultSow = 237582.507390;
00075 
00076 const int defaultFullWeek = 1333;
00077 const int defaultZcount = 158388;
00078 
00079 const int defaultDoy = 207;
00080 const double defaultSod = 64782.507390;
00081 
00082 const int defaultUnixSec = 1122400782;
00083 const int defaultUnixUsec = 507390;
00084 const int defaultFullZcount = 162163380;
00085 
00086 const int defaultEpoch = 1;
00087 const int defaultFullZcount32 = ( defaultEpoch << 29 ) | defaultFullZcount;
00088 
00089 class Test : public BasicFramework
00090 {
00091 public:
00092    Test( const string& progName ) 
00093          : BasicFramework( progName, "Test the New Time Classes!" ),
00094            ansi( defaultUnixSec ),
00095            civil( defaultYear, defaultMonth, defaultDay, defaultHour,
00096                   defaultMinute, defaultSecondD ),
00097            gews( defaultEpoch, defaultWeek, defaultSow ),
00098            gws( defaultFullWeek, defaultSow ),
00099            gwz( defaultFullWeek, defaultZcount ),
00100            gz29( defaultEpoch, defaultFullZcount ),
00101            gz32( defaultFullZcount32 ),
00102            jd( defaultJD ),
00103            mjd( defaultMJD ),
00104            ut( defaultUnixSec, defaultUnixUsec ),
00105            yds( defaultYear, defaultDoy, defaultSod )
00106    {}
00107    
00108    ANSITime ansi;
00109    CivilTime civil;
00110    GPSEpochWeekSecond gews;
00111    GPSWeekSecond gws;
00112    GPSWeekZcount gwz;
00113    GPSZcount29 gz29;
00114    GPSZcount32 gz32;
00115    JulianDate jd;
00116    MJD mjd;
00117    UnixTime ut;
00118    YDSTime yds;
00119    SystemTime st;
00120    
00121 protected:
00122    virtual void process();
00123 
00124    void dumpCommonTime( const TimeTag& t );
00125    
00126    bool simpleCopyTest();
00127    template<class T>
00128    bool isCopySuccess( T& t );
00129 
00130    bool conversionTest();
00131    template<class T>
00132    bool toCommonAndBack( T& t );
00133 
00134    bool simpleEqualityTest();
00135    template<class T>
00136    bool isEqual( T& t );
00137 
00138    bool systemTimeTest();
00139 
00140    bool simpleLessThanTest();
00141 
00142    bool otherTest();
00143 
00144 };
00145 
00146 void Test::process()
00147 {
00148    simpleCopyTest();
00149    simpleEqualityTest();
00150    conversionTest();
00151    systemTimeTest();
00152    simpleLessThanTest();
00153 //   otherTest();
00154 
00155    cout << "Test Processing Complete." << endl;
00156 }
00157 
00158 void Test::dumpCommonTime( const TimeTag& t )
00159 {
00160    long day, sod;
00161    double fsod;
00162 
00163    CommonTime( t.convertToCommonTime() ).get( day, sod, fsod );
00164 
00165    cout << t << " -> " << day << "d " << sod << "s " 
00166         << setprecision(15) << fsod << "f" << endl;
00167 }
00168 
00169 bool Test::simpleCopyTest()
00170 {
00171    cout << "Simple Copy Test:" << endl;
00172    cout << "ANSI:   " << ( isCopySuccess( ansi ) ? "PASS" : "FAIL" ) << endl;
00173    cout << "Civil:  " << ( isCopySuccess( civil ) ? "PASS" : "FAIL" ) << endl;
00174    cout << "GPSEWS: " << ( isCopySuccess( gews ) ? "PASS" : "FAIL" ) << endl;
00175    cout << "GPSWS:  " << ( isCopySuccess( gws ) ? "PASS" : "FAIL" ) << endl;
00176    cout << "GPSWZ:  " << ( isCopySuccess( gwz ) ? "PASS" : "FAIL" ) << endl;
00177    cout << "GZ29:   " << ( isCopySuccess( gz29 ) ? "PASS" : "FAIL" ) << endl;
00178    cout << "GZ32:   " << ( isCopySuccess( gz32 ) ? "PASS" : "FAIL" ) << endl;
00179    cout << "JD:     " << ( isCopySuccess( jd ) ? "PASS" : "FAIL" ) << endl;
00180    cout << "MJD:    " << ( isCopySuccess( mjd ) ? "PASS" : "FAIL" ) << endl;
00181    cout << "Unix:   " << ( isCopySuccess( ut ) ? "PASS" : "FAIL" ) << endl;
00182    cout << "YDS:    " << ( isCopySuccess( yds ) ? "PASS" : "FAIL" ) << endl;
00183    cout << endl;
00184    
00185    return true;
00186 }
00187 
00188 template<class T>
00189 bool Test::isCopySuccess( T& t )
00190 {
00191    T t1( t );
00192    if( t == t1 )
00193    {
00194       return true;
00195    }
00196    return false;
00197 }
00198 
00199 bool Test::conversionTest()
00200 {
00201    cout << "Conversion Test:" << endl;
00202    cout << "ANSI:   " << ( toCommonAndBack( ansi ) ? "PASS" : "FAIL" ) << endl;
00203    cout << "Civil:  " << ( toCommonAndBack( civil ) ? "PASS" : "FAIL" ) << endl;
00204    cout << "GPSEWS: " << ( toCommonAndBack( gews ) ? "PASS" : "FAIL" ) << endl;
00205    cout << "GPSWS:  " << ( toCommonAndBack( gws ) ? "PASS" : "FAIL" ) << endl;
00206    cout << "GPSWZ:  " << ( toCommonAndBack( gwz ) ? "PASS" : "FAIL" ) << endl;
00207    cout << "GZ29:   " << ( toCommonAndBack( gz29 ) ? "PASS" : "FAIL" ) << endl;
00208    cout << "GZ32:   " << ( toCommonAndBack( gz32 ) ? "PASS" : "FAIL" ) << endl;
00209    cout << "JD:     " << ( toCommonAndBack( jd ) ? "PASS" : "FAIL" ) << endl;
00210    cout << "MJD:    " << ( toCommonAndBack( mjd ) ? "PASS" : "FAIL" ) << endl;
00211    cout << "Unix:   " << ( toCommonAndBack( ut ) ? "PASS" : "FAIL" ) << endl;
00212    cout << "YDS:    " << ( toCommonAndBack( yds ) ? "PASS" : "FAIL" ) << endl;
00213    cout << endl;
00214 
00215    if( verboseLevel )
00216    {
00217       dumpCommonTime( ansi );
00218       dumpCommonTime( civil );
00219       dumpCommonTime( gews );
00220       dumpCommonTime( gws );
00221       dumpCommonTime( gwz );
00222       dumpCommonTime( gz29 );
00223       dumpCommonTime( gz32 );
00224       dumpCommonTime( jd );
00225       dumpCommonTime( mjd );
00226       dumpCommonTime( ut );
00227       dumpCommonTime( yds );
00228       cout << endl;
00229    }
00230 
00231    return true;
00232 }
00233 
00234 template<class T>
00235 bool Test::toCommonAndBack( T& t )
00236 {
00237    if( verboseLevel )
00238    {
00239       cout << "myval: " << t << endl;
00240    }
00241 
00242    T t1;
00243    t1.convertFromCommonTime( t.convertToCommonTime() );
00244    if( t != t1 )
00245    {
00246       cout << t << " != " << t1 << endl;
00247       return false;
00248    }
00249    return true;
00250 }
00251 
00252 bool Test::simpleEqualityTest()
00253 {
00254    cout << "Simple Equality Test" << endl;
00255    cout << "ANSI:   " << endl; isEqual( ansi );
00256    cout << "Civil:  " << endl; isEqual( civil );
00257    cout << "GPSEWS: " << endl; isEqual( gews );
00258    cout << "GPSWS:  " << endl; isEqual( gws );
00259    cout << "GPSWZ:  " << endl; isEqual( gwz );
00260    cout << "GZ29:   " << endl; isEqual( gz29 );
00261    cout << "GZ32:   " << endl; isEqual( gz32 );
00262    cout << "JD:     " << endl; isEqual( jd );
00263    cout << "MJD:    " << endl; isEqual( mjd );
00264    cout << "Unix:   " << endl; isEqual( ut );
00265    cout << "YDS:    " << endl; isEqual( yds );
00266    cout << endl;
00267 
00268 }
00269 
00270 template<class T>
00271 bool Test::isEqual( T& t )
00272 {
00273    cout << " to ANSI:   " << ( t == ANSITime( t ) ? "PASS" : "FAIL" )
00274         << endl;
00275    cout << " to Civil:  " << ( t == CivilTime( t ) ? "PASS" : "FAIL" )
00276         << endl;
00277    cout << " to GPSEWS: " << ( t == GPSEpochWeekSecond( t ) ? "PASS" : "FAIL" )
00278         << endl;
00279    cout << " to GPSWS:  " << ( t == GPSWeekSecond( t ) ? "PASS" : "FAIL" )
00280         << endl;
00281    cout << " to GPSWZ:  " << ( t == GPSWeekZcount( t ) ? "PASS" : "FAIL" )
00282         << endl;
00283    cout << " to GZ29:   " << ( t == GPSZcount29( t ) ? "PASS" : "FAIL" )
00284         << endl;
00285    cout << " to GZ32:   " << ( t == GPSZcount32( t ) ? "PASS" : "FAIL" )
00286         << endl;
00287    cout << " to JD:     " << ( t == JulianDate( t ) ? "PASS" : "FAIL" )
00288         << endl;
00289    cout << " to MJD:    " << ( t == MJD( t ) ? "PASS" : "FAIL" )
00290         << endl;
00291    cout << " to Unix:   " << ( t == UnixTime( t ) ? "PASS" : "FAIL" )
00292         << endl;
00293    cout << " to YDS:    " << ( t == YDSTime( t ) ? "PASS" : "FAIL" )
00294         << endl;
00295    cout << endl;
00296    
00297    return true;
00298 }   
00299 
00300 bool Test::systemTimeTest()
00301 {
00302    cout << "SystemTime:  " << st << endl
00303         << "  to ANSI:   " << ANSITime( st ) << endl
00304         << "  to Civil:  " << CivilTime( st ) << endl
00305         << "  to GPSEWS: " << GPSEpochWeekSecond( st ) << endl
00306         << "  to GPSWS:  " << GPSWeekSecond( st ) << endl
00307         << "  to GPSWZ:  " << GPSWeekZcount( st ) << endl
00308         << "  to GZ29:   " << GPSZcount29( st ) << endl
00309         << "  to GZ32:   " << GPSZcount32( st ) << endl
00310         << "  to JD:     " << JulianDate( st ) << endl
00311         << "  to MJD:    " << MJD( st ) << endl
00312         << "  to Unix:   " << UnixTime( st ) << endl
00313         << "  to YDS:    " << YDSTime( st ) << endl;
00314    
00315 }
00316 
00317 bool Test::simpleLessThanTest()
00318 {
00319    ansi = ANSITime( st );
00320    civil = CivilTime( st );
00321    gews = GPSEpochWeekSecond( st );
00322    gws = GPSWeekSecond( st );
00323    gwz = GPSWeekZcount( st );
00324    gz29 = GPSZcount29( st );
00325    gz32 = GPSZcount32( st );
00326    jd = JulianDate( st );
00327    mjd = MJD( st );
00328    ut = UnixTime( st );
00329    yds = YDSTime( st );
00330    
00331    CommonTime ct( st );
00332    ct += 5.0; // add five seconds
00333 
00334    cout << "SimpleLessThanTest: " << endl
00335         << " ANSI:   " << ( ansi < ct ? "PASS" : "FAIL" ) << endl
00336         << " Civil:  " << ( civil < ct ? "PASS" : "FAIL" ) << endl
00337         << " GPSEWS: " << ( gews < ct ? "PASS" : "FAIL" ) << endl
00338         << " GPSWS:  " << ( gws < ct ? "PASS" : "FAIL" ) << endl
00339         << " GPSWZ:  " << ( gwz < ct ? "PASS" : "FAIL" ) << endl
00340         << " GZ29:   " << ( gz29 < ct ? "PASS" : "FAIL" ) << endl
00341         << " GZ32:   " << ( gz32 < ct ? "PASS" : "FAIL" ) << endl
00342         << " JD:     " << ( jd < ct ? "PASS" : "FAIL" ) << endl
00343         << " MJD:    " << ( mjd < ct ? "PASS" : "FAIL" ) << endl
00344         << " Unix:   " << ( ut < ct ? "PASS" : "FAIL" ) << endl
00345         << " YDS:    " << ( yds < ct ? "PASS" : "FAIL" ) << endl;
00346 }
00347 
00348 bool Test::otherTest()
00349 {
00350    try
00351    {
00352       YDSTime one( 2005, 1, 0 ), two( 2005, 234, 5648.09 );
00353       
00354       cout << "one: " << one << endl
00355            << "two: " << two << endl;
00356       
00357       YDSTime tre( one );
00358       
00359       cout << "tre: " << tre << endl;
00360       
00361       if( one == tre )
00362       {
00363          cout << " one == tre " << endl;
00364       }
00365       else
00366       {
00367          cout << " one != tre " << endl;
00368       }
00369       
00370       cout << endl
00371            << "--- scanf test ---" << endl;
00372       
00373       string str( "2005 234 5648.09" );
00374       string fmt( "%Y %j %s" );
00375       
00376       cout << "str: " << str << endl
00377            << "fmt: " << fmt << endl;
00378       
00379       TimeTag::IdToValue info;
00380       TimeTag::getInfo( str, fmt, info );
00381       cout << "parsed info:" << endl;
00382       
00383       for( TimeTag::IdToValue::iterator i = info.begin(); 
00384            i != info.end(); i++ )
00385       {
00386          cout << "info[" << i->first << "] = <" << i->second << ">" << endl
00387               << "  asInt() -> " << asInt( i->second ) << endl;
00388       }
00389       
00390       if( tre.setFromInfo( info ) )
00391       {
00392          cout << "setFromInfo() ok" << endl;
00393       }
00394       else
00395       {
00396          cout << "setFromInfo() failed" << endl;
00397       }
00398       cout << "tre: " << tre << endl;   
00399       
00400       cout << "Altogether in one scanf() call: " << endl;
00401       two = one;
00402       cout << "two = one -> two: " << two << endl;
00403       two.scanf( str, fmt );
00404       cout << "two.scanf( str, fmt ) -> two: " << two << endl;
00405 
00406       cout << "two " << ( (two == tre) ? "==" : "!=" ) << " tre" << endl;
00407 
00408       cout << endl
00409            << "--- scanTime test ---" << endl;
00410       
00411       string str2( "1337 92048.09" );
00412       string fmt2( "%F %g" );
00413       
00414       cout << "two: " << two << endl
00415            << "tre: " << tre << endl
00416            << "str: " << str2 << endl
00417            << "fmt: " << fmt2 << endl;
00418       
00419       scanTime( tre, str, fmt );
00420       
00421       cout << "scanTime( tre, str, fmt);" << endl
00422            << "tre: " << tre << endl;
00423       
00424       return true;
00425    }
00426    catch( gpstk::Exception& exc )
00427    {
00428       cout << exc << endl;
00429    }
00430    
00431    return false;
00432 }
00433 
00434 int main (int argc, char *argv[])
00435 {
00436    try
00437    {
00438       Test t( argv[0] );
00439       
00440       if( !t.initialize( argc, argv ) )
00441       {
00442          return 0;
00443       }
00444       
00445       if( !t.run() )
00446       {
00447          return 1;
00448       }
00449 
00450       return 0;
00451    }
00452    catch( Exception& exc )
00453    {
00454       cout << exc << endl;
00455    }
00456    catch( exception& e )
00457    {
00458       cout << e.what() << endl;
00459    }
00460    catch( ... )
00461    {
00462       cout << "Caught an unknown exception." << endl;
00463    }
00464    return 1;
00465 }
00466 

Generated on Sun May 19 03:31:15 2013 for GPS ToolKit Software Library by  doxygen 1.3.9.1