summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2014-10-31 16:28:47 -0700
committerThiago Macieira <thiago.macieira@intel.com>2014-11-20 05:46:40 +0100
commit849604e2f98296ddcde4f1e323e2383ef4f0dd49 (patch)
tree5b51224f8bb2b1f6eb6ba83762f6d8bd59eefd0f
parent8153386397087ce4f5c8997992edf5c1fd38b8db (diff)
Fix asymmetry in QVariant::cmp when A converts to B but not the opposite
This would result in (a == b) != (b == a). The == operation should be commutative as much as possible. Now, there's still an asymmetry in that b is forced to a type and the conversion may fail. QVariant should have an idea of what conversions are "promotion" and which ones are "demotion" (subject to loss of data and/or can fail), so it can do the promotion first Task-number: QTBUG-42254 Change-Id: I9fa4496bbbf0f8719ff8456cc24247290beac608 Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com>
-rw-r--r--src/corelib/kernel/qvariant.cpp11
-rw-r--r--tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp9
2 files changed, 17 insertions, 3 deletions
diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp
index 73a951c39b..67911f2836 100644
--- a/src/corelib/kernel/qvariant.cpp
+++ b/src/corelib/kernel/qvariant.cpp
@@ -3161,8 +3161,15 @@ bool QVariant::cmp(const QVariant &v) const
else
return toLongLong() == v.toLongLong();
}
- if (!v2.canConvert(v1.d.type) || !v2.convert(v1.d.type))
- return false;
+ if (v2.canConvert(v1.d.type)) {
+ if (!v2.convert(v1.d.type))
+ return false;
+ } else {
+ // try the opposite conversion, it might work
+ qSwap(v1, v2);
+ if (!v2.convert(v1.d.type))
+ return false;
+ }
}
if (v1.d.type >= QMetaType::User) {
int result;
diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp
index 10c7e425f0..eae6311854 100644
--- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp
+++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp
@@ -43,7 +43,7 @@
#include <qlocale.h>
#include <qdebug.h>
#include <qjsondocument.h>
-
+#include <quuid.h>
#include <limits.h>
#include <float.h>
@@ -1437,6 +1437,13 @@ void tst_QVariant::operator_eq_eq_data()
// ### many other combinations missing
{
+ // QUuid can convert to QString, but not the opposite
+ QUuid uuid(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
+ QTest::newRow("uuidstring") << QVariant(uuid) << QVariant(uuid.toString()) << true;
+ QTest::newRow("stringuuid") << QVariant(uuid.toString()) << QVariant(uuid) << true;
+ }
+
+ {
QMap<QString, QVariant> map1;
map1.insert( "X", 1 );