From b1cf188e83eb65c1be9da7d62f43bd802353ace3 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Fri, 25 Sep 2020 16:06:34 +0200 Subject: [PATCH] Rework a test, eliminating some needless conversion via strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Andrei Golubev --- .../corelib/time/qdatetime/tst_qdatetime.cpp | 50 ++++++------------- 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp index a568ef69db..855b7273c8 100644 --- a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp @@ -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: @@ -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)); @@ -1721,31 +1705,27 @@ void tst_QDateTime::currentDateTimeUtc2() void tst_QDateTime::toSecsSinceEpoch_data() { - QTest::addColumn("dateTimeStr"); - QTest::addColumn("valid"); + QTest::addColumn("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()