summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/kernel/qmetatype
diff options
context:
space:
mode:
authorJędrzej Nowacki <jedrzej.nowacki@nokia.com>2011-12-20 17:11:46 +0100
committerQt by Nokia <qt-info@nokia.com>2012-02-16 02:00:15 +0100
commit214e031d56714ba69ef929f1e763e243b393e460 (patch)
tree4732bd79e015eecb85e66ba3a3facc58d5fba9ae /tests/auto/corelib/kernel/qmetatype
parentb9eb3715f55378802a1a0ae2f61d799ab84ee49a (diff)
Implement new static less API for QMetaType.
Currently QMetaType API contains almost only static methods. This works nice until someone needs more information or needs to do more operations on a type. In this case every function call has to do type dispatch. This API allows to avoid redundant type dispatching, by caching a type information in a QMetaType instance. It gives significant performance boost especially for custom types (up to 9x). Change-Id: I223d066268402e072e41ca1d0a3e7bc160655d7f Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com> Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Diffstat (limited to 'tests/auto/corelib/kernel/qmetatype')
-rw-r--r--tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp156
1 files changed, 133 insertions, 23 deletions
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
index f90e7f463f..72913d10f2 100644
--- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
+++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
@@ -78,8 +78,12 @@ private slots:
void createCopy();
void sizeOf_data();
void sizeOf();
+ void sizeOfStaticLess_data();
+ void sizeOfStaticLess();
void flags_data();
void flags();
+ void flagsStaticLess_data();
+ void flagsStaticLess();
void construct_data();
void construct();
void constructCopy_data();
@@ -88,6 +92,8 @@ private slots:
void registerType();
void isRegistered_data();
void isRegistered();
+ void isRegisteredStaticLess_data();
+ void isRegisteredStaticLess();
void registerStreamBuiltin();
void automaticTemplateRegistration();
};
@@ -125,6 +131,9 @@ class MetaTypeTorturer: public QThread
protected:
void run()
{
+ Bar space[1];
+ space[0].~Bar();
+
for (int i = 0; i < 1000; ++i) {
const QByteArray name = QString("Bar%1_%2").arg(i).arg((size_t)QThread::currentThreadId()).toLatin1();
const char *nm = name.constData();
@@ -132,6 +141,15 @@ protected:
#ifdef Q_OS_LINUX
pthread_yield();
#endif
+ QMetaType info(tp);
+ if (!info.isValid()) {
+ ++failureCount;
+ qWarning() << "Wrong typeInfo returned for" << tp;
+ }
+ if (!info.isRegistered()) {
+ ++failureCount;
+ qWarning() << name << "is not a registered metatype";
+ }
if (QMetaType::typeFlags(tp) != (QMetaType::NeedsConstruction | QMetaType::NeedsDestruction)) {
++failureCount;
qWarning() << "Wrong typeInfo returned for" << tp;
@@ -148,9 +166,22 @@ protected:
++failureCount;
qWarning() << "Wrong typeName returned for" << tp;
}
- void *buf = QMetaType::create(tp, 0);
- void *buf2 = QMetaType::create(tp, buf);
- if (!buf) {
+ void *buf1 = QMetaType::create(tp, 0);
+ void *buf2 = QMetaType::create(tp, buf1);
+ void *buf3 = info.create(tp, 0);
+ void *buf4 = info.create(tp, buf1);
+
+ QMetaType::construct(tp, space, 0);
+ QMetaType::destruct(tp, space);
+ QMetaType::construct(tp, space, buf1);
+ QMetaType::destruct(tp, space);
+
+ info.construct(space, 0);
+ info.destruct(space);
+ info.construct(space, buf1);
+ info.destruct(space);
+
+ if (!buf1) {
++failureCount;
qWarning() << "Null buffer returned by QMetaType::create(tp, 0)";
}
@@ -158,9 +189,20 @@ protected:
++failureCount;
qWarning() << "Null buffer returned by QMetaType::create(tp, buf)";
}
- QMetaType::destroy(tp, buf);
+ if (!buf3) {
+ ++failureCount;
+ qWarning() << "Null buffer returned by info.create(tp, 0)";
+ }
+ if (!buf4) {
+ ++failureCount;
+ qWarning() << "Null buffer returned by infocreate(tp, buf)";
+ }
+ QMetaType::destroy(tp, buf1);
QMetaType::destroy(tp, buf2);
+ info.destroy(buf3);
+ info.destroy(buf4);
}
+ new (space) Bar;
}
public:
MetaTypeTorturer() : failureCount(0) { }
@@ -480,13 +522,17 @@ template<int ID>
static void testCreateHelper()
{
typedef typename MetaEnumToType<ID>::Type Type;
- void *actual = QMetaType::create(ID);
+ QMetaType info(ID);
+ void *actual1 = QMetaType::create(ID);
+ void *actual2 = info.create();
if (DefaultValueTraits<ID>::IsInitialized) {
Type *expected = DefaultValueFactory<ID>::create();
- QCOMPARE(*static_cast<Type *>(actual), *expected);
+ QCOMPARE(*static_cast<Type *>(actual1), *expected);
+ QCOMPARE(*static_cast<Type *>(actual2), *expected);
delete expected;
}
- QMetaType::destroy(ID, actual);
+ QMetaType::destroy(ID, actual1);
+ info.destroy(actual2);
}
template<>
@@ -529,9 +575,13 @@ static void testCreateCopyHelper()
{
typedef typename MetaEnumToType<ID>::Type Type;
Type *expected = TestValueFactory<ID>::create();
- void *actual = QMetaType::create(ID, expected);
- QCOMPARE(*static_cast<Type *>(actual), *expected);
- QMetaType::destroy(ID, actual);
+ QMetaType info(ID);
+ void *actual1 = QMetaType::create(ID, expected);
+ void *actual2 = info.create(expected);
+ QCOMPARE(*static_cast<Type *>(actual1), *expected);
+ QCOMPARE(*static_cast<Type *>(actual2), *expected);
+ QMetaType::destroy(ID, actual1);
+ info.destroy(actual2);
delete expected;
}
@@ -588,6 +638,18 @@ void tst_QMetaType::sizeOf()
QCOMPARE(QMetaType::sizeOf(type), size);
}
+void tst_QMetaType::sizeOfStaticLess_data()
+{
+ sizeOf_data();
+}
+
+void tst_QMetaType::sizeOfStaticLess()
+{
+ QFETCH(QMetaType::Type, type);
+ QFETCH(int, size);
+ QCOMPARE(QMetaType(type).sizeOf(), size);
+}
+
struct CustomMovable {};
QT_BEGIN_NAMESPACE
Q_DECLARE_TYPEINFO(CustomMovable, Q_MOVABLE_TYPE);
@@ -653,6 +715,23 @@ void tst_QMetaType::flags()
QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::PointerToQObject), isPointerToQObject);
}
+void tst_QMetaType::flagsStaticLess_data()
+{
+ flags_data();
+}
+
+void tst_QMetaType::flagsStaticLess()
+{
+ QFETCH(int, type);
+ QFETCH(bool, isMovable);
+ QFETCH(bool, isComplex);
+
+ int flags = QMetaType(type).flags();
+ QCOMPARE(bool(flags & QMetaType::NeedsConstruction), isComplex);
+ QCOMPARE(bool(flags & QMetaType::NeedsDestruction), isComplex);
+ QCOMPARE(bool(flags & QMetaType::MovableType), isMovable);
+}
+
void tst_QMetaType::construct_data()
{
create_data();
@@ -688,20 +767,30 @@ template<int ID>
static void testConstructHelper()
{
typedef typename MetaEnumToType<ID>::Type Type;
- int size = QMetaType::sizeOf(ID);
- void *storage = qMallocAligned(size, TypeAlignment<Type>::Value);
- void *actual = QMetaType::construct(ID, storage, /*copy=*/0);
- QCOMPARE(actual, storage);
+ QMetaType info(ID);
+ int size = info.sizeOf();
+ void *storage1 = qMallocAligned(size, TypeAlignment<Type>::Value);
+ void *actual1 = QMetaType::construct(ID, storage1, /*copy=*/0);
+ void *storage2 = qMallocAligned(size, TypeAlignment<Type>::Value);
+ void *actual2 = info.construct(storage2, /*copy=*/0);
+ QCOMPARE(actual1, storage1);
+ QCOMPARE(actual2, storage2);
if (DefaultValueTraits<ID>::IsInitialized) {
Type *expected = DefaultValueFactory<ID>::create();
- QCOMPARE(*static_cast<Type *>(actual), *expected);
+ QCOMPARE(*static_cast<Type *>(actual1), *expected);
+ QCOMPARE(*static_cast<Type *>(actual2), *expected);
delete expected;
}
- QMetaType::destruct(ID, actual);
- qFreeAligned(storage);
+ QMetaType::destruct(ID, actual1);
+ qFreeAligned(storage1);
+ info.destruct(actual2);
+ qFreeAligned(storage2);
QVERIFY(QMetaType::construct(ID, 0, /*copy=*/0) == 0);
QMetaType::destruct(ID, 0);
+
+ QVERIFY(info.construct(0, /*copy=*/0) == 0);
+ info.destruct(0);
}
template<>
@@ -748,15 +837,24 @@ static void testConstructCopyHelper()
{
typedef typename MetaEnumToType<ID>::Type Type;
Type *expected = TestValueFactory<ID>::create();
+ QMetaType info(ID);
int size = QMetaType::sizeOf(ID);
- void *storage = qMallocAligned(size, TypeAlignment<Type>::Value);
- void *actual = QMetaType::construct(ID, storage, expected);
- QCOMPARE(actual, storage);
- QCOMPARE(*static_cast<Type *>(actual), *expected);
- QMetaType::destruct(ID, actual);
- qFreeAligned(storage);
+ QCOMPARE(info.sizeOf(), size);
+ void *storage1 = qMallocAligned(size, TypeAlignment<Type>::Value);
+ void *actual1 = QMetaType::construct(ID, storage1, expected);
+ void *storage2 = qMallocAligned(size, TypeAlignment<Type>::Value);
+ void *actual2 = info.construct(storage2, expected);
+ QCOMPARE(actual1, storage1);
+ QCOMPARE(actual2, storage2);
+ QCOMPARE(*static_cast<Type *>(actual1), *expected);
+ QCOMPARE(*static_cast<Type *>(actual2), *expected);
+ QMetaType::destruct(ID, actual1);
+ qFreeAligned(storage1);
+ info.destruct(actual2);
+ qFreeAligned(storage2);
QVERIFY(QMetaType::construct(ID, 0, expected) == 0);
+ QVERIFY(info.construct(0, expected) == 0);
delete expected;
}
@@ -895,6 +993,18 @@ void tst_QMetaType::isRegistered()
QCOMPARE(QMetaType::isRegistered(typeId), registered);
}
+void tst_QMetaType::isRegisteredStaticLess_data()
+{
+ isRegistered_data();
+}
+
+void tst_QMetaType::isRegisteredStaticLess()
+{
+ QFETCH(int, typeId);
+ QFETCH(bool, registered);
+ QCOMPARE(QMetaType(typeId).isRegistered(), registered);
+}
+
void tst_QMetaType::registerStreamBuiltin()
{
//should not crash;