//============================================================ // Test Suite: timeSystemTest() //============================================================ // // Test will check the TimeSystem comparisons when using // the comparison operators. // //============================================================ int timeSystemTest( void ) { TestUtil testFramework( "CommonTime", "Differing TimeSystem == Operator", __FILE__, __LINE__ ); CommonTime GPS1; GPS1.set( 1000, 200, 0.2, TimeSystem(2) ); CommonTime GPS2; GPS2.set( 100, 200, 0.2, TimeSystem(2) ); CommonTime UTC1; UTC1.set( 1000, 200, 0.2, TimeSystem(5) ); CommonTime UNKNOWN; UNKNOWN.set( 1000, 200, 0.2, TimeSystem(0) ); CommonTime ANY; ANY.set( 1000, 200, 0.2, TimeSystem(1) ); //---------------------------------------- // ??? //---------------------------------------- testFramework.assert( !(GPS1 == GPS2), "Verify same Time System but different time inequality", __LINE__ ); testFramework.assert( GPS1.getTimeSystem() == GPS2.getTimeSystem(), "Verify same Time System equality", __LINE__ ); //---------------------------------------- // Differing TimeSystem != Operator //---------------------------------------- testFramework.changeSourceMethod( "Differing TimeSystem != Operator" ); testFramework.assert( GPS1 != UTC1, "Verify different Time System but same time inequality", __LINE__ ); testFramework.assert( GPS1 != UNKNOWN, "Verify different Time System but same time inequality", __LINE__ ); //---------------------------------------- // ANY TimeSystem == Operator //---------------------------------------- testFramework.changeSourceMethod( "ANY TimeSystem == Operator" ); testFramework.assert( GPS1 == ANY, "Verify TimeSystem=ANY does not matter in TimeSystem=GPS comparisons", __LINE__ ); testFramework.assert( UTC1 == ANY, "Verify TimeSystem=ANY does not matter in TimeSystem=UTC comparisons", __LINE__ ); testFramework.assert( UNKNOWN == ANY, "Verify TimeSystem=ANY does not matter in TimeSystem=UNKOWN comparisons", __LINE__ ); //---------------------------------------- // ANY TimeSystem < Operator //---------------------------------------- testFramework.changeSourceMethod( "ANY TimeSystem < Operator" ); testFramework.assert( !(GPS2 == ANY) && (GPS2 < ANY), "Verify TimeSystem=ANY does not matter in other operator comparisons", __LINE__ ); //---------------------------------------- // setTimeSystem //---------------------------------------- testFramework.changeSourceMethod( "setTimeSystem" ); UNKNOWN.setTimeSystem( TimeSystem(2) ); //Set the Unknown TimeSystem testFramework.assert( UNKNOWN.getTimeSystem()==TimeSystem(2), "Ensure resetting a Time System changes it", __LINE__ ); //---------------------------------------- // The End! //---------------------------------------- return testFramework.countFails(); }
// Test will check converting to/from CommonTime. unsigned toFromCommonTimeTest() { TUDEF("IRNWeekSecond", "isValid"); IRNWeekSecond compare(0,10.0,TimeSystem::IRN); //Initialize an object CommonTime truth; long truthDay, truthSOD; double truthFSOD; truth.set(2451412, 43210,0.0,TimeSystem::IRN); //-------------------------------------------------------------------- //Is the time after the BEGINNING_OF_TIME? //-------------------------------------------------------------------- TUASSERT(compare.convertToCommonTime() > CommonTime::BEGINNING_OF_TIME); //-------------------------------------------------------------------- //Is the set object valid? //-------------------------------------------------------------------- TUASSERT(compare.isValid()); CommonTime test = compare.convertToCommonTime(); //Convert to CommonTime long testDay, testSOD; double testFSOD; test.get(testDay, testSOD, testFSOD); truth.get(truthDay, truthSOD, truthFSOD); // Currently, IRNWeekSecond does not convert to proper CommonTime // These tests will be valid and will be uncommented once issue_248 has been // resolved and merged into master. // TUASSERTE(long, truthDay, testDay); // TUASSERTE(long, truthSOD, testSOD); // TUASSERTFE(truthFSOD, testFSOD); IRNWeekSecond test2; test2.convertFromCommonTime(test); //Convert From testFramework.changeSourceMethod("CommonTimeConversion"); //-------------------------------------------------------------------- //Is the result of conversion the same? //-------------------------------------------------------------------- TUASSERTE(TimeSystem, compare.getTimeSystem(), test2.getTimeSystem()); TUASSERTE(int, compare.week, test2.week); TUASSERTFE(compare.sow, test2.sow); //TUASSERT(test2 == test); TURETURN(); }
//============================================================ // Test Suite: resetTest() //============================================================ // // Test will check the reset method. // //============================================================ int resetTest( void ) { TestUtil testFramework( "CommonTime", "reset" , __FILE__, __LINE__ ); CommonTime Compare; Compare.set(1000,200,0.2); // Initialize with value long day, sod; double fsod; Compare.reset(); // Reset it Compare.get(day,sod,fsod); testFramework.assert( TimeSystem(0) == Compare.getTimeSystem(), "Was the time system reset to expectation?", __LINE__ ); testFramework.assert( 0 == day, "Was the day value reset to expectation?", __LINE__ ); testFramework.assert( 0 == sod, "Was the sod value reset to expectation?", __LINE__ ); testFramework.assert( 0 == fsod, "Was the fsod value reset to expectation?", __LINE__ ); return testFramework.countFails(); }
CommonTime JulianDate::convertToCommonTime() const { try { long double temp_jd( jd + 0.5 ); long jday( static_cast<long>( temp_jd ) ); long double sod = ( temp_jd - static_cast<long double>( jday ) ) * SEC_PER_DAY; CommonTime ct; return ct.set( jday, static_cast<long>( sod ), static_cast<double>( sod - static_cast<long>( sod ) ), timeSystem ); } catch (InvalidParameter& ip) { InvalidRequest ir(ip); GPSTK_THROW(ir); } }
CommonTime WeekSecond::convertToCommonTime() const { try { //int dow = static_cast<int>( sow * DAY_PER_SEC ); // Appears to have rounding issues on 32-bit platforms int dow = static_cast<int>( sow / SEC_PER_DAY ); // NB this assumes MJDEpoch is an integer - what if epoch H:M:S != 0:0:0 ? long jday = MJD_JDAY + MJDEpoch() + (7 * week) + dow; double sod(sow - SEC_PER_DAY * dow); CommonTime ct; return ct.set( jday, static_cast<long>(sod), sod - static_cast<long>(sod), timeSystem ); } catch (InvalidParameter& ip) { GPSTK_RETHROW(ip); } }
//============================================================ // Test Suite: rolloverTest() //============================================================ // // Test to check arithmetic operations function properly when // rolling over or under the three time variables // //============================================================ int rolloverTest( void ) { TestUtil testFramework( "CommonTime", "addSeconds", __FILE__, __LINE__ ); CommonTime fsodRollover; fsodRollover.set(10 , 6789 , 0.000999); CommonTime msodRollover; msodRollover.set(10 , 86399, 0.0001 ); CommonTime dayRollunder; dayRollunder.set(10 , 2 , 0.0001 ); CommonTime msodRollunder; msodRollunder.set(10 , 10 , 0.000001); CommonTime expectedfsodROver; expectedfsodROver.set(10, 6789 , 0.001000); CommonTime expectedmsodROver; expectedmsodROver.set(11, 0 , 0.0001); CommonTime expectedDayRUnder; expectedDayRUnder.set( 9, 86399, 0.0001); CommonTime expectedmsodRUnder; expectedmsodRUnder.set(10, 9 , 0.999999); long obtainedDay, expectedDay; long obtainedMsod, expectedMsod; double obtainedFsod, expectedFsod; double diff; long incrementSecLong = 1L , decrementSecLong = -3L; double incrementSecDouble = 0.000001, decrementSecDouble = -0.000002; //-------------------------------- //Rollover Tests //-------------------------------- //fsod Rollover test fsodRollover.addSeconds(incrementSecDouble); fsodRollover.get(obtainedDay,obtainedMsod,obtainedFsod); expectedfsodROver.get(expectedDay,expectedMsod,expectedFsod); testFramework.assert(obtainedDay == expectedDay , "Rollover of fsod affected day value" , __LINE__); testFramework.assert(obtainedMsod == expectedMsod, "Rollover of fsod did not change msod", __LINE__); diff = fabs(obtainedFsod - expectedFsod); testFramework.assert(diff < eps, "fsod did not rollover properly" , __LINE__); //msod Rollover test msodRollover.addSeconds(incrementSecLong); msodRollover.get(obtainedDay,obtainedMsod,obtainedFsod); expectedmsodROver.get(expectedDay,expectedMsod,expectedFsod); testFramework.assert(obtainedDay == expectedDay , "Rollover of msod did not change day" , __LINE__); testFramework.assert(obtainedMsod == expectedMsod, "msod did not rollover properly" , __LINE__); diff = fabs(obtainedFsod - expectedFsod); testFramework.assert(diff < eps, "Rollover of msod affected fsod oddly", __LINE__); //-------------------------------- //Rollunder Tests //-------------------------------- //fsod Rollover test dayRollunder.addSeconds(decrementSecLong); dayRollunder.get(obtainedDay,obtainedMsod,obtainedFsod); expectedDayRUnder.get(expectedDay,expectedMsod,expectedFsod); testFramework.assert(obtainedDay == expectedDay , "Rollunder of msod did not change day" , __LINE__); testFramework.assert(obtainedMsod == expectedMsod, "msod did not rollunder properly" , __LINE__); diff = fabs(obtainedFsod - expectedFsod); testFramework.assert(diff < eps, "Rollunder of msod affected fsod oddly", __LINE__); //msod Rollover test msodRollunder.addSeconds(decrementSecDouble); msodRollunder.get(obtainedDay,obtainedMsod,obtainedFsod); expectedmsodRUnder.get(expectedDay,expectedMsod,expectedFsod); testFramework.assert(obtainedDay == expectedDay , "Rollunder of fsod affected day value" , __LINE__); testFramework.assert(obtainedMsod == expectedMsod, "Rollunder of fsod did not change msod", __LINE__); diff = fabs(obtainedFsod - expectedFsod); testFramework.assert(diff < eps, "fsod did not rollunder properly" , __LINE__); return testFramework.countFails(); }
//============================================================ // Test Suite: operatorTest() //============================================================ // // Test the comparison operators // //============================================================ int operatorTest( void ) { TestUtil testFramework( "CommonTime", "Differing TimeSystem, Operator ==", __FILE__, __LINE__ ); CommonTime Compare; Compare.set(1000,200,0.2); // Initialize with value CommonTime LessThanDay; LessThanDay.set(100,200,0.2); // Initialize with smaller day value CommonTime LessThanSecond; LessThanSecond.set(1000,20,0.2); // Initialize with smaller second value CommonTime LessThanFSecond; LessThanFSecond.set(1000,200,0.1); // Initialize with smaller fractional second value CommonTime CompareCopy(Compare); // Initialize with copy constructor testFramework.assert( Compare == CompareCopy, "GPSWeekZCount operator ==, Are equivalent objects equivalent?", __LINE__ ); testFramework.assert( !(Compare == LessThanDay), "GPSWeekZCount operator !=, Are non-equivalent objects equivalent?", __LINE__ ); //---------------------------------------- // Operator != //---------------------------------------- testFramework.changeSourceMethod( "Operator !=" ); testFramework.assert( Compare != LessThanDay, "GPSWeekZCount operator !=, Are non-equivalent objects not equivalent?", __LINE__ ); testFramework.assert( Compare != LessThanSecond, "GPSWeekZCount operator !=, Are non-equivalent objects not equivalent?", __LINE__ ); testFramework.assert( Compare != LessThanFSecond, "GPSWeekZCount operator !=, Are non-equivalent objects not equivalent?", __LINE__ ); testFramework.assert( !(Compare != Compare), "GPSWeekZCount operator !=, Are equivalent objects not equivalent?", __LINE__ ); //---------------------------------------- // Operator < //---------------------------------------- testFramework.changeSourceMethod( "Operator <" ); testFramework.assert( LessThanDay < Compare, "Does the < operator function when left_object < right_object?", __LINE__ ); testFramework.assert( LessThanSecond < Compare, "Does the < operator function when left_object < right_object by days?", __LINE__ ); testFramework.assert( !(Compare < LessThanSecond), "Does the < operator function when left_object > right_object by days?", __LINE__ ); testFramework.assert( LessThanFSecond < Compare, "Does the < operator function when left_object < right_object by seconds?", __LINE__ ); testFramework.assert( !(Compare < LessThanFSecond), "Does the < operator function when left_object > right_object by seconds?", __LINE__ ); testFramework.assert( !(Compare < CompareCopy), "Does the < operator function when left_object = right_object?", __LINE__ ); //---------------------------------------- // Greater than assertions //---------------------------------------- testFramework.changeSourceMethod( "Operator >" ); testFramework.assert( Compare > LessThanDay, "Does the > operator function when left_object > right_object by years?", __LINE__ ); testFramework.assert( !(LessThanDay > Compare), "Does the > operator function when left_object < right_object by years?", __LINE__ ); testFramework.assert( Compare > LessThanSecond, "Does the > operator function when left_object > right_object by days?", __LINE__ ); testFramework.assert( !(LessThanSecond > Compare), "Does the > operator function when left_object < right_object by days?", __LINE__ ); testFramework.assert( Compare > LessThanFSecond, "Does the > operator function when left_object > right_object by seconds?", __LINE__ ); testFramework.assert( !(LessThanFSecond > Compare), "Does the > operator function when left_object < right_object by seconds?", __LINE__ ); testFramework.assert( !(Compare > CompareCopy), "Does the > operator function when left_object = right_object?", __LINE__ ); //---------------------------------------- // Less than equals assertion //---------------------------------------- testFramework.changeSourceMethod( "Operator <=" ); testFramework.assert( LessThanDay <= Compare, "Does the < operator function when left_object < right_object by years?", __LINE__ ); testFramework.assert( !(Compare <= LessThanDay), "Does the <= operator function when left_object > right_object by years?", __LINE__ ); testFramework.assert( LessThanSecond <= Compare, "Does the <= operator function when left_object < right_object by days?", __LINE__ ); testFramework.assert( !(Compare <= LessThanSecond), "Does the <= operator function when left_object > right_object by days?", __LINE__ ); testFramework.assert( LessThanFSecond <= Compare, "Does the <= operator function when left_object < right_object by seconds?", __LINE__ ); testFramework.assert( !(Compare <= LessThanFSecond), "Does the <= operator function when left_object > right_object by seconds?", __LINE__ ); testFramework.assert( Compare <= CompareCopy, "Does the <= operator function when left_object = right_object?", __LINE__ ); //---------------------------------------- // Greater than equals assertion //---------------------------------------- testFramework.changeSourceMethod( "Operator >=" ); testFramework.assert( Compare >= LessThanDay, "Does the >= operator function when left_object > right_object by years?", __LINE__ ); testFramework.assert( !(LessThanDay >= Compare), "Does the >= operator function when left_object < right_object by years?", __LINE__ ); testFramework.assert( Compare >= LessThanSecond, "Does the >= operator function when left_object > right_object by days?", __LINE__ ); testFramework.assert( !(LessThanSecond >= Compare), "Does the >= operator function when left_object < right_object by days?", __LINE__ ); testFramework.assert( Compare >= LessThanFSecond, "Does the >= operator function when left_object > right_object by seconds?", __LINE__ ); testFramework.assert( !(LessThanFSecond >= Compare), "Does the >= operator function when left_object < right_object by seconds?", __LINE__ ); // typo from last guy?? testFramework.assert( !(Compare < CompareCopy), "Does the > operator function when left_object = right_object?", __LINE__ ); //---------------------------------------- // The End! //---------------------------------------- return testFramework.countFails(); }
//============================================================ // Test Suite: improperSetTest() //============================================================ // // Test to see if setting improper values induces the // correct exception handling. //============================================================ int improperSetTest( void ) { CommonTime Test; Test.set (700000, 0, 0.); TestUtil testFramework( "CommonTime", "set", __FILE__, __LINE__ ); // Break the input in various ways and make sure the proper exception is called //---------------------------------------- // Does CommonTime.set() work with negative days? //---------------------------------------- try { Test.set(-1,0,0.); testFramework.assert( false, "[testing] CommonTime.set() with negative day, [expected] exception gpstk::Exception, [actual] threw no exception", __LINE__ ); } // QUESTION: Why test for gpstk::InvalidRequest exception instead of gpstk::Exception? // ANSWER: gpstk::InvalidRequest is a child of gpstk::Exception, and depending on CommonTime.cpp // we might sometimes need to be more specific than catching gpstk::Exception // catch( gpstk::InvalidParameter e) // For now, I'm replacing gpstk::InvalidParameter with gpstk::Exception to be consistent throughout this test application catch( gpstk::Exception e) { testFramework.assert( true, "[expected] CommonTime.set() with negative day should throw a gpstk::Exception", __LINE__ ); } catch(...) { testFramework.assert( false, "[testing] CommonTime.set() with negative day, [expected] exception gpstk::Exception, [actual] threw wrong exception", __LINE__ ); } //---------------------------------------- // Does CommonTime.set() work with too many days? //---------------------------------------- try { Test.set(3442449,0,0.); testFramework.assert( false, "[testing] CommonTime.set() with too many days, [expected] exception gpstk::Exception, [actual] threw no exception", __LINE__ ); } catch(gpstk::Exception e) { testFramework.assert( true, "[expected] CommonTime.set() with too many days should throw a gpstk::Exception", __LINE__ ); } catch (...) { testFramework.assert( false, "[testing] CommonTime.set() with too many days, [expected] exception gpstk::Exception, [actual] threw wrong exception", __LINE__ ); } //---------------------------------------- // Does CommonTime.set() work with negative seconds? //---------------------------------------- try { Test.set(700000,-1,0.); testFramework.assert( false, "[testing] CommonTime.set() with negative seconds, [expected] exception gpstk::Exception, [actual] threw no exception", __LINE__ ); } catch(gpstk::Exception e) { testFramework.assert( true, "[expected] CommonTime.set() with negative seconds should throw a gpstk::Exception", __LINE__ ); } catch (...) { testFramework.assert( false, "Fail", __LINE__ ); } //---------------------------------------- // Does a set method work with too many seconds? //---------------------------------------- try { Test.set(700000,24*60*60+1,0.); testFramework.assert( false, "[testing] CommonTime.set() with too many seconds, [expected] exception gpstk::Exception, [actual] threw no exception", __LINE__ ); } catch(gpstk::Exception e) { testFramework.assert( true, "[expected] CommonTime.set() with too many seconds should throw a gpstk::Exception", __LINE__ ); } catch (...) { testFramework.assert( false, "[testing] CommonTime.set() with too many seconds, [expected] exception gpstk::Exception, [actual] threw wrong exception", __LINE__ ); } //---------------------------------------- // Does a set method work with negative fractional seconds? //---------------------------------------- try { Test.set(700000,0,-1.); testFramework.assert( false, "[testing] CommonTime.set() with negative fractional seconds, [expected] exception gpstk::Exception, [actual] threw no exception", __LINE__ ); } catch(gpstk::Exception e) { testFramework.assert( true, "[expected] CommonTime.set() with negative fractional seconds should throw a gpstk::Exception", __LINE__ ); } catch (...) { testFramework.assert( false, "[testing] CommonTime.set() with negative fractional seconds, [expected] exception gpstk::Exception, [actual] threw wrong exception", __LINE__ ); } //---------------------------------------- // Does a set method work with too many fractional seconds? //---------------------------------------- try { Test.set(700000,0,2.); testFramework.assert( false, "[testing] CommonTime.set() with too many fractional seconds, [expected] exception gpstk::Exception, [actual] threw no exception", __LINE__ ); } catch( gpstk::Exception e ) { testFramework.assert( true, "[expected] CommonTime.set() with too many fractional seconds should throw a gpstk::Exception", __LINE__ ); } catch(...) { testFramework.assert( false, "[testing] CommonTime.set() with too many fractional seconds, [expected] exception gpstk::Exception, [actual] threw wrong exception", __LINE__ ); } //---------------------------------------- // Does CommonTime.setInterval() work with negative days? //---------------------------------------- try { Test.setInternal(-1,0,0.); testFramework.assert( false, "[testing] CommonTime.setInterval() with negative days, [expected] exception gpstk::Exception, [actual] threw no exception", __LINE__ ); } catch( gpstk::Exception e ) { testFramework.assert( true, "[expected] CommonTime.set() with negative days should throw a gpstk::Exception", __LINE__ ); } catch (...) { testFramework.assert( false, "[testing] CommonTime.setInterval() with negative days, [expected] exception gpstk::Exception, [actual] threw wrong exception", __LINE__ ); } //---------------------------------------- // Does CommonTime.setInterval() work with too many days? //---------------------------------------- try { Test.setInternal(3442449,0,0.); testFramework.assert( false, "[testing] CommonTime.setInterval() with too many days, [expected] exception gpstk::Exception, [actual] threw no exception", __LINE__ ); } catch(gpstk::Exception e) { testFramework.assert( true, "[expected] CommonTime.setInterval() with too many days should throw a gpstk::Exception", __LINE__ ); } catch (...) { testFramework.assert( false, "[testing] CommonTime.setInterval() with too many days, [expected] exception gpstk::Exception, [actual] threw wrong exception", __LINE__ ); } //---------------------------------------- // Does CommonTime.setInterval() work with negative seconds? //---------------------------------------- try { Test.setInternal(700000,-1,0.); testFramework.assert( false, "[testing] CommonTime.setInterval() with negative seconds, [expected] exception gpstk::Exception, [actual] threw no exception", __LINE__ ); } catch(gpstk::Exception e) { testFramework.assert( true, "[expected] CommonTime.setInterval() with negative seconds should throw a gpstk::Exception", __LINE__ ); } catch (...) { testFramework.assert( false, "[testing] CommonTime.setInterval() with negative seconds, [expected] exception gpstk::Exception, [actual] threw wrong exception", __LINE__ ); } //---------------------------------------- // Does a set method work with too many seconds? //---------------------------------------- try { Test.setInternal(700000,24*60*60+1,0.); testFramework.assert( false, "[testing] CommonTime.setInterval() with too many seconds, [expected] exception gpstk::Exception, [actual] threw no exception", __LINE__ ); } catch(gpstk::Exception e) { testFramework.assert( true, "[expected] CommonTime.setInterval() with too many seconds should throw a gpstk::Exception", __LINE__ ); } // catch(...) // { // testFramework.assert( false, "[testing] CommonTime.setInterval() with too many seconds, [expected] exception gpstk::Exception, [actual] threw wrong exception", __LINE__ ); // } //---------------------------------------- // Does a set method work with negative fractional seconds? //---------------------------------------- try { Test.setInternal(700000,1001,-1.); testFramework.assert( false, "[testing] CommonTime.setInterval() with negative fractional seconds, [expected] exception gpstk::Exception, [actual] threw no exception", __LINE__ ); } catch(gpstk::Exception e) { testFramework.assert( true, "[expected] CommonTime.setInterval() with negative fractional seconds should throw a gpstk::Exception", __LINE__ ); } catch (...) { testFramework.assert( false, "[testing] CommonTime.setInterval() with negative fractional seconds, [expected] exception gpstk::Exception, [actual] threw wrong exception", __LINE__ ); } //---------------------------------------- // Does a set method work with too many fractional seconds? //---------------------------------------- try { Test.setInternal(700000,1001,1001.); testFramework.assert( false, "[testing] CommonTime.setInterval() with too many fractional seconds, [expected] exception gpstk::Exception, [actual] threw no exception", __LINE__ ); } catch(gpstk::Exception e) { testFramework.assert( true, "[expected] CommonTime.setInterval() with too many fractional seconds should throw a gpstk::Exception", __LINE__ ); } catch(...) { testFramework.assert( false, "[testing] CommonTime.setInterval() with too many fractional seconds, [expected] exception gpstk::Exception, [actual] threw wrong exception", __LINE__ ); } //---------------------------------------- // The End! //---------------------------------------- return testFramework.countFails(); }