Fixed Bug in QVariant comparison when containing QStringLists

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 <jedrzej.nowacki@theqtcompany.com>
This commit is contained in:
Clemens Sielaff 2016-07-24 11:21:34 +12:00 committed by Jędrzej Nowacki
parent dbb5c95f4d
commit 2a24c3c268
2 changed files with 57 additions and 0 deletions

View File

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

View File

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