From fed97b92643e227159a796f5ad62d28c6ed4db5e Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Mon, 10 Aug 2020 17:47:59 +0200 Subject: [PATCH] Sort out the overflow cases in tst_QDateTime Use the actual minimum value, rather than minus the maximum, and adapt the overflow expectations so that they're correct west of Greenwich as well as east. Change-Id: I7a5f4510db0fdea3855b5b2bd4c4a86882030efd Reviewed-by: Thiago Macieira --- .../corelib/time/qdatetime/tst_qdatetime.cpp | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp index 91808f79af..233a33065e 100644 --- a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp @@ -627,10 +627,9 @@ void tst_QDateTime::setMSecsSinceEpoch_data() << QDateTime(QDate::fromJulianDay(0x7fffffff), QTime(21, 59, 59, 999), Qt::UTC) << QDateTime(QDate::fromJulianDay(0x7fffffff), QTime(23, 59, 59, 999)); QTest::newRow("min") - // Use -max(), which is min() + 1, to simplify filtering out overflow cases: - << -std::numeric_limits::max() - << QDateTime(QDate(-292275056, 5, 16), QTime(16, 47, 4, 193), Qt::UTC) - << QDateTime(QDate(-292275056, 5, 16), QTime(17, 47, 4, 193), Qt::LocalTime); + << std::numeric_limits::min() + << QDateTime(QDate(-292275056, 5, 16), QTime(16, 47, 4, 192), Qt::UTC) + << QDateTime(QDate(-292275056, 5, 16), QTime(17, 47, 4, 192), Qt::LocalTime); QTest::newRow("max") << std::numeric_limits::max() << QDateTime(QDate(292278994, 8, 17), QTime(7, 12, 55, 807), Qt::UTC) @@ -732,11 +731,14 @@ void tst_QDateTime::fromMSecsSinceEpoch() QDateTime dtLocal = QDateTime::fromMSecsSinceEpoch(msecs, Qt::LocalTime); QDateTime dtUtc = QDateTime::fromMSecsSinceEpoch(msecs, Qt::UTC); QDateTime dtOffset = QDateTime::fromMSecsSinceEpoch(msecs, Qt::OffsetFromUTC, 60*60); - + using Bound = std::numeric_limits; // LocalTime will overflow for "min" or "max" tests, depending on whether // you're East or West of Greenwich. In UTC, we won't overflow. - if (localTimeType == LocalTimeIsUtc - || msecs != std::numeric_limits::max() * localTimeType) + const bool localOverflow = (localTimeType == LocalTimeAheadOfUtc ? msecs == Bound::max() + : localTimeType == LocalTimeBehindUtc ? msecs == Bound::min() + : false); + + if (!localOverflow) QCOMPARE(dtLocal, utc); QCOMPARE(dtUtc, utc); @@ -745,8 +747,7 @@ void tst_QDateTime::fromMSecsSinceEpoch() QCOMPARE(dtOffset, utc); QCOMPARE(dtOffset.offsetFromUtc(), 60*60); - // // OffsetFromUTC will overflow for max - if (msecs != std::numeric_limits::max()) + if (msecs != Bound::max()) // Offset is positive, so overflows max QCOMPARE(dtOffset.time(), utc.time().addMSecs(60*60*1000)); if (zoneIsCET) { @@ -755,8 +756,7 @@ void tst_QDateTime::fromMSecsSinceEpoch() QCOMPARE(dtOffset.toLocalTime(), cet); } - // LocalTime will overflow for max - if (msecs != std::numeric_limits::max()) + if (!localOverflow) QCOMPARE(dtLocal.toMSecsSinceEpoch(), msecs); QCOMPARE(dtUtc.toMSecsSinceEpoch(), msecs); QCOMPARE(dtOffset.toMSecsSinceEpoch(), msecs); @@ -768,8 +768,7 @@ void tst_QDateTime::fromMSecsSinceEpoch() } QDateTime reference(QDate(1970, 1, 1), QTime(), Qt::UTC); - // LocalTime will overflow for max - if (msecs != std::numeric_limits::max()) + if (!localOverflow) QCOMPARE(dtLocal, reference.addMSecs(msecs)); QCOMPARE(dtUtc, reference.addMSecs(msecs)); QCOMPARE(dtOffset, reference.addMSecs(msecs));