From 663cd1771883e1e7ac9c1a0dc8b797601b59ba17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Nowacki?= Date: Fri, 14 Oct 2011 12:58:54 +0200 Subject: Introduce Q_STATIC_ASSERT MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Example of message of failed assert (gcc 4.6, file tst_qglobal.cpp:300): tst_qglobal.cpp:300:92: error: invalid application of ‘sizeof’ to incomplete type ‘QStaticAssertFailure’ Change-Id: Ic1798094f718eaad388d754034115aafbbb6bd5e Reviewed-by: João Abecasis --- tests/auto/corelib/global/qglobal/tst_qglobal.cpp | 68 +++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp index 85aa03f642..9dc78a14c0 100644 --- a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp +++ b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp @@ -52,6 +52,7 @@ private slots: void qassert(); void qtry(); void checkptr(); + void qstaticassert(); }; void tst_QGlobal::qIsNull() @@ -267,5 +268,72 @@ void tst_QGlobal::checkptr() QCOMPARE(q_check_ptr(c), c); } +// Check Q_STATIC_ASSERT, It should compile +// note that, we are not able to test Q_STATIC_ASSERT(false), to do it manually someone has +// to replace expressions (in the asserts) one by one to false, and check if it breaks build. +class MyTrue +{ +public: + MyTrue() + { + Q_STATIC_ASSERT(true); + Q_STATIC_ASSERT(!false); + } + ~MyTrue() + { + Q_STATIC_ASSERT(true); + Q_STATIC_ASSERT(!false); + } + Q_STATIC_ASSERT(true); + Q_STATIC_ASSERT(!false); +}; + +struct MyExpresion +{ + void foo() + { + Q_STATIC_ASSERT(sizeof(MyTrue) > 0); + Q_STATIC_ASSERT(sizeof(MyTrue) > 0); + } +private: + Q_STATIC_ASSERT(sizeof(MyTrue) > 0); + Q_STATIC_ASSERT(sizeof(MyTrue) > 0); +}; + +struct TypeDef +{ + typedef int T; + Q_STATIC_ASSERT(sizeof(T)); +}; + +template +struct Template +{ + static const bool True = true; + typedef typename T1::T DependentType; + Q_STATIC_ASSERT(True); + Q_STATIC_ASSERT(!!True); + Q_STATIC_ASSERT(sizeof(DependentType)); + Q_STATIC_ASSERT(!!sizeof(DependentType)); +}; + +struct MyTemplate +{ + Q_STATIC_ASSERT(Template::True); + Q_STATIC_ASSERT(!!Template::True); +}; + +void tst_QGlobal::qstaticassert() +{ + // Force compilation of these classes + MyTrue tmp1; + MyExpresion tmp2; + MyTemplate tmp3; + Q_UNUSED(tmp1); + Q_UNUSED(tmp2); + Q_UNUSED(tmp3); + QVERIFY(true); // if the test compiles it has passed. +} + QTEST_MAIN(tst_QGlobal) #include "tst_qglobal.moc" -- cgit v1.2.3