diff --git a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp index 3fc91e4f594..b444183f775 100644 --- a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp +++ b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp @@ -14,7 +14,37 @@ #include #include -#include +#include + +template +using AdlSwappableTest = decltype(swap(std::declval(), std::declval())); + +template +constexpr bool q_is_adl_swappable_v = qxp::is_detected_v; + +QT_BEGIN_NAMESPACE + +// +// Check Q_DECLARE_SHARED +// +#define MAKE_CLASS(C) \ + struct C \ + { \ + std::string s; \ + explicit C(std::string s = {}) : s{std::move(s)} {} \ + void swap(C &other) noexcept { std::swap(s, other.s); } \ + } \ + /* end */ + +MAKE_CLASS(NotQDeclareShared); +static_assert(!q_is_adl_swappable_v); + +MAKE_CLASS(Terry); // R.I.P. +Q_DECLARE_SHARED(Terry) + +#undef MAKE_CLASS + +QT_END_NAMESPACE class tst_QGlobal: public QObject { @@ -31,6 +61,8 @@ private slots: void qConstructorFunction(); void qCoreAppStartupFunction(); void qCoreAppStartupFunctionRestart(); + void qDeclareSharedMarksTheTypeRelocatable(); + void qDeclareSharedMakesTheTypeAdlSwappable(); void integerForSize(); void int128Literals(); void buildAbiEndianness(); @@ -395,6 +427,32 @@ void tst_QGlobal::qCoreAppStartupFunctionRestart() qCoreAppStartupFunction(); } +void tst_QGlobal::qDeclareSharedMarksTheTypeRelocatable() +{ + static_assert(!QTypeInfo::isRelocatable); + static_assert( QTypeInfo::isRelocatable); +} + +void tst_QGlobal::qDeclareSharedMakesTheTypeAdlSwappable() +{ + static_assert(!q_is_adl_swappable_v); + static_assert( q_is_adl_swappable_v); + + #define CHECK(Class) do { \ + using C = QT_PREPEND_NAMESPACE(Class); \ + C lhs("lhs"); \ + C rhs("rhs"); \ + QCOMPARE_EQ(lhs.s, "lhs"); \ + QCOMPARE_EQ(rhs.s, "rhs"); \ + /* no using std::swap - we're checking whether the ADL swap works */ \ + swap(lhs, rhs); \ + QCOMPARE_EQ(lhs.s, "rhs"); \ + QCOMPARE_EQ(rhs.s, "lhs"); \ + } while (false) + CHECK(Terry); + #undef CHECK +} + struct isEnum_A { int n_; };