~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/testlib/qtestcase.h

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

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