//-*- mode: c++ -*- #ifndef DATETIME_TEST_H_ #define DATETIME_TEST_H_ #include #include "DateTime.h" static const double TEST_EPSILON = 1e-12; class DateTimeTest : public CxxTest::TestSuite { public: void testDefaultConstructor( void ) { // A DateTime object initialized with no paramaters should set the MJD to 0 DateTime epoch; TS_ASSERT_DELTA( epoch.getMJD(), 0.0, TEST_EPSILON); } void testMJDConstructor( void ) { // A DateTime object initialized with an MJD specified should set the MJD... DateTime valentinesDay2008(54510.0); TS_ASSERT_EQUALS(valentinesDay2008.getMJD(), 54510.0); } void testYMDHMSConstructor( void ) { DateTime valentinesDay2008((unsigned int) 2008, // year (unsigned int) 2, // month (unsigned int) 14, // day (unsigned int) 0, // hour (unsigned int) 0, // minute (double) 0.0); // second TS_ASSERT_EQUALS(valentinesDay2008.getMJD(), 54510.0); } void testSetMJD( void ) { DateTime epochToValentinesDay2008(12345.6789); epochToValentinesDay2008.setMJD(54510.0); TS_ASSERT_EQUALS( epochToValentinesDay2008.getMJD(), 54510.0); } /** * Why does testIncrement exist? * * There once was bug (fixed in r1659) where the LFM would perform an * operation (eg. file I/O or exchange data with MIX/RCM) one * second earlier than it should've. This is particularly annoying * because some assumptions were made about file names. The code * would occasionally die when reading a file named * * 1995-03-21T04-51-59Z.hdf * * instead of * * 1995-03-21T04-52-00Z.hdf * * This test makes sure that incrementing the time (and comparing * with ">=") will not have this "off by 1" error. */ void testGreatOrEqual( void ) { const float dt = 0.04007064; DateTime start(1995, 03, 21, 11, 0, 0); DateTime stop(1995, 03, 21, 12, 0, 0); DateTime next = start; int iStep = 0; start.incrementSeconds(60.0*iStep); DateTime current = start; //cout << numeric_limits::epsilon(); while (current < stop){ if (current >= next){ next = start; next.incrementSeconds(60.0*iStep++); TS_ASSERT_DELTA(int(current.getSecond()), 0.0, 1e-1); //cout << current.getDateTimeString() << endl; } current.incrementSeconds(dt); } } void testSmallSecondResolution( void ) { // Make sure that the modified julian date can capture resolution to the nearest second // 1 second DateTime TT1 = DateTime(1.0/(24.0*60.0*60.0)); // 2 seconds DateTime TT2 = DateTime(2.0/(24.0*60.0*60.0)); // 3 seconds DateTime TT3 = DateTime(3.0/(24.0*60.0*60.0)); //std::cout << std::endl << std::setprecision(50) << std::fixed; //std::cout << "\t" << 1.0/(24.0*60.0*60.0) << std::endl; //std::cout << (int) TT1.getSeconds() << "\t" << TT1.getMJD() << std::endl; //std::cout << (int) TT2.getSeconds() << "\t" << TT2.getMJD() << std::endl; //std::cout << (int) TT3.getSeconds() << "\t" << TT3.getMJD() << std::endl; TS_ASSERT_DELTA( TT1.getSeconds(), 1, TEST_EPSILON); TS_ASSERT_DELTA( TT2.getSeconds(), 2, TEST_EPSILON); TS_ASSERT_DELTA( TT3.getSeconds(), 3, TEST_EPSILON); } void testLargeSecondResolution( void ) { // Check that MJD to YYYY-MM-DD HH:MM:SS obtains the correct number of seconds // Ensures that a bug found by Slava using the TT1,TT2,TT3 DateTimes obtains the correct number of elapsed seconds. // [2008-01-01] @ 04:00:02 DateTime TT1 = DateTime(54466.166689814817800652235746400000); // 59.6 seconds: (0.000689814817800652235746400000) // [2008-01-01] @ 04:00:04 DateTime TT2 = DateTime(54466.166712962964083999395370500000); //61.6 seconds: (0.000712962964083999395370500000) // [2008-01-01] @ 04:00:06 DateTime TT3 = DateTime(54466.166736111110367346554994600000); // 63.5999999 seconds: (0.000736111110367346554994600000) //std::cout << std::endl << std::setprecision(50) << std::fixed; //std::cout << "\t" << 1.0/(24.0*60.0*60.0) << std::endl; //std::cout << (int) TT1.getSeconds() << "\t" << TT1.getMJD() << std::endl; //std::cout << (int) TT2.getSeconds() << "\t" << TT2.getMJD() << std::endl; //std::cout << (int) TT3.getSeconds() << "\t" << TT3.getMJD() << std::endl; TS_ASSERT_DELTA( TT1.getSeconds(), 2, TEST_EPSILON); TS_ASSERT_DELTA( TT2.getSeconds(), 4, TEST_EPSILON); TS_ASSERT_DELTA( TT3.getSeconds(), 6, TEST_EPSILON); } void testSetYMDHMS( void ) { DateTime epochToValentinesDay2008(12345.6789); epochToValentinesDay2008.setYear(2008); epochToValentinesDay2008.setMonth(2); epochToValentinesDay2008.setDay(14); epochToValentinesDay2008.setHours(0); epochToValentinesDay2008.setMinutes(0); epochToValentinesDay2008.setSeconds(0.0); TS_ASSERT_EQUALS( epochToValentinesDay2008.getMJD(), 54510.0); } void testGetYear( void ) { DateTime date(54510.0); TS_ASSERT_EQUALS( date.getYear(), 2008); } void testGetMonth( void ) { DateTime date(54510.0); TS_ASSERT_EQUALS( date.getMonth(), 2); } void testGetDay( void ) { DateTime date(54510.0); TS_ASSERT_EQUALS( date.getDay(), 14); } void testGetHours( void ) { DateTime date(54510.0); TS_ASSERT_EQUALS( date.getHours(), 0); } void testGetMinutes( void ) { DateTime date(54510.0); TS_ASSERT_EQUALS( date.getMinutes(), 0); } void testGetSeconds( void ) { DateTime date(54510.0); TS_ASSERT_EQUALS( date.getSeconds(), 0); } void testIncrementMJD( void ) { DateTime date(0.0); date.incrementMJD(100.123); TS_ASSERT_EQUALS( date.getMJD(), 100.123 ); } void testIncrementYear( void ) { DateTime date((unsigned int) 1492, // year (unsigned int) 10, // month (unsigned int) 12, // day (unsigned int) 12, // hour (unsigned int) 0, // minute (double) 0.0); // second date.incrementYear(516); TS_ASSERT_EQUALS( date.getYear(), 2008 ); } void testIncrementMonth( void ) { DateTime date((unsigned int) 1492, // year (unsigned int) 10, // month (unsigned int) 12, // day (unsigned int) 12, // hour (unsigned int) 0, // minute (double) 0.0); // second date.incrementMonth(4); TS_ASSERT_EQUALS( date.getYear(), 1493 ); TS_ASSERT_EQUALS( date.getMonth(), 2 ); } void testIncrementDay( void ) { DateTime date((unsigned int) 1492, // year (unsigned int) 10, // month (unsigned int) 12, // day (unsigned int) 12, // hour (unsigned int) 0, // minute (double) 0.0); // second date.incrementDay(4); TS_ASSERT_EQUALS( date.getDay(), 16 ); } void testIncrementHours( void ) { DateTime date((unsigned int) 1492, // year (unsigned int) 10, // month (unsigned int) 12, // day (unsigned int) 12, // hour (unsigned int) 0, // minute (double) 0.0); // second date.incrementHours(50); TS_ASSERT_EQUALS( date.getDay(), 14 ); TS_ASSERT_EQUALS( date.getHours(), 14 ); } void testIncrementMinutes( void ) { DateTime date((unsigned int) 1492, // year (unsigned int) 10, // month (unsigned int) 12, // day (unsigned int) 12, // hour (unsigned int) 0, // minute (double) 0.0); // second date.incrementMinutes(65); TS_ASSERT_EQUALS( date.getHours(), 13 ); TS_ASSERT_EQUALS( date.getMinutes(), 5 ); } void testIncrementSeconds( void ) { DateTime date((unsigned int) 1492, // year (unsigned int) 10, // month (unsigned int) 12, // day (unsigned int) 12, // hour (unsigned int) 0, // minute (double) 0.0); // second TS_ASSERT_DELTA( date.getMJD(), -133704.5000, 1e-4 ); date.incrementSeconds(65.5); TS_ASSERT_DELTA( date.getMJD(), -133704.4992419, 1e-4 ); TS_ASSERT_EQUALS( date.getMinutes(), 1 ); TS_ASSERT_EQUALS( date.getSeconds(), 5.5 ); } void testGetDayOfYear( void ) { DateTime date((unsigned int) 2007, // year (unsigned int) 3, // month (unsigned int) 1, // day (unsigned int) 0, // hour (unsigned int) 0, // minute (double) 0.0); // second TS_ASSERT_EQUALS( date.getDayOfYear(), 60); // Verify that it works for leap years: DateTime date_leap_year((unsigned int) 2008, // year (unsigned int) 3, // month (unsigned int) 1, // day (unsigned int) 0, // hour (unsigned int) 0, // minute (double) 0.0); // second TS_ASSERT_EQUALS( date_leap_year.getDayOfYear(), 61); } }; #endif /*DATETIME_TEST_H_*/