diff --git a/autotests/udsentrytest.h b/autotests/udsentrytest.h --- a/autotests/udsentrytest.h +++ b/autotests/udsentrytest.h @@ -29,6 +29,7 @@ private Q_SLOTS: void testSaveLoad(); void testMove(); + void testEquality(); }; #endif diff --git a/autotests/udsentrytest.cpp b/autotests/udsentrytest.cpp --- a/autotests/udsentrytest.cpp +++ b/autotests/udsentrytest.cpp @@ -261,4 +261,74 @@ } } +/** + * Test to verify that equal semantics work. + */ +void UDSEntryTest::testEquality() +{ + KIO::UDSEntry entry; + entry.fastInsert(KIO::UDSEntry::UDS_SIZE, 1); + entry.fastInsert(KIO::UDSEntry::UDS_USER, QStringLiteral("user1")); + entry.fastInsert(KIO::UDSEntry::UDS_GROUP, QStringLiteral("group1")); + entry.fastInsert(KIO::UDSEntry::UDS_NAME, QStringLiteral("filename1")); + entry.fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME, 123456); + entry.fastInsert(KIO::UDSEntry::UDS_CREATION_TIME, 12345); + entry.fastInsert(KIO::UDSEntry::UDS_DEVICE_ID, 2); + entry.fastInsert(KIO::UDSEntry::UDS_INODE, 56); + + // Same as entry + KIO::UDSEntry entry2; + entry2.fastInsert(KIO::UDSEntry::UDS_SIZE, 1); + entry2.fastInsert(KIO::UDSEntry::UDS_USER, QStringLiteral("user1")); + entry2.fastInsert(KIO::UDSEntry::UDS_GROUP, QStringLiteral("group1")); + entry2.fastInsert(KIO::UDSEntry::UDS_NAME, QStringLiteral("filename1")); + entry2.fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME, 123456); + entry2.fastInsert(KIO::UDSEntry::UDS_CREATION_TIME, 12345); + entry2.fastInsert(KIO::UDSEntry::UDS_DEVICE_ID, 2); + entry2.fastInsert(KIO::UDSEntry::UDS_INODE, 56); + + // 3nd entry: different user. + KIO::UDSEntry entry3; + entry3.fastInsert(KIO::UDSEntry::UDS_SIZE, 1); + entry3.fastInsert(KIO::UDSEntry::UDS_USER, QStringLiteral("other user")); + entry3.fastInsert(KIO::UDSEntry::UDS_GROUP, QStringLiteral("group1")); + entry3.fastInsert(KIO::UDSEntry::UDS_NAME, QStringLiteral("filename1")); + entry3.fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME, 123456); + entry3.fastInsert(KIO::UDSEntry::UDS_CREATION_TIME, 12345); + entry3.fastInsert(KIO::UDSEntry::UDS_DEVICE_ID, 2); + entry3.fastInsert(KIO::UDSEntry::UDS_INODE, 56); + + // 4th entry : an additional field + KIO::UDSEntry entry4; + entry4.fastInsert(KIO::UDSEntry::UDS_SIZE, 1); + entry4.fastInsert(KIO::UDSEntry::UDS_USER, QStringLiteral("user1")); + entry4.fastInsert(KIO::UDSEntry::UDS_GROUP, QStringLiteral("group1")); + entry4.fastInsert(KIO::UDSEntry::UDS_NAME, QStringLiteral("filename1")); + entry4.fastInsert(KIO::UDSEntry::UDS_ICON_NAME, QStringLiteral("home")); + entry4.fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME, 123456); + entry4.fastInsert(KIO::UDSEntry::UDS_CREATION_TIME, 12345); + entry4.fastInsert(KIO::UDSEntry::UDS_DEVICE_ID, 2); + entry4.fastInsert(KIO::UDSEntry::UDS_INODE, 56); + + // == + QVERIFY(entry == entry2); + QVERIFY(!(entry == entry3)); + QVERIFY(!(entry == entry4)); + QVERIFY(!(entry2 == entry3)); + + // != + QVERIFY(!(entry != entry2)); + QVERIFY(entry != entry3); + QVERIFY(entry != entry4); + QVERIFY(entry2 != entry3); + + // make entry3 == entry + entry3.replace(KIO::UDSEntry::UDS_USER, QStringLiteral("user1")); + + QVERIFY(entry == entry3); + QVERIFY(entry2 == entry3); + QVERIFY(!(entry != entry3)); + QVERIFY(!(entry2 != entry3)); +} + QTEST_MAIN(UDSEntryTest) diff --git a/src/core/udsentry.h b/src/core/udsentry.h --- a/src/core/udsentry.h +++ b/src/core/udsentry.h @@ -44,6 +44,18 @@ */ KIOCORE_EXPORT QDebug operator<<(QDebug stream, const KIO::UDSEntry &entry); +/** + * Returns true if the entry contains the same data as the other + * @since 5.63 + */ +KIOCORE_EXPORT bool operator== (const KIO::UDSEntry &entry, const KIO::UDSEntry &other); + +/** + * Returns true if the entry does not contain the same data as the other + * @since 5.63 + */ +KIOCORE_EXPORT bool operator!= (const KIO::UDSEntry &entry, const KIO::UDSEntry &other); + namespace KIO { class UDSEntryPrivate; @@ -337,6 +349,7 @@ friend KIOCORE_EXPORT QDataStream& ::operator<< (QDataStream &s, const KIO::UDSEntry &a); friend KIOCORE_EXPORT QDataStream& ::operator>> (QDataStream &s, KIO::UDSEntry &a); friend KIOCORE_EXPORT QDebug (::operator<<) (QDebug stream, const KIO::UDSEntry &entry); + public: /** * Replace or insert field with string value diff --git a/src/core/udsentry.cpp b/src/core/udsentry.cpp --- a/src/core/udsentry.cpp +++ b/src/core/udsentry.cpp @@ -463,3 +463,34 @@ a.d->load(s); return s; } + +KIOCORE_EXPORT bool operator==(const KIO::UDSEntry &entry, const KIO::UDSEntry &other) +{ + if (entry.count() != other.count()) { + return false; + } + + const QVector fields = entry.fields(); + for (uint field : fields) { + if (!other.contains(field)) { + return false; + } + + if (field & UDSEntry::UDS_STRING) { + if (entry.stringValue(field) != other.stringValue(field)) { + return false; + } + } else { + if (entry.numberValue(field) != other.numberValue(field)) { + return false; + } + } + } + + return true; +} + +KIOCORE_EXPORT bool operator!=(const KIO::UDSEntry &entry, const KIO::UDSEntry &other) +{ + return !(entry == other); +} diff --git a/tests/udsentrybenchmark.cpp b/tests/udsentrybenchmark.cpp --- a/tests/udsentrybenchmark.cpp +++ b/tests/udsentrybenchmark.cpp @@ -263,33 +263,6 @@ stream << m_largeEntries; } } -namespace KIO { -static bool operator==(const UDSEntry &a, const UDSEntry &b) -{ - if (a.count() != b.count()) { - return false; - } - - const QVector fields = a.fields(); - for (uint field : fields) { - if (!b.contains(field)) { - return false; - } - - if (field & UDSEntry::UDS_STRING) { - if (a.stringValue(field) != b.stringValue(field)) { - return false; - } - } else { - if (a.numberValue(field) != b.numberValue(field)) { - return false; - } - } - } - - return true; -} -} void UDSEntryBenchmark::loadSmallEntries() { // Save the entries if that has not been done yet.