tst_qguimetatype: Avoid deprecated methods

This makes the 5.15 and 6.x branches more comparable, as in 6.0 the
preferred way is to use the non-static methods (which avoids an
expensive lookup in 6.x).
As a drive-by, Avoid memory leaks if the test fails.

Change-Id: I95b133342a4ea19dd23c235a408f38089706412b
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
(cherry picked from commit a7f24218e3795aa54effc2665e0a505c02b10382)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Fabian Kosmale 2021-03-16 09:27:50 +01:00 committed by Qt Cherry-pick Bot
parent c0220acd71
commit 5c8701aebe

View File

@ -28,6 +28,7 @@
#include <qtest.h> #include <qtest.h>
#include <QtCore/qmetatype.h> #include <QtCore/qmetatype.h>
#include <QScopeGuard>
class tst_QGuiMetaType : public QObject class tst_QGuiMetaType : public QObject
{ {
@ -62,17 +63,20 @@ void tst_QGuiMetaType::constructInPlace_data()
void tst_QGuiMetaType::constructInPlace() void tst_QGuiMetaType::constructInPlace()
{ {
QFETCH(int, typeId); QFETCH(int, typeId);
int size = QMetaType::sizeOf(typeId); QMetaType type(typeId);
int size = type.sizeOf();
void *storage = qMallocAligned(size, 2 * sizeof(qlonglong)); void *storage = qMallocAligned(size, 2 * sizeof(qlonglong));
QCOMPARE(QMetaType::construct(typeId, storage, /*copy=*/0), storage); auto cleanUp = qScopeGuard([&]() {
QMetaType::destruct(typeId, storage); qFreeAligned(storage);
});
QCOMPARE(type.construct(storage, /*copy=*/0), storage);
type.destruct(storage);
QBENCHMARK { QBENCHMARK {
for (int i = 0; i < 100000; ++i) { for (int i = 0; i < 100000; ++i) {
QMetaType::construct(typeId, storage, /*copy=*/0); type.construct(storage, /*copy=*/0);
QMetaType::destruct(typeId, storage); type.destruct(storage);
} }
} }
qFreeAligned(storage);
} }
void tst_QGuiMetaType::constructInPlaceCopy_data() void tst_QGuiMetaType::constructInPlaceCopy_data()
@ -83,19 +87,22 @@ void tst_QGuiMetaType::constructInPlaceCopy_data()
void tst_QGuiMetaType::constructInPlaceCopy() void tst_QGuiMetaType::constructInPlaceCopy()
{ {
QFETCH(int, typeId); QFETCH(int, typeId);
int size = QMetaType::sizeOf(typeId); QMetaType type(typeId);
int size = type.sizeOf();
void *storage = qMallocAligned(size, 2 * sizeof(qlonglong)); void *storage = qMallocAligned(size, 2 * sizeof(qlonglong));
void *other = QMetaType::create(typeId); void *other = type.create();
QCOMPARE(QMetaType::construct(typeId, storage, other), storage); auto cleanUp = qScopeGuard([&]() {
QMetaType::destruct(typeId, storage); type.destroy(other);
qFreeAligned(storage);
});
QCOMPARE(type.construct(storage, other), storage);
type.destruct(storage);
QBENCHMARK { QBENCHMARK {
for (int i = 0; i < 100000; ++i) { for (int i = 0; i < 100000; ++i) {
QMetaType::construct(typeId, storage, other); type.construct(storage, other);
QMetaType::destruct(typeId, storage); type.destruct(storage);
} }
} }
QMetaType::destroy(typeId, other);
qFreeAligned(storage);
} }
QTEST_MAIN(tst_QGuiMetaType) QTEST_MAIN(tst_QGuiMetaType)