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 <ivan.solovev@qt.io>
bb10
Rym Bouabid 2024-02-14 14:33:58 +01:00
parent 22ebe86f15
commit 3275050df4
3 changed files with 30 additions and 22 deletions

View File

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

View File

@ -5,6 +5,7 @@
#define QSTORAGEINFO_H
#include <QtCore/qbytearray.h>
#include <QtCore/qcompare.h>
#include <QtCore/qdir.h>
#include <QtCore/qlist.h>
#include <QtCore/qmetatype.h>
@ -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<QStorageInfoPrivate> d;

View File

@ -2,6 +2,7 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QtTest/private/qcomparisontesthelper_p.h>
#include <QSet>
#include <QStandardPaths>
#include <QStorageInfo>
@ -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<QStorageInfo>();
}
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()