gpszcounttest.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: gpszcounttest.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 
00031 #include <limits>
00032 #include "GPSZcount.hpp"
00033 #include "CommandOptionParser.hpp"
00034 
00035 using namespace std;
00036 using namespace gpstk;
00037 
00038 bool failure = false;
00039 int verboseLevel = 0;
00040 void say(const string& foo);
00041 void process(bool result);
00042 
00043 //
00044 // returns zero if all tests pass
00045 //
00046 int main (int argc, char *argv[])
00047 {
00048       // take care of command line options
00049    CommandOptionNoArg hOption('h', "help", "Display this helpful information.",
00050                               false);
00051    CommandOptionNoArg vOption('v', "verbose", "Increase the verbosity.", 
00052                               false);
00053    CommandOptionNoArg qOption('q', "quiet", "Only issue a return code.", 
00054                               false);
00055    CommandOptionParser cop("GPSZcount Test Suite");
00056    cop.parseOptions(argc, argv);
00057    if (cop.hasErrors())
00058    {
00059       cop.dumpErrors(cout);
00060       cop.displayUsage(cout);
00061       return 1;
00062    }
00063    
00064    if(hOption.getCount())
00065    {
00066       cop.displayUsage(cout);
00067       return 0;
00068    }
00069    
00070    verboseLevel = vOption.getCount();
00071    
00072    try
00073    {
00074          // Proceed with testing
00075       GPSZcount one, two;
00076       
00077       say("Equality Test: ");
00078       process (one == two);
00079       
00080       say("Assignment Test 1: ");
00081       one = GPSZcount(1000, 5);
00082       process(one.getWeek() == 1000 &&
00083               one.getZcount() == 5);
00084       
00085       say("Assignment Test 2: ");
00086       try
00087       {
00088             // this should throw an exception
00089          one.setZcount(GPSZcount::ZCOUNT_WEEK);
00090             // if we get here, it failed
00091          process(false);
00092       }
00093       catch (InvalidParameter& ip)
00094       {
00095          process(true);
00096       }
00097       
00098       say("Addition Test: ");
00099       one += GPSZcount::ZCOUNT_WEEK + 5 ;
00100       process(one.getWeek() == 1001 &&
00101               one.getZcount() == 10) ;
00102       
00103       say("Subtraction Test: ");
00104       one -= GPSZcount::ZCOUNT_WEEK + 5 ;
00105       process(one.getWeek() == 1000 &&
00106               one.getZcount() == 5) ;
00107       
00108       say("Invalid Week Test 1: ");
00109       one.setWeek(numeric_limits<short>::max());
00110       try
00111       {
00112             // this should throw an exception
00113          one += GPSZcount::ZCOUNT_WEEK;
00114             // if we get here, it failed
00115          process(false); 
00116       }
00117       catch (InvalidRequest& ir)
00118       {
00119          process(true);
00120       }
00121       
00122       say("Invalid Week Test 2: " );
00123       one.setWeek(0);
00124       try
00125       {
00126             // this should throw an exception
00127          one -= GPSZcount::ZCOUNT_WEEK;
00128             // if we get here, it failed
00129          process(false);
00130       }
00131       catch (InvalidRequest& ir)
00132       {
00133          process(true);
00134       }
00135       
00136       say("Less-Than Test: ");
00137       one.setWeek(1000).setZcount(GPSZcount::ZCOUNT_WEEK / 2) ;
00138       two = one + 5 ;
00139       process(two >= one) ;
00140       
00141       say("Greater-Than Test: ") ;
00142       two = one - 5 ;
00143       process(two <= one) ;
00144 
00145       say("SameTimeBlock Test 1: ") ;
00146          // set one to be at 4:20 and two at 4:00
00147       one.setWeek(1200).setZcount(4 * GPSZcount::ZCOUNT_HOUR 
00148                                   + 20 * GPSZcount::ZCOUNT_MINUTE) ;
00149       two.setWeek(1200).setZcount(4 * GPSZcount::ZCOUNT_HOUR) ;
00150          // test if one and two are between 4:00 and 5:00
00151       process(one.inSameTimeBlock(two, GPSZcount::ZCOUNT_HOUR)) ;
00152       
00153       say("SameTimeBlock Test 2: ") ;
00154          // test if one and two are NOT between 4:20 and 4:21
00155       process(! one.inSameTimeBlock(two, GPSZcount::ZCOUNT_MINUTE)) ;
00156 
00157       say("SameTimeBlock Test 3: ") ;
00158          // test if one and two are NOT between 4:15 and 5:15
00159       process(! one.inSameTimeBlock(two, GPSZcount::ZCOUNT_HOUR, 
00160                                     15 * GPSZcount::ZCOUNT_MINUTE)) ;
00161       
00162       say("Dump Test: ") ;
00163       if (verboseLevel)
00164       {
00165          cout << endl << one << endl;
00166          one.dump(cout, 1);
00167       }
00168 
00169       say("String Test: ");
00170       one.setWeek(1200).setZcount(123456);
00171       process(string(one) == string("1200w123456z"));
00172       
00173       if (qOption.getCount() == 0)
00174       {
00175             // Display the overall results
00176          cout << "GPSZcount Overall results: " 
00177               << (failure ? "Fail" : "Pass") << endl;
00178       }
00179       
00180       return failure ? 1 : 0 ;
00181    }
00182    catch(gpstk::Exception& exc)
00183    {
00184       cout << endl << endl << "Caught a " << exc.getName() << " exception:" 
00185            << endl << exc << endl ;
00186       return 1 ;
00187    }
00188 }
00189 
00190 void say(const string& foo)
00191 {
00192    if (verboseLevel)
00193       cout << foo << flush;
00194 }
00195 
00196 void process(bool result)
00197 {
00198    failure |= !result;
00199 
00200    if(verboseLevel)
00201    {
00202       cout << (result ? "Pass" : "Fail")  << endl;
00203    }
00204 }

Generated on Wed Feb 8 03:31:00 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1