diff options
Diffstat (limited to 'tests/auto/corelib/kernel/qmetatype')
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/CMakeLists.txt | 65 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/lib1.cpp | 5 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/lib2.cpp | 5 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/lib_common.cpp | 13 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp | 874 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h | 57 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp | 295 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/tst_qmetatype3.cpp | 29 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/tst_qmetatype_common.h | 35 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/tst_qmetatype_libs.h | 24 |
10 files changed, 1004 insertions, 398 deletions
diff --git a/tests/auto/corelib/kernel/qmetatype/CMakeLists.txt b/tests/auto/corelib/kernel/qmetatype/CMakeLists.txt index ac4fb55813..b93d961109 100644 --- a/tests/auto/corelib/kernel/qmetatype/CMakeLists.txt +++ b/tests/auto/corelib/kernel/qmetatype/CMakeLists.txt @@ -1,48 +1,63 @@ -# Generated from qmetatype.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_qmetatype Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qmetatype LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + # Collect test data list(APPEND test_data "./typeFlags.bin") +qt_internal_add_cmake_library(qmetatype_lib1 + INSTALL_DIRECTORY "${INSTALL_TESTSDIR}/tst_qmetatype" + SOURCES + lib1.cpp + PUBLIC_LIBRARIES + Qt::Core +) +qt_internal_add_cmake_library(qmetatype_lib2 + INSTALL_DIRECTORY "${INSTALL_TESTSDIR}/tst_qmetatype" + SOURCES + lib2.cpp + PUBLIC_LIBRARIES + Qt::Core +) +set_target_properties(qmetatype_lib1 PROPERTIES + VERSION 1.0.0 + SOVERSION 0 + C_VISIBILITY_PRESET "hidden" + CXX_VISIBILITY_PRESET "hidden" + VISIBILITY_INLINES_HIDDEN ON +) +set_target_properties(qmetatype_lib2 PROPERTIES + VERSION 1.0.0 + SOVERSION 0 + C_VISIBILITY_PRESET "hidden" + CXX_VISIBILITY_PRESET "hidden" + VISIBILITY_INLINES_HIDDEN ON +) + qt_internal_add_test(tst_qmetatype SOURCES tst_qmetatype.h tst_qmetatype.cpp tst_qmetatype2.cpp tst_qmetatype3.cpp - DEFINES - QT_DISABLE_DEPRECATED_BEFORE=0 INCLUDE_DIRECTORIES ../../../other/qvariant_common - PUBLIC_LIBRARIES + LIBRARIES Qt::CorePrivate Qt::Gui + qmetatype_lib1 + qmetatype_lib2 TESTDATA ${test_data} ) -if(INTEGRITY) - # Resources: - set(testdata_resource_files - "typeFlags.bin" - ) - - qt_internal_add_resource(tst_qmetatype "testdata" - PREFIX - "/" - FILES - ${testdata_resource_files} - ) -endif() - -## Scopes: -##################################################################### - qt_internal_extend_target(tst_qmetatype CONDITION MSVC COMPILE_OPTIONS /bigobj ) - -#### Keys ignored in scope 3:.:.:qmetatype.pro:CLANG: -# QMAKE_CFLAGS_RELEASE = "--O2" "--g" -# QMAKE_CXXFLAGS_RELEASE = "--O2" "--g" diff --git a/tests/auto/corelib/kernel/qmetatype/lib1.cpp b/tests/auto/corelib/kernel/qmetatype/lib1.cpp new file mode 100644 index 0000000000..e5478f87d9 --- /dev/null +++ b/tests/auto/corelib/kernel/qmetatype/lib1.cpp @@ -0,0 +1,5 @@ +// Copyright (C) 2022 Intel Corporation +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#define LIB_NAMESPACE Lib1 +#include "lib_common.cpp" diff --git a/tests/auto/corelib/kernel/qmetatype/lib2.cpp b/tests/auto/corelib/kernel/qmetatype/lib2.cpp new file mode 100644 index 0000000000..0a905e669a --- /dev/null +++ b/tests/auto/corelib/kernel/qmetatype/lib2.cpp @@ -0,0 +1,5 @@ +// Copyright (C) 2022 Intel Corporation +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#define LIB_NAMESPACE Lib2 +#include "lib_common.cpp" diff --git a/tests/auto/corelib/kernel/qmetatype/lib_common.cpp b/tests/auto/corelib/kernel/qmetatype/lib_common.cpp new file mode 100644 index 0000000000..179f539ccc --- /dev/null +++ b/tests/auto/corelib/kernel/qmetatype/lib_common.cpp @@ -0,0 +1,13 @@ +// Copyright (C) 2022 Intel Corporation +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include <qcollator.h> +#include "tst_qmetatype_libs.h" + +#define DECLARE_FUNCTION(TYPE, ID) \ + Q_DECL_EXPORT QMetaType metatype_ ## TYPE() \ + { return QMetaType::fromType<TYPE>(); } + +namespace LIB_NAMESPACE { +FOR_EACH_METATYPE_LIBS(DECLARE_FUNCTION) +} diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index f69327a3f1..81bf5d5ea8 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include "tst_qmetatype.h" @@ -38,6 +13,8 @@ #include <memory> #include <vector> +#include <QtCore/qflags.h> + Q_DECLARE_METATYPE(QMetaType::Type) Q_DECLARE_METATYPE(QPartialOrdering) @@ -146,6 +123,26 @@ static_assert(!QTypeTraits::has_operator_less_than_v<std::tuple<int, Nested2>>); static_assert(QTypeTraits::has_operator_equal_v<std::tuple<int, Nested2>>); static_assert(!QTypeTraits::has_operator_less_than_v<std::tuple<int, Nested2>>); +// optionals of nesteds +static_assert(QTypeTraits::has_operator_equal_v<std::optional<std::variant<QString>>>); +static_assert(QTypeTraits::has_operator_less_than_v<std::optional<std::variant<QString>>>); +static_assert(!QTypeTraits::has_operator_equal_v<std::optional<std::variant<NoOperators>>>); +static_assert(!QTypeTraits::has_operator_less_than_v<std::optional<std::variant<NoOperators>>>); + +static_assert(QTypeTraits::has_operator_equal_v<std::optional<Nested>>); +static_assert(!QTypeTraits::has_operator_less_than_v<std::optional<Nested>>); +static_assert(QTypeTraits::has_operator_equal_v<std::optional<std::tuple<int, Nested>>>); +static_assert(!QTypeTraits::has_operator_less_than_v<std::optional<std::tuple<int, Nested>>>); +static_assert(QTypeTraits::has_operator_equal_v<std::optional<std::tuple<int, Nested>>>); +static_assert(!QTypeTraits::has_operator_less_than_v<std::optional<std::tuple<int, Nested>>>); + +static_assert(QTypeTraits::has_operator_equal_v<std::optional<Nested2>>); +static_assert(!QTypeTraits::has_operator_less_than_v<std::optional<Nested2>>); +static_assert(QTypeTraits::has_operator_equal_v<std::optional<std::tuple<int, Nested2>>>); +static_assert(!QTypeTraits::has_operator_less_than_v<std::optional<std::tuple<int, Nested2>>>); +static_assert(QTypeTraits::has_operator_equal_v<std::optional<std::tuple<int, Nested2>>>); +static_assert(!QTypeTraits::has_operator_less_than_v<std::optional<std::tuple<int, Nested2>>>); + } struct BaseGenericType @@ -401,6 +398,7 @@ public: int Bar::failureCount = 0; +#if QT_CONFIG(thread) class MetaTypeTorturer: public QThread { Q_OBJECT @@ -422,55 +420,65 @@ protected: ++failureCount; qWarning() << "Wrong typeInfo returned for" << tp; } - if (!info.isRegistered()) { + if (info.flags() != (QMetaType::NeedsConstruction | QMetaType::NeedsDestruction | + QMetaType::NeedsCopyConstruction | QMetaType::NeedsMoveConstruction)) { ++failureCount; - qWarning() << name << "is not a registered metatype"; + qWarning() << "Wrong typeInfo returned for" << tp << "got" + << Qt::showbase << Qt::hex << info.flags(); } - if (QMetaType::typeFlags(tp) != (QMetaType::NeedsConstruction | QMetaType::NeedsDestruction)) { + if (!info.isRegistered()) { ++failureCount; - qWarning() << "Wrong typeInfo returned for" << tp; + qWarning() << name << "is not a registered metatype"; } if (!QMetaType::isRegistered(tp)) { ++failureCount; qWarning() << name << "is not a registered metatype"; } - if (QMetaType::type(nm) != tp) { + if (QMetaType::fromName(nm).id() != tp) { ++failureCount; qWarning() << "Wrong metatype returned for" << name; } - 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); +#if QT_DEPRECATED_SINCE(6, 0) +QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED + void *buf1 = QMetaType::create(tp, nullptr); + void *buf2 = QMetaType::create(tp, buf1); + QMetaType::construct(tp, space, nullptr); 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)"; + qWarning() << "Null buffer returned by QMetaType::create(tp, nullptr)"; } if (!buf2) { ++failureCount; qWarning() << "Null buffer returned by QMetaType::create(tp, buf)"; } + + QMetaType::destroy(tp, buf1); + QMetaType::destroy(tp, buf2); +QT_WARNING_POP +#endif + + void *buf3 = info.create(nullptr); + void *buf4 = info.create(buf3); + + info.construct(space, nullptr); + info.destruct(space); + info.construct(space, buf3); + info.destruct(space); + if (!buf3) { ++failureCount; - qWarning() << "Null buffer returned by info.create(tp, 0)"; + qWarning() << "Null buffer returned by info.create(nullptr)"; } if (!buf4) { ++failureCount; - qWarning() << "Null buffer returned by infocreate(tp, buf)"; + qWarning() << "Null buffer returned by info.create(buf)"; } - QMetaType::destroy(tp, buf1); - QMetaType::destroy(tp, buf2); + info.destroy(buf3); info.destroy(buf4); } @@ -500,6 +508,7 @@ void tst_QMetaType::threadSafety() QCOMPARE(t3.failureCount, 0); QCOMPARE(Bar::failureCount, 0); } +#endif namespace TestSpace { @@ -518,7 +527,7 @@ void tst_QMetaType::namespaces() QCOMPARE(qvariant_cast<TestSpace::Foo>(v).d, 11.12); int qungTfuId = qRegisterMetaType<ADD_TESTSPACE(QungTfu)>(); - QCOMPARE(QMetaType::typeName(qungTfuId), "TestSpace::QungTfu"); + QCOMPARE(QMetaType(qungTfuId).name(), "TestSpace::QungTfu"); } void tst_QMetaType::id() @@ -532,13 +541,13 @@ void tst_QMetaType::qMetaTypeId() { QCOMPARE(::qMetaTypeId<QString>(), int(QMetaType::QString)); QCOMPARE(::qMetaTypeId<int>(), int(QMetaType::Int)); - QCOMPARE(::qMetaTypeId<TestSpace::Foo>(), QMetaType::type("TestSpace::Foo")); + QCOMPARE(::qMetaTypeId<TestSpace::Foo>(), QMetaType::fromType<TestSpace::Foo>().id()); - QCOMPARE(::qMetaTypeId<char>(), QMetaType::type("char")); - QCOMPARE(::qMetaTypeId<uchar>(), QMetaType::type("unsigned char")); - QCOMPARE(::qMetaTypeId<signed char>(), QMetaType::type("signed char")); + QCOMPARE(::qMetaTypeId<char>(), QMetaType::fromType<char>().id()); + QCOMPARE(::qMetaTypeId<uchar>(), QMetaType::fromType<unsigned char>().id()); + QCOMPARE(::qMetaTypeId<signed char>(), QMetaType::fromType<signed char>().id()); QVERIFY(::qMetaTypeId<signed char>() != ::qMetaTypeId<char>()); - QCOMPARE(::qMetaTypeId<qint8>(), QMetaType::type("qint8")); + QCOMPARE(::qMetaTypeId<qint8>(), QMetaType::fromType<qint8>().id()); } void tst_QMetaType::properties() @@ -550,14 +559,14 @@ void tst_QMetaType::properties() QCOMPARE(v.typeName(), "QVariantList"); QList<QVariant> values = v.toList(); - QCOMPARE(values.count(), 2); + QCOMPARE(values.size(), 2); QCOMPARE(values.at(0).toInt(), 42); values << 43 << "world"; QVERIFY(setProperty("prop", values)); v = property("prop"); - QCOMPARE(v.toList().count(), 4); + QCOMPARE(v.toList().size(), 4); } void tst_QMetaType::normalizedTypes() @@ -565,13 +574,13 @@ void tst_QMetaType::normalizedTypes() int WhityIntId = ::qMetaTypeId<Whity<int> >(); int WhityDoubleId = ::qMetaTypeId<Whity<double> >(); - QCOMPARE(QMetaType::type("Whity<int>"), WhityIntId); - QCOMPARE(QMetaType::type(" Whity < int > "), WhityIntId); - QCOMPARE(QMetaType::type("Whity<int >"), WhityIntId); + QCOMPARE(QMetaType::fromName("Whity<int>").id(), WhityIntId); + QCOMPARE(QMetaType::fromName(" Whity < int > ").id(), WhityIntId); + QCOMPARE(QMetaType::fromName("Whity<int >").id(), WhityIntId); - QCOMPARE(QMetaType::type("Whity<double>"), WhityDoubleId); - QCOMPARE(QMetaType::type(" Whity< double > "), WhityDoubleId); - QCOMPARE(QMetaType::type("Whity<double >"), WhityDoubleId); + QCOMPARE(QMetaType::fromName("Whity<double>").id(), WhityDoubleId); + QCOMPARE(QMetaType::fromName(" Whity< double > ").id(), WhityDoubleId); + QCOMPARE(QMetaType::fromName("Whity<double >").id(), WhityDoubleId); QCOMPARE(qRegisterMetaType<Whity<int> >(" Whity < int > "), WhityIntId); QCOMPARE(qRegisterMetaType<Whity<int> >("Whity<int>"), WhityIntId); @@ -585,6 +594,21 @@ void tst_QMetaType::normalizedTypes() #define TYPENAME_DATA(MetaTypeName, MetaTypeId, RealType)\ QTest::newRow(#RealType) << int(QMetaType::MetaTypeName) << #RealType; +namespace enumerations { + enum Test { a = 0 }; +} + +static void ignoreInvalidMetaTypeWarning(int typeId) +{ + if (typeId < 0 || typeId > QMetaType::User + 500 || + (typeId > QMetaType::LastCoreType && typeId < QMetaType::FirstGuiType) || + (typeId > QMetaType::LastGuiType && typeId < QMetaType::FirstWidgetsType) || + (typeId > QMetaType::LastWidgetsType && typeId < QMetaType::User)) { + QTest::ignoreMessage(QtWarningMsg, "Trying to construct an instance of an invalid type, type id: " + + QByteArray::number(typeId)); + } +} + void tst_QMetaType::typeName_data() { QTest::addColumn<int>("aType"); @@ -621,6 +645,8 @@ void tst_QMetaType::typeName_data() // template instance class derived from Q_GADGET enabled class QTest::newRow("GadgetDerivedAndTyped<int>") << ::qMetaTypeId<GadgetDerivedAndTyped<int>>() << QString::fromLatin1("GadgetDerivedAndTyped<int>"); QTest::newRow("GadgetDerivedAndTyped<int>*") << ::qMetaTypeId<GadgetDerivedAndTyped<int>*>() << QString::fromLatin1("GadgetDerivedAndTyped<int>*"); + + QTest::newRow("msvcKeywordPartOfName") << ::qMetaTypeId<enumerations::Test>() << QString::fromLatin1("enumerations::Test"); } void tst_QMetaType::typeName() @@ -631,13 +657,15 @@ void tst_QMetaType::typeName() if (aType >= QMetaType::FirstWidgetsType) QSKIP("The test doesn't link against QtWidgets."); - const char *rawname = QMetaType::typeName(aType); + ignoreInvalidMetaTypeWarning(aType); + const char *rawname = QMetaType(aType).name(); QString name = QString::fromLatin1(rawname); QCOMPARE(name, aTypeName); QCOMPARE(name.toLatin1(), QMetaObject::normalizedType(name.toLatin1().constData())); QCOMPARE(rawname == nullptr, aTypeName.isNull()); + ignoreInvalidMetaTypeWarning(aType); QMetaType mt(aType); if (mt.isValid()) { // Gui type are not valid QCOMPARE(QString::fromLatin1(QMetaType(aType).name()), aTypeName); @@ -671,10 +699,8 @@ void tst_QMetaType::type() if (aType >= QMetaType::FirstWidgetsType) QSKIP("The test doesn't link against QtWidgets."); - // QMetaType::type(QByteArray) - QCOMPARE(QMetaType::type(aTypeName), aType); - // QMetaType::type(const char *) - QCOMPARE(QMetaType::type(aTypeName.constData()), aType); + QCOMPARE(QMetaType::fromName(aTypeName).id(), aType); + QCOMPARE(QMetaType::fromName(aTypeName.constData()).id(), aType); } void tst_QMetaType::type_fromSubString_data() @@ -699,7 +725,7 @@ void tst_QMetaType::type_fromSubString() QFETCH(int, size); QFETCH(int, expectedType); QByteArray ba = QByteArray::fromRawData(types + offset, size); - QCOMPARE(QMetaType::type(ba), expectedType); + QCOMPARE(QMetaType::fromName(ba).id(), expectedType); } namespace { @@ -726,7 +752,7 @@ void tst_QMetaType::create_data() QTest::addColumn<int>("type"); QTest::newRow("unknown-type") << int(QMetaType::UnknownType); #define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \ - QTest::newRow(QMetaType::typeName(QMetaType::MetaTypeName)) << int(QMetaType::MetaTypeName); + QTest::newRow(QMetaType(QMetaType::MetaTypeName).name()) << int(QMetaType::MetaTypeName); FOR_EACH_CORE_METATYPE(ADD_METATYPE_TEST_ROW) #undef ADD_METATYPE_TEST_ROW } @@ -735,23 +761,36 @@ template<int ID> static void testCreateHelper() { typedef typename MetaEnumToType<ID>::Type Type; - QMetaType info(ID); + auto expected = std::unique_ptr<Type>(DefaultValueFactory<ID>::create()); +#if QT_DEPRECATED_SINCE(6, 0) +QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED void *actual1 = QMetaType::create(ID); - void *actual2 = info.create(); - Type *expected = DefaultValueFactory<ID>::create(); + auto cleanup1 = qScopeGuard([actual1]() { + QMetaType::destroy(ID, actual1); + }); QCOMPARE(*static_cast<Type *>(actual1), *expected); +QT_WARNING_POP +#endif // QT_DEPRECATED_SINCE(6, 0) + QMetaType info(ID); + void *actual2 = info.create(); + auto cleanup2 = qScopeGuard([&info, actual2]() { + info.destroy(actual2); + }); QCOMPARE(*static_cast<Type *>(actual2), *expected); - delete expected; - - QMetaType::destroy(ID, actual1); - info.destroy(actual2); } template<> void testCreateHelper<QMetaType::Void>() { - void *actual = QMetaType::create(QMetaType::Void); - QMetaType::destroy(QMetaType::Void, actual); +#if QT_DEPRECATED_SINCE(6, 0) +QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED + void *actual1 = QMetaType::create(QMetaType::Void); + QMetaType::destroy(QMetaType::Void, actual1); +QT_WARNING_POP +#endif // QT_DEPRECATED_SINCE(6, 0) + QMetaType info(QMetaType::Void); + void *actual2 = info.create(); + info.destroy(actual2); } @@ -786,15 +825,22 @@ template<int ID> static void testCreateCopyHelper() { typedef typename MetaEnumToType<ID>::Type Type; - Type *expected = TestValueFactory<ID>::create(); - QMetaType info(ID); - void *actual1 = QMetaType::create(ID, expected); - void *actual2 = info.create(expected); + auto expected = std::unique_ptr<Type>(TestValueFactory<ID>::create()); +#if QT_DEPRECATED_SINCE(6, 0) +QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED + void *actual1 = QMetaType::create(ID, expected.get()); + auto cleanup1 = qScopeGuard([actual1]() { + QMetaType::destroy(ID, actual1); + }); QCOMPARE(*static_cast<Type *>(actual1), *expected); +QT_WARNING_POP +#endif // QT_DEPRECATED_SINCE(6, 0) + QMetaType info(ID); + void *actual2 = info.create(expected.get()); + auto cleanup2 = qScopeGuard([&info, actual2]() { + info.destroy(actual2); + }); QCOMPARE(*static_cast<Type *>(actual2), *expected); - QMetaType::destroy(ID, actual1); - info.destroy(actual2); - delete expected; } template<> @@ -802,9 +848,22 @@ void testCreateCopyHelper<QMetaType::Void>() { typedef MetaEnumToType<QMetaType::Void>::Type Type; Type *expected = TestValueFactory<QMetaType::Void>::create(); - void *actual = QMetaType::create(QMetaType::Void, expected); - QCOMPARE(static_cast<Type *>(actual), expected); - QMetaType::destroy(QMetaType::Void, actual); + QCOMPARE(expected, nullptr); // we do not need to delete it +#if QT_DEPRECATED_SINCE(6, 0) +QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED + void *actual1 = QMetaType::create(QMetaType::Void, expected); + auto cleanup1 = qScopeGuard([actual1]() { + QMetaType::destroy(QMetaType::Void, actual1); + }); + QCOMPARE(static_cast<Type *>(actual1), expected); +QT_WARNING_POP +#endif // QT_DEPRECATED_SINCE(6, 0) + QMetaType info(QMetaType::Void); + void *actual2 = info.create(expected); + auto cleanup2 = qScopeGuard([&info, actual2]() { + info.destroy(actual2); + }); + QCOMPARE(static_cast<Type *>(actual2), expected); } void tst_QMetaType::createCopy_data() @@ -821,7 +880,7 @@ void tst_QMetaType::createCopy() switch (type) { case QMetaType::UnknownType: return []() { - char buf[1]; + char buf[1] = {}; QCOMPARE(QMetaType().create(&buf), nullptr); }; #define RETURN_CREATE_COPY_FUNCTION(MetaTypeName, MetaTypeId, RealType) \ @@ -867,7 +926,9 @@ void tst_QMetaType::sizeOf() { QFETCH(int, type); QFETCH(size_t, size); - QCOMPARE(size_t(QMetaType::sizeOf(type)), size); + ignoreInvalidMetaTypeWarning(type); + QMetaType metaType(type); + QCOMPARE(size_t(metaType.sizeOf()), size); } void tst_QMetaType::sizeOfStaticLess_data() @@ -879,6 +940,7 @@ void tst_QMetaType::sizeOfStaticLess() { QFETCH(int, type); QFETCH(size_t, size); + ignoreInvalidMetaTypeWarning(type); QCOMPARE(size_t(QMetaType(type).sizeOf()), size); } @@ -915,6 +977,7 @@ void tst_QMetaType::alignOf() { QFETCH(int, type); QFETCH(size_t, size); + ignoreInvalidMetaTypeWarning(type); QCOMPARE(size_t(QMetaType(type).alignOf()), size); } @@ -979,80 +1042,157 @@ Q_DECLARE_METATYPE(QPairPP) enum FlagsDataEnum {}; Q_DECLARE_METATYPE(FlagsDataEnum); +template <typename T> void addFlagsRow(const char *name, int id = qMetaTypeId<T>()) +{ + QTest::newRow(name) + << id + << bool(QTypeInfo<T>::isRelocatable) + << bool(!std::is_default_constructible_v<T> || !QTypeInfo<T>::isValueInitializationBitwiseZero) + << bool(!std::is_trivially_copy_constructible_v<T>) + << bool(!std::is_trivially_destructible_v<T>) + << bool(QtPrivate::IsPointerToTypeDerivedFromQObject<T>::Value) + << bool(std::is_enum<T>::value) + << false; +} + void tst_QMetaType::flags_data() { QTest::addColumn<int>("type"); - QTest::addColumn<bool>("isMovable"); - QTest::addColumn<bool>("isComplex"); + QTest::addColumn<bool>("isRelocatable"); + QTest::addColumn<bool>("needsConstruction"); + QTest::addColumn<bool>("needsCopyConstruction"); + QTest::addColumn<bool>("needsDestruction"); QTest::addColumn<bool>("isPointerToQObject"); QTest::addColumn<bool>("isEnum"); QTest::addColumn<bool>("isQmlList"); + // invalid ids. + QTest::newRow("-1") << -1 << false << false << false << false << false << false << false; + QTest::newRow("-124125534") << -124125534 << false << false << false << false << false << false << false; + QTest::newRow("124125534") << 124125534 << false << false << false << false << false << false << false; + #define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \ - QTest::newRow(#RealType) << MetaTypeId \ - << bool(QTypeInfo<RealType>::isRelocatable) \ - << bool(QTypeInfo<RealType>::isComplex) \ - << bool(QtPrivate::IsPointerToTypeDerivedFromQObject<RealType>::Value) \ - << bool(std::is_enum<RealType>::value) \ - << false; + addFlagsRow<RealType>(#RealType, MetaTypeId); +QT_FOR_EACH_STATIC_PRIMITIVE_NON_VOID_TYPE(ADD_METATYPE_TEST_ROW) QT_FOR_EACH_STATIC_CORE_CLASS(ADD_METATYPE_TEST_ROW) QT_FOR_EACH_STATIC_PRIMITIVE_POINTER(ADD_METATYPE_TEST_ROW) QT_FOR_EACH_STATIC_CORE_POINTER(ADD_METATYPE_TEST_ROW) #undef ADD_METATYPE_TEST_ROW - QTest::newRow("TestSpace::Foo") << ::qMetaTypeId<TestSpace::Foo>() << false << true << false << false << false; - QTest::newRow("Whity<double>") << ::qMetaTypeId<Whity<double> >() << true << true << false << false << false; - QTest::newRow("CustomMovable") << ::qMetaTypeId<CustomMovable>() << true << true << false << false << false; - QTest::newRow("CustomObject*") << ::qMetaTypeId<CustomObject*>() << true << false << true << false << false; - QTest::newRow("CustomMultiInheritanceObject*") << ::qMetaTypeId<CustomMultiInheritanceObject*>() << true << false << true << false << false; - QTest::newRow("QPair<C,C>") << ::qMetaTypeId<QPair<C,C> >() << false << true << false << false << false; - QTest::newRow("QPair<C,M>") << ::qMetaTypeId<QPair<C,M> >() << false << true << false << false << false; - QTest::newRow("QPair<C,P>") << ::qMetaTypeId<QPair<C,P> >() << false << true << false << false << false; - QTest::newRow("QPair<M,C>") << ::qMetaTypeId<QPair<M,C> >() << false << true << false << false << false; - QTest::newRow("QPair<M,M>") << ::qMetaTypeId<QPair<M,M> >() << true << true << false << false << false; - QTest::newRow("QPair<M,P>") << ::qMetaTypeId<QPair<M,P> >() << true << true << false << false << false; - QTest::newRow("QPair<P,C>") << ::qMetaTypeId<QPair<P,C> >() << false << true << false << false << false; - QTest::newRow("QPair<P,M>") << ::qMetaTypeId<QPair<P,M> >() << true << true << false << false << false; - QTest::newRow("QPair<P,P>") << ::qMetaTypeId<QPair<P,P> >() << true << false << false << false << false; - QTest::newRow("FlagsDataEnum") << ::qMetaTypeId<FlagsDataEnum>() << true << false << false << true << false; - - // invalid ids. - QTest::newRow("-1") << -1 << false << false << false << false << false; - QTest::newRow("-124125534") << -124125534 << false << false << false << false << false; - QTest::newRow("124125534") << 124125534 << false << false << false << false << false; + addFlagsRow<TestSpace::Foo>("TestSpace::Foo"); + addFlagsRow<Whity<double> >("Whity<double> "); + addFlagsRow<CustomMovable>("CustomMovable"); + addFlagsRow<CustomObject*>("CustomObject*"); + addFlagsRow<CustomMultiInheritanceObject*>("CustomMultiInheritanceObject*"); + addFlagsRow<QPair<C,C> >("QPair<C,C>"); + addFlagsRow<QPair<C,M> >("QPair<C,M>"); + addFlagsRow<QPair<C,P> >("QPair<C,P>"); + addFlagsRow<QPair<M,C> >("QPair<M,C>"); + addFlagsRow<QPair<M,M> >("QPair<M,M>"); + addFlagsRow<QPair<M,P> >("QPair<M,P>"); + addFlagsRow<QPair<P,C> >("QPair<P,C>"); + addFlagsRow<QPair<P,M> >("QPair<P,M>"); + addFlagsRow<QPair<P,P> >("QPair<P,P>"); + addFlagsRow<FlagsDataEnum>("FlagsDataEnum"); } void tst_QMetaType::flags() { QFETCH(int, type); - QFETCH(bool, isMovable); - QFETCH(bool, isComplex); + QFETCH(bool, isRelocatable); + QFETCH(bool, needsConstruction); + QFETCH(bool, needsCopyConstruction); + QFETCH(bool, needsDestruction); QFETCH(bool, isPointerToQObject); QFETCH(bool, isEnum); QFETCH(bool, isQmlList); - QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::NeedsConstruction), isComplex); - QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::NeedsDestruction), isComplex); - QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::RelocatableType), isMovable); - QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::PointerToQObject), isPointerToQObject); - QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::IsEnumeration), isEnum); - QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::IsQmlList), isQmlList); + ignoreInvalidMetaTypeWarning(type); + QMetaType meta(type); + + QCOMPARE(bool(meta.flags() & QMetaType::NeedsConstruction), needsConstruction); + QCOMPARE(bool(meta.flags() & QMetaType::NeedsCopyConstruction), needsCopyConstruction); + QCOMPARE(bool(meta.flags() & QMetaType::NeedsDestruction), needsDestruction); + QCOMPARE(bool(meta.flags() & QMetaType::RelocatableType), isRelocatable); + QCOMPARE(bool(meta.flags() & QMetaType::PointerToQObject), isPointerToQObject); + QCOMPARE(bool(meta.flags() & QMetaType::IsEnumeration), isEnum); + QCOMPARE(bool(meta.flags() & QMetaType::IsQmlList), isQmlList); } -void tst_QMetaType::flagsStaticLess_data() +class NonDefaultConstructible +{ + NonDefaultConstructible(int) {} +}; + +struct MoveOnly +{ + MoveOnly() = default; + MoveOnly(const MoveOnly &) = delete; + MoveOnly(MoveOnly &&) = default; + MoveOnly &operator=(const MoveOnly &) = delete; + MoveOnly &operator=(MoveOnly &&) = default; +}; + +class Indestructible +{ +protected: + ~Indestructible() {} +}; + +template <typename T> static void addFlags2Row(QMetaType metaType = QMetaType::fromType<T>()) +{ + QTest::newRow(metaType.name() ? metaType.name() : "UnknownType") + << metaType + << std::is_default_constructible_v<T> + << std::is_copy_constructible_v<T> + << std::is_move_constructible_v<T> + << std::is_destructible_v<T> + << (QTypeTraits::has_operator_equal<T>::value || QTypeTraits::has_operator_less_than<T>::value) + << QTypeTraits::has_operator_less_than<T>::value; +}; + +void tst_QMetaType::flags2_data() { - flags_data(); + QTest::addColumn<QMetaType>("type"); + QTest::addColumn<bool>("isDefaultConstructible"); + QTest::addColumn<bool>("isCopyConstructible"); + QTest::addColumn<bool>("isMoveConstructible"); + QTest::addColumn<bool>("isDestructible"); + QTest::addColumn<bool>("isEqualityComparable"); + QTest::addColumn<bool>("isOrdered"); + + addFlags2Row<void>(QMetaType()); + addFlags2Row<void>(); + +#define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \ + addFlags2Row<RealType>(); + QT_FOR_EACH_STATIC_PRIMITIVE_NON_VOID_TYPE(ADD_METATYPE_TEST_ROW) + QT_FOR_EACH_STATIC_CORE_CLASS(ADD_METATYPE_TEST_ROW) + QT_FOR_EACH_STATIC_PRIMITIVE_POINTER(ADD_METATYPE_TEST_ROW) + QT_FOR_EACH_STATIC_CORE_POINTER(ADD_METATYPE_TEST_ROW) +#undef ADD_METATYPE_TEST_ROW + + addFlags2Row<NonDefaultConstructible>(); + addFlags2Row<MoveOnly>(); + addFlags2Row<QObject>(); + addFlags2Row<Indestructible>(); } -void tst_QMetaType::flagsStaticLess() +void tst_QMetaType::flags2() { - QFETCH(int, type); - QFETCH(bool, isMovable); - QFETCH(bool, isComplex); + QFETCH(QMetaType, type); + QFETCH(bool, isDefaultConstructible); + QFETCH(bool, isCopyConstructible); + QFETCH(bool, isMoveConstructible); + QFETCH(bool, isDestructible); + QFETCH(bool, isEqualityComparable); + QFETCH(bool, isOrdered); - int flags = QMetaType(type).flags(); - QCOMPARE(bool(flags & QMetaType::NeedsConstruction), isComplex); - QCOMPARE(bool(flags & QMetaType::NeedsDestruction), isComplex); - QCOMPARE(bool(flags & QMetaType::RelocatableType), isMovable); + QCOMPARE(type.isDefaultConstructible(), isDefaultConstructible); + QCOMPARE(type.isCopyConstructible(), isCopyConstructible); + QCOMPARE(type.isMoveConstructible(), isMoveConstructible); + QCOMPARE(type.isDestructible(), isDestructible); + QCOMPARE(type.isEqualityComparable(), isEqualityComparable); + QCOMPARE(type.isOrdered(), isOrdered); } void tst_QMetaType::flagsBinaryCompatibility6_0_data() @@ -1080,7 +1220,7 @@ void tst_QMetaType::flagsBinaryCompatibility6_0_data() QTest::addColumn<quint32>("flags"); QFile file(QFINDTESTDATA("typeFlags.bin")); - file.open(QIODevice::ReadOnly); + QVERIFY(file.open(QIODevice::ReadOnly)); QList<quint32> buffer; QDataStream ds(&file); ds >> buffer; @@ -1091,7 +1231,7 @@ void tst_QMetaType::flagsBinaryCompatibility6_0_data() if (id > QMetaType::LastCoreType) continue; // We do not link against QtGui, so we do longer consider such type as registered QVERIFY2(QMetaType::isRegistered(id), "A type could not be removed in BC way"); - QTest::newRow(QMetaType::typeName(id)) << id << flags; + QTest::newRow(QMetaType(id).name()) << id << flags; } } @@ -1100,16 +1240,14 @@ void tst_QMetaType::flagsBinaryCompatibility6_0() QFETCH(quint32, id); QFETCH(quint32, flags); - const auto currentFlags = QMetaType::typeFlags(id); + const auto currentFlags = QMetaType(id).flags(); auto expectedFlags = QMetaType::TypeFlags(flags); - if (!(currentFlags.testFlag(QMetaType::NeedsConstruction) && currentFlags.testFlag(QMetaType::NeedsDestruction))) { - if (expectedFlags.testFlag(QMetaType::NeedsConstruction) && expectedFlags.testFlag(QMetaType::NeedsDestruction)) { - // If type changed from RELOCATABLE to trivial, that's fine - expectedFlags.setFlag(QMetaType::NeedsConstruction, false); - expectedFlags.setFlag(QMetaType::NeedsDestruction, false); - } - } - quint32 mask_5_0 = 0x1fb; // Only compare the values that were already defined in 5.0 + + // Only compare the values that were already defined in 5.0. + // In 6.5, some types lost NeedsConstruction and NeedsDestruction, but + // that's acceptable if that's because they were trivial + quint32 mask_5_0 = 0x1ff & ~quint32(QMetaType::NeedsConstruction | QMetaType::NeedsDestruction + | QMetaType::RelocatableType); QCOMPARE(quint32(currentFlags) & mask_5_0, quint32(expectedFlags) & mask_5_0); } @@ -1123,42 +1261,61 @@ template<int ID> static void testConstructHelper() { typedef typename MetaEnumToType<ID>::Type Type; + auto expected = std::unique_ptr<Type>(DefaultValueFactory<ID>::create()); QMetaType info(ID); int size = info.sizeOf(); +#if QT_DEPRECATED_SINCE(6, 0) +QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED void *storage1 = qMallocAligned(size, alignof(Type)); - void *actual1 = QMetaType::construct(ID, storage1, /*copy=*/0); - void *storage2 = qMallocAligned(size, alignof(Type)); - void *actual2 = info.construct(storage2, /*copy=*/0); + void *actual1 = QMetaType::construct(ID, storage1, /*copy=*/nullptr); + auto cleanup1 = qScopeGuard([storage1, actual1]() { + QMetaType::destruct(ID, actual1); + qFreeAligned(storage1); + }); QCOMPARE(actual1, storage1); - QCOMPARE(actual2, storage2); - Type *expected = DefaultValueFactory<ID>::create(); QCOMPARE(*static_cast<Type *>(actual1), *expected); + QCOMPARE(QMetaType::construct(ID, nullptr, /*copy=*/nullptr), nullptr); + QMetaType::destruct(ID, nullptr); +QT_WARNING_POP +#endif // QT_DEPRECATED_SINCE(6, 0) + void *storage2 = qMallocAligned(size, alignof(Type)); + void *actual2 = info.construct(storage2, /*copy=*/nullptr); + auto cleanup2 = qScopeGuard([&info, storage2, actual2]() { + info.destruct(actual2); + qFreeAligned(storage2); + }); + QCOMPARE(actual2, storage2); QCOMPARE(*static_cast<Type *>(actual2), *expected); - delete expected; - 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); + QCOMPARE(info.construct(nullptr, /*copy=*/nullptr), nullptr); + info.destruct(nullptr); } template<> void testConstructHelper<QMetaType::Void>() { - /*int size = */ QMetaType::sizeOf(QMetaType::Void); - void *storage = 0; - void *actual = QMetaType::construct(QMetaType::Void, storage, /*copy=*/0); - QCOMPARE(actual, storage); - QMetaType::destruct(QMetaType::Void, actual); - qFreeAligned(storage); - - QVERIFY(QMetaType::construct(QMetaType::Void, 0, /*copy=*/0) == 0); - QMetaType::destruct(QMetaType::Void, 0); +#if QT_DEPRECATED_SINCE(6, 0) +QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED + void *storage1 = nullptr; + void *actual1 = QMetaType::construct(QMetaType::Void, storage1, /*copy=*/nullptr); + auto cleanup1 = qScopeGuard([storage1, actual1]() { + QMetaType::destruct(QMetaType::Void, actual1); + qFreeAligned(storage1); + }); + QCOMPARE(actual1, storage1); + QCOMPARE(QMetaType::construct(QMetaType::Void, nullptr, /*copy=*/nullptr), nullptr); + QMetaType::destruct(QMetaType::Void, nullptr); +QT_WARNING_POP +#endif // QT_DEPRECATED_SINCE(6, 0) + QMetaType info(QMetaType::Void); + void *storage2 = nullptr; + void *actual2 = info.construct(storage2, /*copy=*/nullptr); + auto cleanup2 = qScopeGuard([&info, storage2, actual2]() { + info.destruct(actual2); + qFreeAligned(storage2); + }); + QCOMPARE(actual2, storage2); + QVERIFY(info.construct(nullptr, /*copy=*/nullptr) == nullptr); + info.destruct(nullptr); } void tst_QMetaType::construct() @@ -1187,11 +1344,137 @@ FOR_EACH_CORE_METATYPE(RETURN_CONSTRUCT_FUNCTION) TypeTestFunctionGetter::get(type)(); } + +namespace TriviallyConstructibleTests { + +enum Enum0 {}; +enum class Enum1 {}; + +static_assert(QTypeInfo<int>::isValueInitializationBitwiseZero); +static_assert(QTypeInfo<double>::isValueInitializationBitwiseZero); +static_assert(QTypeInfo<Enum0>::isValueInitializationBitwiseZero); +static_assert(QTypeInfo<Enum1>::isValueInitializationBitwiseZero); +static_assert(QTypeInfo<int *>::isValueInitializationBitwiseZero); +static_assert(QTypeInfo<void *>::isValueInitializationBitwiseZero); +static_assert(QTypeInfo<std::nullptr_t>::isValueInitializationBitwiseZero); + +struct A {}; +struct B { B() {} }; +struct C { ~C() {} }; +struct D { D(int) {} }; +struct E { E() {} ~E() {} }; +struct F { int i; }; +struct G { G() : i(0) {} int i; }; +struct H { constexpr H() : i(0) {} int i; }; +struct I { I() : i(42) {} int i; }; +struct J { constexpr J() : i(42) {} int i; }; +struct K { K() : i(0) {} ~K() {} int i; }; + +static_assert(!QTypeInfo<A>::isValueInitializationBitwiseZero); +static_assert(!QTypeInfo<B>::isValueInitializationBitwiseZero); +static_assert(!QTypeInfo<C>::isValueInitializationBitwiseZero); +static_assert(!QTypeInfo<D>::isValueInitializationBitwiseZero); +static_assert(!QTypeInfo<E>::isValueInitializationBitwiseZero); +static_assert(!QTypeInfo<F>::isValueInitializationBitwiseZero); +static_assert(!QTypeInfo<G>::isValueInitializationBitwiseZero); +static_assert(!QTypeInfo<H>::isValueInitializationBitwiseZero); +static_assert(!QTypeInfo<I>::isValueInitializationBitwiseZero); +static_assert(!QTypeInfo<J>::isValueInitializationBitwiseZero); +static_assert(!QTypeInfo<K>::isValueInitializationBitwiseZero); + +} // namespace TriviallyConstructibleTests + +// Value-initializing these trivially constructible types cannot be achieved by +// memset(0) into their storage. For instance, on Itanium, a pointer to a data +// member needs to be value-initialized by setting it to -1. + +// Fits into QVariant +struct TrivialTypeNotZeroInitableSmall { + int TrivialTypeNotZeroInitableSmall::*pdm; +}; + +static_assert(std::is_trivially_default_constructible_v<TrivialTypeNotZeroInitableSmall>); +static_assert(!QTypeInfo<TrivialTypeNotZeroInitableSmall>::isValueInitializationBitwiseZero); +static_assert(sizeof(TrivialTypeNotZeroInitableSmall) < sizeof(QVariant)); // also checked more thoroughly below + +// Does not fit into QVariant internal storage +struct TrivialTypeNotZeroInitableBig { + int a; + double b; + char c; + int array[42]; + void (TrivialTypeNotZeroInitableBig::*pmf)(); + int TrivialTypeNotZeroInitableBig::*pdm; +}; + +static_assert(std::is_trivially_default_constructible_v<TrivialTypeNotZeroInitableBig>); +static_assert(!QTypeInfo<TrivialTypeNotZeroInitableSmall>::isValueInitializationBitwiseZero); +static_assert(sizeof(TrivialTypeNotZeroInitableBig) > sizeof(QVariant)); // also checked more thoroughly below + +void tst_QMetaType::defaultConstructTrivial_QTBUG_109594() +{ + // MSVC miscompiles value-initialization of pointers to data members, + // https://developercommunity.visualstudio.com/t/Pointer-to-data-member-is-not-initialize/10238905 + { + QMetaType mt = QMetaType::fromType<TrivialTypeNotZeroInitableSmall>(); + QVERIFY(mt.isDefaultConstructible()); + auto ptr = static_cast<TrivialTypeNotZeroInitableSmall *>(mt.create()); + const auto cleanup = qScopeGuard([=] { + mt.destroy(ptr); + }); +#ifdef Q_CC_MSVC_ONLY + QEXPECT_FAIL("", "MSVC compiler bug", Continue); +#endif + QCOMPARE(ptr->pdm, nullptr); + + QVariant v(mt); + QVERIFY(QVariant::Private::canUseInternalSpace(mt.iface())); + auto obj = v.value<TrivialTypeNotZeroInitableSmall>(); +#ifdef Q_CC_MSVC_ONLY + QEXPECT_FAIL("", "MSVC compiler bug", Continue); +#endif + QCOMPARE(obj.pdm, nullptr); + } + + { + QMetaType mt = QMetaType::fromType<TrivialTypeNotZeroInitableBig>(); + QVERIFY(mt.isDefaultConstructible()); + auto ptr = static_cast<TrivialTypeNotZeroInitableBig *>(mt.create()); + const auto cleanup = qScopeGuard([=] { + mt.destroy(ptr); + }); + QCOMPARE(ptr->a, 0); + QCOMPARE(ptr->b, 0.0); + QCOMPARE(ptr->c, '\0'); + QCOMPARE(ptr->pmf, nullptr); + for (int i : ptr->array) + QCOMPARE(i, 0); +#ifdef Q_CC_MSVC_ONLY + QEXPECT_FAIL("", "MSVC compiler bug", Continue); +#endif + QCOMPARE(ptr->pdm, nullptr); + + QVariant v(mt); + QVERIFY(!QVariant::Private::canUseInternalSpace(mt.iface())); + auto obj = v.value<TrivialTypeNotZeroInitableBig>(); + QCOMPARE(obj.a, 0); + QCOMPARE(obj.b, 0.0); + QCOMPARE(obj.c, '\0'); + QCOMPARE(obj.pmf, nullptr); + for (int i : obj.array) + QCOMPARE(i, 0); +#ifdef Q_CC_MSVC_ONLY + QEXPECT_FAIL("", "MSVC compiler bug", Continue); +#endif + QCOMPARE(obj.pdm, nullptr); + } +} + void tst_QMetaType::typedConstruct() { auto testMetaObjectWriteOnGadget = [](QVariant &gadget, const QList<GadgetPropertyType> &properties) { - auto metaObject = QMetaType::metaObjectForType(gadget.userType()); + auto metaObject = QMetaType(gadget.userType()).metaObject(); QVERIFY(metaObject != nullptr); QCOMPARE(metaObject->methodCount(), 0); QCOMPARE(metaObject->propertyCount(), properties.size()); @@ -1205,7 +1488,7 @@ void tst_QMetaType::typedConstruct() auto testMetaObjectReadOnGadget = [](QVariant gadget, const QList<GadgetPropertyType> &properties) { - auto metaObject = QMetaType::metaObjectForType(gadget.userType()); + auto metaObject = QMetaType(gadget.userType()).metaObject(); QVERIFY(metaObject != nullptr); QCOMPARE(metaObject->methodCount(), 0); QCOMPARE(metaObject->propertyCount(), properties.size()); @@ -1213,7 +1496,7 @@ void tst_QMetaType::typedConstruct() auto prop = metaObject->property(i); QCOMPARE(properties[i].name, prop.name()); QCOMPARE(properties[i].type, prop.typeName()); - if (!QMetaType::typeFlags(prop.userType()).testFlag(QMetaType::IsGadget)) + if (!QMetaType(prop.userType()).flags().testFlag(QMetaType::IsGadget)) QCOMPARE(properties[i].testData, prop.readOnGadget(gadget.constData())); } }; @@ -1225,7 +1508,7 @@ void tst_QMetaType::typedConstruct() }; registerGadget("DynamicGadget1", dynamicGadget1); - QVariant testGadget1(QMetaType(QMetaType::type("DynamicGadget1"))); + QVariant testGadget1(QMetaType::fromName("DynamicGadget1")); testMetaObjectWriteOnGadget(testGadget1, dynamicGadget1); testMetaObjectReadOnGadget(testGadget1, dynamicGadget1); @@ -1237,10 +1520,10 @@ void tst_QMetaType::typedConstruct() {"DynamicGadget1", "dynamicGadget1_prop", testGadget1} }; registerGadget("DynamicGadget2", dynamicGadget2); - QVariant testGadget2(QMetaType(QMetaType::type("DynamicGadget2"))); + QVariant testGadget2(QMetaType::fromName("DynamicGadget2")); testMetaObjectWriteOnGadget(testGadget2, dynamicGadget2); testMetaObjectReadOnGadget(testGadget2, dynamicGadget2); - auto g2mo = QMetaType::metaObjectForType(testGadget2.userType()); + auto g2mo = QMetaType(testGadget2.userType()).metaObject(); auto dynamicGadget1_prop = g2mo->property(g2mo->indexOfProperty("dynamicGadget1_prop")); testMetaObjectReadOnGadget(dynamicGadget1_prop.readOnGadget(testGadget2.constData()), dynamicGadget1); @@ -1272,7 +1555,7 @@ void tst_QMetaType::typedConstruct() s_managedTypes[podTypeId] = qMakePair(dynamicGadgetProperties, std::shared_ptr<QMetaObject>{}); // Test POD - QCOMPARE(podTypeId, QMetaType::type(podTypeName)); + QCOMPARE(podTypeId, QMetaType::fromName(podTypeName).id()); QVariant podVariant{QMetaType(podTypeId)}; QCOMPARE(myPodTesData, static_cast<const GenericPODType *>(reinterpret_cast<const BaseGenericType *>(podVariant.constData()))->podData); @@ -1286,27 +1569,32 @@ template<int ID> static void testConstructCopyHelper() { typedef typename MetaEnumToType<ID>::Type Type; - Type *expected = TestValueFactory<ID>::create(); + auto expected = std::unique_ptr<Type>(TestValueFactory<ID>::create()); QMetaType info(ID); - int size = QMetaType::sizeOf(ID); +#if QT_DEPRECATED_SINCE(6, 0) +QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED + const int size = QMetaType::sizeOf(ID); QCOMPARE(info.sizeOf(), size); void *storage1 = qMallocAligned(size, alignof(Type)); - void *actual1 = QMetaType::construct(ID, storage1, expected); - void *storage2 = qMallocAligned(size, alignof(Type)); - void *actual2 = info.construct(storage2, expected); + void *actual1 = QMetaType::construct(ID, storage1, expected.get()); + auto cleanup1 = qScopeGuard([storage1, actual1]() { + QMetaType::destruct(ID, actual1); + qFreeAligned(storage1); + }); QCOMPARE(actual1, storage1); - QCOMPARE(actual2, storage2); QCOMPARE(*static_cast<Type *>(actual1), *expected); + QCOMPARE(QMetaType::construct(ID, nullptr, nullptr), nullptr); +QT_WARNING_POP +#endif // QT_DEPRECATED_SINCE(6, 0) + void *storage2 = qMallocAligned(info.sizeOf(), alignof(Type)); + void *actual2 = info.construct(storage2, expected.get()); + auto cleanup2 = qScopeGuard([&info, storage2, actual2]() { + info.destruct(actual2); + qFreeAligned(storage2); + }); + QCOMPARE(actual2, storage2); 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; + QCOMPARE(info.construct(nullptr, expected.get()), nullptr); } template<> @@ -1314,14 +1602,28 @@ void testConstructCopyHelper<QMetaType::Void>() { typedef MetaEnumToType<QMetaType::Void>::Type Type; Type *expected = TestValueFactory<QMetaType::Void>::create(); - /* int size = */QMetaType::sizeOf(QMetaType::Void); - void *storage = 0; - void *actual = QMetaType::construct(QMetaType::Void, storage, expected); - QCOMPARE(actual, storage); - QMetaType::destruct(QMetaType::Void, actual); - qFreeAligned(storage); - - QVERIFY(QMetaType::construct(QMetaType::Void, 0, expected) == 0); + QCOMPARE(expected, nullptr); // we do not need to delete it +#if QT_DEPRECATED_SINCE(6, 0) +QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED + void *storage1 = nullptr; + void *actual1 = QMetaType::construct(QMetaType::Void, storage1, expected); + auto cleanup1 = qScopeGuard([storage1, actual1]() { + QMetaType::destruct(QMetaType::Void, actual1); + qFreeAligned(storage1); + }); + QCOMPARE(actual1, storage1); + QCOMPARE(QMetaType::construct(QMetaType::Void, nullptr, nullptr), nullptr); +QT_WARNING_POP +#endif // QT_DEPRECATED_SINCE(6, 0) + QMetaType info(QMetaType::Void); + void *storage2 = nullptr; + void *actual2 = info.construct(storage2, expected); + auto cleanup2 = qScopeGuard([&info, storage2, actual2]() { + info.destruct(actual2); + qFreeAligned(storage2); + }); + QCOMPARE(actual2, storage2); + QCOMPARE(info.construct(nullptr, expected), nullptr); } void tst_QMetaType::constructCopy_data() @@ -1338,7 +1640,7 @@ void tst_QMetaType::constructCopy() switch (type) { case QMetaType::UnknownType: return []() { - char buf[1], buf2[1]; + char buf[1], buf2[1] = {}; QCOMPARE(QMetaType().construct(&buf, &buf2), nullptr); }; #define RETURN_CONSTRUCT_COPY_FUNCTION(MetaTypeName, MetaTypeId, RealType) \ @@ -1370,7 +1672,7 @@ void tst_QMetaType::selfCompare_data() QTest::newRow("unknown-type") << int(QMetaType::UnknownType) << orderingFor(QMetaType::UnknownType); #define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \ - QTest::newRow(QMetaType::typeName(QMetaType::MetaTypeName)) << int(QMetaType::MetaTypeName) << orderingFor(QMetaType::MetaTypeName); + QTest::newRow(QMetaType(QMetaType::MetaTypeName).name()) << int(QMetaType::MetaTypeName) << orderingFor(QMetaType::MetaTypeName); FOR_EACH_CORE_METATYPE(ADD_METATYPE_TEST_ROW) #undef ADD_METATYPE_TEST_ROW } @@ -1412,42 +1714,49 @@ Q_DECLARE_METATYPE(CustomString) //this line is useless void tst_QMetaType::typedefs() { - QCOMPARE(QMetaType::type("long long"), int(QMetaType::LongLong)); - QCOMPARE(QMetaType::type("unsigned long long"), int(QMetaType::ULongLong)); - QCOMPARE(QMetaType::type("qint8"), int(QMetaType::SChar)); - QCOMPARE(QMetaType::type("quint8"), int(QMetaType::UChar)); - QCOMPARE(QMetaType::type("qint16"), int(QMetaType::Short)); - QCOMPARE(QMetaType::type("quint16"), int(QMetaType::UShort)); - QCOMPARE(QMetaType::type("qint32"), int(QMetaType::Int)); - QCOMPARE(QMetaType::type("quint32"), int(QMetaType::UInt)); - QCOMPARE(QMetaType::type("qint64"), int(QMetaType::LongLong)); - QCOMPARE(QMetaType::type("quint64"), int(QMetaType::ULongLong)); + QCOMPARE(QMetaType::fromName("long long").id(), int(QMetaType::LongLong)); + QCOMPARE(QMetaType::fromName("unsigned long long").id(), int(QMetaType::ULongLong)); + QCOMPARE(QMetaType::fromName("qint8").id(), int(QMetaType::SChar)); + QCOMPARE(QMetaType::fromName("quint8").id(), int(QMetaType::UChar)); + QCOMPARE(QMetaType::fromName("qint16").id(), int(QMetaType::Short)); + QCOMPARE(QMetaType::fromName("quint16").id(), int(QMetaType::UShort)); + QCOMPARE(QMetaType::fromName("qint32").id(), int(QMetaType::Int)); + QCOMPARE(QMetaType::fromName("quint32").id(), int(QMetaType::UInt)); + QCOMPARE(QMetaType::fromName("qint64").id(), int(QMetaType::LongLong)); + QCOMPARE(QMetaType::fromName("quint64").id(), int(QMetaType::ULongLong)); // make sure the qreal typeId is the type id of the type it's defined to - QCOMPARE(QMetaType::type("qreal"), ::qMetaTypeId<qreal>()); + QCOMPARE(QMetaType::fromName("qreal").id(), ::qMetaTypeId<qreal>()); qRegisterMetaType<CustomString>("CustomString"); - QCOMPARE(QMetaType::type("CustomString"), ::qMetaTypeId<CustomString>()); + QCOMPARE(QMetaType::fromName("CustomString").id(), ::qMetaTypeId<CustomString>()); typedef Whity<double> WhityDouble; qRegisterMetaType<WhityDouble>("WhityDouble"); - QCOMPARE(QMetaType::type("WhityDouble"), ::qMetaTypeId<WhityDouble>()); + QCOMPARE(QMetaType::fromName("WhityDouble").id(), ::qMetaTypeId<WhityDouble>()); } +struct RegisterTypeType {}; + void tst_QMetaType::registerType() { // Built-in QCOMPARE(qRegisterMetaType<QString>("QString"), int(QMetaType::QString)); QCOMPARE(qRegisterMetaType<QString>("QString"), int(QMetaType::QString)); + qRegisterMetaType(QMetaType::fromType<QString>()); // Custom int fooId = qRegisterMetaType<TestSpace::Foo>("TestSpace::Foo"); QVERIFY(fooId >= int(QMetaType::User)); QCOMPARE(qRegisterMetaType<TestSpace::Foo>("TestSpace::Foo"), fooId); + qRegisterMetaType(QMetaType::fromType<TestSpace::Foo>()); int movableId = qRegisterMetaType<CustomMovable>("CustomMovable"); QVERIFY(movableId >= int(QMetaType::User)); QCOMPARE(qRegisterMetaType<CustomMovable>("CustomMovable"), movableId); + qRegisterMetaType(QMetaType::fromType<CustomMovable>()); + + // Aliases are deprecated // Alias to built-in typedef QString MyString; @@ -1455,7 +1764,7 @@ void tst_QMetaType::registerType() QCOMPARE(qRegisterMetaType<MyString>("MyString"), int(QMetaType::QString)); QCOMPARE(qRegisterMetaType<MyString>("MyString"), int(QMetaType::QString)); - QCOMPARE(QMetaType::type("MyString"), int(QMetaType::QString)); + QCOMPARE(QMetaType::fromType<MyString>().id(), int(QMetaType::QString)); // Alias to custom type typedef CustomMovable MyMovable; @@ -1464,12 +1773,29 @@ void tst_QMetaType::registerType() QCOMPARE(qRegisterMetaType<MyMovable>("MyMovable"), movableId); QCOMPARE(qRegisterMetaType<MyMovable>("MyMovable"), movableId); - QCOMPARE(QMetaType::type("MyMovable"), movableId); + QCOMPARE(QMetaType::fromType<MyMovable>().id(), movableId); QCOMPARE(qRegisterMetaType<MyFoo>("MyFoo"), fooId); QCOMPARE(qRegisterMetaType<MyFoo>("MyFoo"), fooId); - QCOMPARE(QMetaType::type("MyFoo"), fooId); + QCOMPARE(QMetaType::fromType<MyFoo>().id(), fooId); + + // this portion of the test can only be run once + static bool typeWasRegistered = false; + if (!typeWasRegistered) { + QMetaType mt = QMetaType::fromType<RegisterTypeType>(); + QVERIFY(mt.isValid()); + QCOMPARE_NE(mt.name(), nullptr); + + QVERIFY(!mt.isRegistered()); + QVERIFY(!QMetaType::fromName(mt.name()).isValid()); + + QCOMPARE_GT(qRegisterMetaType(mt), 0); + typeWasRegistered = true; + + QVERIFY(mt.isRegistered()); + QVERIFY(QMetaType::fromName(mt.name()).isValid()); + } } class IsRegisteredDummyType { }; @@ -1490,7 +1816,7 @@ void tst_QMetaType::isRegistered_data() // unknown types QTest::newRow("-1") << -1 << false; QTest::newRow("-42") << -42 << false; - QTest::newRow("IsRegisteredDummyType + 1") << (dummyTypeId + 1) << false; + QTest::newRow("IsRegisteredDummyType + 1000") << (dummyTypeId + 1000) << false; QTest::newRow("QMetaType::UnknownType") << int(QMetaType::UnknownType) << false; } @@ -1513,25 +1839,77 @@ Q_DECLARE_METATYPE(isEnumTest_Enum1) void tst_QMetaType::isEnum() { int type0 = qRegisterMetaType<int>("int"); - QVERIFY((QMetaType::typeFlags(type0) & QMetaType::IsEnumeration) == 0); + QVERIFY((QMetaType(type0).flags() & QMetaType::IsEnumeration) == 0); int type1 = qRegisterMetaType<isEnumTest_Enum0>("isEnumTest_Enum0"); - QVERIFY((QMetaType::typeFlags(type1) & QMetaType::IsEnumeration) == QMetaType::IsEnumeration); + QVERIFY((QMetaType(type1).flags() & QMetaType::IsEnumeration) == QMetaType::IsEnumeration); int type2 = qRegisterMetaType<isEnumTest_Struct0>("isEnumTest_Struct0"); - QVERIFY((QMetaType::typeFlags(type2) & QMetaType::IsEnumeration) == 0); + QVERIFY((QMetaType(type2).flags() & QMetaType::IsEnumeration) == 0); int type3 = qRegisterMetaType<isEnumTest_Enum0 *>("isEnumTest_Enum0 *"); - QVERIFY((QMetaType::typeFlags(type3) & QMetaType::IsEnumeration) == 0); + QVERIFY((QMetaType(type3).flags() & QMetaType::IsEnumeration) == 0); int type4 = qRegisterMetaType<isEnumTest_Struct0::A>("isEnumTest_Struct0::A"); - QVERIFY((QMetaType::typeFlags(type4) & QMetaType::IsEnumeration) == QMetaType::IsEnumeration); + QVERIFY((QMetaType(type4).flags() & QMetaType::IsEnumeration) == QMetaType::IsEnumeration); int type5 = ::qMetaTypeId<isEnumTest_Struct1>(); - QVERIFY((QMetaType::typeFlags(type5) & QMetaType::IsEnumeration) == 0); + QVERIFY((QMetaType(type5).flags() & QMetaType::IsEnumeration) == 0); int type6 = ::qMetaTypeId<isEnumTest_Enum1>(); - QVERIFY((QMetaType::typeFlags(type6) & QMetaType::IsEnumeration) == QMetaType::IsEnumeration); + QVERIFY((QMetaType(type6).flags() & QMetaType::IsEnumeration) == QMetaType::IsEnumeration); +} + +enum E1 : unsigned char {}; +enum E2 : qlonglong {}; +enum class E3 : unsigned short {}; + +namespace myflags { + + Q_NAMESPACE + + enum Flag1 : int { A, B }; + enum Flag2 : short { X, Y }; + + Q_DECLARE_FLAGS(Flags1, myflags::Flag1); + Q_FLAG_NS(Flags1) + Q_DECLARE_FLAGS(Flags2, myflags::Flag2); + Q_FLAG_NS(Flags2) + +} + +template <typename T> +using getUnderlyingTypeNormalized = std::conditional_t< + std::is_signed_v<std::underlying_type_t<T>>, + typename QIntegerForSize<sizeof(T)>::Signed, + typename QIntegerForSize<sizeof(T)>::Unsigned +>; + +void tst_QMetaType::underlyingType_data() +{ + QTest::addColumn<QMetaType>("source"); + QTest::addColumn<QMetaType>("underlying"); + + QTest::newRow("invalid") << QMetaType() << QMetaType(); + QTest::newRow("plain") << QMetaType::fromType<isEnumTest_Enum1>() + << QMetaType::fromType<getUnderlyingTypeNormalized<isEnumTest_Enum1>>(); + QTest::newRow("uchar") << QMetaType::fromType<E1>() + << QMetaType::fromType<getUnderlyingTypeNormalized<E1>>(); + QTest::newRow("long") << QMetaType::fromType<E2>() + << QMetaType::fromType<getUnderlyingTypeNormalized<E2>>(); + QTest::newRow("class_ushort") << QMetaType::fromType<E3>() + << QMetaType::fromType<getUnderlyingTypeNormalized<E3>>(); + QTest::newRow("flags_int") << QMetaType::fromType<myflags::Flags1>() + << QMetaType::fromType<int>(); + QTest::newRow("flags_short") << QMetaType::fromType<myflags::Flags2>() + << QMetaType::fromType<int>(); // sic, not short! +} + +void tst_QMetaType::underlyingType() +{ + QFETCH(QMetaType, source); + QFETCH(QMetaType, underlying); + QCOMPARE(source.underlyingType(), underlying); } void tst_QMetaType::isRegisteredStaticLess_data() @@ -1543,9 +1921,16 @@ void tst_QMetaType::isRegisteredStaticLess() { QFETCH(int, typeId); QFETCH(bool, registered); + ignoreInvalidMetaTypeWarning(typeId); QCOMPARE(QMetaType(typeId).isRegistered(), registered); } +struct NotARegisteredType {}; +void tst_QMetaType::isNotRegistered() +{ + QVERIFY(!QMetaType::fromType<NotARegisteredType>().isRegistered()); +} + typedef QHash<int, uint> IntUIntHash; Q_DECLARE_METATYPE(IntUIntHash) typedef QMap<int, uint> IntUIntMap; @@ -1811,7 +2196,7 @@ void tst_QMetaType::automaticTemplateRegistration_1() sp.data()->setObjectName("Test name"); \ QVariant v = QVariant::fromValue(sp); \ QCOMPARE(v.typeName(), #SMARTPOINTER "<" #ELEMENT_TYPE ">"); \ - QVERIFY(QMetaType::typeFlags(::qMetaTypeId<SMARTPOINTER < ELEMENT_TYPE > >()) & QMetaType::FLAG_TEST); \ + QVERIFY(QMetaType(::qMetaTypeId<SMARTPOINTER < ELEMENT_TYPE > >()).flags() & QMetaType::FLAG_TEST); \ SMARTPOINTER < QObject > extractedPtr = FROMVARIANTFUNCTION<QObject>(v); \ QCOMPARE(extractedPtr.data()->objectName(), sp.data()->objectName()); \ } @@ -1829,7 +2214,7 @@ void tst_QMetaType::automaticTemplateRegistration_1() sp.data()->setObjectName("Test name"); \ QVariant v = QVariant::fromValue(sp); \ QCOMPARE(v.typeName(), #SMARTPOINTER "<" #ELEMENT_TYPE ">"); \ - QVERIFY(QMetaType::typeFlags(::qMetaTypeId<SMARTPOINTER < ELEMENT_TYPE > >()) & QMetaType::FLAG_TEST); \ + QVERIFY(QMetaType(::qMetaTypeId<SMARTPOINTER < ELEMENT_TYPE > >()).flags() & QMetaType::FLAG_TEST); \ SMARTPOINTER < QObject > extractedPtr = FROMVARIANTFUNCTION<QObject>(v); \ QCOMPARE(extractedPtr.data()->objectName(), sp.data()->objectName()); \ } @@ -1849,7 +2234,7 @@ void tst_QMetaType::automaticTemplateRegistration_1() sp.toStrongRef()->setObjectName("Test name"); \ QVariant v = QVariant::fromValue(sp); \ QCOMPARE(v.typeName(), "QWeakPointer<" #ELEMENT_TYPE ">"); \ - QVERIFY(QMetaType::typeFlags(::qMetaTypeId<QWeakPointer < ELEMENT_TYPE > >()) & QMetaType::FLAG_TEST); \ + QVERIFY(QMetaType(::qMetaTypeId<QWeakPointer < ELEMENT_TYPE > >()).flags() & QMetaType::FLAG_TEST); \ } TEST_WEAK_SMARTPOINTER(QObject, WeakPointerToQObject) @@ -1902,38 +2287,39 @@ void tst_QMetaType::saveAndLoadBuiltin() QFETCH(int, type); QFETCH(bool, isStreamable); - void *value = QMetaType::create(type); + QMetaType metaType(type); + void *value = metaType.create(); QByteArray ba; QDataStream stream(&ba, QIODevice::ReadWrite); - QCOMPARE(QMetaType::save(stream, type, value), isStreamable); + QCOMPARE(metaType.save(stream, value), isStreamable); QCOMPARE(stream.status(), QDataStream::Ok); if (isStreamable) { - QVERIFY(QMetaType(type).hasRegisteredDataStreamOperators()); - QVERIFY(QMetaType::load(stream, type, value)); // Hmmm, shouldn't it return false? + QVERIFY(metaType.hasRegisteredDataStreamOperators()); + QVERIFY(metaType.load(stream, value)); // Hmmm, shouldn't it return false? // std::nullptr_t is nullary: it doesn't actually read anything if (type != QMetaType::Nullptr) QCOMPARE(stream.status(), QDataStream::ReadPastEnd); } else { - QVERIFY(!QMetaType(type).hasRegisteredDataStreamOperators()); + QVERIFY(!metaType.hasRegisteredDataStreamOperators()); } stream.device()->seek(0); stream.resetStatus(); - QCOMPARE(QMetaType::load(stream, type, value), isStreamable); + QCOMPARE(metaType.load(stream, value), isStreamable); QCOMPARE(stream.status(), QDataStream::Ok); if (isStreamable) { - QVERIFY(QMetaType::load(stream, type, value)); // Hmmm, shouldn't it return false? + QVERIFY(metaType.load(stream, value)); // Hmmm, shouldn't it return false? // std::nullptr_t is nullary: it doesn't actually read anything if (type != QMetaType::Nullptr) QCOMPARE(stream.status(), QDataStream::ReadPastEnd); } - QMetaType::destroy(type, value); + metaType.destroy(value); } struct CustomStreamableType @@ -1962,25 +2348,27 @@ void tst_QMetaType::saveAndLoadCustom() t.a = 123; int id = ::qMetaTypeId<CustomStreamableType>(); + QMetaType metaType(id); + QByteArray ba; QDataStream stream(&ba, QIODevice::ReadWrite); - QVERIFY(QMetaType::save(stream, id, &t)); + QVERIFY(metaType.save(stream, &t)); QCOMPARE(stream.status(), QDataStream::Ok); CustomStreamableType t2; t2.a = -1; - QVERIFY(QMetaType::load(stream, id, &t2)); // Hmmm, shouldn't it return false? + QVERIFY(metaType.load(stream, &t2)); // Hmmm, shouldn't it return false? QCOMPARE(stream.status(), QDataStream::ReadPastEnd); QCOMPARE(t2.a, -1); stream.device()->seek(0); stream.resetStatus(); - QVERIFY(QMetaType::load(stream, id, &t2)); + QVERIFY(metaType.load(stream, &t2)); QCOMPARE(stream.status(), QDataStream::Ok); QCOMPARE(t2.a, t.a); - QVERIFY(QMetaType::load(stream, id, &t2)); // Hmmm, shouldn't it return false? + QVERIFY(metaType.load(stream, &t2)); // Hmmm, shouldn't it return false? QCOMPARE(stream.status(), QDataStream::ReadPastEnd); } @@ -2018,6 +2406,7 @@ void tst_QMetaType::metaObject_data() QTest::newRow("unknown-type") << int(QMetaType::UnknownType) << static_cast<const QMetaObject *>(0) << false << false << false; QTest::newRow("QObject") << int(QMetaType::QObjectStar) << &QObject::staticMetaObject << false << false << true; QTest::newRow("QFile*") << ::qMetaTypeId<QFile*>() << &QFile::staticMetaObject << false << false << true; + QTest::newRow("MyObject") << ::qMetaTypeId<MyObject>() << &MyObject::staticMetaObject << false << false << false; QTest::newRow("MyObject*") << ::qMetaTypeId<MyObject*>() << &MyObject::staticMetaObject << false << false << true; QTest::newRow("int") << int(QMetaType::Int) << static_cast<const QMetaObject *>(0) << false << false << false; QTest::newRow("QEasingCurve") << ::qMetaTypeId<QEasingCurve>() << &QEasingCurve::staticMetaObject << true << false << false; @@ -2040,7 +2429,6 @@ void tst_QMetaType::metaObject() QFETCH(bool, isGadgetPtr); QFETCH(bool, isQObjectPtr); - QCOMPARE(QMetaType::metaObjectForType(type), result); QMetaType mt(type); QCOMPARE(mt.metaObject(), result); QCOMPARE(!!(mt.flags() & QMetaType::IsGadget), isGadget); diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h index 15fe3e6874..1694e49491 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.h @@ -1,32 +1,6 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QtCore> +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + #include <QTest> #include "tst_qmetatype_common.h" #include "tst_qvariant_common.h" @@ -71,7 +45,9 @@ private: private slots: void defined(); +#if QT_CONFIG(thread) void threadSafety(); +#endif void namespaces(); void id(); void qMetaTypeId(); @@ -95,12 +71,13 @@ private slots: void alignOf(); void flags_data(); void flags(); - void flagsStaticLess_data(); - void flagsStaticLess(); + void flags2_data(); + void flags2(); void flagsBinaryCompatibility6_0_data(); void flagsBinaryCompatibility6_0(); void construct_data(); void construct(); + void defaultConstructTrivial_QTBUG_109594(); void typedConstruct(); void constructCopy_data(); void constructCopy(); @@ -112,7 +89,10 @@ private slots: void isRegistered(); void isRegisteredStaticLess_data(); void isRegisteredStaticLess(); + void isNotRegistered(); void isEnum(); + void underlyingType_data(); + void underlyingType(); void automaticTemplateRegistration_1(); void automaticTemplateRegistration_2(); // defined in tst_qmetatype3.cpp void saveAndLoadBuiltin_data(); @@ -133,9 +113,22 @@ private slots: void fromType(); void operatorEq_data(); void operatorEq(); + void operatorEq2_data(); + void operatorEq2(); + void operatorEqAcrossLibs_data(); + void operatorEqAcrossLibs(); void typesWithInaccessibleDTors(); void voidIsNotUnknown(); void typeNameNormalization(); + void typeNameInQtPrivate(); + + // Tests for deprecated APIs +#if QT_DEPRECATED_SINCE(6, 0) + void testDeprecatedGetters_data() { type_data(); } + void testDeprecatedGetters(); + void testDeprecatedLoadSave_data() { saveAndLoadBuiltin_data(); } + void testDeprecatedLoadSave(); +#endif }; template <typename T> @@ -283,7 +276,7 @@ Q_DECLARE_METATYPE(CustomMovable); const QVariant v = QVariant::fromValue(t); \ QByteArray tn = createTypeName(#CONTAINER "<", #__VA_ARGS__); \ const int expectedType = ::qMetaTypeId<CONTAINER< __VA_ARGS__ > >(); \ - const int type = QMetaType::type(tn); \ + const int type = QMetaType::fromName(tn).id(); \ QCOMPARE(type, expectedType); \ QCOMPARE((QMetaType::fromType<CONTAINER< __VA_ARGS__ >>().id()), expectedType); \ } diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp index 025353d9c0..68bcb53056 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype2.cpp @@ -1,33 +1,8 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include "tst_qmetatype.h" -#include "tst_qvariant_common.h" +#include "tst_qmetatype_libs.h" #include <QtCore/private/qmetaobjectbuilder_p.h> @@ -55,6 +30,30 @@ struct ConvertFunctor } }; +template<typename T> +struct OptionalWrapper +{ + std::optional<T> operator()(const T& t) const + { + if (!CustomConvertibleType::s_ok) + return std::nullopt; + + return t; + } +}; + +template<typename From> +struct ConvertFunctorWithOptional +{ + std::optional<CustomConvertibleType> operator()(const From& f) const + { + if (!CustomConvertibleType::s_ok) + return std::nullopt; + + return CustomConvertibleType(QVariant::fromValue(f)); + } +}; + template<typename From, typename To> bool hasRegisteredConverterFunction() { @@ -106,6 +105,7 @@ void customTypeNotYetConvertible() testCustomTypeNotYetConvertible<QLineF, CustomConvertibleType>(); testCustomTypeNotYetConvertible<QChar, CustomConvertibleType>(); testCustomTypeNotYetConvertible<CustomConvertibleType, CustomConvertibleType2>(); + testCustomTypeNotYetConvertible<CustomConvertibleType, std::optional<CustomConvertibleType>>(); } void registerCustomTypeConversions() @@ -124,20 +124,22 @@ void registerCustomTypeConversions() QVERIFY((QMetaType::registerConverter<CustomConvertibleType, QLine>(&CustomConvertibleType::convert<QLine>))); QVERIFY((QMetaType::registerConverter<CustomConvertibleType, QLineF>(&CustomConvertibleType::convertOk<QLineF>))); QVERIFY((QMetaType::registerConverter<CustomConvertibleType, QChar>(&CustomConvertibleType::convert<QChar>))); - QVERIFY((QMetaType::registerConverter<QString, CustomConvertibleType>(ConvertFunctor<QString>()))); + QVERIFY((QMetaType::registerConverter<QString, CustomConvertibleType>(ConvertFunctorWithOptional<QString>()))); QVERIFY((QMetaType::registerConverter<bool, CustomConvertibleType>(ConvertFunctor<bool>()))); - QVERIFY((QMetaType::registerConverter<int, CustomConvertibleType>(ConvertFunctor<int>()))); + QVERIFY((QMetaType::registerConverter<int, CustomConvertibleType>(ConvertFunctorWithOptional<int>()))); QVERIFY((QMetaType::registerConverter<double, CustomConvertibleType>(ConvertFunctor<double>()))); - QVERIFY((QMetaType::registerConverter<float, CustomConvertibleType>(ConvertFunctor<float>()))); + QVERIFY((QMetaType::registerConverter<float, CustomConvertibleType>(ConvertFunctorWithOptional<float>()))); QVERIFY((QMetaType::registerConverter<QRect, CustomConvertibleType>(ConvertFunctor<QRect>()))); - QVERIFY((QMetaType::registerConverter<QRectF, CustomConvertibleType>(ConvertFunctor<QRectF>()))); + QVERIFY((QMetaType::registerConverter<QRectF, CustomConvertibleType>(ConvertFunctorWithOptional<QRectF>()))); QVERIFY((QMetaType::registerConverter<QPoint, CustomConvertibleType>(ConvertFunctor<QPoint>()))); - QVERIFY((QMetaType::registerConverter<QPointF, CustomConvertibleType>(ConvertFunctor<QPointF>()))); + QVERIFY((QMetaType::registerConverter<QPointF, CustomConvertibleType>(ConvertFunctorWithOptional<QPointF>()))); QVERIFY((QMetaType::registerConverter<QSize, CustomConvertibleType>(ConvertFunctor<QSize>()))); - QVERIFY((QMetaType::registerConverter<QSizeF, CustomConvertibleType>(ConvertFunctor<QSizeF>()))); + QVERIFY((QMetaType::registerConverter<QSizeF, CustomConvertibleType>(ConvertFunctorWithOptional<QSizeF>()))); QVERIFY((QMetaType::registerConverter<QLine, CustomConvertibleType>(ConvertFunctor<QLine>()))); - QVERIFY((QMetaType::registerConverter<QLineF, CustomConvertibleType>(ConvertFunctor<QLineF>()))); + QVERIFY((QMetaType::registerConverter<QLineF, CustomConvertibleType>(ConvertFunctorWithOptional<QLineF>()))); QVERIFY((QMetaType::registerConverter<QChar, CustomConvertibleType>(ConvertFunctor<QChar>()))); + QVERIFY((QMetaType::registerConverter<CustomConvertibleType, std::optional<CustomConvertibleType>>(OptionalWrapper<CustomConvertibleType>()))); + QVERIFY((QMetaType::registerConverter<CustomConvertibleType, CustomConvertibleType2>())); QTest::ignoreMessage(QtWarningMsg, "Type conversion already registered from type CustomConvertibleType to type CustomConvertibleType2"); QVERIFY((!QMetaType::registerConverter<CustomConvertibleType, CustomConvertibleType2>())); @@ -196,7 +198,7 @@ void tst_QMetaType::convertCustomType() QCOMPARE(v.toString(), ok ? testQString : QString()); QCOMPARE(v.value<QString>(), ok ? testQString : QString()); QVERIFY(CustomConvertibleType::s_value.canConvert<CustomConvertibleType>()); - QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toString()), testQString); + QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toString()), ok ? testQString : QString()); QFETCH(bool, testBool); CustomConvertibleType::s_value = testBool; @@ -208,7 +210,7 @@ void tst_QMetaType::convertCustomType() CustomConvertibleType::s_value = testInt; QCOMPARE(v.toInt(), ok ? testInt : 0); QCOMPARE(v.value<int>(), ok ? testInt : 0); - QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toInt()), testInt); + QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toInt()), ok ? testInt : 0); QFETCH(double, testDouble); CustomConvertibleType::s_value = testDouble; @@ -220,7 +222,7 @@ void tst_QMetaType::convertCustomType() CustomConvertibleType::s_value = testFloat; QCOMPARE(v.toFloat(), ok ? testFloat : 0.0); QCOMPARE(v.value<float>(), ok ? testFloat : 0.0); - QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toFloat()), testFloat); + QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toFloat()), ok ? testFloat : 0); QFETCH(QRect, testQRect); CustomConvertibleType::s_value = testQRect; @@ -232,7 +234,7 @@ void tst_QMetaType::convertCustomType() CustomConvertibleType::s_value = testQRectF; QCOMPARE(v.toRectF(), ok ? testQRectF : QRectF()); QCOMPARE(v.value<QRectF>(), ok ? testQRectF : QRectF()); - QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toRectF()), testQRectF); + QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toRectF()), ok ? testQRectF : QRectF()); QFETCH(QPoint, testQPoint); CustomConvertibleType::s_value = testQPoint; @@ -244,7 +246,7 @@ void tst_QMetaType::convertCustomType() CustomConvertibleType::s_value = testQPointF; QCOMPARE(v.toPointF(), ok ? testQPointF : QPointF()); QCOMPARE(v.value<QPointF>(), ok ? testQPointF : QPointF()); - QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toPointF()), testQPointF); + QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toPointF()), ok ? testQPointF : QPointF()); QFETCH(QSize, testQSize); CustomConvertibleType::s_value = testQSize; @@ -256,7 +258,7 @@ void tst_QMetaType::convertCustomType() CustomConvertibleType::s_value = testQSizeF; QCOMPARE(v.toSizeF(), ok ? testQSizeF : QSizeF()); QCOMPARE(v.value<QSizeF>(), ok ? testQSizeF : QSizeF()); - QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toSizeF()), testQSizeF); + QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toSizeF()), ok ? testQSizeF : QSizeF()); QFETCH(QLine, testQLine); CustomConvertibleType::s_value = testQLine; @@ -268,7 +270,7 @@ void tst_QMetaType::convertCustomType() CustomConvertibleType::s_value = testQLineF; QCOMPARE(v.toLineF(), ok ? testQLineF : QLineF()); QCOMPARE(v.value<QLineF>(), ok ? testQLineF : QLineF()); - QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toLineF()), testQLineF); + QCOMPARE((CustomConvertibleType::s_value.value<CustomConvertibleType>().m_foo.toLineF()), ok ? testQLineF : QLineF()); QFETCH(QChar, testQChar); CustomConvertibleType::s_value = testQChar; @@ -277,9 +279,21 @@ void tst_QMetaType::convertCustomType() QFETCH(CustomConvertibleType, testCustom); v = QVariant::fromValue(testCustom); - QVERIFY(v.canConvert(::qMetaTypeId<CustomConvertibleType2>())); + QVERIFY(v.canConvert(QMetaType(::qMetaTypeId<CustomConvertibleType2>()))); QCOMPARE(v.value<CustomConvertibleType2>().m_foo, testCustom.m_foo); + // Check that converters that actually convert to std::optional<T> are not + // taken to indicate success or failure of the conversion. In these cases, + // the conversion must always succeed, even if the converter has returned a + // nullopt. + v = QVariant::fromValue(testCustom); + QVERIFY(v.canConvert(QMetaType::fromType<std::optional<CustomConvertibleType>>())); + QVERIFY(v.convert(QMetaType::fromType<std::optional<CustomConvertibleType>>())); + QCOMPARE(v.value<std::optional<CustomConvertibleType>>().has_value(), ok); + if (ok) { + QCOMPARE(v.value<std::optional<CustomConvertibleType>>().value().m_foo, testCustom.m_foo); + } + QFETCH(DerivedGadgetType, testDerived); v = QVariant::fromValue(testDerived); QCOMPARE(v.metaType(), QMetaType::fromType<DerivedGadgetType>()); @@ -437,6 +451,124 @@ void tst_QMetaType::operatorEq() QCOMPARE(typeB == typeA, eq); QCOMPARE(typeA != typeB, !eq); QCOMPARE(typeB != typeA, !eq); + +#if !defined(Q_OS_WIN) && !defined(Q_OS_INTEGRITY) + // for built-in types or locally-defined types, this must also hold true + if (eq) + QCOMPARE(typeA.iface(), typeB.iface()); +#endif +} + +void tst_QMetaType::operatorEq2_data() +{ + create_data(); +} + +void tst_QMetaType::operatorEq2() +{ + QFETCH(int, type); + QMetaType fromType1, fromType2; + QMetaType fromId1(type), fromId2(type); + + switch (type) { + case QMetaType::UnknownType: + break; +#define GET_METATYPE_FROM_TYPE(MetaTypeName, MetaTypeId, RealType) \ + case QMetaType::MetaTypeName: \ + fromType1 = QMetaType::fromType<RealType>(); \ + fromType2 = QMetaType::fromType<RealType>(); \ + break; +FOR_EACH_CORE_METATYPE(GET_METATYPE_FROM_TYPE) +#undef GET_METATYPE_FROM_TYPE + } + + // sanity check + QCOMPARE(fromId1.id(), type); + QCOMPARE(fromId2.id(), type); + + // confirm that they're all equal + QCOMPARE(fromId1, fromId2); + QCOMPARE(fromType1, fromType2); + QCOMPARE(fromType1, fromId1); + QCOMPARE(fromType2, fromId2); + +#if !defined(Q_OS_WIN) && !defined(Q_OS_INTEGRITY) + // for built-in types (other than void), this must be true + if (type != QMetaType::Void) { + QCOMPARE(fromType1.iface(), fromId1.iface()); + QCOMPARE(fromType2.iface(), fromId1.iface()); + } +#endif +} + +#define DECLARE_LIB_FUNCTION(TYPE, ID) \ + Q_DECL_IMPORT QMetaType metatype_ ## TYPE(); +namespace Lib1 { FOR_EACH_METATYPE_LIBS(DECLARE_LIB_FUNCTION) } +namespace Lib2 { FOR_EACH_METATYPE_LIBS(DECLARE_LIB_FUNCTION) } +#undef DECLARE_LIB_FUNCTION + +using LibMetatypeFunction = QMetaType (*)(); +void tst_QMetaType::operatorEqAcrossLibs_data() +{ + QTest::addColumn<int>("builtinTypeId"); + QTest::addColumn<QMetaType>("localType"); + QTest::addColumn<LibMetatypeFunction>("lib1Function"); + QTest::addColumn<LibMetatypeFunction>("lib2Function"); + +#define ADD_ROW(TYPE, ID) \ + QTest::addRow(QT_STRINGIFY(TYPE)) << int(ID) \ + << QMetaType::fromType<TYPE>() \ + << &Lib1::metatype_ ## TYPE \ + << &Lib2::metatype_ ## TYPE; +FOR_EACH_METATYPE_LIBS(ADD_ROW) +#undef ADD_ROW +} + +void tst_QMetaType::operatorEqAcrossLibs() +{ + QFETCH(int, builtinTypeId); + QFETCH(QMetaType, localType); + QFETCH(LibMetatypeFunction, lib1Function); + QFETCH(LibMetatypeFunction, lib2Function); + + QMetaType lib1Type = lib1Function(); + QMetaType lib2Type = lib2Function(); + + const QtPrivate::QMetaTypeInterface *localIface = localType.iface(); + const QtPrivate::QMetaTypeInterface *lib1Iface = lib1Type.iface(); + const QtPrivate::QMetaTypeInterface *lib2Iface = lib2Type.iface(); + + // DO THIS FIRST: + // if this isn't a built-in type, then the QMetaTypeInterface::typeId is + // initially set to 0 + QCOMPARE(lib1Type, lib2Type); + + int actualTypeId = localType.id(); + bool builtinTypeExpected = builtinTypeId != QMetaType::UnknownType; + bool builtinTypeActually = actualTypeId < QMetaType::User; + + qDebug() << "QMetaType for type" << QByteArray(localType.name()) + << "(type ID" << (actualTypeId >= 0x1000 ? Qt::hex : Qt::dec) << actualTypeId << ')' + << (builtinTypeActually ? "IS" : "is NOT") << "a built-in type;" + << "local interface:" << static_cast<const void *>(localIface) + << "lib1 interface:" << static_cast<const void *>(lib1Iface) + << "lib2 interface:" << static_cast<const void *>(lib2Iface); + + QCOMPARE(builtinTypeActually, builtinTypeExpected); + QCOMPARE(lib1Type.id(), actualTypeId); + QCOMPARE(lib2Type.id(), actualTypeId); + QCOMPARE(QByteArray(lib1Type.name()), QByteArray(localType.name())); + QCOMPARE(QByteArray(lib2Type.name()), QByteArray(localType.name())); + QCOMPARE(lib1Type, localType); + QCOMPARE(lib2Type, localType); + +#if !defined(Q_OS_WIN) && !defined(Q_OS_INTEGRITY) + if (actualTypeId < QMetaType::FirstGuiType && actualTypeId != QMetaType::Void) { + // for built-in QtCore types, we expect the interfaces to be the same too + QCOMPARE(lib1Iface, localIface); + QCOMPARE(lib2Iface, localIface); + } +#endif } class WithPrivateDTor { @@ -508,6 +640,83 @@ void tst_QMetaType::typeNameNormalization() } } +QT_BEGIN_NAMESPACE +namespace QtPrivate { struct tst_QMetaType_TestType {}; } +QT_END_NAMESPACE + +void tst_QMetaType::typeNameInQtPrivate() +{ + using T = QT_PREPEND_NAMESPACE(QtPrivate::tst_QMetaType_TestType); + + // some compilers (GCC) are known to suppress the namespace prefix if the + // type and the function where it is expanded on are on the same namespace + static constexpr char expectedName[] = "QtPrivate::tst_QMetaType_TestType"; + QMetaType mt = QMetaType::fromType<T>(); + QCOMPARE(mt.name(), expectedName); +} + +#if QT_DEPRECATED_SINCE(6, 0) +void tst_QMetaType::testDeprecatedGetters() +{ + QFETCH(int, aType); + QFETCH(QByteArray, aTypeName); + + if (aType >= QMetaType::FirstWidgetsType) + QSKIP("The test doesn't link against QtWidgets."); + + // QMetaType::type("name") -> QMetaType::fromName("name").id() + QT_IGNORE_DEPRECATIONS(QCOMPARE(QMetaType::type(aTypeName), + QMetaType::fromName(aTypeName).id());) + // QMetaType::typeName(int) -> QMetaType(int).name() + QT_IGNORE_DEPRECATIONS(QCOMPARE(QLatin1String(QMetaType::typeName(aType)), + QLatin1String(QMetaType(aType).name()));) + // QMetaType::typeFlags(int) -> QMetaType(int).flags() + QT_IGNORE_DEPRECATIONS(QCOMPARE(QMetaType::typeFlags(aType), + QMetaType(aType).flags());) + // QMetaType::metaObjectForType(int) -> QMetaType(int).metaObject() + QT_IGNORE_DEPRECATIONS(QCOMPARE(QMetaType::metaObjectForType(aType), + QMetaType(aType).metaObject());) +} + +void tst_QMetaType::testDeprecatedLoadSave() +{ + QFETCH(int, type); + QFETCH(bool, isStreamable); + + if (!isStreamable) + return; + + QMetaType metaType(type); + void *value = metaType.create(); + auto cleanup = qScopeGuard([&metaType, value]() { + metaType.destroy(value); + }); + + QByteArray ba; + QDataStream stream(&ba, QIODevice::ReadWrite); + + // Write using deprecated API + QT_IGNORE_DEPRECATIONS(QVERIFY(QMetaType::save(stream, type, value));) + QCOMPARE(stream.status(), QDataStream::Ok); + + // Read using non-deprecated API + stream.device()->seek(0); + QVERIFY(metaType.load(stream, value)); + QCOMPARE(stream.status(), QDataStream::Ok); + + // Write using non-deprecated API + stream.device()->seek(0); + ba.clear(); + QVERIFY(metaType.save(stream, value)); + QCOMPARE(stream.status(), QDataStream::Ok); + + // Read using deprecated API + stream.device()->seek(0); + QT_IGNORE_DEPRECATIONS(QVERIFY(QMetaType::load(stream, type, value));) + QCOMPARE(stream.status(), QDataStream::Ok); +} +#endif // QT_DEPRECATED_SINCE(6, 0) + // Compile-time test, it should be possible to register function pointer types class Undefined; diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype3.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype3.cpp index 52406b1e8d..779044589c 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype3.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype3.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include "tst_qmetatype.h" diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype_common.h b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype_common.h index 1ba9c47fa8..ea59b5cd02 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype_common.h +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype_common.h @@ -1,37 +1,13 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only // Used by both tst_qmetatype and tst_qsettings #ifndef TST_QMETATYPE_H #define TST_QMETATYPE_H -#include <qmetatype.h> +#include <QtCore> + #include <float.h> #define FOR_EACH_PRIMITIVE_METATYPE(F) \ @@ -153,6 +129,9 @@ template<> struct TestValueFactory<QMetaType::UChar> { template<> struct TestValueFactory<QMetaType::Float> { static float *create() { return new float(FLT_MIN); } }; +template<> struct TestValueFactory<QMetaType::Float16> { + static auto create() { return new qfloat16(std::numeric_limits<qfloat16>::min()); } +}; template<> struct TestValueFactory<QMetaType::QObjectStar> { static QObject * *create() { return new QObject *(0); } }; diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype_libs.h b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype_libs.h new file mode 100644 index 0000000000..673fc9083c --- /dev/null +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype_libs.h @@ -0,0 +1,24 @@ +// Copyright (C) 2022 Intel Corporation +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#ifndef TST_QMETATYPE_LIBS_H +#define TST_QMETATYPE_LIBS_H + +#include <qmetatype.h> + +#include <stdlib.h> // for div_t + +// void: builtin metatype, special +// int: builtin metatype, primitive type +// QString: builtin metatype, class +// QCollator: not builtin, class, Q_CORE_EXPORT +// div_t: not builtin, class, no export +#define FOR_EACH_METATYPE_LIBS(F) \ + F(void, QMetaType::Void) \ + F(int, QMetaType::Int) \ + F(QString, QMetaType::QString) \ + F(QCollator, QMetaType::UnknownType) \ + F(div_t, QMetaType::UnknownType) \ + /**/ + +#endif // TST_QMETATYPE_LIBS_H |