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 <edward.welbourne@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Ivan Solovev 2023-05-23 17:58:32 +02:00
parent 5839aed9bd
commit 7a3fed3f20
5 changed files with 69 additions and 28 deletions

View File

@ -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)

View File

@ -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.
*/

View File

@ -5,6 +5,7 @@
#ifndef QTIMEZONE_H
#define QTIMEZONE_H
#include <QtCore/qcompare.h>
#include <QtCore/qdatetime.h>
#include <QtCore/qlocale.h>
#include <QtCore/qswap.h>
@ -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

View File

@ -19,6 +19,7 @@ qt_internal_add_test(tst_qtimezone
QT_NO_KEYWORDS
LIBRARIES
Qt::CorePrivate
Qt::TestPrivate
)
## Scopes:

View File

@ -4,6 +4,7 @@
#include <QTest>
#include <qtimezone.h>
#include <private/qtimezoneprivate_p.h>
#include <private/qcomparisontesthelper_p.h>
#include <qlocale.h>
@ -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<QTimeZone>();
}
void tst_QTimeZone::compare_data()
{
QTest::addColumn<QTimeZone>("left");
QTest::addColumn<QTimeZone>("right");
QTest::addColumn<bool>("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()