DayTimeConversionTest.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: DayTimeConversionTest.cpp 1895 2009-05-12 19:34:29Z afarris $"
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 //  Copyright 2006, The University of Texas at Austin
00022 //
00023 //============================================================================
00024 
00025 /* \example DayTimeToleranceTest.cpp
00026  * This test evaluates user defined comparison tolerance feature of DayTime.
00027  */
00028 
00029 #include <iostream>
00030 #include <stdlib.h>
00031 
00032 #include "StringUtils.hpp"
00033 #include "DayTime.hpp"
00034 
00035 using namespace std;
00036 using namespace gpstk;
00037 
00038 bool testConstructors(short year, short month, short dom,
00039                       short hour, short minute, double seconds,
00040                       short week, double sow, long zcount,
00041                       short hintYear,
00042                       double MJD) 
00043 {
00044    bool cumulativeResult = true;
00045 
00046    gpstk::DayTime case1(year,month,dom,
00047                         hour, minute, seconds); // Calendar constructor
00048    gpstk::DayTime case2(week%1024, sow, hintYear); // 10 bit GPS+SOW wk w/hint
00049    gpstk::DayTime case3(week%1024,zcount, hintYear); // 10 bit GPS+Z count w/hint
00050    gpstk::DayTime case4(MJD); // JD converted to MJD inline
00051 
00052    cout << case1.printf("  %-9B %02d, %Y %02H:%02M:%018.15f ");
00053 
00054    cumulativeResult = cumulativeResult && 
00055          (case1==case2) && (case1==case3) && (case1==case4);
00056 
00057    if (cumulativeResult)
00058       cout << "PASS" << endl;
00059    else
00060    {
00061       cout << " FAIL" << endl << "    Conversion results were: " << endl;
00062       cout << case1.printf("    %-10B %02d, %Y %02H:%02M:%02S   ") << endl;
00063       cout << case2.printf("    %-10B %02d, %Y %02H:%02M:%02S   ") << endl;
00064       cout << case3.printf("    %-10B %02d, %Y %02H:%02M:%02S   ") << endl;
00065       cout << case4.printf("    %-10B %02d, %Y %02H:%02M:%02S   ") << endl;
00066    }
00067 
00068    return (cumulativeResult);   
00069 }
00070 
00071 
00072 bool testMutators(short year, short month, short dom, 
00073                   short hour, short minute, double seconds,
00074                   short doy, double sod,
00075                   short week, double sow, long zcount,
00076                   short hintYear,
00077                   double MJD) 
00078 {
00079    bool cumulativeResult = true;
00080 
00081    gpstk::DayTime case1, case2, case3, case4, case5, case6;
00082    
00083    case1.setYMD(year, month, dom);
00084    case1.setHMS(hour, minute, seconds);
00085 
00086    case2.setYMDHMS(year, month, dom, hour, minute, seconds);
00087    
00088    case3.setYDoy(year, doy);
00089    case3.setSecOfDay(sod);
00090    
00091    case4.setGPS(week%1024, sow, hintYear);
00092 
00093    case5.setGPS(week%1024, zcount, hintYear);
00094 
00095    case6.setMJD(MJD);
00096    
00097    cout << case1.printf("  %-9B %02d, %Y %02H:%02M:%018.15f ");
00098    
00099    cumulativeResult = cumulativeResult && 
00100          (case1==case2) && (case1==case3) && (case1==case4) &&
00101          (case1.GPSzcount()==case5.GPSzcount()) && 
00102          (fabs(case1.MJD()-case6.MJD())<.001/DayTime::SEC_DAY);
00103 
00104    if (cumulativeResult)
00105       cout << "PASS" << endl;
00106    else
00107    {
00108       cout << "FAIL" << endl << "    Conversion results were: " << endl;
00109       cout << case1.printf("      %-10B %02d, %Y %02H:%02M:%018.15f  using setYMD, setHMS ") << endl;
00110       cout << case2.printf("      %-10B %02d, %Y %02H:%02M:%018.15f  using setYMDHMS ") << endl;
00111       cout << case3.printf("      %-10B %02d, %Y %02H:%02M:%018.15f  using setYDoy, setSecOfDay ") << endl;
00112       cout << case4.printf("      %-10B %02d, %Y %02H:%02M:%018.15f  using setGPS(wk, sow, hintYr) ") << endl;
00113       cout << case5.printf("      %-10B %02d, %Y %02H:%02M:%018.15f  using setGPS(wk, zcount, hintYr) ") << endl;
00114       cout << case6.printf("      %-10B %02d, %Y %02H:%02M:%018.15f  using setMJD ") << endl;
00115    }
00116 
00117    return (cumulativeResult);   
00118 }
00119    
00120 bool testRandomAccessors(DayTime &dtb, DayTime &dte, long ndates)
00121 {
00122    bool cumulativeResult=true;
00123 
00124       // Seed the random number generator
00125    gpstk::DayTime dt;
00126    unsigned int seed= (unsigned int) dt.GPSsow();
00127    srand(seed);
00128 
00129    unsigned long dayDiff = (unsigned long) 
00130                            ceil(dte.MJD() - dtb.MJD());
00131    
00132    for (int j=0;j<ndates;++j) // Loop through set of random days
00133    { 
00134       double dayDelta = floor( rand()*1./ RAND_MAX * dayDiff);
00135       double sodDelta = rand()*1./RAND_MAX;
00136       gpstk::DayTime testDate;
00137 
00138       testDate.setMJD(dtb.MJD()+dayDelta+sodDelta);
00139          
00140       short year =     testDate.year();
00141       short month =    testDate.month();
00142       short dom =      testDate.day();
00143       short hour =     testDate.hour();
00144       short minute =   testDate.minute();
00145       double seconds = testDate.second();
00146       short doy =      testDate.DOY();
00147       double sod =     testDate.DOYsecond();
00148       short week =     testDate.GPSfullweek()%1024; 
00149       double sow =     testDate.GPSsow();
00150       long zcount =    testDate.GPSzcount();
00151       short hintYear = testDate.year();
00152       double MJD =     testDate.MJD();
00153       cumulativeResult = cumulativeResult &&
00154          testMutators( year, month, dom, hour, minute, seconds,
00155                        doy, sod,
00156                        week, sow, zcount, hintYear,
00157                        MJD );
00158       } // End loop over random dates
00159    
00160    return cumulativeResult;
00161 }
00162 
00164 int main()
00165 {
00166    using gpstk::DayTime;
00167    
00168    try
00169    {
00170       DayTime::setDayTimeTolerance(DayTime::DAYTIME_TOLERANCE); // microsecond tolerance
00171 
00172       cout << endl;
00173       cout << "DayTime conversion tests." << endl << endl;
00174       
00175       cout << "All comparisons accurate to " << DayTime::DAYTIME_TOLERANCE;
00176       cout << " seconds." << endl << endl;
00177       
00178 
00179       bool cumulativeResult = true;
00180 
00181 
00182       cout << "Testing constructors using documented dates." << endl;
00183       cout << endl;
00184       
00185       
00186          // Directly from ICD-GPS-200
00187          // Beginning of GPS Time, as defined by ICD-GPS-200
00188       cumulativeResult = cumulativeResult &&
00189                              testConstructors(1980,1,6,0,0,0,
00190                                               0, 0., 0,
00191                                               1981,
00192                                               44244.);
00193       
00194          // From GPS Signals and Performan, Misra and Enge, p. 91
00195          // GPS 10 bit week rollover epoch
00196       cumulativeResult = cumulativeResult &&
00197                              testConstructors(1999,8,22,0,0,0,
00198                                               0, 0., 0,
00199                                               2000,
00200                                               51412.);
00201       
00202          // From Hoffman-Wellenhof, et al. 
00203          // The J2000 standard epoch
00204       cumulativeResult = cumulativeResult &&
00205                              testConstructors(2000,1,1,12,0,0,
00206                                               1042, 561600., 374400,
00207                                               2000,
00208                                               2451545 - 2400000.5);
00209 
00210 
00211       cout << endl << "Testing mutators using documented dates." << endl << endl;
00212       
00213          // Directly from ICD-GPS-200
00214          // Beginning of GPS Time, as defined by ICD-GPS-200
00215       cumulativeResult = cumulativeResult &&
00216                              testMutators(1980,1,6,0,0,0,
00217                                           6, 0.,
00218                                           0, 0., 0,
00219                                           1981,
00220                                           44244.);
00221       
00222          // From GPS Signals and Performan, Misra and Enge, p. 91
00223          // GPS 10 bit week rollover epoch
00224       cumulativeResult = cumulativeResult &&
00225                              testMutators(1999,8,22,0,0,0,
00226                                           234, 0.,
00227                                           0, 0., 0,
00228                                           2000,
00229                                           51412.);
00230       
00231          // From Hoffman-Wellenhof, et al. 
00232          // The J2000 standard epoch
00233       cumulativeResult = cumulativeResult &&
00234                              testMutators(2000,1,1,12,0,0,
00235                                           1, 43200.,
00236                                           1042, 561600., 374400,
00237                                           2000,
00238                                           2451545 - 2400000.5);
00239 
00240          // Random accessor/mutator tests
00241       cout << endl;
00242       cout << "Testing accessors and mutators using randomly generated dates.";
00243       cout << endl << endl;
00244       
00245       gpstk::DayTime dtBegin(1995,1,1,0,0,0), dtEnd(2015,1,1,0,0,0);
00246       cumulativeResult = cumulativeResult &&
00247                          testRandomAccessors( dtBegin, dtEnd, 20);
00248 
00249 
00250          // Wrap it up, folks
00251       cout << endl;
00252       cout << setw(34);
00253       cout << DayTime().printf("Completed on %B %d, %Y %H:%02M:%02S");
00254       cout << endl << endl;
00255       
00256       int ret = 0;
00257       if (cumulativeResult)
00258          cout << "All comparison tests PASSED." << endl;
00259       else
00260       {
00261          cout << "One ore more comparison tests FAILED." << endl;
00262          ret = 1;
00263       }
00264       
00265       return ret;
00266    }
00267    catch(gpstk::Exception& e)
00268    {
00269       cout << e << endl;
00270    }
00271    catch(...)
00272    {
00273       cout << "Some other exception thrown..." << endl;
00274    }
00275 
00276    cout << "Exiting with exceptions." << endl;
00277    return -1;
00278 }
00279 
00280 

Generated on Tue May 22 03:30:57 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1