testDates.cpp   testDates.cpp 
#include <config.h>
#include <QString> #include <QString>
#include <QDateTime> #include <QDateTime>
#include <QDate> #include <QDate>
#include <QMap> #include <QMap>
#include <QList> #include <QList>
#include <QtGlobal> #include <QtGlobal>
#include <QLocale> #include <QLocale>
#include <QtDebug> #include <QtDebug>
#include "StelUtils.hpp" #include "StelUtils.hpp"
#include "tests/testDates.hpp" #include "tests/testDates.hpp"
QTEST_MAIN(TestDates); QTEST_MAIN(TestDates);
void TestDates::dateRoundTrip() void TestDates::dateRoundTrip()
{ {
QMap <double, QString> map; QMap<double, QString> map;
map[0.0] = "-4712-01-01T12:00:00"; map[0.0] = "-4712-01-01T12:00:00";
map[-1.0] = "-4713-12-31T12:00:00"; map[-1.0] = "-4713-12-31T12:00:00";
map[2454466.0] = "2007-12-31T12:00:00"; map[2454466.0] = "2007-12-31T12:00:00";
map[1721058.0] = "0000-01-01T12:00:00"; map[1721058.0] = "0000-01-01T12:00:00";
map[2500000.0] = "2132-08-31T12:00:00"; map[2500000.0] = "2132-08-31T12:00:00";
map[366.0] = "-4711-01-01T12:00:00"; map[366.0] = "-4711-01-01T12:00:00";
map[2454534] = "2008-03-08T12:00:00"; map[2454534] = "2008-03-08T12:00:00";
map[2299161.0] = "1582-10-15T12:00:00"; map[2299161.0] = "1582-10-15T12:00:00";
map[2454466.5] = "2008-01-01T00:00:00"; map[2454466.5] = "2008-01-01T00:00:00";
map[1720692.0] = "-0002-12-31T12:00:00"; map[1720692.0] = "-0002-12-31T12:00:00";
skipping to change at line 51 skipping to change at line 50
map[-153.0] = "-4713-08-01T12:00:00"; map[-153.0] = "-4713-08-01T12:00:00";
map[-184.0] = "-4713-07-01T12:00:00"; map[-184.0] = "-4713-07-01T12:00:00";
map[-214.0] = "-4713-06-01T12:00:00"; map[-214.0] = "-4713-06-01T12:00:00";
map[-245.0] = "-4713-05-01T12:00:00"; map[-245.0] = "-4713-05-01T12:00:00";
map[-275.0] = "-4713-04-01T12:00:00"; map[-275.0] = "-4713-04-01T12:00:00";
map[-306.0] = "-4713-03-01T12:00:00"; map[-306.0] = "-4713-03-01T12:00:00";
map[-334.0] = "-4713-02-01T12:00:00"; // 28 days map[-334.0] = "-4713-02-01T12:00:00"; // 28 days
map[-365.0] = "-4713-01-01T12:00:00"; map[-365.0] = "-4713-01-01T12:00:00";
map[-699.0] = "-4714-02-01T12:00:00"; // 28 days map[-699.0] = "-4714-02-01T12:00:00"; // 28 days
map[-1064.0] = "-4715-02-01T12:00:00"; // 28 days map[-1064.0] = "-4715-02-01T12:00:00"; // 28 days
map[-1420.0] = "-4716-02-01T12:00:00"; // 29 days map[-1430.0] = "-4716-02-01T12:00:00"; // 29 days
map[-1785.0] = "-4717-02-01T12:00:00"; // 28 days map[-1795.0] = "-4717-02-01T12:00:00"; // 28 days
// does jdToIsoString() of item in t1d match string in t1s? bool ok;
QList<int> fromString; for (QMap<double, QString>::ConstIterator i=map.constBegin();i!=map.
int testNum=0; constEnd();++i)
int successForwardNum=0;
int successReverseNum=0;
int total = map.keys().size();
QList<double> jdList = map.keys();
qSort(jdList.begin(), jdList.end());
foreach(double jd, jdList)
{ {
testNum++; QCOMPARE(StelUtils::julianDayToISO8601String(i.key()), i.val
if (StelUtils::jdToIsoString(jd) == map[jd]) ue());
{ double tmp = StelUtils::getJulianDayFromISO8601String(i.valu
successForwardNum++; e(), &ok);
} QVERIFY(ok);
if (i.key()!=0.0)
qFuzzyCompare(i.key(), tmp);
else else
{ qFuzzyCompare(i.key()+1.0, tmp+1.0);
qWarning() << QString("%1/%2: %3 => %4 != %5").arg(t
estNum).arg(total).arg(jd).
arg(StelUtils::jdToIsoString(jd)).ar
g(map[jd]);
}
QList<int> fromString = StelUtils::getIntsFromISO8601String(
map[jd]);
double backJd;
double success = StelUtils::getJDFromDate(&backJd,fromString
.at(0),fromString.at(1),fromString.at(2),
fromString.at(3), fromString.at(4),
fromString.at(5));
if (jd == backJd && success)
{
successReverseNum++;
}
else
{
qWarning() << QString("reverse %1 => %2").arg(backJd
).arg(jd);
}
} }
QVERIFY2(total==successForwardNum && total==successReverseNum,
qPrintable(QString("%1/%2 jd=>string, %3/%4 string=
>jd").arg(successForwardNum).arg(total)
.arg(successReverseNum).arg(total)));
} }
void TestDates::formatting() void TestDates::formatting()
{ {
QLocale usEN; QLocale usEN;
// test formatting of StelUtils::localeDateString, the fall-back if QDateTime cannot do it. // test formatting of StelUtils::localeDateString, the fall-back if QDateTime cannot do it.
QLocale::setDefault(QLocale::German); QLocale::setDefault(QLocale::German);
QVERIFY2(QString::compare(StelUtils::localeDateString(2008, 03, 10, 0), QString("10.03.08")) == 0, QVERIFY2(QString::compare(StelUtils::localeDateString(2008, 03, 10, 0), QString("10.03.08")) == 0,
qPrintable("german for 2008-03-10 wrong: " + (StelU tils::localeDateString(2008, 03, 10, 0)))); qPrintable("german for 2008-03-10 wrong: " + (StelU tils::localeDateString(2008, 03, 10, 0))));
skipping to change at line 125 skipping to change at line 97
QString easyLong("d dd ddd dddd M MM MMM MMMM yy yyyy"); QString easyLong("d dd ddd dddd M MM MMM MMMM yy yyyy");
QVERIFY2(QString::compare(QString("9 09 Sun Sunday 3 03 Mar March 08 2008"), StelUtils::localeDateString(2008, 3, 9, 6, easyLong)) == 0, QVERIFY2(QString::compare(QString("9 09 Sun Sunday 3 03 Mar March 08 2008"), StelUtils::localeDateString(2008, 3, 9, 6, easyLong)) == 0,
qPrintable("formatter1 not working: " + StelUtils:: localeDateString(2008, 3, 9, 6, easyLong))); qPrintable("formatter1 not working: " + StelUtils:: localeDateString(2008, 3, 9, 6, easyLong)));
QString hardLong("dddddddd '''doh' ''yyyyyyy"); QString hardLong("dddddddd '''doh' ''yyyyyyy");
QVERIFY2(QString::compare(QString("SundaySunday 'doh '200808y"), Ste lUtils::localeDateString(2008, 3, 9, 6, hardLong)) == 0, QVERIFY2(QString::compare(QString("SundaySunday 'doh '200808y"), Ste lUtils::localeDateString(2008, 3, 9, 6, hardLong)) == 0,
qPrintable("formatter2 not working: " + StelUtils:: localeDateString(2008, 3, 9, 6, hardLong))); qPrintable("formatter2 not working: " + StelUtils:: localeDateString(2008, 3, 9, 6, hardLong)));
// test detection of offset from UTC. // test detection of offset from UTC.
double mar122008 = QDate(2008,3,12).toJulianDay(); double mar122008 = QDate(2008,3,12).toJulianDay();
QVERIFY2(-4.0 == StelUtils::getGMTShiftFromQT(mar122008), qFuzzyCompare(StelUtils::getGMTShiftFromQT(mar122008), -4.f);
qPrintable("gmt shift wrong: " + QString("%1").arg(StelUtils
::getGMTShiftFromQT(mar122008))));
double mar012008 = QDate(2008,3,1).toJulianDay(); double mar012008 = QDate(2008,3,1).toJulianDay();
QVERIFY2(-5.0 == StelUtils::getGMTShiftFromQT(mar012008), qFuzzyCompare(StelUtils::getGMTShiftFromQT(mar012008), -5.f);
qPrintable("gmt shift wrong: " + QString("%1").arg(StelUtils
::getGMTShiftFromQT(mar012008))));
} }
void TestDates::testRolloverAndValidity() void TestDates::testRolloverAndValidity()
{ {
QVERIFY2(31==StelUtils::numberOfDaysInMonthInYear(1, 2008), "1a"); QVERIFY2(31==StelUtils::numberOfDaysInMonthInYear(1, 2008), "1a");
QVERIFY2(29==StelUtils::numberOfDaysInMonthInYear(2, 2008), "1b"); QVERIFY2(29==StelUtils::numberOfDaysInMonthInYear(2, 2008), "1b");
QVERIFY2(28==StelUtils::numberOfDaysInMonthInYear(2, 2007), "1c"); QVERIFY2(28==StelUtils::numberOfDaysInMonthInYear(2, 2007), "1c");
QVERIFY2(29==StelUtils::numberOfDaysInMonthInYear(2, 2000), "1d"); QVERIFY2(29==StelUtils::numberOfDaysInMonthInYear(2, 2000), "1d");
skipping to change at line 176 skipping to change at line 146
QVERIFY(0==ds); QVERIFY(0==ds);
QVERIFY2(StelUtils::changeDateTimeForRollover(-1, 11, 45, 12, 0, 0, &dy, &dm, &dd, &dh, &dmin, &ds), "1n"); QVERIFY2(StelUtils::changeDateTimeForRollover(-1, 11, 45, 12, 0, 0, &dy, &dm, &dd, &dh, &dmin, &ds), "1n");
QVERIFY(-1==dy); QVERIFY(-1==dy);
QVERIFY(12==dm); QVERIFY(12==dm);
QVERIFY(15==dd); QVERIFY(15==dd);
QVERIFY(12==dh); QVERIFY(12==dh);
QVERIFY(0==dmin); QVERIFY(0==dmin);
QVERIFY(0==ds); QVERIFY(0==ds);
} }
void TestDates::qdateRoundTrip()
{
QDate tst = QDate::currentDate();
long jd = tst.toJulianDay();
while (tst.isValid())
{
jd--;
tst = tst.addDays(-1);
QDate comp = QDate::fromJulianDay(jd);
QVERIFY2(tst == comp, qPrintable("failed at jd " + QString("
%1").arg(jd) + " " + tst.toString() + " vs " + comp.toString()));
if (tst!=comp)
break;
}
}
 End of changes. 10 change blocks. 
52 lines changed or deleted 16 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/