1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtTest module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 #include <QtTest/qtest_global.h>
47 #include <QtCore/qstring.h>
48 #include <QtCore/qnamespace.h>
49 #include <QtCore/qmetatype.h>
58 #define QVERIFY(statement) \
60 if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\
64 #define QFAIL(message) \
66 QTest::qFail(message, __FILE__, __LINE__);\
70 #define QVERIFY2(statement, description) \
73 if (!QTest::qVerify(true, #statement, (description), __FILE__, __LINE__))\
76 if (!QTest::qVerify(false, #statement, (description), __FILE__, __LINE__))\
81 #define QCOMPARE(actual, expected) \
83 if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\
87 // Will try to wait for the expression to become true while allowing event processing
88 #define QTRY_VERIFY_WITH_TIMEOUT(__expr, __timeout) \
90 const int __step = 50; \
91 const int __timeoutValue = __timeout; \
95 for (int __i = 0; __i < __timeoutValue && !(__expr); __i+=__step) { \
96 QTest::qWait(__step); \
101 #define QTRY_VERIFY(__expr) QTRY_VERIFY_WITH_TIMEOUT(__expr, 5000)
103 // Will try to wait for the comparison to become successful while allowing event processing
105 #define QTRY_COMPARE_WITH_TIMEOUT(__expr, __expected, __timeout) \
107 const int __step = 50; \
108 const int __timeoutValue = __timeout; \
109 if ((__expr) != (__expected)) { \
112 for (int __i = 0; __i < __timeoutValue && ((__expr) != (__expected)); __i+=__step) { \
113 QTest::qWait(__step); \
115 QCOMPARE(__expr, __expected); \
118 #define QTRY_COMPARE(__expr, __expected) QTRY_COMPARE_WITH_TIMEOUT(__expr, __expected, 5000)
120 #define QSKIP_INTERNAL(statement) \
122 QTest::qSkip(statement, __FILE__, __LINE__);\
126 #ifdef Q_COMPILER_VARIADIC_MACROS
128 #define QSKIP(statement, ...) QSKIP_INTERNAL(statement)
132 #define QSKIP(statement) QSKIP_INTERNAL(statement)
136 #define QEXPECT_FAIL(dataIndex, comment, mode)\
138 if (!QTest::qExpectFail(dataIndex, comment, QTest::mode, __FILE__, __LINE__))\
142 #define QFETCH(type, name)\
143 type name = *static_cast<type *>(QTest::qData(#name, ::qMetaTypeId<type >()))
145 #define QFETCH_GLOBAL(type, name)\
146 type name = *static_cast<type *>(QTest::qGlobalData(#name, ::qMetaTypeId<type >()))
148 #define QTEST(actual, testElement)\
150 if (!QTest::qTest(actual, testElement, #actual, #testElement, __FILE__, __LINE__))\
155 QTest::qWarn(msg, __FILE__, __LINE__)
157 #ifdef QT_TESTCASE_BUILDDIR
158 # define QFINDTESTDATA(basepath)\
159 QTest::qFindTestData(basepath, __FILE__, __LINE__, QT_TESTCASE_BUILDDIR)
161 # define QFINDTESTDATA(basepath)\
162 QTest::qFindTestData(basepath, __FILE__, __LINE__)
168 #define QTEST_COMPARE_DECL(KLASS)\
169 template<> Q_TESTLIB_EXPORT char *toString<KLASS >(const KLASS &);
173 template <typename T>
174 inline char *toString(const T &)
180 Q_TESTLIB_EXPORT char *toHexRepresentation(const char *ba, int length);
181 Q_TESTLIB_EXPORT char *toString(const char *);
182 Q_TESTLIB_EXPORT char *toString(const void *);
184 Q_TESTLIB_EXPORT int qExec(QObject *testObject, int argc = 0, char **argv = 0);
185 Q_TESTLIB_EXPORT int qExec(QObject *testObject, const QStringList &arguments);
187 Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description,
188 const char *file, int line);
189 Q_TESTLIB_EXPORT void qFail(const char *statementStr, const char *file, int line);
190 Q_TESTLIB_EXPORT void qSkip(const char *message, const char *file, int line);
191 Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode,
192 const char *file, int line);
193 Q_TESTLIB_EXPORT void qWarn(const char *message, const char *file = 0, int line = 0);
194 Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message);
196 Q_TESTLIB_EXPORT QString qFindTestData(const char* basepath, const char* file = 0, int line = 0, const char* builddir = 0);
197 Q_TESTLIB_EXPORT QString qFindTestData(const QString& basepath, const char* file = 0, int line = 0, const char* builddir = 0);
199 Q_TESTLIB_EXPORT void *qData(const char *tagName, int typeId);
200 Q_TESTLIB_EXPORT void *qGlobalData(const char *tagName, int typeId);
201 Q_TESTLIB_EXPORT void *qElementData(const char *elementName, int metaTypeId);
202 Q_TESTLIB_EXPORT QObject *testObject();
204 Q_TESTLIB_EXPORT const char *currentTestFunction();
205 Q_TESTLIB_EXPORT const char *currentDataTag();
206 Q_TESTLIB_EXPORT bool currentTestFailed();
208 Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii);
209 Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key);
211 Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
212 char *val1, char *val2,
213 const char *actual, const char *expected,
214 const char *file, int line);
215 Q_TESTLIB_EXPORT void qSleep(int ms);
216 Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name);
218 template <typename T>
219 inline void addColumn(const char *name, T * = 0)
221 addColumnInternal(qMetaTypeId<T>(), name);
223 Q_TESTLIB_EXPORT QTestData &newRow(const char *dataTag);
225 template <typename T>
226 inline bool qCompare(T const &t1, T const &t2, const char *actual, const char *expected,
227 const char *file, int line)
229 return compare_helper(t1 == t2, "Compared values are not the same",
230 toString<T>(t1), toString<T>(t2), actual, expected, file, line);
234 Q_TESTLIB_EXPORT bool qCompare<float>(float const &t1, float const &t2,
235 const char *actual, const char *expected, const char *file, int line);
238 Q_TESTLIB_EXPORT bool qCompare<double>(double const &t1, double const &t2,
239 const char *actual, const char *expected, const char *file, int line);
241 inline bool compare_ptr_helper(const void *t1, const void *t2, const char *actual,
242 const char *expected, const char *file, int line)
244 return compare_helper(t1 == t2, "Compared pointers are not the same",
245 toString(t1), toString(t2), actual, expected, file, line);
248 Q_TESTLIB_EXPORT bool compare_string_helper(const char *t1, const char *t2, const char *actual,
249 const char *expected, const char *file, int line);
252 QTEST_COMPARE_DECL(short)
253 QTEST_COMPARE_DECL(ushort)
254 QTEST_COMPARE_DECL(int)
255 QTEST_COMPARE_DECL(uint)
256 QTEST_COMPARE_DECL(long)
257 QTEST_COMPARE_DECL(ulong)
258 QTEST_COMPARE_DECL(qint64)
259 QTEST_COMPARE_DECL(quint64)
261 QTEST_COMPARE_DECL(float)
262 QTEST_COMPARE_DECL(double)
263 QTEST_COMPARE_DECL(char)
264 QTEST_COMPARE_DECL(bool)
267 template <typename T1, typename T2>
268 bool qCompare(T1 const &, T2 const &, const char *, const char *, const char *, int);
271 inline bool qCompare<double, float>(double const &t1, float const &t2, const char *actual,
272 const char *expected, const char *file, int line)
274 return qCompare<qreal>(qreal(t1), qreal(t2), actual, expected, file, line);
278 inline bool qCompare<float, double>(float const &t1, double const &t2, const char *actual,
279 const char *expected, const char *file, int line)
281 return qCompare<qreal>(qreal(t1), qreal(t2), actual, expected, file, line);
284 template <typename T>
285 inline bool qCompare(const T *t1, const T *t2, const char *actual, const char *expected,
286 const char *file, int line)
288 return compare_ptr_helper(t1, t2, actual, expected, file, line);
290 template <typename T>
291 inline bool qCompare(T *t1, T *t2, const char *actual, const char *expected,
292 const char *file, int line)
294 return compare_ptr_helper(t1, t2, actual, expected, file, line);
297 template <typename T1, typename T2>
298 inline bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected,
299 const char *file, int line)
301 return compare_ptr_helper(t1, static_cast<const T1 *>(t2), actual, expected, file, line);
303 template <typename T1, typename T2>
304 inline bool qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected,
305 const char *file, int line)
307 return compare_ptr_helper(const_cast<const T1 *>(t1),
308 static_cast<const T1 *>(const_cast<const T2 *>(t2)), actual, expected, file, line);
311 inline bool qCompare<char>(const char *t1, const char *t2, const char *actual,
312 const char *expected, const char *file, int line)
314 return compare_string_helper(t1, t2, actual, expected, file, line);
317 inline bool qCompare<char>(char *t1, char *t2, const char *actual, const char *expected,
318 const char *file, int line)
320 return compare_string_helper(t1, t2, actual, expected, file, line);
323 /* The next two specializations are for MSVC that shows problems with implicit
327 inline bool qCompare(char *t1, const char *t2, const char *actual,
328 const char *expected, const char *file, int line)
330 return compare_string_helper(t1, t2, actual, expected, file, line);
333 inline bool qCompare(const char *t1, char *t2, const char *actual,
334 const char *expected, const char *file, int line)
336 return compare_string_helper(t1, t2, actual, expected, file, line);
339 // NokiaX86 and RVCT do not like implicitly comparing bool with int
341 inline bool qCompare(bool const &t1, int const &t2,
342 const char *actual, const char *expected, const char *file, int line)
344 return qCompare<int>(int(t1), t2, actual, expected, file, line);
349 inline bool qTest(const T& actual, const char *elementName, const char *actualStr,
350 const char *expected, const char *file, int line)
352 return qCompare(actual, *static_cast<const T *>(QTest::qElementData(elementName,
353 qMetaTypeId<T>())), actualStr, expected, file, line);
357 #undef QTEST_COMPARE_DECL