From 2a24c3c268d443fb260d731d9f65b57726a40354 Mon Sep 17 00:00:00 2001 From: Clemens Sielaff Date: Sun, 24 Jul 2016 11:21:34 +1200 Subject: [PATCH] Fixed Bug in QVariant comparison when containing QStringLists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As it were, QStringLists were not handled explicitly when comparing QVariants. If both QStringLists contained only a single entry, they were treated as QStrings - if both QStringLists were empty, there were equal (correctly so) - but if one of the QStringLists had more than one entry, the compare function fell through to returning always 1. As discussed here: https://stackoverflow.com/a/38492467/3444217 Added rich comparison tests for all non-numerical, non-recursive QVariants that support them (except QModelIndex and QPersistentModelIndex) Task-number: QTBUG-54893 Change-Id: Icc5480d9ba056ee5efe83da566c5829caa1509d7 Reviewed-by: Jędrzej Nowacki --- src/corelib/kernel/qvariant.cpp | 2 + .../corelib/kernel/qvariant/tst_qvariant.cpp | 55 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 7596699843..4f256cccda 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -3560,6 +3560,8 @@ int QVariant::compare(const QVariant &v) const return v1.toTime() < v2.toTime() ? -1 : 1; case QVariant::DateTime: return v1.toDateTime() < v2.toDateTime() ? -1 : 1; + case QVariant::StringList: + return v1.toStringList() < v2.toStringList() ? -1 : 1; } int r = v1.toString().compare(v2.toString(), Qt::CaseInsensitive); if (r == 0) { diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index 76230ccec8..509eed231d 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -279,6 +279,7 @@ private slots: void metaEnums(); void compareSanity_data(); void compareSanity(); + void compareRich(); void accessSequentialContainerKey(); @@ -4745,6 +4746,60 @@ void tst_QVariant::compareSanity() } } +static void richComparison(const QVariant& less, const QVariant& more) +{ + QVERIFY(less.type() == more.type()); + + QVERIFY(less < more); + QVERIFY(!(more < less)); + + QVERIFY(more > less); + QVERIFY(!(less > more)); + + QVERIFY(less <= more); + QVERIFY(!(more <= less)); + QVERIFY(less <= less); + + QVERIFY(more >= less); + QVERIFY(!(less >= more)); + QVERIFY(more >= more); +} + +void tst_QVariant::compareRich() +{ + richComparison(QUuid("{49d8ad2a-2ee8-4c3d-949f-1b5a3765ddf0}"), + QUuid("{f6d56824-16e9-4543-a375-add2877c2d05}")); + richComparison(QByteArray::fromRawData("a", 1), + QByteArray::fromRawData("b", 1)); + richComparison(QStringLiteral("a"), QStringLiteral("b")); + richComparison(QLatin1String("a"), QLatin1String("b")); + richComparison(QChar('a'), QChar('b')); + richComparison(QDate(2016, 7, 23), QDate(2016, 7, 24)); + richComparison(QTime(0, 0), QTime(0, 1)); + richComparison(QDateTime(QDate(2016, 7, 23), QTime(0, 0)), + QDateTime(QDate(2016, 7, 23), QTime(0, 1))); + + richComparison(QStringList(), QStringList() << QStringLiteral("a")); + richComparison(QStringList(), QStringList() << QStringLiteral("a") + << QStringLiteral("b")); + richComparison(QStringList() << QStringLiteral("a"), + QStringList() << QStringLiteral("b")); + richComparison(QStringList() << QStringLiteral("a"), + QStringList() << QStringLiteral("b") + << QStringLiteral("c")); + richComparison(QStringList() << QStringLiteral("a") + << QStringLiteral("c"), + QStringList() << QStringLiteral("b")); + richComparison(QStringList() << QStringLiteral("a") + << QStringLiteral("c"), + QStringList() << QStringLiteral("b") + << QStringLiteral("d")); + richComparison(QStringList() << QStringLiteral("a") + << QStringLiteral("c"), + QStringList() << QStringLiteral("a") + << QStringLiteral("d")); +} + void tst_QVariant::accessSequentialContainerKey() { QString nameResult;