From 3275050df41e1dd16d3db0423816dd2955e596ab Mon Sep 17 00:00:00 2001 From: Rym Bouabid Date: Wed, 14 Feb 2024 14:33:58 +0100 Subject: [PATCH] QStorageInfo: Use new comparison helper macros Replace operator==() and operator!=() private friends with comparesEqual(). Use QT_TEST_ALL_EQUALITY_OPS macro in unit-tests. Use new \compares command in the documentation to describe the comparison operators provided by QStorageInfo. Task-number: QTBUG-120303 Change-Id: I6434dc8382f6554b9e60840bac4abaeb95b70db6 Reviewed-by: Ivan Solovev --- src/corelib/io/qstorageinfo.cpp | 21 +++++++++++++------ src/corelib/io/qstorageinfo.h | 14 +++---------- .../io/qstorageinfo/tst_qstorageinfo.cpp | 17 ++++++++++----- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/corelib/io/qstorageinfo.cpp b/src/corelib/io/qstorageinfo.cpp index 4f206bc907..b7a17febaf 100644 --- a/src/corelib/io/qstorageinfo.cpp +++ b/src/corelib/io/qstorageinfo.cpp @@ -20,6 +20,8 @@ QT_IMPL_METATYPE_EXTERN(QStorageInfo) \ingroup io \ingroup shared + \compares equality + Allows retrieving information about the volume's space, its mount point, label, and filesystem name. @@ -386,22 +388,29 @@ QStorageInfo QStorageInfo::root() } /*! - \fn bool QStorageInfo::operator==(const QStorageInfo &first, const QStorageInfo &second) + \fn bool QStorageInfo::operator==(const QStorageInfo &lhs, const QStorageInfo &rhs) - Returns true if the \a first QStorageInfo object refers to the same drive or volume - as the \a second; otherwise it returns false. + Returns \c true if the QStorageInfo object \a lhs refers to the same drive or + volume as the QStorageInfo object \a rhs; otherwise it returns \c false. Note that the result of comparing two invalid QStorageInfo objects is always positive. */ /*! - \fn bool QStorageInfo::operator!=(const QStorageInfo &first, const QStorageInfo &second) + \fn bool QStorageInfo::operator!=(const QStorageInfo &lhs, const QStorageInfo &rhs) - Returns true if the \a first QStorageInfo object refers to a different drive or - volume than the \a second; otherwise returns false. + Returns \c true if the QStorageInfo object \a lhs refers to a different drive or + volume than the QStorageInfo object \a rhs; otherwise returns \c false. */ +bool comparesEqual(const QStorageInfo &lhs, const QStorageInfo &rhs) +{ + if (lhs.d == rhs.d) + return true; + return lhs.device() == rhs.device() && lhs.rootPath() == rhs.rootPath(); +} + #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug debug, const QStorageInfo &s) { diff --git a/src/corelib/io/qstorageinfo.h b/src/corelib/io/qstorageinfo.h index 45869519bc..3784fe8e47 100644 --- a/src/corelib/io/qstorageinfo.h +++ b/src/corelib/io/qstorageinfo.h @@ -5,6 +5,7 @@ #define QSTORAGEINFO_H #include +#include #include #include #include @@ -58,17 +59,8 @@ public: private: explicit QStorageInfo(QStorageInfoPrivate &dd); friend class QStorageInfoPrivate; - friend inline bool operator==(const QStorageInfo &first, const QStorageInfo &second) - { - if (first.d == second.d) - return true; - return first.device() == second.device() && first.rootPath() == second.rootPath(); - } - - friend inline bool operator!=(const QStorageInfo &first, const QStorageInfo &second) - { - return !(first == second); - } + friend Q_CORE_EXPORT bool comparesEqual(const QStorageInfo &lhs, const QStorageInfo &rhs); + Q_DECLARE_EQUALITY_COMPARABLE(QStorageInfo) friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QStorageInfo &); QExplicitlySharedDataPointer d; diff --git a/tests/auto/corelib/io/qstorageinfo/tst_qstorageinfo.cpp b/tests/auto/corelib/io/qstorageinfo/tst_qstorageinfo.cpp index 98bd8edfd0..8ee0ed0c7a 100644 --- a/tests/auto/corelib/io/qstorageinfo/tst_qstorageinfo.cpp +++ b/tests/auto/corelib/io/qstorageinfo/tst_qstorageinfo.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include +#include #include #include #include @@ -22,6 +23,7 @@ class tst_QStorageInfo : public QObject private slots: void defaultValues(); void dump(); + void compareCompiles(); void operatorEqual(); void operatorNotEqual(); void root(); @@ -81,31 +83,36 @@ void tst_QStorageInfo::dump() printVolumes(QStorageInfo::mountedVolumes(), qInfoPrinter); } +void tst_QStorageInfo::compareCompiles() +{ + QTestPrivate::testEqualityOperatorsCompile(); +} + void tst_QStorageInfo::operatorEqual() { { QStorageInfo storage1 = QStorageInfo::root(); QStorageInfo storage2(QDir::rootPath()); - QCOMPARE(storage1, storage2); + QT_TEST_EQUALITY_OPS(storage1, storage2, true); } { QStorageInfo storage1(QCoreApplication::applicationDirPath()); QStorageInfo storage2(QCoreApplication::applicationFilePath()); - QCOMPARE(storage1, storage2); + QT_TEST_EQUALITY_OPS(storage1, storage2, true); } { QStorageInfo storage1; QStorageInfo storage2; - QCOMPARE(storage1, storage2); + QT_TEST_EQUALITY_OPS(storage1, storage2, true); } // Test copy ctor { QStorageInfo storage1 = QStorageInfo::root(); QStorageInfo storage2(storage1); - QCOMPARE(storage1, storage2); + QT_TEST_EQUALITY_OPS(storage1, storage2, true); } } @@ -113,7 +120,7 @@ void tst_QStorageInfo::operatorNotEqual() { QStorageInfo storage1 = QStorageInfo::root(); QStorageInfo storage2; - QCOMPARE_NE(storage1, storage2); + QT_TEST_EQUALITY_OPS(storage1, storage2, false); } void tst_QStorageInfo::root()