Skip to content

Commit

Permalink
Rework a test, eliminating some needless conversion via strings
Browse files Browse the repository at this point in the history
The name toSecsSinceEpoch() gave no hint to the fact that the test
was, in fact, *also* testing round-tripping of the ISODateFormat.
Since there are other tests for string conversion, make this a simple
test of toSecsSinceEpoch().

It did the round-tripping via two methods with overly-terse names that
might just as well be local lambdas - now redundant, so removed.

Change-Id: I1e4fb1cc90224312c995596a8f3fe2bc5d9dfa15
Reviewed-by: Mårten Nordheim <[email protected]>
Reviewed-by: Andrei Golubev <[email protected]>
  • Loading branch information
ediosyncratic committed Oct 2, 2020
1 parent 63bbbdc commit b1cf188
Showing 1 changed file with 15 additions and 35 deletions.
50 changes: 15 additions & 35 deletions tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ class tst_QDateTime : public QObject
public:
tst_QDateTime();

static QString str( int y, int month, int d, int h, int min, int s );
static QDateTime dt( const QString& str );
public Q_SLOTS:
void initTestCase();
private Q_SLOTS:
Expand Down Expand Up @@ -273,20 +271,6 @@ void tst_QDateTime::initTestCase()
<< "the Central European timezone";
}

QString tst_QDateTime::str( int y, int month, int d, int h, int min, int s )
{
return QDateTime( QDate(y, month, d), QTime(h, min, s) ).toString( Qt::ISODate );
}

QDateTime tst_QDateTime::dt(const QString &text)
{
#if QT_CONFIG(datestring)
if (text != "INVALID")
return QDateTime::fromString(text, Qt::ISODate);
#endif
return QDateTime();
}

void tst_QDateTime::ctor()
{
QDateTime dt1(QDate(2004, 1, 2), QTime(1, 2, 3));
Expand Down Expand Up @@ -1721,31 +1705,27 @@ void tst_QDateTime::currentDateTimeUtc2()

void tst_QDateTime::toSecsSinceEpoch_data()
{
QTest::addColumn<QString>("dateTimeStr");
QTest::addColumn<bool>("valid");
QTest::addColumn<QDate>("date");

QTest::newRow( "data1" ) << str( 1800, 1, 1, 12, 0, 0 ) << true;
QTest::newRow( "data2" ) << str( 1969, 1, 1, 12, 0, 0 ) << true;
QTest::newRow( "data3" ) << str( 2002, 1, 1, 12, 0, 0 ) << true;
QTest::newRow( "data4" ) << str( 2002, 6, 1, 12, 0, 0 ) << true;
QTest::newRow( "data5" ) << QString("INVALID") << false;
QTest::newRow( "data6" ) << str( 2038, 1, 1, 12, 0, 0 ) << true;
QTest::newRow( "data7" ) << str( 2063, 4, 5, 12, 0, 0 ) << true; // the day of First Contact
QTest::newRow( "data8" ) << str( 2107, 1, 1, 12, 0, 0 ) << true;
QTest::newRow("start-1800") << QDate(1800, 1, 1);
QTest::newRow("start-1969") << QDate(1969, 1, 1);
QTest::newRow("start-2002") << QDate(2002, 1, 1);
QTest::newRow("mid-2002") << QDate(2002, 6, 1);
QTest::newRow("start-2038") << QDate(2038, 1, 1);
QTest::newRow("star-trek-1st-contact") << QDate(2063, 4, 5);
QTest::newRow("start-2107") << QDate(2107, 1, 1);
}

void tst_QDateTime::toSecsSinceEpoch()
{
QFETCH(const QString, dateTimeStr);
const QDateTime datetime = dt(dateTimeStr);
QFETCH(const bool, valid);
QCOMPARE(datetime.isValid(), valid);
const QTime noon(12, 0);
QFETCH(const QDate, date);
const QDateTime dateTime(date, noon);
QVERIFY(dateTime.isValid());

if (valid) {
const qint64 asSecsSinceEpoch = datetime.toSecsSinceEpoch();
QCOMPARE(asSecsSinceEpoch, datetime.toMSecsSinceEpoch() / 1000);
QCOMPARE(QDateTime::fromSecsSinceEpoch(asSecsSinceEpoch), datetime);
}
const qint64 asSecsSinceEpoch = dateTime.toSecsSinceEpoch();
QCOMPARE(asSecsSinceEpoch, dateTime.toMSecsSinceEpoch() / 1000);
QCOMPARE(QDateTime::fromSecsSinceEpoch(asSecsSinceEpoch), dateTime);
}

void tst_QDateTime::daylightSavingsTimeChange_data()
Expand Down

0 comments on commit b1cf188

Please sign in to comment.