/**************************************************************************** ** ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). ** All rights reserved. ** Contact: Nokia Corporation (qt-info@nokia.com) ** ** This file is part of the QtTest module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** GNU Lesser General Public License Usage ** This file may be used under the terms of the GNU Lesser General Public ** License version 2.1 as published by the Free Software Foundation and ** appearing in the file LICENSE.LGPL included in the packaging of this ** file. Please review the following information to ensure the GNU Lesser ** General Public License version 2.1 requirements will be met: ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Nokia gives you certain additional ** rights. These rights are described in the Nokia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU General ** Public License version 3.0 as published by the Free Software Foundation ** and appearing in the file LICENSE.GPL included in the packaging of this ** file. Please review the following information to ensure the GNU General ** Public License version 3.0 requirements will be met: ** http://www.gnu.org/copyleft/gpl.html. ** ** Other Usage ** Alternatively, this file may be used in accordance with the terms and ** conditions contained in a signed written agreement between you and Nokia. ** ** ** ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QTESTCASE_H #define QTESTCASE_H #include #include #include #include QT_BEGIN_HEADER QT_BEGIN_NAMESPACE QT_MODULE(Test) #define QVERIFY(statement) \ do {\ if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\ return;\ } while (0) #define QFAIL(message) \ do {\ QTest::qFail(message, __FILE__, __LINE__);\ return;\ } while (0) #define QVERIFY2(statement, description) \ do {\ if (statement) {\ if (!QTest::qVerify(true, #statement, (description), __FILE__, __LINE__))\ return;\ } else {\ if (!QTest::qVerify(false, #statement, (description), __FILE__, __LINE__))\ return;\ }\ } while (0) #define QCOMPARE(actual, expected) \ do {\ if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\ return;\ } while (0) // Will try to wait for the expression to become true while allowing event processing #define QTRY_VERIFY(__expr) \ do { \ const int __step = 50; \ const int __timeout = 5000; \ if (!(__expr)) { \ QTest::qWait(0); \ } \ for (int __i = 0; __i < __timeout && !(__expr); __i+=__step) { \ QTest::qWait(__step); \ } \ QVERIFY(__expr); \ } while (0) // Will try to wait for the comparison to become successful while allowing event processing #define QTRY_COMPARE(__expr, __expected) \ do { \ const int __step = 50; \ const int __timeout = 5000; \ if ((__expr) != (__expected)) { \ QTest::qWait(0); \ } \ for (int __i = 0; __i < __timeout && ((__expr) != (__expected)); __i+=__step) { \ QTest::qWait(__step); \ } \ QCOMPARE(__expr, __expected); \ } while (0) #ifdef Q_CC_MSVC #define QSKIP(statement) \ do {\ QTest::qSkip(statement, __FILE__, __LINE__);\ return;\ } while (0) #else #define QSKIP(statement, ...) \ do {\ if (strcmp(#__VA_ARGS__, "") != 0)\ QTest::qWarn("The two argument version of QSKIP is deprecated and will be removed soon. "\ "Please update this test by removing the second parameter.", __FILE__, __LINE__);\ QTest::qSkip(statement, __FILE__, __LINE__);\ return;\ } while (0) #endif #define QEXPECT_FAIL(dataIndex, comment, mode)\ do {\ if (!QTest::qExpectFail(dataIndex, comment, QTest::mode, __FILE__, __LINE__))\ return;\ } while (0) #define QFETCH(type, name)\ type name = *static_cast(QTest::qData(#name, ::qMetaTypeId())) #define QFETCH_GLOBAL(type, name)\ type name = *static_cast(QTest::qGlobalData(#name, ::qMetaTypeId())) #define QTEST(actual, testElement)\ do {\ if (!QTest::qTest(actual, testElement, #actual, #testElement, __FILE__, __LINE__))\ return;\ } while (0) #define QWARN(msg)\ QTest::qWarn(msg, __FILE__, __LINE__) class QObject; class QTestData; #define QTEST_COMPARE_DECL(KLASS)\ template<> Q_TESTLIB_EXPORT char *toString(const KLASS &); namespace QTest { template inline char *toString(const T &) { return 0; } Q_TESTLIB_EXPORT char *toHexRepresentation(const char *ba, int length); Q_TESTLIB_EXPORT char *toString(const char *); Q_TESTLIB_EXPORT char *toString(const void *); Q_TESTLIB_EXPORT int qExec(QObject *testObject, int argc = 0, char **argv = 0); Q_TESTLIB_EXPORT int qExec(QObject *testObject, const QStringList &arguments); Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description, const char *file, int line); Q_TESTLIB_EXPORT void qFail(const char *statementStr, const char *file, int line); Q_TESTLIB_EXPORT void qSkip(const char *message, const char *file, int line); Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode, const char *file, int line); Q_TESTLIB_EXPORT void qWarn(const char *message, const char *file = 0, int line = 0); Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message); Q_TESTLIB_EXPORT void *qData(const char *tagName, int typeId); Q_TESTLIB_EXPORT void *qGlobalData(const char *tagName, int typeId); Q_TESTLIB_EXPORT void *qElementData(const char *elementName, int metaTypeId); Q_TESTLIB_EXPORT QObject *testObject(); Q_TESTLIB_EXPORT const char *currentTestFunction(); Q_TESTLIB_EXPORT const char *currentDataTag(); Q_TESTLIB_EXPORT bool currentTestFailed(); Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii); Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key); Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *msg, const char *file, int line); Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *msg, char *val1, char *val2, const char *expected, const char *actual, const char *file, int line); Q_TESTLIB_EXPORT void qSleep(int ms); Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name); template inline void addColumn(const char *name, T * = 0) { addColumnInternal(qMetaTypeId(), name); } Q_TESTLIB_EXPORT QTestData &newRow(const char *dataTag); template inline bool qCompare(T const &t1, T const &t2, const char *actual, const char *expected, const char *file, int line) { return (t1 == t2) ? compare_helper(true, "COMPARE()", file, line) : compare_helper(false, "Compared values are not the same", toString(t1), toString(t2), actual, expected, file, line); } template <> Q_TESTLIB_EXPORT bool qCompare(float const &t1, float const &t2, const char *actual, const char *expected, const char *file, int line); template <> Q_TESTLIB_EXPORT bool qCompare(double const &t1, double const &t2, const char *actual, const char *expected, const char *file, int line); inline bool compare_ptr_helper(const void *t1, const void *t2, const char *actual, const char *expected, const char *file, int line) { return (t1 == t2) ? compare_helper(true, "COMPARE()", file, line) : compare_helper(false, "Compared pointers are not the same", toString(t1), toString(t2), actual, expected, file, line); } Q_TESTLIB_EXPORT bool compare_string_helper(const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line); #ifndef qdoc QTEST_COMPARE_DECL(short) QTEST_COMPARE_DECL(ushort) QTEST_COMPARE_DECL(int) QTEST_COMPARE_DECL(uint) QTEST_COMPARE_DECL(long) QTEST_COMPARE_DECL(ulong) QTEST_COMPARE_DECL(qint64) QTEST_COMPARE_DECL(quint64) QTEST_COMPARE_DECL(float) QTEST_COMPARE_DECL(double) QTEST_COMPARE_DECL(char) QTEST_COMPARE_DECL(bool) #endif #ifndef QTEST_NO_SPECIALIZATIONS template bool qCompare(T1 const &, T2 const &, const char *, const char *, const char *, int); #if defined(QT_COORD_TYPE) && (defined(QT_ARCH_ARM) || defined(QT_NO_FPU) || defined(QT_ARCH_WINDOWSCE)) template <> inline bool qCompare(qreal const &t1, float const &t2, const char *actual, const char *expected, const char *file, int line) { return qCompare(t1, qreal(t2), actual, expected, file, line); } template <> inline bool qCompare(float const &t1, qreal const &t2, const char *actual, const char *expected, const char *file, int line) { return qCompare(qreal(t1), t2, actual, expected, file, line); } #elif defined(QT_COORD_TYPE) || defined(QT_ARCH_ARM) || defined(QT_NO_FPU) || defined(QT_ARCH_WINDOWSCE) template <> inline bool qCompare(qreal const &t1, double const &t2, const char *actual, const char *expected, const char *file, int line) { return qCompare(float(t1), float(t2), actual, expected, file, line); } template <> inline bool qCompare(double const &t1, qreal const &t2, const char *actual, const char *expected, const char *file, int line) { return qCompare(float(t1), float(t2), actual, expected, file, line); } #endif template inline bool qCompare(const T *t1, const T *t2, const char *actual, const char *expected, const char *file, int line) { return compare_ptr_helper(t1, t2, actual, expected, file, line); } template inline bool qCompare(T *t1, T *t2, const char *actual, const char *expected, const char *file, int line) { return compare_ptr_helper(t1, t2, actual, expected, file, line); } template inline bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected, const char *file, int line) { return compare_ptr_helper(t1, static_cast(t2), actual, expected, file, line); } template inline bool qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected, const char *file, int line) { return compare_ptr_helper(const_cast(t1), static_cast(const_cast(t2)), actual, expected, file, line); } template<> inline bool qCompare(const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line) { return compare_string_helper(t1, t2, actual, expected, file, line); } template<> inline bool qCompare(char *t1, char *t2, const char *actual, const char *expected, const char *file, int line) { return compare_string_helper(t1, t2, actual, expected, file, line); } #else /* QTEST_NO_SPECIALIZATIONS */ inline bool qCompare(const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line) { return compare_string_helper(t1, t2, actual, expected, file, line); } inline bool qCompare(char *t1, char *t2, const char *actual, const char *expected, const char *file, int line) { return compare_string_helper(t1, t2, actual, expected, file, line); } #endif /* The next two specializations are for MSVC that shows problems with implicit conversions */ #ifndef QTEST_NO_SPECIALIZATIONS template<> #endif inline bool qCompare(char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line) { return compare_string_helper(t1, t2, actual, expected, file, line); } #ifndef QTEST_NO_SPECIALIZATIONS template<> #endif inline bool qCompare(const char *t1, char *t2, const char *actual, const char *expected, const char *file, int line) { return compare_string_helper(t1, t2, actual, expected, file, line); } // NokiaX86 and RVCT do not like implicitly comparing bool with int #ifndef QTEST_NO_SPECIALIZATIONS template <> #endif inline bool qCompare(bool const &t1, int const &t2, const char *actual, const char *expected, const char *file, int line) { return qCompare(int(t1), t2, actual, expected, file, line); } template inline bool qTest(const T& actual, const char *elementName, const char *actualStr, const char *expected, const char *file, int line) { return qCompare(actual, *static_cast(QTest::qElementData(elementName, qMetaTypeId())), actualStr, expected, file, line); } } #undef QTEST_COMPARE_DECL QT_END_NAMESPACE QT_END_HEADER #endif