From d2ed5884192202dae8efefa66b9bdc2caaf8c551 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Tue, 1 Nov 2011 11:24:10 +0100 Subject: [PATCH] tst_qvariant: add a test QVariant works with not movable types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Soon, QVariant will use more internal storage. It is important that it still work with not movable types Also, check with type that are movable but cannot be copyed without their copy constructor to be called such as QSharedDataPointer Change-Id: I6d67755476e4822468599bebfa8774ad96a15306 Reviewed-by: Jędrzej Nowacki --- .../corelib/kernel/qvariant/tst_qvariant.cpp | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index d307564028..f126151a4c 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -3055,6 +3055,34 @@ struct MyMovable int MyMovable::count = 0; +struct MyNotMovable +{ + static int count; + MyNotMovable *that; + MyNotMovable() : that(this) { count++; } + ~MyNotMovable() { QCOMPARE(that, this); count--; } + MyNotMovable(const MyNotMovable &o) : that(this) { QCOMPARE(o.that, &o); count++; } + MyNotMovable &operator=(const MyNotMovable &o) { + bool ok = that == this && o.that == &o; + if (!ok) qFatal("MyNotMovable has been moved"); + return *this; + } + + //PLAY_WITH_VARIANT test that they are equal, but never that they are not equal + // so it would be fine just to always return true + bool operator==(const MyNotMovable &o) const + { + bool ok = that == this && o.that == &o; + if (!ok) qFatal("MyNotMovable has been moved"); + return ok; + } +}; + +int MyNotMovable::count = 0; + +struct MyShared : QSharedData { + MyMovable movable; +}; QT_BEGIN_NAMESPACE Q_DECLARE_TYPEINFO(MyMovable, Q_MOVABLE_TYPE); @@ -3064,12 +3092,16 @@ Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(MyPrimitive) Q_DECLARE_METATYPE(MyData) Q_DECLARE_METATYPE(MyMovable) +Q_DECLARE_METATYPE(MyNotMovable) Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(QList) +Q_DECLARE_METATYPE(QList) Q_DECLARE_METATYPE(MyPrimitive *) Q_DECLARE_METATYPE(MyData *) Q_DECLARE_METATYPE(MyMovable *) +Q_DECLARE_METATYPE(MyNotMovable *) +Q_DECLARE_METATYPE(QSharedDataPointer) void tst_QVariant::moreCustomTypes() @@ -3125,6 +3157,18 @@ void tst_QVariant::moreCustomTypes() } QCOMPARE(MyMovable::count, 0); + QCOMPARE(MyNotMovable::count, 0); + { + MyNotMovable d; + PLAY_WITH_VARIANT(d, false, QString(), 0, false); + PLAY_WITH_VARIANT(&d, false, QString(), 0, false); + QList l; + PLAY_WITH_VARIANT(l, false, QString(), 0, false); + l << MyNotMovable() << d; + PLAY_WITH_VARIANT(l, false, QString(), 0, false); + } + QCOMPARE(MyNotMovable::count, 0); + { PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true); PLAY_WITH_VARIANT(12.12f, false, "12.12", 12.12f, true); @@ -3170,6 +3214,13 @@ void tst_QVariant::moreCustomTypes() PLAY_WITH_VARIANT(v5, false, QString(), 0, false); } + + QCOMPARE(MyMovable::count, 0); + { + QSharedDataPointer d(new MyShared); + PLAY_WITH_VARIANT(d, false, QString(), 0, false); + } + QCOMPARE(MyMovable::count, 0); }