From 7a3fed3f209e5113434647897c4f16a7ac3d9c01 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Tue, 23 May 2023 17:58:32 +0200 Subject: [PATCH] QTimeZone: use new comparison helper macros The class had operator==() and operator!=() defined as public member functions, so use QT_CORE_REMOVED_SINCE and removed_api.cpp to get rid of these methods and replace them with hidden friends. Extend unit-tests by using the helper functions from QTestPrivate. Task-number: QTBUG-104111 Change-Id: Ib9ca613005e2f1521dea5e3cd9e2baa0b47fede4 Reviewed-by: Edward Welbourne Reviewed-by: Qt CI Bot --- src/corelib/compat/removed_api.cpp | 12 ++++++ src/corelib/time/qtimezone.cpp | 42 +++++++++---------- src/corelib/time/qtimezone.h | 6 +++ .../corelib/time/qtimezone/CMakeLists.txt | 1 + .../corelib/time/qtimezone/tst_qtimezone.cpp | 36 ++++++++++++---- 5 files changed, 69 insertions(+), 28 deletions(-) diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index f8e6fa3a516..efffb1b9bd0 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -859,6 +859,18 @@ QString QString::chopped(qsizetype n) const { return sliced(0, size() - n); } #endif +#include "qtimezone.h" + +bool QTimeZone::operator==(const QTimeZone &other) const +{ + return comparesEqual(*this, other); +} + +bool QTimeZone::operator!=(const QTimeZone &other) const +{ + return !comparesEqual(*this, other); +} + #include "qurl.h" QUrl QUrl::fromEncoded(const QByteArray &input, ParsingMode mode) diff --git a/src/corelib/time/qtimezone.cpp b/src/corelib/time/qtimezone.cpp index 9839107e80a..c7cf18d346e 100644 --- a/src/corelib/time/qtimezone.cpp +++ b/src/corelib/time/qtimezone.cpp @@ -735,7 +735,9 @@ QTimeZone &QTimeZone::operator=(const QTimeZone &other) */ /*! - Returns \c true if this time representation is equal to the \a other. + \fn bool QTimeZone::operator==(const QTimeZone &lhs, const QTimeZone &rhs) + + Returns \c true if \a lhs time zone is equal to the \a rhs time zone. Two representations are different if they are internally described differently, even if they agree in their representation of all moments of @@ -743,35 +745,33 @@ QTimeZone &QTimeZone::operator=(const QTimeZone &other) time zone but the two will not be equal. */ -bool QTimeZone::operator==(const QTimeZone &other) const -{ - if (d.isShort()) - return other.d.isShort() && d.s == other.d.s; +/*! + \fn bool QTimeZone::operator!=(const QTimeZone &lhs, const QTimeZone &rhs) - if (!other.d.isShort()) { - if (d.d == other.d.d) + Returns \c true if \a lhs time zone is not equal to the \a rhs time zone. + + Two representations are different if they are internally described + differently, even if they agree in their representation of all moments of + time. In particular, a lightweight time representation may coincide with a + time zone but the two will not be equal. +*/ + +bool comparesEqual(const QTimeZone &lhs, const QTimeZone &rhs) noexcept +{ + if (lhs.d.isShort()) + return rhs.d.isShort() && lhs.d.s == rhs.d.s; + + if (!rhs.d.isShort()) { + if (lhs.d.d == rhs.d.d) return true; #if QT_CONFIG(timezone) - return d.d && other.d.d && *d.d == *other.d.d; + return lhs.d.d && rhs.d.d && *lhs.d.d == *rhs.d.d; #endif } return false; } -/*! - Returns \c true if this time zone is not equal to the \a other time zone. - - Two representations are different if they are internally described - differently, even if they agree in their representation of all moments of - time. In particular, a lightweight time representation may coincide with a - time zone but the two will not be equal. -*/ -bool QTimeZone::operator!=(const QTimeZone &other) const // ### Qt 7: inline -{ - return !(*this == other); -} - /*! Returns \c true if this time zone is valid. */ diff --git a/src/corelib/time/qtimezone.h b/src/corelib/time/qtimezone.h index 6040624f61a..e9599ae6151 100644 --- a/src/corelib/time/qtimezone.h +++ b/src/corelib/time/qtimezone.h @@ -5,6 +5,7 @@ #ifndef QTIMEZONE_H #define QTIMEZONE_H +#include #include #include #include @@ -114,8 +115,10 @@ public: void swap(QTimeZone &other) noexcept { d.swap(other.d); } +#if QT_CORE_REMOVED_SINCE(6, 7) bool operator==(const QTimeZone &other) const; bool operator!=(const QTimeZone &other) const; +#endif bool isValid() const; @@ -230,6 +233,9 @@ public: # endif #endif // feature timezone private: + friend Q_CORE_EXPORT bool comparesEqual(const QTimeZone &lhs, const QTimeZone &rhs) noexcept; + Q_DECLARE_EQUALITY_COMPARABLE(QTimeZone) + #ifndef QT_NO_DATASTREAM friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &ds, const QTimeZone &tz); #endif diff --git a/tests/auto/corelib/time/qtimezone/CMakeLists.txt b/tests/auto/corelib/time/qtimezone/CMakeLists.txt index e6c0a900148..612bab0db50 100644 --- a/tests/auto/corelib/time/qtimezone/CMakeLists.txt +++ b/tests/auto/corelib/time/qtimezone/CMakeLists.txt @@ -19,6 +19,7 @@ qt_internal_add_test(tst_qtimezone QT_NO_KEYWORDS LIBRARIES Qt::CorePrivate + Qt::TestPrivate ) ## Scopes: diff --git a/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp index 6b0ce704568..c4ba809bb5a 100644 --- a/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp +++ b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -24,6 +25,8 @@ private Q_SLOTS: void createTest(); void nullTest(); void assign(); + void compareCompiles(); + void compare_data(); void compare(); void timespec(); void offset(); @@ -331,19 +334,38 @@ void tst_QTimeZone::assign() #endif } -void tst_QTimeZone::compare() +void tst_QTimeZone::compareCompiles() { + QTestPrivate::testEqualityOperatorsCompile(); +} + +void tst_QTimeZone::compare_data() +{ + QTest::addColumn("left"); + QTest::addColumn("right"); + QTest::addColumn("expectedEqual"); + const QTimeZone local; const QTimeZone utc(QTimeZone::UTC); const auto secondEast = QTimeZone::fromSecondsAheadOfUtc(1); + const auto zeroOffset = QTimeZone::fromSecondsAheadOfUtc(0); + const auto durationEast = QTimeZone::fromDurationAheadOfUtc(std::chrono::seconds{1}); - QCOMPARE_NE(local, utc); - QCOMPARE_NE(utc, secondEast); - QCOMPARE_NE(secondEast, local); + QTest::newRow("local vs default-constructed") << local << QTimeZone() << true; + QTest::newRow("local vs UTC") << local << utc << false; + QTest::newRow("local vs secondEast") << local << secondEast << false; + QTest::newRow("secondEast vs UTC") << secondEast << utc << false; + QTest::newRow("UTC vs zeroOffset") << utc << zeroOffset << true; + QTest::newRow("secondEast vs durationEast") << secondEast << durationEast << true; +} - QCOMPARE(local, QTimeZone()); - QCOMPARE(utc, QTimeZone::fromSecondsAheadOfUtc(0)); - QCOMPARE(secondEast, QTimeZone::fromDurationAheadOfUtc(std::chrono::seconds{1})); +void tst_QTimeZone::compare() +{ + QFETCH(QTimeZone, left); + QFETCH(QTimeZone, right); + QFETCH(bool, expectedEqual); + + QTestPrivate::testEqualityOperators(left, right, expectedEqual); } void tst_QTimeZone::timespec()