Kernel: Fix gcc warning in qsharedmemory_win.cpp
[profile/ivi/qtbase.git] / src / testlib / qtestcase.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtTest module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QTESTCASE_H
43 #define QTESTCASE_H
44
45 #include <QtTest/qtest_global.h>
46
47 #include <QtCore/qstring.h>
48 #include <QtCore/qnamespace.h>
49 #include <QtCore/qmetatype.h>
50
51 #include <string.h>
52
53 QT_BEGIN_HEADER
54
55 QT_BEGIN_NAMESPACE
56
57
58 #define QVERIFY(statement) \
59 do {\
60     if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\
61         return;\
62 } while (0)
63
64 #define QFAIL(message) \
65 do {\
66     QTest::qFail(message, __FILE__, __LINE__);\
67     return;\
68 } while (0)
69
70 #define QVERIFY2(statement, description) \
71 do {\
72     if (statement) {\
73         if (!QTest::qVerify(true, #statement, (description), __FILE__, __LINE__))\
74             return;\
75     } else {\
76         if (!QTest::qVerify(false, #statement, (description), __FILE__, __LINE__))\
77             return;\
78     }\
79 } while (0)
80
81 #define QCOMPARE(actual, expected) \
82 do {\
83     if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\
84         return;\
85 } while (0)
86
87 // Will try to wait for the expression to become true while allowing event processing
88 #define QTRY_VERIFY_WITH_TIMEOUT(__expr, __timeout) \
89 do { \
90     const int __step = 50; \
91     const int __timeoutValue = __timeout; \
92     if (!(__expr)) { \
93         QTest::qWait(0); \
94     } \
95     for (int __i = 0; __i < __timeoutValue && !(__expr); __i+=__step) { \
96         QTest::qWait(__step); \
97     } \
98     QVERIFY(__expr); \
99 } while (0)
100
101 #define QTRY_VERIFY(__expr) QTRY_VERIFY_WITH_TIMEOUT(__expr, 5000)
102
103 // Will try to wait for the comparison to become successful while allowing event processing
104
105 #define QTRY_COMPARE_WITH_TIMEOUT(__expr, __expected, __timeout) \
106 do { \
107     const int __step = 50; \
108     const int __timeoutValue = __timeout; \
109     if ((__expr) != (__expected)) { \
110         QTest::qWait(0); \
111     } \
112     for (int __i = 0; __i < __timeoutValue && ((__expr) != (__expected)); __i+=__step) { \
113         QTest::qWait(__step); \
114     } \
115     QCOMPARE(__expr, __expected); \
116 } while (0)
117
118 #define QTRY_COMPARE(__expr, __expected) QTRY_COMPARE_WITH_TIMEOUT(__expr, __expected, 5000)
119
120 #define QSKIP_INTERNAL(statement) \
121 do {\
122     QTest::qSkip(statement, __FILE__, __LINE__);\
123     return;\
124 } while (0)
125
126 #ifdef Q_COMPILER_VARIADIC_MACROS
127
128 #define QSKIP(statement, ...) QSKIP_INTERNAL(statement)
129
130 #else
131
132 #define QSKIP(statement) QSKIP_INTERNAL(statement)
133
134 #endif
135
136 #define QEXPECT_FAIL(dataIndex, comment, mode)\
137 do {\
138     if (!QTest::qExpectFail(dataIndex, comment, QTest::mode, __FILE__, __LINE__))\
139         return;\
140 } while (0)
141
142 #define QFETCH(type, name)\
143     type name = *static_cast<type *>(QTest::qData(#name, ::qMetaTypeId<type >()))
144
145 #define QFETCH_GLOBAL(type, name)\
146     type name = *static_cast<type *>(QTest::qGlobalData(#name, ::qMetaTypeId<type >()))
147
148 #define QTEST(actual, testElement)\
149 do {\
150     if (!QTest::qTest(actual, testElement, #actual, #testElement, __FILE__, __LINE__))\
151         return;\
152 } while (0)
153
154 #define QWARN(msg)\
155     QTest::qWarn(msg, __FILE__, __LINE__)
156
157 #ifdef QT_TESTCASE_BUILDDIR
158 # define QFINDTESTDATA(basepath)\
159     QTest::qFindTestData(basepath, __FILE__, __LINE__, QT_TESTCASE_BUILDDIR)
160 #else
161 # define QFINDTESTDATA(basepath)\
162     QTest::qFindTestData(basepath, __FILE__, __LINE__)
163 #endif
164
165 class QObject;
166 class QTestData;
167
168 #define QTEST_COMPARE_DECL(KLASS)\
169     template<> Q_TESTLIB_EXPORT char *toString<KLASS >(const KLASS &);
170
171 namespace QTest
172 {
173     template <typename T>
174     inline char *toString(const T &)
175     {
176         return 0;
177     }
178
179
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 *);
183
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);
186
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);
195
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);
198
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();
203
204     Q_TESTLIB_EXPORT const char *currentTestFunction();
205     Q_TESTLIB_EXPORT const char *currentDataTag();
206     Q_TESTLIB_EXPORT bool currentTestFailed();
207
208     Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii);
209     Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key);
210
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);
217
218     template <typename T>
219     inline void addColumn(const char *name, T * = 0)
220     {
221         addColumnInternal(qMetaTypeId<T>(), name);
222     }
223     Q_TESTLIB_EXPORT QTestData &newRow(const char *dataTag);
224
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)
228     {
229         return compare_helper(t1 == t2, "Compared values are not the same",
230                               toString<T>(t1), toString<T>(t2), actual, expected, file, line);
231     }
232
233     template <>
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);
236
237     template <>
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);
240
241     inline bool compare_ptr_helper(const void *t1, const void *t2, const char *actual,
242                                    const char *expected, const char *file, int line)
243     {
244         return compare_helper(t1 == t2, "Compared pointers are not the same",
245                               toString(t1), toString(t2), actual, expected, file, line);
246     }
247
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);
250
251 #ifndef qdoc
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)
260
261     QTEST_COMPARE_DECL(float)
262     QTEST_COMPARE_DECL(double)
263     QTEST_COMPARE_DECL(char)
264     QTEST_COMPARE_DECL(bool)
265 #endif
266
267     template <typename T1, typename T2>
268     bool qCompare(T1 const &, T2 const &, const char *, const char *, const char *, int);
269
270     template <>
271     inline bool qCompare<double, float>(double const &t1, float const &t2, const char *actual,
272                                  const char *expected, const char *file, int line)
273     {
274         return qCompare<qreal>(qreal(t1), qreal(t2), actual, expected, file, line);
275     }
276
277     template <>
278     inline bool qCompare<float, double>(float const &t1, double const &t2, const char *actual,
279                                  const char *expected, const char *file, int line)
280     {
281         return qCompare<qreal>(qreal(t1), qreal(t2), actual, expected, file, line);
282     }
283
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)
287     {
288         return compare_ptr_helper(t1, t2, actual, expected, file, line);
289     }
290     template <typename T>
291     inline bool qCompare(T *t1, T *t2, const char *actual, const char *expected,
292                         const char *file, int line)
293     {
294         return compare_ptr_helper(t1, t2, actual, expected, file, line);
295     }
296
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)
300     {
301         return compare_ptr_helper(t1, static_cast<const T1 *>(t2), actual, expected, file, line);
302     }
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)
306     {
307         return compare_ptr_helper(const_cast<const T1 *>(t1),
308                 static_cast<const T1 *>(const_cast<const T2 *>(t2)), actual, expected, file, line);
309     }
310     template<>
311     inline bool qCompare<char>(const char *t1, const char *t2, const char *actual,
312                                        const char *expected, const char *file, int line)
313     {
314         return compare_string_helper(t1, t2, actual, expected, file, line);
315     }
316     template<>
317     inline bool qCompare<char>(char *t1, char *t2, const char *actual, const char *expected,
318                         const char *file, int line)
319     {
320         return compare_string_helper(t1, t2, actual, expected, file, line);
321     }
322
323     /* The next two specializations are for MSVC that shows problems with implicit
324        conversions
325      */
326     template<>
327     inline bool qCompare(char *t1, const char *t2, const char *actual,
328                          const char *expected, const char *file, int line)
329     {
330         return compare_string_helper(t1, t2, actual, expected, file, line);
331     }
332     template<>
333     inline bool qCompare(const char *t1, char *t2, const char *actual,
334                          const char *expected, const char *file, int line)
335     {
336         return compare_string_helper(t1, t2, actual, expected, file, line);
337     }
338
339     // NokiaX86 and RVCT do not like implicitly comparing bool with int
340     template <>
341     inline bool qCompare(bool const &t1, int const &t2,
342                     const char *actual, const char *expected, const char *file, int line)
343     {
344         return qCompare<int>(int(t1), t2, actual, expected, file, line);
345     }
346
347
348     template <class T>
349     inline bool qTest(const T& actual, const char *elementName, const char *actualStr,
350                      const char *expected, const char *file, int line)
351     {
352         return qCompare(actual, *static_cast<const T *>(QTest::qElementData(elementName,
353                        qMetaTypeId<T>())), actualStr, expected, file, line);
354     }
355 }
356
357 #undef QTEST_COMPARE_DECL
358
359 QT_END_NAMESPACE
360
361 QT_END_HEADER
362
363 #endif