diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp | 139 | ||||
-rw-r--r-- | tests/auto/gui/kernel/qguimetatype/tst_qguimetatype.cpp | 131 |
2 files changed, 270 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index cd01518e9e..8574c14b7c 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -76,6 +76,12 @@ private slots: void create(); void createCopy_data(); void createCopy(); + void sizeOf_data(); + void sizeOf(); + void construct_data(); + void construct(); + void constructCopy_data(); + void constructCopy(); void typedefs(); void isRegistered_data(); void isRegistered(); @@ -552,6 +558,139 @@ FOR_EACH_CORE_METATYPE(RETURN_CREATE_COPY_FUNCTION) TypeTestFunctionGetter::get(type)(); } +void tst_QMetaType::sizeOf_data() +{ + QTest::addColumn<QMetaType::Type>("type"); + QTest::addColumn<int>("size"); +#define ADD_METATYPE_TEST_ROW(TYPE, ID) \ + QTest::newRow(QMetaType::typeName(QMetaType::ID)) << QMetaType::ID << int(sizeof(TYPE)); +FOR_EACH_CORE_METATYPE(ADD_METATYPE_TEST_ROW) +#undef ADD_METATYPE_TEST_ROW +} + +void tst_QMetaType::sizeOf() +{ + QFETCH(QMetaType::Type, type); + QFETCH(int, size); + QCOMPARE(QMetaType::sizeOf(type), size); +} + +void tst_QMetaType::construct_data() +{ + create_data(); +} + +#ifndef Q_ALIGNOF +template<uint N> +struct RoundToNextHighestPowerOfTwo +{ +private: + enum { V1 = N-1 }; + enum { V2 = V1 | (V1 >> 1) }; + enum { V3 = V2 | (V2 >> 2) }; + enum { V4 = V3 | (V3 >> 4) }; + enum { V5 = V4 | (V4 >> 8) }; + enum { V6 = V5 | (V5 >> 16) }; +public: + enum { Value = V6 + 1 }; +}; +#endif + +template<class T> +struct TypeAlignment +{ +#ifdef Q_ALIGNOF + enum { Value = Q_ALIGNOF(T) }; +#else + enum { Value = RoundToNextHighestPowerOfTwo<sizeof(T)>::Value }; +#endif +}; + +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); + if (DefaultValueTraits<ID>::IsInitialized) { + Type *expected = DefaultValueFactory<ID>::create(); + QCOMPARE(*static_cast<Type *>(actual), *expected); + delete expected; + } + QMetaType::destruct(ID, actual); + qFreeAligned(storage); + + QVERIFY(QMetaType::construct(ID, 0, /*copy=*/0) == 0); + QMetaType::destruct(ID, 0); +} + +void tst_QMetaType::construct() +{ + struct TypeTestFunctionGetter + { + static TypeTestFunction get(int type) + { + switch (type) { +#define RETURN_CONSTRUCT_FUNCTION(TYPE, ID) \ + case QMetaType::ID: \ + return testConstructHelper<QMetaType::ID>; +FOR_EACH_CORE_METATYPE(RETURN_CONSTRUCT_FUNCTION) +#undef RETURN_CONSTRUCT_FUNCTION + } + return 0; + } + }; + + QFETCH(QMetaType::Type, type); + TypeTestFunctionGetter::get(type)(); +} + +template<int ID> +static void testConstructCopyHelper() +{ + typedef typename MetaEnumToType<ID>::Type Type; + Type *expected = TestValueFactory<ID>::create(); + 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); + + QVERIFY(QMetaType::construct(ID, 0, expected) == 0); + + delete expected; +} + +void tst_QMetaType::constructCopy_data() +{ + create_data(); +} + +void tst_QMetaType::constructCopy() +{ + struct TypeTestFunctionGetter + { + static TypeTestFunction get(int type) + { + switch (type) { +#define RETURN_CONSTRUCT_COPY_FUNCTION(TYPE, ID) \ + case QMetaType::ID: \ + return testConstructCopyHelper<QMetaType::ID>; +FOR_EACH_CORE_METATYPE(RETURN_CONSTRUCT_COPY_FUNCTION) +#undef RETURN_CONSTRUCT_COPY_FUNCTION + } + return 0; + } + }; + + QFETCH(QMetaType::Type, type); + TypeTestFunctionGetter::get(type)(); +} + typedef QString CustomString; Q_DECLARE_METATYPE(CustomString) //this line is useless diff --git a/tests/auto/gui/kernel/qguimetatype/tst_qguimetatype.cpp b/tests/auto/gui/kernel/qguimetatype/tst_qguimetatype.cpp index 4b5aa347a8..98314a9336 100644 --- a/tests/auto/gui/kernel/qguimetatype/tst_qguimetatype.cpp +++ b/tests/auto/gui/kernel/qguimetatype/tst_qguimetatype.cpp @@ -54,6 +54,12 @@ private slots: void create(); void createCopy_data(); void createCopy(); + void sizeOf_data(); + void sizeOf(); + void construct_data(); + void construct(); + void constructCopy_data(); + void constructCopy(); }; #define FOR_EACH_GUI_METATYPE(F) \ @@ -271,5 +277,130 @@ FOR_EACH_GUI_METATYPE(RETURN_CREATE_COPY_FUNCTION) TypeTestFunctionGetter::get(type)(); } +void tst_QGuiMetaType::sizeOf_data() +{ + QTest::addColumn<QMetaType::Type>("type"); + QTest::addColumn<int>("size"); +#define ADD_METATYPE_TEST_ROW(TYPE, ID) \ + QTest::newRow(QMetaType::typeName(QMetaType::ID)) << QMetaType::ID << int(sizeof(TYPE)); +FOR_EACH_GUI_METATYPE(ADD_METATYPE_TEST_ROW) +#undef ADD_METATYPE_TEST_ROW +} + +void tst_QGuiMetaType::sizeOf() +{ + QFETCH(QMetaType::Type, type); + QFETCH(int, size); + QCOMPARE(QMetaType::sizeOf(type), size); +} + +#ifndef Q_ALIGNOF +template<uint N> +struct RoundToNextHighestPowerOfTwo +{ +private: + enum { V1 = N-1 }; + enum { V2 = V1 | (V1 >> 1) }; + enum { V3 = V2 | (V2 >> 2) }; + enum { V4 = V3 | (V3 >> 4) }; + enum { V5 = V4 | (V4 >> 8) }; + enum { V6 = V5 | (V5 >> 16) }; +public: + enum { Value = V6 + 1 }; +}; +#endif + +template<class T> +struct TypeAlignment +{ +#ifdef Q_ALIGNOF + enum { Value = Q_ALIGNOF(T) }; +#else + enum { Value = RoundToNextHighestPowerOfTwo<sizeof(T)>::Value }; +#endif +}; + +void tst_QGuiMetaType::construct_data() +{ + create_data(); +} + +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); + Type *expected = DefaultValueFactory<ID>::create(); + QVERIFY2(TypeComparator<ID>::equal(*static_cast<Type *>(actual), *expected), QMetaType::typeName(ID)); + delete expected; + QMetaType::destruct(ID, actual); + qFreeAligned(storage); +} + +void tst_QGuiMetaType::construct() +{ + struct TypeTestFunctionGetter + { + static TypeTestFunction get(int type) + { + switch (type) { +#define RETURN_CONSTRUCT_FUNCTION(TYPE, ID) \ + case QMetaType::ID: \ + return testConstructHelper<QMetaType::ID>; +FOR_EACH_GUI_METATYPE(RETURN_CONSTRUCT_FUNCTION) +#undef RETURN_CONSTRUCT_FUNCTION + } + return 0; + } + }; + + QFETCH(QMetaType::Type, type); + TypeTestFunctionGetter::get(type)(); +} + +void tst_QGuiMetaType::constructCopy_data() +{ + create_data(); +} + +template <int ID> +static void testConstructCopyHelper() +{ + typedef typename MetaEnumToType<ID>::Type Type; + Type *expected = TestValueFactory<ID>::create(); + int size = QMetaType::sizeOf(ID); + void *storage = qMallocAligned(size, TypeAlignment<Type>::Value); + void *actual = QMetaType::construct(ID, storage, expected); + QCOMPARE(actual, storage); + QVERIFY2(TypeComparator<ID>::equal(*static_cast<Type*>(actual), *expected), QMetaType::typeName(ID)); + QMetaType::destruct(ID, actual); + qFreeAligned(storage); + delete expected; +} + +void tst_QGuiMetaType::constructCopy() +{ + struct TypeTestFunctionGetter + { + static TypeTestFunction get(int type) + { + switch (type) { +#define RETURN_CONSTRUCT_COPY_FUNCTION(TYPE, ID) \ + case QMetaType::ID: \ + return testConstructCopyHelper<QMetaType::ID>; +FOR_EACH_GUI_METATYPE(RETURN_CONSTRUCT_COPY_FUNCTION) +#undef RETURN_CONSTRUCT_COPY_FUNCTION + } + return 0; + } + }; + + QFETCH(QMetaType::Type, type); + TypeTestFunctionGetter::get(type)(); +} + QTEST_MAIN(tst_QGuiMetaType) #include "tst_qguimetatype.moc" |