1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtTest module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
45 #include <QtTest/qtest_global.h>
47 #include <QtCore/qstring.h>
48 #include <QtCore/qnamespace.h>
49 #include <QtCore/qmetatype.h>
50 #include <QtCore/qtypetraits.h>
59 #define QVERIFY(statement) \
61 if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\
65 #define QFAIL(message) \
67 QTest::qFail(message, __FILE__, __LINE__);\
71 #define QVERIFY2(statement, description) \
74 if (!QTest::qVerify(true, #statement, (description), __FILE__, __LINE__))\
77 if (!QTest::qVerify(false, #statement, (description), __FILE__, __LINE__))\
82 #define QCOMPARE(actual, expected) \
84 if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\
88 // Will try to wait for the expression to become true while allowing event processing
89 #define QTRY_VERIFY_WITH_TIMEOUT(__expr, __timeout) \
91 const int __step = 50; \
92 const int __timeoutValue = __timeout; \
96 for (int __i = 0; __i < __timeoutValue && !(__expr); __i+=__step) { \
97 QTest::qWait(__step); \
102 #define QTRY_VERIFY(__expr) QTRY_VERIFY_WITH_TIMEOUT(__expr, 5000)
104 // Will try to wait for the comparison to become successful while allowing event processing
106 #define QTRY_COMPARE_WITH_TIMEOUT(__expr, __expected, __timeout) \
108 const int __step = 50; \
109 const int __timeoutValue = __timeout; \
110 if ((__expr) != (__expected)) { \
113 for (int __i = 0; __i < __timeoutValue && ((__expr) != (__expected)); __i+=__step) { \
114 QTest::qWait(__step); \
116 QCOMPARE(__expr, __expected); \
119 #define QTRY_COMPARE(__expr, __expected) QTRY_COMPARE_WITH_TIMEOUT(__expr, __expected, 5000)
121 #define QSKIP_INTERNAL(statement) \
123 QTest::qSkip(statement, __FILE__, __LINE__);\
127 #ifdef Q_COMPILER_VARIADIC_MACROS
129 #define QSKIP(statement, ...) QSKIP_INTERNAL(statement)
133 #define QSKIP(statement) QSKIP_INTERNAL(statement)
137 #define QEXPECT_FAIL(dataIndex, comment, mode)\
139 if (!QTest::qExpectFail(dataIndex, comment, QTest::mode, __FILE__, __LINE__))\
143 #define QFETCH(type, name)\
144 type name = *static_cast<type *>(QTest::qData(#name, ::qMetaTypeId<type >()))
146 #define QFETCH_GLOBAL(type, name)\
147 type name = *static_cast<type *>(QTest::qGlobalData(#name, ::qMetaTypeId<type >()))
149 #define QTEST(actual, testElement)\
151 if (!QTest::qTest(actual, testElement, #actual, #testElement, __FILE__, __LINE__))\
156 QTest::qWarn(msg, __FILE__, __LINE__)
158 #ifdef QT_TESTCASE_BUILDDIR
159 # define QFINDTESTDATA(basepath)\
160 QTest::qFindTestData(basepath, __FILE__, __LINE__, QT_TESTCASE_BUILDDIR)
162 # define QFINDTESTDATA(basepath)\
163 QTest::qFindTestData(basepath, __FILE__, __LINE__)
169 #define QTEST_COMPARE_DECL(KLASS)\
170 template<> Q_TESTLIB_EXPORT char *toString<KLASS >(const KLASS &);
174 template <typename T>
175 inline char *toString(const T &)
181 Q_TESTLIB_EXPORT char *toHexRepresentation(const char *ba, int length);
182 Q_TESTLIB_EXPORT char *toString(const char *);
183 Q_TESTLIB_EXPORT char *toString(const void *);
185 Q_TESTLIB_EXPORT int qExec(QObject *testObject, int argc = 0, char **argv = 0);
186 Q_TESTLIB_EXPORT int qExec(QObject *testObject, const QStringList &arguments);
188 Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description,
189 const char *file, int line);
190 Q_TESTLIB_EXPORT void qFail(const char *statementStr, const char *file, int line);
191 Q_TESTLIB_EXPORT void qSkip(const char *message, const char *file, int line);
192 Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode,
193 const char *file, int line);
194 Q_TESTLIB_EXPORT void qWarn(const char *message, const char *file = 0, int line = 0);
195 Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message);
197 Q_TESTLIB_EXPORT QString qFindTestData(const char* basepath, const char* file = 0, int line = 0, const char* builddir = 0);
198 Q_TESTLIB_EXPORT QString qFindTestData(const QString& basepath, const char* file = 0, int line = 0, const char* builddir = 0);
200 Q_TESTLIB_EXPORT void *qData(const char *tagName, int typeId);
201 Q_TESTLIB_EXPORT void *qGlobalData(const char *tagName, int typeId);
202 Q_TESTLIB_EXPORT void *qElementData(const char *elementName, int metaTypeId);
203 Q_TESTLIB_EXPORT QObject *testObject();
205 Q_TESTLIB_EXPORT const char *currentTestFunction();
206 Q_TESTLIB_EXPORT const char *currentDataTag();
207 Q_TESTLIB_EXPORT bool currentTestFailed();
209 Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii);
210 Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key);
212 Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
213 char *val1, char *val2,
214 const char *actual, const char *expected,
215 const char *file, int line);
216 Q_TESTLIB_EXPORT void qSleep(int ms);
217 Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name);
219 template <typename T>
220 inline void addColumn(const char *name, T * = 0)
222 typedef QtPrivate::is_same<T, const char*> QIsSameTConstChar;
223 Q_STATIC_ASSERT_X(!QIsSameTConstChar::value, "const char* is not allowed as a test data format.");
224 addColumnInternal(qMetaTypeId<T>(), name);
226 Q_TESTLIB_EXPORT QTestData &newRow(const char *dataTag);
228 template <typename T>
229 inline bool qCompare(T const &t1, T const &t2, const char *actual, const char *expected,
230 const char *file, int line)
232 return compare_helper(t1 == t2, "Compared values are not the same",
233 toString<T>(t1), toString<T>(t2), actual, expected, file, line);
236 Q_TESTLIB_EXPORT bool qCompare(float const &t1, float const &t2,
237 const char *actual, const char *expected, const char *file, int line);
239 Q_TESTLIB_EXPORT bool qCompare(double const &t1, double const &t2,
240 const char *actual, const char *expected, const char *file, int line);
242 inline bool compare_ptr_helper(const void *t1, const void *t2, const char *actual,
243 const char *expected, const char *file, int line)
245 return compare_helper(t1 == t2, "Compared pointers are not the same",
246 toString(t1), toString(t2), actual, expected, file, line);
249 Q_TESTLIB_EXPORT bool compare_string_helper(const char *t1, const char *t2, const char *actual,
250 const char *expected, const char *file, int line);
253 QTEST_COMPARE_DECL(short)
254 QTEST_COMPARE_DECL(ushort)
255 QTEST_COMPARE_DECL(int)
256 QTEST_COMPARE_DECL(uint)
257 QTEST_COMPARE_DECL(long)
258 QTEST_COMPARE_DECL(ulong)
259 QTEST_COMPARE_DECL(qint64)
260 QTEST_COMPARE_DECL(quint64)
262 QTEST_COMPARE_DECL(float)
263 QTEST_COMPARE_DECL(double)
264 QTEST_COMPARE_DECL(char)
265 QTEST_COMPARE_DECL(bool)
268 template <typename T1, typename T2>
269 bool qCompare(T1 const &, T2 const &, const char *, const char *, const char *, int);
271 inline bool qCompare(double const &t1, float const &t2, const char *actual,
272 const char *expected, const char *file, int line)
274 return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
277 inline bool qCompare(float const &t1, double const &t2, const char *actual,
278 const char *expected, const char *file, int line)
280 return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
283 template <typename T>
284 inline bool qCompare(const T *t1, const T *t2, const char *actual, const char *expected,
285 const char *file, int line)
287 return compare_ptr_helper(t1, t2, actual, expected, file, line);
289 template <typename T>
290 inline bool qCompare(T *t1, T *t2, const char *actual, const char *expected,
291 const char *file, int line)
293 return compare_ptr_helper(t1, t2, actual, expected, file, line);
296 template <typename T1, typename T2>
297 inline bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected,
298 const char *file, int line)
300 return compare_ptr_helper(t1, static_cast<const T1 *>(t2), actual, expected, file, line);
302 template <typename T1, typename T2>
303 inline bool qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected,
304 const char *file, int line)
306 return compare_ptr_helper(const_cast<const T1 *>(t1),
307 static_cast<const T1 *>(const_cast<const T2 *>(t2)), actual, expected, file, line);
309 inline bool qCompare(const char *t1, const char *t2, const char *actual,
310 const char *expected, const char *file, int line)
312 return compare_string_helper(t1, t2, actual, expected, file, line);
314 inline bool qCompare(char *t1, char *t2, const char *actual, const char *expected,
315 const char *file, int line)
317 return compare_string_helper(t1, t2, actual, expected, file, line);
320 /* The next two overloads are for MSVC that shows problems with implicit
323 inline bool qCompare(char *t1, const char *t2, const char *actual,
324 const char *expected, const char *file, int line)
326 return compare_string_helper(t1, t2, actual, expected, file, line);
328 inline bool qCompare(const char *t1, char *t2, const char *actual,
329 const char *expected, const char *file, int line)
331 return compare_string_helper(t1, t2, actual, expected, file, line);
334 // NokiaX86 and RVCT do not like implicitly comparing bool with int
335 inline bool qCompare(bool const &t1, int const &t2,
336 const char *actual, const char *expected, const char *file, int line)
338 return qCompare(int(t1), t2, actual, expected, file, line);
343 inline bool qTest(const T& actual, const char *elementName, const char *actualStr,
344 const char *expected, const char *file, int line)
346 return qCompare(actual, *static_cast<const T *>(QTest::qElementData(elementName,
347 qMetaTypeId<T>())), actualStr, expected, file, line);
351 #undef QTEST_COMPARE_DECL